Zsh completions: Improve option completion

- support arguments with choices
- allow invoking an option multiple times
- complete files if an argument is required, but no choices are
  specified
This commit is contained in:
Paul Ouellette
2019-07-09 00:24:44 -04:00
parent c545e49701
commit c2b2a16abe

View File

@@ -1290,27 +1290,38 @@ end
function Parser:get_zsh_complete() function Parser:get_zsh_complete()
local buf = {("compdef _%s %s\n"):format(self._name, self._name)} local buf = {("compdef _%s %s\n"):format(self._name, self._name)}
table.insert(buf, ("_%s() {"):format(self._name)) table.insert(buf, "_" .. self._name .. "() {")
table.insert(buf, " typeset -A opt_args") table.insert(buf, [[
table.insert(buf, " local context state line") local context state state_descr line ret=1
table.insert(buf, " local ret=1\n") typeset -A opt_args
table.insert(buf, " _arguments -s -S -C \\")
_arguments -s -S \]])
for _, option in ipairs(self._options) do for _, option in ipairs(self._options) do
local line = {} local line = {}
if #option._aliases > 1 then if #option._aliases > 1 then
table.insert(line, ("{%s}"):format(table.concat(option._aliases, ","))) if option._maxcount > 1 then
if option._description then table.insert(line, '"*"')
table.insert(line, ('"[%s]"'):format(get_short_description(option)))
end end
table.insert(line, "{" .. table.concat(option._aliases, ",") .. '}"')
else else
table.insert(line, '"' .. option._aliases[1]) table.insert(line, '"')
if option._maxcount > 1 then
table.insert(line, "*")
end
table.insert(line, option._aliases[1])
end
if option._description then if option._description then
table.insert(line, ("[%s]"):format(get_short_description(option))) table.insert(line, "[" .. get_short_description(option) .. "]")
end
if option._choices then
table.insert(line, ": :(" .. table.concat(option._choices, " ") .. ")")
elseif option._minargs > 0 then
table.insert(line, ": :_files")
end end
table.insert(line, '"') table.insert(line, '"')
end
table.insert(buf, (" "):rep(4) .. table.concat(line) .. " \\") table.insert(buf, " " .. table.concat(line) .. " \\")
end end
table.insert(buf, " && ret=0\n") table.insert(buf, " && ret=0\n")