Skip to content

Commit

Permalink
feat(server): show evaluated node value in the hover message
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed May 3, 2024
1 parent e3b719f commit 8bb960e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
3 changes: 3 additions & 0 deletions server/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TextDocuments,
} from "vscode-languageserver/node.js";
import * as TSParser from "tree-sitter";
import Engine from "publicodes";

export type GlobalConfig = {
hasConfigurationCapability: boolean;
Expand Down Expand Up @@ -77,6 +78,8 @@ export type LSContext = {
fileInfos: Map<FilePath, FileInfos>;
diagnostics: Diagnostic[];

engine: Engine<string>;

// TODO: maybe to remove
ruleToFileNameMap: Map<DottedName, FilePath>;
fileNameToRulesMap: Map<FilePath, DottedName[]>;
Expand Down
24 changes: 6 additions & 18 deletions server/src/onHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { HandlerResult, Hover, HoverParams } from "vscode-languageserver";
import { LSContext } from "./context";
import { getFullRefName } from "./treeSitter";
import { fileURLToPath } from "url";
import { serializeEvaluation, serializeUnit } from "publicodes";

export default function (ctx: LSContext) {
return (params: HoverParams): HandlerResult<Hover, void> => {
Expand Down Expand Up @@ -31,24 +32,19 @@ export default function (ctx: LSContext) {
switch (node.type) {
case "name": {
try {
// TODO: could be extracted to an helper?
const fullRefName = getFullRefName(
ctx,
fileURLToPath(textDocument.uri),
node,
);

const rawRule = ctx.rawPublicodesRules[fullRefName];
const nodeValue = ctx.engine.evaluate(fullRefName);

const value =
`## ${rawRule?.titre ?? fullRefName}\n\n` +
Object.entries(rawRule ?? {})
.map(([key, value]) => {
if (key !== "titre") {
return `### ${key}\n\n${value}`;
}
})
.join("\n\n");
// TODO: polish the hover message
const value = `**${rawRule?.titre ?? fullRefName}** (${serializeEvaluation(nodeValue)})
${rawRule?.description ? `\n### Description\n\n${rawRule.description}\n\n` : ""}
${rawRule?.note ? `### Note\n\n${rawRule.note}` : ""}`;

return {
contents: {
Expand All @@ -63,14 +59,6 @@ export default function (ctx: LSContext) {
break;
}
}
default: {
return {
contents: {
kind: "markdown",
value: `(${node.type})`,
},
};
}
}
};
}
2 changes: 2 additions & 0 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import validate from "./validate";
import onDefinitionHandler from "./onDefinition";
import onHoverHandler from "./onHover";
import { semanticTokensFullProvider } from "./semanticTokens";
import Engine from "publicodes";

let ctx: LSContext = {
// Create a connection for the server, using Node's IPC as a transport.
Expand All @@ -36,6 +37,7 @@ let ctx: LSContext = {
hasWorkspaceFolderCapability: false,
hasDiagnosticRelatedInformationCapability: false,
},
engine: new Engine({}),
fileInfos: new Map(),
diagnostics: [],
ruleToFileNameMap: new Map(),
Expand Down
6 changes: 3 additions & 3 deletions server/src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TextDocument } from "vscode-languageserver-textdocument";
import { LSContext } from "./context";
import Engine from "publicodes";
import { Diagnostic, DiagnosticSeverity } from "vscode-languageserver/node.js";
import { DiagnosticSeverity } from "vscode-languageserver/node.js";
import { parseDocument } from "./parseRules";
import { fileURLToPath, pathToFileURL } from "node:url";

Expand Down Expand Up @@ -56,8 +56,8 @@ export default async function validate(
ctx.connection.console.log(
`[validate] Parsing ${Object.keys(ctx.rawPublicodesRules).length} rules`,
);
const engine = new Engine(ctx.rawPublicodesRules);
ctx.parsedRules = engine.getParsedRules();
ctx.engine = new Engine(ctx.rawPublicodesRules);
ctx.parsedRules = ctx.engine.getParsedRules();
ctx.connection.console.log(
`[validate] Parsed ${Object.keys(ctx.parsedRules).length} rules`,
);
Expand Down

0 comments on commit 8bb960e

Please sign in to comment.