Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the list of files in search results in VS code?

I'm trying to search a string in the code base. Ctrl + Shift + F should do it, but I want to get the list of files in search results.

I tried using the VS Code command:

commands.executeCommand("workbench.action.findInFiles");

However, it simply did the search but didn't return anything.

Is there an API to get the list of search results?

like image 986
yuanOrange Avatar asked Nov 17 '22 05:11

yuanOrange


2 Answers

The Copy All right-click command in the Search Results pane will get you the entire search results, including the file names.

You can then use regex find/replace to delete the results, leaving only the file names.

like image 53
Foo Bar Avatar answered Dec 28 '22 09:12

Foo Bar


Here is how I am getting the files from a search across files result. This is after the workbench.action.findInFiles command has been run.

/**
 * Get the relative paths of the current search results 
 * for the next `runInSearchPanel` call  
 * 
 * @returns array of paths or undefined  
 */
exports.getSearchResultsFiles = async function () {

    await vscode.commands.executeCommand('search.action.copyAll');
    let results = await vscode.env.clipboard.readText();

    if (results)  {
        // regex to get just the file names
        results = results.replaceAll(/^\s*\d.*$\s?|^$\s/gm, "");
        let resultsArray = results.split(/[\r\n]{1,2}/);  // does this cover all OS's?

        let pathArray = resultsArray.filter(result => result !== "");
        pathArray = pathArray.map(path => this.getRelativeFilePath(path))

        return pathArray.join(", ");
    }
    else {
        // notifyMessage?
        return undefined;
    }
}

It is designed to get the relative paths of those files, you can change that if you want.

like image 33
Mark Avatar answered Dec 28 '22 10:12

Mark