xonecas.io
64875
Mc

mini-coder / commit e716b2d

feat: path completion in the editor

xonecas2026-09-26 e716b2dd048bc2a92d43bacb52e2793e1b76397d parent b2efeef

4 files changed

src/tui/commands.ts+2−5
@@ -1,4 +1,5 @@
11 import { dim } from "./styles.ts";
2+import { commonPrefix } from "./complete.ts";
23
34 export interface CommandContext {
45 /** Appends already-styled lines to scrollback. */
@@ -18,6 +19,7 @@ const KEYBINDINGS: [string, string][] = [
1819 ["Esc", "pause the turn at the next step boundary"],
1920 ["Ctrl+C", "cancel the turn"],
2021 ["Ctrl+D", "exit on an empty draft"],
22+ ["Tab", "complete path (after command completion)"],
2123 ];
2224
2325 /** One aligned `key description` block; the key column is dimmed. */
@@ -56,8 +58,3 @@ export function completeCommand(draft: string): string | null {
5658 return shared === typed ? null : `/${shared}`;
5759 }
5860
59-function commonPrefix(a: string, b: string): string {
60- let i = 0;
61- while (i < a.length && i < b.length && a[i] === b[i]) i++;
62- return a.slice(0, i);
63-}
src/tui/complete.ts+85−0
@@ -0,0 +1,85 @@
1+import { readdirSync, statSync, type Dirent } from "node:fs";
2+import { homedir } from "node:os";
3+import { 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+ */
18+const PATH_LIKE = /^(?:~|\/|\.{1,2}(?:\/|$)|\w+\/)/;
19+
20+/** A directory, or a symlink that resolves to one (`statSync` follows). */
21+function 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+ */
37+export 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. */
81+export 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+}
src/tui/editor.ts+19−0
@@ -288,4 +288,23 @@ export class Editor {
288288 this.lines[this.row] = current.slice(0, start).join("") + current.slice(this.col).join("");
289289 this.col = start;
290290 }
291+
292+ /**
293+ * Applies `step` to the word ending at the caret, replacing it; false when
294+ * there is no word before the caret. Whitespace is `isSpace` (Unicode `\s`),
295+ * so a path word may carry `/`, `~`, `.`, `$` — no path-specific separator
296+ * set. No completion-undo: bash's TAB-_ restore is out; the user backspaces
297+ * or re-types instead, with Esc and the editor's own keys as recovery.
298+ */
299+ completeWord(step: (word: string) => string | null): boolean {
300+ const current = codePoints(this.lines[this.row]);
301+ const start = wordStart(current, this.col);
302+ if (start === this.col || isSpace(current[this.col - 1])) return false;
303+ const word = current.slice(start, this.col).join("");
304+ const completed = step(word);
305+ if (completed === null) return false;
306+ this.lines[this.row] = current.slice(0, start).join("") + completed + current.slice(this.col).join("");
307+ this.col = start + codePoints(completed).length;
308+ return true;
309+ }
291310 }
src/tui/tui.ts+4−1
@@ -4,6 +4,7 @@ import { runAgentTurn, type AgentEvent, type AgentOptions, type Phase } from "..
44 import { Terminal, expandTabs, sanitize, wrapLine, type Key } from "./term.ts";
55 import { Editor } from "./editor.ts";
66 import { completeCommand, findCommand, type CommandContext } from "./commands.ts";
7+import { completePath } from "./complete.ts";
78 import { MarkdownStream, TailStream, type BodyLine, type StreamRenderer } from "./stream.ts";
89 import { blue, cyan, dim, green, red, teal } from "./styles.ts";
910 import { DIFF_ADD, DIFF_DELETE, NORMAL_BG, sgrBg, sgrPlain } from "./theme.ts";
@@ -262,8 +263,10 @@ class Tui {
262263 const completed = completeCommand(this.editor.text());
263264 if (completed !== null) {
264265 this.editor.setText(completed);
265- this.render();
266+ } else {
267+ this.editor.completeWord((word) => completePath(word, process.cwd()));
266268 }
269+ this.render();
267270 return;
268271 }
269272 const result = this.editor.handle(key);