refactor(picker.finder): function signature of finders changed to fun(opts, ctx)

This commit is contained in:
Folke Lemaitre 2025-01-22 21:12:27 +01:00
parent 9db49b7e6c
commit caf20764f1
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
8 changed files with 26 additions and 21 deletions

View file

@ -5,7 +5,7 @@ local M = {}
---@param opts snacks.picker.buffers.Config
---@type snacks.picker.finder
function M.buffers(opts, filter)
function M.buffers(opts, ctx)
opts = vim.tbl_extend("force", {
hidden = false,
unloaded = true,
@ -48,7 +48,7 @@ function M.buffers(opts, filter)
return a.info.lastused > b.info.lastused
end)
end
return filter:filter(items)
return ctx.filter:filter(items)
end
return M

View file

@ -6,11 +6,11 @@ local uv = vim.uv or vim.loop
---@param opts snacks.picker.diagnostics.Config
---@type snacks.picker.finder
function M.diagnostics(opts, filter)
function M.diagnostics(opts, ctx)
local items = {} ---@type snacks.picker.finder.Item[]
local current_buf = vim.api.nvim_get_current_buf()
local cwd = vim.fs.normalize(uv.cwd() or ".")
for _, diag in ipairs(vim.diagnostic.get(filter.buf, { severity = opts.severity })) do
for _, diag in ipairs(vim.diagnostic.get(ctx.filter.buf, { severity = opts.severity })) do
local buf = diag.bufnr
if buf and vim.api.nvim_buf_is_valid(buf) then
local file = vim.fs.normalize(vim.api.nvim_buf_get_name(buf), { _fast = true })
@ -32,7 +32,7 @@ function M.diagnostics(opts, filter)
}
end
end
return filter:filter(items)
return ctx.filter:filter(items)
end
return M

View file

@ -102,9 +102,9 @@ end
---@param opts snacks.picker.files.Config
---@type snacks.picker.finder
function M.files(opts, filter)
function M.files(opts, ctx)
local cwd = not (opts.dirs and #opts.dirs > 0) and vim.fs.normalize(opts and opts.cwd or uv.cwd() or ".") or nil
local cmd, args = get_cmd(opts, filter)
local cmd, args = get_cmd(opts, ctx.filter)
return require("snacks.picker.source.proc").proc(vim.tbl_deep_extend("force", {
cmd = cmd,
args = args,

View file

@ -33,7 +33,7 @@ end
---@param opts snacks.picker.git.log.Config
---@type snacks.picker.finder
function M.log(opts, filter)
function M.log(opts, ctx)
local args = {
"log",
"--pretty=format:%h %s (%ch)",
@ -50,13 +50,13 @@ function M.log(opts, filter)
local file ---@type string?
if opts.current_line then
local cursor = vim.api.nvim_win_get_cursor(filter.current_win)
file = vim.api.nvim_buf_get_name(filter.current_buf)
local cursor = vim.api.nvim_win_get_cursor(ctx.filter.current_win)
file = vim.api.nvim_buf_get_name(ctx.filter.current_buf)
local line = cursor[1]
args[#args + 1] = "-L"
args[#args + 1] = line .. ",+1:" .. file
elseif opts.current_file then
file = vim.api.nvim_buf_get_name(filter.current_buf)
file = vim.api.nvim_buf_get_name(ctx.filter.current_buf)
if opts.follow then
args[#args + 1] = "--follow"
end

View file

@ -100,13 +100,13 @@ end
---@param opts snacks.picker.grep.Config
---@type snacks.picker.finder
function M.grep(opts, filter)
if opts.need_search ~= false and filter.search == "" then
function M.grep(opts, ctx)
if opts.need_search ~= false and ctx.filter.search == "" then
return function() end
end
local absolute = (opts.dirs and #opts.dirs > 0) or opts.buffers
local cwd = not absolute and vim.fs.normalize(opts and opts.cwd or uv.cwd() or ".") or nil
local cmd, args = get_cmd(opts, filter)
local cmd, args = get_cmd(opts, ctx.filter)
return require("snacks.picker.source.proc").proc(vim.tbl_deep_extend("force", {
notify = not opts.live,
cmd = cmd,

View file

@ -35,7 +35,7 @@ local severities = {
---@param opts snacks.picker.qf.Config
---@type snacks.picker.finder
function M.qf(opts, filter)
function M.qf(opts, ctx)
local win = opts.qf_win
win = win == 0 and vim.api.nvim_get_current_win() or win
@ -68,7 +68,7 @@ function M.qf(opts, filter)
ret[#ret].item.line = ret[#ret].item.line .. "\n" .. item.text
end
end
return filter:filter(ret)
return ctx.filter:filter(ret)
end
return M

View file

@ -29,7 +29,7 @@ end
--- current working directory or a custom directory.
---@param opts snacks.picker.recent.Config
---@type snacks.picker.finder
function M.files(opts, filter)
function M.files(opts, ctx)
local current_file = vim.fs.normalize(vim.api.nvim_buf_get_name(0), { _fast = true })
---@type number[]
local bufs = vim.tbl_filter(function(b)
@ -44,7 +44,7 @@ function M.files(opts, filter)
---@async
---@param cb async fun(item: snacks.picker.finder.Item)
return function(cb)
for file in oldfiles(filter, extra) do
for file in oldfiles(ctx.filter, extra) do
if file ~= current_file then
cb({ file = file, text = file, recent = true })
end
@ -60,12 +60,12 @@ M.recent = M.files
--- You can customize the behavior by providing a custom action.
---@param opts snacks.picker.recent.Config
---@type snacks.picker.finder
function M.projects(opts, filter)
function M.projects(opts, ctx)
---@async
---@param cb async fun(item: snacks.picker.finder.Item)
return function(cb)
local dirs = {} ---@type table<string, boolean>
for file in oldfiles(filter) do
for file in oldfiles(ctx.filter) do
local dir = Snacks.git.get_root(file)
if dir and not dirs[dir] then
dirs[dir] = true