Skip to content

Commit

Permalink
feat: can selectively add entries to quickfix (#564)
Browse files Browse the repository at this point in the history
* bugfix: fix to enable adding or replacing of quickfix entries

* feat: added option to send only matched files to the quickfix list
  • Loading branch information
Janshagen authored Jan 26, 2025
1 parent a3fc662 commit b594b9a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
9 changes: 6 additions & 3 deletions doc/oil.txt
Original file line number Diff line number Diff line change
Expand Up @@ -605,9 +605,12 @@ send_to_qflist *actions.send_to_qflis
previous entries.

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 default keymaps
Expand Down
6 changes: 6 additions & 0 deletions lua/oil/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,12 @@ M.send_to_qflist = {
opts = vim.tbl_deep_extend("keep", opts or {}, {
target = "qflist",
action = "r",
only_matching_search = false,
})
util.send_to_quickfix({
target = opts.target,
action = opts.action,
only_matching_search = opts.only_matching_search,
})
end,
parameters = {
Expand All @@ -519,6 +521,10 @@ M.send_to_qflist = {
type = '"r"|"a"',
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",
},
},
}

Expand Down
25 changes: 20 additions & 5 deletions lua/oil/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ end

---Send files from the current oil directory to quickfix
---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)
if type(opts) ~= "table" then
opts = {}
Expand All @@ -767,10 +767,11 @@ M.send_to_quickfix = function(opts)
if not range then
range = { start_lnum = 1, end_lnum = vim.fn.line("$") }
end
local match_all = not opts.only_matching_search
local qf_entries = {}
for i = range.start_lnum, range.end_lnum do
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 = {
filename = dir .. entry.name,
lnum = 1,
Expand All @@ -786,13 +787,14 @@ M.send_to_quickfix = function(opts)
end
vim.api.nvim_exec_autocmds("QuickFixCmdPre", {})
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
vim.fn.setloclist(0, {}, mode, { title = qf_title, items = qf_entries })
vim.fn.setloclist(0, {}, action, { title = qf_title, items = qf_entries })
else
vim.fn.setqflist({}, mode, { title = qf_title, items = qf_entries })
vim.fn.setqflist({}, action, { title = qf_title, items = qf_entries })
end
vim.api.nvim_exec_autocmds("QuickFixCmdPost", {})
vim.cmd.copen()
end

---@return boolean
Expand All @@ -817,6 +819,19 @@ M.get_visual_range = function()
return { start_lnum = start_lnum, end_lnum = end_lnum }
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 callback fun()
M.run_after_load = function(bufnr, callback)
Expand Down

0 comments on commit b594b9a

Please sign in to comment.