typescript: enable built-in 2020 semantic token support

This commit is contained in:
Martin Aeschlimann
2021-03-17 00:11:51 +01:00
parent 1b69563545
commit c09b3d2358
3 changed files with 43 additions and 17 deletions

View File

@@ -21,7 +21,7 @@
"dependencies": {
"jsonc-parser": "^2.2.1",
"semver": "5.5.1",
"typescript-vscode-sh-plugin": "^0.6.14",
"typescript-vscode-sh-plugin": "^0.7.1",
"vscode-extension-telemetry": "0.1.1",
"vscode-nls": "^4.1.1"
},

View File

@@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/
// all constants are const
import { TokenEncodingConsts, TokenModifier, TokenType, VersionRequirement } from 'typescript-vscode-sh-plugin/lib/constants';
import * as vscode from 'vscode';
import * as Proto from '../protocol';
import { ClientCapability, ExecConfig, ITypeScriptServiceClient, ServerResponse } from '../typescriptService';
@@ -67,7 +66,7 @@ class DocumentSemanticTokensProvider implements vscode.DocumentSemanticTokensPro
return this._provideSemanticTokens(document, { file, start, length }, token);
}
async _provideSemanticTokens(document: vscode.TextDocument, requestArg: ExperimentalProtocol.EncodedSemanticClassificationsRequestArgs, token: vscode.CancellationToken): Promise<vscode.SemanticTokens | null> {
async _provideSemanticTokens(document: vscode.TextDocument, requestArg: Proto.EncodedSemanticClassificationsRequestArgs, token: vscode.CancellationToken): Promise<vscode.SemanticTokens | null> {
const file = this.client.toOpenedFilePath(document);
if (!file) {
return null;
@@ -75,6 +74,8 @@ class DocumentSemanticTokensProvider implements vscode.DocumentSemanticTokensPro
const versionBeforeRequest = document.version;
requestArg.format = '2020';
const response = await (this.client as ExperimentalProtocol.IExtendedTypeScriptServiceClient).execute('encodedSemanticClassifications-full', requestArg, token, {
cancelOnResourceChange: document.uri
});
@@ -148,9 +149,42 @@ function waitForDocumentChangesToEnd(document: vscode.TextDocument) {
}
// typescript-vscode-sh-plugin encodes type and modifiers in the classification:
// typescript encodes type and modifiers in the classification:
// TSClassification = (TokenType + 1) << 8 + TokenModifier
declare const enum TokenType {
class = 0,
enum = 1,
interface = 2,
namespace = 3,
typeParameter = 4,
type = 5,
parameter = 6,
variable = 7,
enumMember = 8,
property = 9,
function = 10,
method = 11,
_ = 12
}
declare const enum TokenModifier {
declaration = 0,
static = 1,
async = 2,
readonly = 3,
defaultLibrary = 4,
local = 5,
_ = 6
}
declare const enum TokenEncodingConsts {
typeOffset = 8,
modifierMask = 255
}
declare const enum VersionRequirement {
major = 3,
minor = 7
}
function getTokenTypeFromClassification(tsClassification: number): number | undefined {
if (tsClassification > TokenEncodingConsts.modifierMask) {
return (tsClassification >> TokenEncodingConsts.typeOffset) - 1;
@@ -174,7 +208,7 @@ tokenTypes[TokenType.variable] = 'variable';
tokenTypes[TokenType.enumMember] = 'enumMember';
tokenTypes[TokenType.property] = 'property';
tokenTypes[TokenType.function] = 'function';
tokenTypes[TokenType.member] = 'method';
tokenTypes[TokenType.method] = 'method';
const tokenModifiers: string[] = [];
tokenModifiers[TokenModifier.async] = 'async';
@@ -184,14 +218,6 @@ tokenModifiers[TokenModifier.static] = 'static';
tokenModifiers[TokenModifier.local] = 'local';
tokenModifiers[TokenModifier.defaultLibrary] = 'defaultLibrary';
// make sure token types and modifiers are complete
if (tokenTypes.filter(t => !!t).length !== TokenType._) {
console.warn('typescript-vscode-sh-plugin has added new tokens types.');
}
if (tokenModifiers.filter(t => !!t).length !== TokenModifier._) {
console.warn('typescript-vscode-sh-plugin has added new tokens modifiers.');
}
// mapping for the original ExperimentalProtocol.ClassificationType from TypeScript (only used when plugin is not available)
const tokenTypeMap: number[] = [];
tokenTypeMap[ExperimentalProtocol.ClassificationType.className] = TokenType.class;

View File

@@ -74,10 +74,10 @@ semver@^5.3.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==
typescript-vscode-sh-plugin@^0.6.14:
version "0.6.14"
resolved "https://registry.yarnpkg.com/typescript-vscode-sh-plugin/-/typescript-vscode-sh-plugin-0.6.14.tgz#a81031b502f6346a26ea49ce082438c3e353bb38"
integrity sha512-AkNlRBbI6K7gk29O92qthNSvc6jjmNQ6isVXoYxkFwPa8D04tIv2SOPd+sd+mNpso4tNdL2gy7nVtrd5yFqvlA==
typescript-vscode-sh-plugin@^0.7.1:
version "0.7.1"
resolved "https://registry.yarnpkg.com/typescript-vscode-sh-plugin/-/typescript-vscode-sh-plugin-0.7.1.tgz#0be744032aa7fdcb764497342fd0fd2d093ed7ca"
integrity sha512-upXxgeI21tEMJxRVxF4PekQBM+eF0cRp0GgD2sblQkEm43NR+Fcbusa+FKAekUgwkbjVq1Loq+vKCCUbxM6R8A==
vscode-extension-telemetry@0.1.1:
version "0.1.1"