[英]VSCode extension: Custom Editor with Webview both on typescript
[英]Updating editor.selections in vscode Extension
我目前正在開發一個小的 vscode 擴展。 在擴展代碼中,我想通過相應地更新保存 GUI 中當前存在的所有選擇的TextEditor.selections
數組來將多選的每個選擇向右移動一個字符。
將TextEditor.selection
設置為新的vscode.Selection
允許更新TextEditor.selections
數組的主要選擇。 這樣,GUI 中的主要選擇就會正確更新。 但是,將TextEditor.selections[n]
設置為新選擇不會導致 GUI 更新。 因此,我目前只能更新主要選擇,而不是全部更新。 似乎設置速記TextEditor.selection
會發布 GUI 更新,而設置TextEditor.selections[n]
不會。
那么,如何更新TextEditor.selections
中的所有選擇?
這是我更新選擇的代碼:
import * as vscode from "vscode";
function updateSelections(
editor: vscode.TextEditor,
selections: vscode.Selection[]
) {
selections.forEach((sel) => {
const selStart = sel.start;
const selEnd = sel.end;
// TODO: only primary selection appears to be editable ; "editor.selections[n] = something" does not change anything
// This works
editor.selection = new vscode.Selection(
selStart.translate(0, 1),
selEnd.translate(0, 1)
);
// This does not work
editor.selections[0] = new vscode.Selection(
selStart.translate(0, 1),
selEnd.translate(0, 1)
);
});
}
如果您使用更新的選擇分配一個新數組,它會起作用
editor.selections = editor.selections.map( sel => new vscode.Selection(sel.start.translate(0,1), sel.end.translate(0,1)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.