Skip to content

feat: extend typescript help signatures with mongodb items VSCODE-403 #509

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
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
Prev Previous commit
Next Next commit
feat: use all mongodb help signatures
  • Loading branch information
alenakhineika committed Apr 8, 2023
commit 25560ba38877e6bdde8930422275b505490ab52a
26 changes: 13 additions & 13 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 Down Expand Up @@ -116,7 +116,7 @@ export default class LanguageServerController {
}

deactivate(): void {
// Stop the language server
// Stop the language server.
void this._client.stop();
}

Expand All @@ -126,12 +126,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
52 changes: 0 additions & 52 deletions src/language/mongoDBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
InsertTextFormat,
MarkupKind,
DiagnosticSeverity,
SignatureHelpContext,
} from 'vscode-languageserver/node';
import type {
CancellationToken,
Expand Down Expand Up @@ -844,57 +843,6 @@ export default class MongoDBService {
return [];
}

/**
* Parse code from a playground to identify
* where the cursor is and show mongodb method signature help items.
*/
provideSignatureHelp(
textFromEditor: string,
position: { line: number; character: number },
context?: SignatureHelpContext
) {
this._connection.console.log(
`Provide signature help for a position: ${util.inspect(position)}`
);

const state = this._visitor.parseASTWForSignatureHelp(
textFromEditor,
position
);
this._connection.console.log(
`VISITOR signature help state: ${util.inspect(state)}`
);

if (state.isFind) {
return {
activeSignatureHelp: context?.activeSignatureHelp,
signatures: [
{
label: 'Collection.find(query, projection, options) : Cursor',
documentation: 'Selects documents in a collection or view.',
parameters: [],
},
],
};
}

if (state.isAggregation) {
return {
activeSignatureHelp: context?.activeSignatureHelp,
signatures: [
{
label: 'Collection.aggregate(pipeline, options) : Cursor',
documentation:
'Calculates aggregate values for the data in a collection or a view.',
parameters: [],
},
],
};
}

this._connection.console.log('VISITOR found no mongodb signature help');
}

// Highlight the usage of commands that only works inside interactive session.
provideDiagnostics(textFromEditor: string) {
const lines = textFromEditor.split(/\r?\n/g);
Expand Down
43 changes: 19 additions & 24 deletions src/language/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
RequestType,
TextDocumentSyncKind,
Connection,
SignatureHelpParams,
} from 'vscode-languageserver/node';
import { TextDocument } from 'vscode-languageserver-textdocument';

Expand All @@ -30,7 +29,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);

// TypeScript language service.
Expand Down Expand Up @@ -67,14 +66,15 @@ 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: ['.'],
},
// Tell the client that the server supports help signatures.
signatureHelpProvider: {
resolveProvider: true,
triggerCharacters: [','],
triggerCharacters: [',', '('],
},
// documentFormattingProvider: true,
// documentRangeFormattingProvider: true,
Expand All @@ -101,7 +101,7 @@ connection.onInitialized(() => {
// }
});

// The example settings
// The example settings.
interface ExampleSettings {
maxNumberOfProblems: number;
}
Expand Down Expand Up @@ -160,20 +160,22 @@ 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.
// Pass the extension path to the MongoDB and TypeScript services.
connection.onRequest(ServerCommands.SET_EXTENSION_PATH, (extensionPath) => {
return mongoDBService.setExtensionPath(extensionPath);
mongoDBService.setExtensionPath(extensionPath);
typeScriptService.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 Down Expand Up @@ -234,26 +236,19 @@ connection.onCompletionResolve((item: CompletionItem): CompletionItem => {
return item;
});

// Provide MongoDB or TypeScript help signatures.
connection.onSignatureHelp((params: SignatureHelpParams) => {
const document = documents.get(params.textDocument.uri);
// Provide MongoDB help signatures.
connection.onSignatureHelp((signatureHelpParms) => {
const document = documents.get(signatureHelpParms.textDocument.uri);

if (!document) {
return;
return Promise.resolve(null);
}

const textFromEditor = document.getText() || '';
const mongodbSignatures = mongoDBService.provideSignatureHelp(
textFromEditor,
params.position,
params.context
// Provide MongoDB or TypeScript help signatures.
return typeScriptService.doSignatureHelp(
document,
signatureHelpParms.position
);

if (mongodbSignatures) {
return mongodbSignatures;
}

return typeScriptService.doSignatureHelp(document, params.position);
});

connection.onRequest('textDocument/rangeFormatting', (event) => {
Expand Down
Loading