feat(picker): added git_grep picker. Closes #986

This commit is contained in:
Folke Lemaitre 2025-02-07 10:41:24 +01:00
parent 155e219f04
commit 2dc901634b
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
2 changed files with 59 additions and 0 deletions

View file

@ -240,6 +240,22 @@ M.git_files = {
submodules = false,
}
-- Grep in git files
---@class snacks.picker.git.grep.Config: snacks.picker.Config
---@field untracked? boolean search in untracked files
---@field submodules? boolean search in submodule files
---@field need_search? boolean require a search pattern
M.git_grep = {
finder = "git_grep",
format = "file",
untracked = false,
need_search = true,
submodules = false,
show_empty = true,
supports_live = true,
live = true,
}
-- Git log
---@class snacks.picker.git.log.Config: snacks.picker.Config
---@field follow? boolean track file history across renames

View file

@ -32,6 +32,49 @@ function M.files(opts, ctx)
}, ctx)
end
---@param opts snacks.picker.git.grep.Config
---@type snacks.picker.finder
function M.grep(opts, ctx)
if opts.need_search ~= false and ctx.filter.search == "" then
return function() end
end
local args = { "-c", "core.quotepath=false", "grep", "--line-number", "--column", "--no-color", "-I" }
if opts.untracked then
table.insert(args, "--untracked")
elseif opts.submodules then
table.insert(args, "--recurse-submodules")
end
table.insert(args, ctx.filter.search)
if not opts.cwd then
opts.cwd = Snacks.git.get_root() or uv.cwd() or "."
ctx.picker:set_cwd(opts.cwd)
end
local cwd = vim.fs.normalize(opts.cwd) or nil
return require("snacks.picker.source.proc").proc({
opts,
{
cmd = "git",
args = args,
notify = false,
---@param item snacks.picker.finder.Item
transform = function(item)
item.cwd = cwd
local file, line, col, text = item.text:match("^(.+):(%d+):(%d+):(.*)$")
if not file then
if not item.text:match("WARNING") then
Snacks.notify.error("invalid grep output:\n" .. item.text)
end
return false
else
item.line = text
item.file = file
item.pos = { tonumber(line), tonumber(col) - 1 }
end
end,
},
}, ctx)
end
---@param opts snacks.picker.git.log.Config
---@type snacks.picker.finder
function M.log(opts, ctx)