在 Visual Studio Code 文档中设置选择范围 [英] Set selection range in the Visual Studio Code document

查看:60
本文介绍了在 Visual Studio Code 文档中设置选择范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...

推荐答案

new Selection() 有 2 个重载(2 或 4 个参数):

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

  1. 选择(锚点:vscode.Position,活动:vscode.Position)
  2. 选择(anchorLine:数字,anchorCharacter:数字,activeLine:数字,activeCharacter:数字)

示例,使用 4 个参数:

Example, using 4 arguments:

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

<小时>

要制作多个光标,您需要设置 textEditor.selections

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

这篇关于在 Visual Studio Code 文档中设置选择范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