Skip to content
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

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ webpack.test.config.js
.eslintrc
.eslintignore
playgrounds/**
!global.d.ts
44 changes: 44 additions & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type {
FindCursor,
AggregationCursor,
} from '@mongosh/service-provider-core';
import {
Document,
FindOptions,
ExplainVerbosityLike,
} from '@mongosh/service-provider-core';

declare global {
let use: (dbName: string) => void;

enum Stages {
match = '$match',
}

let db: {
getCollection(coll: string): {
find(
query?: Document,
projection?: Document,
options?: FindOptions
): Promise<FindCursor>;
aggregate(
pipeline: [{ [key in Stages]: Document }],
options: Document & {
explain?: never;
}
): Promise<AggregationCursor>;
aggregate(
pipeline: [{ [key in Stages]: Document }],
options: Document & {
explain: ExplainVerbosityLike;
}
): Promise<Document>;
aggregate(
...stages: [{ [key in Stages]: Document }]
): Promise<AggregationCursor>;
};
};
}

export {};
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"reformat": "prettier --write ."
},
"engines": {
"vscode": "^1.77.0",
"vscode": "^1.77.3",
"node": "^16.16.0",
"npm": "^8.15.1"
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/check-vsix-size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const vsixFileName = path.resolve(
);
const size = fs.statSync(vsixFileName).size;

const maxSize = 7 * 1000000; // 7 MB
const maxSize = 9 * 1000000; // 9 MB

if (size >= maxSize) {
throw new Error(
Expand Down
98 changes: 98 additions & 0 deletions src/language/convertKind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { CompletionItemKind } from 'vscode-languageserver/node';

const enum Kind {
alias = 'alias',
callSignature = 'call',
class = 'class',
const = 'const',
constructorImplementation = 'constructor',
constructSignature = 'construct',
directory = 'directory',
enum = 'enum',
enumMember = 'enum member',
externalModuleName = 'external module name',
function = 'function',
indexSignature = 'index',
interface = 'interface',
keyword = 'keyword',
let = 'let',
localFunction = 'local function',
localVariable = 'local var',
method = 'method',
memberGetAccessor = 'getter',
memberSetAccessor = 'setter',
memberVariable = 'property',
module = 'module',
primitiveType = 'primitive type',
script = 'script',
type = 'type',
variable = 'var',
warning = 'warning',
string = 'string',
parameter = 'parameter',
typeParameter = 'type parameter',
}

// eslint-disable-next-line complexity
export const convertKind = (kind: string): CompletionItemKind => {
switch (kind) {
case Kind.primitiveType:
case Kind.keyword:
return CompletionItemKind.Keyword;

case Kind.const:
case Kind.let:
case Kind.variable:
case Kind.localVariable:
case Kind.alias:
case Kind.parameter:
return CompletionItemKind.Variable;

case Kind.memberVariable:
case Kind.memberGetAccessor:
case Kind.memberSetAccessor:
return CompletionItemKind.Field;

case Kind.function:
case Kind.localFunction:
return CompletionItemKind.Function;

case Kind.method:
case Kind.constructSignature:
case Kind.callSignature:
case Kind.indexSignature:
return CompletionItemKind.Method;

case Kind.enum:
return CompletionItemKind.Enum;

case Kind.enumMember:
return CompletionItemKind.EnumMember;

case Kind.module:
case Kind.externalModuleName:
return CompletionItemKind.Module;

case Kind.class:
case Kind.type:
return CompletionItemKind.Class;

case Kind.interface:
return CompletionItemKind.Interface;

case Kind.warning:
return CompletionItemKind.Text;

case Kind.script:
return CompletionItemKind.File;

case Kind.directory:
return CompletionItemKind.Folder;

case Kind.string:
return CompletionItemKind.Constant;

default:
return CompletionItemKind.Property;
}
};
45 changes: 45 additions & 0 deletions src/language/loadLibrary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { readFileSync } from 'fs';
import { join } from 'path';

export const GLOBAL_CONFIG_LIBRARY_NAME = 'global.d.ts';

const contents: { [name: string]: string } = Object.create(null);

/**
* Load files related to the language features.
*/
export const loadLibrary = ({
libraryName,
extensionPath,
}: {
libraryName: string;
extensionPath?: string;
}) => {
if (!extensionPath) {
console.error(
`Unable to load library ${libraryName}: extensionPath is undefined`
);
return '';
}

let libraryPath;

if (libraryName === GLOBAL_CONFIG_LIBRARY_NAME) {
libraryPath = join(extensionPath, libraryName);
}

let content = contents[libraryName];

if (typeof content !== 'string' && libraryPath) {
try {
content = readFileSync(libraryPath, 'utf8');
} catch (e) {
console.error(`Unable to load library ${libraryName} at ${libraryPath}`);
content = '';
}

contents[libraryName] = content;
}

return content;
};
36 changes: 35 additions & 1 deletion 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 TypeScriptService from './tsLanguageService';

import { ServerCommands } from './serverCommands';
import {
Expand All @@ -31,6 +32,9 @@ const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
// MongoDB language service.
const mongoDBService = new MongoDBService(connection);

// TypeScript language service.
const typeScriptService = new TypeScriptService();

let hasConfigurationCapability = false;
// let hasWorkspaceFolderCapability = false;
// let hasDiagnosticRelatedInformationCapability = false;
Expand Down Expand Up @@ -67,6 +71,11 @@ connection.onInitialize((params: InitializeParams) => {
resolveProvider: true,
triggerCharacters: ['.'],
},
// Tell the client that the server supports help signatures.
signatureHelpProvider: {
resolveProvider: true,
triggerCharacters: [',', '('],
},
// documentFormattingProvider: true,
// documentRangeFormattingProvider: true,
// codeLensProvider: {
Expand Down Expand Up @@ -159,9 +168,10 @@ connection.onRequest(
}
);

// 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) => {
mongoDBService.setExtensionPath(extensionPath);
typeScriptService.setExtensionPath(extensionPath);
});

// Connect the MongoDB language service to CliServiceProvider
Expand Down Expand Up @@ -204,6 +214,15 @@ connection.onRequest(
connection.onCompletion((params: TextDocumentPositionParams) => {
const document = documents.get(params.textDocument.uri);

/* const document = documents.get(params.textDocument.uri);
if (!document) {
return Promise.resolve([]);
}
return typeScriptService.doComplete({
document,
position: params.position,
}); */

return mongoDBService.provideCompletionItems({
document,
position: params.position,
Expand All @@ -226,6 +245,21 @@ connection.onCompletionResolve((item: CompletionItem): CompletionItem => {
return item;
});

// Provide MongoDB signature help.
connection.onSignatureHelp((signatureHelpParms) => {
const document = documents.get(signatureHelpParms.textDocument.uri);

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

// Provide MongoDB or TypeScript help signatures.
return typeScriptService.doSignatureHelp({
document,
position: signatureHelpParms.position,
});
});

connection.onRequest('textDocument/rangeFormatting', (event) => {
// connection.console.log(
// `textDocument/rangeFormatting: ${JSON.stringify({ event })}`
Expand Down
Loading