Skip to content

Commit

Permalink
Merge pull request #448 from KxSystems/ee-color
Browse files Browse the repository at this point in the history
[LS] Semantic highlighting for locals
  • Loading branch information
ecmel authored Nov 5, 2024
2 parents 7605a62 + 9be7801 commit 86d9f73
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
54 changes: 47 additions & 7 deletions server/src/qLangServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
RenameParams,
SelectionRange,
SelectionRangeParams,
SemanticTokens,
SemanticTokensParams,
ServerCapabilities,
SymbolKind,
TextDocumentChangeEvent,
Expand Down Expand Up @@ -71,6 +73,7 @@ import {
SemiColon,
WhiteSpace,
RCurly,
local,
} from "./parser";
import { lint } from "./linter";
import { readFileSync } from "node:fs";
Expand Down Expand Up @@ -125,6 +128,9 @@ export default class QLangServer {
this.connection.languages.callHierarchy.onOutgoingCalls(
this.onOutgoingCallsCallHierarchy.bind(this),
);
this.connection.languages.semanticTokens.on(
this.onSemanticTokens.bind(this),
);
this.connection.onDidChangeConfiguration(
this.onDidChangeConfiguration.bind(this),
);
Expand All @@ -149,6 +155,13 @@ export default class QLangServer {
completionProvider: { resolveProvider: false },
selectionRangeProvider: true,
callHierarchyProvider: true,
semanticTokensProvider: {
full: true,
legend: {
tokenTypes: ["variable"],
tokenModifiers: ["declaration", "readonly"],
},
},
};
}

Expand Down Expand Up @@ -445,6 +458,33 @@ export default class QLangServer {
: [];
}

public onSemanticTokens({
textDocument,
}: SemanticTokensParams): SemanticTokens {
const tokens = this.parse({ uri: textDocument.uri });
const result = { data: [] } as SemanticTokens;
let range: Range = Range.create(0, 0, 0, 0);
let line = 0;
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,
);
}
}
return result;
}

/* istanbul ignore next */
public scan() {
const folders = this.params.workspaceFolders;
Expand Down Expand Up @@ -487,13 +527,13 @@ export default class QLangServer {
private context({ uri, tokens }: Tokenized, all = true): Tokenized[] {
if (all) {
this.documents.all().forEach((document) => {
const path = fileURLToPath(document.uri);
if (path) {
this.cached.set(
pathToFileURL(path).toString(),
document.uri === uri ? tokens : parse(document.getText()),
);
}
const path = document.uri.startsWith("file://")
? fileURLToPath(document.uri)
: "";
this.cached.set(
path ? pathToFileURL(path).toString() : document.uri,
document.uri === uri ? tokens : parse(document.getText()),
);
});
return Array.from(this.cached.entries(), (entry) => ({
uri: entry[0],
Expand Down
12 changes: 12 additions & 0 deletions test/suite/qLangServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ describe("qLangServer", () => {
onIncomingCalls() {},
onOutgoingCalls() {},
},
semanticTokens: {
on() {},
},
},
sendDiagnostics() {},
});
Expand All @@ -99,6 +102,7 @@ describe("qLangServer", () => {
assert.ok(capabilities.completionProvider);
assert.ok(capabilities.selectionRangeProvider);
assert.ok(capabilities.callHierarchyProvider);
assert.ok(capabilities.semanticTokensProvider);
});
});

Expand Down Expand Up @@ -376,6 +380,14 @@ describe("qLangServer", () => {
});
});

describe("onSemanticTokens", () => {
it("should find semantic local variables", () => {
const params = createDocument("a:{[b;c]d:1;b*c*d}");
const result = server.onSemanticTokens(params);
assert.strictEqual(result.data.length, 30);
});
});

describe("setSettings", () => {
let defaultSettings = {
debug: false,
Expand Down

0 comments on commit 86d9f73

Please sign in to comment.