update semantic test

This commit is contained in:
Martin Aeschlimann 2019-12-03 09:44:10 +01:00
parent 491a638b98
commit 56c6acd2fe

View file

@ -8,31 +8,42 @@ import * as jsoncParser from 'jsonc-parser';
export function activate(context: vscode.ExtensionContext): any {
const tokenModifiers = ['static', 'abstract', 'deprecated'];
const tokenTypes = ['strings', 'types', 'structs', 'classes', 'functions', 'variables'];
const tokenModifiers = ['static', 'abstract', 'deprecated', 'declaration', 'documentation', 'member', 'async'];
const tokenTypes = ['types', 'structs', 'classes', 'interfaces', 'enums', 'parameterTypes', 'functions', 'variables'];
const legend = new vscode.SemanticTokensLegend(tokenTypes, tokenModifiers);
const semanticHighlightProvider: vscode.SemanticTokensProvider = {
provideSemanticTokens(document: vscode.TextDocument): vscode.ProviderResult<vscode.SemanticTokens> {
const builder = new vscode.SemanticTokensBuilder();
function addToken(type: string, modifiers: string[], startLine: number, startCharacter: number, length: number) {
let tokenType = legend.tokenTypes.indexOf(type);
if (tokenType === -1) {
tokenType = 0;
}
let tokenModifiers = 0;
for (let i = 0; i < modifiers.length; i++) {
const index = legend.tokenModifiers.indexOf(modifiers[i]);
if (index !== -1) {
tokenModifiers = tokenModifiers | 1 << index;
}
}
builder.push(startLine, startCharacter, length, tokenType, tokenModifiers);
}
const visitor: jsoncParser.JSONVisitor = {
onObjectProperty: (property: string, _offset: number, length: number, startLine: number, startCharacter: number) => {
const [type, ...modifiers] = property.split('.');
let tokenType = legend.tokenTypes.indexOf(type);
if (tokenType === -1) {
tokenType = 0;
addToken(type, modifiers, startLine, startCharacter, length);
},
onLiteralValue: (value: any, _offset: number, length: number, startLine: number, startCharacter: number) => {
if (typeof value === 'string') {
const [type, ...modifiers] = value.split('.');
addToken(type, modifiers, startLine, startCharacter, length);
}
let tokenModifiers = 0;
for (let i = 0; i < modifiers.length; i++) {
const index = legend.tokenModifiers.indexOf(modifiers[i]);
if (index !== -1) {
tokenModifiers = tokenModifiers | 1 << index;
}
}
builder.push(startLine, startCharacter, length, tokenType, tokenModifiers);
}
};
jsoncParser.visit(document.getText(), visitor);
@ -42,6 +53,6 @@ export function activate(context: vscode.ExtensionContext): any {
};
context.subscriptions.push(vscode.languages.registerSemanticTokensProvider({ pattern: '**/color-test.json' }, semanticHighlightProvider, legend));
context.subscriptions.push(vscode.languages.registerSemanticTokensProvider({ pattern: '**/*semantic-test.json' }, semanticHighlightProvider, legend));
}