在 VS Code 扩展中获取当前突出显示

Ren*_*oth 3 visual-studio-code vscode-extensions

我不是在谈论当前的选择,它可以通过vscode.window.activeTextEditor.selection.

当光标位于标识符、变量名称等内时,它会突出显示,如下图所示:

VS Code 截图

这个高亮对象叫什么?我如何访问它?

Ren*_*oth 5

搜索从“突出显示”到“标识符”以及其他任何内容,答案更加明显。TextDocument有一个getWordRangeAtPosition方法,它接受一个位置并返回单词的范围。

const editor = vscode.window.activeTextEditor;
let cursorPosition = editor.selection.start;
let wordRange = editor.document.getWordRangeAtPosition(cursorPosition);
let highlight = editor.document.getText(wordRange);
// highlight will now contain the currently highlighted word

Run Code Online (Sandbox Code Playgroud)