Select a block of text via VS Code API

Viewed 47

How can I select/highlight a block of text by two different vscode.Positions via VS Code API?

enter image description here

1 Answers

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];
Related