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