在Visual Studio代码文档中设置选择范围

时间:2017-11-12 15:45:27

标签: typescript visual-studio-code vscode-extensions

我在扩展程序中有一个命令,在运行命令之前,我想更改选择范围以获得整行...

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);

我创建了一个新范围,但我不知道如何将文档选择设置为新范围......

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

为了注册用于设置光标位置的命令,我使用了这个:

let cmd = vscode.commands.registerTextEditorCommand('extension.mysnippet', (te) => {
  // selection start = line 3, char 5 ||| selection end = line 3, char 5
  te.selection = new vscode.Selection(5, 3, 5, 3)
});
context.subscriptions.push(cmd);