From 09fbb3284073b118496d4a295cd6bd896fbfa09f Mon Sep 17 00:00:00 2001 From: Emile Rolley Date: Mon, 21 Oct 2024 18:36:52 +0200 Subject: [PATCH] fix(server): correctly localize unknow ref errors --- client/src/test/diagnostics.test.ts | 14 ++++++++++++++ .../diagnostics-unknown-ref.publicodes | 2 ++ server/src/treeSitter.ts | 17 +++++++---------- 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 client/testFixture/diagnostics-unknown-ref.publicodes diff --git a/client/src/test/diagnostics.test.ts b/client/src/test/diagnostics.test.ts index d8aefcb..6dfcc56 100644 --- a/client/src/test/diagnostics.test.ts +++ b/client/src/test/diagnostics.test.ts @@ -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) { diff --git a/client/testFixture/diagnostics-unknown-ref.publicodes b/client/testFixture/diagnostics-unknown-ref.publicodes new file mode 100644 index 0000000..fccc350 --- /dev/null +++ b/client/testFixture/diagnostics-unknown-ref.publicodes @@ -0,0 +1,2 @@ +test: + valeur: unknown diff --git a/server/src/treeSitter.ts b/server/src/treeSitter.ts index 81aaa4f..98a0beb 100644 --- a/server/src/treeSitter.ts +++ b/server/src/treeSitter.ts @@ -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"; @@ -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; } @@ -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; }