I'm currently developing a vscode extension that involves moving each selection of a multi-selection to the right by one character. The challenge lies in updating the TextEditor.selections
array, which stores all current selections in the GUI.
When I set TextEditor.selection
to a new vscode.Selection
, the primary selection in the GUI updates accurately. However, updating TextEditor.selections[n]
with a new selection does not reflect changes in the GUI. This limitation prevents me from updating all selections simultaneously. It seems that using the shorthand TextEditor.selection
triggers a GUI update, while using TextEditor.selections[n]
does not.
My question is: how can I update all selections in TextEditor.selections
?
Below is the code snippet I have for updating 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)
);
});
}