【问题标题】:VSC: how to position cursor at specified line in extension?VSC:如何将光标定位在扩展中的指定行?
【发布时间】:2022-11-29 11:00:43
【问题描述】:

我想打开一个 Textdocument 并跳转到某一行。

到目前为止,这是我尝试过的:

vscode.workspace.openTextDocument(vscode.Uri.file(sctPath)).then(document => {
    vscode.window.showTextDocument(document).then(() => {
        let editor = vscode.window.activeTextEditor!;
        let range = document.lineAt(20).range;
        editor.revealRange(range);
    })
})

文档打开,但光标未转到行 20

每当我手动标记一条线时,就会记住光标位置。

是不是漏接电话之类的?

【问题讨论】:

  • 你忘记将 selection 设置到正确的位置
  • @rioVio:谢谢,确实不见了!

标签: visual-studio-code vscode-extensions


【解决方案1】:

使用 showTextDocument 的其他参数可以很容易地完成此操作:

let pos = new vscode.Position(20,0);  // set cursor to start of line 21, it is 0-based

await vscode.window.showTextDocument(
          vscode.Uri.file(sctPath), 
          {selection: new vscode.Range(pos, pos)}
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 2015-09-07
    • 1970-01-01
    相关资源
    最近更新 更多