簡體   English   中英

在 VSCode 中,如何通過 API 向文檔添加新的選擇?

[英]In VSCode, how to add a new selection to the document via the API?

嘗試編寫我的第一個 VSCode 擴展時,我遇到了一個簡單的問題。 我通過類似的方式建立了一個新的選擇

var new_sel = new Selection(start, end);

其中startendvscode.Position對象。 現在我想將它作為一個選擇添加到我的文檔中,但我很迷茫。 我在vscode.commands.registerTextEditorCommand命令中,所以我可以訪問TextEditorTextEditorEdit對象及其所有字段,但我在 API 中看不到任何關於從文檔中添加/減去選擇的內容。

如何添加選擇,以便它反映在TextEditor.selections中? (我試圖簡單地將它推送到那個數組,但我在控制台中收到“調試器附加”錯誤。)

TextEditor.selections可以簡單地被覆蓋。

但是請注意,嘗試poppush送到TextEditor.selections對我不起作用。

這是訣竅:

let mySelections: vscode.Selection[] = [];
let line1 = 12, char1 = 5, line2 = 20, char2 = 3;  //Just some examples.
let line3 = 29, char3 = 3, line4 = 26, char4 = 7;  //Just some examples.

mySelections.push(line1, char1, line2, char2);
mySelections.push(line3, char3, line4, char4);
vscode.window.activeTextEditor.selections = mySelections;  

暫無
暫無

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

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