Skip to content
Myr

Fix: Ensure fish completion handles multiline help strings correctly (#3126)

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

authored by ReneS · committed by GitHub · parent c69643b · Browse files · 4 files+71 added−8 removed
This commit

State

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

Diff stats

4 files+71 added−8 removed

  • tests:+50 added−3 removed
  • other:+21 added−5 removed

4 files+71 added−8 removed

modified:src/click/shell_completion.py+18 added-5 removed
178 unmodified lines
179 set -l response (env %(complete_var)s=fish_complete COMP_WORDS=(commandline -cp) \180COMP_CWORD=(commandline -t) %(prog_name)s);181182 for completion in $response;183 set -l metadata (string split "," $completion);183 set -l metadata (string split \n $completion);184185 if test $metadata[1] = "dir";186 __fish_complete_directories $metadata[2];187 else if test $metadata[1] = "file";188 __fish_complete_path $metadata[2];189 else if test $metadata[1] = "plain";190 if test $metadata[3] != "_";191 echo $metadata[2]\t$metadata[3];192 else;190 echo $metadata[2];193 echo $metadata[2];194 end;195 end;196 end;197end;198
221 unmodified lines
420421 return args, incomplete422423 def format_completion(self, item: CompletionItem) -> str:420 if item.help:421 return f"{item.type},{item.value}\t{item.help}"424 """Format completion item for Fish shell.425423 return f"{item.type},{item.value}"426 Escapes newlines in both value and help text to prevent427 Fish shell parsing errors.428429 .. versionchanged:: 8.3430 Escape newlines in help text to fix completion errors431 with multi-line help strings.432 """433 help_ = item.help or "_"434 value = item.value.replace("\n", r"\n")435 help_escaped = help_.replace("\n", r"\n")436 return f"{item.type}\n{value}\n{help_escaped}"437438439ShellCompleteType = t.TypeVar("ShellCompleteType", bound="type[ShellComplete]")440441442_available_shells: dict[str, type[ShellComplete]] = {
236 unmodified lines
modified:tests/test_shell_completion.py+50 added-3 removed
355 unmodified lines
356 ("bash", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain,a\nplain,b\n"),357 ("bash", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain,b\n"),358 ("zsh", {"COMP_WORDS": "", "COMP_CWORD": "0"}, "plain\na\n_\nplain\nb\nbee\n"),359 ("zsh", {"COMP_WORDS": "a b", "COMP_CWORD": "1"}, "plain\nb\nbee\n"),360 ("fish", {"COMP_WORDS": "", "COMP_CWORD": ""}, "plain,a\nplain,b\tbee\n"),361 ("fish", {"COMP_WORDS": "a b", "COMP_CWORD": "b"}, "plain,b\tbee\n"),362 ("fish", {"COMP_WORDS": 'a "b', "COMP_CWORD": '"b'}, "plain,b\tbee\n"),360 ("fish", {"COMP_WORDS": "", "COMP_CWORD": ""}, "plain\na\n_\nplain\nb\nbee\n"),361 ("fish", {"COMP_WORDS": "a b", "COMP_CWORD": "b"}, "plain\nb\nbee\n"),362 ("fish", {"COMP_WORDS": 'a "b', "COMP_CWORD": '"b'}, "plain\nb\nbee\n"),363 ],364)365@pytest.mark.usefixtures("_patch_for_completion")366def test_full_complete(runner, shell, env, expect):
191 unmodified lines
558 with warnings.catch_warnings(record=True) as current_warnings:559 assert not current_warnings, "There should be no warnings to start"560 _get_completions(cli, args=[], incomplete="")561 assert not current_warnings, "There should be no warnings after either"562563564@pytest.mark.usefixtures("_patch_for_completion")565def test_fish_multiline_help_complete(runner):566 """Test Fish completion with multi-line help text doesn't cause errors."""567 cli = Command(568 "cli",569 params=[570 Option(571 ["--at", "--attachment-type"],572 type=(str, str),573 multiple=True,574 help=(575 "\b\nAttachment with explicit mimetype,\n--at image.jpg image/jpeg"576 ),577 ),578 Option(["--other"], help="Normal help"),579 ],580 )581582 result = runner.invoke(583 cli,584 env={585 "COMP_WORDS": "cli --",586 "COMP_CWORD": "--",587 "_CLI_COMPLETE": "fish_complete",588 },589 )590591 # Should not fail592 assert result.exit_code == 0593594 # Output should contain escaped newlines, not literal newlines595 # Fish expects: plain\n--at\n{help_with_\\n}596 lines = result.output.split("\n")597598 # Find the --at completion block (3 lines: type, value, help)599 for i in range(0, len(lines) - 2, 3):600 if lines[i] == "plain" and lines[i + 1] in ("--at", "--attachment-type"):601 help_line = lines[i + 2]602 # Help should have escaped newlines (\\n), not actual newlines603 assert "\\n" in help_line604 # Should contain the example text605 assert "image.jpg" in help_line.replace("\\n", " ")606 break607 else:608 pytest.fail("--at completion not found in output")
modified:.gitignore+1 added
4 unmodified lines
5.coverage*6htmlcov/7.tox/8docs/_build/9.venv
modified:CHANGES.rst+2 added
43 unmodified lines
44 :pr:`2969`45- Use :func:`os.startfile` on Windows to open URLs in :func:`open_url`,46 replacing the ``start`` built-in which cannot be invoked without47 ``shell=True``. :issue:`3164` :pr:`3186`48- Fix Fish shell completion errors when option help text contains newlines.49 :issue:`3043`5051Version 8.3.352-------------53
1366 unmodified lines