Skip to content

Commit 547e455

Browse files
authored
fix(chat): avoid passing file path when its not used in playground run (#844)
1 parent 0a5a76f commit 547e455

File tree

8 files changed

+98
-57
lines changed

8 files changed

+98
-57
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@
192192
"title": "MongoDB: Change Active Connection with Participant"
193193
},
194194
{
195-
"command": "mdb.runParticipantQuery",
195+
"command": "mdb.runParticipantCode",
196196
"title": "Run Content Generated by Participant"
197197
},
198198
{
199-
"command": "mdb.openParticipantQueryInPlayground",
199+
"command": "mdb.openParticipantCodeInPlayground",
200200
"title": "Open Generated by Participant Content In Playground"
201201
},
202202
{
@@ -766,15 +766,15 @@
766766
"when": "false"
767767
},
768768
{
769-
"command": "mdb.runParticipantQuery",
769+
"command": "mdb.runParticipantCode",
770770
"when": "false"
771771
},
772772
{
773-
"command": "mdb.openParticipantQueryInPlayground",
773+
"command": "mdb.openParticipantCodeInPlayground",
774774
"when": "false"
775775
},
776776
{
777-
"command": "mdb.runParticipantQuery",
777+
"command": "mdb.runParticipantCode",
778778
"when": "false"
779779
},
780780
{

src/commands/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ enum EXTENSION_COMMANDS {
7777
MDB_DROP_STREAM_PROCESSOR = 'mdb.dropStreamProcessor',
7878

7979
// Chat participant.
80-
OPEN_PARTICIPANT_QUERY_IN_PLAYGROUND = 'mdb.openParticipantQueryInPlayground',
81-
RUN_PARTICIPANT_QUERY = 'mdb.runParticipantQuery',
80+
OPEN_PARTICIPANT_CODE_IN_PLAYGROUND = 'mdb.openParticipantCodeInPlayground',
81+
RUN_PARTICIPANT_CODE = 'mdb.runParticipantCode',
8282
CONNECT_WITH_PARTICIPANT = 'mdb.connectWithParticipant',
8383
SELECT_DATABASE_WITH_PARTICIPANT = 'mdb.selectDatabaseWithParticipant',
8484
SELECT_COLLECTION_WITH_PARTICIPANT = 'mdb.selectCollectionWithParticipant',

src/editors/playgroundController.ts

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ interface ToCompile {
5959

6060
let 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,

src/mdbExtensionController.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import { ConnectionStorage } from './storage/connectionStorage';
4444
import type StreamProcessorTreeItem from './explorer/streamProcessorTreeItem';
4545
import type {
4646
ParticipantCommand,
47-
RunParticipantQueryCommandArgs,
47+
RunParticipantCodeCommandArgs,
4848
} from './participant/participant';
4949
import ParticipantController from './participant/participant';
5050
import type { OpenSchemaCommandArgs } from './participant/prompts/schema';
@@ -296,17 +296,17 @@ export default class MDBExtensionController implements vscode.Disposable {
296296

297297
// ------ CHAT PARTICIPANT ------ //
298298
this.registerParticipantCommand(
299-
EXTENSION_COMMANDS.OPEN_PARTICIPANT_QUERY_IN_PLAYGROUND,
300-
({ runnableContent }: RunParticipantQueryCommandArgs) => {
301-
return this._playgroundController.createPlaygroundFromParticipantQuery({
299+
EXTENSION_COMMANDS.OPEN_PARTICIPANT_CODE_IN_PLAYGROUND,
300+
({ runnableContent }: RunParticipantCodeCommandArgs) => {
301+
return this._playgroundController.createPlaygroundFromParticipantCode({
302302
text: runnableContent,
303303
});
304304
}
305305
);
306306
this.registerParticipantCommand(
307-
EXTENSION_COMMANDS.RUN_PARTICIPANT_QUERY,
308-
({ runnableContent }: RunParticipantQueryCommandArgs) => {
309-
return this._playgroundController.evaluateParticipantQuery(
307+
EXTENSION_COMMANDS.RUN_PARTICIPANT_CODE,
308+
({ runnableContent }: RunParticipantCodeCommandArgs) => {
309+
return this._playgroundController.evaluateParticipantCode(
310310
runnableContent
311311
);
312312
}

src/participant/participant.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ interface NamespaceQuickPicks {
5555
data: string;
5656
}
5757

58-
export type RunParticipantQueryCommandArgs = {
58+
export type RunParticipantCodeCommandArgs = {
5959
runnableContent: string;
6060
};
6161

@@ -244,16 +244,16 @@ export default class ParticipantController {
244244
return;
245245
}
246246

247-
const commandArgs: RunParticipantQueryCommandArgs = {
247+
const commandArgs: RunParticipantCodeCommandArgs = {
248248
runnableContent,
249249
};
250250
stream.button({
251-
command: EXTENSION_COMMANDS.RUN_PARTICIPANT_QUERY,
251+
command: EXTENSION_COMMANDS.RUN_PARTICIPANT_CODE,
252252
title: vscode.l10n.t('▶️ Run'),
253253
arguments: [commandArgs],
254254
});
255255
stream.button({
256-
command: EXTENSION_COMMANDS.OPEN_PARTICIPANT_QUERY_IN_PLAYGROUND,
256+
command: EXTENSION_COMMANDS.OPEN_PARTICIPANT_CODE_IN_PLAYGROUND,
257257
title: vscode.l10n.t('Open in playground'),
258258
arguments: [commandArgs],
259259
});

src/test/suite/editors/playgroundController.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@ suite('Playground Controller Test Suite', function () {
324324
sandbox.fake.rejects(false)
325325
);
326326

327-
const result = await testPlaygroundController._evaluateWithCancelModal(
328-
''
329-
);
327+
const result = await testPlaygroundController._evaluateWithCancelModal({
328+
codeToEvaluate: '',
329+
});
330330

331331
expect(result).to.deep.equal({ result: undefined });
332332
});

0 commit comments

Comments
 (0)