@lenylvt/pi-tui 0.62.5

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.
Files changed (127) hide show
  1. package/README.md +767 -0
  2. package/dist/autocomplete.d.ts +50 -0
  3. package/dist/autocomplete.d.ts.map +1 -0
  4. package/dist/autocomplete.js +623 -0
  5. package/dist/autocomplete.js.map +1 -0
  6. package/dist/components/box.d.ts +22 -0
  7. package/dist/components/box.d.ts.map +1 -0
  8. package/dist/components/box.js +104 -0
  9. package/dist/components/box.js.map +1 -0
  10. package/dist/components/cancellable-loader.d.ts +22 -0
  11. package/dist/components/cancellable-loader.d.ts.map +1 -0
  12. package/dist/components/cancellable-loader.js +35 -0
  13. package/dist/components/cancellable-loader.js.map +1 -0
  14. package/dist/components/editor.d.ts +244 -0
  15. package/dist/components/editor.d.ts.map +1 -0
  16. package/dist/components/editor.js +1861 -0
  17. package/dist/components/editor.js.map +1 -0
  18. package/dist/components/image.d.ts +28 -0
  19. package/dist/components/image.d.ts.map +1 -0
  20. package/dist/components/image.js +69 -0
  21. package/dist/components/image.js.map +1 -0
  22. package/dist/components/input.d.ts +37 -0
  23. package/dist/components/input.d.ts.map +1 -0
  24. package/dist/components/input.js +426 -0
  25. package/dist/components/input.js.map +1 -0
  26. package/dist/components/loader.d.ts +21 -0
  27. package/dist/components/loader.d.ts.map +1 -0
  28. package/dist/components/loader.js +49 -0
  29. package/dist/components/loader.js.map +1 -0
  30. package/dist/components/markdown.d.ts +95 -0
  31. package/dist/components/markdown.d.ts.map +1 -0
  32. package/dist/components/markdown.js +660 -0
  33. package/dist/components/markdown.js.map +1 -0
  34. package/dist/components/select-list.d.ts +50 -0
  35. package/dist/components/select-list.d.ts.map +1 -0
  36. package/dist/components/select-list.js +159 -0
  37. package/dist/components/select-list.js.map +1 -0
  38. package/dist/components/settings-list.d.ts +50 -0
  39. package/dist/components/settings-list.d.ts.map +1 -0
  40. package/dist/components/settings-list.js +185 -0
  41. package/dist/components/settings-list.js.map +1 -0
  42. package/dist/components/spacer.d.ts +12 -0
  43. package/dist/components/spacer.d.ts.map +1 -0
  44. package/dist/components/spacer.js +23 -0
  45. package/dist/components/spacer.js.map +1 -0
  46. package/dist/components/text.d.ts +19 -0
  47. package/dist/components/text.d.ts.map +1 -0
  48. package/dist/components/text.js +89 -0
  49. package/dist/components/text.js.map +1 -0
  50. package/dist/components/truncated-text.d.ts +13 -0
  51. package/dist/components/truncated-text.d.ts.map +1 -0
  52. package/dist/components/truncated-text.js +51 -0
  53. package/dist/components/truncated-text.js.map +1 -0
  54. package/dist/editor-component.d.ts +39 -0
  55. package/dist/editor-component.d.ts.map +1 -0
  56. package/dist/editor-component.js +2 -0
  57. package/dist/editor-component.js.map +1 -0
  58. package/dist/fuzzy.d.ts +16 -0
  59. package/dist/fuzzy.d.ts.map +1 -0
  60. package/dist/fuzzy.js +107 -0
  61. package/dist/fuzzy.js.map +1 -0
  62. package/dist/index.d.ts +23 -0
  63. package/dist/index.d.ts.map +1 -0
  64. package/dist/index.js +32 -0
  65. package/dist/index.js.map +1 -0
  66. package/dist/keybindings.d.ts +193 -0
  67. package/dist/keybindings.d.ts.map +1 -0
  68. package/dist/keybindings.js +174 -0
  69. package/dist/keybindings.js.map +1 -0
  70. package/dist/keys.d.ts +170 -0
  71. package/dist/keys.d.ts.map +1 -0
  72. package/dist/keys.js +1124 -0
  73. package/dist/keys.js.map +1 -0
  74. package/dist/kill-ring.d.ts +28 -0
  75. package/dist/kill-ring.d.ts.map +1 -0
  76. package/dist/kill-ring.js +44 -0
  77. package/dist/kill-ring.js.map +1 -0
  78. package/dist/stdin-buffer.d.ts +48 -0
  79. package/dist/stdin-buffer.d.ts.map +1 -0
  80. package/dist/stdin-buffer.js +317 -0
  81. package/dist/stdin-buffer.js.map +1 -0
  82. package/dist/terminal-image.d.ts +68 -0
  83. package/dist/terminal-image.d.ts.map +1 -0
  84. package/dist/terminal-image.js +288 -0
  85. package/dist/terminal-image.js.map +1 -0
  86. package/dist/terminal.d.ts +84 -0
  87. package/dist/terminal.d.ts.map +1 -0
  88. package/dist/terminal.js +285 -0
  89. package/dist/terminal.js.map +1 -0
  90. package/dist/tui.d.ts +218 -0
  91. package/dist/tui.d.ts.map +1 -0
  92. package/dist/tui.js +966 -0
  93. package/dist/tui.js.map +1 -0
  94. package/dist/undo-stack.d.ts +17 -0
  95. package/dist/undo-stack.d.ts.map +1 -0
  96. package/dist/undo-stack.js +25 -0
  97. package/dist/undo-stack.js.map +1 -0
  98. package/dist/utils.d.ts +78 -0
  99. package/dist/utils.d.ts.map +1 -0
  100. package/dist/utils.js +960 -0
  101. package/dist/utils.js.map +1 -0
  102. package/package.json +55 -0
  103. package/src/autocomplete.ts +771 -0
  104. package/src/components/box.ts +137 -0
  105. package/src/components/cancellable-loader.ts +40 -0
  106. package/src/components/editor.ts +2230 -0
  107. package/src/components/image.ts +104 -0
  108. package/src/components/input.ts +503 -0
  109. package/src/components/loader.ts +55 -0
  110. package/src/components/markdown.ts +820 -0
  111. package/src/components/select-list.ts +229 -0
  112. package/src/components/settings-list.ts +250 -0
  113. package/src/components/spacer.ts +28 -0
  114. package/src/components/text.ts +106 -0
  115. package/src/components/truncated-text.ts +65 -0
  116. package/src/editor-component.ts +74 -0
  117. package/src/fuzzy.ts +133 -0
  118. package/src/index.ts +104 -0
  119. package/src/keybindings.ts +244 -0
  120. package/src/keys.ts +1356 -0
  121. package/src/kill-ring.ts +46 -0
  122. package/src/stdin-buffer.ts +386 -0
  123. package/src/terminal-image.ts +381 -0
  124. package/src/terminal.ts +360 -0
  125. package/src/tui.ts +1200 -0
  126. package/src/undo-stack.ts +28 -0
  127. package/src/utils.ts +1068 -0
