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