use plugin

This commit is contained in:
Martin Aeschlimann 2020-01-09 17:36:45 +01:00
parent 741a8b2b2f
commit b8a7184825
4 changed files with 71 additions and 45 deletions

View file

@ -55,37 +55,42 @@ export function getSemanticTokens(jsLanguageService: ts.LanguageService, current
return resultTokens;
}
enum TokenType {
'class',
'enum',
'interface',
'namespace',
'typeParameter',
'type',
'parameter',
'variable',
'property',
'constant',
'function',
'member',
_sentinel
}
enum TokenModifier {
'declaration',
'static',
'async',
_sentinel
}
export function getSemanticTokenLegend() {
const tokenTypes = [];
for (let i = 0; i < TokenType._sentinel; i++) {
tokenTypes.push(TokenType[i]);
}
const tokenModifiers = [];
for (let i = 0; i < TokenModifier._sentinel; i++) {
tokenModifiers.push(TokenModifier[i]);
}
return { types: tokenTypes, modifiers: tokenModifiers };
}
const tokenTypes: string[] = ['class', 'enum', 'interface', 'namespace', 'typeParameter', 'type', 'parameter', 'variable', 'property', 'constant', 'function', 'member'];
const tokenModifiers: string[] = ['declaration', 'static', 'async'];
const enum TokenType {
'class' = 0,
'enum' = 1,
'interface' = 2,
'namespace' = 3,
'typeParameter' = 4,
'type' = 5,
'parameter' = 6,
'variable' = 7,
'property' = 8,
'constant' = 9,
'function' = 10,
'member' = 11
}
const enum TokenModifier {
'declaration' = 0x01,
'static' = 0x02,
'async' = 0x04,
}
const tokenFromDeclarationMapping: { [name: string]: TokenType } = {
[ts.SyntaxKind.VariableDeclaration]: TokenType.variable,
[ts.SyntaxKind.Parameter]: TokenType.parameter,

View file

@ -20,7 +20,8 @@
"rimraf": "^2.6.3",
"semver": "5.5.1",
"vscode-extension-telemetry": "0.1.1",
"vscode-nls": "^4.0.0"
"vscode-nls": "^4.0.0",
"typescript-vscode-sh-plugin":"^0.1.0"
},
"devDependencies": {
"@types/node": "^12.11.7",
@ -953,6 +954,11 @@
}
]
}
],
"typescriptServerPlugins": [
{
"name": "typescript-vscode-sh-plugin"
}
]
}
}

View file

@ -56,14 +56,19 @@ class SemanticTokensProvider implements vscode.SemanticTokensProvider {
return null;
}
const versionBeforeRequest = document.version;
if (_options.ranges) {
// const allArgs = _options.ranges.map(r => ({file, start: document.offsetAt(r.start), length: document.offsetAt(r.end) - document.offsetAt(r.start)}));
}
const args: ExperimentalProtocol.EncodedSemanticClassificationsRequestArgs = {
file: file,
start: 0,
length: document.getText().length,
};
const versionBeforeRequest = document.version;
const response = await (this.client as ExperimentalProtocol.IExtendedTypeScriptServiceClient).execute('encodedSemanticClassifications-full', args, token);
const versionAfterRequest = document.version;
@ -84,23 +89,33 @@ class SemanticTokensProvider implements vscode.SemanticTokensProvider {
const tsTokens = response.body.spans;
for (let i = 0, len = Math.floor(tsTokens.length / 3); i < len; i++) {
const tokenType = tokenTypeMap[tsTokens[3 * i + 2]];
if (typeof tokenType === 'number') {
console.log(TokenType[tokenType]);
const offset = tsTokens[3 * i];
const length = tsTokens[3 * i + 1];
// 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);
const endCharacter = (line === endPos.line ? endPos.character : document.lineAt(line).text.length);
builder.push(line, startCharacter, endCharacter - startCharacter, tokenType, 0);
const tsClassification = tsTokens[3 * i + 2];
let tokenType = 0;
let tokenModifiers = 0;
if (tsClassification > 0xFF) {
// classifications as returned by the typescript-vscode-sh-plugin
tokenType = (tsClassification >> 8) - 1;
tokenModifiers = tsClassification & 0xFF;
} else {
tokenType = tokenTypeMap[tsClassification];
if (tokenType === undefined) {
continue;
}
}
const offset = tsTokens[3 * i];
const length = tsTokens[3 * i + 1];
// 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);
const endCharacter = (line === endPos.line ? endPos.character : document.lineAt(line).text.length);
builder.push(line, startCharacter, endCharacter - startCharacter, tokenType, tokenModifiers);
}
}
return new vscode.SemanticTokens(builder.build());

View file

@ -78,7 +78,7 @@ export default class LanguageProvider extends Disposable {
import('./features/signatureHelp').then(provider => this._register(provider.register(selector, this.client))),
import('./features/tagClosing').then(provider => this._register(provider.register(selector, this.description.id, this.client))),
import('./features/typeDefinitions').then(provider => this._register(provider.register(selector, this.client))),
import('./features/semanticColoring').then(provider => this._register(provider.register(selector, this.client))),
import('./features/semanticTokens').then(provider => this._register(provider.register(selector, this.client))),
import('./features/callHierarchy').then(provider => this._register(provider.register(selector, this.client))),
]);
}