【发布时间】:2020-05-21 11:05:23
【问题描述】:
我目前正在开发一个小型 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)
);
});
}
【问题讨论】:
标签: typescript visual-studio-code vscode-extensions