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 1 commit
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
61 changes: 26 additions & 35 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,34 +495,21 @@ export default class PlaygroundController {
);
}

try {
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();
});

return Promise.race([
this._evaluate(codeToEvaluate),
new Promise<{ result: undefined }>((resolve) =>
token.onCancellationRequested(() => {
resolve({ result: undefined });
})
),
]);
}
);
} 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 @@ -698,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 @@ -708,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