@@ -2,7 +2,7 @@ function __kmake_get_srctree
2
2
# Check if '-C' / '--directory' was found on the command line
3
3
set tokens (commandline -xpc )
4
4
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)]
6
6
else
7
7
echo $PWD
8
8
end
66
66
67
67
function __kmake_pos_args
68
68
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
69
73
set srcarch (__kmake_get_srcarch)
70
74
71
75
for token in (commandline -xpc )
@@ -167,20 +171,47 @@ function __kmake_pos_args
167
171
$configs \t ' config target' \
168
172
$targets \t ' build target'
169
173
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
177
207
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 '
181
211
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
184
215
end
185
216
186
217
string join \n -- \
0 commit comments