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

267
Visualizações
Vscode extension: how to implement a hover function to add arguments to commands, e.g., to run search command with query

I am trying to develop a custom button in vscode hover function. Meaning if I click the shortcut button, it will go to search sidebar and have a specific query search. Refer to the two images which is uploaded.

Hover Search Button

Result Sidebar change

In vscode command form, the extension code will look like this. (This works well except it requires to run from command pallete)

let disposable = vscode.commands.registerCommand('test-hover.helloWorldSearch', () => {
    var word = "i_want_to_search";
    const searchOptions = {
        query: word,
    };
    vscode.commands.executeCommand('workbench.action.findInFiles', searchOptions);
});
context.subscriptions.push(disposable);

After that I convert into hover by using vscode.languages.registerHoverProvider, and the code look like this:

     let disposable1 = vscode.languages.registerHoverProvider('javascript', {
     provideHover(document, position, token) {
            var word = "i_want_to_search";
            const searchOptions = {
                query: word,
            };
            const commentCommandUri = vscode.Uri.parse(`command:workbench.action.findInFiles`);
            const contents = new vscode.MarkdownString(`[Search String](${commentCommandUri})`);
            contents.isTrusted = true;
            return new vscode.Hover(contents);
        }
    });
    context.subscriptions.push(disposable1);

This code is able to navigate the sidebar to search In file BUT without any query seacrh. The problem is, I can't pass "searchOptions" to the command if I am using "vscode.Uri.parse". Is there any other way to do the same action as the above code but able to pass the searchOption? I have tried to search many different examples in the internet but most only come out to run command without searchOption.

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

0

The arguments to your command have to be appended as an encoded string, as shown in command-uris.

const searchCommandUri = vscode.Uri.parse(
    `command:workbench.action.findInFiles?${encodeURIComponent(JSON.stringify(searchOptions))}`
);

const contents = new vscode.MarkdownString(`[Search String](${searchCommandUri})`);

If you wanted to get the word that you are hovering over, try this code:

const word = document.getText(document.getWordRangeAtPosition(position));

Now your "Search String" hover button will search the word that is being hovered over.

Then change the text: "Search String" to the actual word you will be searching for:

const contents = new vscode.MarkdownString(`[Search: ${word}](${searchCommandUri})`);

hover with a search button demo

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