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

feat(participant): export to a playground VSCODE-574 #832

Merged
merged 16 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@
"dark": "images/dark/play.svg"
}
},
{
"command": "mdb.exportCodeToPlayground",
"title": "Export Code to Playground"
},
{
"command": "mdb.exportToPython",
"title": "MongoDB: Export To Python 3"
Expand Down Expand Up @@ -747,6 +751,17 @@
"when": "mdb.isPlayground == true"
}
],
"mdb.copilot": [
{
"command": "mdb.exportCodeToPlayground"
}
],
"editor/context": [
{
"submenu": "mdb.copilot",
"group": "1_main@2"
}
],
"commandPalette": [
{
"command": "mdb.selectDatabaseWithParticipant",
Expand Down Expand Up @@ -948,6 +963,10 @@
"command": "mdb.runPlayground",
"when": "false"
},
{
"command": "mdb.exportCodeToPlayground",
"when": "false"
},
{
"command": "mdb.createIndexFromTreeView",
"when": "false"
Expand Down Expand Up @@ -994,6 +1013,12 @@
}
]
},
"submenus": [
{
"id": "mdb.copilot",
"label": "MongoDB Copilot"
alenakhineika marked this conversation as resolved.
Show resolved Hide resolved
}
],
"keybindings": [
{
"command": "mdb.runSelectedPlaygroundBlocks",
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum EXTENSION_COMMANDS {
MDB_RUN_SELECTED_PLAYGROUND_BLOCKS = 'mdb.runSelectedPlaygroundBlocks',
MDB_RUN_ALL_PLAYGROUND_BLOCKS = 'mdb.runAllPlaygroundBlocks',
MDB_RUN_ALL_OR_SELECTED_PLAYGROUND_BLOCKS = 'mdb.runPlayground',
MDB_EXPORT_CODE_TO_PLAYGROUND = 'mdb.exportCodeToPlayground',

MDB_FIX_THIS_INVALID_INTERACTIVE_SYNTAX = 'mdb.fixThisInvalidInteractiveSyntax',
MDB_FIX_ALL_INVALID_INTERACTIVE_SYNTAX = 'mdb.fixAllInvalidInteractiveSyntax',
Expand Down
63 changes: 26 additions & 37 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ let dummySandbox;
// 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 @@ -160,7 +160,7 @@ export default class PlaygroundController {
this._playgroundSelectedCodeActionProvider =
playgroundSelectedCodeActionProvider;

this._activeConnectionChangedHandler = () => {
this._activeConnectionChangedHandler = (): void => {
void this._activeConnectionChanged();
};
this._connectionController.addEventListener(
Expand All @@ -170,7 +170,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 @@ -438,7 +438,10 @@ export default class PlaygroundController {
return this._createPlaygroundFileWithContent(content);
}

async _evaluate(codeToEvaluate: string): Promise<ShellEvaluateResult> {
async _evaluate(
codeToEvaluate: string,
token?: vscode.CancellationToken
Anemy marked this conversation as resolved.
Show resolved Hide resolved
): Promise<ShellEvaluateResult> {
const connectionId = this._connectionController.getActiveConnectionId();

if (!connectionId) {
Expand All @@ -449,13 +452,14 @@ export default class PlaygroundController {

this._statusView.showMessage('Getting results...');

let result: ShellEvaluateResult;
let result: ShellEvaluateResult = null;
try {
// Send a request to the language server to execute scripts from a playground.
result = await this._languageServerController.evaluate({
codeToEvaluate,
connectionId,
filePath: vscode.window.activeTextEditor?.document.uri.fsPath,
token,
});
} catch (error) {
const msg =
Expand Down Expand Up @@ -491,36 +495,21 @@ export default class PlaygroundController {
);
}

try {
const progressResult = await vscode.window.withProgress(
{
location: ProgressLocation.Notification,
title: 'Running MongoDB playground...',
cancellable: true,
},
async (progress, token) => {
token.onCancellationRequested(() => {
// If a user clicked the cancel button terminate all playground scripts.
this._languageServerController.cancelAll();

return { result: undefined };
});

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

return result;
}
);

return progressResult;
} catch (error) {
log.error('Evaluating playground with cancel modal failed', error);

return { result: undefined };
}
return await vscode.window.withProgress(
{
location: ProgressLocation.Notification,
title: 'Running MongoDB playground...',
cancellable: true,
},
async (progress, token): Promise<ShellEvaluateResult> => {
token.onCancellationRequested(() => {
// If the user clicks the cancel button,
// terminate all processes running on the language server.
this._languageServerController.cancelAll();
alenakhineika marked this conversation as resolved.
Show resolved Hide resolved
});
return this._evaluate(codeToEvaluate, token);
}
);
}

async _openInResultPane(result: PlaygroundResult): Promise<void> {
Expand Down Expand Up @@ -700,7 +689,7 @@ export default class PlaygroundController {
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 +699,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
15 changes: 2 additions & 13 deletions src/language/languageServerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import type {
LanguageClientOptions,
ServerOptions,
} from 'vscode-languageclient/node';
import {
LanguageClient,
TransportKind,
CancellationTokenSource,
} from 'vscode-languageclient/node';
import { LanguageClient, TransportKind } from 'vscode-languageclient/node';
import type { ExtensionContext } from 'vscode';
import { workspace } from 'vscode';
import util from 'util';
Expand All @@ -32,7 +28,6 @@ const log = createLogger('language server controller');
*/
export default class LanguageServerController {
_context: ExtensionContext;
_source?: CancellationTokenSource;
_isExecutingInProgress = false;
_client: LanguageClient;
_currentConnectionId: string | null = null;
Expand Down Expand Up @@ -190,20 +185,15 @@ export default class LanguageServerController {
inputLength: playgroundExecuteParameters.codeToEvaluate.length,
});
this._isExecutingInProgress = true;

this._consoleOutputChannel.clear();

// Instantiate a new CancellationTokenSource object
// that generates a cancellation token for each run of a playground.
this._source = new CancellationTokenSource();

// Send a request with a cancellation token
// to the language server instance to execute scripts from a playground
// and return results to the playground controller when ready.
const res: ShellEvaluateResult = await this._client.sendRequest(
ServerCommands.EXECUTE_CODE_FROM_PLAYGROUND,
playgroundExecuteParameters,
this._source.token
playgroundExecuteParameters.token
);

this._isExecutingInProgress = false;
Expand Down Expand Up @@ -279,7 +269,6 @@ export default class LanguageServerController {
// the onCancellationRequested event will be fired,
// and IsCancellationRequested will return true.
if (this._isExecutingInProgress) {
this._source?.cancel();
this._isExecutingInProgress = false;
}
}
Expand Down
Loading
Loading