Skip to content
Myr

Fix Zsh completions with colons (#2846)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Resolves #2846.

authored by Marco Ricci · committed by GitHub · parent 565f36d · Browse files · 3 files+98 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Andreas Backx <andreas@backx.org>

Diff stats

3 files+98 added−1 removed

  • tests:+75 added−0 removed
  • other:+23 added−1 removed

3 files+98 added−1 removed

modified:src/click/shell_completion.py+21 added-1 removed
122 unmodified lines
123124%(complete_func)s_setup;125"""126127# See ZshComplete.format_completion below, and issue #2703, before128# changing this script.129#130# (TL;DR: _describe is picky about the format, but this Zsh script snippet131# is already widely deployed. So freeze this script, and use clever-ish132# handling of colons in ZshComplet.format_completion.)133_SOURCE_ZSH = """\134#compdef %(prog_name)s135136%(complete_func)s() {
241 unmodified lines
378379 return args, incomplete380381 def format_completion(self, item: CompletionItem) -> str:376 return f"{item.type}\n{item.value}\n{item.help if item.help else '_'}"382 help_ = item.help or "_"383 # The zsh completion script uses `_describe` on items with help384 # texts (which splits the item help from the item value at the385 # first unescaped colon) and `compadd` on items without help386 # text (which uses the item value as-is and does not support387 # colon escaping). So escape colons in the item value if and388 # only if the item help is not the sentinel "_" value, as used389 # by the completion script.390 #391 # (The zsh completion script is potentially widely deployed, and392 # thus harder to fix than this method.)393 #394 # See issue #1812 and issue #2703 for further context.395 value = item.value.replace(":", r"\:") if help_ != "_" else item.value396 return f"{item.type}\n{value}\n{help_}"397398399class FishComplete(ShellComplete):400 """Shell completion for Fish."""
264 unmodified lines
modified:tests/test_shell_completion.py+75 added
1import textwrap2import warnings3from collections.abc import Mapping45import pytest67import click.shell_completion
347 unmodified lines
355 result = runner.invoke(cli, env=env)356 assert result.output == expect357358359@pytest.mark.parametrize(360 ("env", "expect"),361 [362 (363 {"COMP_WORDS": "", "COMP_CWORD": "0"},364 textwrap.dedent(365 """\366 plain367 a368 _369 plain370 b371 bee372 plain373 c\\:d374 cee:dee375 plain376 c:e377 _378 """379 ),380 ),381 (382 {"COMP_WORDS": "a c", "COMP_CWORD": "1"},383 textwrap.dedent(384 """\385 plain386 c\\:d387 cee:dee388 plain389 c:e390 _391 """392 ),393 ),394 (395 {"COMP_WORDS": "a c:", "COMP_CWORD": "1"},396 textwrap.dedent(397 """\398 plain399 c\\:d400 cee:dee401 plain402 c:e403 _404 """405 ),406 ),407 ],408)409@pytest.mark.usefixtures("_patch_for_completion")410def test_zsh_full_complete_with_colons(411 runner, env: Mapping[str, str], expect: str412) -> None:413 cli = Group(414 "cli",415 commands=[416 Command("a"),417 Command("b", help="bee"),418 Command("c:d", help="cee:dee"),419 Command("c:e"),420 ],421 )422 result = runner.invoke(423 cli,424 env={425 **env,426 "_CLI_COMPLETE": "zsh_complete",427 },428 )429 assert result.output == expect430431432@pytest.mark.usefixtures("_patch_for_completion")433def test_context_settings(runner):434 def complete(ctx, param, incomplete):435 return ctx.obj["choices"]436
111 unmodified lines
modified:CHANGES.rst+2 added
6 unmodified lines
78- Fix reconciliation of `default`, `flag_value` and `type` parameters for9 flag options, as well as parsing and normalization of environment variables.10 :issue:`2952` :pr:`2956`11- Fix completion for the Z shell (``zsh``) for completion items containing12 colons. :issue:`2703` :pr:`2846`1314Version 8.2.115-------------16
1242 unmodified lines