@draht/tui 2026.3.2-2

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