Skip to content

Commit 4655b8c

Browse files
authored
Autocomplete details (#267)
* Adds autcomplete details via mongosh i18n package * Aligned i18n version with mongosh version * Back to version 0.8.0 of the i18n package * Markdown is possible after all.
1 parent 4c21059 commit 4655b8c

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@
868868
"@iconify/react": "^1.1.3",
869869
"@leafygreen-ui/toggle": "3.0.1",
870870
"@mongosh/browser-runtime-electron": "^0.6.1",
871+
"@mongosh/i18n": "^0.8.0",
871872
"@mongosh/service-provider-server": "^0.6.1",
872873
"@mongosh/shell-api": "^0.6.1",
873874
"analytics-node": "^3.5.0",

src/language/mongoDBService.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/* eslint-disable no-sync */
22
import * as util from 'util';
3-
import { CompletionItemKind, CancellationToken, Connection, CompletionItem } from 'vscode-languageserver';
3+
import { CompletionItemKind, CancellationToken, Connection, CompletionItem, MarkupContent, MarkupKind } from 'vscode-languageserver';
44
import fs from 'fs';
55
import path from 'path';
66
import { signatures } from '@mongosh/shell-api';
7+
import Translator from '@mongosh/i18n/lib/translator';
78
import { Worker as WorkerThreads } from 'worker_threads';
89

910
import { CollectionItem } from '../types/collectionItemType';
@@ -398,14 +399,28 @@ export default class MongoDBService {
398399
// Get shell API symbols/methods completion from mongosh.
399400
_getShellCompletionItems(): ShellCompletionItem {
400401
const shellSymbols = {};
402+
const translator = new Translator();
401403

402404
Object.keys(signatures).map((symbol) => {
403405
shellSymbols[symbol] = Object.keys(
404406
signatures[symbol].attributes || {}
405-
).map((item) => ({
406-
label: item,
407-
kind: CompletionItemKind.Method
408-
}));
407+
).map((item) => {
408+
const documentation = translator.translate(`shell-api.classes.${symbol}.help.attributes.${item}.description`) || '';
409+
const link = translator.translate(`shell-api.classes.${symbol}.help.attributes.${item}.link`) || '';
410+
const detail = translator.translate(`shell-api.classes.${symbol}.help.attributes.${item}.example`) || '';
411+
412+
const markdownDocumentation: MarkupContent = {
413+
kind: MarkupKind.Markdown,
414+
value: link ? `${documentation}\n\n[Read More](${link})` : documentation
415+
};
416+
417+
return {
418+
label: item,
419+
kind: CompletionItemKind.Method,
420+
documentation: markdownDocumentation,
421+
detail
422+
};
423+
});
409424
});
410425

411426
return shellSymbols;

src/test/suite/language/mongoDBService.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ suite('MongoDBService Test Suite', () => {
106106
const testMongoDBService = new MongoDBService(connection);
107107

108108
before(async () => {
109-
testMongoDBService._getDatabasesCompletionItems = (): void => {};
109+
testMongoDBService._getDatabasesCompletionItems = (): void => { };
110110
testMongoDBService._getCollectionsCompletionItems = (): Promise<boolean> =>
111111
Promise.resolve(true);
112112
testMongoDBService._getFieldsCompletionItems = (): Promise<boolean> =>
@@ -642,6 +642,12 @@ suite('MongoDBService Test Suite', () => {
642642
'kind',
643643
CompletionItemKind.Method
644644
);
645+
expect(findShellCompletion).to.have.property(
646+
'documentation'
647+
);
648+
expect(findShellCompletion).to.have.property(
649+
'detail'
650+
);
645651
});
646652

647653
test('provide only collection names and shell db symbol completion after find cursor', async () => {
@@ -673,6 +679,12 @@ suite('MongoDBService Test Suite', () => {
673679
'kind',
674680
CompletionItemKind.Method
675681
);
682+
expect(findShellCompletion).to.have.property(
683+
'documentation'
684+
);
685+
expect(findShellCompletion).to.have.property(
686+
'detail'
687+
);
676688
expect(findCursorCompletion).to.be.undefined;
677689
});
678690

@@ -711,6 +723,12 @@ suite('MongoDBService Test Suite', () => {
711723
'kind',
712724
CompletionItemKind.Method
713725
);
726+
expect(findShellCompletion).to.have.property(
727+
'documentation'
728+
);
729+
expect(findShellCompletion).to.have.property(
730+
'detail'
731+
);
714732
expect(findCursorCompletion).to.be.undefined;
715733
});
716734

0 commit comments

Comments
 (0)