簡體   English   中英

如何從 VSCode 擴展 API 中獲取突出顯示的文本

[英]How to get highlighted text from VSCode Extension API

我正在使用 VSCode API 來構建擴展,但我沒有找到任何文檔涵蓋如何獲取用戶突出顯示的文本,如下所示:

在此處輸入圖像描述

關於光標當前所在的單詞有一個類似的問題,但我想獲取整個突出顯示的文本。

您可以使用activeTextEditor API 調用來使用活動文本編輯器,然后使用selections字段來獲取您的選擇范圍。 您的代碼將如下所示:

let editor = vscode.window.activeTextEditor
let selections = editor.selections

VSCode API 文檔: https ://code.visualstudio.com/api/references/vscode-api#TextEditor

解決方案是Mark's answer here 的修改版本。

const editor = vscode.window.activeTextEditor;
const selection = editor.selection;
if (selection && !selection.isEmpty) {
    const firstSelectedCharacter = selection.start.character;
    const selectionRange = new vscode.Range(selection.start.line, firstSelectedCharacter, selection.end.line, selection.end.character);
    const highlighted = editor.document.getText(selectionRange);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM