@nghyane/arcane-tui 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,772 @@
1
+ import * as fs from "node:fs";
2
+ import * as os from "node:os";
3
+ import * as path from "node:path";
4
+ import { fuzzyFind } from "@nghyane/arcane-natives";
5
+ import { getProjectDir } from "@nghyane/arcane-utils/dirs";
6
+
7
+ const PATH_DELIMITERS = new Set([" ", "\t", '"', "'", "="]);
8
+
9
+ function buildAutocompleteFuzzyDiscoveryProfile(
10
+ query: string,
11
+ basePath: string,
12
+ ): {
13
+ query: string;
14
+ path: string;
15
+ maxResults: number;
16
+ hidden: boolean;
17
+ gitignore: boolean;
18
+ cache: boolean;
19
+ } {
20
+ return {
21
+ query,
22
+ path: basePath,
23
+ maxResults: 100,
24
+ hidden: true,
25
+ gitignore: true,
26
+ cache: true,
27
+ };
28
+ }
29
+
30
+ function findLastDelimiter(text: string): number {
31
+ for (let i = text.length - 1; i >= 0; i -= 1) {
32
+ if (PATH_DELIMITERS.has(text[i] ?? "")) {
33
+ return i;
34
+ }
35
+ }
36
+ return -1;
37
+ }
38
+
39
+ function findUnclosedQuoteStart(text: string): number | null {
40
+ let inQuotes = false;
41
+ let quoteStart = -1;
42
+
43
+ for (let i = 0; i < text.length; i += 1) {
44
+ if (text[i] === '"') {
45
+ inQuotes = !inQuotes;
46
+ if (inQuotes) {
47
+ quoteStart = i;
48
+ }
49
+ }
50
+ }
51
+
52
+ return inQuotes ? quoteStart : null;
53
+ }
54
+
55
+ function isTokenStart(text: string, index: number): boolean {
56
+ return index === 0 || PATH_DELIMITERS.has(text[index - 1] ?? "");
57
+ }
58
+
59
+ function extractQuotedPrefix(text: string): string | null {
60
+ const quoteStart = findUnclosedQuoteStart(text);
61
+ if (quoteStart === null) {
62
+ return null;
63
+ }
64
+
65
+ if (quoteStart > 0 && text[quoteStart - 1] === "@") {
66
+ if (!isTokenStart(text, quoteStart - 1)) {
67
+ return null;
68
+ }
69
+ return text.slice(quoteStart - 1);
70
+ }
71
+
72
+ if (!isTokenStart(text, quoteStart)) {
73
+ return null;
74
+ }
75
+
76
+ return text.slice(quoteStart);
77
+ }
78
+
79
+ function parsePathPrefix(prefix: string): { rawPrefix: string; isAtPrefix: boolean; isQuotedPrefix: boolean } {
80
+ if (prefix.startsWith('@"')) {
81
+ return { rawPrefix: prefix.slice(2), isAtPrefix: true, isQuotedPrefix: true };
82
+ }
83
+ if (prefix.startsWith('"')) {
84
+ return { rawPrefix: prefix.slice(1), isAtPrefix: false, isQuotedPrefix: true };
85
+ }
86
+ if (prefix.startsWith("@")) {
87
+ return { rawPrefix: prefix.slice(1), isAtPrefix: true, isQuotedPrefix: false };
88
+ }
89
+ return { rawPrefix: prefix, isAtPrefix: false, isQuotedPrefix: false };
90
+ }
91
+
92
+ function buildCompletionValue(
93
+ path: string,
94
+ options: { isDirectory: boolean; isAtPrefix: boolean; isQuotedPrefix: boolean },
95
+ ): string {
96
+ const needsQuotes = options.isQuotedPrefix || path.includes(" ");
97
+ const prefix = options.isAtPrefix ? "@" : "";
98
+
99
+ if (!needsQuotes) {
100
+ return `${prefix}${path}`;
101
+ }
102
+
103
+ const openQuote = `${prefix}"`;
104
+ const closeQuote = options.isDirectory ? "" : '"';
105
+ return `${openQuote}${path}${closeQuote}`;
106
+ }
107
+
108
+ /**
109
+ * Check if query is a subsequence of target (fuzzy match).
110
+ * "wig" matches "skill:wig" because w-i-g appear in order.
111
+ */
112
+ function fuzzyMatch(query: string, target: string): boolean {
113
+ if (query.length === 0) return true;
114
+ if (query.length > target.length) return false;
115
+
116
+ let qi = 0;
117
+ for (let ti = 0; ti < target.length && qi < query.length; ti++) {
118
+ if (query[qi] === target[ti]) qi++;
119
+ }
120
+ return qi === query.length;
121
+ }
122
+
123
+ /**
124
+ * Score a fuzzy match. Higher = better match.
125
+ * Prioritizes: exact match > starts-with > contains > subsequence
126
+ */
127
+ function fuzzyScore(query: string, target: string): number {
128
+ if (query.length === 0) return 1;
129
+ if (target === query) return 100;
130
+ if (target.startsWith(query)) return 80;
131
+ if (target.includes(query)) return 60;
132
+
133
+ // Subsequence match - score by how "tight" the match is
134
+ // (fewer gaps between matched characters = higher score)
135
+ let qi = 0;
136
+ let gaps = 0;
137
+ let lastMatchIdx = -1;
138
+ for (let ti = 0; ti < target.length && qi < query.length; ti++) {
139
+ if (query[qi] === target[ti]) {
140
+ if (lastMatchIdx >= 0 && ti - lastMatchIdx > 1) gaps++;
141
+ lastMatchIdx = ti;
142
+ qi++;
143
+ }
144
+ }
145
+ if (qi !== query.length) return 0;
146
+
147
+ // Base score 40 for subsequence, minus penalty for gaps
148
+ return Math.max(1, 40 - gaps * 5);
149
+ }
150
+
151
+ export interface AutocompleteItem {
152
+ value: string;
153
+ label: string;
154
+ description?: string;
155
+ /** Dim hint text shown inline after cursor when this item is selected */
156
+ hint?: string;
157
+ }
158
+
159
+ export interface SlashCommand {
160
+ name: string;
161
+ description?: string;
162
+ // Function to get argument completions for this command
163
+ // Returns null if no argument completion is available
164
+ getArgumentCompletions?(argumentPrefix: string): AutocompleteItem[] | null;
165
+ /** Return inline hint text for the current argument state (shown as dim ghost text after cursor) */
166
+ getInlineHint?(argumentText: string): string | null;
167
+ }
168
+
169
+ export interface AutocompleteProvider {
170
+ /** Get autocomplete suggestions for current text/cursor position */
171
+ getSuggestions(
172
+ lines: string[],
173
+ cursorLine: number,
174
+ cursorCol: number,
175
+ ): Promise<{
176
+ items: AutocompleteItem[];
177
+ prefix: string; // What we're matching against (e.g., "/" or "src/")
178
+ } | null>;
179
+
180
+ /** Apply the selected item and return new text + cursor position */
181
+ applyCompletion(
182
+ lines: string[],
183
+ cursorLine: number,
184
+ cursorCol: number,
185
+ item: AutocompleteItem,
186
+ prefix: string,
187
+ ): {
188
+ lines: string[];
189
+ cursorLine: number;
190
+ cursorCol: number;
191
+ };
192
+
193
+ /** Get inline hint text to show as dim ghost text after the cursor */
194
+ getInlineHint?(lines: string[], cursorLine: number, cursorCol: number): string | null;
195
+ }
196
+
197
+ // Combined provider that handles both slash commands and file paths.
198
+ export class CombinedAutocompleteProvider implements AutocompleteProvider {
199
+ #commands: (SlashCommand | AutocompleteItem)[];
200
+ #basePath: string;
201
+ // Intentionally separate from arcane-natives cache: this cache is a local,
202
+ // per-directory readdir fast-path for prefix completions. Global fuzzy
203
+ // discovery continues to use native fuzzyFind + shared scan cache.
204
+ #dirCache: Map<string, { entries: fs.Dirent[]; timestamp: number }> = new Map();
205
+ readonly #DIR_CACHE_TTL = 2000; // 2 seconds
206
+
207
+ constructor(commands: (SlashCommand | AutocompleteItem)[] = [], basePath: string = getProjectDir()) {
208
+ this.#commands = commands;
209
+ this.#basePath = basePath;
210
+ }
211
+
212
+ async getSuggestions(
213
+ lines: string[],
214
+ cursorLine: number,
215
+ cursorCol: number,
216
+ ): Promise<{ items: AutocompleteItem[]; prefix: string } | null> {
217
+ const currentLine = lines[cursorLine] || "";
218
+ const textBeforeCursor = currentLine.slice(0, cursorCol);
219
+
220
+ // Check for @ file reference (fuzzy search) - must be after a delimiter or at start
221
+ const atPrefix = this.#extractAtPrefix(textBeforeCursor);
222
+ if (atPrefix) {
223
+ const { rawPrefix, isQuotedPrefix } = parsePathPrefix(atPrefix);
224
+ const suggestions =
225
+ rawPrefix.length > 0
226
+ ? await this.#getFuzzyFileSuggestions(rawPrefix, { isQuotedPrefix })
227
+ : await this.#getFileSuggestions("@");
228
+ if (suggestions.length === 0 && rawPrefix.length > 0) {
229
+ const fallback = await this.#getFileSuggestions(atPrefix);
230
+ if (fallback.length === 0) return null;
231
+ return { items: fallback, prefix: atPrefix };
232
+ }
233
+ if (suggestions.length === 0) return null;
234
+
235
+ return {
236
+ items: suggestions,
237
+ prefix: atPrefix,
238
+ };
239
+ }
240
+
241
+ // Check for slash commands
242
+ if (textBeforeCursor.startsWith("/")) {
243
+ const spaceIndex = textBeforeCursor.indexOf(" ");
244
+
245
+ if (spaceIndex === -1) {
246
+ // No space yet - complete command names
247
+ const prefix = textBeforeCursor.slice(1); // Remove the "/"
248
+ const lowerPrefix = prefix.toLowerCase();
249
+
250
+ // Filter commands using fuzzy matching (subsequence match)
251
+ const matches = this.#commands
252
+ .filter(cmd => {
253
+ const name = "name" in cmd ? cmd.name : cmd.value;
254
+ if (!name) return false;
255
+ // Match name or description
256
+ if (fuzzyMatch(lowerPrefix, name.toLowerCase())) return true;
257
+ const desc = cmd.description?.toLowerCase();
258
+ return desc ? fuzzyMatch(lowerPrefix, desc) : false;
259
+ })
260
+ .map(cmd => {
261
+ const name = "name" in cmd ? cmd.name : cmd.value;
262
+ const lowerName = name?.toLowerCase() ?? "";
263
+ const lowerDesc = cmd.description?.toLowerCase() ?? "";
264
+ // Score name matches higher than description matches
265
+ const nameScore = fuzzyMatch(lowerPrefix, lowerName) ? fuzzyScore(lowerPrefix, lowerName) : 0;
266
+ const descScore = fuzzyMatch(lowerPrefix, lowerDesc) ? fuzzyScore(lowerPrefix, lowerDesc) * 0.5 : 0;
267
+ return {
268
+ value: name,
269
+ label: "name" in cmd ? cmd.name : cmd.label,
270
+ score: Math.max(nameScore, descScore),
271
+ ...(cmd.description && { description: cmd.description }),
272
+ };
273
+ })
274
+ .sort((a, b) => b.score - a.score)
275
+ .map(({ score: _, ...rest }) => rest);
276
+
277
+ if (matches.length === 0) return null;
278
+
279
+ return {
280
+ items: matches,
281
+ prefix: textBeforeCursor,
282
+ };
283
+ } else {
284
+ // Space found - complete command arguments
285
+ const commandName = textBeforeCursor.slice(1, spaceIndex); // Command without "/"
286
+ const argumentText = textBeforeCursor.slice(spaceIndex + 1); // Text after space
287
+
288
+ const command = this.#commands.find(cmd => {
289
+ const name = "name" in cmd ? cmd.name : cmd.value;
290
+ return name === commandName;
291
+ });
292
+ if (!command || !("getArgumentCompletions" in command) || !command.getArgumentCompletions) {
293
+ return null; // No argument completion for this command
294
+ }
295
+
296
+ const argumentSuggestions = command.getArgumentCompletions(argumentText);
297
+ if (!argumentSuggestions || argumentSuggestions.length === 0) {
298
+ return null;
299
+ }
300
+
301
+ return {
302
+ items: argumentSuggestions,
303
+ prefix: argumentText,
304
+ };
305
+ }
306
+ }
307
+
308
+ // Check for file paths - triggered by Tab or if we detect a path pattern
309
+ const pathMatch = this.#extractPathPrefix(textBeforeCursor, false);
310
+
311
+ if (pathMatch !== null) {
312
+ const suggestions = await this.#getFileSuggestions(pathMatch);
313
+ if (suggestions.length === 0) return null;
314
+
315
+ // Check if we have an exact match that is a directory
316
+ // In that case, we might want to return suggestions for the directory content instead
317
+ // But only if the prefix ends with /
318
+ if (suggestions.length === 1 && suggestions[0]?.value === pathMatch && !pathMatch.endsWith("/")) {
319
+ // Exact match found (e.g. user typed "src" and "src/" is the only match)
320
+ // We still return it so user can select it and add /
321
+ return {
322
+ items: suggestions,
323
+ prefix: pathMatch,
324
+ };
325
+ }
326
+
327
+ return {
328
+ items: suggestions,
329
+ prefix: pathMatch,
330
+ };
331
+ }
332
+
333
+ return null;
334
+ }
335
+
336
+ applyCompletion(
337
+ lines: string[],
338
+ cursorLine: number,
339
+ cursorCol: number,
340
+ item: AutocompleteItem,
341
+ prefix: string,
342
+ ): { lines: string[]; cursorLine: number; cursorCol: number } {
343
+ const currentLine = lines[cursorLine] || "";
344
+ const beforePrefix = currentLine.slice(0, cursorCol - prefix.length);
345
+ const afterCursor = currentLine.slice(cursorCol);
346
+
347
+ // Check if we're completing a slash command (prefix starts with "/" but NOT a file path)
348
+ // Slash commands are at the start of the line and don't contain path separators after the first /
349
+ const isSlashCommand = prefix.startsWith("/") && beforePrefix.trim() === "" && !prefix.slice(1).includes("/");
350
+ if (isSlashCommand) {
351
+ // This is a command name completion
352
+ const newLine = `${beforePrefix}/${item.value} ${afterCursor}`;
353
+ const newLines = [...lines];
354
+ newLines[cursorLine] = newLine;
355
+
356
+ return {
357
+ lines: newLines,
358
+ cursorLine,
359
+ cursorCol: beforePrefix.length + item.value.length + 2, // +2 for "/" and space
360
+ };
361
+ }
362
+
363
+ // Check if we're completing a file attachment (prefix starts with "@")
364
+ if (prefix.startsWith("@")) {
365
+ // This is a file attachment completion
366
+ const newLine = `${beforePrefix + item.value} ${afterCursor}`;
367
+ const newLines = [...lines];
368
+ newLines[cursorLine] = newLine;
369
+
370
+ return {
371
+ lines: newLines,
372
+ cursorLine,
373
+ cursorCol: beforePrefix.length + item.value.length + 1, // +1 for space
374
+ };
375
+ }
376
+
377
+ // Check if we're in a slash command context (beforePrefix contains "/command ")
378
+ const textBeforeCursor = currentLine.slice(0, cursorCol);
379
+ if (textBeforeCursor.includes("/") && textBeforeCursor.includes(" ")) {
380
+ // This is likely a command argument completion
381
+ const newLine = beforePrefix + item.value + afterCursor;
382
+ const newLines = [...lines];
383
+ newLines[cursorLine] = newLine;
384
+
385
+ return {
386
+ lines: newLines,
387
+ cursorLine,
388
+ cursorCol: beforePrefix.length + item.value.length,
389
+ };
390
+ }
391
+
392
+ // For file paths, complete the path
393
+ const newLine = beforePrefix + item.value + afterCursor;
394
+ const newLines = [...lines];
395
+ newLines[cursorLine] = newLine;
396
+
397
+ return {
398
+ lines: newLines,
399
+ cursorLine,
400
+ cursorCol: beforePrefix.length + item.value.length,
401
+ };
402
+ }
403
+
404
+ // Extract @ prefix for fuzzy file suggestions
405
+ #extractAtPrefix(text: string): string | null {
406
+ const quotedPrefix = extractQuotedPrefix(text);
407
+ if (quotedPrefix?.startsWith('@"')) {
408
+ return quotedPrefix;
409
+ }
410
+
411
+ const lastDelimiterIndex = findLastDelimiter(text);
412
+ const tokenStart = lastDelimiterIndex === -1 ? 0 : lastDelimiterIndex + 1;
413
+
414
+ if (text[tokenStart] === "@") {
415
+ return text.slice(tokenStart);
416
+ }
417
+
418
+ return null;
419
+ }
420
+
421
+ // Extract a path-like prefix from the text before cursor
422
+ #extractPathPrefix(text: string, forceExtract: boolean = false): string | null {
423
+ const quotedPrefix = extractQuotedPrefix(text);
424
+ if (quotedPrefix) {
425
+ return quotedPrefix;
426
+ }
427
+
428
+ const lastDelimiterIndex = findLastDelimiter(text);
429
+ const pathPrefix = lastDelimiterIndex === -1 ? text : text.slice(lastDelimiterIndex + 1);
430
+
431
+ // For forced extraction (Tab key), always return something
432
+ if (forceExtract) {
433
+ return pathPrefix;
434
+ }
435
+
436
+ // For natural triggers, return if it looks like a path, ends with /, starts with ~/, .
437
+ // Only return empty string if the text looks like it's starting a path context
438
+ if (pathPrefix.includes("/") || pathPrefix.startsWith(".") || pathPrefix.startsWith("~/")) {
439
+ return pathPrefix;
440
+ }
441
+
442
+ // Return empty string only after a space (not for completely empty text)
443
+ // Empty text should not trigger file suggestions - that's for forced Tab completion
444
+ if (pathPrefix === "" && text.endsWith(" ")) {
445
+ return pathPrefix;
446
+ }
447
+
448
+ return null;
449
+ }
450
+
451
+ // Expand home directory (~/) to actual home path
452
+ #expandHomePath(filePath: string): string {
453
+ if (filePath.startsWith("~/")) {
454
+ const expandedPath = path.join(os.homedir(), filePath.slice(2));
455
+ // Preserve trailing slash if original path had one
456
+ return filePath.endsWith("/") && !expandedPath.endsWith("/") ? `${expandedPath}/` : expandedPath;
457
+ } else if (filePath === "~") {
458
+ return os.homedir();
459
+ }
460
+ return filePath;
461
+ }
462
+
463
+ async #resolveScopedFuzzyQuery(
464
+ rawQuery: string,
465
+ ): Promise<{ baseDir: string; query: string; displayBase: string } | null> {
466
+ const slashIndex = rawQuery.lastIndexOf("/");
467
+ if (slashIndex === -1) {
468
+ return null;
469
+ }
470
+
471
+ const displayBase = rawQuery.slice(0, slashIndex + 1);
472
+ const query = rawQuery.slice(slashIndex + 1);
473
+
474
+ let baseDir: string;
475
+ if (displayBase.startsWith("~/")) {
476
+ baseDir = this.#expandHomePath(displayBase);
477
+ } else if (displayBase.startsWith("/")) {
478
+ baseDir = displayBase;
479
+ } else {
480
+ baseDir = path.join(this.#basePath, displayBase);
481
+ }
482
+
483
+ try {
484
+ if (!(await fs.promises.stat(baseDir)).isDirectory()) {
485
+ return null;
486
+ }
487
+ } catch {
488
+ return null;
489
+ }
490
+
491
+ return { baseDir, query, displayBase };
492
+ }
493
+
494
+ #scopedPathForDisplay(displayBase: string, relativePath: string): string {
495
+ if (displayBase === "/") {
496
+ return `/${relativePath}`;
497
+ }
498
+ return `${displayBase}${relativePath}`;
499
+ }
500
+
501
+ async #getCachedDirEntries(searchDir: string): Promise<fs.Dirent[]> {
502
+ const now = Date.now();
503
+ const cached = this.#dirCache.get(searchDir);
504
+
505
+ if (cached && now - cached.timestamp < this.#DIR_CACHE_TTL) {
506
+ return cached.entries;
507
+ }
508
+
509
+ const entries = await fs.promises.readdir(searchDir, { withFileTypes: true });
510
+ this.#dirCache.set(searchDir, { entries, timestamp: now });
511
+
512
+ if (this.#dirCache.size > 100) {
513
+ const sortedKeys = [...this.#dirCache.entries()]
514
+ .sort((a, b) => a[1].timestamp - b[1].timestamp)
515
+ .slice(0, 50)
516
+ .map(([key]) => key);
517
+ for (const key of sortedKeys) {
518
+ this.#dirCache.delete(key);
519
+ }
520
+ }
521
+
522
+ return entries;
523
+ }
524
+
525
+ invalidateDirCache(dir?: string): void {
526
+ if (dir) {
527
+ this.#dirCache.delete(dir);
528
+ } else {
529
+ this.#dirCache.clear();
530
+ }
531
+ }
532
+
533
+ // Get file/directory suggestions for a given path prefix
534
+ async #getFileSuggestions(prefix: string): Promise<AutocompleteItem[]> {
535
+ try {
536
+ let searchDir: string;
537
+ let searchPrefix: string;
538
+ const { rawPrefix, isAtPrefix, isQuotedPrefix } = parsePathPrefix(prefix);
539
+ let expandedPrefix = rawPrefix;
540
+
541
+ // Handle home directory expansion
542
+ if (expandedPrefix.startsWith("~")) {
543
+ expandedPrefix = this.#expandHomePath(expandedPrefix);
544
+ }
545
+
546
+ const isRootPrefix =
547
+ rawPrefix === "" ||
548
+ rawPrefix === "./" ||
549
+ rawPrefix === "../" ||
550
+ rawPrefix === "~" ||
551
+ rawPrefix === "~/" ||
552
+ rawPrefix === "/" ||
553
+ (isAtPrefix && rawPrefix === "");
554
+
555
+ if (isRootPrefix) {
556
+ // Complete from specified position
557
+ if (rawPrefix.startsWith("~") || expandedPrefix.startsWith("/")) {
558
+ searchDir = expandedPrefix;
559
+ } else {
560
+ searchDir = path.join(this.#basePath, expandedPrefix);
561
+ }
562
+ searchPrefix = "";
563
+ } else if (rawPrefix.endsWith("/")) {
564
+ // If prefix ends with /, show contents of that directory
565
+ if (rawPrefix.startsWith("~") || expandedPrefix.startsWith("/")) {
566
+ searchDir = expandedPrefix;
567
+ } else {
568
+ searchDir = path.join(this.#basePath, expandedPrefix);
569
+ }
570
+ searchPrefix = "";
571
+ } else {
572
+ // Split into directory and file prefix
573
+ const dir = path.dirname(expandedPrefix);
574
+ const file = path.basename(expandedPrefix);
575
+ if (rawPrefix.startsWith("~") || expandedPrefix.startsWith("/")) {
576
+ searchDir = dir;
577
+ } else {
578
+ searchDir = path.join(this.#basePath, dir);
579
+ }
580
+ searchPrefix = file;
581
+ }
582
+
583
+ const entries = await this.#getCachedDirEntries(searchDir);
584
+ const suggestions: AutocompleteItem[] = [];
585
+
586
+ for (const entry of entries) {
587
+ if (!entry.name.toLowerCase().startsWith(searchPrefix.toLowerCase())) {
588
+ continue;
589
+ }
590
+ // Skip .git directory
591
+ if (entry.name === ".git") {
592
+ continue;
593
+ }
594
+
595
+ // Check if entry is a directory (or a symlink pointing to a directory)
596
+ let isDirectory = entry.isDirectory();
597
+ if (!isDirectory && entry.isSymbolicLink()) {
598
+ try {
599
+ const fullPath = path.join(searchDir, entry.name);
600
+ isDirectory = (await fs.promises.stat(fullPath)).isDirectory();
601
+ } catch {
602
+ // Broken symlink, file deleted between readdir and stat, or permission error
603
+ continue;
604
+ }
605
+ }
606
+
607
+ let relativePath: string;
608
+ const name = entry.name;
609
+ const displayPrefix = rawPrefix;
610
+
611
+ if (displayPrefix.endsWith("/")) {
612
+ // If prefix ends with /, append entry to the prefix
613
+ relativePath = displayPrefix + name;
614
+ } else if (displayPrefix.includes("/")) {
615
+ // Preserve ~/ format for home directory paths
616
+ if (displayPrefix.startsWith("~/")) {
617
+ const homeRelativeDir = displayPrefix.slice(2); // Remove ~/
618
+ const dir = path.dirname(homeRelativeDir);
619
+ relativePath = `~/${dir === "." ? name : path.join(dir, name)}`;
620
+ } else if (displayPrefix.startsWith("/")) {
621
+ // Absolute path - construct properly
622
+ const dir = path.dirname(displayPrefix);
623
+ if (dir === "/") {
624
+ relativePath = `/${name}`;
625
+ } else {
626
+ relativePath = `${dir}/${name}`;
627
+ }
628
+ } else {
629
+ relativePath = path.join(path.dirname(displayPrefix), name);
630
+ }
631
+ } else {
632
+ // For standalone entries, preserve ~/ if original prefix was ~/
633
+ if (displayPrefix.startsWith("~")) {
634
+ relativePath = `~/${name}`;
635
+ } else {
636
+ relativePath = name;
637
+ }
638
+ }
639
+
640
+ const pathValue = isDirectory ? `${relativePath}/` : relativePath;
641
+ const value = buildCompletionValue(pathValue, {
642
+ isDirectory,
643
+ isAtPrefix,
644
+ isQuotedPrefix,
645
+ });
646
+
647
+ suggestions.push({
648
+ value,
649
+ label: name + (isDirectory ? "/" : ""),
650
+ });
651
+ }
652
+
653
+ // Sort directories first, then alphabetically
654
+ suggestions.sort((a, b) => {
655
+ const aIsDir = a.value.endsWith("/");
656
+ const bIsDir = b.value.endsWith("/");
657
+ if (aIsDir && !bIsDir) return -1;
658
+ if (!aIsDir && bIsDir) return 1;
659
+ return a.label.localeCompare(b.label);
660
+ });
661
+
662
+ return suggestions;
663
+ } catch {
664
+ // Directory doesn't exist or not accessible
665
+ return [];
666
+ }
667
+ }
668
+
669
+ async #getFuzzyFileSuggestions(query: string, options: { isQuotedPrefix: boolean }): Promise<AutocompleteItem[]> {
670
+ try {
671
+ const scopedQuery = await this.#resolveScopedFuzzyQuery(query);
672
+ const searchPath = scopedQuery?.baseDir ?? this.#basePath;
673
+ const fuzzyQuery = scopedQuery?.query ?? query;
674
+ const result = await fuzzyFind(buildAutocompleteFuzzyDiscoveryProfile(fuzzyQuery, searchPath));
675
+ const filteredMatches = result.matches.filter(entry => {
676
+ const p = entry.path.endsWith("/") ? entry.path.slice(0, -1) : entry.path;
677
+ const normalized = p.replaceAll("\\", "/");
678
+ return !/(^|\/)\.git(\/|$)/.test(normalized);
679
+ });
680
+ const topEntries = filteredMatches.slice(0, 20);
681
+ const suggestions: AutocompleteItem[] = [];
682
+ for (const { path: entryPath, isDirectory } of topEntries) {
683
+ const pathWithoutSlash = isDirectory ? entryPath.slice(0, -1) : entryPath;
684
+ const displayPath = scopedQuery
685
+ ? this.#scopedPathForDisplay(scopedQuery.displayBase, pathWithoutSlash)
686
+ : pathWithoutSlash;
687
+ const entryName = path.basename(pathWithoutSlash);
688
+ const completionPath = isDirectory ? `${displayPath}/` : displayPath;
689
+ const value = buildCompletionValue(completionPath, {
690
+ isDirectory,
691
+ isAtPrefix: true,
692
+ isQuotedPrefix: options.isQuotedPrefix,
693
+ });
694
+ suggestions.push({
695
+ value,
696
+ label: entryName + (isDirectory ? "/" : ""),
697
+ description: displayPath,
698
+ });
699
+ }
700
+ return suggestions;
701
+ } catch {
702
+ return [];
703
+ }
704
+ }
705
+
706
+ // Force file completion (called on Tab key) - always returns suggestions
707
+ async getForceFileSuggestions(
708
+ lines: string[],
709
+ cursorLine: number,
710
+ cursorCol: number,
711
+ ): Promise<{ items: AutocompleteItem[]; prefix: string } | null> {
712
+ const currentLine = lines[cursorLine] || "";
713
+ const textBeforeCursor = currentLine.slice(0, cursorCol);
714
+
715
+ // Don't trigger if we're typing a slash command at the start of the line
716
+ if (textBeforeCursor.trim().startsWith("/") && !textBeforeCursor.trim().includes(" ")) {
717
+ return null;
718
+ }
719
+
720
+ // Force extract path prefix - this will always return something
721
+ const pathMatch = this.#extractPathPrefix(textBeforeCursor, true);
722
+ if (pathMatch !== null) {
723
+ const suggestions = await this.#getFileSuggestions(pathMatch);
724
+ if (suggestions.length === 0) return null;
725
+
726
+ return {
727
+ items: suggestions,
728
+ prefix: pathMatch,
729
+ };
730
+ }
731
+
732
+ return null;
733
+ }
734
+
735
+ // Check if we should trigger file completion (called on Tab key)
736
+ shouldTriggerFileCompletion(lines: string[], cursorLine: number, cursorCol: number): boolean {
737
+ const currentLine = lines[cursorLine] || "";
738
+ const textBeforeCursor = currentLine.slice(0, cursorCol);
739
+
740
+ // Don't trigger if we're typing a slash command at the start of the line
741
+ if (textBeforeCursor.trim().startsWith("/") && !textBeforeCursor.trim().includes(" ")) {
742
+ return false;
743
+ }
744
+
745
+ return true;
746
+ }
747
+
748
+ /** Get inline hint text for slash commands with subcommand hints */
749
+ getInlineHint(lines: string[], cursorLine: number, cursorCol: number): string | null {
750
+ const currentLine = lines[cursorLine] || "";
751
+ const textBeforeCursor = currentLine.slice(0, cursorCol);
752
+
753
+ if (!textBeforeCursor.startsWith("/")) return null;
754
+
755
+ const spaceIndex = textBeforeCursor.indexOf(" ");
756
+ if (spaceIndex === -1) return null;
757
+
758
+ const commandName = textBeforeCursor.slice(1, spaceIndex);
759
+ const argumentText = textBeforeCursor.slice(spaceIndex + 1);
760
+
761
+ const command = this.#commands.find(cmd => {
762
+ const name = "name" in cmd ? cmd.name : cmd.value;
763
+ return name === commandName;
764
+ });
765
+
766
+ if (!command || !("getInlineHint" in command) || !command.getInlineHint) {
767
+ return null;
768
+ }
769
+
770
+ return command.getInlineHint(argumentText);
771
+ }
772
+ }