From 1aa566375a12d0975342d793e27ebf88b50bb241 Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sat, 17 Feb 2024 08:43:59 +0800 Subject: [PATCH] fix(typescript): semantic tokens result inconsistent with vscode --- .../typescript/lib/features/semanticTokens.ts | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/packages/typescript/lib/features/semanticTokens.ts b/packages/typescript/lib/features/semanticTokens.ts index 5945e444..3162846c 100644 --- a/packages/typescript/lib/features/semanticTokens.ts +++ b/packages/typescript/lib/features/semanticTokens.ts @@ -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'); @@ -32,7 +28,7 @@ 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) { @@ -40,28 +36,21 @@ export function register(ctx: SharedContext) { 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);