mirror of
https://github.com/folke/snacks.nvim
synced 2025-08-06 03:38:16 +00:00
feat(picker): added git_grep
picker. Closes #986
This commit is contained in:
parent
155e219f04
commit
2dc901634b
2 changed files with 59 additions and 0 deletions
|
@ -240,6 +240,22 @@ M.git_files = {
|
||||||
submodules = false,
|
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
|
-- Git log
|
||||||
---@class snacks.picker.git.log.Config: snacks.picker.Config
|
---@class snacks.picker.git.log.Config: snacks.picker.Config
|
||||||
---@field follow? boolean track file history across renames
|
---@field follow? boolean track file history across renames
|
||||||
|
|
|
@ -32,6 +32,49 @@ function M.files(opts, ctx)
|
||||||
}, ctx)
|
}, ctx)
|
||||||
end
|
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
|
---@param opts snacks.picker.git.log.Config
|
||||||
---@type snacks.picker.finder
|
---@type snacks.picker.finder
|
||||||
function M.log(opts, ctx)
|
function M.log(opts, ctx)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue