Skip to content
Myr

Fix broken fish completion and multiline help string

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

Fix regression introduced in 8.4.0 by #3126 and properly fix #3043 and #3502

authored by Sitiyou · committed by Kevin Deldycke · parent 5b1aba8 · Browse files · 2 files+14 added−65 removed
This commit

State

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

Diff stats

2 files+14 added−65 removed

  • tests:+3 added−50 removed
  • other:+11 added−15 removed

2 files+14 added−65 removed

modified:src/click/shell_completion.py+11 added-15 removed
179 unmodified lines
180 set -l response (env %(complete_var)s=fish_complete COMP_WORDS=(commandline -cp) \181COMP_CWORD=(commandline -t) %(prog_name)s);182183 for completion in $response;184 set -l metadata (string split \n $completion);184 set -l metadata (string split "," $completion);185186 if test $metadata[1] = "dir";187 __fish_complete_directories $metadata[2];188 else if test $metadata[1] = "file";189 __fish_complete_path $metadata[2];190 else if test $metadata[1] = "plain";191 if test $metadata[3] != "_";192 echo $metadata[2]\t$metadata[3];193 else;194 echo $metadata[2];191 echo $metadata[2];195 end;192 end;193 end;194end;195
226 unmodified lines
422 .. versionchanged:: 8.4.0423 Escape newlines in value and help to fix completion errors with424 multi-line help strings.425 """430 # The fish completion script splits each response line on literal431 # newlines, so any newline in the value or help would corrupt the432 # frame. Replace them with the two-character escape "\n" so the text433 # round-trips through fish without breaking the format. The "_"434 # sentinel for missing help mirrors :class:`ZshComplete`.435 help_ = item.help or "_"436 value = item.value.replace("\n", r"\n")437 help_escaped = help_.replace("\n", r"\n")438 return f"{item.type}\n{value}\n{help_escaped}"426 # According to https://fishshell.com/docs/current/cmds/complete.html427 # Command substitutions found in ARGUMENTS should return a newline-428 # separated list of arguments, and each argument may optionally have a tab429 # character followed by the argument description.430 if item.help:431 help_ = item.help.replace("\n", "\\n").replace("\t", " ")432 return f"{item.type},{item.value}\t{help_}"433434 return f"{item.type},{item.value}"435436437ShellCompleteType = t.TypeVar("ShellCompleteType", bound="type[ShellComplete]")438439440_available_shells: dict[str, type[ShellComplete]] = {
236 unmodified lines
modified:tests/test_shell_completion.py+3 added-50 removed
357 unmodified lines
358 ("bash", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain,a\nplain,b\n"),359 ("bash", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain,b\n"),360 ("zsh", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain\na\n_\nplain\nb\nbee\n"),361 ("zsh", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain\nb\nbee\n"),362 ("fish", {"COMP_WORDS": "", "COMP_CWORD": ""}, "plain\na\n_\nplain\nb\nbee\n"),363 ("fish", {"COMP_WORDS": "a b", "COMP_CWORD": "b"}, "plain\nb\nbee\n"),364 ("fish", {"COMP_WORDS": 'a "b', "COMP_CWORD": '"b'}, "plain\nb\nbee\n"),362 ("fish", {"COMP_WORDS": "", "COMP_CWORD": ""}, "plain,a\nplain,b\tbee\n"),363 ("fish", {"COMP_WORDS": "a b", "COMP_CWORD": "b"}, "plain,b\tbee\n"),364 ("fish", {"COMP_WORDS": 'a "b', "COMP_CWORD": '"b'}, "plain,b\tbee\n"),365 ],366)367@pytest.mark.usefixtures("_patch_for_completion")368def test_full_complete(runner, shell, env, expect):
206 unmodified lines
575 with warnings.catch_warnings(record=True) as current_warnings:576 assert not current_warnings, "There should be no warnings to start"577 _get_completions(cli, args=[], incomplete="")578 assert not current_warnings, "There should be no warnings after either"579580581@pytest.mark.usefixtures("_patch_for_completion")582def test_fish_multiline_help_complete(runner):583 """Test Fish completion with multi-line help text doesn't cause errors."""584 cli = Command(585 "cli",586 params=[587 Option(588 ["--at", "--attachment-type"],589 type=(str, str),590 multiple=True,591 help=(592 "\b\nAttachment with explicit mimetype,\n--at image.jpg image/jpeg"593 ),594 ),595 Option(["--other"], help="Normal help"),596 ],597 )598599 result = runner.invoke(600 cli,601 env={602 "COMP_WORDS": "cli --",603 "COMP_CWORD": "--",604 "_CLI_COMPLETE": "fish_complete",605 },606 )607608 # Should not fail609 assert result.exit_code == 0610611 # Output should contain escaped newlines, not literal newlines612 # Fish expects: plain\n--at\n{help_with_\\n}613 lines = result.output.split("\n")614615 # Find the --at completion block (3 lines: type, value, help)616 for i in range(0, len(lines) - 2, 3):617 if lines[i] == "plain" and lines[i + 1] in ("--at", "--attachment-type"):618 help_line = lines[i + 2]619 # Help should have escaped newlines (\\n), not actual newlines620 assert "\\n" in help_line621 # Should contain the example text622 assert "image.jpg" in help_line.replace("\\n", " ")623 break624 else:625 pytest.fail("--at completion not found in output")