Skip to content

Commit 147dc0b

Browse files
authored
Fix fish completion when commandline contains multiple commands (#9727)
This changes some of the options passed to "complete", see also https://fishshell.com/docs/current/cmds/complete.html The flag --current-process means to only include tokens of the current command, as delimited by shell metacharacters like ;, & and |, and newlines. This fixes completion of a command line like "python && pip <TAB>". Previously we'd run "eval python" (!) Also, avoid using "eval" if possible since it's usually not necessary in fish 3.0.0 and later. Flag --cut-at-cursor means to only include tokens left of the cursor. It's extremely unusual in fish that completions use anything right of the cursor to complete. I didn't check pip sources but I doubt it's an exception. I also used --cut-at-cursor for the current token because it potentially offers more completions, and fish will filter them anyway. for example, now it's possible to get completions on a commandline like pip uninstall urllb3 ^ cursor is here, so "commmandline -tc" is "url" This correctly completes to "urllib3", because fish uses fuzzy matching ;)
1 parent 5672421 commit 147dc0b

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

news/9727.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix fish shell completion when commandline contains multiple commands.

src/pip/_internal/commands/completion.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@
3838
""",
3939
"fish": """
4040
function __fish_complete_pip
41-
set -lx COMP_WORDS (commandline -o) ""
42-
set -lx COMP_CWORD ( \\
43-
math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\
44-
)
41+
set -lx COMP_WORDS \\
42+
(commandline --current-process --tokenize --cut-at-cursor) \\
43+
(commandline --current-token --cut-at-cursor)
44+
set -lx COMP_CWORD (math (count $COMP_WORDS) - 1)
4545
set -lx PIP_AUTO_COMPLETE 1
46-
string split \\ -- (eval $COMP_WORDS[1])
46+
set -l completions
47+
if string match -q '2.*' $version
48+
set completions (eval $COMP_WORDS[1])
49+
else
50+
set completions ($COMP_WORDS[1])
51+
end
52+
string split \\ -- $completions
4753
end
4854
complete -fa "(__fish_complete_pip)" -c {prog}
4955
""",

tests/functional/test_completion.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@
2323
"fish",
2424
"""\
2525
function __fish_complete_pip
26-
set -lx COMP_WORDS (commandline -o) ""
27-
set -lx COMP_CWORD ( \\
28-
math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\
29-
)
26+
set -lx COMP_WORDS \\
27+
(commandline --current-process --tokenize --cut-at-cursor) \\
28+
(commandline --current-token --cut-at-cursor)
29+
set -lx COMP_CWORD (math (count $COMP_WORDS) - 1)
3030
set -lx PIP_AUTO_COMPLETE 1
31-
string split \\ -- (eval $COMP_WORDS[1])
31+
set -l completions
32+
if string match -q '2.*' $version
33+
set completions (eval $COMP_WORDS[1])
34+
else
35+
set completions ($COMP_WORDS[1])
36+
end
37+
string split \\ -- $completions
3238
end
3339
complete -fa "(__fish_complete_pip)" -c pip""",
3440
),

0 commit comments

Comments
 (0)