Skip to content

Commit 6fb3732

Browse files
refactor: clean up (#512)
* refactor: clean up * refactor: add namespace state
1 parent f881b13 commit 6fb3732

File tree

9 files changed

+534
-452
lines changed

9 files changed

+534
-452
lines changed

package-lock.json

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

package.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"reformat": "prettier --write ."
6262
},
6363
"engines": {
64-
"vscode": "^1.76.2",
64+
"vscode": "^1.77.0",
6565
"node": "^16.16.0",
6666
"npm": "^8.15.1"
6767
},
@@ -959,10 +959,10 @@
959959
}
960960
},
961961
"dependencies": {
962-
"@babel/parser": "^7.21.3",
963-
"@babel/traverse": "^7.21.3",
964-
"@fortawesome/fontawesome-svg-core": "^6.3.0",
965-
"@fortawesome/free-solid-svg-icons": "^6.3.0",
962+
"@babel/parser": "^7.21.4",
963+
"@babel/traverse": "^7.21.4",
964+
"@fortawesome/fontawesome-svg-core": "^6.4.0",
965+
"@fortawesome/free-solid-svg-icons": "^6.4.0",
966966
"@fortawesome/react-fontawesome": "^0.2.0",
967967
"@iconify-icons/codicon": "^1.2.23",
968968
"@iconify/react": "^1.1.4",
@@ -974,19 +974,19 @@
974974
"@mongosh/service-provider-server": "^1.8.0",
975975
"@mongosh/shell-api": "^1.8.0",
976976
"analytics-node": "^6.2.0",
977-
"bson": "^5.1.0",
977+
"bson": "^5.2.0",
978978
"bson-transpilers": "^2.0.3",
979979
"classnames": "^2.3.2",
980980
"debug": "^4.3.4",
981981
"dotenv": "^16.0.3",
982982
"micromatch": "^4.0.5",
983-
"mongodb": "^5.1.0",
983+
"mongodb": "^5.2.0",
984984
"mongodb-build-info": "^1.5.0",
985985
"mongodb-cloud-info": "^1.1.3",
986986
"mongodb-connection-string-url": "^2.6.0",
987987
"mongodb-data-service": "^22.5.1",
988988
"mongodb-query-parser": "^2.4.11",
989-
"mongodb-schema": "^10.0.1",
989+
"mongodb-schema": "^10.0.2",
990990
"numeral": "^2.0.6",
991991
"react": "^17.0.2",
992992
"react-dom": "^17.0.2",
@@ -1012,16 +1012,16 @@
10121012
"@types/micromatch": "^4.0.2",
10131013
"@types/mkdirp": "^2.0.0",
10141014
"@types/mocha": "^8.2.3",
1015-
"@types/node": "^14.18.40",
1016-
"@types/react": "^17.0.53",
1015+
"@types/node": "^14.18.42",
1016+
"@types/react": "^17.0.58",
10171017
"@types/react-dom": "^17.0.19",
10181018
"@types/sinon": "^9.0.11",
10191019
"@types/uuid": "^8.3.4",
1020-
"@types/vscode": "^1.76.0",
1021-
"@typescript-eslint/eslint-plugin": "^5.56.0",
1022-
"@typescript-eslint/parser": "^5.56.0",
1020+
"@types/vscode": "^1.77.0",
1021+
"@typescript-eslint/eslint-plugin": "^5.58.0",
1022+
"@typescript-eslint/parser": "^5.58.0",
10231023
"@vscode/test-electron": "^2.3.0",
1024-
"@vscode/vsce": "^2.18.0",
1024+
"@vscode/vsce": "^2.19.0",
10251025
"@wojtekmaj/enzyme-adapter-react-17": "^0.8.0",
10261026
"autoprefixer": "^9.8.8",
10271027
"buffer": "^6.0.3",
@@ -1034,9 +1034,9 @@
10341034
"css-loader": "^3.6.0",
10351035
"depcheck": "^1.4.3",
10361036
"download": "^8.0.0",
1037-
"electron": "^23.2.0",
1037+
"electron": "^23.2.3",
10381038
"enzyme": "^3.11.0",
1039-
"eslint": "^8.36.0",
1039+
"eslint": "^8.38.0",
10401040
"eslint-config-mongodb-js": "^5.0.3",
10411041
"eslint-plugin-mocha": "^10.1.0",
10421042
"execa": "^1.0.0",
@@ -1059,7 +1059,7 @@
10591059
"pre-commit": "^1.2.2",
10601060
"prettier": "^2.8.7",
10611061
"process": "^0.11.10",
1062-
"semver": "^7.3.8",
1062+
"semver": "^7.4.0",
10631063
"sinon": "^9.2.4",
10641064
"sinon-chai": "^3.7.0",
10651065
"stream-browserify": "^3.0.0",
@@ -1068,7 +1068,7 @@
10681068
"ts-loader": "^9.4.2",
10691069
"ts-node": "^10.9.1",
10701070
"typescript": "^4.9.5",
1071-
"webpack": "^5.76.3",
1071+
"webpack": "^5.79.0",
10721072
"webpack-cli": "^4.10.0",
10731073
"xvfb-maybe": "^0.2.1",
10741074
"yargs-parser": "^20.2.9"

src/language/languageServerController.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ export default class LanguageServerController {
4343

4444
// The debug options for the server
4545
// --inspect=6009: runs the server in Node's Inspector mode
46-
// so VS Code can attach to the server for debugging
46+
// so VS Code can attach to the server for debugging.
4747
const debugOptions = { execArgv: ['--nolazy', '--inspect=6009'] };
4848

49-
// If the extension is launched in debug mode then the debug server options are used
50-
// Otherwise the run options are used
49+
// If the extension is launched in debug mode then the debug server options are used.
50+
// Otherwise the run options are used.
5151
const serverOptions: ServerOptions = {
5252
run: { module: serverModule, transport: TransportKind.ipc },
5353
debug: {
@@ -57,15 +57,15 @@ export default class LanguageServerController {
5757
},
5858
};
5959

60-
// Options to control the language client
60+
// Options to control the language client.
6161
const clientOptions: LanguageClientOptions = {
62-
// Register the server for mongodb documents
62+
// Register the language server for mongodb documents.
6363
documentSelector: [
64-
{ scheme: 'untitled', language: 'javascript' },
65-
{ scheme: 'file', language: 'javascript' },
64+
{ pattern: '**/*.mongodb.js' },
65+
{ pattern: '**/*.mongodb' },
6666
],
6767
synchronize: {
68-
// Notify the server about file changes in the workspace
68+
// Notify the server about file changes in the workspace.
6969
fileEvents: workspace.createFileSystemWatcher('**/*'),
7070
},
7171
outputChannel: vscode.window.createOutputChannel(
@@ -78,7 +78,7 @@ export default class LanguageServerController {
7878
clientOptions,
7979
});
8080

81-
// Create the language server client
81+
// Create the language server client.
8282
this._client = new LanguageClient(
8383
'mongodbLanguageServer',
8484
'MongoDB Language Server',
@@ -88,6 +88,9 @@ export default class LanguageServerController {
8888
}
8989

9090
async startLanguageServer(): Promise<void> {
91+
// Start the client. This will also launch the server.
92+
await this._client.start();
93+
9194
// Push the disposable client to the context's subscriptions so that the
9295
// client can be deactivated on extension deactivation.
9396
if (!this._context.subscriptions.includes(this._client)) {
@@ -115,9 +118,13 @@ export default class LanguageServerController {
115118
);
116119
}
117120

118-
deactivate(): void {
119-
// Stop the language server
120-
void this._client.stop();
121+
deactivate(): Thenable<void> | undefined {
122+
if (!this._client) {
123+
return undefined;
124+
}
125+
126+
// Stop the language server.
127+
return this._client.stop();
121128
}
122129

123130
async evaluate(
@@ -126,12 +133,12 @@ export default class LanguageServerController {
126133
this._isExecutingInProgress = true;
127134

128135
// Instantiate a new CancellationTokenSource object
129-
// that generates a cancellation token for each run of a playground
136+
// that generates a cancellation token for each run of a playground.
130137
this._source = new CancellationTokenSource();
131138

132139
// Send a request with a cancellation token
133-
// to the language server server to execute scripts from a playground
134-
// and return results to the playground controller when ready
140+
// to the language server instance to execute scripts from a playground
141+
// and return results to the playground controller when ready.
135142
const result: ShellEvaluateResult = await this._client.sendRequest(
136143
ServerCommands.EXECUTE_CODE_FROM_PLAYGROUND,
137144
playgroundExecuteParameters,

src/language/mongoDBService.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ export default class MongoDBService {
399399
getExportToLanguageMode(
400400
params: PlaygroundTextAndSelection
401401
): ExportToLanguageMode {
402-
const state = this._visitor.parseAST(params);
402+
const state = this._visitor.parseASTForExportToLanguage(params);
403403

404404
if (state.isArraySelection) {
405405
return ExportToLanguageMode.AGGREGATION;
@@ -420,7 +420,7 @@ export default class MongoDBService {
420420
params: PlaygroundTextAndSelection
421421
): ExportToLanguageNamespace {
422422
try {
423-
const state = this._visitor.parseAST(params);
423+
const state = this._visitor.parseASTForNamespace(params);
424424
return {
425425
databaseName: state.databaseName,
426426
collectionName: state.collectionName,
@@ -802,13 +802,10 @@ export default class MongoDBService {
802802
position: { line: number; character: number }
803803
): Promise<CompletionItem[]> {
804804
this._connection.console.log(
805-
`LS current symbol position: ${util.inspect(position)}`
805+
`Provide completion items for a position: ${util.inspect(position)}`
806806
);
807807

808-
const state = this._visitor.parseASTWithPlaceholder(
809-
textFromEditor,
810-
position
811-
);
808+
const state = this._visitor.parseASTForCompletion(textFromEditor, position);
812809
this._connection.console.log(
813810
`VISITOR completion state: ${util.inspect(state)}`
814811
);
@@ -842,12 +839,11 @@ export default class MongoDBService {
842839
}
843840
}
844841

845-
this._connection.console.log('VISITOR no completion');
842+
this._connection.console.log('VISITOR found no mongodb completion');
846843
return [];
847844
}
848845

849846
// Highlight the usage of commands that only works inside interactive session.
850-
// eslint-disable-next-line complexity
851847
provideDiagnostics(textFromEditor: string) {
852848
const lines = textFromEditor.split(/\r?\n/g);
853849
const diagnostics: Diagnostic[] = [];

src/language/server.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
import { TextDocument } from 'vscode-languageserver-textdocument';
1414

1515
import MongoDBService from './mongoDBService';
16+
1617
import { ServerCommands } from './serverCommands';
1718
import {
1819
PlaygroundEvaluateParams,
@@ -27,7 +28,7 @@ const connection: Connection = createConnection(ProposedFeatures.all);
2728
// The text document manager supports full document sync only.
2829
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
2930

30-
// MongoDB Playground Service Manager.
31+
// MongoDB language service.
3132
const mongoDBService = new MongoDBService(connection);
3233

3334
let hasConfigurationCapability = false;
@@ -61,7 +62,7 @@ connection.onInitialize((params: InitializeParams) => {
6162
},
6263
},
6364
},
64-
// Tell the client that the server supports code completion
65+
// Tell the client that the server supports code completion.
6566
completionProvider: {
6667
resolveProvider: true,
6768
triggerCharacters: ['.'],
@@ -91,7 +92,7 @@ connection.onInitialized(() => {
9192
// }
9293
});
9394

94-
// The example settings
95+
// The example settings.
9596
interface ExampleSettings {
9697
maxNumberOfProblems: number;
9798
}
@@ -118,8 +119,6 @@ connection.onDidChangeConfiguration((/* change */) => {
118119

119120
// Only keep settings for open documents.
120121
documents.onDidClose((e) => {
121-
// connection.console.log(`documents.onDidClose: ${JSON.stringify(e)}`);
122-
123122
documentSettings.delete(e.document.uri);
124123
});
125124

@@ -152,19 +151,21 @@ connection.onDidChangeWatchedFiles((/* _change */) => {
152151
// );
153152
});
154153

155-
// Execute the entire playground script.
154+
// Execute a playground.
156155
connection.onRequest(
157156
ServerCommands.EXECUTE_CODE_FROM_PLAYGROUND,
158157
(evaluateParams: PlaygroundEvaluateParams, token) => {
159158
return mongoDBService.evaluate(evaluateParams, token);
160159
}
161160
);
162161

162+
// Pass the extension path to the MongoDB service.
163163
connection.onRequest(ServerCommands.SET_EXTENSION_PATH, (extensionPath) => {
164-
return mongoDBService.setExtensionPath(extensionPath);
164+
mongoDBService.setExtensionPath(extensionPath);
165165
});
166166

167-
// Connect to CliServiceProvider to enable shell completions.
167+
// Connect the MongoDB language service to CliServiceProvider
168+
// using the current connection of the client.
168169
connection.onRequest(ServerCommands.CONNECT_TO_SERVICE_PROVIDER, (params) => {
169170
return mongoDBService.connectToServiceProvider(params);
170171
});
@@ -183,21 +184,23 @@ connection.onRequest(
183184
}
184185
);
185186

187+
// Identify if the playground selection is an array or object.
186188
connection.onRequest(
187189
ServerCommands.GET_EXPORT_TO_LANGUAGE_MODE,
188190
(params: PlaygroundTextAndSelection) => {
189191
return mongoDBService.getExportToLanguageMode(params);
190192
}
191193
);
192194

195+
// Find the current namespace for a playground selection.
193196
connection.onRequest(
194197
ServerCommands.GET_NAMESPACE_FOR_SELECTION,
195198
(params: PlaygroundTextAndSelection) => {
196199
return mongoDBService.getNamespaceForSelection(params);
197200
}
198201
);
199202

200-
// This handler provides the list of the completion items.
203+
// Provide MongoDB completion items.
201204
connection.onCompletion((params: TextDocumentPositionParams) => {
202205
const textFromEditor = documents.get(params.textDocument.uri)?.getText();
203206

@@ -258,6 +261,7 @@ connection.onDidOpenTextDocument((/* params */) => {
258261
// params.textDocument.text the initial full content of the document.
259262
// connection.console.log(`${params.textDocument.uri} opened.`);
260263
});
264+
261265
connection.onDidChangeTextDocument((/* params */) => {
262266
// The content of a text document did change in VSCode.
263267
// params.textDocument.uri uniquely identifies the document.
@@ -268,6 +272,7 @@ connection.onDidChangeTextDocument((/* params */) => {
268272
// )}`
269273
// );
270274
});
275+
271276
connection.onDidCloseTextDocument((/* params */) => {
272277
// A text document got closed in VSCode.
273278
// params.textDocument.uri uniquely identifies the document.

0 commit comments

Comments
 (0)