@@ -0,0 +1,771 @@
1
+ import { spawn } from "child_process";
2
+ import { readdirSync, statSync } from "fs";
3
+ import { homedir } from "os";
4
+ import { basename, dirname, join } from "path";
5
+ import { fuzzyFilter } from "./fuzzy.js";
6
+
7
+ const PATH_DELIMITERS = new Set([" ", "\t", '"', "'", "="]);
8
+
9
+ function toDisplayPath(value: string): string {
10
+ return value.replace(/\\/g, "/");
11
+ }
12
+
13
+ function escapeRegex(value: string): string {
14
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
15
+ }
16
+
17
+ function buildFdPathQuery(query: string): string {
18
+ const normalized = toDisplayPath(query);
19
+ if (!normalized.includes("/")) {
20
+ return normalized;
21
+ }
22
+
23
+ const hasTrailingSeparator = normalized.endsWith("/");
24
+ const trimmed = normalized.replace(/^\/+|\/+$/g, "");
25
+ if (!trimmed) {
26
+ return normalized;
27
+ }
28
+
29
+ const separatorPattern = "[\\\\/]";
30
+ const segments = trimmed
31
+ .split("/")
32
+ .filter(Boolean)
33
+ .map((segment) => escapeRegex(segment));
34
+ if (segments.length === 0) {
35
+ return normalized;
36
+ }
37
+
38
+ let pattern = segments.join(separatorPattern);
39
+ if (hasTrailingSeparator) {
40
+ pattern += separatorPattern;
41
+ }
42
+ return pattern;
43
+ }
44
+
45
+ function findLastDelimiter(text: string): number {
46
+ for (let i = text.length - 1; i >= 0; i -= 1) {
47
+ if (PATH_DELIMITERS.has(text[i] ?? "")) {
48
+ return i;
49
+ }
50
+ }
51
+ return -1;
52
+ }
53
+
54
+ function findUnclosedQuoteStart(text: string): number | null {
55
+ let inQuotes = false;
56
+ let quoteStart = -1;
57
+
58
+ for (let i = 0; i < text.length; i += 1) {
59
+ if (text[i] === '"') {
60
+ inQuotes = !inQuotes;
61
+ if (inQuotes) {
62
+ quoteStart = i;
63
+ }
64
+ }
65
+ }
66
+
67
+ return inQuotes ? quoteStart : null;
68
+ }
69
+
70
+ function isTokenStart(text: string, index: number): boolean {
71
+ return index === 0 || PATH_DELIMITERS.has(text[index - 1] ?? "");
72
+ }
73
+
74
+ function extractQuotedPrefix(text: string): string | null {
75
+ const quoteStart = findUnclosedQuoteStart(text);
76
+ if (quoteStart === null) {
77
+ return null;
78
+ }
79
+
80
+ if (quoteStart > 0 && text[quoteStart - 1] === "@") {
81
+ if (!isTokenStart(text, quoteStart - 1)) {
82
+ return null;
83
+ }
84
+ return text.slice(quoteStart - 1);
85
+ }
86
+
87
+ if (!isTokenStart(text, quoteStart)) {
88
+ return null;
89
+ }
90
+
91
+ return text.slice(quoteStart);
92
+ }
93
+
94
+ function parsePathPrefix(prefix: string): { rawPrefix: string; isAtPrefix: boolean; isQuotedPrefix: boolean } {
95
+ if (prefix.startsWith('@"')) {
96
+ return { rawPrefix: prefix.slice(2), isAtPrefix: true, isQuotedPrefix: true };
97
+ }
98
+ if (prefix.startsWith('"')) {
99
+ return { rawPrefix: prefix.slice(1), isAtPrefix: false, isQuotedPrefix: true };
100
+ }
101
+ if (prefix.startsWith("@")) {
102
+ return { rawPrefix: prefix.slice(1), isAtPrefix: true, isQuotedPrefix: false };
103
+ }
104
+ return { rawPrefix: prefix, isAtPrefix: false, isQuotedPrefix: false };
105
+ }
106
+
107
+ function buildCompletionValue(
108
+ path: string,
109
+ options: { isDirectory: boolean; isAtPrefix: boolean; isQuotedPrefix: boolean },
110
+ ): string {
111
+ const needsQuotes = options.isQuotedPrefix || path.includes(" ");
112
+ const prefix = options.isAtPrefix ? "@" : "";
113
+
114
+ if (!needsQuotes) {
115
+ return `${prefix}${path}`;
116
+ }
117
+
118
+ const openQuote = `${prefix}"`;
119
+ const closeQuote = '"';
120
+ return `${openQuote}${path}${closeQuote}`;
121
+ }
122
+
123
+ // Use fd to walk directory tree (fast, respects .gitignore)
124
+ async function walkDirectoryWithFd(
125
+ baseDir: string,
126
+ fdPath: string,
127
+ query: string,
128
+ maxResults: number,
129
+ signal: AbortSignal,
130
+ ): Promise<Array<{ path: string; isDirectory: boolean }>> {
131
+ const args = [
132
+ "--base-directory",
133
+ baseDir,
134
+ "--max-results",
135
+ String(maxResults),
136
+ "--type",
137
+ "f",
138
+ "--type",
139
+ "d",
140
+ "--full-path",
141
+ "--hidden",
142
+ "--exclude",
143
+ ".git",
144
+ "--exclude",
145
+ ".git/*",
146
+ "--exclude",
147
+ ".git/**",
148
+ ];
149
+
150
+ if (query) {
151
+ args.push(buildFdPathQuery(query));
152
+ }
153
+
154
+ return await new Promise((resolve) => {
155
+ if (signal.aborted) {
156
+ resolve([]);
157
+ return;
158
+ }
159
+
160
+ const child = spawn(fdPath, args, {
161
+ stdio: ["ignore", "pipe", "pipe"],
162
+ });
163
+ let stdout = "";
164
+ let resolved = false;
165
+
166
+ const finish = (results: Array<{ path: string; isDirectory: boolean }>) => {
167
+ if (resolved) return;
168
+ resolved = true;
169
+ signal.removeEventListener("abort", onAbort);
170
+ resolve(results);
171
+ };
172
+
173
+ const onAbort = () => {
174
+ if (child.exitCode === null) {
175
+ child.kill("SIGKILL");
176
+ }
177
+ };
178
+
179
+ signal.addEventListener("abort", onAbort, { once: true });
180
+ child.stdout.setEncoding("utf-8");
181
+ child.stdout.on("data", (chunk: string) => {
182
+ stdout += chunk;
183
+ });
184
+ child.on("error", () => {
185
+ finish([]);
186
+ });
187
+ child.on("close", (code) => {
188
+ if (signal.aborted || code !== 0 || !stdout) {
189
+ finish([]);
190
+ return;
191
+ }
192
+
193
+ const lines = stdout.trim().split("\n").filter(Boolean);
194
+ const results: Array<{ path: string; isDirectory: boolean }> = [];
195
+
196
+ for (const line of lines) {
197
+ const displayLine = toDisplayPath(line);
198
+ const hasTrailingSeparator = displayLine.endsWith("/");
199
+ const normalizedPath = hasTrailingSeparator ? displayLine.slice(0, -1) : displayLine;
200
+ if (normalizedPath === ".git" || normalizedPath.startsWith(".git/") || normalizedPath.includes("/.git/")) {
201
+ continue;
202
+ }
203
+
204
+ results.push({
205
+ path: displayLine,
206
+ isDirectory: hasTrailingSeparator,
207
+ });
208
+ }
209
+
210
+ finish(results);
211
+ });
212
+ });
213
+ }
214
+
215
+ export interface AutocompleteItem {
216
+ value: string;
217
+ label: string;
218
+ description?: string;
219
+ }
220
+
221
+ export interface SlashCommand {
222
+ name: string;
223
+ description?: string;
224
+ // Function to get argument completions for this command
225
+ // Returns null if no argument completion is available
226
+ getArgumentCompletions?(argumentPrefix: string): AutocompleteItem[] | null;
227
+ }
228
+
229
+ export interface AutocompleteSuggestions {
230
+ items: AutocompleteItem[];
231
+ prefix: string; // What we're matching against (e.g., "/" or "src/")
232
+ }
233
+
234
+ export interface AutocompleteProvider {
235
+ // Get autocomplete suggestions for current text/cursor position
236
+ // Returns null if no suggestions available
237
+ getSuggestions(
238
+ lines: string[],
239
+ cursorLine: number,
240
+ cursorCol: number,
241
+ options: { signal: AbortSignal; force?: boolean },
242
+ ): Promise<AutocompleteSuggestions | null>;
243
+
244
+ // Apply the selected item
245
+ // Returns the new text and cursor position
246
+ applyCompletion(
247
+ lines: string[],
248
+ cursorLine: number,
249
+ cursorCol: number,
250
+ item: AutocompleteItem,
251
+ prefix: string,
252
+ ): {
253
+ lines: string[];
254
+ cursorLine: number;
255
+ cursorCol: number;
256
+ };
257
+ }
258
+
259
+ // Combined provider that handles both slash commands and file paths
260
+ export class CombinedAutocompleteProvider implements AutocompleteProvider {
261
+ private commands: (SlashCommand | AutocompleteItem)[];
262
+ private basePath: string;
263
+ private fdPath: string | null;
264
+
265
+ constructor(
266
+ commands: (SlashCommand | AutocompleteItem)[] = [],
267
+ basePath: string = process.cwd(),
268
+ fdPath: string | null = null,
269
+ ) {
270
+ this.commands = commands;
271
+ this.basePath = basePath;
272
+ this.fdPath = fdPath;
273
+ }
274
+
275
+ async getSuggestions(
276
+ lines: string[],
277
+ cursorLine: number,
278
+ cursorCol: number,
279
+ options: { signal: AbortSignal; force?: boolean },
280
+ ): Promise<AutocompleteSuggestions | null> {
281
+ const currentLine = lines[cursorLine] || "";
282
+ const textBeforeCursor = currentLine.slice(0, cursorCol);
283
+
284
+ const atPrefix = this.extractAtPrefix(textBeforeCursor);
285
+ if (atPrefix) {
286
+ const { rawPrefix, isQuotedPrefix } = parsePathPrefix(atPrefix);
287
+ const suggestions = await this.getFuzzyFileSuggestions(rawPrefix, {
288
+ isQuotedPrefix,
289
+ signal: options.signal,
290
+ });
291
+ if (suggestions.length === 0) return null;
292
+
293
+ return {
294
+ items: suggestions,
295
+ prefix: atPrefix,
296
+ };
297
+ }
298
+
299
+ if (!options.force && textBeforeCursor.startsWith("/")) {
300
+ const spaceIndex = textBeforeCursor.indexOf(" ");
301
+
302
+ if (spaceIndex === -1) {
303
+ const prefix = textBeforeCursor.slice(1);
304
+ const commandItems = this.commands.map((cmd) => ({
305
+ name: "name" in cmd ? cmd.name : cmd.value,
306
+ label: "name" in cmd ? cmd.name : cmd.label,
307
+ description: cmd.description,
308
+ }));
309
+
310
+ const filtered = fuzzyFilter(commandItems, prefix, (item) => item.name).map((item) => ({
311
+ value: item.name,
312
+ label: item.label,
313
+ ...(item.description && { description: item.description }),
314
+ }));
315
+
316
+ if (filtered.length === 0) return null;
317
+
318
+ return {
319
+ items: filtered,
320
+ prefix: textBeforeCursor,
321
+ };
322
+ }
323
+
324
+ const commandName = textBeforeCursor.slice(1, spaceIndex);
325
+ const argumentText = textBeforeCursor.slice(spaceIndex + 1);
326
+
327
+ const command = this.commands.find((cmd) => {
328
+ const name = "name" in cmd ? cmd.name : cmd.value;
329
+ return name === commandName;
330
+ });
331
+ if (!command || !("getArgumentCompletions" in command) || !command.getArgumentCompletions) {
332
+ return null;
333
+ }
334
+
335
+ const argumentSuggestions = command.getArgumentCompletions(argumentText);
336
+ if (!argumentSuggestions || argumentSuggestions.length === 0) {
337
+ return null;
338
+ }
339
+
340
+ return {
341
+ items: argumentSuggestions,
342
+ prefix: argumentText,
343
+ };
344
+ }
345
+
346
+ const pathMatch = this.extractPathPrefix(textBeforeCursor, options.force ?? false);
347
+ if (pathMatch === null) {
348
+ return null;
349
+ }
350
+
351
+ const suggestions = this.getFileSuggestions(pathMatch);
352
+ if (suggestions.length === 0) return null;
353
+
354
+ return {
355
+ items: suggestions,
356
+ prefix: pathMatch,
357
+ };
358
+ }
359
+
360
+ applyCompletion(
361
+ lines: string[],
362
+ cursorLine: number,
363
+ cursorCol: number,
364
+ item: AutocompleteItem,
365
+ prefix: string,
366
+ ): { lines: string[]; cursorLine: number; cursorCol: number } {
367
+ const currentLine = lines[cursorLine] || "";
368
+ const beforePrefix = currentLine.slice(0, cursorCol - prefix.length);
369
+ const afterCursor = currentLine.slice(cursorCol);
370
+ const isQuotedPrefix = prefix.startsWith('"') || prefix.startsWith('@"');
371
+ const hasLeadingQuoteAfterCursor = afterCursor.startsWith('"');
372
+ const hasTrailingQuoteInItem = item.value.endsWith('"');
373
+ const adjustedAfterCursor =
374
+ isQuotedPrefix && hasTrailingQuoteInItem && hasLeadingQuoteAfterCursor ? afterCursor.slice(1) : afterCursor;
375
+
376
+ // Check if we're completing a slash command (prefix starts with "/" but NOT a file path)
377
+ // Slash commands are at the start of the line and don't contain path separators after the first /
378
+ const isSlashCommand = prefix.startsWith("/") && beforePrefix.trim() === "" && !prefix.slice(1).includes("/");
379
+ if (isSlashCommand) {
380
+ // This is a command name completion
381
+ const newLine = `${beforePrefix}/${item.value} ${adjustedAfterCursor}`;
382
+ const newLines = [...lines];
383
+ newLines[cursorLine] = newLine;
384
+
385
+ return {
386
+ lines: newLines,
387
+ cursorLine,
388
+ cursorCol: beforePrefix.length + item.value.length + 2, // +2 for "/" and space
389
+ };
390
+ }
391
+
392
+ // Check if we're completing a file attachment (prefix starts with "@")
393
+ if (prefix.startsWith("@")) {
394
+ // This is a file attachment completion
395
+ // Don't add space after directories so user can continue autocompleting
396
+ const isDirectory = item.label.endsWith("/");
397
+ const suffix = isDirectory ? "" : " ";
398
+ const newLine = `${beforePrefix + item.value}${suffix}${adjustedAfterCursor}`;
399
+ const newLines = [...lines];
400
+ newLines[cursorLine] = newLine;
401
+
402
+ const hasTrailingQuote = item.value.endsWith('"');
403
+ const cursorOffset = isDirectory && hasTrailingQuote ? item.value.length - 1 : item.value.length;
404
+
405
+ return {
406
+ lines: newLines,
407
+ cursorLine,
408
+ cursorCol: beforePrefix.length + cursorOffset + suffix.length,
409
+ };
410
+ }
411
+
412
+ // Check if we're in a slash command context (beforePrefix contains "/command ")
413
+ const textBeforeCursor = currentLine.slice(0, cursorCol);
414
+ if (textBeforeCursor.includes("/") && textBeforeCursor.includes(" ")) {
415
+ // This is likely a command argument completion
416
+ const newLine = beforePrefix + item.value + adjustedAfterCursor;
417
+ const newLines = [...lines];
418
+ newLines[cursorLine] = newLine;
419
+
420
+ const isDirectory = item.label.endsWith("/");
421
+ const hasTrailingQuote = item.value.endsWith('"');
422
+ const cursorOffset = isDirectory && hasTrailingQuote ? item.value.length - 1 : item.value.length;
423
+
424
+ return {
425
+ lines: newLines,
426
+ cursorLine,
427
+ cursorCol: beforePrefix.length + cursorOffset,
428
+ };
429
+ }
430
+
431
+ // For file paths, complete the path
432
+ const newLine = beforePrefix + item.value + adjustedAfterCursor;
433
+ const newLines = [...lines];
434
+ newLines[cursorLine] = newLine;
435
+
436
+ const isDirectory = item.label.endsWith("/");
437
+ const hasTrailingQuote = item.value.endsWith('"');
438
+ const cursorOffset = isDirectory && hasTrailingQuote ? item.value.length - 1 : item.value.length;
439
+
440
+ return {
441
+ lines: newLines,
442
+ cursorLine,
443
+ cursorCol: beforePrefix.length + cursorOffset,
444
+ };
445
+ }
446
+
447
+ // Extract @ prefix for fuzzy file suggestions
448
+ private extractAtPrefix(text: string): string | null {
449
+ const quotedPrefix = extractQuotedPrefix(text);
450
+ if (quotedPrefix?.startsWith('@"')) {
451
+ return quotedPrefix;
452
+ }
453
+
454
+ const lastDelimiterIndex = findLastDelimiter(text);
455
+ const tokenStart = lastDelimiterIndex === -1 ? 0 : lastDelimiterIndex + 1;
456
+
457
+ if (text[tokenStart] === "@") {
458
+ return text.slice(tokenStart);
459
+ }
460
+
461
+ return null;
462
+ }
463
+
464
+ // Extract a path-like prefix from the text before cursor
465
+ private extractPathPrefix(text: string, forceExtract: boolean = false): string | null {
466
+ const quotedPrefix = extractQuotedPrefix(text);
467
+ if (quotedPrefix) {
468
+ return quotedPrefix;
469
+ }
470
+
471
+ const lastDelimiterIndex = findLastDelimiter(text);
472
+ const pathPrefix = lastDelimiterIndex === -1 ? text : text.slice(lastDelimiterIndex + 1);
473
+
474
+ // For forced extraction (Tab key), always return something
475
+ if (forceExtract) {
476
+ return pathPrefix;
477
+ }
478
+
479
+ // For natural triggers, return if it looks like a path, ends with /, starts with ~/, .
480
+ // Only return empty string if the text looks like it's starting a path context
481
+ if (pathPrefix.includes("/") || pathPrefix.startsWith(".") || pathPrefix.startsWith("~/")) {
482
+ return pathPrefix;
483
+ }
484
+
485
+ // Return empty string only after a space (not for completely empty text)
486
+ // Empty text should not trigger file suggestions - that's for forced Tab completion
487
+ if (pathPrefix === "" && text.endsWith(" ")) {
488
+ return pathPrefix;
489
+ }
490
+
491
+ return null;
492
+ }
493
+
494
+ // Expand home directory (~/) to actual home path
495
+ private expandHomePath(path: string): string {
496
+ if (path.startsWith("~/")) {
497
+ const expandedPath = join(homedir(), path.slice(2));
498
+ // Preserve trailing slash if original path had one
499
+ return path.endsWith("/") && !expandedPath.endsWith("/") ? `${expandedPath}/` : expandedPath;
500
+ } else if (path === "~") {
501
+ return homedir();
502
+ }
503
+ return path;
504
+ }
505
+
506
+ private resolveScopedFuzzyQuery(rawQuery: string): { baseDir: string; query: string; displayBase: string } | null {
507
+ const normalizedQuery = toDisplayPath(rawQuery);
508
+ const slashIndex = normalizedQuery.lastIndexOf("/");
509
+ if (slashIndex === -1) {
510
+ return null;
511
+ }
512
+
513
+ const displayBase = normalizedQuery.slice(0, slashIndex + 1);
514
+ const query = normalizedQuery.slice(slashIndex + 1);
515
+
516
+ let baseDir: string;
517
+ if (displayBase.startsWith("~/")) {
518
+ baseDir = this.expandHomePath(displayBase);
519
+ } else if (displayBase.startsWith("/")) {
520
+ baseDir = displayBase;
521
+ } else {
522
+ baseDir = join(this.basePath, displayBase);
523
+ }
524
+
525
+ try {
526
+ if (!statSync(baseDir).isDirectory()) {
527
+ return null;
528
+ }
529
+ } catch {
530
+ return null;
531
+ }
532
+
533
+ return { baseDir, query, displayBase };
534
+ }
535
+
536
+ private scopedPathForDisplay(displayBase: string, relativePath: string): string {
537
+ const normalizedRelativePath = toDisplayPath(relativePath);
538
+ if (displayBase === "/") {
539
+ return `/${normalizedRelativePath}`;
540
+ }
541
+ return `${toDisplayPath(displayBase)}${normalizedRelativePath}`;
542
+ }
543
+
544
+ // Get file/directory suggestions for a given path prefix
545
+ private getFileSuggestions(prefix: string): AutocompleteItem[] {
546
+ try {
547
+ let searchDir: string;
548
+ let searchPrefix: string;
549
+ const { rawPrefix, isAtPrefix, isQuotedPrefix } = parsePathPrefix(prefix);
550
+ let expandedPrefix = rawPrefix;
551
+
552
+ // Handle home directory expansion
553
+ if (expandedPrefix.startsWith("~")) {
554
+ expandedPrefix = this.expandHomePath(expandedPrefix);
555
+ }
556
+
557
+ const isRootPrefix =
558
+ rawPrefix === "" ||
559
+ rawPrefix === "./" ||
560
+ rawPrefix === "../" ||
561
+ rawPrefix === "~" ||
562
+ rawPrefix === "~/" ||
563
+ rawPrefix === "/" ||
564
+ (isAtPrefix && rawPrefix === "");
565
+
566
+ if (isRootPrefix) {
567
+ // Complete from specified position
568
+ if (rawPrefix.startsWith("~") || expandedPrefix.startsWith("/")) {
569
+ searchDir = expandedPrefix;
570
+ } else {
571
+ searchDir = join(this.basePath, expandedPrefix);
572
+ }
573
+ searchPrefix = "";
574
+ } else if (rawPrefix.endsWith("/")) {
575
+ // If prefix ends with /, show contents of that directory
576
+ if (rawPrefix.startsWith("~") || expandedPrefix.startsWith("/")) {
577
+ searchDir = expandedPrefix;
578
+ } else {
579
+ searchDir = join(this.basePath, expandedPrefix);
580
+ }
581
+ searchPrefix = "";
582
+ } else {
583
+ // Split into directory and file prefix
584
+ const dir = dirname(expandedPrefix);
585
+ const file = basename(expandedPrefix);
586
+ if (rawPrefix.startsWith("~") || expandedPrefix.startsWith("/")) {
587
+ searchDir = dir;
588
+ } else {
589
+ searchDir = join(this.basePath, dir);
590
+ }
591
+ searchPrefix = file;
592
+ }
593
+
594
+ const entries = readdirSync(searchDir, { withFileTypes: true });
595
+ const suggestions: AutocompleteItem[] = [];
596
+
597
+ for (const entry of entries) {
598
+ if (!entry.name.toLowerCase().startsWith(searchPrefix.toLowerCase())) {
599
+ continue;
600
+ }
601
+
602
+ // Check if entry is a directory (or a symlink pointing to a directory)
603
+ let isDirectory = entry.isDirectory();
604
+ if (!isDirectory && entry.isSymbolicLink()) {
605
+ try {
606
+ const fullPath = join(searchDir, entry.name);
607
+ isDirectory = statSync(fullPath).isDirectory();
608
+ } catch {
609
+ // Broken symlink or permission error - treat as file
610
+ }
611
+ }
612
+
613
+ let relativePath: string;
614
+ const name = entry.name;
615
+ const displayPrefix = rawPrefix;
616
+
617
+ if (displayPrefix.endsWith("/")) {
618
+ // If prefix ends with /, append entry to the prefix
619
+ relativePath = displayPrefix + name;
620
+ } else if (displayPrefix.includes("/") || displayPrefix.includes("\\")) {
621
+ // Preserve ~/ format for home directory paths
622
+ if (displayPrefix.startsWith("~/")) {
623
+ const homeRelativeDir = displayPrefix.slice(2); // Remove ~/
624
+ const dir = dirname(homeRelativeDir);
625
+ relativePath = `~/${dir === "." ? name : join(dir, name)}`;
626
+ } else if (displayPrefix.startsWith("/")) {
627
+ // Absolute path - construct properly
628
+ const dir = dirname(displayPrefix);
629
+ if (dir === "/") {
630
+ relativePath = `/${name}`;
631
+ } else {
632
+ relativePath = `${dir}/${name}`;
633
+ }
634
+ } else {
635
+ relativePath = join(dirname(displayPrefix), name);
636
+ // path.join normalizes away ./ prefix, preserve it
637
+ if (displayPrefix.startsWith("./") && !relativePath.startsWith("./")) {
638
+ relativePath = `./${relativePath}`;
639
+ }
640
+ }
641
+ } else {
642
+ // For standalone entries, preserve ~/ if original prefix was ~/
643
+ if (displayPrefix.startsWith("~")) {
644
+ relativePath = `~/${name}`;
645
+ } else {
646
+ relativePath = name;
647
+ }
648
+ }
649
+
650
+ relativePath = toDisplayPath(relativePath);
651
+ const pathValue = isDirectory ? `${relativePath}/` : relativePath;
652
+ const value = buildCompletionValue(pathValue, {
653
+ isDirectory,
654
+ isAtPrefix,
655
+ isQuotedPrefix,
656
+ });
657
+
658
+ suggestions.push({
659
+ value,
660
+ label: name + (isDirectory ? "/" : ""),
661
+ });
662
+ }
663
+
664
+ // Sort directories first, then alphabetically
665
+ suggestions.sort((a, b) => {
666
+ const aIsDir = a.value.endsWith("/");
667
+ const bIsDir = b.value.endsWith("/");
668
+ if (aIsDir && !bIsDir) return -1;
669
+ if (!aIsDir && bIsDir) return 1;
670
+ return a.label.localeCompare(b.label);
671
+ });
672
+
673
+ return suggestions;
674
+ } catch (_e) {
675
+ // Directory doesn't exist or not accessible
676
+ return [];
677
+ }
678
+ }
679
+
680
+ // Score an entry against the query (higher = better match)
681
+ // isDirectory adds bonus to prioritize folders
682
+ private scoreEntry(filePath: string, query: string, isDirectory: boolean): number {
683
+ const fileName = basename(filePath);
684
+ const lowerFileName = fileName.toLowerCase();
685
+ const lowerQuery = query.toLowerCase();
686
+
687
+ let score = 0;
688
+
689
+ // Exact filename match (highest)
690
+ if (lowerFileName === lowerQuery) score = 100;
691
+ // Filename starts with query
692
+ else if (lowerFileName.startsWith(lowerQuery)) score = 80;
693
+ // Substring match in filename
694
+ else if (lowerFileName.includes(lowerQuery)) score = 50;
695
+ // Substring match in full path
696
+ else if (filePath.toLowerCase().includes(lowerQuery)) score = 30;
697
+
698
+ // Directories get a bonus to appear first
699
+ if (isDirectory && score > 0) score += 10;
700
+
701
+ return score;
702
+ }
703
+
704
+ // Fuzzy file search using fd (fast, respects .gitignore)
705
+ private async getFuzzyFileSuggestions(
706
+ query: string,
707
+ options: { isQuotedPrefix: boolean; signal: AbortSignal },
708
+ ): Promise<AutocompleteItem[]> {
709
+ if (!this.fdPath || options.signal.aborted) {
710
+ return [];
711
+ }
712
+
713
+ try {
714
+ const scopedQuery = this.resolveScopedFuzzyQuery(query);
715
+ const fdBaseDir = scopedQuery?.baseDir ?? this.basePath;
716
+ const fdQuery = scopedQuery?.query ?? query;
717
+ const entries = await walkDirectoryWithFd(fdBaseDir, this.fdPath, fdQuery, 100, options.signal);
718
+ if (options.signal.aborted) {
719
+ return [];
720
+ }
721
+
722
+ const scoredEntries = entries
723
+ .map((entry) => ({
724
+ ...entry,
725
+ score: fdQuery ? this.scoreEntry(entry.path, fdQuery, entry.isDirectory) : 1,
726
+ }))
727
+ .filter((entry) => entry.score > 0);
728
+
729
+ scoredEntries.sort((a, b) => b.score - a.score);
730
+ const topEntries = scoredEntries.slice(0, 20);
731
+
732
+ const suggestions: AutocompleteItem[] = [];
733
+ for (const { path: entryPath, isDirectory } of topEntries) {
734
+ const pathWithoutSlash = isDirectory ? entryPath.slice(0, -1) : entryPath;
735
+ const displayPath = scopedQuery
736
+ ? this.scopedPathForDisplay(scopedQuery.displayBase, pathWithoutSlash)
737
+ : pathWithoutSlash;
738
+ const entryName = basename(pathWithoutSlash);
739
+ const completionPath = isDirectory ? `${displayPath}/` : displayPath;
740
+ const value = buildCompletionValue(completionPath, {
741
+ isDirectory,
742
+ isAtPrefix: true,
743
+ isQuotedPrefix: options.isQuotedPrefix,
744
+ });
745
+
746
+ suggestions.push({
747
+ value,
748
+ label: entryName + (isDirectory ? "/" : ""),
749
+ description: displayPath,
750
+ });
751
+ }
752
+
753
+ return suggestions;
754
+ } catch {
755
+ return [];
756
+ }
757
+ }
758
+
759
+ // Check if we should trigger file completion (called on Tab key)
760
+ shouldTriggerFileCompletion(lines: string[], cursorLine: number, cursorCol: number): boolean {
761
+ const currentLine = lines[cursorLine] || "";
762
+ const textBeforeCursor = currentLine.slice(0, cursorCol);
763
+
764
+ // Don't trigger if we're typing a slash command at the start of the line
765
+ if (textBeforeCursor.trim().startsWith("/") && !textBeforeCursor.trim().includes(" ")) {
766
+ return false;
767
+ }
768
+
769
+ return true;
770
+ }
771
+ }