@ny4n/tui 0.0.0-stage → 1.0.1

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