Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

261
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda