Home > other >  How do I use the cursorMove command in an extension so that the cursor jumps to a specific position
How do I use the cursorMove command in an extension so that the cursor jumps to a specific position

Time:03-10

I'm currently trying to find a command that executes so that the cursor moves to a specific location I already have saved as a variable

My locations are saved like line = functions[i].location.range.start.line for the specific line and column = functions[i].location.range.start.character for the specific position
I already know that I can kind of create the position with new vscode.Position(line,column)

Now, how exactly do I integrate this into the command
"cursorMove({to: $ViewPosition, select: boolean, by: $By, value: number})"?

The (default) thing that works for me to move the cursor up by one line, with

vscode.commands.executeCommand("cursorMove",{
     to: "up",
     by: "wrappedLine",
     select: false,
     value: 1
});

I want to implement my location/position from the functions array so that the command

let clickEvent = vscode.commands.registerCommand('homeItem.click', (functions[i].location) => {
    vscode.commands.executeCommand("cursorMove", {
        to: x,
        by: x,
        select: false,
        value: x
    });
});

sets the cursor focus to the provided position

CodePudding user response:

I see two options:

  1. cursorMove's are relative to the current position. Not to an absolute position as I think you know. So you would have to get the difference between the current cursor position vscode.window.activeTextEditor.selection.active.line and your line = functions[i].location.range.start.line and use that in the cursorMove command (after doing the math to know if you need up or down).

And you have to do the same for character and do a second cursorMove within the correct line to move by character if you care about that. So it is a lot of work.

  1. Just set the cursor position by setting a selection.
const myPos = new vscode.Position(x,x);     // I think you know how to get the values, let us know if you don't
vscode.window.activeTextEditor?.selections = [new vscode.Selection(myPos, myPos)];

or do this check first:

if (vscode.window.activeTextEditor) {
  const myPos = new vscode.Position(x,x);     // I think you know how to get the values, let us know if you don't
  vscode.window.activeTextEditor?.selections = [new vscode.Selection(myPos, myPos)];
}
  • Related