How to put cursor on specified line in vscode editor extension programatically?
Asked Answered
H

1

6

I am working on vscode TextEditorEdit extension. Here I need to put the cursor on a specified line number after opening the file as an editor, based on an api response content. How can I achieve that? or what class or interface provides the functionality to do this?

Below are the codes I tried to achieve this. but both didn't work. Not even raising any exception or error.

Although this is opening and showing the file on the editor properly. But not make any effect when I try to change the cursor position.

  1. THENing the vscode.window.showTextDocument() and assigning the editor.selection with custom selection. This makes no effect on editor.
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) 
  });
});

I also tried revealing the range with editor.revealRange(rng, vscode.TextEditorRevealType.InCenter) but no effect of this also.

  1. Adding the options?: parameter in vscode.window.showTextDocument(). But this too doesn't do anything. Please see the below code-
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})
});

I guess, the problem is with opening the file first and showing the text document into the editor, and then changing the cursor location.

Hardened answered 22/10, 2021 at 5:47 Comment(1)
I've tried your first approach and it works fine for me, I can see a selection on the correct range. The cursor is invisible until I click somewhere however. Is that the issue here?Philosopher
P
2

You can use the cursorMove built-in command of VSCode:

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

This snippet first opens the document and shows it in the editor, then moves the cursor by the given amount of line, then moves it by the given amount of characters.

Philosopher answered 25/10, 2021 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.