Visual Studio Code中的语言能够被扩展吗?

7

场景

我有一些JSON文件,描述了一系列要执行的任务,其中每个任务可以引用JSON文件中的其他任务和对象。

{
    "tasks": [
        { "id": "first", "action": "doSomething()", "result": {} },
        { "id": "second", "action": "doSomething(${id:first.result})", "result": {} },
    ]
}

我希望能在JSON中实现JSON模式验证以及自定义语言文本效果,例如关键字着色甚至支持字符串内的“转到定义”。
我可以创建一个扩展程序,为文件扩展名“*.foo.json”指定JSON模式。如果vscode将文件识别为JSON文件,则可以在编辑器中进行模式验证和代码完成。
我还可以在“*.foo.json”文件的扩展程序中创建一个新的“foo”语言,其中包含JSON字符串内的自定义关键字着色。我通过创建从JSON.tmLanguage.json复制的TextMate(*.tmLanguage.json)文件并修改“stringcontent”定义来实现这一点。
问题是,只有当我在状态栏中选择“JSON”作为文件类型时,模式验证和提示才能起作用;而只有当我在状态栏中选择“foo”作为文件类型时,自定义文本着色才能起作用。
是否有办法同时实现两者?我是否可以在vscode中扩展JSON语言处理方式?

1
我想知道是否设置文件关联(settings.json条目之一)将foo设置为.json或反之是否有帮助。很容易检查。 - Mark
不幸的是,这似乎没有任何区别。 - Matt Miller
1个回答

6

vscode团队的帮助下,以下代码得以正常运作。

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

'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();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接