__fish_complete_suffix: enable fuzzy completion, simplify

fish's internal completion logic is much smarter than the globbing in this
function, so let's just reuse "complete -C", and filter directories and
files with the given suffix.

Thanks to @Kratacoa for reporting on Gitter.

Using "complete -C" works well no prefix is given. Since in this repository
only the openocd completions pass a prefix, I left the prefix-case as is.
It could probably be improved and simplified as well.  The prefix argument was
introduced to avoid cd's side effects inside a completion. Using cd is tempting
though because it would allow to use the same logic as without a prefix.
This commit is contained in:
Johannes Altmanninger 2020-08-08 22:36:30 +02:00
parent 0dd334ee46
commit 93cb0e2abb

View file

@ -48,6 +48,13 @@ function __fish_complete_suffix -d "Complete using files"
end end
end end
# Simple and common case: no prefix, just complete normally and filter out unwanted suffixes.
if test -z $prefix
set -l suffix (string escape --style=regex -- $suff)
# Use normal file completions. Any valid command works here as, as long as it has no
# user-defined completions. The builtin ":" should work.
set files (complete -C ": $comp" | string match -r "^.*(?:$suffix|/)\$")
else
# Strip leading ./ as it confuses the detection of base and suffix # Strip leading ./ as it confuses the detection of base and suffix
# It is conditionally re-added below. # It is conditionally re-added below.
set base $prefix(string replace -r '^("\')?\\./' '' -- $comp | string trim -c '\'"') # " make emacs syntax highlighting happy set base $prefix(string replace -r '^("\')?\\./' '' -- $comp | string trim -c '\'"') # " make emacs syntax highlighting happy
@ -83,6 +90,7 @@ function __fish_complete_suffix -d "Complete using files"
# "Escape" files starting with a literal dash `-` with a `./` # "Escape" files starting with a literal dash `-` with a `./`
set files (string replace -r -- "^-" "./-" $files) set files (string replace -r -- "^-" "./-" $files)
end end
end
if set -q files[1] if set -q files[1]
if string match -qr -- . "$desc" if string match -qr -- . "$desc"