How can I select/highlight a block of text by two different vscode.Position
s via VS Code API?
Asked
Active
Viewed 130 times
1

HamedFathi
- 3,667
- 5
- 32
- 72
-
1create a `vscode.Selection` and assign it in an array to the `editor.selections` – rioV8 Jul 14 '22 at 15:34
-
As rioV8 said you have the anchor and active `Position`s of the selection. Put those into a `new Selection(postions here)` and then `editor.selections = [your new selection]. – Mark Jul 14 '22 at 16:07
1 Answers
0
First, you need to create the two Position objects:
const pos1 = new vscode.Position(line1, col1);
const pos2 = new vscode.Position(line2, col2);
Second, you create a Selection using pos1
as the anchor position, and pos2
as the active position:
const sel = new vscode.Selection(pos1, pos2)
Third, you assign an array containing that selection to the selections
of the currently active TextEditor:
const editor = vscode.window.activeTextEditor;
editor.selections = [sel];

jackdbd
- 4,583
- 3
- 26
- 36

HamedFathi
- 3,667
- 5
- 32
- 72