Skip to content

Commit

Permalink
refactor: clean up (#512)
Browse files Browse the repository at this point in the history
* refactor: clean up

* refactor: add namespace state
  • Loading branch information
alenakhineika authored Apr 17, 2023
1 parent f881b13 commit 6fb3732
Show file tree
Hide file tree
Showing 9 changed files with 534 additions and 452 deletions.
705 changes: 357 additions & 348 deletions package-lock.json

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"reformat": "prettier --write ."
},
"engines": {
"vscode": "^1.76.2",
"vscode": "^1.77.0",
"node": "^16.16.0",
"npm": "^8.15.1"
},
Expand Down Expand Up @@ -959,10 +959,10 @@
}
},
"dependencies": {
"@babel/parser": "^7.21.3",
"@babel/traverse": "^7.21.3",
"@fortawesome/fontawesome-svg-core": "^6.3.0",
"@fortawesome/free-solid-svg-icons": "^6.3.0",
"@babel/parser": "^7.21.4",
"@babel/traverse": "^7.21.4",
"@fortawesome/fontawesome-svg-core": "^6.4.0",
"@fortawesome/free-solid-svg-icons": "^6.4.0",
"@fortawesome/react-fontawesome": "^0.2.0",
"@iconify-icons/codicon": "^1.2.23",
"@iconify/react": "^1.1.4",
Expand All @@ -974,19 +974,19 @@
"@mongosh/service-provider-server": "^1.8.0",
"@mongosh/shell-api": "^1.8.0",
"analytics-node": "^6.2.0",
"bson": "^5.1.0",
"bson": "^5.2.0",
"bson-transpilers": "^2.0.3",
"classnames": "^2.3.2",
"debug": "^4.3.4",
"dotenv": "^16.0.3",
"micromatch": "^4.0.5",
"mongodb": "^5.1.0",
"mongodb": "^5.2.0",
"mongodb-build-info": "^1.5.0",
"mongodb-cloud-info": "^1.1.3",
"mongodb-connection-string-url": "^2.6.0",
"mongodb-data-service": "^22.5.1",
"mongodb-query-parser": "^2.4.11",
"mongodb-schema": "^10.0.1",
"mongodb-schema": "^10.0.2",
"numeral": "^2.0.6",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand All @@ -1012,16 +1012,16 @@
"@types/micromatch": "^4.0.2",
"@types/mkdirp": "^2.0.0",
"@types/mocha": "^8.2.3",
"@types/node": "^14.18.40",
"@types/react": "^17.0.53",
"@types/node": "^14.18.42",
"@types/react": "^17.0.58",
"@types/react-dom": "^17.0.19",
"@types/sinon": "^9.0.11",
"@types/uuid": "^8.3.4",
"@types/vscode": "^1.76.0",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"@types/vscode": "^1.77.0",
"@typescript-eslint/eslint-plugin": "^5.58.0",
"@typescript-eslint/parser": "^5.58.0",
"@vscode/test-electron": "^2.3.0",
"@vscode/vsce": "^2.18.0",
"@vscode/vsce": "^2.19.0",
"@wojtekmaj/enzyme-adapter-react-17": "^0.8.0",
"autoprefixer": "^9.8.8",
"buffer": "^6.0.3",
Expand All @@ -1034,9 +1034,9 @@
"css-loader": "^3.6.0",
"depcheck": "^1.4.3",
"download": "^8.0.0",
"electron": "^23.2.0",
"electron": "^23.2.3",
"enzyme": "^3.11.0",
"eslint": "^8.36.0",
"eslint": "^8.38.0",
"eslint-config-mongodb-js": "^5.0.3",
"eslint-plugin-mocha": "^10.1.0",
"execa": "^1.0.0",
Expand All @@ -1059,7 +1059,7 @@
"pre-commit": "^1.2.2",
"prettier": "^2.8.7",
"process": "^0.11.10",
"semver": "^7.3.8",
"semver": "^7.4.0",
"sinon": "^9.2.4",
"sinon-chai": "^3.7.0",
"stream-browserify": "^3.0.0",
Expand All @@ -1068,7 +1068,7 @@
"ts-loader": "^9.4.2",
"ts-node": "^10.9.1",
"typescript": "^4.9.5",
"webpack": "^5.76.3",
"webpack": "^5.79.0",
"webpack-cli": "^4.10.0",
"xvfb-maybe": "^0.2.1",
"yargs-parser": "^20.2.9"
Expand Down
37 changes: 22 additions & 15 deletions src/language/languageServerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export default class LanguageServerController {

// 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
// so VS Code can attach to the server for debugging.
const debugOptions = { execArgv: ['--nolazy', '--inspect=6009'] };

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

// Options to control the language client
// Options to control the language client.
const clientOptions: LanguageClientOptions = {
// Register the server for mongodb documents
// Register the language server for mongodb documents.
documentSelector: [
{ scheme: 'untitled', language: 'javascript' },
{ scheme: 'file', language: 'javascript' },
{ pattern: '**/*.mongodb.js' },
{ pattern: '**/*.mongodb' },
],
synchronize: {
// Notify the server about file changes in the workspace
// Notify the server about file changes in the workspace.
fileEvents: workspace.createFileSystemWatcher('**/*'),
},
outputChannel: vscode.window.createOutputChannel(
Expand All @@ -78,7 +78,7 @@ export default class LanguageServerController {
clientOptions,
});

// Create the language server client
// Create the language server client.
this._client = new LanguageClient(
'mongodbLanguageServer',
'MongoDB Language Server',
Expand All @@ -88,6 +88,9 @@ export default class LanguageServerController {
}

async startLanguageServer(): Promise<void> {
// Start the client. This will also launch the server.
await this._client.start();

// Push the disposable client to the context's subscriptions so that the
// client can be deactivated on extension deactivation.
if (!this._context.subscriptions.includes(this._client)) {
Expand Down Expand Up @@ -115,9 +118,13 @@ export default class LanguageServerController {
);
}

deactivate(): void {
// Stop the language server
void this._client.stop();
deactivate(): Thenable<void> | undefined {
if (!this._client) {
return undefined;
}

// Stop the language server.
return this._client.stop();
}

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

// Instantiate a new CancellationTokenSource object
// that generates a cancellation token for each run of a playground
// 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 server to execute scripts from a playground
// and return results to the playground controller when ready
// to the language server instance to execute scripts from a playground
// and return results to the playground controller when ready.
const result: ShellEvaluateResult = await this._client.sendRequest(
ServerCommands.EXECUTE_CODE_FROM_PLAYGROUND,
playgroundExecuteParameters,
Expand Down
14 changes: 5 additions & 9 deletions src/language/mongoDBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ export default class MongoDBService {
getExportToLanguageMode(
params: PlaygroundTextAndSelection
): ExportToLanguageMode {
const state = this._visitor.parseAST(params);
const state = this._visitor.parseASTForExportToLanguage(params);

if (state.isArraySelection) {
return ExportToLanguageMode.AGGREGATION;
Expand All @@ -420,7 +420,7 @@ export default class MongoDBService {
params: PlaygroundTextAndSelection
): ExportToLanguageNamespace {
try {
const state = this._visitor.parseAST(params);
const state = this._visitor.parseASTForNamespace(params);
return {
databaseName: state.databaseName,
collectionName: state.collectionName,
Expand Down Expand Up @@ -802,13 +802,10 @@ export default class MongoDBService {
position: { line: number; character: number }
): Promise<CompletionItem[]> {
this._connection.console.log(
`LS current symbol position: ${util.inspect(position)}`
`Provide completion items for a position: ${util.inspect(position)}`
);

const state = this._visitor.parseASTWithPlaceholder(
textFromEditor,
position
);
const state = this._visitor.parseASTForCompletion(textFromEditor, position);
this._connection.console.log(
`VISITOR completion state: ${util.inspect(state)}`
);
Expand Down Expand Up @@ -842,12 +839,11 @@ export default class MongoDBService {
}
}

this._connection.console.log('VISITOR no completion');
this._connection.console.log('VISITOR found no mongodb completion');
return [];
}

// Highlight the usage of commands that only works inside interactive session.
// eslint-disable-next-line complexity
provideDiagnostics(textFromEditor: string) {
const lines = textFromEditor.split(/\r?\n/g);
const diagnostics: Diagnostic[] = [];
Expand Down
23 changes: 14 additions & 9 deletions src/language/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { TextDocument } from 'vscode-languageserver-textdocument';

import MongoDBService from './mongoDBService';

import { ServerCommands } from './serverCommands';
import {
PlaygroundEvaluateParams,
Expand All @@ -27,7 +28,7 @@ const connection: Connection = createConnection(ProposedFeatures.all);
// The text document manager supports full document sync only.
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);

// MongoDB Playground Service Manager.
// MongoDB language service.
const mongoDBService = new MongoDBService(connection);

let hasConfigurationCapability = false;
Expand Down Expand Up @@ -61,7 +62,7 @@ connection.onInitialize((params: InitializeParams) => {
},
},
},
// Tell the client that the server supports code completion
// Tell the client that the server supports code completion.
completionProvider: {
resolveProvider: true,
triggerCharacters: ['.'],
Expand Down Expand Up @@ -91,7 +92,7 @@ connection.onInitialized(() => {
// }
});

// The example settings
// The example settings.
interface ExampleSettings {
maxNumberOfProblems: number;
}
Expand All @@ -118,8 +119,6 @@ connection.onDidChangeConfiguration((/* change */) => {

// Only keep settings for open documents.
documents.onDidClose((e) => {
// connection.console.log(`documents.onDidClose: ${JSON.stringify(e)}`);

documentSettings.delete(e.document.uri);
});

Expand Down Expand Up @@ -152,19 +151,21 @@ connection.onDidChangeWatchedFiles((/* _change */) => {
// );
});

// Execute the entire playground script.
// Execute a playground.
connection.onRequest(
ServerCommands.EXECUTE_CODE_FROM_PLAYGROUND,
(evaluateParams: PlaygroundEvaluateParams, token) => {
return mongoDBService.evaluate(evaluateParams, token);
}
);

// Pass the extension path to the MongoDB service.
connection.onRequest(ServerCommands.SET_EXTENSION_PATH, (extensionPath) => {
return mongoDBService.setExtensionPath(extensionPath);
mongoDBService.setExtensionPath(extensionPath);
});

// Connect to CliServiceProvider to enable shell completions.
// Connect the MongoDB language service to CliServiceProvider
// using the current connection of the client.
connection.onRequest(ServerCommands.CONNECT_TO_SERVICE_PROVIDER, (params) => {
return mongoDBService.connectToServiceProvider(params);
});
Expand All @@ -183,21 +184,23 @@ connection.onRequest(
}
);

// Identify if the playground selection is an array or object.
connection.onRequest(
ServerCommands.GET_EXPORT_TO_LANGUAGE_MODE,
(params: PlaygroundTextAndSelection) => {
return mongoDBService.getExportToLanguageMode(params);
}
);

// Find the current namespace for a playground selection.
connection.onRequest(
ServerCommands.GET_NAMESPACE_FOR_SELECTION,
(params: PlaygroundTextAndSelection) => {
return mongoDBService.getNamespaceForSelection(params);
}
);

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

Expand Down Expand Up @@ -258,6 +261,7 @@ connection.onDidOpenTextDocument((/* params */) => {
// params.textDocument.text the initial full content of the document.
// connection.console.log(`${params.textDocument.uri} opened.`);
});

connection.onDidChangeTextDocument((/* params */) => {
// The content of a text document did change in VSCode.
// params.textDocument.uri uniquely identifies the document.
Expand All @@ -268,6 +272,7 @@ connection.onDidChangeTextDocument((/* params */) => {
// )}`
// );
});

connection.onDidCloseTextDocument((/* params */) => {
// A text document got closed in VSCode.
// params.textDocument.uri uniquely identifies the document.
Expand Down
Loading

0 comments on commit 6fb3732

Please sign in to comment.