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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With