Line | Branch | Exec | Source |
---|---|---|---|
1 | #include <fcntl.h> | ||
2 | #include <stdbool.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <string.h> | ||
5 | #include <sys/stat.h> | ||
6 | #include <unistd.h> | ||
7 | #include "completion.h" | ||
8 | #include "bind.h" | ||
9 | #include "command/alias.h" | ||
10 | #include "command/args.h" | ||
11 | #include "command/parse.h" | ||
12 | #include "command/run.h" | ||
13 | #include "command/serialize.h" | ||
14 | #include "commands.h" | ||
15 | #include "compiler.h" | ||
16 | #include "config.h" | ||
17 | #include "exec.h" | ||
18 | #include "filetype.h" | ||
19 | #include "mode.h" | ||
20 | #include "options.h" | ||
21 | #include "show.h" | ||
22 | #include "syntax/color.h" | ||
23 | #include "tag.h" | ||
24 | #include "terminal/cursor.h" | ||
25 | #include "terminal/key.h" | ||
26 | #include "terminal/style.h" | ||
27 | #include "util/arith.h" | ||
28 | #include "util/array.h" | ||
29 | #include "util/ascii.h" | ||
30 | #include "util/bit.h" | ||
31 | #include "util/bsearch.h" | ||
32 | #include "util/debug.h" | ||
33 | #include "util/intmap.h" | ||
34 | #include "util/log.h" | ||
35 | #include "util/numtostr.h" | ||
36 | #include "util/path.h" | ||
37 | #include "util/str-array.h" | ||
38 | #include "util/str-util.h" | ||
39 | #include "util/string-view.h" | ||
40 | #include "util/string.h" | ||
41 | #include "util/xdirent.h" | ||
42 | #include "util/xmalloc.h" | ||
43 | #include "vars.h" | ||
44 | |||
45 | // NOLINTNEXTLINE(*-avoid-non-const-global-variables) | ||
46 | extern char **environ; | ||
47 | |||
48 | typedef enum { | ||
49 | COLLECT_ALL, // (directories and files) | ||
50 | COLLECT_EXECUTABLES, // (directories and executable files) | ||
51 | COLLECT_DIRS_ONLY, | ||
52 | } FileCollectionType; | ||
53 | |||
54 | ✗ | static bool is_executable(int dir_fd, const char *filename) | |
55 | { | ||
56 | ✗ | return faccessat(dir_fd, filename, X_OK, 0) == 0; | |
57 | } | ||
58 | |||
59 | 10 | static bool do_collect_files ( | |
60 | PointerArray *array, | ||
61 | const char *dirname, | ||
62 | const char *dirprefix, | ||
63 | const char *fileprefix, | ||
64 | FileCollectionType type | ||
65 | ) { | ||
66 | 10 | DIR *const dir = xopendir(dirname); | |
67 |
1/2✓ Branch 0 (3→4) taken 10 times.
✗ Branch 1 (3→45) not taken.
|
10 | if (!dir) { |
68 | return false; | ||
69 | } | ||
70 | |||
71 | 10 | const int dir_fd = dirfd(dir); | |
72 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→10) taken 10 times.
|
10 | if (unlikely(dir_fd < 0)) { |
73 | ✗ | LOG_ERRNO("dirfd"); | |
74 | ✗ | xclosedir(dir); | |
75 | ✗ | return false; | |
76 | } | ||
77 | |||
78 | 10 | size_t dlen = strlen(dirprefix); | |
79 | 10 | size_t flen = strlen(fileprefix); | |
80 | 10 | const struct dirent *de; | |
81 | |||
82 |
2/2✓ Branch 0 (42→11) taken 299 times.
✓ Branch 1 (42→43) taken 10 times.
|
309 | while ((de = xreaddir(dir))) { |
83 | 299 | const char *name = de->d_name; | |
84 |
5/6✓ Branch 0 (11→12) taken 289 times.
✓ Branch 1 (11→14) taken 10 times.
✓ Branch 2 (12→13) taken 279 times.
✓ Branch 3 (12→14) taken 10 times.
✗ Branch 4 (13→14) not taken.
✓ Branch 5 (13→15) taken 279 times.
|
299 | if (streq(name, ".") || streq(name, "..") || unlikely(streq(name, ""))) { |
85 | 247 | continue; | |
86 | } | ||
87 | |||
88 | // TODO: add a global option to allow dotfiles to be included | ||
89 | // even when there's no prefix | ||
90 |
4/4✓ Branch 0 (15→16) taken 229 times.
✓ Branch 1 (15→17) taken 50 times.
✓ Branch 2 (18→19) taken 227 times.
✓ Branch 3 (18→20) taken 52 times.
|
279 | if (flen ? strncmp(name, fileprefix, flen) != 0 : name[0] == '.') { |
91 | 227 | continue; | |
92 | } | ||
93 | |||
94 | 52 | struct stat st; | |
95 |
1/2✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 52 times.
|
52 | if (fstatat(dir_fd, name, &st, AT_SYMLINK_NOFOLLOW)) { |
96 | ✗ | continue; | |
97 | } | ||
98 | |||
99 | 52 | bool is_dir = S_ISDIR(st.st_mode); | |
100 |
1/2✗ Branch 0 (23→24) not taken.
✓ Branch 1 (23→27) taken 52 times.
|
52 | if (S_ISLNK(st.st_mode)) { |
101 | ✗ | if (!fstatat(dir_fd, name, &st, 0)) { | |
102 | ✗ | is_dir = S_ISDIR(st.st_mode); | |
103 | } | ||
104 | } | ||
105 | |||
106 |
2/2✓ Branch 0 (27→28) taken 51 times.
✓ Branch 1 (27→36) taken 1 times.
|
52 | if (!is_dir) { |
107 |
1/4✗ Branch 0 (28→29) not taken.
✗ Branch 1 (28→30) not taken.
✗ Branch 2 (28→35) not taken.
✓ Branch 3 (28→36) taken 51 times.
|
51 | switch (type) { |
108 | ✗ | case COLLECT_DIRS_ONLY: | |
109 | ✗ | continue; | |
110 | case COLLECT_ALL: | ||
111 | break; | ||
112 | ✗ | case COLLECT_EXECUTABLES: | |
113 | ✗ | if (!is_executable(dir_fd, name)) { | |
114 | ✗ | continue; | |
115 | } | ||
116 | ✗ | if (!dlen) { | |
117 | ✗ | dirprefix = "./"; | |
118 | ✗ | dlen = 2; | |
119 | } | ||
120 | break; | ||
121 | ✗ | default: | |
122 | − | BUG("unhandled FileCollectionType value"); | |
123 | } | ||
124 | } | ||
125 | |||
126 | 52 | ptr_array_append(array, path_joinx(dirprefix, name, is_dir)); | |
127 | } | ||
128 | |||
129 | 10 | xclosedir(dir); | |
130 | 10 | return true; | |
131 | } | ||
132 | |||
133 | 10 | static void collect_files(EditorState *e, CompletionState *cs, FileCollectionType type) | |
134 | { | ||
135 | 10 | StringView esc = cs->escaped; | |
136 |
2/2✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→10) taken 8 times.
|
10 | if (strview_has_prefix(&esc, "~/")) { |
137 | 2 | CommandRunner runner = normal_mode_cmdrunner(e); | |
138 | 2 | runner.expand_tilde_slash = false; | |
139 | 2 | char *str = parse_command_arg(&runner, esc.data, esc.length); | |
140 | 2 | const char *slash = xstrrchr(str, '/'); | |
141 | 2 | cs->tilde_expanded = true; | |
142 | 2 | char *dir = path_dirname(cs->parsed); | |
143 | 2 | char *dirprefix = path_dirname(str); | |
144 | 2 | do_collect_files(&cs->completions, dir, dirprefix, slash + 1, type); | |
145 | 2 | free(dirprefix); | |
146 | 2 | free(dir); | |
147 | 2 | free(str); | |
148 | } else { | ||
149 | 8 | const char *slash = strrchr(cs->parsed, '/'); | |
150 |
2/2✓ Branch 0 (10→11) taken 5 times.
✓ Branch 1 (10→12) taken 3 times.
|
8 | if (!slash) { |
151 | 5 | do_collect_files(&cs->completions, ".", "", cs->parsed, type); | |
152 | } else { | ||
153 | 3 | char *dir = path_dirname(cs->parsed); | |
154 | 3 | do_collect_files(&cs->completions, dir, dir, slash + 1, type); | |
155 | 3 | free(dir); | |
156 | } | ||
157 | } | ||
158 | |||
159 |
2/2✓ Branch 0 (15→16) taken 4 times.
✓ Branch 1 (15→18) taken 6 times.
|
10 | if (cs->completions.count == 1) { |
160 | // Add space if completed string is not a directory | ||
161 | 4 | const char *s = cs->completions.ptrs[0]; | |
162 | 4 | size_t len = strlen(s); | |
163 |
1/2✓ Branch 0 (16→17) taken 4 times.
✗ Branch 1 (16→18) not taken.
|
4 | if (len > 0) { |
164 | 4 | cs->add_space_after_single_match = s[len - 1] != '/'; | |
165 | } | ||
166 | } | ||
167 | 10 | } | |
168 | |||
169 | 5 | void collect_normal_aliases(EditorState *e, PointerArray *a, const char *prefix) | |
170 | { | ||
171 | 5 | collect_hashmap_keys(&e->aliases, a, prefix); | |
172 | 5 | } | |
173 | |||
174 | 5 | static void collect_bound_keys(const IntMap *bindings, PointerArray *a, const char *prefix) | |
175 | { | ||
176 | 5 | size_t prefix_len = strlen(prefix); | |
177 | 5 | char keystr[KEYCODE_STR_MAX]; | |
178 |
2/2✓ Branch 0 (9→3) taken 282 times.
✓ Branch 1 (9→10) taken 5 times.
|
292 | for (IntMapIter it = intmap_iter(bindings); intmap_next(&it); ) { |
179 | 282 | size_t keylen = keycode_to_string(it.entry->key, keystr); | |
180 |
2/2✓ Branch 0 (4→5) taken 124 times.
✓ Branch 1 (4→8) taken 158 times.
|
282 | if (str_has_strn_prefix(keystr, prefix, prefix_len)) { |
181 | 124 | ptr_array_append(a, xmemdup(keystr, keylen + 1)); | |
182 | } | ||
183 | } | ||
184 | 5 | } | |
185 | |||
186 | 1 | void collect_bound_normal_keys(EditorState *e, PointerArray *a, const char *prefix) | |
187 | { | ||
188 | 1 | collect_bound_keys(&e->normal_mode->key_bindings, a, prefix); | |
189 | 1 | } | |
190 | |||
191 | 1 | void collect_hl_styles(EditorState *e, PointerArray *a, const char *prefix) | |
192 | { | ||
193 | 1 | const char *dot = strchr(prefix, '.'); | |
194 |
1/4✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 1 times.
✗ Branch 2 (3→4) not taken.
✗ Branch 3 (3→6) not taken.
|
1 | if (!dot || dot == prefix || (dot - prefix) > FILETYPE_NAME_MAX) { |
195 | // No dot found in prefix, or found at offset 0, or buffer too small; | ||
196 | // just collect matching highlight names added by the `hi` command | ||
197 | 1 | collect_builtin_styles(a, prefix); | |
198 | 1 | collect_hashmap_keys(&e->styles.other, a, prefix); | |
199 | 2 | return; | |
200 | } | ||
201 | |||
202 | // Copy and null-terminate the filetype part of `prefix` (before the dot) | ||
203 | ✗ | char filetype[FILETYPE_NAME_MAX + 1]; | |
204 | ✗ | size_t ftlen = dot - prefix; | |
205 | ✗ | memcpy(filetype, prefix, ftlen); | |
206 | ✗ | filetype[ftlen] = '\0'; | |
207 | |||
208 | // Find or load the Syntax for `filetype` | ||
209 | ✗ | const Syntax *syn = find_syntax(&e->syntaxes, filetype); | |
210 | ✗ | if (!syn) { | |
211 | ✗ | syn = load_syntax_by_filetype(e, filetype); | |
212 | ✗ | if (!syn) { | |
213 | return; | ||
214 | } | ||
215 | } | ||
216 | |||
217 | // Collect all emit names from `syn` that start with the string after | ||
218 | // the dot | ||
219 | ✗ | collect_syntax_emit_names(syn, a, dot + 1); | |
220 | } | ||
221 | |||
222 | 3 | void collect_compilers(EditorState *e, PointerArray *a, const char *prefix) | |
223 | { | ||
224 | 3 | collect_hashmap_keys(&e->compilers, a, prefix); | |
225 | 3 | } | |
226 | |||
227 | 2 | void collect_env(EditorState* UNUSED_ARG(e), PointerArray *a, const char *prefix) | |
228 | { | ||
229 | 2 | size_t prefix_len = strlen(prefix); | |
230 |
1/2✓ Branch 0 (2→8) taken 2 times.
✗ Branch 1 (2→9) not taken.
|
2 | if (memchr(prefix, '=', prefix_len)) { |
231 | return; | ||
232 | } | ||
233 | |||
234 |
2/2✓ Branch 0 (8→3) taken 320 times.
✓ Branch 1 (8→9) taken 2 times.
|
322 | for (size_t i = 0; environ[i]; i++) { |
235 | 320 | const char *var = environ[i]; | |
236 |
2/2✓ Branch 0 (3→4) taken 2 times.
✓ Branch 1 (3→7) taken 318 times.
|
320 | if (str_has_strn_prefix(var, prefix, prefix_len)) { |
237 | 2 | const char *delim = strchr(var, '='); | |
238 |
1/2✓ Branch 0 (4→5) taken 2 times.
✗ Branch 1 (4→7) not taken.
|
2 | if (likely(delim && delim != var)) { |
239 | 2 | ptr_array_append(a, xstrcut(var, delim - var)); | |
240 | } | ||
241 | } | ||
242 | } | ||
243 | } | ||
244 | |||
245 | 2 | static void complete_alias(EditorState *e, const CommandArgs *a) | |
246 | { | ||
247 | 2 | CompletionState *cs = &e->cmdline.completion; | |
248 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 1 times.
|
2 | if (a->nr_args == 0) { |
249 | 1 | collect_normal_aliases(e, &cs->completions, cs->parsed); | |
250 |
2/4✓ Branch 0 (4→5) taken 1 times.
✗ Branch 1 (4→10) not taken.
✓ Branch 2 (5→6) taken 1 times.
✗ Branch 3 (5→10) not taken.
|
1 | } else if (a->nr_args == 1 && cs->parsed[0] == '\0') { |
251 | 1 | const char *cmd = find_alias(&e->aliases, a->args[0]); | |
252 |
1/2✓ Branch 0 (7→8) taken 1 times.
✗ Branch 1 (7→10) not taken.
|
1 | if (cmd) { |
253 | 1 | ptr_array_append(&cs->completions, xstrdup(cmd)); | |
254 | } | ||
255 | } | ||
256 | 2 | } | |
257 | |||
258 | 9 | static void complete_bind(EditorState *e, const CommandArgs *a) | |
259 | { | ||
260 |
3/4✓ Branch 0 (2→3) taken 7 times.
✓ Branch 1 (2→30) taken 2 times.
✓ Branch 2 (3→4) taken 7 times.
✗ Branch 3 (3→30) not taken.
|
9 | if (u64_popcount(a->flag_set) > 1 || a->nr_flag_args > 1) { |
261 | // Don't complete bindings for multiple modes | ||
262 | return; | ||
263 | } | ||
264 | |||
265 | 7 | const ModeHandler *mode; | |
266 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 7 times.
|
7 | if (cmdargs_has_flag(a, 'c')) { |
267 | ✗ | mode = e->command_mode; | |
268 |
2/2✓ Branch 0 (8→9) taken 3 times.
✓ Branch 1 (8→10) taken 4 times.
|
7 | } else if (cmdargs_has_flag(a, 's')) { |
269 | 3 | mode = e->search_mode; | |
270 |
2/2✓ Branch 0 (11→12) taken 1 times.
✓ Branch 1 (11→18) taken 3 times.
|
4 | } else if (cmdargs_has_flag(a, 'T')) { |
271 | 1 | BUG_ON(a->nr_flag_args != 1); | |
272 | 1 | BUG_ON(a->flags[0] != 'T'); | |
273 | 1 | mode = get_mode_handler(&e->modes, a->args[0]); | |
274 |
1/2✓ Branch 0 (17→19) taken 1 times.
✗ Branch 1 (17→30) not taken.
|
1 | if (!mode) { |
275 | return; | ||
276 | } | ||
277 | } else { | ||
278 | 3 | mode = e->normal_mode; | |
279 | } | ||
280 | |||
281 | 7 | const IntMap *key_bindings = &mode->key_bindings; | |
282 | 7 | CompletionState *cs = &e->cmdline.completion; | |
283 |
2/2✓ Branch 0 (19→20) taken 4 times.
✓ Branch 1 (19→22) taken 3 times.
|
7 | if (a->nr_args == 0) { |
284 | 4 | collect_bound_keys(key_bindings, &cs->completions, cs->parsed); | |
285 | 4 | return; | |
286 | } | ||
287 | |||
288 |
2/4✓ Branch 0 (22→23) taken 3 times.
✗ Branch 1 (22→30) not taken.
✓ Branch 2 (23→24) taken 3 times.
✗ Branch 3 (23→30) not taken.
|
3 | if (a->nr_args != 1 || cs->parsed[0] != '\0') { |
289 | return; | ||
290 | } | ||
291 | |||
292 | 3 | KeyCode key = parse_key_string(a->args[a->nr_flag_args]); | |
293 |
1/2✓ Branch 0 (25→26) taken 3 times.
✗ Branch 1 (25→30) not taken.
|
3 | if (key == KEY_NONE) { |
294 | return; | ||
295 | } | ||
296 | |||
297 | 3 | const CachedCommand *cmd = lookup_binding(key_bindings, key); | |
298 |
1/2✓ Branch 0 (27→28) taken 3 times.
✗ Branch 1 (27→30) not taken.
|
3 | if (!cmd) { |
299 | return; | ||
300 | } | ||
301 | |||
302 | 3 | ptr_array_append(&cs->completions, xstrdup(cmd->cmd_str)); | |
303 | } | ||
304 | |||
305 | ✗ | static void complete_cd(EditorState *e, const CommandArgs* UNUSED_ARG(a)) | |
306 | { | ||
307 | ✗ | CompletionState *cs = &e->cmdline.completion; | |
308 | ✗ | collect_files(e, cs, COLLECT_DIRS_ONLY); | |
309 | ✗ | if (str_has_prefix("-", cs->parsed)) { | |
310 | ✗ | if (likely(xgetenv("OLDPWD"))) { | |
311 | ✗ | ptr_array_append(&cs->completions, xstrdup("-")); | |
312 | } | ||
313 | } | ||
314 | ✗ | } | |
315 | |||
316 | 4 | static void complete_exec(EditorState *e, const CommandArgs *a) | |
317 | { | ||
318 | // TODO: add completion for [-ioe] option arguments | ||
319 | 4 | CompletionState *cs = &e->cmdline.completion; | |
320 | 4 | collect_files(e, cs, a->nr_args == 0 ? COLLECT_EXECUTABLES : COLLECT_ALL); | |
321 | 4 | } | |
322 | |||
323 | 1 | static void complete_compile(EditorState *e, const CommandArgs *a) | |
324 | { | ||
325 | 1 | CompletionState *cs = &e->cmdline.completion; | |
326 | 1 | size_t n = a->nr_args; | |
327 |
1/2✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→4) not taken.
|
1 | if (n == 0) { |
328 | 1 | collect_compilers(e, &cs->completions, cs->parsed); | |
329 | } else { | ||
330 | ✗ | collect_files(e, cs, n == 1 ? COLLECT_EXECUTABLES : COLLECT_ALL); | |
331 | } | ||
332 | 1 | } | |
333 | |||
334 | 4 | static void complete_cursor(EditorState *e, const CommandArgs *a) | |
335 | { | ||
336 | 4 | CompletionState *cs = &e->cmdline.completion; | |
337 | 4 | size_t n = a->nr_args; | |
338 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 3 times.
|
4 | if (n == 0) { |
339 | 1 | collect_cursor_modes(&cs->completions, cs->parsed); | |
340 |
2/2✓ Branch 0 (4→5) taken 2 times.
✓ Branch 1 (4→6) taken 1 times.
|
3 | } else if (n == 1) { |
341 | 2 | collect_cursor_types(&cs->completions, cs->parsed); | |
342 |
1/2✓ Branch 0 (6→7) taken 1 times.
✗ Branch 1 (6→11) not taken.
|
1 | } else if (n == 2) { |
343 | 1 | collect_cursor_colors(&cs->completions, cs->parsed); | |
344 | // Add an example #rrggbb color, to make things more discoverable | ||
345 | 1 | static const char rgb_example[] = "#22AABB"; | |
346 |
1/2✓ Branch 0 (8→9) taken 1 times.
✗ Branch 1 (8→11) not taken.
|
1 | if (str_has_prefix(rgb_example, cs->parsed)) { |
347 | 1 | ptr_array_append(&cs->completions, xstrdup(rgb_example)); | |
348 | } | ||
349 | } | ||
350 | 4 | } | |
351 | |||
352 | 3 | static void complete_def_mode(EditorState *e, const CommandArgs *a) | |
353 | { | ||
354 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→16) taken 1 times.
|
3 | if (a->nr_args == 0) { |
355 | return; | ||
356 | } | ||
357 | |||
358 | 2 | CompletionState *cs = &e->cmdline.completion; | |
359 | 2 | PointerArray *completions = &cs->completions; | |
360 | 2 | const char *prefix = cs->parsed; | |
361 | 2 | size_t prefix_len = strlen(prefix); | |
362 | |||
363 |
2/2✓ Branch 0 (14→4) taken 6 times.
✓ Branch 1 (14→15) taken 2 times.
|
8 | for (HashMapIter it = hashmap_iter(&e->modes); hashmap_next(&it); ) { |
364 | 6 | const char *name = it.entry->key; | |
365 | 6 | const ModeHandler *mode = it.entry->value; | |
366 |
1/2✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 6 times.
|
6 | if (!str_has_strn_prefix(name, prefix, prefix_len)) { |
367 | ✗ | continue; | |
368 | } | ||
369 |
2/2✓ Branch 0 (6→7) taken 4 times.
✓ Branch 1 (6→8) taken 2 times.
|
6 | if (mode->cmds != &normal_commands) { |
370 | // Exclude command/search mode | ||
371 | 4 | continue; | |
372 | } | ||
373 |
2/2✓ Branch 0 (8→9) taken 1 times.
✓ Branch 1 (8→10) taken 1 times.
|
2 | if (string_array_contains_str(a->args + 1 + a->nr_flag_args, name)) { |
374 | // Exclude modes already specified in a previous argument | ||
375 | 1 | continue; | |
376 | } | ||
377 | 1 | ptr_array_append(completions, xstrdup(name)); | |
378 | } | ||
379 | } | ||
380 | |||
381 | 3 | static void complete_errorfmt(EditorState *e, const CommandArgs *a) | |
382 | { | ||
383 | 3 | CompletionState *cs = &e->cmdline.completion; | |
384 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 2 times.
|
3 | if (a->nr_args == 0) { |
385 | 1 | collect_compilers(e, &cs->completions, cs->parsed); | |
386 |
2/4✓ Branch 0 (4→5) taken 2 times.
✗ Branch 1 (4→8) not taken.
✓ Branch 2 (6→7) taken 2 times.
✗ Branch 3 (6→8) not taken.
|
2 | } else if (a->nr_args >= 2 && !cmdargs_has_flag(a, 'i')) { |
387 | 2 | collect_errorfmt_capture_names(&cs->completions, cs->parsed); | |
388 | } | ||
389 | 3 | } | |
390 | |||
391 | 1 | static void complete_ft(EditorState *e, const CommandArgs *a) | |
392 | { | ||
393 | 1 | CompletionState *cs = &e->cmdline.completion; | |
394 |
1/2✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→4) not taken.
|
1 | if (a->nr_args == 0) { |
395 | 1 | collect_ft(&e->filetypes, &cs->completions, cs->parsed); | |
396 | } | ||
397 | 1 | } | |
398 | |||
399 | 2 | static void complete_hi(EditorState *e, const CommandArgs *a) | |
400 | { | ||
401 | 2 | CompletionState *cs = &e->cmdline.completion; | |
402 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 1 times.
|
2 | if (a->nr_args == 0) { |
403 | 1 | collect_hl_styles(e, &cs->completions, cs->parsed); | |
404 | } else { | ||
405 | // TODO: Take into account previous arguments and don't | ||
406 | // suggest repeat attributes or excess colors | ||
407 | 1 | collect_colors_and_attributes(&cs->completions, cs->parsed); | |
408 | } | ||
409 | 2 | } | |
410 | |||
411 | 1 | static void complete_include(EditorState *e, const CommandArgs *a) | |
412 | { | ||
413 | 1 | CompletionState *cs = &e->cmdline.completion; | |
414 |
1/2✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→7) not taken.
|
1 | if (a->nr_args == 0) { |
415 |
1/2✓ Branch 0 (4→5) taken 1 times.
✗ Branch 1 (4→6) not taken.
|
1 | if (cmdargs_has_flag(a, 'b')) { |
416 | 1 | collect_builtin_includes(&cs->completions, cs->parsed); | |
417 | } else { | ||
418 | ✗ | collect_files(e, cs, COLLECT_ALL); | |
419 | } | ||
420 | } | ||
421 | 1 | } | |
422 | |||
423 | 1 | static void complete_macro(EditorState *e, const CommandArgs *a) | |
424 | { | ||
425 | 1 | static const char verbs[][8] = { | |
426 | "cancel", | ||
427 | "play", | ||
428 | "record", | ||
429 | "stop", | ||
430 | "toggle", | ||
431 | }; | ||
432 | |||
433 |
1/2✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→4) not taken.
|
1 | if (a->nr_args != 0) { |
434 | return; | ||
435 | } | ||
436 | |||
437 | 1 | CompletionState *cs = &e->cmdline.completion; | |
438 | 1 | COLLECT_STRINGS(verbs, &cs->completions, cs->parsed); | |
439 | } | ||
440 | |||
441 | 1 | static void complete_mode(EditorState *e, const CommandArgs *a) | |
442 | { | ||
443 |
1/2✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→4) not taken.
|
1 | if (a->nr_args != 0) { |
444 | return; | ||
445 | } | ||
446 | |||
447 | 1 | CompletionState *cs = &e->cmdline.completion; | |
448 | 1 | collect_hashmap_keys(&e->modes, &cs->completions, cs->parsed); | |
449 | } | ||
450 | |||
451 | 1 | static void complete_move_tab(EditorState *e, const CommandArgs *a) | |
452 | { | ||
453 |
1/2✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→4) not taken.
|
1 | if (a->nr_args != 0) { |
454 | return; | ||
455 | } | ||
456 | |||
457 | 1 | static const char words[][8] = {"left", "right"}; | |
458 | 1 | CompletionState *cs = &e->cmdline.completion; | |
459 | 1 | COLLECT_STRINGS(words, &cs->completions, cs->parsed); | |
460 | } | ||
461 | |||
462 | 4 | static void complete_open(EditorState *e, const CommandArgs *a) | |
463 | { | ||
464 |
1/2✓ Branch 0 (3→4) taken 4 times.
✗ Branch 1 (3→5) not taken.
|
4 | if (!cmdargs_has_flag(a, 't')) { |
465 | 4 | collect_files(e, &e->cmdline.completion, COLLECT_ALL); | |
466 | } | ||
467 | 4 | } | |
468 | |||
469 | 3 | static void complete_option(EditorState *e, const CommandArgs *a) | |
470 | { | ||
471 | 3 | CompletionState *cs = &e->cmdline.completion; | |
472 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→6) taken 2 times.
|
3 | if (a->nr_args == 0) { |
473 |
1/2✓ Branch 0 (4→5) taken 1 times.
✗ Branch 1 (4→9) not taken.
|
1 | if (!cmdargs_has_flag(a, 'r')) { |
474 | 1 | collect_ft(&e->filetypes, &cs->completions, cs->parsed); | |
475 | } | ||
476 |
2/2✓ Branch 0 (6→7) taken 1 times.
✓ Branch 1 (6→8) taken 1 times.
|
2 | } else if (a->nr_args & 1) { |
477 | 1 | collect_auto_options(&cs->completions, cs->parsed); | |
478 | } else { | ||
479 | 1 | collect_option_values(e, &cs->completions, a->args[a->nr_args - 1], cs->parsed); | |
480 | } | ||
481 | 3 | } | |
482 | |||
483 | 1 | static void complete_save(EditorState *e, const CommandArgs* UNUSED_ARG(a)) | |
484 | { | ||
485 | 1 | collect_files(e, &e->cmdline.completion, COLLECT_ALL); | |
486 | 1 | } | |
487 | |||
488 | 1 | static void complete_quit(EditorState *e, const CommandArgs* UNUSED_ARG(a)) | |
489 | { | ||
490 | 1 | CompletionState *cs = &e->cmdline.completion; | |
491 |
1/2✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→5) not taken.
|
1 | if (str_has_prefix("0", cs->parsed)) { |
492 | 1 | ptr_array_append(&cs->completions, xstrdup("0")); | |
493 | } | ||
494 |
1/2✓ Branch 0 (5→6) taken 1 times.
✗ Branch 1 (5→8) not taken.
|
1 | if (str_has_prefix("1", cs->parsed)) { |
495 | 1 | ptr_array_append(&cs->completions, xstrdup("1")); | |
496 | } | ||
497 | 1 | } | |
498 | |||
499 | ✗ | static void complete_redo(EditorState *e, const CommandArgs* UNUSED_ARG(a)) | |
500 | { | ||
501 | ✗ | const Change *change = e->buffer->cur_change; | |
502 | ✗ | CompletionState *cs = &e->cmdline.completion; | |
503 | ✗ | for (unsigned long i = 1, n = change->nr_prev; i <= n; i++) { | |
504 | ✗ | ptr_array_append(&cs->completions, xstrdup(ulong_to_str(i))); | |
505 | } | ||
506 | ✗ | } | |
507 | |||
508 | 7 | static void complete_set(EditorState *e, const CommandArgs *a) | |
509 | { | ||
510 | 7 | CompletionState *cs = &e->cmdline.completion; | |
511 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→6) taken 6 times.
|
7 | if ((a->nr_args + 1) & 1) { |
512 | 1 | bool local = cmdargs_has_flag(a, 'l'); | |
513 | 1 | bool global = cmdargs_has_flag(a, 'g'); | |
514 | 1 | collect_options(&cs->completions, cs->parsed, local, global); | |
515 | } else { | ||
516 | 6 | collect_option_values(e, &cs->completions, a->args[a->nr_args - 1], cs->parsed); | |
517 | } | ||
518 | 7 | } | |
519 | |||
520 | 2 | static void complete_setenv(EditorState *e, const CommandArgs *a) | |
521 | { | ||
522 | 2 | CompletionState *cs = &e->cmdline.completion; | |
523 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 1 times.
|
2 | if (a->nr_args == 0) { |
524 | 1 | collect_env(e, &cs->completions, cs->parsed); | |
525 |
2/4✓ Branch 0 (4→5) taken 1 times.
✗ Branch 1 (4→12) not taken.
✓ Branch 2 (5→6) taken 1 times.
✗ Branch 3 (5→12) not taken.
|
1 | } else if (a->nr_args == 1 && cs->parsed[0] == '\0') { |
526 | 1 | BUG_ON(!a->args[0]); | |
527 | 1 | const char *value = getenv(a->args[0]); | |
528 |
1/2✓ Branch 0 (9→10) taken 1 times.
✗ Branch 1 (9→12) not taken.
|
1 | if (value) { |
529 | 1 | ptr_array_append(&cs->completions, xstrdup(value)); | |
530 | } | ||
531 | } | ||
532 | 2 | } | |
533 | |||
534 | 6 | static void complete_show(EditorState *e, const CommandArgs *a) | |
535 | { | ||
536 | 6 | CompletionState *cs = &e->cmdline.completion; | |
537 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→4) taken 4 times.
|
6 | if (a->nr_args == 0) { |
538 | 2 | collect_show_subcommands(&cs->completions, cs->parsed); | |
539 |
1/2✓ Branch 0 (4→5) taken 4 times.
✗ Branch 1 (4→8) not taken.
|
4 | } else if (a->nr_args == 1) { |
540 | 4 | BUG_ON(!a->args[0]); | |
541 | 4 | collect_show_subcommand_args(e, &cs->completions, a->args[0], cs->parsed); | |
542 | } | ||
543 | 6 | } | |
544 | |||
545 | ✗ | static void complete_tag(EditorState *e, const CommandArgs *a) | |
546 | { | ||
547 | ✗ | CompletionState *cs = &e->cmdline.completion; | |
548 | ✗ | if (!cmdargs_has_flag(a, 'r')) { | |
549 | ✗ | BUG_ON(!cs->parsed); | |
550 | ✗ | StringView prefix = strview_from_cstring(cs->parsed); | |
551 | ✗ | collect_tags(&e->tagfile, &cs->completions, &prefix); | |
552 | } | ||
553 | ✗ | } | |
554 | |||
555 | 1 | static void complete_toggle(EditorState *e, const CommandArgs *a) | |
556 | { | ||
557 | 1 | CompletionState *cs = &e->cmdline.completion; | |
558 |
1/2✓ Branch 0 (2→3) taken 1 times.
✗ Branch 1 (2→5) not taken.
|
1 | if (a->nr_args == 0) { |
559 | 1 | bool global = cmdargs_has_flag(a, 'g'); | |
560 | 1 | collect_toggleable_options(&cs->completions, cs->parsed, global); | |
561 | } | ||
562 | 1 | } | |
563 | |||
564 | 1 | static void complete_wsplit(EditorState *e, const CommandArgs *a) | |
565 | { | ||
566 | 1 | CompletionState *cs = &e->cmdline.completion; | |
567 |
2/4✓ Branch 0 (3→4) taken 1 times.
✗ Branch 1 (3→7) not taken.
✓ Branch 2 (5→6) taken 1 times.
✗ Branch 3 (5→7) not taken.
|
1 | if (!cmdargs_has_flag(a, 't') && !cmdargs_has_flag(a, 'n')) { |
568 | 1 | collect_files(e, cs, COLLECT_ALL); | |
569 | } | ||
570 | 1 | } | |
571 | |||
572 | typedef struct { | ||
573 | char cmd_name[12]; | ||
574 | void (*complete)(EditorState *e, const CommandArgs *a); | ||
575 | } CompletionHandler; | ||
576 | |||
577 | static const CompletionHandler completion_handlers[] = { | ||
578 | {"alias", complete_alias}, | ||
579 | {"bind", complete_bind}, | ||
580 | {"cd", complete_cd}, | ||
581 | {"compile", complete_compile}, | ||
582 | {"cursor", complete_cursor}, | ||
583 | {"def-mode", complete_def_mode}, | ||
584 | {"errorfmt", complete_errorfmt}, | ||
585 | {"exec", complete_exec}, | ||
586 | {"ft", complete_ft}, | ||
587 | {"hi", complete_hi}, | ||
588 | {"include", complete_include}, | ||
589 | {"macro", complete_macro}, | ||
590 | {"mode", complete_mode}, | ||
591 | {"move-tab", complete_move_tab}, | ||
592 | {"open", complete_open}, | ||
593 | {"option", complete_option}, | ||
594 | {"quit", complete_quit}, | ||
595 | {"redo", complete_redo}, | ||
596 | {"save", complete_save}, | ||
597 | {"set", complete_set}, | ||
598 | {"setenv", complete_setenv}, | ||
599 | {"show", complete_show}, | ||
600 | {"tag", complete_tag}, | ||
601 | {"toggle", complete_toggle}, | ||
602 | {"wsplit", complete_wsplit}, | ||
603 | }; | ||
604 | |||
605 | 18 | UNITTEST { | |
606 | 18 | CHECK_BSEARCH_ARRAY(completion_handlers, cmd_name, strcmp); | |
607 | // Ensure handlers are kept in sync with renamed/removed commands | ||
608 |
2/2✓ Branch 0 (8→4) taken 450 times.
✓ Branch 1 (8→9) taken 18 times.
|
468 | for (size_t i = 0; i < ARRAYLEN(completion_handlers); i++) { |
609 | 450 | const char *name = completion_handlers[i].cmd_name; | |
610 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 450 times.
|
450 | if (!find_normal_command(name)) { |
611 | − | BUG("completion handler for non-existent command: \"%s\"", name); | |
612 | } | ||
613 | } | ||
614 | 18 | } | |
615 | |||
616 | 20 | static bool can_collect_flags ( | |
617 | char **args, | ||
618 | size_t argc, | ||
619 | size_t nr_flag_args, | ||
620 | bool allow_flags_after_nonflags | ||
621 | ) { | ||
622 |
2/2✓ Branch 0 (2→5) taken 12 times.
✓ Branch 1 (2→11) taken 8 times.
|
20 | if (allow_flags_after_nonflags) { |
623 |
2/2✓ Branch 0 (5→3) taken 6 times.
✓ Branch 1 (5→12) taken 10 times.
|
16 | for (size_t i = 0; i < argc; i++) { |
624 |
2/2✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→12) taken 2 times.
|
6 | if (streq(args[i], "--")) { |
625 | return false; | ||
626 | } | ||
627 | } | ||
628 | return true; | ||
629 | } | ||
630 | |||
631 |
2/2✓ Branch 0 (11→6) taken 14 times.
✓ Branch 1 (11→12) taken 4 times.
|
18 | for (size_t i = 0, nonflag = 0; i < argc; i++) { |
632 |
2/2✓ Branch 0 (6→7) taken 6 times.
✓ Branch 1 (6→9) taken 8 times.
|
14 | if (args[i][0] != '-') { |
633 |
2/2✓ Branch 0 (7→8) taken 3 times.
✓ Branch 1 (7→12) taken 3 times.
|
6 | if (++nonflag > nr_flag_args) { |
634 | return false; | ||
635 | } | ||
636 | 3 | continue; | |
637 | } | ||
638 |
2/2✓ Branch 0 (9→10) taken 7 times.
✓ Branch 1 (9→12) taken 1 times.
|
8 | if (streq(args[i], "--")) { |
639 | return false; | ||
640 | } | ||
641 | } | ||
642 | |||
643 | return true; | ||
644 | } | ||
645 | |||
646 | 20 | static bool collect_command_flags ( | |
647 | PointerArray *array, | ||
648 | char **args, | ||
649 | size_t argc, | ||
650 | const Command *cmd, | ||
651 | const CommandArgs *a, | ||
652 | const char *prefix | ||
653 | ) { | ||
654 | 20 | BUG_ON(prefix[0] != '-'); | |
655 | 20 | bool flags_after_nonflags = !(cmd->cmdopts & CMDOPT_NO_FLAGS_AFTER_ARGS); | |
656 |
2/2✓ Branch 0 (4→5) taken 14 times.
✓ Branch 1 (4→21) taken 6 times.
|
20 | if (!can_collect_flags(args, argc, a->nr_flag_args, flags_after_nonflags)) { |
657 | return false; | ||
658 | } | ||
659 | |||
660 | 14 | const char *flags = cmd->flags; | |
661 |
4/4✓ Branch 0 (5→6) taken 3 times.
✓ Branch 1 (5→11) taken 11 times.
✓ Branch 2 (6→7) taken 2 times.
✓ Branch 3 (6→11) taken 1 times.
|
14 | if (ascii_isalnum(prefix[1]) && prefix[2] == '\0') { |
662 |
1/2✓ Branch 0 (7→8) taken 2 times.
✗ Branch 1 (7→10) not taken.
|
2 | if (strchr(flags, prefix[1])) { |
663 | 2 | ptr_array_append(array, xmemdup(prefix, 3)); | |
664 | } | ||
665 | 2 | return true; | |
666 | } | ||
667 | |||
668 |
2/2✓ Branch 0 (11→12) taken 11 times.
✓ Branch 1 (11→21) taken 1 times.
|
12 | if (prefix[1] != '\0') { |
669 | return true; | ||
670 | } | ||
671 | |||
672 | 11 | char buf[3] = "-"; | |
673 |
2/2✓ Branch 0 (20→13) taken 53 times.
✓ Branch 1 (20→21) taken 11 times.
|
64 | for (size_t i = 0; flags[i]; i++) { |
674 |
4/4✓ Branch 0 (13→14) taken 47 times.
✓ Branch 1 (13→16) taken 6 times.
✓ Branch 2 (15→16) taken 13 times.
✓ Branch 3 (15→17) taken 34 times.
|
53 | if (!ascii_isalnum(flags[i]) || cmdargs_has_flag(a, flags[i])) { |
675 | 19 | continue; | |
676 | } | ||
677 | 34 | buf[1] = flags[i]; | |
678 | 34 | ptr_array_append(array, xmemdup(buf, 3)); | |
679 | } | ||
680 | |||
681 | return true; | ||
682 | } | ||
683 | |||
684 | 3 | static void collect_command_flag_args ( | |
685 | EditorState *e, | ||
686 | PointerArray *array, | ||
687 | const char *prefix, | ||
688 | const char *cmd, | ||
689 | const CommandArgs *a | ||
690 | ) { | ||
691 | 3 | char flag = a->flags[0]; | |
692 |
2/2✓ Branch 0 (2→3) taken 2 times.
✓ Branch 1 (2→6) taken 1 times.
|
3 | if (streq(cmd, "bind")) { |
693 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 2 times.
|
2 | WARN_ON(flag != 'T'); |
694 | 2 | collect_hashmap_keys(&e->modes, array, prefix); | |
695 |
1/2✓ Branch 0 (6→7) taken 1 times.
✗ Branch 1 (6→13) not taken.
|
1 | } else if (streq(cmd, "exec")) { |
696 |
2/4✓ Branch 0 (7→8) taken 1 times.
✗ Branch 1 (7→10) not taken.
✗ Branch 2 (8→9) not taken.
✓ Branch 3 (8→10) taken 1 times.
|
1 | int fd = (flag == 'i') ? 0 : (flag == 'o' ? 1 : 2); |
697 |
1/2✗ Branch 0 (10→11) not taken.
✓ Branch 1 (10→12) taken 1 times.
|
1 | WARN_ON(fd == 2 && flag != 'e'); |
698 | 1 | collect_exec_actions(array, prefix, fd); | |
699 | } | ||
700 | // TODO: Completions for `open -e` and `save -e` | ||
701 | 3 | } | |
702 | |||
703 | // NOLINTNEXTLINE(misc-no-recursion) | ||
704 | 86 | static void collect_completions(EditorState *e, char **args, size_t argc) | |
705 | { | ||
706 | 86 | CompletionState *cs = &e->cmdline.completion; | |
707 | 86 | PointerArray *arr = &cs->completions; | |
708 | 86 | const char *prefix = cs->parsed; | |
709 |
2/2✓ Branch 0 (2→3) taken 4 times.
✓ Branch 1 (2→7) taken 82 times.
|
86 | if (!argc) { |
710 | 4 | collect_normal_commands(arr, prefix); | |
711 | 4 | collect_normal_aliases(e, arr, prefix); | |
712 | 8 | return; | |
713 | } | ||
714 | |||
715 |
2/2✓ Branch 0 (7→5) taken 157 times.
✓ Branch 1 (7→8) taken 82 times.
|
239 | for (size_t i = 0; i < argc; i++) { |
716 |
1/2✓ Branch 0 (5→6) taken 157 times.
✗ Branch 1 (5→39) not taken.
|
157 | if (!args[i]) { |
717 | // Embedded NULLs indicate there are multiple commands. | ||
718 | // Just return early here and avoid handling this case. | ||
719 | return; | ||
720 | } | ||
721 | } | ||
722 | |||
723 | 82 | const char *cmdname = args[0]; | |
724 | 82 | const Command *cmd = find_normal_command(cmdname); | |
725 |
1/2✓ Branch 0 (9→10) taken 82 times.
✗ Branch 1 (9→39) not taken.
|
82 | if (!cmd) { |
726 | return; | ||
727 | } | ||
728 | |||
729 | 82 | char **args_copy = copy_string_array(args + 1, argc - 1); | |
730 | 82 | CommandArgs a = cmdargs_new(args_copy); | |
731 | 82 | ArgParseError err = do_parse_args(cmd, &a); | |
732 | |||
733 |
2/2✓ Branch 0 (12→13) taken 3 times.
✓ Branch 1 (12→15) taken 79 times.
|
82 | if (err == ARGERR_OPTION_ARGUMENT_MISSING) { |
734 | 3 | collect_command_flag_args(e, arr, prefix, cmdname, &a); | |
735 | 3 | goto out; | |
736 | } | ||
737 | |||
738 | 79 | bool dash = (prefix[0] == '-'); | |
739 | 79 | if ( | |
740 |
2/2✓ Branch 0 (15→16) taken 78 times.
✓ Branch 1 (15→19) taken 1 times.
|
79 | (err != ARGERR_NONE && err != ARGERR_TOO_FEW_ARGUMENTS) |
741 |
5/6✓ Branch 0 (16→17) taken 6 times.
✓ Branch 1 (16→20) taken 72 times.
✓ Branch 2 (17→18) taken 6 times.
✗ Branch 3 (17→20) not taken.
✓ Branch 4 (18→19) taken 1 times.
✓ Branch 5 (18→21) taken 5 times.
|
78 | || (a.nr_args >= cmd->max_args && cmd->max_args != 0xFF && !dash) |
742 | ) { | ||
743 | 2 | goto out; | |
744 | } | ||
745 | |||
746 |
4/4✓ Branch 0 (20→21) taken 15 times.
✓ Branch 1 (20→24) taken 57 times.
✓ Branch 2 (22→23) taken 14 times.
✓ Branch 3 (22→24) taken 6 times.
|
77 | if (dash && collect_command_flags(arr, args + 1, argc - 1, cmd, &a, prefix)) { |
747 | 14 | goto out; | |
748 | } | ||
749 | |||
750 |
2/2✓ Branch 0 (24→25) taken 1 times.
✓ Branch 1 (24→26) taken 62 times.
|
63 | if (cmd->max_args == 0) { |
751 | 1 | goto out; | |
752 | } | ||
753 | |||
754 | 62 | const CompletionHandler *h = BSEARCH(cmdname, completion_handlers, vstrcmp); | |
755 |
2/2✓ Branch 0 (27→28) taken 59 times.
✓ Branch 1 (27→29) taken 3 times.
|
62 | if (h) { |
756 | 59 | h->complete(e, &a); | |
757 |
2/2✓ Branch 0 (29→30) taken 1 times.
✓ Branch 1 (29→31) taken 2 times.
|
3 | } else if (streq(cmdname, "repeat")) { |
758 |
2/2✓ Branch 0 (31→32) taken 1 times.
✓ Branch 1 (31→33) taken 1 times.
|
2 | if (a.nr_args == 1) { |
759 | 1 | collect_normal_commands(arr, prefix); | |
760 |
1/2✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→35) taken 1 times.
|
1 | } else if (a.nr_args >= 2) { |
761 | 1 | collect_completions(e, args + 2, argc - 2); | |
762 | } | ||
763 | } | ||
764 | |||
765 | 1 | out: | |
766 | 82 | free_string_array(args_copy); | |
767 | } | ||
768 | |||
769 | 338 | static bool is_var(const char *str, size_t len) | |
770 | { | ||
771 |
4/4✓ Branch 0 (2→3) taken 289 times.
✓ Branch 1 (2→9) taken 49 times.
✓ Branch 2 (3→4) taken 217 times.
✓ Branch 3 (3→9) taken 72 times.
|
338 | if (len == 0 || str[0] != '$') { |
772 | return false; | ||
773 | } | ||
774 |
2/2✓ Branch 0 (4→5) taken 199 times.
✓ Branch 1 (4→9) taken 18 times.
|
217 | if (len == 1) { |
775 | return true; | ||
776 | } | ||
777 |
2/2✓ Branch 0 (5→8) taken 127 times.
✓ Branch 1 (5→9) taken 72 times.
|
199 | if (!is_alpha_or_underscore(str[1])) { |
778 | return false; | ||
779 | } | ||
780 |
2/2✓ Branch 0 (8→6) taken 316 times.
✓ Branch 1 (8→9) taken 109 times.
|
425 | for (size_t i = 2; i < len; i++) { |
781 |
2/2✓ Branch 0 (6→7) taken 298 times.
✓ Branch 1 (6→9) taken 18 times.
|
316 | if (!is_alnum_or_underscore(str[i])) { |
782 | return false; | ||
783 | } | ||
784 | } | ||
785 | return true; | ||
786 | } | ||
787 | |||
788 | 18 | UNITTEST { | |
789 | // NOLINTBEGIN(bugprone-assert-side-effect) | ||
790 | 18 | BUG_ON(!is_var(STRN("$VAR"))); | |
791 | 18 | BUG_ON(!is_var(STRN("$xy_190"))); | |
792 | 18 | BUG_ON(!is_var(STRN("$__x_y_z"))); | |
793 | 18 | BUG_ON(!is_var(STRN("$x"))); | |
794 | 18 | BUG_ON(!is_var(STRN("$A"))); | |
795 | 18 | BUG_ON(!is_var(STRN("$_0"))); | |
796 | 18 | BUG_ON(!is_var(STRN("$"))); | |
797 | 18 | BUG_ON(is_var(STRN(""))); | |
798 | 18 | BUG_ON(is_var(STRN("A"))); | |
799 | 18 | BUG_ON(is_var(STRN("$.a"))); | |
800 | 18 | BUG_ON(is_var(STRN("$xyz!"))); | |
801 | 18 | BUG_ON(is_var(STRN("$1"))); | |
802 | 18 | BUG_ON(is_var(STRN("$09"))); | |
803 | 18 | BUG_ON(is_var(STRN("$1a"))); | |
804 | // NOLINTEND(bugprone-assert-side-effect) | ||
805 | 18 | } | |
806 | |||
807 | 2073 | static int strptrcmp(const void *v1, const void *v2) | |
808 | { | ||
809 | 2073 | const char *const *s1 = v1; | |
810 | 2073 | const char *const *s2 = v2; | |
811 | 2073 | return strcmp(*s1, *s2); | |
812 | } | ||
813 | |||
814 | 86 | static void init_completion(EditorState *e, const CommandLine *cmdline) | |
815 | { | ||
816 | 86 | CompletionState *cs = &e->cmdline.completion; | |
817 | 86 | const CommandRunner runner = normal_mode_cmdrunner(e); | |
818 | 86 | BUG_ON(cs->orig); | |
819 | 86 | BUG_ON(runner.e != e); | |
820 | 86 | BUG_ON(!runner.lookup_alias); | |
821 | |||
822 | 86 | const size_t cmdline_pos = cmdline->pos; | |
823 | 86 | char *const cmd = string_clone_cstring(&cmdline->buf); | |
824 | 86 | PointerArray array = PTR_ARRAY_INIT; | |
825 | 86 | ssize_t semicolon = -1; | |
826 | 86 | ssize_t completion_pos = -1; | |
827 | |||
828 | 86 | for (size_t pos = 0; true; ) { | |
829 |
2/2✓ Branch 0 (12→10) taken 161 times.
✓ Branch 1 (12→13) taken 250 times.
|
411 | while (ascii_isspace(cmd[pos])) { |
830 | 161 | pos++; | |
831 | } | ||
832 | |||
833 |
2/2✓ Branch 0 (13→14) taken 31 times.
✓ Branch 1 (13→15) taken 219 times.
|
250 | if (pos >= cmdline_pos) { |
834 | 31 | completion_pos = cmdline_pos; | |
835 | 31 | break; | |
836 | } | ||
837 | |||
838 |
1/2✓ Branch 0 (15→16) taken 219 times.
✗ Branch 1 (15→41) not taken.
|
219 | if (!cmd[pos]) { |
839 | break; | ||
840 | } | ||
841 | |||
842 |
2/2✓ Branch 0 (16→17) taken 3 times.
✓ Branch 1 (16→19) taken 216 times.
|
219 | if (cmd[pos] == ';') { |
843 | 3 | semicolon = array.count; | |
844 | 3 | ptr_array_append(&array, NULL); | |
845 | 3 | pos++; | |
846 | 3 | continue; | |
847 | } | ||
848 | |||
849 | 216 | CommandParseError err; | |
850 | 216 | size_t end = find_end(cmd, pos, &err); | |
851 |
3/4✓ Branch 0 (20→21) taken 216 times.
✗ Branch 1 (20→22) not taken.
✓ Branch 2 (21→22) taken 55 times.
✓ Branch 3 (21→23) taken 161 times.
|
216 | if (err != CMDERR_NONE || end >= cmdline_pos) { |
852 | 55 | completion_pos = pos; | |
853 | 55 | break; | |
854 | } | ||
855 | |||
856 |
2/2✓ Branch 0 (23→24) taken 85 times.
✓ Branch 1 (23→38) taken 76 times.
|
161 | if (semicolon + 1 == array.count) { |
857 | 85 | char *name = xstrslice(cmd, pos, end); | |
858 | 85 | const char *value = runner.lookup_alias(runner.e, name); | |
859 |
1/2✗ Branch 0 (26→27) not taken.
✓ Branch 1 (26→35) taken 85 times.
|
85 | if (value) { |
860 | ✗ | size_t save = array.count; | |
861 | ✗ | if (parse_commands(&runner, &array, value) != CMDERR_NONE) { | |
862 | ✗ | for (size_t i = save, n = array.count; i < n; i++) { | |
863 | ✗ | free(array.ptrs[i]); | |
864 | ✗ | array.ptrs[i] = NULL; | |
865 | } | ||
866 | ✗ | array.count = save; | |
867 | ✗ | ptr_array_append(&array, parse_command_arg(&runner, name, end - pos)); | |
868 | } else { | ||
869 | // Remove NULL | ||
870 | ✗ | array.count--; | |
871 | } | ||
872 | } else { | ||
873 | 85 | ptr_array_append(&array, parse_command_arg(&runner, name, end - pos)); | |
874 | } | ||
875 | 85 | free(name); | |
876 | } else { | ||
877 | 76 | ptr_array_append(&array, parse_command_arg(&runner, cmd + pos, end - pos)); | |
878 | } | ||
879 | 161 | pos = end; | |
880 | } | ||
881 | |||
882 | 86 | const char *str = cmd + completion_pos; | |
883 | 86 | size_t len = cmdline_pos - completion_pos; | |
884 |
2/2✓ Branch 0 (41→42) taken 1 times.
✓ Branch 1 (41→46) taken 85 times.
|
86 | if (is_var(str, len)) { |
885 | 1 | char *name = xstrslice(str, 1, len); | |
886 | 1 | completion_pos++; | |
887 | 1 | collect_env(e, &cs->completions, name); | |
888 | 1 | collect_normal_vars(&cs->completions, name); | |
889 | 1 | free(name); | |
890 | } else { | ||
891 | 85 | cs->escaped = string_view(str, len); | |
892 | 85 | cs->parsed = parse_command_arg(&runner, str, len); | |
893 | 85 | cs->add_space_after_single_match = true; | |
894 | 85 | size_t count = array.count; | |
895 |
2/2✓ Branch 0 (47→48) taken 82 times.
✓ Branch 1 (47→49) taken 3 times.
|
85 | char **args = count ? (char**)array.ptrs + 1 + semicolon : NULL; |
896 | 82 | size_t argc = count ? count - semicolon - 1 : 0; | |
897 | 85 | collect_completions(e, args, argc); | |
898 | } | ||
899 | |||
900 | 86 | ptr_array_free(&array); | |
901 | 86 | ptr_array_sort(&cs->completions, strptrcmp); | |
902 | 86 | cs->orig = cmd; // (takes ownership) | |
903 | 86 | cs->tail = strview_from_cstring(cmd + cmdline_pos); | |
904 | 86 | cs->head_len = completion_pos; | |
905 | 86 | } | |
906 | |||
907 | 92 | static void do_complete_command(CommandLine *cmdline) | |
908 | { | ||
909 | 92 | const CompletionState *cs = &cmdline->completion; | |
910 | 92 | const PointerArray *arr = &cs->completions; | |
911 | 92 | const StringView middle = strview_from_cstring(arr->ptrs[cs->idx]); | |
912 | 92 | const StringView tail = cs->tail; | |
913 | 92 | const size_t head_length = cs->head_len; | |
914 | |||
915 | 92 | String buf = string_new(head_length + tail.length + middle.length + 16); | |
916 | 92 | string_append_buf(&buf, cs->orig, head_length); | |
917 | 92 | string_append_escaped_arg_sv(&buf, middle, !cs->tilde_expanded); | |
918 | |||
919 | 92 | bool single_completion = (arr->count == 1); | |
920 |
4/4✓ Branch 0 (5→6) taken 41 times.
✓ Branch 1 (5→8) taken 51 times.
✓ Branch 2 (6→7) taken 40 times.
✓ Branch 3 (6→8) taken 1 times.
|
92 | if (single_completion && cs->add_space_after_single_match) { |
921 | 40 | string_append_byte(&buf, ' '); | |
922 | } | ||
923 | |||
924 | 92 | size_t pos = buf.len; | |
925 | 92 | string_append_strview(&buf, &tail); | |
926 | 92 | cmdline_set_text(cmdline, string_borrow_cstring(&buf)); | |
927 | 92 | cmdline->pos = pos; | |
928 | 92 | string_free(&buf); | |
929 | |||
930 |
2/2✓ Branch 0 (12→13) taken 41 times.
✓ Branch 1 (12→14) taken 51 times.
|
92 | if (single_completion) { |
931 | 41 | reset_completion(cmdline); | |
932 | } | ||
933 | 92 | } | |
934 | |||
935 | 105 | void complete_command_next(EditorState *e) | |
936 | { | ||
937 | 105 | CompletionState *cs = &e->cmdline.completion; | |
938 | 105 | const bool init = !cs->orig; | |
939 |
2/2✓ Branch 0 (2→3) taken 85 times.
✓ Branch 1 (2→4) taken 20 times.
|
105 | if (init) { |
940 | 85 | init_completion(e, &e->cmdline); | |
941 | } | ||
942 | 105 | size_t count = cs->completions.count; | |
943 |
2/2✓ Branch 0 (4→5) taken 87 times.
✓ Branch 1 (4→9) taken 18 times.
|
105 | if (!count) { |
944 | return; | ||
945 | } | ||
946 |
2/2✓ Branch 0 (5→6) taken 20 times.
✓ Branch 1 (5→8) taken 67 times.
|
87 | if (!init) { |
947 | 20 | cs->idx = size_increment_wrapped(cs->idx, count); | |
948 | } | ||
949 | 87 | do_complete_command(&e->cmdline); | |
950 | } | ||
951 | |||
952 | 5 | void complete_command_prev(EditorState *e) | |
953 | { | ||
954 | 5 | CompletionState *cs = &e->cmdline.completion; | |
955 | 5 | const bool init = !cs->orig; | |
956 |
2/2✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 4 times.
|
5 | if (init) { |
957 | 1 | init_completion(e, &e->cmdline); | |
958 | } | ||
959 | 5 | size_t count = cs->completions.count; | |
960 |
1/2✓ Branch 0 (4→5) taken 5 times.
✗ Branch 1 (4→9) not taken.
|
5 | if (!count) { |
961 | return; | ||
962 | } | ||
963 |
2/2✓ Branch 0 (5→6) taken 4 times.
✓ Branch 1 (5→8) taken 1 times.
|
5 | if (!init) { |
964 | 4 | cs->idx = size_decrement_wrapped(cs->idx, count); | |
965 | } | ||
966 | 5 | do_complete_command(&e->cmdline); | |
967 | } | ||
968 | |||
969 | 124 | void reset_completion(CommandLine *cmdline) | |
970 | { | ||
971 | 124 | CompletionState *cs = &cmdline->completion; | |
972 | 124 | free(cs->parsed); | |
973 | 124 | free(cs->orig); | |
974 | 124 | ptr_array_free(&cs->completions); | |
975 | 124 | *cs = (CompletionState){.orig = NULL}; | |
976 | 124 | } | |
977 | |||
978 | 12 | void collect_hashmap_keys(const HashMap *map, PointerArray *a, const char *prefix) | |
979 | { | ||
980 | 12 | size_t prefix_len = strlen(prefix); | |
981 |
2/2✓ Branch 0 (8→3) taken 42 times.
✓ Branch 1 (8→9) taken 12 times.
|
66 | for (HashMapIter it = hashmap_iter(map); hashmap_next(&it); ) { |
982 | 42 | const char *name = it.entry->key; | |
983 |
2/2✓ Branch 0 (3→4) taken 11 times.
✓ Branch 1 (3→7) taken 31 times.
|
42 | if (str_has_strn_prefix(name, prefix, prefix_len)) { |
984 | 11 | ptr_array_append(a, xstrdup(name)); | |
985 | } | ||
986 | } | ||
987 | 12 | } | |
988 |