Set selection range in the Visual Studio Code document

喜你入骨 提交于 2019-12-14 02:56:45

问题


I have a command in an extension, before run the command, I want to change the selection range to get the whole lines...

const sel = textEditor.selection;
const firstLine = textEditor.document.lineAt(sel.start.line);
const lastLine = textEditor.document.lineAt(sel.end.line);

const range = new vscode.Range(firstLine.lineNumber, firstLine.range.start.character, lastLine.lineNumber, lastLine.range.end.character);

I've created a new range, but I don't know how to set the selection of the document to a new range...


回答1:


new Selection() has 2 overloads (2 or 4 arguments):

  1. Selection(anchor: vscode.Position, active: vscode.Position)
  2. Selection(anchorLine: number, anchorCharacter: number, activeLine: number, activeCharacter: number)

Example, using 4 arguments:

textEditor.selection = new vscode.Selection(firstLine.lineNumber, firstLine.range.start.character, 
lastLine.lineNumber, lastLine.range.end.character)

To make multiple cursors you need to set textEditor.selections

textEditor.selections = [
    new vscode.Selection(0, 0, 0, 10),
    new vscode.Selection(1, 0, 1, 10),
];


来源:https://stackoverflow.com/questions/47250698/set-selection-range-in-the-visual-studio-code-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!