Skip to content

Commit

Permalink
feat(cmd): Add path complete for optional args
Browse files Browse the repository at this point in the history
  • Loading branch information
EdenEast committed Oct 4, 2022
1 parent 6412719 commit 2b30bc9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lua/packer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,9 @@ end
-- Completion user plugins
-- Intended to provide completion for PackerUpdate/Sync/Install command
packer.plugin_complete = function(lead, _, _)
if vim.startswith(lead, '--lockfile=') then
return require('packer.util').path_complete(lead)
end
if vim.startswith(lead, '-') then
return vim.tbl_filter(function(name)
return vim.startswith(name, lead)
Expand Down
4 changes: 4 additions & 0 deletions lua/packer/lockfile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ local function collect_commits(plugins)
end

lockfile.completion = function(lead, _, _)
if vim.startswith(lead, '--path=') then
return require("packer.util").path_complete(lead)
end

if vim.startswith(lead, '-') then
return vim.tbl_filter(function(name)
return vim.startswith(name, lead)
Expand Down
38 changes: 38 additions & 0 deletions lua/packer/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,44 @@ util.join_paths = function(...)
return table.concat({ ... }, separator)
end

util.path_complete = function(lead)
local split = vim.split(lead, '=')
local command, path = split[1] .. '=', split[2]
if #path == 0 then
path = '.'
end
path = vim.fs.normalize(path)

local completion_list = {}
local is_dir = vim.fn.isdirectory(path) == 1
local dirpath = is_dir and path or vim.fs.dirname(path)
local filepath = not is_dir and vim.fs.basename(path)

local sep = util.get_separator()
local dir = vim.loop.fs_opendir(dirpath)

local function join(d, f)
return d:sub(#d, #d) == sep and d .. f or d .. sep .. f
end

local res = vim.loop.fs_readdir(dir)
while res ~= nil do
for _, entry in ipairs(res) do
if filepath then
if vim.startswith(entry.name, filepath) then
completion_list[#completion_list + 1] = command .. join(dirpath, entry.name)
end
else
completion_list[#completion_list + 1] = command .. join(dirpath, entry.name)
end
end
res = vim.loop.fs_readdir(dir)
end

vim.loop.fs_closedir(dir)
return completion_list
end

util.get_plugin_short_name = function(plugin)
local path = vim.fn.expand(plugin[1])
local name_segments = vim.split(path, util.get_separator())
Expand Down

0 comments on commit 2b30bc9

Please sign in to comment.