@@ -59,10 +59,14 @@ interface ToCompile {
59
59
60
60
let dummySandbox ;
61
61
62
+ function getActiveEditorFilePath ( ) : string | undefined {
63
+ return vscode . window . activeTextEditor ?. document . uri . fsPath ;
64
+ }
65
+
62
66
// TODO: this function was copied from the compass-export-to-language module
63
67
// https://github.com/mongodb-js/compass/blob/7c4bc0789a7b66c01bb7ba63955b3b11ed40c094/packages/compass-export-to-language/src/modules/count-aggregation-stages-in-string.js
64
68
// 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 => {
66
70
if ( ! dummySandbox ) {
67
71
dummySandbox = vm . createContext ( Object . create ( null ) , {
68
72
codeGeneration : { strings : false , wasm : false } ,
@@ -112,6 +116,9 @@ const exportModeMapping: Record<
112
116
[ ExportToLanguageMode . OTHER ] : undefined ,
113
117
} ;
114
118
119
+ const connectBeforeRunningMessage =
120
+ 'Please connect to a database before running a playground.' ;
121
+
115
122
/**
116
123
* This controller manages playground.
117
124
*/
@@ -160,7 +167,7 @@ export default class PlaygroundController {
160
167
this . _playgroundSelectedCodeActionProvider =
161
168
playgroundSelectedCodeActionProvider ;
162
169
163
- this . _activeConnectionChangedHandler = ( ) => {
170
+ this . _activeConnectionChangedHandler = ( ) : void => {
164
171
void this . _activeConnectionChanged ( ) ;
165
172
} ;
166
173
this . _connectionController . addEventListener (
@@ -170,7 +177,7 @@ export default class PlaygroundController {
170
177
171
178
const onDidChangeActiveTextEditor = (
172
179
editor : vscode . TextEditor | undefined
173
- ) => {
180
+ ) : void => {
174
181
if ( editor ?. document . uri . scheme === PLAYGROUND_RESULT_SCHEME ) {
175
182
this . _playgroundResultViewColumn = editor . viewColumn ;
176
183
this . _playgroundResultTextDocument = editor ?. document ;
@@ -373,7 +380,7 @@ export default class PlaygroundController {
373
380
return this . _createPlaygroundFileWithContent ( content ) ;
374
381
}
375
382
376
- createPlaygroundFromParticipantQuery ( {
383
+ createPlaygroundFromParticipantCode ( {
377
384
text,
378
385
} : {
379
386
text : string ;
@@ -438,13 +445,17 @@ export default class PlaygroundController {
438
445
return this . _createPlaygroundFileWithContent ( content ) ;
439
446
}
440
447
441
- async _evaluate ( codeToEvaluate : string ) : Promise < ShellEvaluateResult > {
448
+ async _evaluate ( {
449
+ codeToEvaluate,
450
+ filePath,
451
+ } : {
452
+ codeToEvaluate : string ;
453
+ filePath ?: string ;
454
+ } ) : Promise < ShellEvaluateResult > {
442
455
const connectionId = this . _connectionController . getActiveConnectionId ( ) ;
443
456
444
457
if ( ! connectionId ) {
445
- throw new Error (
446
- 'Please connect to a database before running a playground.'
447
- ) ;
458
+ throw new Error ( connectBeforeRunningMessage ) ;
448
459
}
449
460
450
461
this . _statusView . showMessage ( 'Getting results...' ) ;
@@ -455,7 +466,7 @@ export default class PlaygroundController {
455
466
result = await this . _languageServerController . evaluate ( {
456
467
codeToEvaluate,
457
468
connectionId,
458
- filePath : vscode . window . activeTextEditor ?. document . uri . fsPath ,
469
+ filePath,
459
470
} ) ;
460
471
} catch ( error ) {
461
472
const msg =
@@ -482,13 +493,15 @@ export default class PlaygroundController {
482
493
return this . _activeTextEditor ?. document . getText ( selection ) || '' ;
483
494
}
484
495
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 > {
488
503
if ( ! this . _connectionController . isCurrentlyConnected ( ) ) {
489
- throw new Error (
490
- 'Please connect to a database before running a playground.'
491
- ) ;
504
+ throw new Error ( connectBeforeRunningMessage ) ;
492
505
}
493
506
494
507
try {
@@ -507,9 +520,10 @@ export default class PlaygroundController {
507
520
} ) ;
508
521
509
522
// 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
+ } ) ;
513
527
514
528
return result ;
515
529
}
@@ -572,11 +586,18 @@ export default class PlaygroundController {
572
586
}
573
587
}
574
588
575
- async evaluateParticipantQuery ( text : string ) : Promise < boolean > {
589
+ async evaluateParticipantCode ( codeToEvaluate : string ) : Promise < boolean > {
576
590
const shouldConfirmRunCopilotCode = vscode . workspace
577
591
. getConfiguration ( 'mdb' )
578
592
. get ( 'confirmRunCopilotCode' ) ;
579
593
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
+
580
601
if ( shouldConfirmRunCopilotCode === true ) {
581
602
const name = this . _connectionController . getActiveConnectionName ( ) ;
582
603
const confirmRunCopilotCode = await vscode . window . showInformationMessage (
@@ -591,7 +612,9 @@ export default class PlaygroundController {
591
612
}
592
613
593
614
const evaluateResponse : ShellEvaluateResult =
594
- await this . _evaluateWithCancelModal ( text ) ;
615
+ await this . _evaluateWithCancelModal ( {
616
+ codeToEvaluate,
617
+ } ) ;
595
618
596
619
if ( ! evaluateResponse || ! evaluateResponse . result ) {
597
620
return false ;
@@ -602,15 +625,19 @@ export default class PlaygroundController {
602
625
return true ;
603
626
}
604
627
605
- async _evaluatePlayground ( text : string ) : Promise < boolean > {
628
+ async _evaluatePlayground ( {
629
+ codeToEvaluate,
630
+ filePath,
631
+ } : {
632
+ codeToEvaluate : string ;
633
+ filePath ?: string ;
634
+ } ) : Promise < boolean > {
606
635
const shouldConfirmRunAll = vscode . workspace
607
636
. getConfiguration ( 'mdb' )
608
637
. get ( 'confirmRunAll' ) ;
609
638
610
639
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 ) ;
614
641
615
642
return false ;
616
643
}
@@ -629,7 +656,10 @@ export default class PlaygroundController {
629
656
}
630
657
631
658
const evaluateResponse : ShellEvaluateResult =
632
- await this . _evaluateWithCancelModal ( text ) ;
659
+ await this . _evaluateWithCancelModal ( {
660
+ codeToEvaluate,
661
+ filePath,
662
+ } ) ;
633
663
634
664
if ( ! evaluateResponse || ! evaluateResponse . result ) {
635
665
return false ;
@@ -652,7 +682,10 @@ export default class PlaygroundController {
652
682
653
683
this . _isPartialRun = true ;
654
684
655
- return this . _evaluatePlayground ( this . _selectedText || '' ) ;
685
+ return this . _evaluatePlayground ( {
686
+ codeToEvaluate : this . _selectedText || '' ,
687
+ filePath : getActiveEditorFilePath ( ) ,
688
+ } ) ;
656
689
}
657
690
658
691
runAllPlaygroundBlocks ( ) : Promise < boolean > {
@@ -669,7 +702,10 @@ export default class PlaygroundController {
669
702
670
703
this . _isPartialRun = false ;
671
704
672
- return this . _evaluatePlayground ( this . _getAllText ( ) ) ;
705
+ return this . _evaluatePlayground ( {
706
+ codeToEvaluate : this . _getAllText ( ) ,
707
+ filePath : getActiveEditorFilePath ( ) ,
708
+ } ) ;
673
709
}
674
710
675
711
runAllOrSelectedPlaygroundBlocks ( ) : Promise < boolean > {
@@ -693,14 +729,17 @@ export default class PlaygroundController {
693
729
codeToEvaluate = this . _selectedText ;
694
730
}
695
731
696
- return this . _evaluatePlayground ( codeToEvaluate ) ;
732
+ return this . _evaluatePlayground ( {
733
+ codeToEvaluate,
734
+ filePath : getActiveEditorFilePath ( ) ,
735
+ } ) ;
697
736
}
698
737
699
738
async fixThisInvalidInteractiveSyntax ( {
700
739
documentUri,
701
740
range,
702
741
fix,
703
- } : ThisDiagnosticFix ) {
742
+ } : ThisDiagnosticFix ) : Promise < boolean > {
704
743
const edit = new vscode . WorkspaceEdit ( ) ;
705
744
edit . replace ( documentUri , range , fix ) ;
706
745
await vscode . workspace . applyEdit ( edit ) ;
@@ -710,7 +749,7 @@ export default class PlaygroundController {
710
749
async fixAllInvalidInteractiveSyntax ( {
711
750
documentUri,
712
751
diagnostics,
713
- } : AllDiagnosticFixes ) {
752
+ } : AllDiagnosticFixes ) : Promise < boolean > {
714
753
const edit = new vscode . WorkspaceEdit ( ) ;
715
754
716
755
for ( const { range, fix } of diagnostics ) {
@@ -884,7 +923,7 @@ export default class PlaygroundController {
884
923
language,
885
924
num_stages : selectedText
886
925
? countAggregationStagesInString ( selectedText )
887
- : null ,
926
+ : undefined ,
888
927
with_import_statements : importStatements ,
889
928
with_builders : builders ,
890
929
with_driver_syntax : driverSyntax ,
0 commit comments