Skip to content

Commit

Permalink
feat(server): improve autocompletion to be relative and more resilien…
Browse files Browse the repository at this point in the history
…t to errors

This is not optimal yet but seems to be useful. So, let's test it in the
wild.
  • Loading branch information
EmileRolley committed Oct 21, 2024
1 parent 1b3d86f commit ba951dc
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 86 deletions.
5 changes: 1 addition & 4 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ export function activate(context: ExtensionContext) {
documentSelector: [{ scheme: "file", language: "publicodes" }],
synchronize: {
// Notify the server about file changes to '.clientrc files contained in the workspace
fileEvents: [
workspace.createFileSystemWatcher("**/.clientrc"),
workspace.createFileSystemWatcher("**/.publicodes"),
],
fileEvents: [workspace.createFileSystemWatcher("**/.clientrc")],
},
markdown: {
isTrusted: true,
Expand Down
48 changes: 34 additions & 14 deletions client/src/test/completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ suite("Should do completion", () => {
await testCompletion(docUri, new vscode.Position(7, 20), true);
});

test("Complete in expressions", async () => {
await testCompletion(docUri, new vscode.Position(2, 18), false);
});
// TODO: implement correct test
// test("Complete in expressions", async () => {
// await testCompletion(docUri, new vscode.Position(11, 12), false, [
// ruleItem("rule a"),
// ruleItem("b"),
// ruleItem("c"),
// ]);
// });
});

async function testCompletion(
docUri: vscode.Uri,
position: vscode.Position,
shouldBeEmtpy: boolean,
// expectedCompletionList?: vscode.CompletionList,
shouldBeEmpty: boolean,
expectedRulesCompletionItems?: vscode.CompletionItem[],
) {
await activate(docUri);

Expand All @@ -37,17 +42,32 @@ async function testCompletion(
position,
)) as vscode.CompletionList;

if (shouldBeEmtpy) {
if (shouldBeEmpty) {
assert.ok(actualCompletionList.items.length === 0);
} else {
assert.ok(actualCompletionList.items.length > 0);
// assert.ok(
// actualCompletionList.items.length === expectedCompletionList.items.length,
// );
// expectedCompletionList.items.forEach((expectedItem, i) => {
// const actualItem = actualCompletionList.items[i];
// assert.equal(actualItem.label, expectedItem.label);
// assert.equal(actualItem.kind, expectedItem.kind);
// });
const actualRulesCompletionItems = actualCompletionList.items.filter(
(item) => {
return (
item.kind !== vscode.CompletionItemKind.Property &&
item.kind !== vscode.CompletionItemKind.Keyword
);
},
);
console.log(actualRulesCompletionItems);
console.log(expectedRulesCompletionItems);
assert.ok(
actualRulesCompletionItems.length ===
expectedRulesCompletionItems?.length,
);
expectedRulesCompletionItems?.forEach((expectedItem, i) => {
const actualItem = actualRulesCompletionItems[i];
assert.equal(actualItem.label, expectedItem.label);
assert.equal(actualItem.kind, expectedItem.kind);
});
}
}

function ruleItem(label: string): vscode.CompletionItem {
return new vscode.CompletionItem(label, vscode.CompletionItemKind.Function);
}
4 changes: 2 additions & 2 deletions client/testFixture/completion.publicodes
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
rule a:
titre: Résultat
valeur: b + c
valeur: rule a . b + rule a . c

rule a . b:
valeur: 2
description: |
Description.

rule a . c: 3
rule a . c:
4 changes: 2 additions & 2 deletions client/testFixture/main.publicodes
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
rule a:
titre: Résultat
valeur: b + c
valeur: rule a . b + rule a . c

rule a . b:
valeur: 2
description: |
Description.

rule a . c: 3
rule a . c:
2 changes: 1 addition & 1 deletion language-configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"increaseIndentPattern": "^\\s*.*(:|-) ?(&\\w+)?(\\{[^}\"']*|\\([^)\"']*)?$",
"decreaseIndentPattern": "^\\s+\\}$"
},
"wordPattern": "(^.?[^\\s]+)+|([^\\s\n={[][\\w\\-\\./$%&*:\"']+)",
"wordPattern": "",
"onEnterRules": [
{
"beforeText": "^\\s*(moyenne|somme|une de ces conditions|toutes ces conditions|variations|le maximum de|le minimum de|suggestions|références|les règles)\\:\\s*$",
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@
"editor.tabSize": 2,
"editor.autoIndent": "advanced",
"editor.quickSuggestions": {
"other": true,
"comments": false,
"strings": true
"other": "on",
"comments": "off",
"strings": "on"
},
"editor.wordBasedSuggestions": "off",
"editor.semanticHighlight.enable": true,
Expand Down
Loading

0 comments on commit ba951dc

Please sign in to comment.