@@ -59,10 +59,14 @@ interface ToCompile {
5959
6060let dummySandbox ;
6161
62+ function getActiveEditorFilePath ( ) : string | undefined {
63+ return vscode . window . activeTextEditor ?. document . uri . fsPath ;
64+ }
65+
6266// TODO: this function was copied from the compass-export-to-language module
6367// https://github.com/mongodb-js/compass/blob/7c4bc0789a7b66c01bb7ba63955b3b11ed40c094/packages/compass-export-to-language/src/modules/count-aggregation-stages-in-string.js
6468// and should be updated as well when the better solution for the problem will be found.
65- const countAggregationStagesInString = ( str : string ) => {
69+ const countAggregationStagesInString = ( str : string ) : number => {
6670 if ( ! dummySandbox ) {
6771 dummySandbox = vm . createContext ( Object . create ( null ) , {
6872 codeGeneration : { strings : false , wasm : false } ,
@@ -112,6 +116,9 @@ const exportModeMapping: Record<
112116 [ ExportToLanguageMode . OTHER ] : undefined ,
113117} ;
114118
119+ const connectBeforeRunningMessage =
120+ 'Please connect to a database before running a playground.' ;
121+
115122/**
116123 * This controller manages playground.
117124 */
@@ -160,7 +167,7 @@ export default class PlaygroundController {
160167 this . _playgroundSelectedCodeActionProvider =
161168 playgroundSelectedCodeActionProvider ;
162169
163- this . _activeConnectionChangedHandler = ( ) => {
170+ this . _activeConnectionChangedHandler = ( ) : void => {
164171 void this . _activeConnectionChanged ( ) ;
165172 } ;
166173 this . _connectionController . addEventListener (
@@ -170,7 +177,7 @@ export default class PlaygroundController {
170177
171178 const onDidChangeActiveTextEditor = (
172179 editor : vscode . TextEditor | undefined
173- ) => {
180+ ) : void => {
174181 if ( editor ?. document . uri . scheme === PLAYGROUND_RESULT_SCHEME ) {
175182 this . _playgroundResultViewColumn = editor . viewColumn ;
176183 this . _playgroundResultTextDocument = editor ?. document ;
@@ -373,7 +380,7 @@ export default class PlaygroundController {
373380 return this . _createPlaygroundFileWithContent ( content ) ;
374381 }
375382
376- createPlaygroundFromParticipantQuery ( {
383+ createPlaygroundFromParticipantCode ( {
377384 text,
378385 } : {
379386 text : string ;
@@ -438,13 +445,17 @@ export default class PlaygroundController {
438445 return this . _createPlaygroundFileWithContent ( content ) ;
439446 }
440447
441- async _evaluate ( codeToEvaluate : string ) : Promise < ShellEvaluateResult > {
448+ async _evaluate ( {
449+ codeToEvaluate,
450+ filePath,
451+ } : {
452+ codeToEvaluate : string ;
453+ filePath ?: string ;
454+ } ) : Promise < ShellEvaluateResult > {
442455 const connectionId = this . _connectionController . getActiveConnectionId ( ) ;
443456
444457 if ( ! connectionId ) {
445- throw new Error (
446- 'Please connect to a database before running a playground.'
447- ) ;
458+ throw new Error ( connectBeforeRunningMessage ) ;
448459 }
449460
450461 this . _statusView . showMessage ( 'Getting results...' ) ;
@@ -455,7 +466,7 @@ export default class PlaygroundController {
455466 result = await this . _languageServerController . evaluate ( {
456467 codeToEvaluate,
457468 connectionId,
458- filePath : vscode . window . activeTextEditor ?. document . uri . fsPath ,
469+ filePath,
459470 } ) ;
460471 } catch ( error ) {
461472 const msg =
@@ -482,13 +493,15 @@ export default class PlaygroundController {
482493 return this . _activeTextEditor ?. document . getText ( selection ) || '' ;
483494 }
484495
485- async _evaluateWithCancelModal (
486- codeToEvaluate : string
487- ) : Promise < ShellEvaluateResult > {
496+ async _evaluateWithCancelModal ( {
497+ codeToEvaluate,
498+ filePath,
499+ } : {
500+ codeToEvaluate : string ;
501+ filePath ?: string ;
502+ } ) : Promise < ShellEvaluateResult > {
488503 if ( ! this . _connectionController . isCurrentlyConnected ( ) ) {
489- throw new Error (
490- 'Please connect to a database before running a playground.'
491- ) ;
504+ throw new Error ( connectBeforeRunningMessage ) ;
492505 }
493506
494507 try {
@@ -507,9 +520,10 @@ export default class PlaygroundController {
507520 } ) ;
508521
509522 // Run all playground scripts.
510- const result : ShellEvaluateResult = await this . _evaluate (
511- codeToEvaluate
512- ) ;
523+ const result : ShellEvaluateResult = await this . _evaluate ( {
524+ codeToEvaluate,
525+ filePath,
526+ } ) ;
513527
514528 return result ;
515529 }
@@ -572,11 +586,18 @@ export default class PlaygroundController {
572586 }
573587 }
574588
575- async evaluateParticipantQuery ( text : string ) : Promise < boolean > {
589+ async evaluateParticipantCode ( codeToEvaluate : string ) : Promise < boolean > {
576590 const shouldConfirmRunCopilotCode = vscode . workspace
577591 . getConfiguration ( 'mdb' )
578592 . get ( 'confirmRunCopilotCode' ) ;
579593
594+ if ( ! this . _connectionController . isCurrentlyConnected ( ) ) {
595+ // TODO(VSCODE-618): Prompt user to connect when clicked.
596+ void vscode . window . showErrorMessage ( connectBeforeRunningMessage ) ;
597+
598+ return false ;
599+ }
600+
580601 if ( shouldConfirmRunCopilotCode === true ) {
581602 const name = this . _connectionController . getActiveConnectionName ( ) ;
582603 const confirmRunCopilotCode = await vscode . window . showInformationMessage (
@@ -591,7 +612,9 @@ export default class PlaygroundController {
591612 }
592613
593614 const evaluateResponse : ShellEvaluateResult =
594- await this . _evaluateWithCancelModal ( text ) ;
615+ await this . _evaluateWithCancelModal ( {
616+ codeToEvaluate,
617+ } ) ;
595618
596619 if ( ! evaluateResponse || ! evaluateResponse . result ) {
597620 return false ;
@@ -602,15 +625,19 @@ export default class PlaygroundController {
602625 return true ;
603626 }
604627
605- async _evaluatePlayground ( text : string ) : Promise < boolean > {
628+ async _evaluatePlayground ( {
629+ codeToEvaluate,
630+ filePath,
631+ } : {
632+ codeToEvaluate : string ;
633+ filePath ?: string ;
634+ } ) : Promise < boolean > {
606635 const shouldConfirmRunAll = vscode . workspace
607636 . getConfiguration ( 'mdb' )
608637 . get ( 'confirmRunAll' ) ;
609638
610639 if ( ! this . _connectionController . isCurrentlyConnected ( ) ) {
611- void vscode . window . showErrorMessage (
612- 'Please connect to a database before running a playground.'
613- ) ;
640+ void vscode . window . showErrorMessage ( connectBeforeRunningMessage ) ;
614641
615642 return false ;
616643 }
@@ -629,7 +656,10 @@ export default class PlaygroundController {
629656 }
630657
631658 const evaluateResponse : ShellEvaluateResult =
632- await this . _evaluateWithCancelModal ( text ) ;
659+ await this . _evaluateWithCancelModal ( {
660+ codeToEvaluate,
661+ filePath,
662+ } ) ;
633663
634664 if ( ! evaluateResponse || ! evaluateResponse . result ) {
635665 return false ;
@@ -652,7 +682,10 @@ export default class PlaygroundController {
652682
653683 this . _isPartialRun = true ;
654684
655- return this . _evaluatePlayground ( this . _selectedText || '' ) ;
685+ return this . _evaluatePlayground ( {
686+ codeToEvaluate : this . _selectedText || '' ,
687+ filePath : getActiveEditorFilePath ( ) ,
688+ } ) ;
656689 }
657690
658691 runAllPlaygroundBlocks ( ) : Promise < boolean > {
@@ -669,7 +702,10 @@ export default class PlaygroundController {
669702
670703 this . _isPartialRun = false ;
671704
672- return this . _evaluatePlayground ( this . _getAllText ( ) ) ;
705+ return this . _evaluatePlayground ( {
706+ codeToEvaluate : this . _getAllText ( ) ,
707+ filePath : getActiveEditorFilePath ( ) ,
708+ } ) ;
673709 }
674710
675711 runAllOrSelectedPlaygroundBlocks ( ) : Promise < boolean > {
@@ -693,14 +729,17 @@ export default class PlaygroundController {
693729 codeToEvaluate = this . _selectedText ;
694730 }
695731
696- return this . _evaluatePlayground ( codeToEvaluate ) ;
732+ return this . _evaluatePlayground ( {
733+ codeToEvaluate,
734+ filePath : getActiveEditorFilePath ( ) ,
735+ } ) ;
697736 }
698737
699738 async fixThisInvalidInteractiveSyntax ( {
700739 documentUri,
701740 range,
702741 fix,
703- } : ThisDiagnosticFix ) {
742+ } : ThisDiagnosticFix ) : Promise < boolean > {
704743 const edit = new vscode . WorkspaceEdit ( ) ;
705744 edit . replace ( documentUri , range , fix ) ;
706745 await vscode . workspace . applyEdit ( edit ) ;
@@ -710,7 +749,7 @@ export default class PlaygroundController {
710749 async fixAllInvalidInteractiveSyntax ( {
711750 documentUri,
712751 diagnostics,
713- } : AllDiagnosticFixes ) {
752+ } : AllDiagnosticFixes ) : Promise < boolean > {
714753 const edit = new vscode . WorkspaceEdit ( ) ;
715754
716755 for ( const { range, fix } of diagnostics ) {
@@ -884,7 +923,7 @@ export default class PlaygroundController {
884923 language,
885924 num_stages : selectedText
886925 ? countAggregationStagesInString ( selectedText )
887- : null ,
926+ : undefined ,
888927 with_import_statements : importStatements ,
889928 with_builders : builders ,
890929 with_driver_syntax : driverSyntax ,
0 commit comments