Skip to content

Commit dc0e848

Browse files
change(command): add completion to command
1 parent 79fac76 commit dc0e848

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

lua/php-use-sort/init.lua

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,37 +110,32 @@ local function setup_autocmd()
110110
group = group,
111111
})
112112
end
113-
114-
local function get_completions(context)
115-
local completions = {}
116-
local words = vim.split(context, "%s")
117-
local current_word = words[#words] or ""
118-
119-
if #words == 2 and current_word == "" then
120-
completions = { "length", "alphabetical" }
121-
elseif #words == 3 and current_word == "" then
122-
completions = { "asc", "desc" }
123-
end
124-
125-
return completions
126-
end
127-
128-
local function setup_command()
113+
function setup_command()
129114
vim.api.nvim_create_user_command("PhpUseSort", function(opts)
130-
local args = vim.split(opts.args, "%s")
115+
local args = vim.split(opts.args, "%s+")
131116
local order_by = args[1] or ""
132117
local sort_order = args[2] or ""
133118

134119
PhpUseSort.main(order_by, sort_order)
135120
end, {
136121
nargs = "?",
137-
complete = function(_, _, _)
138-
local context = vim.fn.getcmdline()
122+
complete = function(_, line)
123+
local order_by = { "alphabetical", "length" }
124+
local direction = { "asc", "desc" }
139125

140-
if context ~= nil then
141-
return get_completions(context)
142-
else
143-
return {}
126+
local l = vim.split(line, "%s+")
127+
local n = #l - 2
128+
129+
if n == 0 then
130+
return vim.tbl_filter(function(val)
131+
return vim.startswith(val, l[2])
132+
end, order_by)
133+
end
134+
135+
if n == 1 then
136+
return vim.tbl_filter(function(val)
137+
return vim.startswith(val, l[3])
138+
end, direction)
144139
end
145140
end,
146141
desc = "Sort PHP use lines by length or alphabetical order. Accepts sorting options.",
@@ -193,8 +188,8 @@ end
193188

194189
function PhpUseSort.setup(options)
195190
require("php-use-sort.config").setup(options.opts)
196-
setup_command()
197191
setup_autocmd()
192+
setup_command()
198193
end
199194

200195
return PhpUseSort

0 commit comments

Comments
 (0)