Skip to content

Commit f264fd2

Browse files
committed
Add auto completion of enums
1 parent dcbc25c commit f264fd2

4 files changed

Lines changed: 49 additions & 15 deletions

File tree

server/src/language-service/completionProvider.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -251,20 +251,15 @@ export function getObjectCompletion(
251251
).map((obj) => xvrToCompletionItem(obj, range))
252252
);
253253
}
254+
const enums = getEnumCompletions(obj, currentAttr.attr);
255+
const completions = [...enums, ...references];
254256
if (isEndAttribute(offset, currentAttr.attr)) {
257+
completions.push(...getObjectAttributeCompletion(obj, offset, doc));
255258
if (currentAttr.last) {
256-
return [
257-
...getObjectAttributeCompletion(obj, offset, doc),
258-
...references,
259-
...snippets,
260-
];
259+
completions.push(...snippets);
261260
}
262-
return [
263-
...getObjectAttributeCompletion(obj, offset, doc),
264-
...references,
265-
];
266261
}
267-
return references;
262+
return completions;
268263
}
269264

270265
function getCurrentAttr(
@@ -312,6 +307,7 @@ function getObjectAttributeCompletion(
312307
value: formatObjectAttribute(attr),
313308
kind: "markdown",
314309
},
310+
sortText: `2${attr.name}`,
315311
textEdit: TextEdit.replace(rangeNewLine.range, text),
316312
insertTextMode: InsertTextMode.asIs,
317313
insertTextFormat: InsertTextFormat.Snippet,
@@ -351,6 +347,28 @@ function isReferenceAttribute(obj: SepticObject, attr: Attribute): boolean {
351347
return objectInfo.refs.attributes.includes(attr.key);
352348
}
353349

350+
function getEnumCompletions(obj: SepticObject, attr: Attribute): CompletionItem[] {
351+
const objectInfo = SepticMetaInfoProvider.getInstance().getObjectDocumentation(obj.type);
352+
if (!objectInfo) {
353+
return [];
354+
}
355+
let attrDoc = objectInfo.attributes.find((a) => a.name === attr.key);
356+
if (!attrDoc) {
357+
return [];
358+
}
359+
if (attrDoc.dataType !== "enum") {
360+
return [];
361+
}
362+
return attrDoc.enums.map((value) => {
363+
return {
364+
label: value,
365+
kind: CompletionItemKind.EnumMember,
366+
detail: `Enum member of ${attrDoc.name}`,
367+
sortText: `1${value}`,
368+
};
369+
});
370+
}
371+
354372
function isEndAttribute(offset: number, attr: Attribute): boolean {
355373
return offset >= attr.end;
356374
}
@@ -419,7 +437,7 @@ const priorityMapping: { [key: string]: number } = {
419437
"Tvr": 5,
420438
};
421439

422-
function xvrToCompletionItem(obj: SepticObject, range: Range): CompletionItem {
440+
function xvrToCompletionItem(obj: SepticObject, range: Range): CompletionItem {
423441
let priority = priorityMapping[obj.type] ?? 6;
424442
return {
425443
label: obj.identifier!.name,

server/src/septic/septicMetaInfo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ export class SepticMetaInfoProvider {
224224
.join("\n"),
225225
insertTextMode: InsertTextMode.asIs,
226226
detail: obj.description,
227+
sortText: `3${obj.prefix}`,
227228
};
228229
return compItem;
229230
});

server/src/test/completion.test.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ describe("Test completion of object attributes", () => {
219219

220220
describe("Test completion of attribute with references to xvrs", () => {
221221
it("Expect Xvrs for attribute that references Xvrs", () => {
222-
const content = loadFile("completionAttrRefs.cnfg");
222+
const content = loadFile("completion/completionAttrRefs.cnfg");
223223
const doc = TextDocument.create("", "", 0, content);
224224
const cnfg = parseSepticSync(doc.getText());
225225
const offset = doc.offsetAt(Position.create(13, 28));
@@ -230,7 +230,7 @@ describe("Test completion of attribute with references to xvrs", () => {
230230
expect(variableCompItems.length).to.equal(2);
231231
});
232232
it("Expect Mvrs for attribute that references Mvrs", () => {
233-
const content = loadFile("completionAttrRefs.cnfg");
233+
const content = loadFile("completion/completionAttrRefs.cnfg");
234234
const doc = TextDocument.create("", "", 0, content);
235235
const cnfg = parseSepticSync(doc.getText());
236236
const offset = doc.offsetAt(Position.create(17, 19));
@@ -242,7 +242,7 @@ describe("Test completion of attribute with references to xvrs", () => {
242242
expect(variableCompItems[0].label).to.equal("MvrTest");
243243
});
244244
it("Expect completion of xvr in-between existing attr values", () => {
245-
const content = loadFile("completionAttrRefs.cnfg");
245+
const content = loadFile("completion/completionAttrRefs.cnfg");
246246
const doc = TextDocument.create("", "", 0, content);
247247
const cnfg = parseSepticSync(doc.getText());
248248
const offset = doc.offsetAt(Position.create(27, 20));
@@ -253,7 +253,7 @@ describe("Test completion of attribute with references to xvrs", () => {
253253
expect(variableCompItems.length).to.equal(2);
254254
});
255255
it("Expect no completion of xvr non-reference attribute", () => {
256-
const content = loadFile("completionAttrRefs.cnfg");
256+
const content = loadFile("completion/completionAttrRefs.cnfg");
257257
const doc = TextDocument.create("", "", 0, content);
258258
const cnfg = parseSepticSync(doc.getText());
259259
const offset = doc.offsetAt(Position.create(20, 20));
@@ -265,6 +265,21 @@ describe("Test completion of attribute with references to xvrs", () => {
265265
});
266266
});
267267

268+
describe("Test completion of attribute enum datatype", () => {
269+
it("Expect Xvrs for attribute that references Xvrs", () => {
270+
const content = "Evr: TestEvr\nUserInput= \n";
271+
const doc = TextDocument.create("", "", 0, content);
272+
const cnfg = parseSepticSync(doc.getText());
273+
const offset = doc.offsetAt(Position.create(1, 11));
274+
const compItems = getObjectCompletion(offset, cnfg, cnfg, doc);
275+
const variableCompItems = compItems.filter(
276+
(item) => item.kind === CompletionItemKind.EnumMember
277+
);
278+
const enums = SepticMetaInfoProvider.getInstance().getObjectDocumentation("Evr")?.attributes.find(attr => attr.name === "UserInput")?.enums;
279+
expect(variableCompItems.length).to.equal(enums?.length);
280+
});
281+
});
282+
268283
describe("Test calc completion", () => {
269284
it("Completion suggest Xvrs", () => {
270285
const text = `Mvr: TestMvr\nEvr: TestEvr\nCalcPvr: TestCalc\nAlg= " "\n`;

server/src/test/fixtures/completionAttrRefs.cnfg renamed to server/src/test/fixtures/completion/completionAttrRefs.cnfg

File renamed without changes.

0 commit comments

Comments
 (0)