From 661bc5da418e104c25086a145f27a2095b2b2b35 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Tue, 14 Jan 2020 17:06:45 +0100 Subject: [PATCH] define constants in typescript-vscode-sh-plugin --- .../typescript-language-features/package.json | 2 +- .../src/features/semanticTokens.ts | 110 ++++++++---------- .../src/utils/api.ts | 1 - .../typescript-language-features/yarn.lock | 8 +- 4 files changed, 54 insertions(+), 67 deletions(-) diff --git a/extensions/typescript-language-features/package.json b/extensions/typescript-language-features/package.json index 681c5c4b2d1..4238a966b5b 100644 --- a/extensions/typescript-language-features/package.json +++ b/extensions/typescript-language-features/package.json @@ -19,7 +19,7 @@ "jsonc-parser": "^2.1.1", "rimraf": "^2.6.3", "semver": "5.5.1", - "typescript-vscode-sh-plugin": "^0.3.2", + "typescript-vscode-sh-plugin": "^0.5.0", "vscode-extension-telemetry": "0.1.1", "vscode-nls": "^4.0.0" }, diff --git a/extensions/typescript-language-features/src/features/semanticTokens.ts b/extensions/typescript-language-features/src/features/semanticTokens.ts index 78f7dfb36a8..e34dc09de9a 100644 --- a/extensions/typescript-language-features/src/features/semanticTokens.ts +++ b/extensions/typescript-language-features/src/features/semanticTokens.ts @@ -9,7 +9,10 @@ import * as Proto from '../protocol'; import { VersionDependentRegistration } from '../utils/dependentRegistration'; import API from '../utils/api'; -const minTypeScriptVersion = API.v370; +// all constants are const +import { TokenType, TokenModifier, TokenEncodingConsts, VersionRequirement } from 'typescript-vscode-sh-plugin/lib/constants'; + +const minTypeScriptVersion = API.fromVersionString(`${VersionRequirement.major}.${VersionRequirement.minor}`); export function register(selector: vscode.DocumentSelector, client: ITypeScriptServiceClient) { return new VersionDependentRegistration(client, minTypeScriptVersion, () => { @@ -29,18 +32,16 @@ class SemanticTokensProvider implements vscode.SemanticTokensProvider { } getLegend(): vscode.SemanticTokensLegend { - const tokenTypes = []; - for (let i = 0; i < VSCodeShPlugin.TokenType._sentinel; i++) { - tokenTypes.push(VSCodeShPlugin.TokenType[i]); + if (tokenTypes.length !== TokenType._) { + console.warn('typescript-vscode-sh-plugin has added new tokens types.'); } - const tokenModifiers = []; - for (let i = 0; i < VSCodeShPlugin.TokenModifier._sentinel; i++) { - tokenModifiers.push(VSCodeShPlugin.TokenModifier[i]); + if (tokenModifiers.length !== TokenModifier._) { + console.warn('typescript-vscode-sh-plugin has added new tokens modifiers.'); } return new vscode.SemanticTokensLegend(tokenTypes, tokenModifiers); } - async provideSemanticTokens(document: vscode.TextDocument, _options: vscode.SemanticTokensRequestOptions, token: vscode.CancellationToken): Promise { + async provideSemanticTokens(document: vscode.TextDocument, options: vscode.SemanticTokensRequestOptions, token: vscode.CancellationToken): Promise { const file = this.client.toOpenedFilePath(document); if (!file) { return null; @@ -51,8 +52,8 @@ class SemanticTokensProvider implements vscode.SemanticTokensProvider { const allTokenSpans: number[][] = []; let requestArgs: ExperimentalProtocol.EncodedSemanticClassificationsRequestArgs[] = []; - if (_options.ranges) { - requestArgs = _options.ranges.map(r => { const start = document.offsetAt(r.start); const length = document.offsetAt(r.end) - start; return { file, start, length }; }); + if (options.ranges) { + requestArgs = options.ranges.map(r => { const start = document.offsetAt(r.start); const length = document.offsetAt(r.end) - start; return { file, start, length }; }); requestArgs = requestArgs.sort((a1, a2) => a1.start - a2.start); } else { requestArgs = [{ file, start: 0, length: document.getText().length }]; // full document @@ -81,10 +82,10 @@ class SemanticTokensProvider implements vscode.SemanticTokensProvider { const tsClassification = tokenSpan[i++]; let tokenModifiers = 0; - let tokenType = VSCodeShPlugin.getTokenTypeFromClassification(tsClassification); + let tokenType = getTokenTypeFromClassification(tsClassification); if (tokenType !== undefined) { // it's a classification as returned by the typescript-vscode-sh-plugin - tokenModifiers = VSCodeShPlugin.getTokenModifierFromClassification(tsClassification); + tokenModifiers = getTokenModifierFromClassification(tsClassification); } else { // typescript-vscode-sh-plugin is not present tokenType = tokenTypeMap[tsClassification]; @@ -108,61 +109,48 @@ class SemanticTokensProvider implements vscode.SemanticTokensProvider { } } -namespace VSCodeShPlugin { +// typescript-vscode-sh-plugin encodes type and modifiers in the classification: +// TSClassification = (TokenType + 1) << 8 + TokenModifier - // typescript-vscode-sh-plugin encodes type and modifiers in the classification: - // TSClassification = (TokenType + 1) << 8 + TokenModifier - - const TokenTypeOffset = 8; - const TokenModifierMask = (1 << TokenTypeOffset) - 1; // 0xFF - - export function getTokenTypeFromClassification(tsClassification: number): number | undefined { - if (tsClassification > TokenModifierMask) { - return (tsClassification >> TokenTypeOffset) - 1; - } - return undefined; - } - - export function getTokenModifierFromClassification(tsClassification: number) { - return tsClassification & TokenModifierMask; - } - - // Don't change TokenType and TokenModifier enums without adopting typescript-vscode-sh-plugin - export enum TokenType { - 'class', - 'enum', - 'interface', - 'namespace', - 'typeParameter', - 'type', - 'parameter', - 'variable', - 'property', - 'constant', - 'function', - 'member', - _sentinel - } - - export enum TokenModifier { - 'declaration', - 'static', - 'async', - 'readonly', - _sentinel +function getTokenTypeFromClassification(tsClassification: number): number | undefined { + if (tsClassification > TokenEncodingConsts.modifierMask) { + return (tsClassification >> TokenEncodingConsts.typeOffset) - 1; } + return undefined; } -// mapping for the original ExperimentalProtocol.ClassificationType from TypeScript (only used when plugin is not available) +function getTokenModifierFromClassification(tsClassification: number) { + return tsClassification & TokenEncodingConsts.modifierMask; +} +const tokenTypes: string[] = []; +tokenTypes[TokenType.class] = 'class'; +tokenTypes[TokenType.enum] = 'enum'; +tokenTypes[TokenType.interface] = 'interface'; +tokenTypes[TokenType.namespace] = 'namespace'; +tokenTypes[TokenType.typeParameter] = 'typeParameter'; +tokenTypes[TokenType.type] = 'type'; +tokenTypes[TokenType.parameter] = 'parameter'; +tokenTypes[TokenType.variable] = 'variable'; +tokenTypes[TokenType.property] = 'property'; +tokenTypes[TokenType.function] = 'function'; +tokenTypes[TokenType.member] = 'member'; + +const tokenModifiers: string[] = []; +tokenModifiers[TokenModifier.async] = 'async'; +tokenModifiers[TokenModifier.declaration] = 'declaration'; +tokenModifiers[TokenModifier.readonly] = 'readonly'; +tokenModifiers[TokenModifier.static] = 'static'; + +// mapping for the original ExperimentalProtocol.ClassificationType from TypeScript (only used when plugin is not available) const tokenTypeMap: number[] = []; -tokenTypeMap[ExperimentalProtocol.ClassificationType.className] = VSCodeShPlugin.TokenType.class; -tokenTypeMap[ExperimentalProtocol.ClassificationType.enumName] = VSCodeShPlugin.TokenType.enum; -tokenTypeMap[ExperimentalProtocol.ClassificationType.interfaceName] = VSCodeShPlugin.TokenType.interface; -tokenTypeMap[ExperimentalProtocol.ClassificationType.moduleName] = VSCodeShPlugin.TokenType.namespace; -tokenTypeMap[ExperimentalProtocol.ClassificationType.typeParameterName] = VSCodeShPlugin.TokenType.typeParameter; -tokenTypeMap[ExperimentalProtocol.ClassificationType.typeAliasName] = VSCodeShPlugin.TokenType.type; -tokenTypeMap[ExperimentalProtocol.ClassificationType.parameterName] = VSCodeShPlugin.TokenType.parameter; +tokenTypeMap[ExperimentalProtocol.ClassificationType.className] = TokenType.class; +tokenTypeMap[ExperimentalProtocol.ClassificationType.enumName] = TokenType.enum; +tokenTypeMap[ExperimentalProtocol.ClassificationType.interfaceName] = TokenType.interface; +tokenTypeMap[ExperimentalProtocol.ClassificationType.moduleName] = TokenType.namespace; +tokenTypeMap[ExperimentalProtocol.ClassificationType.typeParameterName] = TokenType.typeParameter; +tokenTypeMap[ExperimentalProtocol.ClassificationType.typeAliasName] = TokenType.type; +tokenTypeMap[ExperimentalProtocol.ClassificationType.parameterName] = TokenType.parameter; namespace ExperimentalProtocol { diff --git a/extensions/typescript-language-features/src/utils/api.ts b/extensions/typescript-language-features/src/utils/api.ts index 8f181f14b65..0fa41cb01a0 100644 --- a/extensions/typescript-language-features/src/utils/api.ts +++ b/extensions/typescript-language-features/src/utils/api.ts @@ -31,7 +31,6 @@ export default class API { public static readonly v340 = API.fromSimpleString('3.4.0'); public static readonly v345 = API.fromSimpleString('3.4.5'); public static readonly v350 = API.fromSimpleString('3.5.0'); - public static readonly v370 = API.fromSimpleString('3.7.0'); public static readonly v380 = API.fromSimpleString('3.8.0'); public static fromVersionString(versionString: string): API { diff --git a/extensions/typescript-language-features/yarn.lock b/extensions/typescript-language-features/yarn.lock index cc6e217941d..086a2a9a106 100644 --- a/extensions/typescript-language-features/yarn.lock +++ b/extensions/typescript-language-features/yarn.lock @@ -626,10 +626,10 @@ tweetnacl@^0.14.3, tweetnacl@~0.14.0: resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64" integrity sha1-WuaBd/GS1EViadEIr6k/+HQ/T2Q= -typescript-vscode-sh-plugin@^0.3.2: - version "0.3.2" - resolved "https://registry.yarnpkg.com/typescript-vscode-sh-plugin/-/typescript-vscode-sh-plugin-0.3.2.tgz#bde9d0eba24ca5856024811fa354a9f5a7aeebe5" - integrity sha512-XsalETsSf3y7VWxk36plqHpsbl+TUDl278HEuhPHVBcNnwuTjcIq52J/CJw84xYmxmBcTIPUgIgLLS4OE5nX2A== +typescript-vscode-sh-plugin@^0.5.0: + version "0.5.0" + resolved "https://registry.yarnpkg.com/typescript-vscode-sh-plugin/-/typescript-vscode-sh-plugin-0.5.0.tgz#014dd928f2fa5000396147ed00792a2c901d97b9" + integrity sha512-MKqivbdkgllHS3Rab/zvXlGAxwCb1AHzgO/a8vmG6i5kExGIytwjUyXALdnnLUWS03B9eEJmIjzOz4y3MpgliQ== uri-js@^4.2.2: version "4.2.2"