Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

409
Visualizações
Vscode move to line X after openTextDocument

I'm developing a VS Code extension that jump to a specific file:num, but I'm stuck at the step of moving the cursor to a specific line after opening a file. How can I achieve this :

export const openAndMoveToLine = async (file_line: string) => {

  // /home/user/some/path.php:10
  let [filename, line_number] = file_line.split(":")
  
  // opening the file => OK
  let setting: vscode.Uri = vscode.Uri.parse(filename)
  let doc = await vscode.workspace.openTextDocument(setting)
  vscode.window.showTextDocument(doc, 1, false);

  // FIXME: After being opened, now move to the line X  => NOK **/

  await vscode.commands.executeCommand("cursorMove",     {
          to: "down", by:'wrappedLine',
          value: parseInt(line_number)
    });

}

Thank you

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You will first need to get access to the active editor. This is done by adding a .then to the showTextDocument call (which is a Thenable function) that returns a text editor object. You will then be able to use the textEditor variable (as in the example) to set the position of the cursor using the selection property as follows:

vscode.window.showTextDocument(doc, 1, false).then((textEditor: TextEditor) => {
  const lineNumber = 1;
  const characterNumberOnLine = 1;
  const position = new vscode.Position(lineNumber, characterNumberOnLine);
  const newSelection = new vscode.Selection(position, position);
  textEditor.selection = newSelection;
});

Reference to selection API can be found here.

The usecase that you are exploring has been discussed in GitHub issue that can be found here.

about 4 years ago · Juan Pablo Isaza Relatório

0

It can be done with the TextDocumentShowOptions easily:

const showDocOptions = {
    preserveFocus: false,
    preview: false,
    viewColumn: 1,

    // replace with your line_number's
    selection: new vscode.Range(314, 0, 314, 0)
};

let doc = await vscode.window.showTextDocument(setting, showDocOptions);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda