Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(chat): avoid passing file path when its not used in playground run VSCODE-625 #844

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@
"title": "MongoDB: Change Active Connection with Participant"
},
{
"command": "mdb.runParticipantQuery",
"command": "mdb.runParticipantCode",
"title": "Run Content Generated by Participant"
},
{
"command": "mdb.openParticipantQueryInPlayground",
"command": "mdb.openParticipantCodeInPlayground",
"title": "Open Generated by Participant Content In Playground"
},
{
Expand Down Expand Up @@ -765,15 +765,15 @@
"when": "false"
},
{
"command": "mdb.runParticipantQuery",
"command": "mdb.runParticipantCode",
"when": "false"
},
{
"command": "mdb.openParticipantQueryInPlayground",
"command": "mdb.openParticipantCodeInPlayground",
"when": "false"
},
{
"command": "mdb.runParticipantQuery",
"command": "mdb.runParticipantCode",
"when": "false"
},
{
Expand Down
4 changes: 2 additions & 2 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ enum EXTENSION_COMMANDS {
MDB_DROP_STREAM_PROCESSOR = 'mdb.dropStreamProcessor',

// Chat participant.
OPEN_PARTICIPANT_QUERY_IN_PLAYGROUND = 'mdb.openParticipantQueryInPlayground',
RUN_PARTICIPANT_QUERY = 'mdb.runParticipantQuery',
OPEN_PARTICIPANT_CODE_IN_PLAYGROUND = 'mdb.openParticipantCodeInPlayground',
RUN_PARTICIPANT_CODE = 'mdb.runParticipantCode',
CONNECT_WITH_PARTICIPANT = 'mdb.connectWithParticipant',
SELECT_DATABASE_WITH_PARTICIPANT = 'mdb.selectDatabaseWithParticipant',
SELECT_COLLECTION_WITH_PARTICIPANT = 'mdb.selectCollectionWithParticipant',
Expand Down
101 changes: 70 additions & 31 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ interface ToCompile {

let dummySandbox;

function getActiveEditorFilePath(): string | undefined {
return vscode.window.activeTextEditor?.document.uri.fsPath;
}

// TODO: this function was copied from the compass-export-to-language module
// https://github.com/mongodb-js/compass/blob/7c4bc0789a7b66c01bb7ba63955b3b11ed40c094/packages/compass-export-to-language/src/modules/count-aggregation-stages-in-string.js
// and should be updated as well when the better solution for the problem will be found.
const countAggregationStagesInString = (str: string) => {
const countAggregationStagesInString = (str: string): number => {
if (!dummySandbox) {
dummySandbox = vm.createContext(Object.create(null), {
codeGeneration: { strings: false, wasm: false },
Expand Down Expand Up @@ -112,6 +116,9 @@ const exportModeMapping: Record<
[ExportToLanguageMode.OTHER]: undefined,
};

const connectBeforeRunningMessage =
'Please connect to a database before running a playground.';

/**
* This controller manages playground.
*/
Expand Down Expand Up @@ -160,7 +167,7 @@ export default class PlaygroundController {
this._playgroundSelectedCodeActionProvider =
playgroundSelectedCodeActionProvider;

this._activeConnectionChangedHandler = () => {
this._activeConnectionChangedHandler = (): void => {
void this._activeConnectionChanged();
};
this._connectionController.addEventListener(
Expand All @@ -170,7 +177,7 @@ export default class PlaygroundController {

const onDidChangeActiveTextEditor = (
editor: vscode.TextEditor | undefined
) => {
): void => {
if (editor?.document.uri.scheme === PLAYGROUND_RESULT_SCHEME) {
this._playgroundResultViewColumn = editor.viewColumn;
this._playgroundResultTextDocument = editor?.document;
Expand Down Expand Up @@ -373,7 +380,7 @@ export default class PlaygroundController {
return this._createPlaygroundFileWithContent(content);
}

createPlaygroundFromParticipantQuery({
createPlaygroundFromParticipantCode({
text,
}: {
text: string;
Expand Down Expand Up @@ -438,13 +445,17 @@ export default class PlaygroundController {
return this._createPlaygroundFileWithContent(content);
}

async _evaluate(codeToEvaluate: string): Promise<ShellEvaluateResult> {
async _evaluate({
codeToEvaluate,
filePath,
}: {
codeToEvaluate: string;
filePath?: string;
}): Promise<ShellEvaluateResult> {
const connectionId = this._connectionController.getActiveConnectionId();

if (!connectionId) {
throw new Error(
'Please connect to a database before running a playground.'
);
throw new Error(connectBeforeRunningMessage);
}

this._statusView.showMessage('Getting results...');
Expand All @@ -455,7 +466,7 @@ export default class PlaygroundController {
result = await this._languageServerController.evaluate({
codeToEvaluate,
connectionId,
filePath: vscode.window.activeTextEditor?.document.uri.fsPath,
filePath,
});
} catch (error) {
const msg =
Expand All @@ -482,13 +493,15 @@ export default class PlaygroundController {
return this._activeTextEditor?.document.getText(selection) || '';
}

async _evaluateWithCancelModal(
codeToEvaluate: string
): Promise<ShellEvaluateResult> {
async _evaluateWithCancelModal({
codeToEvaluate,
filePath,
}: {
codeToEvaluate: string;
filePath?: string;
}): Promise<ShellEvaluateResult> {
if (!this._connectionController.isCurrentlyConnected()) {
throw new Error(
'Please connect to a database before running a playground.'
);
throw new Error(connectBeforeRunningMessage);
}

try {
Expand All @@ -507,9 +520,10 @@ export default class PlaygroundController {
});

// Run all playground scripts.
const result: ShellEvaluateResult = await this._evaluate(
codeToEvaluate
);
const result: ShellEvaluateResult = await this._evaluate({
codeToEvaluate,
filePath,
});

return result;
}
Expand Down Expand Up @@ -572,11 +586,18 @@ export default class PlaygroundController {
}
}

async evaluateParticipantQuery(text: string): Promise<boolean> {
async evaluateParticipantCode(codeToEvaluate: string): Promise<boolean> {
const shouldConfirmRunCopilotCode = vscode.workspace
.getConfiguration('mdb')
.get('confirmRunCopilotCode');

if (!this._connectionController.isCurrentlyConnected()) {
// TODO(VSCODE-618): Prompt user to connect when clicked.
void vscode.window.showErrorMessage(connectBeforeRunningMessage);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another drive-by, adding an error message when trying to run when not connected, and a todo for the changes in VSCODE-618.


return false;
}

if (shouldConfirmRunCopilotCode === true) {
const name = this._connectionController.getActiveConnectionName();
const confirmRunCopilotCode = await vscode.window.showInformationMessage(
Expand All @@ -591,7 +612,9 @@ export default class PlaygroundController {
}

const evaluateResponse: ShellEvaluateResult =
await this._evaluateWithCancelModal(text);
await this._evaluateWithCancelModal({
codeToEvaluate,
});

if (!evaluateResponse || !evaluateResponse.result) {
return false;
Expand All @@ -602,15 +625,19 @@ export default class PlaygroundController {
return true;
}

async _evaluatePlayground(text: string): Promise<boolean> {
async _evaluatePlayground({
codeToEvaluate,
filePath,
}: {
codeToEvaluate: string;
filePath?: string;
}): Promise<boolean> {
const shouldConfirmRunAll = vscode.workspace
.getConfiguration('mdb')
.get('confirmRunAll');

if (!this._connectionController.isCurrentlyConnected()) {
void vscode.window.showErrorMessage(
'Please connect to a database before running a playground.'
);
void vscode.window.showErrorMessage(connectBeforeRunningMessage);

return false;
}
Expand All @@ -629,7 +656,10 @@ export default class PlaygroundController {
}

const evaluateResponse: ShellEvaluateResult =
await this._evaluateWithCancelModal(text);
await this._evaluateWithCancelModal({
codeToEvaluate,
filePath,
});

if (!evaluateResponse || !evaluateResponse.result) {
return false;
Expand All @@ -652,7 +682,10 @@ export default class PlaygroundController {

this._isPartialRun = true;

return this._evaluatePlayground(this._selectedText || '');
return this._evaluatePlayground({
codeToEvaluate: this._selectedText || '',
filePath: getActiveEditorFilePath(),
});
}

runAllPlaygroundBlocks(): Promise<boolean> {
Expand All @@ -669,7 +702,10 @@ export default class PlaygroundController {

this._isPartialRun = false;

return this._evaluatePlayground(this._getAllText());
return this._evaluatePlayground({
codeToEvaluate: this._getAllText(),
filePath: getActiveEditorFilePath(),
});
}

runAllOrSelectedPlaygroundBlocks(): Promise<boolean> {
Expand All @@ -693,14 +729,17 @@ export default class PlaygroundController {
codeToEvaluate = this._selectedText;
}

return this._evaluatePlayground(codeToEvaluate);
return this._evaluatePlayground({
codeToEvaluate,
filePath: getActiveEditorFilePath(),
});
}

async fixThisInvalidInteractiveSyntax({
documentUri,
range,
fix,
}: ThisDiagnosticFix) {
}: ThisDiagnosticFix): Promise<boolean> {
const edit = new vscode.WorkspaceEdit();
edit.replace(documentUri, range, fix);
await vscode.workspace.applyEdit(edit);
Expand All @@ -710,7 +749,7 @@ export default class PlaygroundController {
async fixAllInvalidInteractiveSyntax({
documentUri,
diagnostics,
}: AllDiagnosticFixes) {
}: AllDiagnosticFixes): Promise<boolean> {
const edit = new vscode.WorkspaceEdit();

for (const { range, fix } of diagnostics) {
Expand Down Expand Up @@ -884,7 +923,7 @@ export default class PlaygroundController {
language,
num_stages: selectedText
? countAggregationStagesInString(selectedText)
: null,
: undefined,
with_import_statements: importStatements,
with_builders: builders,
with_driver_syntax: driverSyntax,
Expand Down
14 changes: 7 additions & 7 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { ConnectionStorage } from './storage/connectionStorage';
import type StreamProcessorTreeItem from './explorer/streamProcessorTreeItem';
import type {
ParticipantCommand,
RunParticipantQueryCommandArgs,
RunParticipantCodeCommandArgs,
} from './participant/participant';
import ParticipantController from './participant/participant';
import type { OpenSchemaCommandArgs } from './participant/prompts/schema';
Expand Down Expand Up @@ -296,17 +296,17 @@ export default class MDBExtensionController implements vscode.Disposable {

// ------ CHAT PARTICIPANT ------ //
this.registerParticipantCommand(
EXTENSION_COMMANDS.OPEN_PARTICIPANT_QUERY_IN_PLAYGROUND,
({ runnableContent }: RunParticipantQueryCommandArgs) => {
return this._playgroundController.createPlaygroundFromParticipantQuery({
EXTENSION_COMMANDS.OPEN_PARTICIPANT_CODE_IN_PLAYGROUND,
({ runnableContent }: RunParticipantCodeCommandArgs) => {
return this._playgroundController.createPlaygroundFromParticipantCode({
text: runnableContent,
});
}
);
this.registerParticipantCommand(
EXTENSION_COMMANDS.RUN_PARTICIPANT_QUERY,
({ runnableContent }: RunParticipantQueryCommandArgs) => {
return this._playgroundController.evaluateParticipantQuery(
EXTENSION_COMMANDS.RUN_PARTICIPANT_CODE,
({ runnableContent }: RunParticipantCodeCommandArgs) => {
return this._playgroundController.evaluateParticipantCode(
runnableContent
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/participant/participant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ interface NamespaceQuickPicks {
data: string;
}

export type RunParticipantQueryCommandArgs = {
export type RunParticipantCodeCommandArgs = {
runnableContent: string;
};

Expand Down Expand Up @@ -210,16 +210,16 @@ export default class ParticipantController {
return;
}

const commandArgs: RunParticipantQueryCommandArgs = {
const commandArgs: RunParticipantCodeCommandArgs = {
runnableContent,
};
stream.button({
command: EXTENSION_COMMANDS.RUN_PARTICIPANT_QUERY,
command: EXTENSION_COMMANDS.RUN_PARTICIPANT_CODE,
title: vscode.l10n.t('▶️ Run'),
arguments: [commandArgs],
});
stream.button({
command: EXTENSION_COMMANDS.OPEN_PARTICIPANT_QUERY_IN_PLAYGROUND,
command: EXTENSION_COMMANDS.OPEN_PARTICIPANT_CODE_IN_PLAYGROUND,
title: vscode.l10n.t('Open in playground'),
arguments: [commandArgs],
});
Expand Down
6 changes: 3 additions & 3 deletions src/test/suite/editors/playgroundController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,9 @@ suite('Playground Controller Test Suite', function () {
sandbox.fake.rejects(false)
);

const result = await testPlaygroundController._evaluateWithCancelModal(
''
);
const result = await testPlaygroundController._evaluateWithCancelModal({
codeToEvaluate: '',
});

expect(result).to.deep.equal({ result: undefined });
});
Expand Down
Loading
Loading