FzfLua: Add command to search in filtered files
This commit is contained in:
parent
81abafe706
commit
4c89fa6339
1 changed files with 40 additions and 1 deletions
|
|
@ -87,13 +87,51 @@ local function live_grep_in_quick_fix_list_files()
|
|||
|
||||
if #files > 0 then
|
||||
fzf_lua.live_grep({
|
||||
search_dirs = files,
|
||||
search_paths = files,
|
||||
})
|
||||
else
|
||||
print("No files in quickfix list")
|
||||
end
|
||||
end
|
||||
|
||||
-- Two-step search in cwd:
|
||||
-- 1) type a literal file name prefix (e.g. `Go - `, case-insensitive), <Tab> multi-select
|
||||
-- or <C-q> to take all matches
|
||||
-- 2) live_grep restricted to the files picked in step 1
|
||||
local function grep_in_filtered_files()
|
||||
local cwd = vim.fn.getcwd()
|
||||
|
||||
-- rg matches the glob against the file name only; escape glob metacharacters
|
||||
local function files_cmd(args)
|
||||
local prefix = (args[1] or ""):gsub("[%*%?%[%]{}\\]", "\\%0")
|
||||
return string.format("rg --files --color=never --iglob %s", vim.fn.shellescape(prefix .. "*"))
|
||||
end
|
||||
|
||||
---@diagnostic disable-next-line: missing-fields
|
||||
fzf_lua.fzf_live(files_cmd, {
|
||||
cwd = cwd,
|
||||
prompt = "Filename starts with❯ ",
|
||||
exec_empty_query = true,
|
||||
fzf_opts = { ["--multi"] = true },
|
||||
actions = {
|
||||
["default"] = function(selected)
|
||||
if not selected or #selected == 0 then
|
||||
return
|
||||
end
|
||||
local files = {}
|
||||
for _, name in ipairs(selected) do
|
||||
table.insert(files, cwd .. "/" .. name)
|
||||
end
|
||||
fzf_lua.live_grep({
|
||||
cwd = cwd,
|
||||
prompt = string.format("Grep in %d files❯ ", #files),
|
||||
search_paths = files,
|
||||
})
|
||||
end,
|
||||
},
|
||||
})
|
||||
end
|
||||
|
||||
local function find_all_files_including_hidden()
|
||||
fzf_lua.files(
|
||||
{
|
||||
|
|
@ -125,6 +163,7 @@ keymap("n", "<leader>fh", function() fzf_lua.help_tags() end, { desc = "FZF: dis
|
|||
keymap("n", "<leader>fa", find_all_files_including_hidden, { desc = "FZF: Find all files" })
|
||||
keymap('n', '<leader>fp', live_grep_with_glob, { desc = 'Live grep with glob pattern' })
|
||||
keymap('n', '<leader>fcp', live_grep_in_current_dir, { desc = 'Live grep in current directory' })
|
||||
keymap('n', '<leader>fv', grep_in_filtered_files, { desc = "Filter files by name, then grep inside them" })
|
||||
keymap('n', '<leader>ccp', copy_file_path_and_position, { desc = "Copy cursor position" })
|
||||
keymap('n', '<leader>ccd', copy_current_file_path, { desc = 'Copy current file path' })
|
||||
keymap('n', '<leader>ccf', copy_filename, { desc = 'Copy current file path' })
|
||||
|
|
|
|||
Loading…
Reference in a new issue