Skip to content

Commit 97aca1b

Browse files
authored
Merge pull request #9 from bitloops/validation-errors-add
Added completion for StandardVO
2 parents 5339d1e + 9284de3 commit 97aca1b

File tree

5 files changed

+58
-16
lines changed

5 files changed

+58
-16
lines changed

server/src/completion.ts

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,66 @@
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 StandardVOCompletion = {
16+
prefixes: ['StandardVO'],
17+
// , 'StandardVO.Currency'],
18+
completions: {
19+
StandardVO: ['Currency'],
20+
// 'StandardVO.Currency': ['Value', 'Errors'],
21+
},
22+
};
923

1024
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-
}));
25+
public static onCompletion(
26+
documents: TextDocuments<TextDocument>,
27+
_textDocumentPosition: TextDocumentPositionParams,
28+
) {
29+
const document = documents.get(_textDocumentPosition.textDocument.uri);
30+
let text = documents.get(_textDocumentPosition.textDocument.uri).getText();
31+
let position = _textDocumentPosition.position;
32+
const offset = document.offsetAt(position);
33+
for (const prefix of StandardVOCompletion.prefixes) {
34+
const linePrefix = text.slice(offset - prefix.length - 1, offset);
35+
if (linePrefix.endsWith(prefix + '.')) {
36+
return [
37+
...StandardVOCompletion.completions[prefix].map((symbol) => ({
38+
label: symbol,
39+
kind: CompletionItemKind.Keyword,
40+
data: 2,
41+
//we could add snippets too
42+
})),
43+
];
44+
}
45+
}
46+
return [
47+
...allKeywords.map((symbol) => ({
48+
label: symbol,
49+
kind: CompletionItemKind.Keyword,
50+
data: 1,
51+
})),
52+
];
1753
}
1854

1955
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];
56+
if (item.data == 1) {
57+
if (documentation[item.label as keyof typeof documentation]) {
58+
item.documentation = documentation[item.label as keyof typeof documentation];
59+
}
60+
if (details[item.label as keyof typeof details]) {
61+
item.detail = details[item.label as keyof typeof details];
62+
}
2363
}
24-
if (details[item.label as keyof typeof details]) {
25-
item.detail = details[item.label as keyof typeof details];
26-
}
27-
2864
return item;
2965
}
3066
}

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export const components = {
1212
GraphQLController: 'GraphQLController',
1313
RESTController: 'RESTController',
1414
PackagePort: 'PackagePort',
15+
Command: 'Command',
16+
Query: 'Query',
1517
};
1618

1719
export const keywords = [
@@ -139,6 +141,9 @@ export const keywords = [
139141
'Operations',
140142
'Mutation',
141143
'Query',
144+
145+
'StandardVO',
142146
];
143147

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

syntaxes/bitloops.tmLanguage.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@
6464
"patterns": [
6565
{
6666
"name": "keyword.control.templateClass.bitloops",
67-
"match": "\\b(Config|(REST\\.Methods)|REST|DTO|UseCase|Root|Entity|Rule|OK|Errors|ValueObject|Props|ReadModel|RESTController|RESTRouter|GraphQLController|Feature|Mapper|RepoPort|RepoAdapter|PackagePort|PackageAdapter|ApplicationErrors|DomainErrors|DomainError|ApplicationError)\\b"
67+
"match": "\\b(Config|(REST\\.Methods)|REST|DTO|UseCase|Root|Entity|Rule|OK|Errors|ValueObject|Props|ReadModel|RESTController|RESTRouter|GraphQLController|Feature|Mapper|RepoPort|RepoAdapter|PackagePort|PackageAdapter|ApplicationErrors|DomainErrors|DomainError|ApplicationError|Command|Query|CommandHandler|QueryHandler)\\b"
6868
}
6969
]
7070
},
7171
"myClasses": {
7272
"patterns": [
7373
{
7474
"name": "entity.name.class.bitloops",
75-
"match": "\\b[A-Z]+[a-zA-Z0-9]*(DTO|UseCase|Aggregate|VO|Entity|Error|Controller|Router|Feature|Mapper|RepoPort|RepoAdapter|PackagePort|PackageAdapter|Error)\\b"
75+
"match": "\\b[A-Z]+[a-zA-Z0-9]*(DTO|UseCase|Aggregate|VO|Entity|Error|Controller|Router|Feature|Mapper|RepoPort|RepoAdapter|PackagePort|PackageAdapter|Error|Props|Rule|Command|Query|ReadModel|CommandHandler|QueryHandler)\\b"
7676
}
7777
]
7878
},

0 commit comments

Comments
 (0)