1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2025-2026 Benjamin Abendroth <braph93@gmx.de>
'''Code for generating a Zsh auto completion file.'''
from collections import namedtuple, OrderedDict
from . import config as config_
from . import generation
from . import shell
from . import utils
from . import zsh_complete
from . import zsh_helpers
from . import zsh_utils
from . import zsh_when
from . import zsh_wrapper
from .output import Output
from .str_utils import indent
Arg = namedtuple('Arg', ('option', 'when', 'option_spec'))
class ZshQuery:
'''Helper class for using the `query` function.'''
# pylint: disable=too-few-public-methods
def __init__(self, ctxt):
self.ctxt = ctxt
self.used = False
def use(self, command=None):
'''Return the function name of query and use a command.'''
self.used = True
return self.ctxt.helpers.use_function('query', command)
class ZshCompletionFunction:
'''Class for generating a zsh completion function.'''
# pylint: disable=too-many-branches
# pylint: disable=too-few-public-methods
# pylint: disable=too-many-instance-attributes
def __init__(self, ctxt, commandline):
self.commandline = commandline
self.ctxt = ctxt
self.funcname = ctxt.helpers.make_completion_funcname(commandline)
self.subcommands = commandline.get_subcommands()
self.command_counter = 0
self.completer = zsh_complete.ZshCompleter()
self.code = None
self.query = ZshQuery(ctxt)
self._generate_completion_code()
def _complete(self, option, command, *args):
context = self.ctxt.get_option_context(self.commandline, option)
return self.completer.complete(context, [], command, *args)
def _complete_option(self, option):
if option.complete:
action = self._complete(option, *option.complete).get_action_string()
else:
action = None
option_spec = zsh_utils.make_option_spec(
option.option_strings,
conflicting_options = option.get_conflicting_option_strings(),
description = option.help,
complete = option.complete,
optional_arg = option.optional_arg,
repeatable = option.repeatable,
hidden = option.hidden,
final = option.final,
metavar = option.metavar,
action = action,
long_opt_arg_sep = option.long_opt_arg_sep
)
return Arg(option, option.when, option_spec)
def _complete_subcommands(self, positional):
choices = positional.get_choices()
self.command_counter += 1
spec = zsh_utils.make_positional_spec(
positional.get_positional_num(),
False,
f'command{self.command_counter}',
self._complete(positional, 'choices', choices).get_action_string()
)
return Arg(positional, None, spec)
def _complete_positional(self, positional):
spec = zsh_utils.make_positional_spec(
positional.get_positional_num(),
positional.repeatable,
positional.help or positional.metavar or ' ',
self._complete(positional, *positional.complete).get_action_string()
)
return Arg(positional, positional.when, spec)
def _generate_completion_code(self):
self.code = OrderedDict()
self.code['0-init'] = ''
self.code['1-capture'] = ''
self.code['2-subcommands'] = ''
self.code['3-options'] = ''
# We have to call these functions first, because they tell us if
# the query function is used.
self.code['1-capture'] = self._generate_option_capture()
self.code['2-subcommands'] = self._generate_subcommand_call()
self.code['3-options'] = self._generate_option_parsing()
if self.query.used:
r = 'local opts=%s\n' % shell.quote(utils.get_query_option_strings(self.commandline))
r += "local HAVING_OPTIONS=() OPTION_VALUES=() POSITIONALS=() INCOMPLETE_OPTION=''\n"
r += '%s init "$opts" "${words[@]}"' % self.query.use()
self.code['0-init'] = r
def _generate_option_capture(self):
local_vars = []
set_cmds = []
for option in self.commandline.options:
if option.capture:
local_vars.append(option.capture)
set_cmds.append('IFS=$"\\n" %s=($(%s get_option %s))' % (
option.capture,
self.query.use('get_option'),
shell.join_quoted(option.option_strings)))
if local_vars:
r = 'local %s' % ' '.join(f'{s}=()' for s in local_vars)
for cmd in set_cmds:
r += '\n%s' % cmd
return r
return ''
def _generate_subcommand_call(self):
if not self.subcommands:
return ''
query = self.query.use('get_positional')
positional_num = self.subcommands.get_positional_num()
r = 'case "$(%s get_positional %d)" in\n' % (query, positional_num)
for subcommand in self.subcommands.subcommands:
sub_funcname = self.ctxt.helpers.make_completion_funcname(subcommand)
cmds = utils.get_all_command_variations(subcommand)
pattern = '|'.join(shell.quote(s) for s in cmds)
r += f' {pattern}) {sub_funcname}; return $?;;\n'
r += 'esac'
return r
def _generate_option_parsing(self):
args = []
if self.commandline.inherit_options:
options = self.commandline.get_options(with_parent_options=True)
else:
options = self.commandline.get_options()
for option in options:
args.append(self._complete_option(option))
for cmdline in self.commandline.get_parents():
for option in cmdline.get_positionals():
args.append(self._complete_positional(option))
if cmdline.get_subcommands():
args.append(self._complete_subcommands(cmdline.get_subcommands()))
for option in self.commandline.get_positionals():
args.append(self._complete_positional(option))
if self.subcommands:
args.append(self._complete_subcommands(self.subcommands))
if self.commandline.wraps:
args.append(Arg(None, None, "'::*'"))
if not args:
return ''
args_with_when = []
args_without_when = []
for arg in args:
if arg.when is None:
args_without_when.append(arg)
else:
args_with_when.append(arg)
r = ''
if not args_without_when:
r += 'local -a args=()\n'
else:
r += 'local -a args=(\n'
for arg in args_without_when:
r += ' %s\n' % arg.option_spec
r += ')\n'
for arg in args_with_when:
when_cmd = zsh_when.generate_when_conditions(self.query, arg.when)
r += '%s &&\\\n' % when_cmd
r += ' args+=(%s)\n' % arg.option_spec
if self.ctxt.config.option_stacking:
r += '_arguments -S -s -w "${args[@]}"'
else:
r += '_arguments -S "${args[@]}"'
return r
def get_code(self):
'''Return the code of the completion function.'''
return '%s() {\n%s\n}' % (
self.funcname,
indent('\n\n'.join(c for c in self.code.values() if c), 2))
def get_zstyles(commandline, out_list):
'''Get zstyle commands for disabling sorting completion suggestions.'''
prog = commandline.get_program_name()
for option in filter(lambda o: o.nosort, commandline.options):
for option_string in option.option_strings:
r = f"zstyle ':completion:*:*:{prog}:option{option_string}-*' sort false"
out_list.append(r)
for positional in filter(lambda p: p.nosort, commandline.positionals):
number = positional.number
r = f"zstyle ':completion:*:*:{prog}:argument-{number}:*' sort false"
out_list.append(r)
def generate_completion(commandline, config=None):
'''Code for generating a Zsh auto completion file.'''
if config is None:
config = config_.Config()
commandline = generation.enhance_commandline(commandline, config)
helpers = zsh_helpers.ZshHelpers(config, commandline.prog)
ctxt = generation.GenerationContext(config, helpers)
functions = generation.visit_commandlines(ZshCompletionFunction, ctxt, commandline)
all_progs = ' '.join([commandline.prog] + commandline.aliases)
if helpers.is_used('query'):
types = utils.get_defined_option_types(commandline)
if types.short:
ctxt.helpers.use_function('query', 'short_options')
if types.long:
ctxt.helpers.use_function('query', 'long_options')
if types.old:
ctxt.helpers.use_function('query', 'old_options')
output = Output(config, helpers)
if config.zsh_compdef:
output.add('#compdef %s' % all_progs)
output.add_generation_notice()
output.add_comments()
output.add_included_files()
completion_func, wrapper_code = zsh_wrapper.generate_wrapper(ctxt, commandline)
output.add_helper_functions_code()
output.extend(function.get_code() for function in functions)
zstyles = []
commandline.visit_commandlines(lambda c: get_zstyles(c, zstyles))
if zstyles:
output.add('\n'.join(zstyles))
if wrapper_code:
output.add(wrapper_code)
if config.zsh_compdef:
output.add('%s "$@"' % completion_func)
else:
output.add('compdef %s %s' % (completion_func, all_progs))
output.add_vim_modeline('zsh')
return output.get()
|