有没有办法对 Telescope 的 live_grep() 返回的文件进行 grep?

问题描述 投票:0回答:1

我想搜索包含“A”但也包含“B”的文件,例如 live_grep 结果上的 live_grep。

我知道我可以通过 RegExp 实现类似的效果,但对我来说,一步一步分别搜索“A”和“B”会更容易。

我想要实现的目标如下(来自 TypeScript React 项目):

  1. live_grep()
    <Button>
    (来自整个项目目录)
  2. 返回包含 Button 组件的文件
  3. live_grep()
    <Badge>
    (但仅限于 1 返回的文件。)
  4. 返回包含 Badge 组件和 Button 组件的文件
neovim neovim-plugin telescope.nvim
1个回答
0
投票

我成功地通过使用 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,
  },
© www.soinside.com 2019 - 2024. All rights reserved.