fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Tiago de Paula__fish_complete_suffix: Remove –foo= from tokenb980e50ac

master
4.0 KiB87 linesraw
1# localization: skip(private)
2#
3# Find files ending in any of the non-switch arguments and output them as completions.
4# * --description provides the description that should be part of each generated completion,
5# * --prefix=DIR makes __fish_complete_suffix behave as if it were called in DIR rather than $PWD
6# * --complete=PREFIX only lists files that begin with PREFIX
7#
8# Files matching the above preconditions are printed first then other, non-matching files
9# are printed for fallback purposes. As such, it is imperative that `complete` calls that
10# shell out to `__fish_complete_suffix` are made with a `-k` switch to ensure sort order
11# is preserved.
12function __fish_complete_suffix -d "Complete using files"
13    set -l _flag_prefix ""
14    # We need to remove '--name=' from the completion. See __fish_complete_directories
15    set -l _flag_complete (commandline -ct | string replace -r -- '^-[^=]*=' '')
16
17    argparse 'prefix=' 'description=' 'complete=' -- $argv
18
19    set -l suff (string escape --style=regex -- $argv)
20    set -l files
21
22    # Simple and common case: no prefix, just complete normally and sort matching files first.
23    if test -z $_flag_prefix
24        # Use normal file completions.
25        set files (complete -C "__fish_command_without_completions $_flag_complete")
26        set -l files_with_suffix (string match -r -- (string join "|" "^.*"$suff\$) $files)
27        set -l directories (string match -r -- '^.*/$' $files)
28        set files $files_with_suffix $directories $files
29    else
30        # Only directories are supported as prefixes, and to use the same logic
31        # for both absolute prefixed paths and relative non-prefixed paths, $_flag_prefix
32        # must terminate in a `/` if it is present, so it can be unconditionally
33        # prefixed to any path to get the desired result.
34        if not string match -qr '/$' $_flag_prefix
35            set _flag_prefix $_flag_prefix/
36        end
37
38        # Strip leading ./ as it confuses the detection of base and suffix
39        # It is conditionally re-added below.
40        set -l base $_flag_prefix(string replace -r '^("\')?\\./' '' -- $_flag_complete | string trim -c '\'"') # " make emacs syntax highlighting happy
41
42        set -l all
43        set -l files_with_suffix
44        set -l dirs
45        # If $_flag_complete is "./ma" and the file is "main.py", we'll catch that case here,
46        # but complete.cpp will not consider it a match, so we have to output the
47        # correct form.
48
49        # Also do directory completion, since there might be files with the correct
50        # suffix in a subdirectory.
51        set all $base*
52        set files_with_suffix (string match -r -- (string join "|" ".*"$suff) $all)
53        if not string match -qr '/$' -- $argv
54            set dirs $base*/
55
56            # The problem is that we now have each directory included twice in the output,
57            # once as `dir` and once as `dir/`. The runtime here is O(n) for n directories
58            # in the output, but hopefully since we have only one level (no nested results)
59            # it should be fast. The alternative is to shell out to `sort` and remove any
60            # duplicate results, but it would have to be a huge `n` to make up for the fork
61            # overhead.
62            for dir in $dirs
63                set all (string match -v (string match -r '(.*)/$' -- $dir)[2] -- $all)
64            end
65        end
66
67        set files $files_with_suffix $dirs $all
68        if string match -qr '^\\./' -- $_flag_complete
69            set files ./$files
70        else
71            # "Escape" files starting with a literal dash `-` with a `./`
72            set files (string replace -r -- "^-" "./-" $files)
73        end
74    end
75
76    if set -q files[1]
77        if string match -qr -- . "$_flag_description"
78            set _flag_description "\t$_flag_description"
79        end
80        if string match -qr -- . "$_flag_prefix"
81            set -l prefix (string escape --style=regex -- $_flag_prefix)
82            set files (string replace -r -- "^$prefix" "" $files)
83        end
84        printf "%s$_flag_description\n" $files
85    end
86
87end