首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将光标放在vscode编辑器扩展中的指定行上?

如何将光标放在vscode编辑器扩展中的指定行上?
EN

Stack Overflow用户
提问于 2021-10-22 05:47:36
回答 1查看 372关注 0票数 6

我正在开发vscode TextEditorEdit扩展。在这里,我需要根据api响应内容,在将文件作为编辑器打开后,将光标放在指定的行号上。我怎样才能做到这一点?或者是哪个类或接口提供了这样做的功能?

下面是我试图实现这一目标的代码。但两者都没起作用。甚至不会引发任何异常或错误。

虽然是,但它在编辑器上正确地打开和显示文件。但当我试图更改光标位置时,不会产生任何影响。

  1. THENing vscode.window.showTextDocument()和使用自定义选择分配editor.selection。这对编辑器没有影响。

代码语言:javascript
复制
vscode.workspace.openTextDocument(openPath).then(async doc => {

  let pos1 = new vscode.Position(57, 40)
  let pos2 = new vscode.Position(57, 42)
  let sel = new vscode.Selection(pos1,pos2)
  let rng = new vscode.Range(pos1, pos2)
  
  vscode.window.showTextDocument(doc, vscode.ViewColumn.One).then((editor: vscode.TextEditor) => {
    editor.selection = sel
    //editor.revealRange(rng, vscode.TextEditorRevealType.InCenter) 
  });
});

我也试着用editor.revealRange(rng, vscode.TextEditorRevealType.InCenter)揭示范围,但也没有效果。

  1. vscode.window.showTextDocument()中添加options?:参数。但这也没什么用。请参阅下面的代码-

代码语言:javascript
复制
vscode.workspace.openTextDocument(openPath).then(async doc => {
  
  let pos1 = new vscode.Position(57, 40)
  let pos2 = new vscode.Position(57, 42)
  let rng = new vscode.Range(pos1, pos2)
  
  vscode.window.showTextDocument(doc, {selection: rng, viewColumn: vscode.ViewColumn.One})
});

我猜问题是先打开文件,然后将文本文档显示到编辑器中,然后更改光标位置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-25 10:06:51

您可以使用cursorMove内置的VSCode命令:

代码语言:javascript
复制
  let openPath = URI.file(mainPath);
  vscode.workspace.openTextDocument(openPath).then(async (doc) => {
    let line = 56;
    let char = 10;
    let pos1 = new vscode.Position(0, 0);
    let pos2 = new vscode.Position(0, 0);
    let sel = new vscode.Selection(pos1, pos2);
    vscode.window.showTextDocument(doc, vscode.ViewColumn.One).then((e) => {
      e.selection = sel;
      vscode.commands
        .executeCommand("cursorMove", {
          to: "down",
          by: "line",
          value: line,
        })
        .then(() =>
          vscode.commands.executeCommand("cursorMove", {
            to: "right",
            by: "character",
            value: char,
          })
        );
    });
  });

这个片段首先打开文档并在编辑器中显示它,然后按给定的行数移动光标,然后按给定的字符数量移动它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69671964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档