Skip to content

Commit 81d0206

Browse files
committed
fish: completions: kmake: Make directory and object suggestions smarter
Take into account the current token with how suggestions are offered, which makes them more helpful and faster. Signed-off-by: Nathan Chancellor <[email protected]>
1 parent 0b34d64 commit 81d0206

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

fish/completions/kmake.fish

+44-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function __kmake_get_srctree
22
# Check if '-C' / '--directory' was found on the command line
33
set tokens (commandline -xpc)
44
if set index (contains -i -- -C $tokens); or set index (contains -i -- --directory $tokens)
5-
echo $tokens[(math $index + 1)]
5+
path resolve $tokens[(math $index + 1)]
66
else
77
echo $PWD
88
end
@@ -66,6 +66,10 @@ end
6666

6767
function __kmake_pos_args
6868
set srctree (__kmake_get_srctree)
69+
# obviously not in a kernel tree, do not suggest anything
70+
if not test -e $srctree/Makefile
71+
return 0
72+
end
6973
set srcarch (__kmake_get_srcarch)
7074

7175
for token in (commandline -xpc)
@@ -167,20 +171,47 @@ function __kmake_pos_args
167171
$configs\t'config target' \
168172
$targets\t'build target'
169173

170-
# directory targets
171-
if test -e $srctree/Makefile
172-
if command -q fd
173-
fd -g Makefile (path filter -d $srctree/*) &| string replace $srctree/ ''
174-
else
175-
find $srctree -mindepth 2 -name Makefile -printf '%P\n'
176-
end &| path dirname &| string match -evr '^(Documentation|scripts|tools)' &| string match -er "^(?:arch/$srcarch|(?!arch))" &| path sort --key basename &| path sort --key dirname &| string join /\t'directory target'\n
174+
# We do not care about potential targets in:
175+
# Documentation
176+
# scripts
177+
# tools
178+
# and we only care about targets for the current srcarch.
179+
set top_level_dirs (path filter -f $srctree/*/Makefile | path dirname | path basename | string match -erv "^(?:Documentation|scripts|tools)") arch/$srcarch
180+
181+
# First, see if we have started a token already
182+
set token (commandline -ct)
183+
if test -z "$token"
184+
# If not, just show the available top level directories, as there
185+
# is not much else that makes sense to suggest at this point.
186+
string join \n -- $top_level_dirs/\t'directory target'
187+
else
188+
# If we have started a token, check to see if it contains a slash
189+
# yet (implicitly through the return code of 'string split') and
190+
# that the first part is a part of the valid top level directories,
191+
# as we may be completing a configuration value.
192+
if set base_top_level_dir (string split -f 1 -- / $token); and contains $base_top_level_dir $top_level_dirs arch
193+
# If it does, only show the next level of directory options. More
194+
# could produce too much output for little gain in selections.
195+
set token_limit (math (string split -- / $token | count) + 1)
196+
197+
# fd to quickly find Makefiles in directories that could be reched
198+
# with the current selection. By sorting by dirname, we can bail
199+
# out of the for loop as soon as we encounter an item longer than
200+
# our limit.
201+
fd -g Makefile (path filter -d $srctree/$token*) | string replace $srctree/ '' | path dirname | path sort --key dirname | while read -l possible_folder
202+
if test (string split -- / $possible_folder | count) -gt $token_limit
203+
break
204+
end
205+
echo $possible_folder/\t'directory target'
206+
end
177207

178-
# object targets
179-
if command -q fd
180-
fd -e c . (path filter -d $srctree/*) &| string replace $srctree/ ''
208+
# Show any possible .o targets (typically from .c and .S files)
209+
# that could be built with the current selection
210+
string join \n -- (path filter -f $srctree/$token*.{c,S} | string replace $srctree/ '' | path change-extension .o)\t'object target'
181211
else
182-
find $srctree -mindepth 2 -name '*.c' -printf '%P\n'
183-
end &| string match -evr '^(Documentation|scripts|tools)' &| string match -er "^(?:arch/$srcarch|(?!arch))" &| path sort --key basename &| path sort --key dirname &| path change-extension .o &| string join \t'object target'\n
212+
# Filter top level directories to only ones that can be matched with current selection
213+
string join \n -- (string match -er -- "^$token" $top_level_dirs)/\t'directory target'
214+
end
184215
end
185216

186217
string join \n -- \

0 commit comments

Comments
 (0)