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