Skip to content

Commit

Permalink
Merge pull request #26 from publicodes/feat-improve-errors
Browse files Browse the repository at this point in the history
feat(server): more precise position for unknown ref error
  • Loading branch information
EmileRolley authored Oct 21, 2024
2 parents 11c4505 + 09fbb32 commit 5a60009
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
14 changes: 14 additions & 0 deletions client/src/test/diagnostics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ Un problème est survenu lors du parsing de l'expression \`autoconsommation + pr
},
]);
});

test("Unknown reference", async () => {
await testDiagnostics(getDocUri("diagnostics-unknown-ref.publicodes"), [
{
message: `La référence "unknown" est introuvable.
[ Solution ]
- Vérifiez que la référence "unknown" est bien écrite.`,
range: toRange(1, 9, 1, 17),
severity: vscode.DiagnosticSeverity.Error,
source: "publicodes",
},
]);
});
});

function toRange(sLine: number, sChar: number, eLine: number, eChar: number) {
Expand Down
2 changes: 2 additions & 0 deletions client/testFixture/diagnostics-unknown-ref.publicodes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
valeur: unknown
17 changes: 7 additions & 10 deletions server/src/treeSitter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import TSParser, { SyntaxNode } from "tree-sitter";
import Publicodes from "tree-sitter-publicodes";
import { TextDocument } from "vscode-languageserver-textdocument";
import { DottedName, FileInfos, LSContext, Position } from "./context";
import { DottedName, LSContext } from "./context";
import { utils } from "publicodes";
import assert from "assert";
import { trimQuotedString } from "./helpers";
Expand Down Expand Up @@ -112,15 +111,13 @@ export function getRefInRule(
return null;
}

let ruleNode: SyntaxNode | null = tsTree.rootNode.descendantForPosition(
let ruleNode: SyntaxNode | null = tsTree.rootNode.descendantsOfType(
"rule_body",
ruleDef.defPos.start,
);
if (ruleNode == undefined) {
return null;
}
ruleDef.defPos.end,
)[0];

ruleNode = ruleNode.nextNamedSibling;
if (ruleNode == undefined || ruleNode.type !== "rule_body") {
if (ruleNode == undefined) {
return null;
}

Expand All @@ -131,7 +128,7 @@ function searchRefInNode(
node: SyntaxNode,
refName: DottedName,
): SyntaxNode | null {
if (node.type === "reference" && node.text === refName) {
if (node.type === "reference" && node.text.trim() === refName) {
return node;
}

Expand Down

0 comments on commit 5a60009

Please sign in to comment.