Skip to content

Commit bc588c3

Browse files
VSCODE: use cancellation token (#54)
* feat: use cancellation token * feat: show loading progress * docs: add open questions * feat: close node connection when playground is canceled * refactor: show information about cancelation instantly * test: update tests * test: better info messages testing * refactor: return resolve * docs: add comments to the code * feat: check server alive * feat: use node worker threads * refactor: update function returning type * refactor: move promise to the language server * refactor: debug in worker * refactor: change names and structure of returns * refactor: do not close mongo client * test: add test for canceling a playground * test: try longer timeout * test: remove rimeout * test: try update async * feat: configure LSP inspector * docs: how to stream logs to LSP inspector * refactor: update text of info messages * test: add test when runAllPlaygroundBlocks is missing active connection
1 parent d82d6ea commit bc588c3

12 files changed

+404
-65
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
// https://github.com/Microsoft/language-server-protocol-inspector
2121
"mongodbLanguageServer.trace.server": {
2222
"format": "json",
23-
"verbosity": "info"
23+
"verbosity": "verbose"
2424
}
2525
}

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@
241241
{
242242
"command": "mdb.refreshSchema",
243243
"title": "Refresh"
244+
},
245+
{
246+
"command": "mdb.startStreamLanguageServerLogs",
247+
"title": "LSP Inspector: Start Stream LSP Logs"
244248
}
245249
],
246250
"menus": {
@@ -393,6 +397,37 @@
393397
"configuration": {
394398
"title": "MongoDB",
395399
"properties": {
400+
"mongodbLanguageServer.maxNumberOfProblems": {
401+
"scope": "resource",
402+
"type": "number",
403+
"default": 100,
404+
"description": "Controls the maximum number of problems produced by the server."
405+
},
406+
"mongodbLanguageServer.trace.server": {
407+
"scope": "window",
408+
"type": "object",
409+
"properties": {
410+
"verbosity": {
411+
"type": "string",
412+
"enum": [
413+
"off",
414+
"messages",
415+
"verbose"
416+
],
417+
"default": "off",
418+
"description": "Controls the verbosity of the trace."
419+
},
420+
"format": {
421+
"type": "string",
422+
"enum": [
423+
"text",
424+
"json"
425+
],
426+
"default": "text",
427+
"description": "Controls the output format of the trace."
428+
}
429+
}
430+
},
396431
"mdb.shell": {
397432
"type": "string",
398433
"enum": [
@@ -477,7 +512,8 @@
477512
"ts-log": "^2.1.4",
478513
"uuid": "^7.0.0",
479514
"vscode-languageclient": "^6.1.3",
480-
"vscode-languageserver": "^6.1.1"
515+
"vscode-languageserver": "^6.1.1",
516+
"ws": "^7.2.3"
481517
},
482518
"devDependencies": {
483519
"@types/chai": "^4.2.9",
@@ -490,6 +526,7 @@
490526
"@types/react": "^16.9.25",
491527
"@types/react-dom": "^16.9.5",
492528
"@types/vscode": "^1.41.0",
529+
"@types/ws": "^7.2.4",
493530
"@typescript-eslint/eslint-plugin": "^2.19.2",
494531
"@typescript-eslint/parser": "^2.19.2",
495532
"autoprefixer": "^9.7.5",

src/editors/playgroundController.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import ConnectionController, { DataServiceEventTypes } from '../connectionContro
44
import { LanguageServerController } from '../language';
55
import TelemetryController, { TelemetryEventTypes } from '../telemetry/telemetryController';
66
import ActiveConnectionCodeLensProvider from './activeConnectionCodeLensProvider';
7-
import formatOutput from '../utils/formatOutput';
8-
import { OutputChannel } from 'vscode';
7+
import { OutputChannel, ProgressLocation } from 'vscode';
98
import playgroundTemplate from '../templates/playgroundTemplate';
109

1110
/**
@@ -62,6 +61,7 @@ export default class PlaygroundController {
6261
})
6362
.then((document) => {
6463
vscode.window.showTextDocument(document);
64+
this._outputChannel.show(true);
6565
resolve(true);
6666
}, reject);
6767
});
@@ -111,17 +111,18 @@ export default class PlaygroundController {
111111
);
112112
}
113113

114-
// Run playground as a background process using the Language Server
115-
const res = await this._languageServerController.executeAll(codeToEvaluate, activeConnectionString);
114+
// Send a request to the language server to execute scripts from a playground
115+
const result = await this._languageServerController.executeAll(codeToEvaluate, activeConnectionString);
116116

117-
if (res) {
117+
if (result) {
118+
// Send metrics to Segment
118119
this._telemetryController?.track(
119120
TelemetryEventTypes.PLAYGROUND_CODE_EXECUTED,
120-
this.prepareTelemetry(res)
121+
this.prepareTelemetry(result)
121122
);
122123
}
123124

124-
return Promise.resolve(formatOutput(res));
125+
return Promise.resolve(result);
125126
}
126127

127128
runAllPlaygroundBlocks(): Promise<boolean> {
@@ -131,7 +132,13 @@ export default class PlaygroundController {
131132
.getConfiguration('mdb')
132133
.get('confirmRunAll');
133134

134-
if (activeConnection && shouldConfirmRunAll === true) {
135+
if (!activeConnection) {
136+
vscode.window.showErrorMessage('Please connect to a database before running a playground.');
137+
138+
return resolve(false);
139+
}
140+
141+
if (shouldConfirmRunAll === true) {
135142
const name = this._connectionController.getActiveConnectionName();
136143
const confirmRunAll = await vscode.window.showInformationMessage(
137144
`Are you sure you want to run this playground against ${name}? This confirmation can be disabled in the extension settings.`,
@@ -140,26 +147,45 @@ export default class PlaygroundController {
140147
);
141148

142149
if (confirmRunAll !== 'Yes') {
143-
return Promise.resolve(false);
150+
return resolve(false);
144151
}
145152
}
146153

147154
const activeEditor = vscode.window.activeTextEditor;
148155
const codeToEvaluate = activeEditor?.document.getText() || '';
149156
let result;
150157

151-
try {
158+
// Show a running progress in the notification area with support for cancellation
159+
await vscode.window.withProgress({
160+
location: ProgressLocation.Notification,
161+
title: 'Running MongoDB playground...',
162+
cancellable: true
163+
}, async (progress, token) => {
164+
token.onCancellationRequested(() => {
165+
// If a user clicked the cancel button terminate all playground scripts
166+
this._languageServerController.cancelAll();
167+
this._outputChannel.clear();
168+
this._outputChannel.show(true);
169+
170+
return resolve(false);
171+
});
172+
173+
// Run all playground scripts
152174
result = await this.evaluate(codeToEvaluate);
153-
} catch (error) {
154-
vscode.window.showErrorMessage(`Unable to run playground: ${error.message}`);
175+
});
176+
177+
if (!result) {
178+
this._outputChannel.clear();
179+
this._outputChannel.show(true);
180+
155181
return resolve(false);
156182
}
157183

158184
this._outputChannel.clear();
159185
this._outputChannel.appendLine(result);
160186
this._outputChannel.show(true);
161187

162-
resolve(true);
188+
return resolve(true);
163189
});
164190
}
165191

src/language/README.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@ We can also extend the mongodb language server and client with custom methods to
2828

2929
### Debugging
3030

31-
#### Output Channel
31+
#### Client
3232

33-
`MongoDB Language Server`
33+
Debugging the client code is as easy as debugging a normal extension. Set a breakpoint in the client code and debug the extension by pressing `F5`. Use `console.log()` for printing debug information in the Debug Console of the extension.
3434

35-
From server side: `connection.console.log(<string>)`
35+
#### Server
3636

37-
![MongoDB Language Server output channel](https://user-images.githubusercontent.com/23074/76441349-a489e980-6395-11ea-8247-50cfe9b3ff61.png)
37+
Since the server is started by the `LanguageClient` running separately, we need to attach a debugger to the running server. To do so, switch to the Run view and select the launch configuration `Extension + Server Inspector` and press `F5`. This will run both the extension and the Server Inspector.
3838

39-
#### Log Streaming + LSP Inspector
39+
Use `connection.console.log()` for printing debug information in the `MongoDB Language Server` Output Channel of the server.
4040

41-
https://github.com/microsoft/vscode-extension-samples/tree/master/lsp-log-streaming-sample
42-
https://microsoft.github.io/language-server-protocol/inspector/
41+
![MongoDB Language Server output channel](https://user-images.githubusercontent.com/23074/76441349-a489e980-6395-11ea-8247-50cfe9b3ff61.png)
42+
43+
### Logging Support
4344

44-
In `.vscode/settings.json`
45+
In `.vscode/settings.json` specify a `mongodbLanguageServer.trace.server` setting that instructs the Client to log communications between Language Client/Server to a channel of the Language Client's name.
4546

4647
```json
4748
"mongodbLanguageServer.trace.server": {
@@ -50,18 +51,42 @@ In `.vscode/settings.json`
5051
},
5152
```
5253

54+
You can also configure logging from VSCode setting interface.
55+
56+
![MongoDB Language Server log settings](./lsp-trace-server.png)
57+
58+
The logs will be printed in the `MongoDB Language Server` Output Channel.
59+
60+
These logs are useful for developing and testing the Language Server, but they can be lengthy and hard to read. To visualize Cleint/Server logs use the [Language Server Protocol Inspector](https://github.com/Microsoft/language-server-protocol-inspector).
61+
62+
#### Strem Logs To LSP Inspector
63+
64+
- Install the [Language Server Protocol Inspector](https://marketplace.visualstudio.com/items?itemName=octref.lsp-inspector-webview) webview extension.
65+
- Run the MongoDB extension.
66+
- Activate the extension using any activation command.
67+
- Open a playground.
68+
- Check `mongodbLanguageServer.trace.server` settings.
69+
- Run command "LSP Inspector: Start LSP Inspector" to open the LSP inspector webview.
70+
- Run command "LSP Inspector: Start Stream LSP Logs" to start streaming logs to the LSP inspector port specified by the `languageServerExample.port` setting (The default value is 7000).
71+
72+
![MongoDB Language Server log settings](./lsp-stream-logs.png)
73+
74+
https://github.com/microsoft/vscode-extension-samples/tree/master/lsp-log-streaming-sample
75+
https://github.com/microsoft/vscode-extension-samples/tree/master/lsp-sample
76+
77+
5378
#### LSP Notifications
5479

5580
From the server:
5681

5782
```javascript
58-
connection.sendNotification('mongodbNotification', `Hi, Friend.`);
83+
connection.sendNotification('showInformationMessage', `Hi, Friend.`);
5984
```
6085

6186
From the client:
6287

6388
```javascript
64-
client.onNotification('mongodbNotification', (messsage) => {
89+
client.onNotification('showInformationMessage', (messsage) => {
6590
vscode.window.showInformationMessage(messsage);
6691
});
6792
```

0 commit comments

Comments
 (0)