typescript Visual Studio代码扩展的文档突出显示器并非始终激发

6rvt4ljy  于 2023-03-04  发布在  TypeScript
关注(0)|答案(1)|浏览(140)

我的自定义VS代码扩展的文档突出显示事件registerDocumentHighlightProvider不会在某些字符上触发,如:[]{}。但是它会为其他字符(如A-Z)触发。有没有办法为所有字符触发文档突出显示?
Getting Started示例的基础上,我使用以下TypeScript代码(我的完整程序)进行了测试:

import * as vscode from 'vscode';

export function activate(context: vscode.ExtensionContext) {
    let disposable = vscode.commands.registerCommand('helloworld.helloWorld', () => {
        vscode.window.showInformationMessage('Hello World from HelloWorld!');
    });
    
    let docSelector: vscode.DocumentSelector = { scheme: 'file', language: 'json' };
    let highlight: Highlighter = new Highlighter();
    context.subscriptions.push(
        vscode.languages.registerDocumentHighlightProvider(docSelector, highlight),
        disposable
    );
}

export class Highlighter implements vscode.DocumentHighlightProvider {
    provideDocumentHighlights(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken) {
        // highlight single character at cursor 
        let range = new vscode.Range(position.line, position.character, position.line, position.character + 1);
        let highlight = new vscode.DocumentHighlight(range, vscode.DocumentHighlightKind.Write);
        return [ highlight ];
    }
}

export function deactivate() {}

在VS代码中调试扩展,我使用以下JSON文件进行了测试:

{
  "test": "[`${SOMEFUNC(param1)} etc.`]",
}

当光标位于字符上时,provideDocumentHighlights中的断点不会触发:[]{}),但会在A-Z字符上遇到断点。

py49o6xq

py49o6xq1#

我放弃了registerDocumentHighlightProvider的方法。相反,我使用Syntax Highlighting的注入语法和嵌入式语言以及Language Configuration来匹配括号突出显示。下面是我的完整解决方案,以防其他人发现它有帮助。

./src/extension.ts-从我的主程序中删除了registerDocumentHighlightProvider

import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
    let disposable = vscode.commands.registerCommand('helloworld.helloWorld', () => {
        vscode.window.showInformationMessage('Hello World from HelloWorld!');
    });
    context.subscriptions.push(disposable);
}
export function deactivate() {}

./package.json-将语言和语法添加到contributes部分

{
  "contributes": {
    "languages": [
      {
        "id": "customscript",
        "extensions": [ ".customscript" ],
        "configuration": "./customscript.language-configuration.json"
      }
    ],
    "grammars": [
      {
        "language": "customscript",
        "scopeName": "source.customscript",
        "path": "./syntaxes/customscript.tmLanguage.json"
      },
      {
        "scopeName": "customscript-json.injection",
        "path": "./syntaxes/injection.json",
        "injectTo": [ "source.json"],
        "embeddedLanguages": {
          "meta.embedded.customscript.expression": "customscript"
        }
      }
    ]
  }
}

./customscript.language-configuration.json-添加了配置文件,以便VS代码与括号匹配

{
    "brackets": [
        ["{", "}"],
        ["[", "]"],
        ["(", ")"]
    ],
    "autoClosingPairs": [
        { "open": "{", "close": "}" },
        { "open": "[", "close": "]" },
        { "open": "(", "close": ")" }
    ],
    "surroundingPairs": [
        ["{", "}"],
        ["[", "]"],
        ["(", ")"]
    ]
}

./syntaxes/customscript.tmLanguage.json-为我的自定义脚本语言添加了语法文件

{
  "scopeName": "source.customscript",
  "patterns": [{ "include": "#expression" }],
  "repository": {
    "expression": {
      "begin": "\\[",
      "beginCaptures": { "0": { "name": "punctuation.expression.open.customscript" } },
      "end": "\\]",
      "endCaptures": { "0": { "name": "punctuation.expression.close.customscript" } },
      "name": "meta.embedded.customscript.expression"
    }
  }
}

./syntaxes/injection.json-添加了将自定义脚本和json链接在一起的注入文件

{
  "scopeName": "customscript-json.injection",
  "injectionSelector": "L:string.quoted.double.json",
  "patterns": [ { "include": "source.customscript" } ]
}

package.jsoncustomscript.tmLanguage.json共享嵌入的作用域meta.embedded.customscript.expression,这有助于一切正常工作。下面是在VS代码调试器中运行的结果,其中包含问题中提到的test.json:

相关问题