refactor(picker): pass finder context to transform

This commit is contained in:
Folke Lemaitre 2025-01-22 23:47:00 +01:00
parent 5a4b684c0d
commit 9c0e2d4362
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
7 changed files with 23 additions and 42 deletions

View file

@ -6,7 +6,7 @@ local M = {}
---@alias snacks.picker.format fun(item:snacks.picker.Item, picker:snacks.Picker):snacks.picker.Highlight[]
---@alias snacks.picker.preview fun(ctx: snacks.picker.preview.ctx):boolean?
---@alias snacks.picker.sort fun(a:snacks.picker.Item, b:snacks.picker.Item):boolean
---@alias snacks.picker.transform fun(item:snacks.picker.finder.Item):(boolean|snacks.picker.finder.Item|nil)
---@alias snacks.picker.transform fun(item:snacks.picker.finder.Item, ctx:snacks.picker.finder.ctx):(boolean|snacks.picker.finder.Item|nil)
---@alias snacks.picker.Pos {[1]:number, [2]:number}
--- Generic filter used by finders to pre-filter items

View file

@ -117,12 +117,12 @@ function M.files(opts, ctx)
item.file = item.text
end,
},
})
}, ctx)
end
---@param opts snacks.picker.proc.Config
---@type snacks.picker.finder
function M.zoxide(opts)
function M.zoxide(opts, ctx)
return require("snacks.picker.source.proc").proc({
opts,
{
@ -133,7 +133,7 @@ function M.zoxide(opts)
item.file = item.text
end,
},
})
}, ctx)
end
return M

View file

@ -12,7 +12,7 @@ local uv = vim.uv or vim.loop
---@param opts snacks.picker.git.files.Config
---@type snacks.picker.finder
function M.files(opts)
function M.files(opts, ctx)
local args = { "-c", "core.quotepath=false", "ls-files", "--exclude-standard", "--cached" }
if opts.untracked then
table.insert(args, "--others")
@ -31,7 +31,7 @@ function M.files(opts)
item.file = item.text
end,
},
})
}, ctx)
end
---@param opts snacks.picker.git.log.Config
@ -87,12 +87,12 @@ function M.log(opts, ctx)
item.file = file
end,
},
})
}, ctx)
end
---@param opts snacks.picker.Config
---@type snacks.picker.finder
function M.status(opts)
function M.status(opts, ctx)
local args = {
"--no-pager",
"status",
@ -116,19 +116,19 @@ function M.status(opts)
item.file = file
end,
},
})
}, ctx)
end
---@param opts snacks.picker.Config
---@type snacks.picker.finder
function M.diff(opts)
function M.diff(opts, ctx)
local args = { "--no-pager", "diff", "--no-color", "--no-ext-diff" }
local file, line ---@type string?, number?
local header, hunk = {}, {} ---@type string[], string[]
local finder = require("snacks.picker.source.proc").proc({
opts,
{ cmd = "git", args = args },
})
}, ctx)
return function(cb)
local function add()
if file and line and #hunk > 0 then
@ -168,7 +168,7 @@ end
---@param opts snacks.picker.Config
---@type snacks.picker.finder
function M.branches(opts)
function M.branches(opts, ctx)
local args = { "--no-pager", "branch", "--no-color", "-vvl" }
local cwd = vim.fs.normalize(opts and opts.cwd or uv.cwd() or ".") or nil
cwd = Snacks.git.get_root(cwd)
@ -208,7 +208,7 @@ function M.branches(opts)
return false -- skip items we could not parse
end,
},
})
}, ctx)
end
return M

View file

@ -129,7 +129,7 @@ function M.grep(opts, ctx)
end
end,
},
})
}, ctx)
end
return M

View file

@ -16,11 +16,12 @@ local islist = vim.islist or vim.tbl_islist
---@field transform? snacks.picker.transform
---@param opts snacks.picker.proc.Config|{[1]: snacks.picker.Config, [2]: snacks.picker.proc.Config}
---@return fun(cb:async fun(item:snacks.picker.finder.Item))
function M.proc(opts)
---@type snacks.picker.finder
function M.proc(opts, ctx)
if islist(opts) then
local transform = opts[2].transform
opts = Snacks.config.merge(unpack(vim.deepcopy(opts))) --[[@as snacks.picker.proc.Config]]
opts.transform = opts[2].transform
opts.transform = transform
end
assert(opts.cmd, "`opts.cmd` is required")
---@async
@ -28,7 +29,7 @@ function M.proc(opts)
if opts.transform then
local _cb = cb
cb = function(item)
local t = opts.transform(item)
local t = opts.transform(item, ctx)
item = type(t) == "table" and t or item
if t ~= false then
_cb(item)

View file

@ -1,20 +0,0 @@
local M = {}
---@class snacks.picker
---@field smart fun(opts?: snacks.picker.smart.Config): snacks.picker.finder
---@param opts snacks.picker.smart.Config
---@type snacks.picker.finder
function M.smart(opts, filter)
local done = {} ---@type table<string, boolean>
local finder = Snacks.picker.config.finder(opts.finders or { "files", "buffers", "recent" })
return require("snacks.picker.core.finder").wrap(finder, function(item)
local path = Snacks.picker.util.path(item)
if not path or done[path] then
return false
end
done[path] = true
end)(opts, filter)
end
return M

View file

@ -6,7 +6,7 @@ local M = {}
---@param opts snacks.picker.proc.Config
---@type snacks.picker.finder
function M.cliphist(opts)
function M.cliphist(opts, ctx)
return require("snacks.picker.source.proc").proc({
opts,
{
@ -39,12 +39,12 @@ function M.cliphist(opts)
end
end,
},
})
}, ctx)
end
---@param opts snacks.picker.proc.Config
---@type snacks.picker.finder
function M.man(opts)
function M.man(opts, ctx)
return require("snacks.picker.source.proc").proc({
opts,
{
@ -64,7 +64,7 @@ function M.man(opts)
end
end,
},
})
}, ctx)
end
return M