我想搜索包含“A”但也包含“B”的文件,例如 live_grep 结果上的 live_grep。
我知道我可以通过 RegExp 实现类似的效果,但对我来说,一步一步分别搜索“A”和“B”会更容易。
我想要实现的目标如下(来自 TypeScript React 项目):
<Button>
(来自整个项目目录)<Badge>
(但仅限于 1 返回的文件。)我成功地通过使用 telescope-live-grep-args.nvim 扩展和一些 lua 编码来实现结果。
{
"nvim-telescope/telescope.nvim",
dependencies = { "nvim-telescope/telescope-live-grep-args.nvim" },
config = function()
require("telescope").setup({
defaults = {
mappings = {
n = {
["<C-r>"] = function(p_bufnr)
-- send results to quick fix list
require("telescope.actions").send_to_qflist(p_bufnr)
local qflist = vim.fn.getqflist()
local paths = {}
local hash = {}
for k in pairs(qflist) do
local path = vim.fn.bufname(qflist[k]["bufnr"]) -- extract path from quick fix list
if not hash[path] then -- add to paths table, if not already appeared
paths[#paths + 1] = path
hash[path] = true -- remember existing paths
end
end
-- show search scope with message
vim.notify("find in ...\n " .. table.concat(paths, "\n "))
-- execute live_grep_args with search scope
require("telescope").extensions.live_grep_args.live_grep_args({ search_dirs = paths })
end,
},
},
},
})
require("telescope").load_extension("live_grep_args")
end,
},