64875
Mc
mini-coder / files
mini-coder / src / tui / complete.ts
3.8 KB
1import { readdirSync, statSync, type Dirent } from "node:fs"; 2import { homedir } from "node:os"; 3import { join, resolve } from "node:path"; 4 5/** 6 * Deliberately bash-unfaithful: no `cdable_vars`, no `~user` expansion (`~` 7 * always means `homedir()`), no `$VAR`, backtick, or history expansion (word 8 * text is always literal), and no shell options — dotfile hiding, byte-exact 9 * matching, and dir-slash behavior below hold unconditionally. 10 */ 11 12/** 13 * A word must already carry path syntax to be completed: a leading `~`, a 14 * leading `./` or `../` (or bare `.` / `..`), a leading `/`, or any segment 15 * followed by a slash somewhere in the word. Bare words (`re`, `hello`) return 16 * null without a syscall, so mid-sentence prose never gets hijacked. 17 */ 18const PATH_LIKE = /^(?:~|\/|\.{1,2}(?:\/|$)|\w+\/)/; 19 20/** A directory, or a symlink that resolves to one (`statSync` follows). */ 21function isDir(path: string, entry: Dirent): boolean { 22 if (entry.isDirectory()) return true; 23 if (!entry.isSymbolicLink()) return false; 24 try { 25 return statSync(join(path, entry.name)).isDirectory(); 26 } catch { 27 return false; // broken link completes file-shaped, like bash. 28 } 29} 30 31/** 32 * Completes `word` as a file path, bash-readline-style: the returned word is 33 * the whole feedback, a strict extension of what was typed. Unreadable 34 * locations, paths through files, and nothing longer to add all give null and 35 * the caller leaves the draft alone. 36 */ 37export function completePath(word: string, cwd: string): string | null { 38 if (!PATH_LIKE.test(word)) return null; 39 40 // Word = dirPart + prefix, split at the last slash, slash kept with dirPart. 41 const slash = word.lastIndexOf("/"); 42 const prefix = word.slice(slash + 1); 43 44 // Resolve dirPart to the directory to list. `~` means homedir() here and 45 // nowhere else: the returned word below reuses the user's shorthand verbatim 46 // (`~/Doc⇥` stays `~/Documents/`), so this resolve only locates the listing. 47 // Concatenate before resolving — `resolve(home, "/x")` would reset to root. 48 let dir: string; 49 if (word.startsWith("~")) dir = resolve(homedir() + word.slice(1, slash)); 50 else if (slash === 0) dir = "/"; 51 else dir = resolve(cwd, word.slice(0, slash)); 52 53 let entries; 54 try { 55 entries = readdirSync(dir, { withFileTypes: true }); 56 } catch { 57 return null; // vanished, permission, or a file in the path: nothing better. 58 } 59 60 // Bash hides dotfiles unless the prefix itself starts with one (`~/.⇥` 61 // includes hidden entries). A directory completion grows a trailing `/` so 62 // the next Tab descends into it; a symlink to a directory counts as one — 63 // `withFileTypes` reports the link itself, so the target needs one stat. 64 const candidates = entries 65 .filter((entry) => entry.name.startsWith(prefix) && (prefix.startsWith(".") || !entry.name.startsWith("."))) 66 .map((entry) => (isDir(dir, entry) ? `${entry.name}/` : entry.name)); 67 if (candidates.length === 0) return null; 68 if (candidates.length === 1) { 69 // Worth returning only when it adds something the user did not already 70 // type (`renameTag` typed against the single file match `renameTag` → null). 71 return candidates[0] === prefix ? null : word.slice(0, word.length - prefix.length) + candidates[0]; 72 } 73 const shared = candidates.reduce(commonPrefix); 74 // Recombine over the untouched dirPart, so `~/Doc⇥` becomes `~/Documents/` 75 // and never `/home/xonecas/Documents/`; no exact-name short-circuit here. 76 return shared.length > prefix.length ? word.slice(0, word.length - prefix.length) + shared : null; 77} 78 79/** Case-sensitive longest common prefix; commands.ts uses it for command 80 * completion, completePath for path candidates. */ 81export function commonPrefix(a: string, b: string): string { 82 let i = 0; 83 while (i < a.length && i < b.length && a[i] === b[i]) i++; 84 return a.slice(0, i); 85}