gpt4 book ai didi

javascript - 如何在 VS 代码中获取搜索结果中的文件列表?

转载 作者:行者123 更新时间:2023-12-03 21:01:37 27 4
gpt4 key购买 nike

我正在尝试在代码库中搜索字符串。 Ctrl + Shift + F 应该这样做,但我想在搜索结果中获取文件列表。
我尝试使用 VS Code 命令:

commands.executeCommand("workbench.action.findInFiles");
但是,它只是进行了搜索,但没有返回任何内容。
是否有获取搜索结果列表的 API?

最佳答案

这是我从跨文件搜索结果中获取文件的方式。这是在 workbench.action.findInFiles 之后命令已运行。

/**
* 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;
}
}
它旨在获取这些文件的相对路径,您可以根据需要进行更改。

关于javascript - 如何在 VS 代码中获取搜索结果中的文件列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57087400/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com