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
Provenance
- Commit
a1235aa- Tree
202f159- Author
Marco Ricci <the-13th-letter@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
565f36d
3 files changed+98 added−1 removed
modified:src/click/shell_completion.py+21 added-1 removed
122 unmodified lines123 124 %(complete_func)s_setup;125 """126 127+# 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)s135 136 %(complete_func)s() {241 unmodified lines378 379 return args, incomplete380 381 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_}"397 398 399 class FishComplete(ShellComplete):400 """Shell completion for Fish."""264 unmodified linesmodified:tests/test_shell_completion.py+75 added
1+import textwrap2 import warnings3+from collections.abc import Mapping4 5 import pytest6 7 import click.shell_completion347 unmodified lines355 result = runner.invoke(cli, env=env)356 assert result.output == expect357 358 359+@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")410+def 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 == expect430+431+432+@pytest.mark.usefixtures("_patch_for_completion")433 def test_context_settings(runner):434 def complete(ctx, param, incomplete):435 return ctx.obj["choices"]436 111 unmodified linesmodified:CHANGES.rst+2 added
6 unmodified lines7 8 - 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`13 14 Version 8.2.115 -------------16 1242 unmodified lines