feat: can selectively add entries to quickfix (#564)

* bugfix: fix to enable adding or replacing of quickfix entries

* feat: added option to send only matched files to the quickfix list
This commit is contained in:
Anton Janshagen 2025-01-26 18:18:37 +01:00 committed by GitHub
parent a3fc6623fa
commit b594b9a905
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 8 deletions

View file

@ -605,9 +605,12 @@ send_to_qflist *actions.send_to_qflis
previous entries. previous entries.
Parameters: Parameters:
{action} `"r"|"a"` Replace or add to current quickfix list (see
|setqflist-action|)
{target} `"qflist"|"loclist"` The target list to send files to {target} `"qflist"|"loclist"` The target list to send files to
{action} `"r"|"a"` Replace or add to current quickfix list
(see |setqflist-action|)
{only_matching_search} `boolean` Whether to only add the files that matches
the last search. This option only applies when search
highlighting is active
show_help *actions.show_help* show_help *actions.show_help*
Show default keymaps Show default keymaps

View file

@ -504,10 +504,12 @@ M.send_to_qflist = {
opts = vim.tbl_deep_extend("keep", opts or {}, { opts = vim.tbl_deep_extend("keep", opts or {}, {
target = "qflist", target = "qflist",
action = "r", action = "r",
only_matching_search = false,
}) })
util.send_to_quickfix({ util.send_to_quickfix({
target = opts.target, target = opts.target,
action = opts.action, action = opts.action,
only_matching_search = opts.only_matching_search,
}) })
end, end,
parameters = { parameters = {
@ -519,6 +521,10 @@ M.send_to_qflist = {
type = '"r"|"a"', type = '"r"|"a"',
desc = "Replace or add to current quickfix list (see |setqflist-action|)", desc = "Replace or add to current quickfix list (see |setqflist-action|)",
}, },
only_matching_search = {
type = "boolean",
desc = "Whether to only add the files that matches the last search. This option only applies when search highlighting is active",
},
}, },
} }

View file

@ -753,7 +753,7 @@ end
---Send files from the current oil directory to quickfix ---Send files from the current oil directory to quickfix
---based on the provided options. ---based on the provided options.
---@param opts {target?: "qflist"|"loclist", mode?: "r"|"a"} ---@param opts {target?: "qflist"|"loclist", action?: "r"|"a", only_matching_search?: boolean}
M.send_to_quickfix = function(opts) M.send_to_quickfix = function(opts)
if type(opts) ~= "table" then if type(opts) ~= "table" then
opts = {} opts = {}
@ -767,10 +767,11 @@ M.send_to_quickfix = function(opts)
if not range then if not range then
range = { start_lnum = 1, end_lnum = vim.fn.line("$") } range = { start_lnum = 1, end_lnum = vim.fn.line("$") }
end end
local match_all = not opts.only_matching_search
local qf_entries = {} local qf_entries = {}
for i = range.start_lnum, range.end_lnum do for i = range.start_lnum, range.end_lnum do
local entry = oil.get_entry_on_line(0, i) local entry = oil.get_entry_on_line(0, i)
if entry and entry.type == "file" then if entry and entry.type == "file" and (match_all or M.is_matching(entry)) then
local qf_entry = { local qf_entry = {
filename = dir .. entry.name, filename = dir .. entry.name,
lnum = 1, lnum = 1,
@ -786,13 +787,14 @@ M.send_to_quickfix = function(opts)
end end
vim.api.nvim_exec_autocmds("QuickFixCmdPre", {}) vim.api.nvim_exec_autocmds("QuickFixCmdPre", {})
local qf_title = "oil files" local qf_title = "oil files"
local mode = opts.mode == "a" and "a" or "r" local action = opts.action == "a" and "a" or "r"
if opts.target == "loclist" then if opts.target == "loclist" then
vim.fn.setloclist(0, {}, mode, { title = qf_title, items = qf_entries }) vim.fn.setloclist(0, {}, action, { title = qf_title, items = qf_entries })
else else
vim.fn.setqflist({}, mode, { title = qf_title, items = qf_entries }) vim.fn.setqflist({}, action, { title = qf_title, items = qf_entries })
end end
vim.api.nvim_exec_autocmds("QuickFixCmdPost", {}) vim.api.nvim_exec_autocmds("QuickFixCmdPost", {})
vim.cmd.copen()
end end
---@return boolean ---@return boolean
@ -817,6 +819,19 @@ M.get_visual_range = function()
return { start_lnum = start_lnum, end_lnum = end_lnum } return { start_lnum = start_lnum, end_lnum = end_lnum }
end end
---@param entry oil.Entry
---@return boolean
M.is_matching = function(entry)
-- if search highlightig is not enabled, all files are considered to match
local search_highlighting_is_off = (vim.v.hlsearch == 0)
if search_highlighting_is_off then
return true
end
local pattern = vim.fn.getreg("/")
local position_of_match = vim.fn.match(entry.name, pattern)
return position_of_match ~= -1
end
---@param bufnr integer ---@param bufnr integer
---@param callback fun() ---@param callback fun()
M.run_after_load = function(bufnr, callback) M.run_after_load = function(bufnr, callback)