Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LS] Semantic highlighting for lambda definitions #479

Merged
merged 1 commit into from
Dec 17, 2024
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
8 changes: 3 additions & 5 deletions server/src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ function assignment(state: State, token: Token) {
} else {
let top = peek(stack);
if (top?.tokenType === Colon || top?.tokenType === DoubleColon) {
token.assignment = [top];
stack.pop();
if (stack.length > 0) {
token.assignment.push(...stack);
clear(stack);
token.assignment = [];
while ((top = stack.pop())) {
token.assignment.push(top);
}
}
stack.push(token);
Expand Down
4 changes: 4 additions & 0 deletions server/src/parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ export function tokenId(token: Token) {
return (token.tokenType.tokenTypeIdx?.toString(16) || "").padStart(2, "0");
}

export function lamdaDefinition(token: Token) {
return token.assignment?.find((value) => value.tokenType === LCurly);
}

export const enum FindKind {
Reference,
Definition,
Expand Down
31 changes: 18 additions & 13 deletions server/src/qLangServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ import {
WhiteSpace,
RCurly,
local,
LCurly,
lamdaDefinition,
} from "./parser";
import { lint } from "./linter";
import { readFileSync } from "node:fs";
Expand Down Expand Up @@ -158,7 +160,7 @@ export default class QLangServer {
semanticTokensProvider: {
full: true,
legend: {
tokenTypes: ["variable"],
tokenTypes: ["variable", "function"],
tokenModifiers: ["declaration", "readonly"],
},
},
Expand Down Expand Up @@ -468,18 +470,21 @@ export default class QLangServer {
let character = 0;
let delta = 0;
for (const token of tokens) {
if (assignable(token) && local(token, tokens)) {
line = range.start.line;
character = range.start.character;
range = rangeFromToken(token);
delta = range.start.line - line;
result.data.push(
delta,
delta ? range.start.character : range.start.character - character,
token.image.length,
0,
3,
);
if (assignable(token)) {
let kind = lamdaDefinition(token) ? 1 : local(token, tokens) ? 0 : -1;
if (kind >= 0) {
line = range.start.line;
character = range.start.character;
range = rangeFromToken(token);
delta = range.start.line - line;
result.data.push(
delta,
delta ? range.start.character : range.start.character - character,
token.image.length,
kind,
3,
);
}
}
}
return result;
Expand Down
6 changes: 3 additions & 3 deletions test/suite/qLangServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,19 @@ describe("qLangServer", () => {
it("should tokenize local variables", () => {
const params = createDocument("a:{[b;c]d:1;b*c*d}");
const result = server.onSemanticTokens(params);
assert.strictEqual(result.data.length, 30);
assert.strictEqual(result.data.length, 35);
});

it("should ignore qualified variables", () => {
const params = createDocument("a:{.ns.b:1;.ns.b}");
const result = server.onSemanticTokens(params);
assert.strictEqual(result.data.length, 0);
assert.strictEqual(result.data.length, 5);
});

it("should detect empty lists", () => {
const params = createDocument("a:{b:();b}");
const result = server.onSemanticTokens(params);
assert.strictEqual(result.data.length, 10);
assert.strictEqual(result.data.length, 15);
});
});

Expand Down
Loading