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