在vscode 团队的帮助下,下面的代码可以正常工作。
包.json
...
"activationEvents": [
"onLanguage:json",
"onLanguage:jsonc"
],
"main": "./src/extension",
"dependencies": {
"jsonc": "^0.1.0",
"jsonc-parser": "^1.0.0",
"vscode-nls": "^3.2.1"
},
...
src/extension.js
'use strict';
const path = require( 'path' );
const vscode = require( 'vscode' );
const { getLocation, visit, parse, ParseError, ParseErrorCode } = require( 'jsonc-parser' );
module.exports = {
activate
};
let pendingFooJsonDecoration;
const decoration = vscode.window.createTextEditorDecorationType( {
color: '#04f1f9' // something like cyan
} );
// wire up *.foo.json decorations
function activate ( context /* vscode.ExtensionContext */) {
// decorate when changing the active editor editor
context.subscriptions.push( vscode.window.onDidChangeActiveTextEditor( editor => updateFooJsonDecorations( editor ), null, context.subscriptions ) );
// decorate when the document changes
context.subscriptions.push( vscode.workspace.onDidChangeTextDocument( event => {
if ( vscode.window.activeTextEditor && event.document === vscode.window.activeTextEditor.document ) {
if ( pendingFooJsonDecoration ) {
clearTimeout( pendingFooJsonDecoration );
}
pendingFooJsonDecoration = setTimeout( () => updateFooJsonDecorations( vscode.window.activeTextEditor ), 1000);
}
}, null, context.subscriptions ) );
// decorate the active editor now
updateFooJsonDecorations( vscode.window.activeTextEditor );
// decorate when then cursor moves
context.subscriptions.push( new EditorEventHandler() );
}
const substitutionRegex = /\$\{[\w\:\.]+\}/g;
function updateFooJsonDecorations ( editor /* vscode.TextEditor */ ) {
if ( !editor || !path.basename( editor.document.fileName ).endsWith( '.foo.json' ) ) {
return;
}
const ranges /* vscode.Range[] */ = [];
visit( editor.document.getText(), {
onLiteralValue: ( value, offset, length ) => {
const matches = [];
let match;
while ( ( match = substitutionRegex.exec( value ) ) !== null) {
matches.push( match );
const start = offset + match.index + 1;
const end = match.index + 1 + offset + match[ 0 ].length;
ranges.push( new vscode.Range( editor.document.positionAt( start ), editor.document.positionAt( end ) ) );
}
}
});
editor.setDecorations( decoration, ranges );
}
class EditorEventHandler {
constructor () {
let subscriptions /*: Disposable[] */ = [];
vscode.window.onDidChangeTextEditorSelection( ( e /* TextEditorSelectionChangeEvent */ ) => {
if ( e.textEditor === vscode.window.activeTextEditor) {
updateFooJsonDecorations( e.textEditor );
}
}, this, subscriptions );
this._disposable = vscode.Disposable.from( ...subscriptions );
}
dispose () {
this._disposable.dispose();
}
}