Skip to content

Commit 4272963

Browse files
committed
added completion after dot for StandardVO
1 parent 4a32980 commit 4272963

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

server/src/completion.ts

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,65 @@
11
import {
22
CompletionItem,
33
CompletionItemKind,
4+
InsertTextFormat,
5+
InsertTextMode,
46
TextDocumentPositionParams,
7+
TextDocuments,
58
} from 'vscode-languageserver/node.js';
69
import { documentation } from './information/documentation.js';
710
import { details } from './information/details.js';
811
import { allKeywords, components, keywords } from './types/keywords.js';
12+
import { TextDocument } from 'vscode-languageserver-textdocument';
13+
14+
//this would come from package
15+
const StandardVO = {
16+
prefixes: ['StandardVO', 'StandardVO.Currency'],
17+
completions: {
18+
StandardVO: ['Currency'],
19+
'StandardVO.Currency': ['Value', 'Create'],
20+
},
21+
};
922

1023
export class CompletionItemProvider {
11-
public static onCompletion(_textDocumentPosition: TextDocumentPositionParams) {
12-
return allKeywords.map((symbol) => ({
13-
label: symbol,
14-
kind: CompletionItemKind.Keyword,
15-
data: 1,
16-
}));
24+
public static onCompletion(
25+
documents: TextDocuments<TextDocument>,
26+
_textDocumentPosition: TextDocumentPositionParams,
27+
) {
28+
const document = documents.get(_textDocumentPosition.textDocument.uri);
29+
let text = documents.get(_textDocumentPosition.textDocument.uri).getText();
30+
let position = _textDocumentPosition.position;
31+
const offset = document.offsetAt(position);
32+
for (const prefix of StandardVO.prefixes) {
33+
const linePrefix = text.slice(offset - prefix.length - 1, offset);
34+
if (linePrefix.endsWith(prefix + '.')) {
35+
return [
36+
...StandardVO.completions[prefix].map((symbol) => ({
37+
label: symbol,
38+
kind: CompletionItemKind.Keyword,
39+
data: 2,
40+
//we could check for snippets too
41+
})),
42+
];
43+
}
44+
}
45+
return [
46+
...allKeywords.map((symbol) => ({
47+
label: symbol,
48+
kind: CompletionItemKind.Keyword,
49+
data: 1,
50+
})),
51+
];
1752
}
1853

1954
public static onCompletionResolve(item: CompletionItem): CompletionItem {
20-
const label: string = item.label;
21-
if (documentation[item.label as keyof typeof documentation]) {
22-
item.documentation = documentation[item.label as keyof typeof documentation];
55+
if (item.data == 1) {
56+
if (documentation[item.label as keyof typeof documentation]) {
57+
item.documentation = documentation[item.label as keyof typeof documentation];
58+
}
59+
if (details[item.label as keyof typeof details]) {
60+
item.detail = details[item.label as keyof typeof details];
61+
}
2362
}
24-
if (details[item.label as keyof typeof details]) {
25-
item.detail = details[item.label as keyof typeof details];
26-
}
27-
2863
return item;
2964
}
3065
}

server/src/lsp-connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class LspConnection {
2929

3030
this.documents.onDidClose(server.onDidClose.bind(server));
3131
this.documents.onDidChangeContent(server.onDidChangeContent.bind(server));
32-
this.connection.onCompletion(server.completion.bind(server));
32+
this.connection.onCompletion(server.completion.bind(server, this.documents));
3333
this.connection.onCompletionResolve(server.completionResolve.bind(server));
3434
return this;
3535
}

server/src/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export class BitloopsServer {
6464
// Tell the client that this server supports code completion.
6565
completionProvider: {
6666
resolveProvider: true,
67+
triggerCharacters: ['.'],
6768
},
6869
},
6970
};

server/src/types/keywords.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ export const keywords = [
141141
'Operations',
142142
'Mutation',
143143
'Query',
144+
145+
'StandardVO',
144146
];
145147

148+
// 'StandardVO',
146149
export const allKeywords = Array.from(new Set([...Object.values(components), ...keywords]));

0 commit comments

Comments
 (0)