| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Code for generating a Fish auto completion file.''' |
| 5 |
|
| 6 |
from . import config as config_ |
| 7 |
from . import utils |
| 8 |
from . import algo |
| 9 |
from . import fish_prepare |
| 10 |
from . import fish_complete |
| 11 |
from . import fish_helpers |
| 12 |
from . import generation |
| 13 |
from .output import Output |
| 14 |
from .fish_utils import FishCompleteCommand, VariableManager |
| 15 |
from .fish_conditions import ( |
| 16 |
Conditions, Not, |
| 17 |
HasHiddenOption, HasOption, PositionalNum, PositionalContains |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
class FishCompletionDefinition: |
| 22 |
'''Class holding a completion definition.''' |
| 23 |
|
| 24 |
# pylint: disable=too-many-instance-attributes |
| 25 |
# pylint: disable=too-many-arguments |
| 26 |
# pylint: disable=too-many-positional-arguments |
| 27 |
# pylint: disable=too-few-public-methods |
| 28 |
|
| 29 |
def __init__( |
| 30 |
self, |
| 31 |
ctxt, |
| 32 |
short_options=None, # List of short options |
| 33 |
long_options=None, # List of long options |
| 34 |
old_options=None, # List of old-style options |
| 35 |
positional=None, # Positional number |
| 36 |
description=None, # Description |
| 37 |
requires_argument=False, # Option requires an argument |
| 38 |
keep_order=False, # Do not sort completion suggestions |
| 39 |
completion_obj=None): |
| 40 |
|
| 41 |
if short_options is None: |
| 42 |
short_options = [] |
| 43 |
if long_options is None: |
| 44 |
long_options = [] |
| 45 |
if old_options is None: |
| 46 |
old_options = [] |
| 47 |
|
| 48 |
self.ctxt = ctxt |
| 49 |
self.short_options = short_options |
| 50 |
self.long_options = long_options |
| 51 |
self.old_options = old_options |
| 52 |
self.positional = positional |
| 53 |
self.description = description |
| 54 |
self.requires_argument = requires_argument |
| 55 |
self.keep_order = keep_order |
| 56 |
self.completion_obj = completion_obj |
| 57 |
self.conditions = Conditions() |
| 58 |
|
| 59 |
def get_complete_cmd(self, ctxt): |
| 60 |
'''Return a `complete` command for current definition.''' |
| 61 |
|
| 62 |
cmd = FishCompleteCommand() |
| 63 |
cmd.set_command('$prog', raw=True) |
| 64 |
|
| 65 |
cmd.add_old_options(self.old_options) |
| 66 |
cmd.add_long_options(self.long_options) |
| 67 |
|
| 68 |
# The Fish manual for complete says that if short option stacking is |
| 69 |
# not desired, we should add the short options as old options. |
| 70 |
if not ctxt.config.option_stacking: |
| 71 |
cmd.add_old_options(self.short_options) |
| 72 |
else: |
| 73 |
cmd.add_short_options(self.short_options) |
| 74 |
|
| 75 |
if self.description is not None: |
| 76 |
cmd.set_description(self.description) |
| 77 |
|
| 78 |
if self.requires_argument: |
| 79 |
cmd.flags.add('r') |
| 80 |
|
| 81 |
if self.keep_order: |
| 82 |
cmd.flags.add('k') |
| 83 |
|
| 84 |
cmd.parse_args(self.completion_obj.get_args()) |
| 85 |
|
| 86 |
for condition in self.conditions.get_conditions(ctxt): |
| 87 |
cmd.add_condition(condition, raw=True) |
| 88 |
|
| 89 |
return cmd |
| 90 |
|
| 91 |
|
| 92 |
def _get_positional_contains(option): |
| 93 |
commandlines = option.parent.get_parents(include_self=True) |
| 94 |
del commandlines[0] |
| 95 |
|
| 96 |
conditions = [] |
| 97 |
|
| 98 |
for commandline in commandlines: |
| 99 |
cmds = utils.get_all_command_variations(commandline) |
| 100 |
num = commandline.parent.get_subcommands().get_positional_num() |
| 101 |
conditions.append(PositionalContains(num, cmds)) |
| 102 |
|
| 103 |
return conditions |
| 104 |
|
| 105 |
|
| 106 |
class FishCompletionGenerator: |
| 107 |
'''Class for generating completions.''' |
| 108 |
|
| 109 |
# pylint: disable=too-many-instance-attributes |
| 110 |
|
| 111 |
def __init__(self, ctxt, commandline, parent=None): |
| 112 |
self.ctxt = ctxt |
| 113 |
self.commandline = commandline |
| 114 |
self.parent = parent |
| 115 |
self.children = [] |
| 116 |
|
| 117 |
self.completer = fish_complete.FishCompleter() |
| 118 |
self.subcommands = self.commandline.get_subcommands() |
| 119 |
self.lines = [] |
| 120 |
self.conditions = VariableManager('C') |
| 121 |
self.complete_definitions = [] |
| 122 |
|
| 123 |
for option in self.commandline.get_options(): |
| 124 |
self.complete_definitions.append( |
| 125 |
self._complete_option(option)) |
| 126 |
|
| 127 |
for positional in self.commandline.get_positionals(): |
| 128 |
self.complete_definitions.append( |
| 129 |
self._complete_positional(positional)) |
| 130 |
|
| 131 |
if self.subcommands: |
| 132 |
self.complete_definitions.append(self._complete_subcommands()) |
| 133 |
|
| 134 |
for sub in self.subcommands.subcommands: |
| 135 |
self.children.append(FishCompletionGenerator(ctxt, sub, self)) |
| 136 |
|
| 137 |
for definition in self.complete_definitions: |
| 138 |
if isinstance(definition.completion_obj, fish_complete.FishCompleteCommandArg): |
| 139 |
cmdline = self |
| 140 |
while cmdline: |
| 141 |
cmdline._fix_command_arg(definition.positional) |
| 142 |
cmdline = cmdline.parent |
| 143 |
|
| 144 |
for definition in self.complete_definitions: |
| 145 |
cmd = definition.get_complete_cmd(self.ctxt) |
| 146 |
|
| 147 |
if not self.ctxt.config.fish_inline_conditions: |
| 148 |
conditions = cmd.conditions |
| 149 |
cmd.conditions = [] |
| 150 |
|
| 151 |
for condition in conditions: |
| 152 |
cmd.add_condition(self.conditions.add(condition.string), raw=True) |
| 153 |
|
| 154 |
cmd.insert_condition(0, '$P', raw=True) |
| 155 |
self.lines.append(cmd.get()) |
| 156 |
|
| 157 |
def _complete(self, context, *args): |
| 158 |
return self.completer.complete(context, [], *args) |
| 159 |
|
| 160 |
def _fix_command_arg(self, positional): |
| 161 |
for definition in self.complete_definitions: |
| 162 |
if not (definition.short_options or |
| 163 |
definition.long_options or |
| 164 |
definition.old_options): |
| 165 |
continue |
| 166 |
|
| 167 |
definition.conditions.add(Not(PositionalNum('>=', positional - 1))) |
| 168 |
|
| 169 |
def visit(self, callback): |
| 170 |
'''Execute callback for generator objects.''' |
| 171 |
|
| 172 |
callback(self) |
| 173 |
for child in self.children: |
| 174 |
child.visit(callback) |
| 175 |
|
| 176 |
def get_all(self): |
| 177 |
'''Return all generator objects.''' |
| 178 |
|
| 179 |
r = [] |
| 180 |
self.visit(r.append) |
| 181 |
return r |
| 182 |
|
| 183 |
def _complete_option(self, option): |
| 184 |
context = self.ctxt.get_option_context(self.commandline, option) |
| 185 |
|
| 186 |
if option.complete: |
| 187 |
completion_obj = self._complete(context, *option.complete) |
| 188 |
else: |
| 189 |
completion_obj = self._complete(context, 'none') |
| 190 |
|
| 191 |
definition = FishCompletionDefinition( |
| 192 |
self.ctxt, |
| 193 |
short_options = option.get_short_option_strings(), |
| 194 |
long_options = option.get_long_option_strings(), |
| 195 |
old_options = option.get_old_option_strings(), |
| 196 |
requires_argument = option.has_required_arg(), |
| 197 |
keep_order = option.nosort, |
| 198 |
description = option.help, |
| 199 |
completion_obj = completion_obj) |
| 200 |
|
| 201 |
not_has_options = [] |
| 202 |
|
| 203 |
final_options = self.commandline.get_final_option_strings() |
| 204 |
not_has_options.extend(final_options) |
| 205 |
|
| 206 |
conflicting_options = option.get_conflicting_option_strings() |
| 207 |
not_has_options.extend(conflicting_options) |
| 208 |
|
| 209 |
if not option.repeatable: |
| 210 |
not_has_options.extend(option.option_strings) |
| 211 |
|
| 212 |
if not_has_options: |
| 213 |
not_has_options = algo.uniq(not_has_options) |
| 214 |
definition.conditions.add(Not(HasOption(not_has_options))) |
| 215 |
|
| 216 |
if option.hidden: |
| 217 |
definition.conditions.add(HasHiddenOption(option.option_strings)) |
| 218 |
|
| 219 |
# If we don't inherit options, add a condition to the option that |
| 220 |
# ensures that we're in the right (sub)command. |
| 221 |
if not self.commandline.inherit_options and self.subcommands: |
| 222 |
positional = self.subcommands.get_positional_num() |
| 223 |
definition.conditions.add(PositionalNum('==', positional - 1)) |
| 224 |
|
| 225 |
definition.conditions.extend(_get_positional_contains(option)) |
| 226 |
definition.conditions.add_when(option.when_parsed) |
| 227 |
|
| 228 |
return definition |
| 229 |
|
| 230 |
def _complete_positional(self, positional): |
| 231 |
context = self.ctxt.get_option_context(self.commandline, positional) |
| 232 |
|
| 233 |
if positional.complete: |
| 234 |
completion_obj = self._complete(context, *positional.complete) |
| 235 |
else: |
| 236 |
completion_obj = self._complete(context, 'none') |
| 237 |
|
| 238 |
definition = FishCompletionDefinition( |
| 239 |
self.ctxt, |
| 240 |
positional = positional.get_positional_num(), |
| 241 |
requires_argument = True, |
| 242 |
keep_order = positional.nosort, |
| 243 |
description = positional.help, |
| 244 |
completion_obj = completion_obj |
| 245 |
) |
| 246 |
|
| 247 |
definition.conditions.add_when(positional.when_parsed) |
| 248 |
definition.conditions.extend(_get_positional_contains(positional)) |
| 249 |
|
| 250 |
positional_num = positional.get_positional_num() |
| 251 |
operator = '>=' if positional.repeatable else '==' |
| 252 |
definition.conditions.add(PositionalNum(operator, positional_num - 1)) |
| 253 |
|
| 254 |
return definition |
| 255 |
|
| 256 |
def _complete_subcommands(self): |
| 257 |
positional = self.subcommands |
| 258 |
items = positional.get_choices() |
| 259 |
context = self.ctxt.get_option_context(self.commandline, positional) |
| 260 |
completion_obj = self._complete(context, 'choices', items) |
| 261 |
|
| 262 |
definition = FishCompletionDefinition( |
| 263 |
self.ctxt, |
| 264 |
description = 'Commands', |
| 265 |
completion_obj = completion_obj |
| 266 |
) |
| 267 |
|
| 268 |
positional_num = positional.get_positional_num() |
| 269 |
definition.conditions.add(PositionalNum('==', positional_num - 1)) |
| 270 |
definition.conditions.extend(_get_positional_contains(positional)) |
| 271 |
return definition |
| 272 |
|
| 273 |
def get_command_comment(self): |
| 274 |
'''Return a comment for the current command.''' |
| 275 |
return '# command %s' % self.commandline.get_command_path() |
| 276 |
|
| 277 |
|
| 278 |
def generate_completion(commandline, config=None): |
| 279 |
'''Code for generating a Fish auto completion file.''' |
| 280 |
|
| 281 |
if config is None: |
| 282 |
config = config_.Config() |
| 283 |
|
| 284 |
commandline = generation.enhance_commandline(commandline, config) |
| 285 |
helpers = fish_helpers.FishHelpers(config, commandline.prog) |
| 286 |
ctxt = generation.GenerationContext(config, helpers) |
| 287 |
result = FishCompletionGenerator(ctxt, commandline) |
| 288 |
prepare = fish_prepare.get_prepare_function(commandline, ctxt) |
| 289 |
|
| 290 |
if helpers.is_used('query_init'): |
| 291 |
types = utils.get_defined_option_types(commandline) |
| 292 |
if types.short: |
| 293 |
ctxt.helpers.use_function('query_init', 'short_options') |
| 294 |
if types.long: |
| 295 |
ctxt.helpers.use_function('query_init', 'long_options') |
| 296 |
if types.old: |
| 297 |
ctxt.helpers.use_function('query_init', 'old_options') |
| 298 |
if types.subcommands: |
| 299 |
ctxt.helpers.use_function('query_init', 'subcommands') |
| 300 |
|
| 301 |
output = Output(config, helpers) |
| 302 |
output.add_generation_notice() |
| 303 |
output.add_comments() |
| 304 |
output.add_included_files() |
| 305 |
output.add_helper_functions_code() |
| 306 |
|
| 307 |
with output.add_as_block() as block: |
| 308 |
block.add("set -l prog '%s'" % commandline.prog) |
| 309 |
block.add("set -l P '%s'" % prepare) |
| 310 |
|
| 311 |
for func in ["positional_contains", "has_option", "option_is", |
| 312 |
"option_match", "num_of_positionals", |
| 313 |
"positional_position", "positional_match"]: |
| 314 |
if helpers.is_used(func): |
| 315 |
block.add(f"set -l {func} '{helpers.use_function(func)}'") |
| 316 |
|
| 317 |
block.add('') |
| 318 |
block.add('# Delete existing completions') |
| 319 |
block.add('complete -c $prog -e') |
| 320 |
block.add('') |
| 321 |
block.add('# Generally disable file completion') |
| 322 |
block.add('complete -c $prog -x') |
| 323 |
|
| 324 |
if result.commandline.wraps: |
| 325 |
with output.add_as_block() as block: |
| 326 |
block.add('# Wrap command') |
| 327 |
block.add('complete -c $prog -w %s' % result.commandline.wraps) |
| 328 |
|
| 329 |
for generator in result.get_all(): |
| 330 |
if generator.lines: |
| 331 |
with output.add_as_block() as block: |
| 332 |
block.add(generator.get_command_comment()) |
| 333 |
block.extend(generator.conditions.get_lines()) |
| 334 |
block.extend(generator.lines) |
| 335 |
|
| 336 |
if commandline.aliases: |
| 337 |
with output.add_as_block() as block: |
| 338 |
for alias in commandline.aliases: |
| 339 |
block.add('complete -c %s -e' % alias) |
| 340 |
block.add('complete -c %s -w %s' % (alias, commandline.prog)) |
| 341 |
|
| 342 |
output.add_vim_modeline('fish') |
| 343 |
|
| 344 |
return output.get() |