Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 627
Tanmay Bairagi Avatar asked Oct 22 '21 05:10

Tanmay Bairagi


1 Answers

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.

like image 119
Charles Avatar answered Nov 25 '22 05:11

Charles