typescript - 在 Visual Studio Code 文档中设置选择范围

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

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

最佳答案

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

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

  • 示例,使用 4 个参数:
    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),
    ];
    

    关于typescript - 在 Visual Studio Code 文档中设置选择范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47250698/

    相关文章:

    python-3.x - 如何让 pylint 在 VS Code 中使用 python 3?

    java - HttpServer 不是 API 的 VSCode Java 访问限制错误

    typescript - 更新 vscode 扩展中的 editor.selections

    visual-studio-code - 显示基于单词的建议和自己的完成提供程序的结果

    javascript - 在 TypeScript 中将类导出为 Node.js 模块

    typescript - 访问 Vue 中的任意插槽

    reactjs - 在 typescript 中 react 高阶组件(HOC)

    python - 当断点位于 while 循环内时,在 VSCode 中调试 jupyter 笔记本时出错

    visual-studio-code - 当用户右键单击资源管理器/上下文中的文件时,如何在 vscode 扩展名中获取文件名或路径?

    node.js - 套接字 Io 未找到 404