Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22.x
cache: yarn
- name: Install dependencies
run: yarn install --immutable
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
uses: actions/setup-node@v4
with:
registry-url: https://registry.npmjs.org/
node-version: 20.x
node-version: 22.x
cache: yarn
- uses: softprops/action-gh-release@v2
with:
Expand Down
34 changes: 34 additions & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,40 @@ export function activate(context: ExtensionContext) {
),
);

context.subscriptions.push(
languages.registerCodeActionsProvider(
{ scheme: "file", language: "publicodes" },
{
provideCodeActions(document, range, context, token) {
const params = {
textDocument: { uri: document.uri.toString() },
range,
context,
};
return client.sendRequest("textDocument/codeAction", params, token);
},
},
),
);

context.subscriptions.push(
languages.registerDocumentSymbolProvider(
{ scheme: "file", language: "publicodes" },
{
provideDocumentSymbols(document, token) {
const params = {
textDocument: { uri: document.uri.toString() },
};
return client.sendRequest(
"textDocument/documentSymbol",
params,
token,
);
},
},
),
);

// Start the client. This will also launch the server
client.start();
}
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"vscode"
],
"engines": {
"vscode": "^1.75.0"
"vscode": "^1.75.0",
"node": ">=22.0.0"
},
"main": "./client/out/extension",
"scripts": {
Expand Down Expand Up @@ -104,17 +105,16 @@
"devDependencies": {
"@types/mocha": "^9.1.0",
"@types/node": "^16.11.7",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"@typescript-eslint/eslint-plugin": "^8.26.1",
"@typescript-eslint/parser": "^8.26.1",
"@vscode/vsce": "^2.26.1",
"concurrently": "^8.2.2",
"eslint": "^8.35.0",
"eslint": "^9.22.0",
"mocha": "^9.2.1",
"prettier": "^3.3.0",
"tsc": "^2.0.4",
"tsc-alias": "^1.8.8",
"tsup": "^8.0.2",
"typescript": "^5.0.2"
"tsc-alias": "^1.8.11",
"typescript": "^5.8.2"
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
4 changes: 2 additions & 2 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
"license": "MIT",
"type": "module",
"engines": {
"node": "*"
"node": ">=22.0.0"
},
"repository": {
"type": "git",
"url": "https://github.com/publicodes/publicodes-language-server"
},
"dependencies": {
"@publicodes/tools": "^1.2.5",
"@publicodes/tools": "^1.7.0",
"fs": "^0.0.1-security",
"path": "^0.12.7",
"publicodes": "^1.8.1",
Expand Down
Binary file added server/publicodes-language-server-0.1.0.tgz
Binary file not shown.
119 changes: 119 additions & 0 deletions server/src/codeAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import {
CodeAction,
CodeActionKind,
CodeActionParams,
Command,
Position,
Range,
TextEdit,
} from "vscode-languageserver";
import { PublicodesDiagnosticCode } from "./validate";
import { DottedName, getRuleDef, LSContext } from "./context";
import { fileURLToPath } from "node:url";

export const PublicodesCommands = { CREATE_RULE: "publicodes.createRule" };

export type CreateRuleParams = {
uri: string;
range: Range;
ruleName: DottedName;
ruleNameToCreate: string;
position?: "before" | "after";
};

export function codeActionHandler(
_ctx: LSContext,
params: CodeActionParams,
): (Command | CodeAction)[] | undefined {
return params.context.diagnostics.flatMap((diagnostic) => {
if (
diagnostic.code === PublicodesDiagnosticCode.UNKNOWN_REF &&
diagnostic.data
) {
const shortRefName = diagnostic.data.refName;
const fullRefName = diagnostic.data.ruleName + " . " + shortRefName;

return [
CodeAction.create(
`Create a new rule: '${shortRefName}'`,
Command.create(
`Create a new rule: '${shortRefName}'`,
PublicodesCommands.CREATE_RULE,
{
uri: params.textDocument.uri,
range: diagnostic.range,
ruleName: diagnostic.data.ruleName,
ruleNameToCreate: shortRefName,
position: diagnostic.data.position,
} as CreateRuleParams,
),
CodeActionKind.QuickFix,
),
CodeAction.create(
`Create a new rule: '${fullRefName}'`,
Command.create(
`Create a new rule: '${fullRefName}'`,
PublicodesCommands.CREATE_RULE,
{
uri: params.textDocument.uri,
range: diagnostic.range,
ruleName: diagnostic.data.ruleName,
ruleNameToCreate: fullRefName,
} as CreateRuleParams,
),
CodeActionKind.QuickFix,
),
];
}

if (
diagnostic.code === PublicodesDiagnosticCode.UNKNOWN_PARENT &&
diagnostic.data
) {
return [
CodeAction.create(
`Create the missing rule: '${diagnostic.data.parentRule}'`,
Command.create(
`Create the missing rule: '${diagnostic.data.parentRule}'`,
PublicodesCommands.CREATE_RULE,
{
uri: params.textDocument.uri,
range: diagnostic.range,
ruleName: diagnostic.data.ruleName,
ruleNameToCreate: diagnostic.data.parentRule,
position: "before",
} as CreateRuleParams,
),
CodeActionKind.QuickFix,
),
];
}

return [];
});
}

export function createRule(
ctx: LSContext,
{ uri, ruleName, ruleNameToCreate, position }: CreateRuleParams,
) {
const ruleDefPos = getRuleDef(ctx, ruleName)?.defPos;

if (!ruleDefPos) {
ctx.connection.console.error(
`[createRule] rule def not found in ${uri}: ${ruleName}`,
);
return;
}
const editPos =
position === "before"
? Position.create(ruleDefPos.start.row, 0)
: Position.create(ruleDefPos.end.row, 0);

ctx.connection.workspace.applyEdit({
label: `Create a new rule: ${ruleNameToCreate}`,
changes: {
[uri]: [TextEdit.insert(editPos, `${ruleNameToCreate}:\n\n`)],
},
});
}
20 changes: 20 additions & 0 deletions server/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,23 @@ export type LSContext = {
rawPublicodesRules: RawPublicodes;
ruleToFileNameMap: Map<DottedName, FilePath>;
};

/**
* Get the position of a rule definition in a file.
*
* @returns The position of the rule definition or undefined if not found.
*/
export function getRuleDef(
ctx: LSContext,
ruleName: string,
): RuleDef | undefined {
const fileName = ctx.ruleToFileNameMap.get(ruleName);
if (!fileName) {
ctx.connection.console.error(`[getRuleDefPos] file not found: ${ruleName}`);
return;
}

return ctx.fileInfos
.get(fileName)
?.ruleDefs.find((rule) => rule.dottedName === ruleName);
}
9 changes: 9 additions & 0 deletions server/src/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { GlobalConfig } from "./context";
import { tokenModifiers, tokenTypes } from "./semanticTokens";
import { PUBLICODES_FILE_EXTENSIONS } from "./parseRules";
import { PublicodesCommands } from "./codeAction";

export default function initialize(params: InitializeParams): {
config: GlobalConfig;
Expand All @@ -32,6 +33,14 @@ export default function initialize(params: InitializeParams): {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Full,

codeActionProvider: true,

documentSymbolProvider: true,

executeCommandProvider: {
commands: [PublicodesCommands.CREATE_RULE],
},

// Tell the client that this server supports code completion.
completionProvider: {
resolveProvider: true,
Expand Down
9 changes: 4 additions & 5 deletions server/src/parseRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ La règle '${dottedName}' est déjà définie dans le fichier : '${ruleFilePath}
}

ctx.fileInfos.set(filePath, {
// NOTE: not needed for now (we use the parsedRules from the engine)
ruleDefs,
rawRules,
tsTree,
Expand Down Expand Up @@ -159,8 +158,8 @@ La règle '${name}' est définie plusieurs fois dans le fichier.
const match = e.message.match(
/^Implicit map keys need to be followed by map values at line (\d+), column (\d+)/,
);
const line = Number(match?.[1]) - 1 ?? 0;
const column = Number(match?.[2]) - 1 ?? 0;
const line = Number(match?.[1] ?? 1) - 1;
const column = Number(match?.[2] ?? 1) - 1;
const name = e.message.match(/\s*(.*)\n\s*\^+/)?.[1] ?? "<nom>";

errors.push({
Expand All @@ -184,8 +183,8 @@ L'attribut '${name}' doit être suivi d'une valeur.
const match = e.message.match(
/^Implicit keys need to be on a single line at line (\d+), column (\d+)/,
);
const line = Number(match?.[1]) - 1 ?? 0;
const column = Number(match?.[2]) - 1 ?? 0;
const line = Number(match?.[1] ?? 1) - 1;
const column = Number(match?.[2] ?? 1) - 1;
const name = e.message.match(/\s*(.*)\n\s*\^+/)?.[1] ?? "<nom>";

errors.push({
Expand Down
47 changes: 45 additions & 2 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ProposedFeatures,
InitializeParams,
DeleteFilesParams,
SymbolKind,
} from "vscode-languageserver/node.js";
import { TextDocument } from "vscode-languageserver-textdocument";

Expand All @@ -18,8 +19,12 @@ import onHoverHandler from "./onHover";
import { semanticTokensFullProvider } from "./semanticTokens";
import Engine from "publicodes";
import { fileURLToPath } from "node:url";
import { deleteFileFromCtx } from "./helpers";
import { parseDocument } from "./parseRules";
import { deleteFileFromCtx, positionToRange } from "./helpers";
import {
codeActionHandler,
createRule,
PublicodesCommands,
} from "./codeAction";

let ctx: LSContext = {
// Create a connection for the server, using Node's IPC as a transport.
Expand Down Expand Up @@ -142,3 +147,41 @@ ctx.connection.workspace.onDidRenameFiles((e) => {
deleteFileFromCtx(ctx, oldUri);
});
});

ctx.connection.onCodeAction((params) => codeActionHandler(ctx, params));

ctx.connection.onExecuteCommand((params) => {
switch (params.command) {
case PublicodesCommands.CREATE_RULE: {
if (params.arguments == undefined || params.arguments.length === 0) {
ctx.connection.console.error(
`[onExecuteCommand] ${PublicodesCommands.CREATE_RULE} missing arguments`,
);
return;
}
createRule(ctx, params.arguments[0]);
break;
}
}
});

ctx.connection.onDocumentSymbol((params) => {
const uri = params.textDocument.uri;
const filePath = fileURLToPath(uri);
const fileInfo = ctx.fileInfos.get(filePath);
if (fileInfo == undefined) {
ctx.connection.console.error(
`[onDocumentSymbol] file info not found: ${filePath}`,
);
return [];
}

return fileInfo.ruleDefs.map(({ dottedName, namesPos, defPos }) => {
return {
name: dottedName,
kind: SymbolKind.Namespace,
range: positionToRange(defPos),
selectionRange: positionToRange(namesPos),
};
});
});
Loading
Loading