Skip to content
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
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"request": "attach",
"name": "Attach to Language Server",
"protocol": "inspector",
"port": 6005,
"port": 6009,
"sourceMaps": true,
"outFiles": [
"${workspaceFolder}/out/**/*.js"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@
"redux": "^4.0.5",
"ts-log": "^2.1.4",
"uuid": "^7.0.0",
"vscode-languageclient": "^6.1.1",
"vscode-languageclient": "^6.1.3",
"vscode-languageserver": "^6.1.1"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-keyfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ config({ path: resolve(__dirname, '../.env') });
}
})().catch((error) => {
ui.fail('Failed to generate constants keyfile');
console.log(error);
console.log(error.message);
})
27 changes: 15 additions & 12 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as vscode from 'vscode';

import ConnectionController, { DataServiceEventTypes } from '../connectionController';
import { LanguageServerController } from '../language';
import TelemetryController, { TelemetryEventTypes } from '../telemetry/telemetryController';
import { ElectronRuntime } from '@mongosh/browser-runtime-electron';
import { CompassServiceProvider } from '@mongosh/service-provider-server';
import ActiveConnectionCodeLensProvider from './activeConnectionCodeLensProvider';
import formatOutput from '../utils/formatOutput';
import { OutputChannel } from 'vscode';
Expand All @@ -14,16 +13,23 @@ import playgroundTemplate from '../templates/playgroundTemplate';
*/
export default class PlaygroundController {
_context: vscode.ExtensionContext;
_telemetryController?: TelemetryController;
_connectionController: ConnectionController;
_languageServerController: LanguageServerController;
_telemetryController?: TelemetryController;
_activeDB?: any;
_activeConnectionCodeLensProvider?: ActiveConnectionCodeLensProvider;
_outputChannel: OutputChannel;

constructor(context: vscode.ExtensionContext, connectionController: ConnectionController, telemetryController?: TelemetryController) {
constructor(
context: vscode.ExtensionContext,
connectionController: ConnectionController,
languageServerController: LanguageServerController,
telemetryController?: TelemetryController
) {
this._context = context;
this._telemetryController = telemetryController;
this._connectionController = connectionController;
this._languageServerController = languageServerController;
this._telemetryController = telemetryController;
this._outputChannel = vscode.window.createOutputChannel(
'Playground output'
);
Expand Down Expand Up @@ -97,19 +103,16 @@ export default class PlaygroundController {
}

async evaluate(codeToEvaluate: string): Promise<any> {
const activeConnection = this._connectionController.getActiveDataService();
const activeConnectionString = this._connectionController.getActiveConnectionDriverUrl();

if (!activeConnection) {
if (!activeConnectionString) {
return Promise.reject(
new Error('Please connect to a database before running a playground.')
);
}

const serviceProvider = CompassServiceProvider.fromDataService(
activeConnection
);
const runtime = new ElectronRuntime(serviceProvider);
const res = await runtime.evaluate(codeToEvaluate);
// Run playground as a background process using the Language Server
const res = await this._languageServerController.executeAll(codeToEvaluate, activeConnectionString);

if (res) {
this._telemetryController?.track(
Expand Down
42 changes: 21 additions & 21 deletions src/language/languageServerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ const log = createLogger('LanguageServerController');
*/
export default class LanguageServerController {
_connectionController?: ConnectionController;
client?: LanguageClient;
client: LanguageClient;

constructor(
context: vscode.ExtensionContext,
connectionController: ConnectionController
connectionController?: ConnectionController
) {
this._connectionController = connectionController;
this.activate(context);
}

async activate(context: ExtensionContext): Promise<LanguageClient> {
// The server is implemented in node
const serverModule = context.asAbsolutePath(
path.join('out', 'language', 'server.js')
);
const serverModule = path.join(context.extensionPath, 'out', 'language', 'server.js');

// The debug options for the server
// --inspect=6009: runs the server in Node's Inspector mode so VS Code can attach to the server for debugging
const debugOptions = { execArgv: ['--nolazy', '--inspect=6009'] };
Expand Down Expand Up @@ -61,7 +58,7 @@ export default class LanguageServerController {
}
};

log.info('Activating MongoDB language server', {
log.info('Creating MongoDB Language Server', {
serverOptions,
clientOptions
});
Expand All @@ -73,25 +70,28 @@ export default class LanguageServerController {
serverOptions,
clientOptions
);
}

activate() {
// Start the client. This will also launch the server
this.client.start();

await this.client.onReady();
/**
* TODO: Notification is for setup docs only.
*/
this.client.onNotification('mongodbNotification', (messsage) => {
vscode.window.showInformationMessage(messsage);
this.client.onReady().then(() => {
/**
* TODO: Notification is for setup docs only.
*/
this.client.onNotification('mongodbNotification', (messsage) => {
vscode.window.showInformationMessage(messsage);
});
});

return new Promise((resolve) => resolve(this.client));
}

deactivate(): Thenable<void> | undefined {
if (!this.client) {
return undefined;
}
return this.client.stop();
}

executeAll(codeToEvaluate: string, connectionString: string, connectionOptions: any = {}): Thenable<any> | undefined {
return this.client.onReady().then(() => {
return this.client.sendRequest('executeAll', { codeToEvaluate, connectionString, connectionOptions });
});
}
}
12 changes: 9 additions & 3 deletions src/language/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
TextDocumentPositionParams,
RequestType
} from 'vscode-languageserver';
import { ElectronRuntime } from '@mongosh/browser-runtime-electron';
import { CliServiceProvider } from '@mongosh/service-provider-server';

// Create a connection for the server. The connection uses Node's IPC as a transport.
// Also include all preview / proposed LSP features.
Expand Down Expand Up @@ -275,9 +277,13 @@ connection.onCompletionResolve(
/**
* Execute the entire playground script.
*/
connection.onRequest('executeAll', (event) => {
// connection.console.log(`executeAll: ${JSON.stringify(event)}`);
return '';
connection.onRequest('executeAll', async (params) => {
const connectionOptions = params.connectionOptions || {};
const runtime = new ElectronRuntime(
await CliServiceProvider.connect(params.connectionString, connectionOptions)
);

return await runtime.evaluate(params.codeToEvaluate);
});

/**
Expand Down
6 changes: 6 additions & 0 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as vscode from 'vscode';
import ConnectionController from './connectionController';
import { EditorsController, PlaygroundController } from './editors';
import { ExplorerController, CollectionTreeItem } from './explorer';
import { LanguageServerController } from './language';
import { TelemetryController } from './telemetry';
import { StatusView } from './views';
import { createLogger } from './logging';
Expand All @@ -30,6 +31,7 @@ export default class MDBExtensionController implements vscode.Disposable {
_statusView: StatusView;
_storageController: StorageController;
_telemetryController: TelemetryController;
_languageServerController: LanguageServerController;

constructor(
context: vscode.ExtensionContext,
Expand All @@ -52,6 +54,7 @@ export default class MDBExtensionController implements vscode.Disposable {
);
}

this._languageServerController = new LanguageServerController(context);
this._editorsController = new EditorsController(
context,
this._connectionController
Expand All @@ -62,6 +65,7 @@ export default class MDBExtensionController implements vscode.Disposable {
this._playgroundController = new PlaygroundController(
context,
this._connectionController,
this._languageServerController,
this._telemetryController
);
}
Expand All @@ -70,6 +74,7 @@ export default class MDBExtensionController implements vscode.Disposable {
this._connectionController.loadSavedConnections();
this._explorerController.createTreeView();
this._telemetryController.activate();
this._languageServerController.activate();

log.info('Registering commands...');

Expand Down Expand Up @@ -407,5 +412,6 @@ export default class MDBExtensionController implements vscode.Disposable {
this._explorerController.deactivate();
this._playgroundController.deactivate();
this._telemetryController.deactivate();
this._languageServerController.deactivate();
}
}
Loading