@@ -2,16 +2,12 @@ import { join } from "node:path";
22 import * as c from "yoctocolors";
33
44 import { addPromptHistory, getPromptHistory } from "../session/db/index.ts";
5-import {
6- getAtCompletions,
7- getCommandCompletions,
8- getFilePathCompletions,
9-} from "./completions.ts";
105 import {
116 type ImageAttachment,
127 isImageFilename,
138 loadImageFile,
149 } from "./image-types.ts";
10+import { getInputCompletion } from "./input-completion.ts";
1511 import { terminal } from "./terminal-io.ts";
1612
1713 // ─── ANSI escape sequences ────────────────────────────────────────────────────
@@ -625,48 +621,24 @@ export async function readline(opts: { cwd?: string }): Promise<InputResult> {
625621
626622 if (raw === TAB) {
627623 const beforeCursor = buf.slice(0, cursor);
628-
629- // Apply a single completion or show multiple options
630- const applyCompletions = (
631- completions: string[],
632- replaceFrom: number,
633- ) => {
634- if (completions.length === 1 && completions[0]) {
635- const replacement = completions[0];
636- buf = buf.slice(0, replaceFrom) + replacement + buf.slice(cursor);
637- cursor = replaceFrom + replacement.length;
638- renderPrompt();
639- } else if (completions.length > 1) {
640- process.stdout.write("\n");
641- for (const item of completions) process.stdout.write(` ${item}\n`);
642- renderPrompt();
624+ const completion = await getInputCompletion(beforeCursor, cursor, cwd);
625+
626+ if (completion?.completions.length === 1 && completion.completions[0]) {
627+ const replacement = completion.completions[0];
628+ buf =
629+ buf.slice(0, completion.replaceFrom) +
630+ replacement +
631+ buf.slice(cursor);
632+ cursor = completion.replaceFrom + replacement.length;
633+ renderPrompt();
634+ } else if ((completion?.completions.length ?? 0) > 1) {
635+ process.stdout.write("\n");
636+ for (const item of completion?.completions ?? []) {
637+ process.stdout.write(` ${item}\n`);
643638 }
644- };
645-
646- // 1) Command completion: `/...`
647- if (beforeCursor.startsWith("/")) {
648- const completions = getCommandCompletions(beforeCursor, cwd);
649- applyCompletions(completions, 0);
650- continue;
651- }
652-
653- // 2) @ reference completion: `@...`
654- const atMatch = beforeCursor.match(/@(\S*)$/);
655- if (atMatch) {
656- const completions = await getAtCompletions(atMatch[0], cwd);
657- applyCompletions(completions, cursor - (atMatch[0] ?? "").length);
658- continue;
639+ renderPrompt();
659640 }
660641
661- // 3) Bare file path completion: complete the current word as a path
662- const wordMatch = beforeCursor.match(/(\S+)$/);
663- if (wordMatch) {
664- const completions = await getFilePathCompletions(
665- wordMatch[1] ?? "",
666- cwd,
667- );
668- applyCompletions(completions, cursor - (wordMatch[1] ?? "").length);
669- }
670642 continue;
671643 }