Skip to content

Commit

Permalink
fix(typescript): semantic tokens result inconsistent with vscode
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Feb 17, 2024
1 parent 2573f78 commit 1aa5663
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions packages/typescript/lib/features/semanticTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ export function register(ctx: SharedContext) {
const length = range ? (document.offsetAt(range.end) - start) : document.getText().length;

if (ctx.language.typescript?.languageServiceHost.getCancellationToken?.().isCancellationRequested()) return;
const response2 = safeCall(() => ctx.languageService.getEncodedSyntacticClassifications(file, { start, length }));
if (!response2) return;

if (ctx.language.typescript?.languageServiceHost.getCancellationToken?.().isCancellationRequested()) return;
const response1 = safeCall(() => ctx.languageService.getEncodedSemanticClassifications(file, { start, length }, ts.SemanticClassificationFormat.TwentyTwenty));
if (!response1) return;
const response = safeCall(() => ctx.languageService.getEncodedSemanticClassifications(file, { start, length }, ts.SemanticClassificationFormat.TwentyTwenty));
if (!response) return;

let tokenModifiersTable: number[] = [];
tokenModifiersTable[TokenModifier.async] = 1 << legend.tokenModifiers.indexOf('async');
Expand All @@ -32,36 +28,29 @@ export function register(ctx: SharedContext) {
tokenModifiersTable[TokenModifier.defaultLibrary] = 1 << legend.tokenModifiers.indexOf('defaultLibrary');
tokenModifiersTable = tokenModifiersTable.map(mod => Math.max(mod, 0));

const tokenSpan = [...response1.spans, ...response2.spans];
const tokenSpan = response.spans;
const tokens: [number, number, number, number, number][] = [];
let i = 0;
while (i < tokenSpan.length) {
const offset = tokenSpan[i++];
const length = tokenSpan[i++];
const tsClassification = tokenSpan[i++];

let tokenModifiers = 0;
let tokenType = getTokenTypeFromClassification(tsClassification);
if (tokenType !== undefined) {
// it's a classification as returned by the typescript-vscode-sh-plugin
tokenModifiers = getTokenModifierFromClassification(tsClassification);
} else {
// typescript-vscode-sh-plugin is not present
tokenType = tokenTypeMap[tsClassification];
if (tokenType === undefined) {
continue;
}
const tokenType = getTokenTypeFromClassification(tsClassification);
if (tokenType === undefined) {
continue;
}

const tokenModifiers = getTokenModifierFromClassification(tsClassification);

// we can use the document's range conversion methods because the result is at the same version as the document
const startPos = document.positionAt(offset);
const endPos = document.positionAt(offset + length);
const serverToken = tsTokenTypeToServerTokenType(tokenType);
if (serverToken === undefined) {
continue;
}

const serverTokenModifiers = tsTokenModifierToServerTokenModifier(tokenModifiers);
// we can use the document's range conversion methods because the result is at the same version as the document
const startPos = document.positionAt(offset);
const endPos = document.positionAt(offset + length);

for (let line = startPos.line; line <= endPos.line; line++) {
const startCharacter = (line === startPos.line ? startPos.character : 0);
Expand Down

0 comments on commit 1aa5663

Please sign in to comment.