test-SemanticColoringProvider

This commit is contained in:
Martin Aeschlimann 2019-11-21 20:45:13 +01:00
parent fc704d942c
commit 28909fd2ad
4 changed files with 76 additions and 1 deletions

View file

@ -5,12 +5,20 @@
"publisher": "vscode",
"license": "MIT",
"private": true,
"activationEvents": [
"onLanguage:json"
],
"main": "./out/colorizerTestMain",
"enableProposedApi": true,
"engines": {
"vscode": "*"
},
"scripts": {
"vscode:prepublish": "node ../../node_modules/gulp/bin/gulp.js --gulpfile ../../build/gulpfile.extensions.js compile-extension:vscode-colorize-tests ./tsconfig.json"
},
"dependencies": {
"jsonc-parser": "2.2.0"
},
"devDependencies": {
"@types/node": "^12.11.7",
"mocha-junit-reporter": "^1.17.0",

View file

@ -0,0 +1,60 @@
/*---------------------------------------------------------------------------------------------
* 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 * 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 legend = new vscode.SemanticColoringLegend(tokenTypes, tokenModifiers);
/*
* A certain token (at index `i` is encoded using 5 uint32 integers):
* - at index `5*i` - `deltaLine`: token line number, relative to `SemanticColoringArea.line`
* - at index `5*i+1` - `startCharacter`: token start character offset inside the line (inclusive)
* - at index `5*i+2` - `endCharacter`: token end character offset inside the line (exclusive)
* - at index `5*i+3` - `tokenType`: will be looked up in `SemanticColoringLegend.tokenTypes`
* - at index `5*i+4` - `tokenModifiers`: each set bit will be looked up in `SemanticColoringLegend.tokenModifiers`
*/
const semanticHighlightProvider: vscode.SemanticColoringProvider = {
provideSemanticColoring(document: vscode.TextDocument): vscode.ProviderResult<vscode.SemanticColoring> {
const result: number[] = [];
const visitor: jsoncParser.JSONVisitor = {
onObjectProperty: (property: string, _offset: number, length: number, startLine: number, startCharacter: number) => {
result.push(startLine);
result.push(startCharacter);
result.push(startCharacter + length);
const segments = property.split('.');
let tokenType = legend.tokenTypes.indexOf(segments[0]);
if (tokenType === -1) {
tokenType = 0;
}
result.push(tokenType);
let tokenModifiers = 0;
for (let i = 1; i < segments.length; i++) {
const index = legend.tokenTypes.indexOf(segments[0]);
if (index !== -1) {
tokenModifiers = tokenModifiers | 1 << index;
}
}
result.push(tokenModifiers);
}
};
jsoncParser.visit(document.getText(), visitor);
return new vscode.SemanticColoring([new vscode.SemanticColoringArea(0, new Uint32Array(result))]);
}
};
context.subscriptions.push(vscode.languages.registerSemanticColoringProvider({ pattern: 'color-test.json' }, semanticHighlightProvider, legend));
}

View file

@ -3,5 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/// <reference path="../../../../src/vs/vscode.d.ts" />
/// <reference path="../../../../src/vs/vscode.proposed.d.ts" />
/// <reference types='@types/node'/>
/// <reference path='../../../../src/vs/vscode.d.ts'/>

View file

@ -1042,6 +1042,11 @@ json3@3.3.2:
resolved "https://registry.yarnpkg.com/json3/-/json3-3.3.2.tgz#3c0434743df93e2f5c42aee7b19bcb483575f4e1"
integrity sha1-PAQ0dD35Pi9cQq7nsZvLSDV19OE=
jsonc-parser@2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/jsonc-parser/-/jsonc-parser-2.2.0.tgz#f206f87f9d49d644b7502052c04e82dd6392e9ef"
integrity sha512-4fLQxW1j/5fWj6p78vAlAafoCKtuBm6ghv+Ij5W2DrDx0qE+ZdEl2c6Ko1mgJNF5ftX1iEWQQ4Ap7+3GlhjkOA==
jsonify@~0.0.0:
version "0.0.0"
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"