Can a language in Visual Studio Code be extended?

回眸只為那壹抹淺笑 提交于 2019-12-05 11:25:25
Matt Miller

With some help from the vscode team, the code below got things working.

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();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!