Skip to content

Commit 865ef7e

Browse files
committed
feat(cmd): Add path complete for optional args
1 parent ddd4a46 commit 865ef7e

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

Diff for: lua/packer.lua

+3
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,9 @@ end
954954
-- Completion user plugins
955955
-- Intended to provide completion for PackerUpdate/Sync/Install command
956956
packer.plugin_complete = function(lead, _, _)
957+
if vim.startswith(lead, '--lockfile=') then
958+
return require('packer.util').path_complete(lead)
959+
end
957960
if vim.startswith(lead, '-') then
958961
return vim.tbl_filter(function(name)
959962
return vim.startswith(name, lead)

Diff for: lua/packer/lockfile.lua

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ local function collect_commits(plugins)
6565
end
6666

6767
lockfile.completion = function(lead, _, _)
68+
if vim.startswith(lead, '--path=') then
69+
return require("packer.util").path_complete(lead)
70+
end
71+
6872
if vim.startswith(lead, '-') then
6973
return vim.tbl_filter(function(name)
7074
return vim.startswith(name, lead)

Diff for: lua/packer/util.lua

+38
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,44 @@ util.join_paths = function(...)
6464
return table.concat({ ... }, separator)
6565
end
6666

67+
util.path_complete = function(lead)
68+
local split = vim.split(lead, '=')
69+
local command, path = split[1] .. '=', split[2]
70+
if #path == 0 then
71+
path = '.'
72+
end
73+
path = vim.fs.normalize(path)
74+
75+
local completion_list = {}
76+
local is_dir = vim.fn.isdirectory(path) == 1
77+
local dirpath = is_dir and path or vim.fs.dirname(path)
78+
local filepath = not is_dir and vim.fs.basename(path)
79+
80+
local sep = util.get_separator()
81+
local dir = vim.loop.fs_opendir(dirpath)
82+
83+
local function join(d, f)
84+
return d:sub(#d, #d) == sep and d .. f or d .. sep .. f
85+
end
86+
87+
local res = vim.loop.fs_readdir(dir)
88+
while res ~= nil do
89+
for _, entry in ipairs(res) do
90+
if filepath then
91+
if vim.startswith(entry.name, filepath) then
92+
completion_list[#completion_list + 1] = command .. join(dirpath, entry.name)
93+
end
94+
else
95+
completion_list[#completion_list + 1] = command .. join(dirpath, entry.name)
96+
end
97+
end
98+
res = vim.loop.fs_readdir(dir)
99+
end
100+
101+
vim.loop.fs_closedir(dir)
102+
return completion_list
103+
end
104+
67105
util.get_plugin_short_name = function(plugin)
68106
local path = vim.fn.expand(plugin[1])
69107
local name_segments = vim.split(path, util.get_separator())

0 commit comments

Comments
 (0)