问题描述
场景
我有描述一系列要执行的任务的JSON文件,其中每个任务可以引用JSON文件中的其他任务和对象.
I have JSON files that describe a series of tasks to be carried out, where each task can refer to other tasks and objects in the JSON file.
{
"tasks": [
{ "id": "first", "action": "doSomething()", "result": {} },
{ "id": "second", "action": "doSomething(${id:first.result})", "result": {} },
]
}
我想同时具有JSON模式验证和自定义语言文本效果,例如关键字着色,甚至在JSON字符串中都支持转到定义".
I'd like to have both JSON schema validation and custom language text effects like keyword coloring and even "Go to definition" support within strings in the JSON.
我能做什么
我可以创建一个扩展名,该扩展名为文件扩展名"* .foo.json"指定JSON模式.如果vscode将文件识别为JSON文件,则可以在编辑器中进行模式验证和代码完成.
I can create an extension that specifies a JSON schema for a file extension "*.foo.json". This gives schema validation and code completion in the editor if vscode recognizes the file as a JSON file.
我还可以在扩展名中为"* .foo.json"文件创建一种新的"foo"语言,该文件在JSON字符串中具有自定义关键字颜色.通过创建从JSON.tmLanguage.json复制的TextMate(* .tmLanguage.json)文件,然后修改"stringcontent"定义来实现此目的.
I can also create a new "foo" language in the extension for "*.foo.json" files that has custom keyword coloring within the JSON strings. I do this by creating a TextMate (*.tmLanguage.json) file copied from the JSON.tmLanguage.json, and then modifying the "stringcontent" definition.
问题
问题是模式验证和提示仅在我在状态栏中选择了"JSON"作为文件类型时才起作用,而自定义文本着色仅在我在状态栏中选择了"foo"作为文件类型时才起作用文件类型.
The problem is that the schema validation and hints only work if I have "JSON" chosen in the status bar as the file type, and the custom text coloring only works if I have "foo" chosen in the status bar as the file type.
有什么办法可以同时拥有两者?我可以以某种方式在vscode中扩展JSON语言处理吗?
Is there any way to have both at once? Can I somehow extend the JSON language handling within vscode?
推荐答案
带有一些 vscode小组的帮助,下面的代码可以正常工作.
With some help from the vscode team, the code below got things working.
package.json
package.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
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();
}
}
这篇关于可以扩展Visual Studio Code中的语言吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!