Split TS changes to separate PR
This commit is contained in:
parent
f7a26173af
commit
e7f8a94eb2
|
@ -1,124 +0,0 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import { ITypeScriptServiceClient, ExperimentalProtocol } from '../typescriptService';
|
||||
|
||||
function timeout(time: number): Promise<void> {
|
||||
return new Promise((resolve, _reject) => {
|
||||
setTimeout(resolve, time);
|
||||
});
|
||||
}
|
||||
|
||||
class SemanticColoringProvider implements vscode.SemanticColoringProvider {
|
||||
|
||||
constructor(
|
||||
private readonly client: ITypeScriptServiceClient
|
||||
) {
|
||||
}
|
||||
|
||||
getLegend(): vscode.SemanticColoringLegend {
|
||||
const tokens: string[] = [];
|
||||
|
||||
tokens[ExperimentalProtocol.ClassificationType.comment] = 'comment'; // ok
|
||||
tokens[ExperimentalProtocol.ClassificationType.identifier] = 'identifier';
|
||||
tokens[ExperimentalProtocol.ClassificationType.keyword] = 'keyword';
|
||||
tokens[ExperimentalProtocol.ClassificationType.numericLiteral] = 'numericLiteral';
|
||||
tokens[ExperimentalProtocol.ClassificationType.operator] = 'operator';
|
||||
tokens[ExperimentalProtocol.ClassificationType.stringLiteral] = 'stringLiteral';
|
||||
tokens[ExperimentalProtocol.ClassificationType.regularExpressionLiteral] = 'regularExpressionLiteral';
|
||||
tokens[ExperimentalProtocol.ClassificationType.whiteSpace] = 'whiteSpace';
|
||||
tokens[ExperimentalProtocol.ClassificationType.text] = 'text';
|
||||
tokens[ExperimentalProtocol.ClassificationType.punctuation] = 'punctuation';
|
||||
tokens[ExperimentalProtocol.ClassificationType.className] = 'class'; // ok
|
||||
tokens[ExperimentalProtocol.ClassificationType.enumName] = 'enum'; // ok
|
||||
tokens[ExperimentalProtocol.ClassificationType.interfaceName] = 'interface'; // ok
|
||||
tokens[ExperimentalProtocol.ClassificationType.moduleName] = 'moduleName';
|
||||
tokens[ExperimentalProtocol.ClassificationType.typeParameterName] = 'parameterType'; // ok
|
||||
tokens[ExperimentalProtocol.ClassificationType.typeAliasName] = 'typeAliasName';
|
||||
tokens[ExperimentalProtocol.ClassificationType.parameterName] = 'parameter'; // ok
|
||||
tokens[ExperimentalProtocol.ClassificationType.docCommentTagName] = 'docCommentTagName';
|
||||
tokens[ExperimentalProtocol.ClassificationType.jsxOpenTagName] = 'jsxOpenTagName';
|
||||
tokens[ExperimentalProtocol.ClassificationType.jsxCloseTagName] = 'jsxCloseTagName';
|
||||
tokens[ExperimentalProtocol.ClassificationType.jsxSelfClosingTagName] = 'jsxSelfClosingTagName';
|
||||
tokens[ExperimentalProtocol.ClassificationType.jsxAttribute] = 'jsxAttribute';
|
||||
tokens[ExperimentalProtocol.ClassificationType.jsxText] = 'jsxText';
|
||||
tokens[ExperimentalProtocol.ClassificationType.jsxAttributeStringLiteralValue] = 'jsxAttributeStringLiteralValue';
|
||||
tokens[ExperimentalProtocol.ClassificationType.bigintLiteral] = 'bigintLiteral';
|
||||
|
||||
return new vscode.SemanticColoringLegend(tokens, []);
|
||||
}
|
||||
|
||||
async provideSemanticColoring(document: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.SemanticColoring | null> {
|
||||
await timeout(0);
|
||||
|
||||
const file = this.client.toOpenedFilePath(document);
|
||||
if (!file) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const args: ExperimentalProtocol.EncodedSemanticClassificationsRequestArgs = {
|
||||
file: file,
|
||||
start: 0,
|
||||
length: document.getText().length,
|
||||
};
|
||||
|
||||
const versionBeforeRequest = document.version;
|
||||
const response = await this.client.execute('encodedSemanticClassifications-full', args, token);
|
||||
const versionAfterRequest = document.version;
|
||||
|
||||
if (versionBeforeRequest !== versionAfterRequest) {
|
||||
// A new request will come in soon...
|
||||
return null;
|
||||
}
|
||||
|
||||
if (response.type !== 'response') {
|
||||
return null;
|
||||
}
|
||||
if (!response.body) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const tsTokens = response.body.spans;
|
||||
|
||||
let result: number[] = [];
|
||||
let resultLen = 0;
|
||||
const pushResultToken = (line: number, startCharacter: number, endCharacter: number, tokenType: number): void => {
|
||||
result[resultLen++] = line;
|
||||
result[resultLen++] = startCharacter;
|
||||
result[resultLen++] = endCharacter;
|
||||
result[resultLen++] = tokenType;
|
||||
result[resultLen++] = 0;
|
||||
};
|
||||
|
||||
for (let i = 0, len = Math.floor(tsTokens.length / 3); i < len; i++) {
|
||||
const offset = tsTokens[3 * i];
|
||||
const length = tsTokens[3 * i + 1];
|
||||
const tokenType = tsTokens[3 * i + 2];
|
||||
|
||||
// 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);
|
||||
pushResultToken(line, startCharacter, endCharacter, tokenType);
|
||||
}
|
||||
}
|
||||
|
||||
return new vscode.SemanticColoring([new vscode.SemanticColoringArea(0, new Uint32Array(result))]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export function register(
|
||||
selector: vscode.DocumentSelector,
|
||||
client: ITypeScriptServiceClient
|
||||
) {
|
||||
const provider = new SemanticColoringProvider(client);
|
||||
return vscode.languages.registerSemanticColoringProvider(selector, provider, provider.getLegend());
|
||||
}
|
|
@ -74,7 +74,6 @@ export default class LanguageProvider extends Disposable {
|
|||
import('./features/references').then(provider => this._register(provider.register(selector, this.client))),
|
||||
import('./features/referencesCodeLens').then(provider => this._register(provider.register(selector, this.description.id, this.client, cachedResponse))),
|
||||
import('./features/rename').then(provider => this._register(provider.register(selector, this.client, this.fileConfigurationManager))),
|
||||
import('./features/semanticColoring').then(provider => this._register(provider.register(selector, this.client))),
|
||||
import('./features/smartSelect').then(provider => this._register(provider.register(selector, this.client))),
|
||||
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))),
|
||||
|
|
|
@ -26,74 +26,6 @@ export namespace ServerResponse {
|
|||
export type Response<T extends Proto.Response> = T | Cancelled | typeof NoContent;
|
||||
}
|
||||
|
||||
export namespace ExperimentalProtocol {
|
||||
/**
|
||||
* A request to get encoded semantic classifications for a span in the file
|
||||
*/
|
||||
export interface EncodedSemanticClassificationsRequest extends Proto.FileRequest {
|
||||
arguments: EncodedSemanticClassificationsRequestArgs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Arguments for EncodedSemanticClassificationsRequest request.
|
||||
*/
|
||||
export interface EncodedSemanticClassificationsRequestArgs extends Proto.FileRequestArgs {
|
||||
/**
|
||||
* Start position of the span.
|
||||
*/
|
||||
start: number;
|
||||
/**
|
||||
* Length of the span.
|
||||
*/
|
||||
length: number;
|
||||
}
|
||||
|
||||
export const enum EndOfLineState {
|
||||
None,
|
||||
InMultiLineCommentTrivia,
|
||||
InSingleQuoteStringLiteral,
|
||||
InDoubleQuoteStringLiteral,
|
||||
InTemplateHeadOrNoSubstitutionTemplate,
|
||||
InTemplateMiddleOrTail,
|
||||
InTemplateSubstitutionPosition,
|
||||
}
|
||||
|
||||
export const enum ClassificationType {
|
||||
comment = 1,
|
||||
identifier = 2,
|
||||
keyword = 3,
|
||||
numericLiteral = 4,
|
||||
operator = 5,
|
||||
stringLiteral = 6,
|
||||
regularExpressionLiteral = 7,
|
||||
whiteSpace = 8,
|
||||
text = 9,
|
||||
punctuation = 10,
|
||||
className = 11,
|
||||
enumName = 12,
|
||||
interfaceName = 13,
|
||||
moduleName = 14,
|
||||
typeParameterName = 15,
|
||||
typeAliasName = 16,
|
||||
parameterName = 17,
|
||||
docCommentTagName = 18,
|
||||
jsxOpenTagName = 19,
|
||||
jsxCloseTagName = 20,
|
||||
jsxSelfClosingTagName = 21,
|
||||
jsxAttribute = 22,
|
||||
jsxText = 23,
|
||||
jsxAttributeStringLiteralValue = 24,
|
||||
bigintLiteral = 25,
|
||||
}
|
||||
|
||||
export interface EncodedSemanticClassificationsResponse extends Proto.Response {
|
||||
body?: {
|
||||
endOfLineState: EndOfLineState;
|
||||
spans: number[];
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
interface StandardTsServerRequests {
|
||||
'applyCodeActionCommand': [Proto.ApplyCodeActionCommandRequestArgs, Proto.ApplyCodeActionCommandResponse];
|
||||
'completionEntryDetails': [Proto.CompletionDetailsRequestArgs, Proto.CompletionDetailsResponse];
|
||||
|
@ -104,7 +36,6 @@ interface StandardTsServerRequests {
|
|||
'definitionAndBoundSpan': [Proto.FileLocationRequestArgs, Proto.DefinitionInfoAndBoundSpanReponse];
|
||||
'docCommentTemplate': [Proto.FileLocationRequestArgs, Proto.DocCommandTemplateResponse];
|
||||
'documentHighlights': [Proto.DocumentHighlightsRequestArgs, Proto.DocumentHighlightsResponse];
|
||||
'encodedSemanticClassifications-full': [ExperimentalProtocol.EncodedSemanticClassificationsRequestArgs, ExperimentalProtocol.EncodedSemanticClassificationsResponse];
|
||||
'format': [Proto.FormatRequestArgs, Proto.FormatResponse];
|
||||
'formatonkey': [Proto.FormatOnKeyRequestArgs, Proto.FormatResponse];
|
||||
'getApplicableRefactors': [Proto.GetApplicableRefactorsRequestArgs, Proto.GetApplicableRefactorsResponse];
|
||||
|
|
Loading…
Reference in a new issue