Skip to content

Commit

Permalink
implemented semantic highlighting for lambda definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ecmel committed Dec 16, 2024
1 parent 82f3a31 commit b85f5eb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
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

0 comments on commit b85f5eb

Please sign in to comment.