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