| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Code for generating a Bash auto completion file.''' |
| 5 |
|
| 6 |
from collections import OrderedDict |
| 7 |
|
| 8 |
from . import config as config_ |
| 9 |
from . import shell |
| 10 |
from . import algo |
| 11 |
from . import utils |
| 12 |
from . import helpers |
| 13 |
from . import bash_helpers |
| 14 |
from . import bash_complete |
| 15 |
from . import bash_parser |
| 16 |
from . import bash_parser_v2 |
| 17 |
from . import bash_option_completion |
| 18 |
from . import bash_option_strings_completion |
| 19 |
from . import bash_positionals_completion |
| 20 |
from . import bash_versions |
| 21 |
from . import bash_patterns |
| 22 |
from .str_utils import indent, join_with_wrap |
| 23 |
from .bash_utils import VariableManager |
| 24 |
from .output import Output |
| 25 |
from . import generation |
| 26 |
|
| 27 |
|
| 28 |
class BashCompletionGenerator: |
| 29 |
'''Class for generating completions.''' |
| 30 |
|
| 31 |
# pylint: disable=too-few-public-methods |
| 32 |
# pylint: disable=too-many-instance-attributes |
| 33 |
|
| 34 |
def __init__(self, ctxt, commandline): |
| 35 |
self.ctxt = ctxt |
| 36 |
self.commandline = commandline |
| 37 |
self.options = commandline.get_options() |
| 38 |
self.positionals = commandline.get_positionals() |
| 39 |
self.subcommands = commandline.get_subcommands() |
| 40 |
self.completer = bash_complete.BashCompleter() |
| 41 |
self.variable_manager = VariableManager('OPT_') |
| 42 |
self._generate() |
| 43 |
|
| 44 |
def complete_option(self, option): |
| 45 |
'''Complete an option.''' |
| 46 |
context = self.ctxt.get_option_context(self.commandline, option) |
| 47 |
obj = self.completer.complete(context, [], *option.complete) |
| 48 |
code = obj.get_code(False) |
| 49 |
if code and option.nosort: |
| 50 |
code += '\ncompopt -o nosort' |
| 51 |
return code |
| 52 |
|
| 53 |
def _generate_commandline_parsing(self): |
| 54 |
local_vars = [] |
| 55 |
for cmdline in self.commandline.get_all_commandlines(): |
| 56 |
for option in cmdline.options: |
| 57 |
if option.capture is not None: |
| 58 |
local_vars.append(option.capture) |
| 59 |
|
| 60 |
r = 'local END_OF_OPTIONS POSITIONALS\n' |
| 61 |
|
| 62 |
if local_vars: |
| 63 |
local_vars = algo.uniq(local_vars) |
| 64 |
line_length = self.ctxt.config.line_length - 2 |
| 65 |
r += join_with_wrap(' ', '\n', line_length, local_vars, 'local -a ') |
| 66 |
|
| 67 |
r += '\n%s' % self.ctxt.helpers.use_function('parse_commandline') |
| 68 |
return r |
| 69 |
|
| 70 |
def _generate_subcommand_call(self): |
| 71 |
if not self.subcommands: |
| 72 |
return None |
| 73 |
|
| 74 |
positional_num = self.subcommands.get_positional_num() |
| 75 |
r = 'if (( %i < ${#POSITIONALS[@]} )); then\n' % (positional_num - 1) |
| 76 |
r += ' case "${POSITIONALS[%i]}" in\n' % (positional_num - 1) |
| 77 |
for subcommand in self.subcommands.subcommands: |
| 78 |
cmds = utils.get_all_command_variations(subcommand) |
| 79 |
pattern = bash_patterns.make_pattern([shell.quote(s) for s in cmds]) |
| 80 |
funcname = self.ctxt.helpers.make_completion_funcname(subcommand) |
| 81 |
if utils.is_worth_a_function(subcommand): |
| 82 |
if self.commandline.inherit_options: |
| 83 |
r += ' %s) %s && return 0;;\n' % (pattern, funcname) |
| 84 |
else: |
| 85 |
r += ' %s) %s && return 0 || return 1;;\n' % (pattern, funcname) |
| 86 |
else: |
| 87 |
if self.commandline.inherit_options: |
| 88 |
r += ' %s);;\n' % pattern |
| 89 |
else: |
| 90 |
r += ' %s) return 0;;\n' % pattern |
| 91 |
r += ' esac\n' |
| 92 |
r += 'fi' |
| 93 |
return r |
| 94 |
|
| 95 |
def _generate_command_arg_call(self): |
| 96 |
def is_command_arg(positional): |
| 97 |
return (positional.complete and |
| 98 |
positional.complete[0] == 'command_arg') |
| 99 |
|
| 100 |
try: |
| 101 |
positional = list(filter(is_command_arg, self.positionals))[0] |
| 102 |
except IndexError: |
| 103 |
return None |
| 104 |
|
| 105 |
num = positional.get_positional_num() |
| 106 |
r = 'if (( ${#POSITIONALS[@]} >= %d )); then\n' % num |
| 107 |
r += ' local realpos\n' |
| 108 |
r += ' for realpos in "${!COMP_WORDS[@]}"; do\n' |
| 109 |
r += ' [[ "${COMP_WORDS[realpos]}" == "${POSITIONALS[%d]}" ]] && {\n' % (num - 2) |
| 110 |
r += ' %s $realpos\n' % bash_versions.command_offset(self.ctxt) |
| 111 |
r += ' return 0;\n' |
| 112 |
r += ' }\n' |
| 113 |
r += ' done\n' |
| 114 |
r += 'fi' |
| 115 |
return r |
| 116 |
|
| 117 |
def _generate(self): |
| 118 |
# The completion function returns 0 (success) if there was a completion match. |
| 119 |
# This return code is used for dealing with subcommands. |
| 120 |
|
| 121 |
if not utils.is_worth_a_function(self.commandline): |
| 122 |
self.result = '' |
| 123 |
return |
| 124 |
|
| 125 |
code = OrderedDict() |
| 126 |
code['init_completion'] = None |
| 127 |
code['command_line_parsing'] = None |
| 128 |
code['subcommand_call'] = None |
| 129 |
code['command_arg'] = None |
| 130 |
code['option_completion'] = None |
| 131 |
code['option_strings_completion'] = None |
| 132 |
code['positional_completion'] = None |
| 133 |
|
| 134 |
code['subcommand_call'] = self._generate_subcommand_call() |
| 135 |
code['command_arg'] = self._generate_command_arg_call() |
| 136 |
code['option_completion'] = bash_option_completion.generate_option_completion(self) |
| 137 |
code['option_strings_completion'] = bash_option_strings_completion.generate(self) |
| 138 |
code['positional_completion'] = bash_positionals_completion.generate(self) |
| 139 |
|
| 140 |
if self.commandline.parent is None: |
| 141 |
# The root parser makes those variables local and sets up the completion. |
| 142 |
r = 'local cur prev words cword split words_dequoted\n' |
| 143 |
r += '%s -n =: || return\n' % bash_versions.init_completion(self.ctxt) |
| 144 |
r += self.ctxt.helpers.use_function('dequote_words') |
| 145 |
code['init_completion'] = r |
| 146 |
|
| 147 |
v1 = bash_parser.generate(self.commandline, self.variable_manager) |
| 148 |
v2 = bash_parser_v2.generate(self.commandline, self.variable_manager) |
| 149 |
c = v1 if len(v1) < len(v2) else v2 |
| 150 |
|
| 151 |
func = helpers.ShellFunction('parse_commandline', c) |
| 152 |
self.ctxt.helpers.add_function(func) |
| 153 |
|
| 154 |
# This sets up END_OF_OPTIONS, POSITIONALS and the OPT_* variables. |
| 155 |
code['command_line_parsing'] = self._generate_commandline_parsing() |
| 156 |
|
| 157 |
r = '%s() {\n' % self.ctxt.helpers.make_completion_funcname(self.commandline) |
| 158 |
r += '%s\n\n' % indent('\n\n'.join(c for c in code.values() if c), 2) |
| 159 |
r += ' return 1\n' |
| 160 |
r += '}' |
| 161 |
|
| 162 |
self.result = r |
| 163 |
|
| 164 |
|
| 165 |
def _generate_wrapper(ctxt, commandline): |
| 166 |
make_completion_funcname = ctxt.helpers.make_completion_funcname |
| 167 |
completion_funcname = make_completion_funcname(commandline) |
| 168 |
wrapper_funcname = make_completion_funcname(commandline, '__wrapper') |
| 169 |
|
| 170 |
if not commandline.wraps: |
| 171 |
return (completion_funcname, None) |
| 172 |
|
| 173 |
r = '%s %s\n' % (bash_versions.completion_loader(ctxt), commandline.wraps) |
| 174 |
r += '\n' |
| 175 |
r += '%s() {\n' % wrapper_funcname |
| 176 |
r += ' local WRAPS=%s\n' % shell.quote(commandline.wraps) |
| 177 |
r += ' local COMP_LINE_OLD="$COMP_LINE"\n' |
| 178 |
r += ' local COMP_POINT_OLD="$COMP_POINT"\n' |
| 179 |
r += ' local COMP_WORDS_OLD=("${COMP_WORDS[@]}")\n' |
| 180 |
r += '\n' |
| 181 |
r += ' COMP_LINE="${COMP_LINE/$1/$WRAPS}"\n' |
| 182 |
r += ' COMP_WORDS[0]=%s\n' % shell.quote(commandline.wraps) |
| 183 |
r += ' COMP_POINT=$(( COMP_POINT + ${#WRAPS} - ${#1} ))\n' |
| 184 |
r += '\n' |
| 185 |
r += ' %s 0\n' % bash_versions.command_offset(ctxt) |
| 186 |
r += '\n' |
| 187 |
r += ' COMP_LINE="$COMP_LINE_OLD"\n' |
| 188 |
r += ' COMP_POINT="$COMP_POINT_OLD"\n' |
| 189 |
r += ' COMP_WORDS=("${COMP_WORDS_OLD[@]}")\n' |
| 190 |
r += ' local COMPREPLY_OLD=("${COMPREPLY[@]}")\n' |
| 191 |
r += ' %s "$@"\n' % completion_funcname |
| 192 |
r += '\n' |
| 193 |
r += ' COMPREPLY=("${COMPREPLY_OLD[@]}" "${COMPREPLY[@]}")\n' |
| 194 |
r += '}' |
| 195 |
|
| 196 |
return (wrapper_funcname, r) |
| 197 |
|
| 198 |
|
| 199 |
def generate_completion(commandline, config=None): |
| 200 |
'''Code for generating a Bash auto completion file.''' |
| 201 |
|
| 202 |
if config is None: |
| 203 |
config = config_.Config() |
| 204 |
|
| 205 |
commandline = generation.enhance_commandline(commandline, config) |
| 206 |
helpers = bash_helpers.BashHelpers(config, commandline.prog) |
| 207 |
ctxt = generation.GenerationContext(config, helpers) |
| 208 |
|
| 209 |
if ctxt.config.bash_completions_version >= (2, 12): |
| 210 |
helpers.define('bash_completions_v_2_12') |
| 211 |
|
| 212 |
result = generation.visit_commandlines(BashCompletionGenerator, ctxt, commandline) |
| 213 |
|
| 214 |
completion_func, wrapper_code = _generate_wrapper(ctxt, commandline) |
| 215 |
|
| 216 |
output = Output(config, helpers) |
| 217 |
output.add_generation_notice() |
| 218 |
output.add_comments() |
| 219 |
output.add_included_files() |
| 220 |
output.add_helper_functions_code() |
| 221 |
output.extend(generator.result for generator in result) |
| 222 |
output.add(wrapper_code) |
| 223 |
output.add('complete -F %s %s' % ( |
| 224 |
completion_func, ' '.join([commandline.prog] + commandline.aliases) |
| 225 |
)) |
| 226 |
output.add_vim_modeline('sh') |
| 227 |
|
| 228 |
return output.get() |