Skip to content

Commit 85903aa

Browse files
committed
feat(git): Better pathspec completion.
1 parent f242d54 commit 85903aa

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

lua/diffview/init.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ M.completers = {
190190
local candidates = {}
191191

192192
if ctx.argidx > ctx.divideridx then
193-
utils.vec_push(candidates, unpack(vim.fn.getcompletion(ctx.arg_lead, "file", 0)))
193+
if adapter then
194+
utils.vec_push(candidates, unpack(adapter:path_completion(ctx.arg_lead)))
195+
else
196+
utils.vec_push(candidates, unpack(vim.fn.getcompletion(ctx.arg_lead, "file", 0)))
197+
end
194198
elseif adapter then
195199
if not has_rev_arg and ctx.arg_lead:sub(1, 1) ~= "-" then
196200
utils.vec_push(candidates, unpack(adapter.comp.open:get_all_names()))
@@ -217,10 +221,11 @@ M.completers = {
217221
adapter.comp.file_history:get_completion(ctx.arg_lead)
218222
or adapter.comp.file_history:get_all_names()
219223
))
224+
utils.vec_push(candidates, unpack(adapter:path_completion(ctx.arg_lead)))
225+
else
226+
utils.vec_push(candidates, unpack(vim.fn.getcompletion(ctx.arg_lead, "file", 0)))
220227
end
221228

222-
utils.vec_push(candidates, unpack(vim.fn.getcompletion(ctx.arg_lead, "file", 0)))
223-
224229
return candidates
225230
end,
226231
}

lua/diffview/vcs/adapter.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,5 +384,11 @@ VCSAdapter.flags = {
384384
options = {},
385385
}
386386

387+
---@param arg_lead string
388+
---@return string[]
389+
function VCSAdapter:path_completion(arg_lead)
390+
return vim.fn.getcompletion(arg_lead, "file", 0)
391+
end
392+
387393
M.VCSAdapter = VCSAdapter
388394
return M

lua/diffview/vcs/adapters/git/init.lua

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ GitAdapter.Rev = GitRev
3030

3131
---@return string, string
3232
function M.pathspec_split(pathspec)
33-
local magic = pathspec:match("^:[/!^]*:?") or pathspec:match("^:%b()") or ""
33+
local magic = utils.str_match(pathspec, {
34+
"^:[/!^]+:?",
35+
"^:%b()",
36+
"^:",
37+
}) or ""
3438
local pattern = pathspec:sub(1 + #magic, -1)
3539
return magic or "", pattern or ""
3640
end
@@ -1572,7 +1576,10 @@ GitAdapter.flags = {
15721576
end, value)
15731577
), " ")
15741578
end,
1575-
completion = function(_)
1579+
---@param panel FHOptionPanel
1580+
completion = function(panel)
1581+
local view = panel.parent.parent
1582+
15761583
return function(_, cmd_line, cur_pos)
15771584
local ok, ctx = pcall(arg_parser.scan_sh_args, cmd_line, cur_pos)
15781585

@@ -1586,7 +1593,7 @@ GitAdapter.flags = {
15861593
utils.vec_slice(quoted, 1, ctx.argidx - 1),
15871594
utils.str_quote(v, { only_if_whitespace = true })
15881595
), " ")
1589-
end, vim.fn.getcompletion(ctx.arg_lead, "file"))
1596+
end, view.adapter:path_completion(ctx.arg_lead))
15901597
end
15911598
end
15921599
end,
@@ -1641,6 +1648,14 @@ end
16411648

16421649
-- Completion
16431650

1651+
function GitAdapter:path_completion(arg_lead)
1652+
local magic, pattern = M.pathspec_split(arg_lead)
1653+
1654+
return vim.tbl_map(function(v)
1655+
return magic .. v
1656+
end, vim.fn.getcompletion(pattern, "file", 0))
1657+
end
1658+
16441659
function GitAdapter:rev_candidates()
16451660
logger.lvl(1).debug("[completion] Revision candidates requested.")
16461661
-- stylua: ignore start

0 commit comments

Comments
 (0)