Skip to content

Commit

Permalink
feat(server): more precise diagnostic position for unknown refs
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed May 9, 2024
1 parent 08a7c9f commit 2d32e4d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
52 changes: 50 additions & 2 deletions server/src/treeSitter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import TSParser from "tree-sitter";
import TSParser, { Query, SyntaxNode } from "tree-sitter";
import Publicodes from "tree-sitter-publicodes";
import { TextDocument } from "vscode-languageserver-textdocument";
import { FileInfos, LSContext } from "./context";
import { DottedName, FileInfos, LSContext } from "./context";
import { utils } from "publicodes";
import assert from "assert";

Expand Down Expand Up @@ -84,3 +84,51 @@ export function getRuleNameAt(

return ruleDef?.dottedName;
}

export function getRefInRule(
ctx: LSContext,
filePath: string,
ruleName: DottedName,
refName: DottedName,
): SyntaxNode | null {
const { ruleDefs, tsTree } = ctx.fileInfos.get(filePath)!;

const ruleDef = ruleDefs.find((ruleDef) => ruleDef.dottedName === ruleName);
if (ruleDef == undefined) {
return null;
}

let ruleNode: SyntaxNode | null = tsTree.rootNode.descendantForPosition(
ruleDef.defPos.start,
);
if (ruleNode == undefined) {
return null;
}

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

return searchRefInNode(ruleNode, refName);
}

function searchRefInNode(
node: SyntaxNode,
refName: DottedName,
): SyntaxNode | null {
if (node.type === "reference" && node.text === refName) {
return node;
}

let child = node.firstChild;
while (child) {
const found = searchRefInNode(child, refName);
if (found !== null) {
return found;
}
child = child.nextNamedSibling;
}

return null;
}
30 changes: 30 additions & 0 deletions server/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { parseDocument } from "./parseRules";
import { fileURLToPath, pathToFileURL } from "node:url";
import { Logger } from "publicodes";
import { mapAppend, positionToRange } from "./helpers";
import { getRefInRule } from "./treeSitter";

export default async function validate(
ctx: LSContext,
Expand Down Expand Up @@ -137,6 +138,35 @@ function getDiagnosticFromErrorMsg(
};
}

if (message.includes(`✖️ La référence "`)) {
const refName = message.match(
/✖️ La référence "(.*)" est introuvable/,
)?.[1];

if (refName) {
const refNode = getRefInRule(ctx, filePath, wrongRule, refName!);

if (refNode) {
return {
filePath,
diagnostic: {
severity,
range: positionToRange({
start: refNode.startPosition,
end: refNode.endPosition,
}),
message: `[ Erreur de syntaxe ]
La référence "${refName}" est introuvable.
[ Solution ]
- Vérifiez que la référence "${refName}" est bien écrite.
- Vérifiez que la règle "${refName}" est bien définie.`,
},
};
}
}
}

const pos = ctx.fileInfos
.get(filePath)
?.ruleDefs.find(({ dottedName }) => dottedName === wrongRule)?.namesPos ?? {
Expand Down

0 comments on commit 2d32e4d

Please sign in to comment.