@prometheus-ai/tui 0.5.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 (65) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +704 -0
  3. package/dist/types/autocomplete.d.ts +76 -0
  4. package/dist/types/bracketed-paste.d.ts +26 -0
  5. package/dist/types/components/box.d.ts +17 -0
  6. package/dist/types/components/cancellable-loader.d.ts +21 -0
  7. package/dist/types/components/editor.d.ts +105 -0
  8. package/dist/types/components/image.d.ts +84 -0
  9. package/dist/types/components/input.d.ts +18 -0
  10. package/dist/types/components/loader.d.ts +13 -0
  11. package/dist/types/components/markdown.d.ts +61 -0
  12. package/dist/types/components/scroll-view.d.ts +40 -0
  13. package/dist/types/components/select-list.d.ts +48 -0
  14. package/dist/types/components/settings-list.d.ts +41 -0
  15. package/dist/types/components/spacer.d.ts +11 -0
  16. package/dist/types/components/tab-bar.d.ts +56 -0
  17. package/dist/types/components/text.d.ts +13 -0
  18. package/dist/types/components/truncated-text.d.ts +10 -0
  19. package/dist/types/deccara.d.ts +49 -0
  20. package/dist/types/editor-component.d.ts +36 -0
  21. package/dist/types/fuzzy.d.ts +15 -0
  22. package/dist/types/index.d.ts +28 -0
  23. package/dist/types/keybindings.d.ts +189 -0
  24. package/dist/types/keys.d.ts +208 -0
  25. package/dist/types/kill-ring.d.ts +27 -0
  26. package/dist/types/kitty-graphics.d.ts +94 -0
  27. package/dist/types/stdin-buffer.d.ts +43 -0
  28. package/dist/types/symbols.d.ts +25 -0
  29. package/dist/types/terminal-capabilities.d.ts +196 -0
  30. package/dist/types/terminal.d.ts +103 -0
  31. package/dist/types/ttyid.d.ts +9 -0
  32. package/dist/types/tui.d.ts +275 -0
  33. package/dist/types/utils.d.ts +89 -0
  34. package/package.json +73 -0
  35. package/src/autocomplete.ts +871 -0
  36. package/src/bracketed-paste.ts +47 -0
  37. package/src/components/box.ts +156 -0
  38. package/src/components/cancellable-loader.ts +40 -0
  39. package/src/components/editor.ts +2695 -0
  40. package/src/components/image.ts +318 -0
  41. package/src/components/input.ts +459 -0
  42. package/src/components/loader.ts +86 -0
  43. package/src/components/markdown.ts +1189 -0
  44. package/src/components/scroll-view.ts +166 -0
  45. package/src/components/select-list.ts +331 -0
  46. package/src/components/settings-list.ts +212 -0
  47. package/src/components/spacer.ts +28 -0
  48. package/src/components/tab-bar.ts +175 -0
  49. package/src/components/text.ts +110 -0
  50. package/src/components/truncated-text.ts +61 -0
  51. package/src/deccara.ts +314 -0
  52. package/src/editor-component.ts +71 -0
  53. package/src/fuzzy.ts +143 -0
  54. package/src/index.ts +44 -0
  55. package/src/keybindings.ts +279 -0
  56. package/src/keys.ts +537 -0
  57. package/src/kill-ring.ts +46 -0
  58. package/src/kitty-graphics.ts +270 -0
  59. package/src/stdin-buffer.ts +423 -0
  60. package/src/symbols.ts +26 -0
  61. package/src/terminal-capabilities.ts +1009 -0
  62. package/src/terminal.ts +1114 -0
  63. package/src/ttyid.ts +70 -0
  64. package/src/tui.ts +2988 -0
  65. package/src/utils.ts +452 -0
@@ -0,0 +1,2695 @@
1
+ import { getProjectDir, logger } from "@prometheus-ai/utils";
2
+ import type { AutocompleteProvider, CombinedAutocompleteProvider } from "../autocomplete";
3
+ import { BracketedPasteHandler } from "../bracketed-paste";
4
+ import { getKeybindings, type KeybindingsManager } from "../keybindings";
5
+ import { extractPrintableText, matchesKey } from "../keys";
6
+ import { KillRing } from "../kill-ring";
7
+ import type { SymbolTheme } from "../symbols";
8
+ import { type Component, CURSOR_MARKER, type Focusable } from "../tui";
9
+ import {
10
+ getSegmenter,
11
+ getWordNavKind,
12
+ moveWordLeft,
13
+ moveWordRight,
14
+ padding,
15
+ replaceTabs,
16
+ sliceByColumn,
17
+ truncateToWidth,
18
+ visibleWidth,
19
+ } from "../utils";
20
+ import { SelectList, type SelectListLayoutOptions, type SelectListTheme } from "./select-list";
21
+
22
+ const AUTOCOMPLETE_SELECT_LIST_LAYOUT: SelectListLayoutOptions = {
23
+ overflowSearch: false,
24
+ };
25
+
26
+ const SLASH_COMMAND_SELECT_LIST_LAYOUT: SelectListLayoutOptions = {
27
+ minPrimaryColumnWidth: 12,
28
+ maxPrimaryColumnWidth: 32,
29
+ overflowSearch: false,
30
+ };
31
+
32
+ function sanitizeLoadedText(text: string): string {
33
+ // Normalize CRLF/CR → LF, then strip C0 control chars except \n.
34
+ return replaceTabs(text.replace(/\r\n?/g, "\n")).replace(/[\x00-\x09\x0b-\x1f]/g, "");
35
+ }
36
+
37
+ const segmenter = getSegmenter();
38
+
39
+ /**
40
+ * Represents a chunk of text for word-wrap layout.
41
+ * Tracks both the text content and its position in the original line.
42
+ */
43
+ interface TextChunk {
44
+ text: string;
45
+ startIndex: number;
46
+ endIndex: number;
47
+ }
48
+
49
+ /**
50
+ * Split a line into word-wrapped chunks.
51
+ * Wraps at word boundaries when possible, falling back to character-level
52
+ * wrapping for words longer than the available width.
53
+ *
54
+ * @param line - The text line to wrap
55
+ * @param maxWidth - Maximum visible width per chunk
56
+ * @returns Array of chunks with text and position information
57
+ */
58
+ function wordWrapLine(line: string, maxWidth: number): TextChunk[] {
59
+ if (!line || maxWidth <= 0) {
60
+ return [{ text: "", startIndex: 0, endIndex: 0 }];
61
+ }
62
+
63
+ const lineWidth = visibleWidth(line);
64
+ if (lineWidth <= maxWidth) {
65
+ return [{ text: line, startIndex: 0, endIndex: line.length }];
66
+ }
67
+
68
+ const chunks: TextChunk[] = [];
69
+
70
+ // Split into tokens (words and whitespace runs)
71
+ const tokens: { text: string; startIndex: number; endIndex: number; isWhitespace: boolean }[] = [];
72
+ let currentToken = "";
73
+ let tokenStart = 0;
74
+ let inWhitespace = false;
75
+ let charIndex = 0;
76
+
77
+ for (const seg of segmenter.segment(line)) {
78
+ const grapheme = seg.segment;
79
+ const graphemeIsWhitespace = getWordNavKind(grapheme) === "whitespace";
80
+
81
+ if (currentToken === "") {
82
+ inWhitespace = graphemeIsWhitespace;
83
+ tokenStart = charIndex;
84
+ } else if (graphemeIsWhitespace !== inWhitespace) {
85
+ // Token type changed - save current token
86
+ tokens.push({
87
+ text: currentToken,
88
+ startIndex: tokenStart,
89
+ endIndex: charIndex,
90
+ isWhitespace: inWhitespace,
91
+ });
92
+ currentToken = "";
93
+ tokenStart = charIndex;
94
+ inWhitespace = graphemeIsWhitespace;
95
+ }
96
+
97
+ currentToken += grapheme;
98
+ charIndex += grapheme.length;
99
+ }
100
+
101
+ // Push final token
102
+ if (currentToken) {
103
+ tokens.push({
104
+ text: currentToken,
105
+ startIndex: tokenStart,
106
+ endIndex: charIndex,
107
+ isWhitespace: inWhitespace,
108
+ });
109
+ }
110
+
111
+ // Build chunks using word wrapping
112
+ let currentChunk = "";
113
+ let currentWidth = 0;
114
+ let chunkStartIndex = 0;
115
+ let atLineStart = true; // Track if we're at the start of a line (for skipping whitespace)
116
+
117
+ function consumePrefixToWidth(text: string, availableWidth: number): { text: string; len: number } {
118
+ let prefix = "";
119
+ let prefixWidth = 0;
120
+ let len = 0;
121
+ for (const seg of segmenter.segment(text)) {
122
+ const grapheme = seg.segment;
123
+ const graphemeWidth = visibleWidth(grapheme);
124
+ if (prefixWidth + graphemeWidth > availableWidth) break;
125
+ prefix += grapheme;
126
+ prefixWidth += graphemeWidth;
127
+ len += grapheme.length;
128
+ if (prefixWidth === availableWidth) break;
129
+ }
130
+ return { text: prefix, len };
131
+ }
132
+ function hasWideGrapheme(text: string): boolean {
133
+ for (const seg of segmenter.segment(text)) {
134
+ if (visibleWidth(seg.segment) > 1) return true;
135
+ }
136
+ return false;
137
+ }
138
+ for (const token of tokens) {
139
+ const tokenWidth = visibleWidth(token.text);
140
+
141
+ // Skip leading whitespace at line start
142
+ if (atLineStart && token.isWhitespace) {
143
+ chunkStartIndex = token.endIndex;
144
+ continue;
145
+ }
146
+ atLineStart = false;
147
+
148
+ // If this single token is wider than maxWidth, we need to break it
149
+ if (tokenWidth > maxWidth) {
150
+ // If we're mid-line, try to use the remaining width by consuming a prefix of this long token.
151
+ let consumedPrefix = "";
152
+ let consumedPrefixLen = 0; // JS string index (code units) consumed from token.text
153
+ if (currentChunk && currentWidth < maxWidth) {
154
+ const remainingWidth = maxWidth - currentWidth;
155
+ const consumed = consumePrefixToWidth(token.text, remainingWidth);
156
+ consumedPrefix = consumed.text;
157
+ consumedPrefixLen = consumed.len;
158
+ }
159
+ // First, push any accumulated chunk (optionally filled with the prefix).
160
+ if (currentChunk) {
161
+ if (consumedPrefix) {
162
+ chunks.push({
163
+ text: currentChunk + consumedPrefix,
164
+ startIndex: chunkStartIndex,
165
+ endIndex: token.startIndex + consumedPrefixLen,
166
+ });
167
+ currentChunk = "";
168
+ currentWidth = 0;
169
+ chunkStartIndex = token.startIndex + consumedPrefixLen;
170
+ } else {
171
+ chunks.push({
172
+ text: currentChunk,
173
+ startIndex: chunkStartIndex,
174
+ endIndex: token.startIndex,
175
+ });
176
+ currentChunk = "";
177
+ currentWidth = 0;
178
+ chunkStartIndex = token.startIndex;
179
+ }
180
+ }
181
+ // Break the remaining long token by grapheme
182
+ const remainingText = consumedPrefixLen > 0 ? token.text.slice(consumedPrefixLen) : token.text;
183
+ let tokenChunk = "";
184
+ let tokenChunkWidth = 0;
185
+ let tokenChunkStart = token.startIndex + consumedPrefixLen;
186
+ let tokenCharIndex = token.startIndex + consumedPrefixLen;
187
+ for (const seg of segmenter.segment(remainingText)) {
188
+ const grapheme = seg.segment;
189
+ const graphemeWidth = visibleWidth(grapheme);
190
+ if (tokenChunkWidth + graphemeWidth > maxWidth && tokenChunk) {
191
+ chunks.push({
192
+ text: tokenChunk,
193
+ startIndex: tokenChunkStart,
194
+ endIndex: tokenCharIndex,
195
+ });
196
+ tokenChunk = grapheme;
197
+ tokenChunkWidth = graphemeWidth;
198
+ tokenChunkStart = tokenCharIndex;
199
+ } else {
200
+ tokenChunk += grapheme;
201
+ tokenChunkWidth += graphemeWidth;
202
+ }
203
+ tokenCharIndex += grapheme.length;
204
+ }
205
+ // Keep remainder as start of next chunk
206
+ if (tokenChunk) {
207
+ currentChunk = tokenChunk;
208
+ currentWidth = tokenChunkWidth;
209
+ chunkStartIndex = tokenChunkStart;
210
+ }
211
+ continue;
212
+ }
213
+
214
+ // Check if adding this token would exceed width
215
+ if (currentWidth + tokenWidth > maxWidth) {
216
+ // For wide-character tokens (e.g., CJK runs), prefer using remaining width before wrapping
217
+ // the whole token to the next line. This avoids leaving a short ASCII word alone.
218
+ if (currentChunk && !token.isWhitespace && currentWidth < maxWidth && hasWideGrapheme(token.text)) {
219
+ const remainingWidth = maxWidth - currentWidth;
220
+ const consumed = consumePrefixToWidth(token.text, remainingWidth);
221
+ if (consumed.text) {
222
+ chunks.push({
223
+ text: currentChunk + consumed.text,
224
+ startIndex: chunkStartIndex,
225
+ endIndex: token.startIndex + consumed.len,
226
+ });
227
+ const remainder = token.text.slice(consumed.len);
228
+ currentChunk = remainder;
229
+ currentWidth = visibleWidth(remainder);
230
+ chunkStartIndex = token.startIndex + consumed.len;
231
+ atLineStart = false;
232
+ continue;
233
+ }
234
+ }
235
+ // Push current chunk (trimming trailing whitespace for display)
236
+ const trimmedChunk = currentChunk.trimEnd();
237
+ if (trimmedChunk || chunks.length === 0) {
238
+ chunks.push({
239
+ text: trimmedChunk,
240
+ startIndex: chunkStartIndex,
241
+ endIndex: chunkStartIndex + currentChunk.length,
242
+ });
243
+ }
244
+ // Start new line - skip leading whitespace
245
+ atLineStart = true;
246
+ if (token.isWhitespace) {
247
+ currentChunk = "";
248
+ currentWidth = 0;
249
+ chunkStartIndex = token.endIndex;
250
+ } else {
251
+ currentChunk = token.text;
252
+ currentWidth = tokenWidth;
253
+ chunkStartIndex = token.startIndex;
254
+ atLineStart = false;
255
+ }
256
+ } else {
257
+ // Add token to current chunk
258
+ currentChunk += token.text;
259
+ currentWidth += tokenWidth;
260
+ }
261
+ }
262
+
263
+ // Push final chunk
264
+ if (currentChunk) {
265
+ chunks.push({
266
+ text: currentChunk,
267
+ startIndex: chunkStartIndex,
268
+ endIndex: line.length,
269
+ });
270
+ }
271
+
272
+ return chunks.length > 0 ? chunks : [{ text: "", startIndex: 0, endIndex: 0 }];
273
+ }
274
+
275
+ const DEFAULT_PAGE_SCROLL_LINES = 10;
276
+
277
+ interface EditorState {
278
+ lines: string[];
279
+ cursorLine: number;
280
+ cursorCol: number;
281
+ }
282
+
283
+ interface LayoutLine {
284
+ text: string;
285
+ hasCursor: boolean;
286
+ cursorPos?: number;
287
+ }
288
+
289
+ export interface EditorTheme {
290
+ borderColor: (str: string) => string;
291
+ selectList: SelectListTheme;
292
+ symbols: SymbolTheme;
293
+ editorPaddingX?: number;
294
+ /** Style function for inline hint/ghost text (dim text after cursor) */
295
+ hintStyle?: (text: string) => string;
296
+ }
297
+
298
+ export interface EditorTopBorder {
299
+ /** The status content (already styled) */
300
+ content: string;
301
+ /** Visible width of the content */
302
+ width: number;
303
+ }
304
+
305
+ interface HistoryEntry {
306
+ prompt: string;
307
+ }
308
+
309
+ interface HistoryStorage {
310
+ add(prompt: string, cwd?: string): Promise<void>;
311
+ getRecent(limit: number): HistoryEntry[];
312
+ }
313
+
314
+ type HistoryCursorAnchor = "start" | "end";
315
+
316
+ export class Editor implements Component, Focusable {
317
+ #state: EditorState = {
318
+ lines: [""],
319
+ cursorLine: 0,
320
+ cursorCol: 0,
321
+ };
322
+
323
+ /** Focusable interface - set by TUI when focus changes */
324
+ focused: boolean = false;
325
+
326
+ #theme: EditorTheme;
327
+ #useTerminalCursor = false;
328
+
329
+ /** When set, replaces the normal cursor glyph at end-of-text with this ANSI-styled string. */
330
+ cursorOverride: string | undefined;
331
+ /** Display width of the cursorOverride glyph (needed because override may contain ANSI escapes). */
332
+ cursorOverrideWidth: number | undefined;
333
+ /** Optional hook that styles displayed input text with zero-width ANSI escapes.
334
+ * MUST preserve visible width (may only add SGR codes, never glyphs). Applied per
335
+ * layout line to the user-text segments — never to the cursor glyph or inline hint. */
336
+ decorateText: ((text: string) => string) | undefined;
337
+ #promptGutter: string | undefined;
338
+
339
+ // Store last layout width for cursor navigation
340
+ #lastLayoutWidth: number = 80;
341
+ #paddingXOverride: number | undefined;
342
+ #maxHeight?: number;
343
+ #scrollOffset: number = 0;
344
+
345
+ // Emacs-style kill ring
346
+ #killRing = new KillRing();
347
+ #lastAction: "kill" | "yank" | null = null;
348
+
349
+ // Character jump mode
350
+ #jumpMode: "forward" | "backward" | null = null;
351
+
352
+ // Preferred visual column for vertical cursor movement (sticky column)
353
+ #preferredVisualCol: number | null = null;
354
+
355
+ // Border color (can be changed dynamically)
356
+ borderColor: (str: string) => string;
357
+
358
+ // Autocomplete support
359
+ #autocompleteProvider?: AutocompleteProvider;
360
+ #autocompleteList?: SelectList;
361
+ #autocompleteState: "regular" | "force" | null = null;
362
+ #autocompletePrefix: string = "";
363
+ #autocompleteRequestId: number = 0;
364
+ #autocompleteMaxVisible: number = 5;
365
+ onAutocompleteUpdate?: () => void;
366
+
367
+ // Paste tracking for large pastes
368
+ #pastes: Map<number, string> = new Map();
369
+ #pasteCounter: number = 0;
370
+
371
+ // Bracketed paste mode buffering
372
+ #pasteHandler = new BracketedPasteHandler();
373
+
374
+ // Prompt history for up/down navigation
375
+ #history: string[] = [];
376
+ #historyIndex: number = -1; // -1 = not browsing, 0 = most recent, 1 = older, etc.
377
+ #historyStorage?: HistoryStorage;
378
+
379
+ // Undo stack for editor state changes
380
+ #undoStack: EditorState[] = [];
381
+ #suspendUndo = false;
382
+
383
+ // Debounce timer for autocomplete updates
384
+ #autocompleteTimeout?: NodeJS.Timeout;
385
+
386
+ onSubmit?: (text: string) => void;
387
+ onAltEnter?: (text: string) => void;
388
+ onChange?: (text: string) => void;
389
+ onAutocompleteCancel?: () => void;
390
+ disableSubmit: boolean = false;
391
+
392
+ // Custom top border (for status line integration)
393
+ #topBorderContent?: EditorTopBorder;
394
+ #borderVisible = true;
395
+
396
+ constructor(theme: EditorTheme) {
397
+ this.#theme = theme;
398
+ this.borderColor = theme.borderColor;
399
+ }
400
+
401
+ setAutocompleteProvider(provider: AutocompleteProvider): void {
402
+ this.#autocompleteProvider = provider;
403
+ }
404
+
405
+ /**
406
+ * Set custom content for the top border (e.g., status line).
407
+ * Pass undefined to use the default plain border.
408
+ */
409
+ setTopBorder(content: EditorTopBorder | undefined): void {
410
+ this.#topBorderContent = content;
411
+ }
412
+
413
+ /**
414
+ * Show or hide the editor border chrome.
415
+ */
416
+ setBorderVisible(borderVisible: boolean): void {
417
+ this.#borderVisible = borderVisible;
418
+ }
419
+
420
+ setPromptGutter(promptGutter: string | undefined): void {
421
+ this.#promptGutter = promptGutter;
422
+ }
423
+
424
+ /**
425
+ * Get the available width for top border content given a total terminal width.
426
+ * Accounts for the border characters and horizontal padding when visible.
427
+ */
428
+ getTopBorderAvailableWidth(terminalWidth: number): number {
429
+ const paddingX = this.#getEditorPaddingX();
430
+ const borderWidth = this.#getHorizontalChromeWidth(paddingX);
431
+ return Math.max(0, terminalWidth - borderWidth * 2);
432
+ }
433
+
434
+ /**
435
+ * Use the real terminal cursor instead of rendering a cursor glyph.
436
+ */
437
+ setUseTerminalCursor(useTerminalCursor: boolean): void {
438
+ this.#useTerminalCursor = useTerminalCursor;
439
+ }
440
+
441
+ getUseTerminalCursor(): boolean {
442
+ return this.#useTerminalCursor;
443
+ }
444
+
445
+ setMaxHeight(maxHeight: number | undefined): void {
446
+ if (this.#maxHeight === maxHeight) return;
447
+ this.#maxHeight = maxHeight;
448
+ // Don't reset scrollOffset — #updateScrollOffset will clamp it on next render
449
+ }
450
+
451
+ setPaddingX(paddingX: number): void {
452
+ this.#paddingXOverride = Math.max(0, paddingX);
453
+ }
454
+
455
+ getAutocompleteMaxVisible(): number {
456
+ return this.#autocompleteMaxVisible;
457
+ }
458
+
459
+ setAutocompleteMaxVisible(maxVisible: number): void {
460
+ const newMaxVisible = Number.isFinite(maxVisible) ? Math.max(3, Math.min(20, Math.floor(maxVisible))) : 5;
461
+ if (this.#autocompleteMaxVisible !== newMaxVisible) {
462
+ this.#autocompleteMaxVisible = newMaxVisible;
463
+ }
464
+ }
465
+
466
+ setHistoryStorage(storage: HistoryStorage): void {
467
+ this.#historyStorage = storage;
468
+ const recent = storage.getRecent(100);
469
+ this.#history = recent.map(entry => entry.prompt);
470
+ this.#historyIndex = -1;
471
+ }
472
+
473
+ /**
474
+ * Add a prompt to history for up/down arrow navigation.
475
+ * Called after successful submission.
476
+ */
477
+ addToHistory(text: string): void {
478
+ const trimmed = text.trim();
479
+ if (!trimmed) return;
480
+ // Don't add consecutive duplicates
481
+ if (this.#history.length > 0 && this.#history[0] === trimmed) return;
482
+ this.#history.unshift(trimmed);
483
+ // Limit history size
484
+ if (this.#history.length > 100) {
485
+ this.#history.pop();
486
+ }
487
+
488
+ const stor = this.#historyStorage;
489
+ if (stor) {
490
+ stor.add(trimmed, getProjectDir()).catch(error => {
491
+ logger.error("HistoryStorage add failed", { error: String(error) });
492
+ });
493
+ }
494
+ }
495
+
496
+ #isEditorEmpty(): boolean {
497
+ return this.#state.lines.length === 1 && this.#state.lines[0] === "";
498
+ }
499
+
500
+ #isOnFirstVisualLine(): boolean {
501
+ const visualLines = this.#buildVisualLineMap(this.#lastLayoutWidth);
502
+ const currentVisualLine = this.#findCurrentVisualLine(visualLines);
503
+ return currentVisualLine === 0;
504
+ }
505
+
506
+ #isOnLastVisualLine(): boolean {
507
+ const visualLines = this.#buildVisualLineMap(this.#lastLayoutWidth);
508
+ const currentVisualLine = this.#findCurrentVisualLine(visualLines);
509
+ return currentVisualLine === visualLines.length - 1;
510
+ }
511
+
512
+ #navigateHistory(direction: 1 | -1): void {
513
+ this.#resetKillSequence();
514
+ if (this.#history.length === 0) return;
515
+ const newIndex = this.#historyIndex - direction; // Up(-1) increases index, Down(1) decreases
516
+ if (newIndex < -1 || newIndex >= this.#history.length) return;
517
+ this.#historyIndex = newIndex;
518
+ if (this.#historyIndex === -1) {
519
+ // Returned to "current" state - clear editor
520
+ this.#setTextInternal("", "end");
521
+ } else {
522
+ const cursorAnchor: HistoryCursorAnchor = direction === -1 ? "start" : "end";
523
+ this.#setTextInternal(this.#history[this.#historyIndex] || "", cursorAnchor);
524
+ }
525
+ }
526
+ /** Internal setText that doesn't reset history state - used by navigateHistory */
527
+ #setTextInternal(text: string, cursorAnchor: HistoryCursorAnchor = "end"): void {
528
+ this.#undoStack.length = 0;
529
+ const lines = sanitizeLoadedText(text).split("\n");
530
+ this.#state.lines = lines.length === 0 ? [""] : lines;
531
+ if (cursorAnchor === "start") {
532
+ this.#state.cursorLine = 0;
533
+ this.#setCursorCol(0);
534
+ } else {
535
+ this.#state.cursorLine = this.#state.lines.length - 1;
536
+ this.#setCursorCol(this.#state.lines[this.#state.cursorLine]?.length || 0);
537
+ }
538
+ if (this.onChange) {
539
+ this.onChange(this.getText());
540
+ }
541
+ }
542
+
543
+ invalidate(): void {
544
+ // No cached state to invalidate currently
545
+ }
546
+
547
+ #getEditorPaddingX(): number {
548
+ const padding = this.#paddingXOverride ?? this.#theme.editorPaddingX ?? 2;
549
+ return Math.max(0, padding);
550
+ }
551
+
552
+ #getHorizontalChromeWidth(paddingX: number): number {
553
+ return this.#borderVisible ? paddingX + 1 : 0;
554
+ }
555
+
556
+ #getPromptGutterWidth(width: number, paddingX: number): number {
557
+ if (this.#borderVisible || !this.#promptGutter) return 0;
558
+ const chromeWidth = 2 * this.#getHorizontalChromeWidth(paddingX);
559
+ const availableWidth = Math.max(0, width - chromeWidth);
560
+ return Math.min(visibleWidth(this.#promptGutter), availableWidth);
561
+ }
562
+
563
+ #getPromptGutter(
564
+ width: number,
565
+ paddingX: number,
566
+ ): { firstLine: string; continuation: string; width: number } | undefined {
567
+ if (this.#borderVisible || !this.#promptGutter) return undefined;
568
+ const gutterWidth = this.#getPromptGutterWidth(width, paddingX);
569
+ if (gutterWidth === 0) return undefined;
570
+ return {
571
+ firstLine: sliceByColumn(this.#promptGutter, 0, gutterWidth, true),
572
+ continuation: padding(gutterWidth),
573
+ width: gutterWidth,
574
+ };
575
+ }
576
+
577
+ #getContentWidth(width: number, paddingX: number): number {
578
+ const chromeWidth = 2 * this.#getHorizontalChromeWidth(paddingX);
579
+ return Math.max(0, width - chromeWidth - this.#getPromptGutterWidth(width, paddingX));
580
+ }
581
+
582
+ #getLayoutWidth(width: number, paddingX: number): number {
583
+ const contentWidth = this.#getContentWidth(width, paddingX);
584
+ const cursorReserve = this.#borderVisible && paddingX === 0 ? 1 : 0;
585
+ // Keep cursor/scroll layout addressable even when a borderless prompt gutter consumes every visible column.
586
+ return Math.max(1, contentWidth - cursorReserve);
587
+ }
588
+
589
+ #getVisibleContentHeight(contentLines: number): number {
590
+ if (this.#maxHeight === undefined) return contentLines;
591
+ const verticalChrome = this.#borderVisible ? 2 : 0;
592
+ return Math.max(1, this.#maxHeight - verticalChrome);
593
+ }
594
+
595
+ /** Apply the optional input decorator to a plain (ANSI-free) text segment.
596
+ * Decoration only adds zero-width SGR codes, so visible width is unchanged. */
597
+ #decorate(text: string): string {
598
+ const decorate = this.decorateText;
599
+ return decorate !== undefined && text.length > 0 ? decorate(text) : text;
600
+ }
601
+
602
+ #getStyledInputCursor(): { text: string; width: number } {
603
+ const cursorChar = this.#theme.symbols.inputCursor;
604
+ // Keep the software cursor steady. Ghostty/cmux can leave visual
605
+ // afterimages for SGR blink cells during rapid input-row repaints.
606
+ return { text: cursorChar, width: visibleWidth(cursorChar) };
607
+ }
608
+
609
+ #renderEndOfLineCursorAtWidthLimit(
610
+ before: string,
611
+ marker: string,
612
+ maxWidth: number,
613
+ replacement?: { text: string; width: number },
614
+ ): { text: string; width: number } {
615
+ const beforeGraphemes = [...segmenter.segment(before)];
616
+ const lastGrapheme = beforeGraphemes[beforeGraphemes.length - 1]?.segment;
617
+ const lastGraphemeWidth = lastGrapheme ? visibleWidth(lastGrapheme) : 0;
618
+ const builtInCursor = this.#getStyledInputCursor();
619
+ const fallbackReplacement = lastGrapheme
620
+ ? { text: `\x1b[7m${lastGrapheme}\x1b[0m`, width: lastGraphemeWidth }
621
+ : builtInCursor;
622
+ const clampReplacement = (candidate: { text: string; width: number }): { text: string; width: number } => {
623
+ let text = sliceByColumn(candidate.text, 0, maxWidth, true);
624
+ let width = visibleWidth(text);
625
+ if (width > maxWidth) {
626
+ text = "";
627
+ width = 0;
628
+ }
629
+ return { text, width };
630
+ };
631
+
632
+ let clampedReplacement = clampReplacement(replacement ?? fallbackReplacement);
633
+ if (replacement && clampedReplacement.width === 0) {
634
+ // A custom override that cannot fit at all should first fall back to the highlighted tail.
635
+ clampedReplacement = clampReplacement(fallbackReplacement);
636
+ }
637
+ if (lastGrapheme && clampedReplacement.width === 0) {
638
+ // If even the highlighted trailing grapheme cannot fit, show the built-in single-column cursor.
639
+ clampedReplacement = clampReplacement(builtInCursor);
640
+ }
641
+
642
+ const replacedSpanWidth = Math.min(maxWidth, Math.max(lastGraphemeWidth, clampedReplacement.width));
643
+ const prefixWidth = Math.max(0, maxWidth - replacedSpanWidth);
644
+ const beforePrefix = sliceByColumn(before, 0, prefixWidth, true);
645
+ const replacementPad = padding(Math.max(0, replacedSpanWidth - clampedReplacement.width));
646
+ return {
647
+ text: `${beforePrefix}${replacementPad}${clampedReplacement.text}${marker}`,
648
+ width: visibleWidth(beforePrefix) + replacedSpanWidth,
649
+ };
650
+ }
651
+
652
+ #renderTerminalCursorMarker(text: string, marker: string, maxWidth: number): string {
653
+ if (!marker) return text;
654
+ if (visibleWidth(text) < maxWidth) {
655
+ return text + marker;
656
+ }
657
+
658
+ let insertAt = text.length;
659
+ let offset = 0;
660
+ for (const seg of segmenter.segment(text)) {
661
+ if (visibleWidth(seg.segment) > 0) {
662
+ insertAt = offset;
663
+ }
664
+ offset += seg.segment.length;
665
+ }
666
+
667
+ return `${text.slice(0, insertAt)}${marker}${text.slice(insertAt)}`;
668
+ }
669
+
670
+ #getPageScrollStep(totalVisualLines: number): number {
671
+ const visibleHeight =
672
+ this.#maxHeight === undefined ? DEFAULT_PAGE_SCROLL_LINES : this.#getVisibleContentHeight(totalVisualLines);
673
+ return Math.max(1, visibleHeight - 1);
674
+ }
675
+
676
+ #updateScrollOffset(layoutWidth: number, layoutLines: LayoutLine[], visibleHeight: number): void {
677
+ if (layoutLines.length <= visibleHeight) {
678
+ this.#scrollOffset = 0;
679
+ return;
680
+ }
681
+
682
+ const visualLines = this.#buildVisualLineMap(layoutWidth);
683
+ const cursorLine = this.#findCurrentVisualLine(visualLines);
684
+ if (cursorLine < this.#scrollOffset) {
685
+ this.#scrollOffset = cursorLine;
686
+ } else if (cursorLine >= this.#scrollOffset + visibleHeight) {
687
+ this.#scrollOffset = cursorLine - visibleHeight + 1;
688
+ }
689
+
690
+ const maxOffset = Math.max(0, layoutLines.length - visibleHeight);
691
+ this.#scrollOffset = Math.min(this.#scrollOffset, maxOffset);
692
+ }
693
+
694
+ render(width: number): string[] {
695
+ const paddingX = this.#getEditorPaddingX();
696
+ const borderVisible = this.#borderVisible;
697
+ const promptGutter = this.#getPromptGutter(width, paddingX);
698
+ const contentAreaWidth = this.#getContentWidth(width, paddingX);
699
+ const layoutWidth = this.#getLayoutWidth(width, paddingX);
700
+ this.#lastLayoutWidth = layoutWidth;
701
+
702
+ // Box-drawing characters for rounded corners
703
+ const box = this.#theme.symbols.boxRound;
704
+ const borderWidth = this.#getHorizontalChromeWidth(paddingX);
705
+ const topLeft = this.borderColor(`${box.topLeft}${box.horizontal.repeat(paddingX)}`);
706
+ const topRight = this.borderColor(`${box.horizontal.repeat(paddingX)}${box.topRight}`);
707
+ const bottomLeft = this.borderColor(`${box.bottomLeft}${box.horizontal}${padding(Math.max(0, paddingX - 1))}`);
708
+ const horizontal = this.borderColor(box.horizontal);
709
+
710
+ // Layout the text
711
+ const layoutLines = this.#layoutText(layoutWidth);
712
+ const visibleContentHeight = this.#getVisibleContentHeight(layoutLines.length);
713
+ this.#updateScrollOffset(layoutWidth, layoutLines, visibleContentHeight);
714
+ const visibleLayoutLines = layoutLines.slice(this.#scrollOffset, this.#scrollOffset + visibleContentHeight);
715
+
716
+ const result: string[] = [];
717
+
718
+ if (borderVisible) {
719
+ // Render top border: ╭─ [status content] ────────────────╮
720
+ const topFillWidth = Math.max(0, width - borderWidth * 2);
721
+ if (this.#topBorderContent) {
722
+ const { content, width: statusWidth } = this.#topBorderContent;
723
+ if (statusWidth <= topFillWidth) {
724
+ // Status fits - add fill after it
725
+ const fillWidth = topFillWidth - statusWidth;
726
+ result.push(topLeft + content + this.borderColor(box.horizontal.repeat(fillWidth)) + topRight);
727
+ } else {
728
+ // Status too long - truncate it
729
+ const truncated = truncateToWidth(content, Math.max(0, topFillWidth - 1));
730
+ const truncatedWidth = visibleWidth(truncated);
731
+ const fillWidth = Math.max(0, topFillWidth - truncatedWidth);
732
+ result.push(topLeft + truncated + this.borderColor(box.horizontal.repeat(fillWidth)) + topRight);
733
+ }
734
+ } else {
735
+ result.push(topLeft + horizontal.repeat(topFillWidth) + topRight);
736
+ }
737
+ }
738
+
739
+ // Render each layout line
740
+ // Emit hardware cursor marker only when focused and not showing autocomplete
741
+ const emitCursorMarker = this.focused && !this.#autocompleteState;
742
+ const lineContentWidth = contentAreaWidth;
743
+
744
+ // Compute inline hint text (dim ghost text after cursor)
745
+ const inlineHint = this.#getInlineHint();
746
+ const hintStyle = this.#theme.hintStyle ?? ((t: string) => `\x1b[2m${t}\x1b[0m`);
747
+
748
+ for (let visibleIndex = 0; visibleIndex < visibleLayoutLines.length; visibleIndex++) {
749
+ const layoutLine = visibleLayoutLines[visibleIndex]!;
750
+ let displayText = layoutLine.text;
751
+ let displayWidth = visibleWidth(layoutLine.text);
752
+ let cursorInPadding = false;
753
+ let decorated = false;
754
+ const showPromptGutter = promptGutter !== undefined && visibleIndex === 0;
755
+ const gutterText =
756
+ promptGutter === undefined ? "" : showPromptGutter ? promptGutter.firstLine : promptGutter.continuation;
757
+
758
+ // Add cursor if this line has it
759
+ const hasCursor = layoutLine.hasCursor && layoutLine.cursorPos !== undefined;
760
+ const marker = emitCursorMarker ? CURSOR_MARKER : "";
761
+
762
+ if (!borderVisible && displayWidth > lineContentWidth) {
763
+ displayText = sliceByColumn(displayText, 0, lineContentWidth, true);
764
+ displayWidth = visibleWidth(displayText);
765
+ }
766
+
767
+ if (!borderVisible && lineContentWidth === 0) {
768
+ if (hasCursor && !this.#useTerminalCursor) {
769
+ const zeroWidthCursorBudget = visibleWidth(gutterText);
770
+ const zeroWidthCursorReplacement = this.cursorOverride
771
+ ? { text: this.cursorOverride, width: this.cursorOverrideWidth ?? 1 }
772
+ : this.#getStyledInputCursor();
773
+ if (showPromptGutter && zeroWidthCursorBudget > 0) {
774
+ // Keep the leading prompt glyph visible when the gutter consumes the whole row.
775
+ const promptGlyph = [...segmenter.segment(gutterText)][0]?.segment ?? "";
776
+ const promptGlyphWidth = visibleWidth(promptGlyph);
777
+ const remainingCursorWidth = Math.max(0, zeroWidthCursorBudget - promptGlyphWidth);
778
+ if (remainingCursorWidth === 0) {
779
+ result.push(`\x1b[7m${promptGlyph}\x1b[0m${marker}`);
780
+ } else {
781
+ const widthLimitedCursor = this.#renderEndOfLineCursorAtWidthLimit(
782
+ "",
783
+ marker,
784
+ remainingCursorWidth,
785
+ zeroWidthCursorReplacement,
786
+ );
787
+ result.push(`${promptGlyph}${widthLimitedCursor.text}`);
788
+ }
789
+ } else {
790
+ const widthLimitedCursor = this.#renderEndOfLineCursorAtWidthLimit(
791
+ gutterText,
792
+ marker,
793
+ zeroWidthCursorBudget,
794
+ zeroWidthCursorReplacement,
795
+ );
796
+ result.push(widthLimitedCursor.text);
797
+ }
798
+ } else if (hasCursor && this.#useTerminalCursor) {
799
+ result.push(this.#renderTerminalCursorMarker(gutterText, marker, visibleWidth(gutterText)));
800
+ } else {
801
+ result.push(gutterText + (hasCursor ? marker : ""));
802
+ }
803
+ continue;
804
+ }
805
+
806
+ if (hasCursor && this.#useTerminalCursor) {
807
+ if (marker) {
808
+ const before = displayText.slice(0, layoutLine.cursorPos);
809
+ const after = displayText.slice(layoutLine.cursorPos);
810
+ if (after.length === 0 && inlineHint) {
811
+ const hintText = hintStyle(truncateToWidth(inlineHint, Math.max(0, lineContentWidth - displayWidth)));
812
+ displayText = before + marker + hintText;
813
+ displayWidth += visibleWidth(inlineHint);
814
+ } else if (after.length === 0 && !borderVisible && displayWidth >= lineContentWidth) {
815
+ displayText = this.#renderTerminalCursorMarker(before, marker, lineContentWidth);
816
+ } else {
817
+ displayText = before + marker + after;
818
+ }
819
+ }
820
+ } else if (hasCursor && !this.#useTerminalCursor) {
821
+ const before = displayText.slice(0, layoutLine.cursorPos);
822
+ const after = displayText.slice(layoutLine.cursorPos);
823
+
824
+ if (after.length > 0) {
825
+ // Cursor is on a character (grapheme) - replace it with highlighted version
826
+ // Get the first grapheme from 'after'
827
+ const afterGraphemes = [...segmenter.segment(after)];
828
+ const firstGrapheme = afterGraphemes[0]?.segment || "";
829
+ const restAfter = after.slice(firstGrapheme.length);
830
+ const cursor = `\x1b[7m${firstGrapheme}\x1b[0m`;
831
+ // Decorate the plain text on each side of the cursor glyph. The reverse-video
832
+ // reset (\x1b[0m) ends in "m" (a word char), so a boundary match on restAfter
833
+ // would fail in the whole-line fallback below — decorate the segments here.
834
+ displayText = this.#decorate(before) + marker + cursor + this.#decorate(restAfter);
835
+ decorated = true;
836
+ // displayWidth stays the same - we're replacing, not adding
837
+ } else if (this.cursorOverride) {
838
+ // Cursor override replaces the normal end-of-text cursor glyph
839
+ const overrideWidth = this.cursorOverrideWidth ?? 1;
840
+ if (!borderVisible && displayWidth + overrideWidth > lineContentWidth) {
841
+ // Borderless editors have no spare padding cell for an end-of-line cursor glyph.
842
+ // Preserve cursorOverride by replacing the tail of the line with it.
843
+ const widthLimitedCursor = this.#renderEndOfLineCursorAtWidthLimit(before, marker, lineContentWidth, {
844
+ text: this.cursorOverride,
845
+ width: overrideWidth,
846
+ });
847
+ displayText = widthLimitedCursor.text;
848
+ displayWidth = widthLimitedCursor.width;
849
+ } else if (inlineHint) {
850
+ const availWidth = Math.max(0, lineContentWidth - displayWidth - overrideWidth);
851
+ const hintText = hintStyle(truncateToWidth(inlineHint, availWidth));
852
+ displayText = before + marker + this.cursorOverride + hintText;
853
+ displayWidth += overrideWidth + Math.min(visibleWidth(inlineHint), availWidth);
854
+ } else {
855
+ displayText = before + marker + this.cursorOverride;
856
+ displayWidth += overrideWidth;
857
+ }
858
+ } else {
859
+ // Cursor is at the end - add thin cursor glyph
860
+ const { text: cursor, width: cursorWidth } = this.#getStyledInputCursor();
861
+ if (!borderVisible && displayWidth + cursorWidth > lineContentWidth) {
862
+ // Borderless editors have no spare padding cell for an end-of-line cursor glyph.
863
+ // Highlight the last grapheme so the cursor stays visible without consuming width.
864
+ const widthLimitedCursor = this.#renderEndOfLineCursorAtWidthLimit(before, marker, lineContentWidth);
865
+ displayText = widthLimitedCursor.text;
866
+ displayWidth = widthLimitedCursor.width;
867
+ } else if (inlineHint) {
868
+ const availWidth = Math.max(0, lineContentWidth - displayWidth - cursorWidth);
869
+ const hintText = hintStyle(truncateToWidth(inlineHint, availWidth));
870
+ displayText = before + marker + cursor + hintText;
871
+ displayWidth += cursorWidth + Math.min(visibleWidth(inlineHint), availWidth);
872
+ } else {
873
+ displayText = before + marker + cursor;
874
+ displayWidth += cursorWidth;
875
+ }
876
+ if (displayWidth > lineContentWidth && paddingX > 0) {
877
+ cursorInPadding = true;
878
+ }
879
+ }
880
+ }
881
+
882
+ // No cursor on this line, or a branch that left the user text intact: decorate the
883
+ // whole line. CURSOR_MARKER and cursor glyphs begin with ESC, so word boundaries
884
+ // around a decorated keyword stay intact when matched against the assembled line.
885
+ if (!decorated) {
886
+ displayText = this.#decorate(displayText);
887
+ }
888
+
889
+ const linePad = padding(Math.max(0, lineContentWidth - displayWidth));
890
+
891
+ if (!borderVisible) {
892
+ result.push(gutterText + displayText + linePad);
893
+ continue;
894
+ }
895
+
896
+ // All lines have consistent borders based on padding
897
+ const isLastLine = visibleIndex === visibleLayoutLines.length - 1;
898
+ const rightPaddingWidth = Math.max(0, paddingX - (cursorInPadding ? 1 : 0));
899
+ if (isLastLine) {
900
+ const bottomRightPadding = Math.max(0, paddingX - 1 - (cursorInPadding ? 1 : 0));
901
+ const bottomRightAdjusted = this.borderColor(
902
+ `${padding(bottomRightPadding)}${box.horizontal}${box.bottomRight}`,
903
+ );
904
+ result.push(`${bottomLeft}${displayText}${linePad}${bottomRightAdjusted}`);
905
+ } else {
906
+ const leftBorder = this.borderColor(`${box.vertical}${padding(paddingX)}`);
907
+ const rightBorder = this.borderColor(`${padding(rightPaddingWidth)}${box.vertical}`);
908
+ result.push(leftBorder + displayText + linePad + rightBorder);
909
+ }
910
+ }
911
+
912
+ // Add autocomplete list if active
913
+ if (this.#autocompleteState && this.#autocompleteList) {
914
+ const autocompleteResult = this.#autocompleteList.render(width);
915
+ result.push(...autocompleteResult);
916
+ }
917
+
918
+ return result;
919
+ }
920
+
921
+ handleInput(data: string): void {
922
+ const kb = getKeybindings();
923
+
924
+ // Handle character jump mode (awaiting next character to jump to)
925
+ if (this.#jumpMode !== null) {
926
+ // Cancel if the hotkey is pressed again
927
+ if (kb.matches(data, "tui.editor.jumpForward") || kb.matches(data, "tui.editor.jumpBackward")) {
928
+ this.#jumpMode = null;
929
+ return;
930
+ }
931
+
932
+ const printableText = extractPrintableText(data);
933
+ if (printableText) {
934
+ const direction = this.#jumpMode;
935
+ this.#jumpMode = null;
936
+ this.#jumpToChar(printableText, direction);
937
+ return;
938
+ }
939
+
940
+ // Control character - cancel and fall through to normal handling
941
+ this.#jumpMode = null;
942
+ }
943
+
944
+ // Handle bracketed paste mode
945
+ const paste = this.#pasteHandler.process(data);
946
+ if (paste.handled) {
947
+ if (paste.pasteContent !== undefined) {
948
+ this.#handlePaste(paste.pasteContent);
949
+ if (paste.remaining.length > 0) {
950
+ this.handleInput(paste.remaining);
951
+ }
952
+ }
953
+ return;
954
+ }
955
+
956
+ // Handle special key combinations first
957
+
958
+ // Ctrl+C is reserved by parent components for app-level handling.
959
+ // Do not consume arbitrary user-bound "copy" keys here, since the editor
960
+ // has no copy implementation and would make those keys disappear.
961
+ if (matchesKey(data, "ctrl+c")) {
962
+ return;
963
+ }
964
+
965
+ // Undo
966
+ if (kb.matches(data, "tui.editor.undo")) {
967
+ this.#applyUndo();
968
+ return;
969
+ }
970
+
971
+ // Handle autocomplete special keys first (but don't block other input)
972
+ if (this.#autocompleteState && this.#autocompleteList) {
973
+ // Escape - cancel autocomplete
974
+ if (kb.matches(data, "tui.select.cancel")) {
975
+ this.#cancelAutocomplete(true);
976
+ return;
977
+ }
978
+ // Let the autocomplete list handle navigation and selection
979
+ else if (
980
+ kb.matches(data, "tui.select.up") ||
981
+ kb.matches(data, "tui.select.down") ||
982
+ kb.matches(data, "tui.select.pageUp") ||
983
+ kb.matches(data, "tui.select.pageDown") ||
984
+ kb.matches(data, "tui.input.submit") ||
985
+ data === "\n" ||
986
+ kb.matches(data, "tui.input.tab")
987
+ ) {
988
+ // Only pass navigation keys to the list, not Enter/Tab (we handle those directly)
989
+ if (
990
+ kb.matches(data, "tui.select.up") ||
991
+ kb.matches(data, "tui.select.down") ||
992
+ kb.matches(data, "tui.select.pageUp") ||
993
+ kb.matches(data, "tui.select.pageDown")
994
+ ) {
995
+ this.#autocompleteList.handleInput(data);
996
+ this.onAutocompleteUpdate?.();
997
+ return;
998
+ }
999
+
1000
+ // If Tab was pressed, always apply the selection
1001
+ if (kb.matches(data, "tui.input.tab")) {
1002
+ const selected = this.#autocompleteList.getSelectedItem();
1003
+ if (selected && this.#autocompleteProvider) {
1004
+ const shouldChainSlashCommandAutocomplete = this.#isSlashCommandNameAutocompleteSelection();
1005
+ const result = this.#autocompleteProvider.applyCompletion(
1006
+ this.#state.lines,
1007
+ this.#state.cursorLine,
1008
+ this.#state.cursorCol,
1009
+ selected,
1010
+ this.#autocompletePrefix,
1011
+ );
1012
+
1013
+ this.#state.lines = result.lines;
1014
+ this.#state.cursorLine = result.cursorLine;
1015
+ this.#setCursorCol(result.cursorCol);
1016
+
1017
+ this.#cancelAutocomplete();
1018
+ this.onAutocompleteUpdate?.();
1019
+
1020
+ if (this.onChange) {
1021
+ this.onChange(this.getText());
1022
+ }
1023
+
1024
+ result.onApplied?.();
1025
+
1026
+ if (shouldChainSlashCommandAutocomplete && this.#isCompletedSlashCommandAtCursor()) {
1027
+ void this.#tryTriggerAutocomplete();
1028
+ }
1029
+ }
1030
+ return;
1031
+ }
1032
+
1033
+ // If Enter was pressed on a slash command, apply completion and submit
1034
+ if ((kb.matches(data, "tui.input.submit") || data === "\n") && this.#autocompletePrefix.startsWith("/")) {
1035
+ // Check for stale autocomplete state due to debounce
1036
+ const currentLine = this.#state.lines[this.#state.cursorLine] ?? "";
1037
+ const currentTextBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
1038
+ if (currentTextBeforeCursor !== this.#autocompletePrefix) {
1039
+ // Autocomplete is stale - cancel and fall through to normal submission
1040
+ this.#cancelAutocomplete();
1041
+ } else {
1042
+ const selected = this.#autocompleteList.getSelectedItem();
1043
+ if (selected && this.#autocompleteProvider) {
1044
+ const result = this.#autocompleteProvider.applyCompletion(
1045
+ this.#state.lines,
1046
+ this.#state.cursorLine,
1047
+ this.#state.cursorCol,
1048
+ selected,
1049
+ this.#autocompletePrefix,
1050
+ );
1051
+
1052
+ this.#state.lines = result.lines;
1053
+ this.#state.cursorLine = result.cursorLine;
1054
+ this.#setCursorCol(result.cursorCol);
1055
+ result.onApplied?.();
1056
+ }
1057
+ this.#cancelAutocomplete();
1058
+ }
1059
+ // Don't return - fall through to submission logic
1060
+ }
1061
+ // If Enter was pressed on a file path, apply completion
1062
+ else if (kb.matches(data, "tui.input.submit") || data === "\n") {
1063
+ const selected = this.#autocompleteList.getSelectedItem();
1064
+ if (selected && this.#autocompleteProvider) {
1065
+ const result = this.#autocompleteProvider.applyCompletion(
1066
+ this.#state.lines,
1067
+ this.#state.cursorLine,
1068
+ this.#state.cursorCol,
1069
+ selected,
1070
+ this.#autocompletePrefix,
1071
+ );
1072
+
1073
+ this.#state.lines = result.lines;
1074
+ this.#state.cursorLine = result.cursorLine;
1075
+ this.#setCursorCol(result.cursorCol);
1076
+
1077
+ this.#cancelAutocomplete();
1078
+ this.onAutocompleteUpdate?.();
1079
+
1080
+ if (this.onChange) {
1081
+ this.onChange(this.getText());
1082
+ }
1083
+
1084
+ result.onApplied?.();
1085
+ }
1086
+ return;
1087
+ }
1088
+ }
1089
+ // For other keys (like regular typing), DON'T return here
1090
+ // Let them fall through to normal character handling
1091
+ }
1092
+
1093
+ // Tab key - context-aware completion (but not when already autocompleting)
1094
+ if (kb.matches(data, "tui.input.tab") && !this.#autocompleteState) {
1095
+ this.#handleTabCompletion();
1096
+ return;
1097
+ }
1098
+
1099
+ // Continue with rest of input handling
1100
+ // Ctrl+K - Delete to end of line
1101
+ if (matchesKey(data, "ctrl+k")) {
1102
+ this.#deleteToEndOfLine();
1103
+ }
1104
+ // Ctrl+U - Delete to start of line
1105
+ else if (matchesKey(data, "ctrl+u")) {
1106
+ this.#deleteToStartOfLine();
1107
+ }
1108
+ // Ctrl+W - Delete word backwards
1109
+ else if (matchesKey(data, "ctrl+w")) {
1110
+ this.#deleteWordBackwards();
1111
+ }
1112
+ // Option/Alt+Backspace - Delete word backwards
1113
+ else if (matchesKey(data, "alt+backspace")) {
1114
+ this.#deleteWordBackwards();
1115
+ }
1116
+ // Option/Alt+D - Delete word forwards
1117
+ else if (matchesKey(data, "alt+d") || matchesKey(data, "alt+delete")) {
1118
+ this.#deleteWordForwards();
1119
+ }
1120
+ // Ctrl+Y - Yank from kill ring
1121
+ else if (matchesKey(data, "ctrl+y")) {
1122
+ this.#yankFromKillRing();
1123
+ }
1124
+ // Alt+Y - Yank-pop (cycle kill ring)
1125
+ else if (matchesKey(data, "alt+y")) {
1126
+ this.#yankPop();
1127
+ }
1128
+ // Ctrl+A - Move to start of line
1129
+ else if (matchesKey(data, "ctrl+a")) {
1130
+ this.#moveToLineStart();
1131
+ }
1132
+ // Ctrl+E - Move to end of line
1133
+ else if (matchesKey(data, "ctrl+e")) {
1134
+ this.#moveToLineEnd();
1135
+ }
1136
+ // Alt+Enter - special handler if callback exists, otherwise new line
1137
+ else if (matchesKey(data, "alt+enter")) {
1138
+ if (this.onAltEnter) {
1139
+ this.onAltEnter(this.getText());
1140
+ } else {
1141
+ this.#addNewLine();
1142
+ }
1143
+ }
1144
+ // New line
1145
+ else if (
1146
+ (data.charCodeAt(0) === 10 && data.length > 1) || // Ctrl+Enter with modifiers
1147
+ matchesKey(data, "ctrl+enter") || // Ctrl+Enter (Kitty/modifyOtherKeys, including lock bits/keypad Enter)
1148
+ data === "\x1b\r" || // Option+Enter in some terminals (legacy)
1149
+ data === "\x1b[13;2~" || // Shift+Enter in some terminals (legacy format)
1150
+ kb.matches(data, "tui.input.newLine") || // Shift+Enter (Kitty protocol, handles lock bits)
1151
+ (data.length > 1 && data.includes("\x1b") && data.includes("\r")) ||
1152
+ (data === "\n" && data.length === 1) // Shift+Enter from iTerm2 mapping
1153
+ ) {
1154
+ if (this.#shouldSubmitOnBackslashEnter(data, kb)) {
1155
+ this.#handleBackspace();
1156
+ this.#submitValue();
1157
+ return;
1158
+ }
1159
+ this.#addNewLine();
1160
+ }
1161
+ // Plain Enter - submit (handles both legacy \r and Kitty protocol with lock bits)
1162
+ else if (kb.matches(data, "tui.input.submit") || data === "\n") {
1163
+ // If submit is disabled, do nothing
1164
+ if (this.disableSubmit) {
1165
+ return;
1166
+ }
1167
+
1168
+ // Synchronous slash command completion for the race condition where
1169
+ // async autocomplete hasn't resolved yet (user types /q quickly + Enter).
1170
+ // Match the existing selected-item behavior when autocomplete IS showing.
1171
+ if (!this.#autocompleteState) {
1172
+ const currentLine = this.#state.lines[this.#state.cursorLine] ?? "";
1173
+ const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
1174
+ if (
1175
+ textBeforeCursor.startsWith("/") &&
1176
+ this.#isInSubmittedSlashCommandContext() &&
1177
+ this.#autocompleteProvider?.trySyncSlashCompletion
1178
+ ) {
1179
+ const syncResult = this.#autocompleteProvider.trySyncSlashCompletion(textBeforeCursor);
1180
+ if (syncResult && syncResult.items.length > 0) {
1181
+ // Invalidate any pending async autocomplete so its stale results are discarded
1182
+ this.#autocompleteRequestId += 1;
1183
+ // Apply the best match and submit the completed command
1184
+ const selected = syncResult.items[0]!;
1185
+ const result = this.#autocompleteProvider.applyCompletion(
1186
+ this.#state.lines,
1187
+ this.#state.cursorLine,
1188
+ this.#state.cursorCol,
1189
+ selected,
1190
+ syncResult.prefix,
1191
+ );
1192
+ this.#state.lines = result.lines;
1193
+ this.#state.cursorLine = result.cursorLine;
1194
+ this.#setCursorCol(result.cursorCol);
1195
+ result.onApplied?.();
1196
+ }
1197
+ }
1198
+ }
1199
+
1200
+ this.#submitValue();
1201
+ }
1202
+ // Backspace (including Shift+Backspace)
1203
+ else if (kb.matches(data, "tui.editor.deleteCharBackward") || matchesKey(data, "shift+backspace")) {
1204
+ this.#handleBackspace();
1205
+ }
1206
+ // Line navigation shortcuts (Home/End keys)
1207
+ else if (kb.matches(data, "tui.editor.cursorLineStart")) {
1208
+ this.#moveToLineStart();
1209
+ } else if (kb.matches(data, "tui.editor.cursorLineEnd")) {
1210
+ this.#moveToLineEnd();
1211
+ }
1212
+ // Page navigation (PageUp/PageDown)
1213
+ else if (kb.matches(data, "tui.editor.pageUp")) {
1214
+ if (this.#isEditorEmpty()) {
1215
+ this.#navigateHistory(-1);
1216
+ } else if (this.#historyIndex > -1 && this.#isOnFirstVisualLine()) {
1217
+ this.#navigateHistory(-1);
1218
+ } else {
1219
+ this.#pageScroll(-1);
1220
+ }
1221
+ } else if (kb.matches(data, "tui.editor.pageDown")) {
1222
+ if (this.#historyIndex > -1 && this.#isOnLastVisualLine()) {
1223
+ this.#navigateHistory(1);
1224
+ } else {
1225
+ this.#pageScroll(1);
1226
+ }
1227
+ }
1228
+ // Forward delete (Fn+Backspace or Delete key, including Shift+Delete)
1229
+ else if (kb.matches(data, "tui.editor.deleteCharForward") || matchesKey(data, "shift+delete")) {
1230
+ this.#handleForwardDelete();
1231
+ }
1232
+ // Word navigation (Option/Alt + Arrow or Ctrl + Arrow)
1233
+ else if (kb.matches(data, "tui.editor.cursorWordLeft")) {
1234
+ // Word left
1235
+ this.#resetKillSequence();
1236
+ this.#moveWordBackwards();
1237
+ } else if (kb.matches(data, "tui.editor.cursorWordRight")) {
1238
+ // Word right
1239
+ this.#resetKillSequence();
1240
+ this.#moveWordForwards();
1241
+ }
1242
+ // Arrow keys
1243
+ else if (kb.matches(data, "tui.editor.cursorUp")) {
1244
+ // Up - history navigation or cursor movement
1245
+ if (this.#isEditorEmpty()) {
1246
+ this.#navigateHistory(-1); // Start browsing history
1247
+ } else if (this.#historyIndex > -1 && this.#isOnFirstVisualLine()) {
1248
+ this.#navigateHistory(-1); // Navigate to older history entry
1249
+ } else if (this.#isOnFirstVisualLine()) {
1250
+ // Already at top - jump to start of line
1251
+ this.#moveToLineStart();
1252
+ } else {
1253
+ this.#moveCursor(-1, 0); // Cursor movement (within text or history entry)
1254
+ }
1255
+ } else if (kb.matches(data, "tui.editor.cursorDown")) {
1256
+ // Down - history navigation or cursor movement
1257
+ if (this.#historyIndex > -1 && this.#isOnLastVisualLine()) {
1258
+ this.#navigateHistory(1); // Navigate to newer history entry or clear
1259
+ } else if (this.#isOnLastVisualLine()) {
1260
+ // Already at bottom - jump to end of line
1261
+ this.#moveToLineEnd();
1262
+ } else {
1263
+ this.#moveCursor(1, 0); // Cursor movement (within text or history entry)
1264
+ }
1265
+ } else if (kb.matches(data, "tui.editor.cursorRight")) {
1266
+ // Right
1267
+ this.#moveCursor(0, 1);
1268
+ } else if (kb.matches(data, "tui.editor.cursorLeft")) {
1269
+ // Left
1270
+ this.#moveCursor(0, -1);
1271
+ }
1272
+ // Shift+Space - insert regular space (Kitty protocol sends escape sequence)
1273
+ else if (matchesKey(data, "shift+space")) {
1274
+ this.#insertCharacter(" ");
1275
+ }
1276
+ // Character jump mode triggers
1277
+ else if (kb.matches(data, "tui.editor.jumpForward")) {
1278
+ this.#jumpMode = "forward";
1279
+ } else if (kb.matches(data, "tui.editor.jumpBackward")) {
1280
+ this.#jumpMode = "backward";
1281
+ }
1282
+ // Printable keystrokes, including Kitty CSI-u text-producing sequences.
1283
+ else {
1284
+ const printableText = extractPrintableText(data);
1285
+ if (printableText) {
1286
+ this.#insertCharacter(printableText);
1287
+ }
1288
+ }
1289
+ }
1290
+
1291
+ #layoutText(contentWidth: number): LayoutLine[] {
1292
+ const layoutLines: LayoutLine[] = [];
1293
+
1294
+ if (this.#state.lines.length === 0 || (this.#state.lines.length === 1 && this.#state.lines[0] === "")) {
1295
+ // Empty editor
1296
+ layoutLines.push({
1297
+ text: "",
1298
+ hasCursor: true,
1299
+ cursorPos: 0,
1300
+ });
1301
+ return layoutLines;
1302
+ }
1303
+
1304
+ // Process each logical line
1305
+ for (let i = 0; i < this.#state.lines.length; i++) {
1306
+ const line = this.#state.lines[i] || "";
1307
+ const isCurrentLine = i === this.#state.cursorLine;
1308
+ const lineVisibleWidth = visibleWidth(line);
1309
+
1310
+ if (lineVisibleWidth <= contentWidth) {
1311
+ // Line fits in one layout line
1312
+ if (isCurrentLine) {
1313
+ layoutLines.push({
1314
+ text: line,
1315
+ hasCursor: true,
1316
+ cursorPos: this.#state.cursorCol,
1317
+ });
1318
+ } else {
1319
+ layoutLines.push({
1320
+ text: line,
1321
+ hasCursor: false,
1322
+ });
1323
+ }
1324
+ } else {
1325
+ // Line needs wrapping - use word-aware wrapping
1326
+ const chunks = wordWrapLine(line, contentWidth);
1327
+
1328
+ for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
1329
+ const chunk = chunks[chunkIndex];
1330
+ if (!chunk) continue;
1331
+
1332
+ const cursorPos = this.#state.cursorCol;
1333
+ const isLastChunk = chunkIndex === chunks.length - 1;
1334
+
1335
+ // Determine if cursor is in this chunk
1336
+ // For word-wrapped chunks, we need to handle the case where
1337
+ // cursor might be in trimmed whitespace at end of chunk
1338
+ let hasCursorInChunk = false;
1339
+ let adjustedCursorPos = 0;
1340
+
1341
+ if (isCurrentLine) {
1342
+ if (isLastChunk) {
1343
+ // Last chunk: cursor belongs here if >= startIndex
1344
+ hasCursorInChunk = cursorPos >= chunk.startIndex;
1345
+ adjustedCursorPos = cursorPos - chunk.startIndex;
1346
+ } else {
1347
+ // Non-last chunk: cursor belongs here if in range [startIndex, endIndex)
1348
+ // But we need to handle the visual position in the trimmed text
1349
+ hasCursorInChunk = cursorPos >= chunk.startIndex && cursorPos < chunk.endIndex;
1350
+ if (hasCursorInChunk) {
1351
+ adjustedCursorPos = cursorPos - chunk.startIndex;
1352
+ // Clamp to text length (in case cursor was in trimmed whitespace)
1353
+ if (adjustedCursorPos > chunk.text.length) {
1354
+ adjustedCursorPos = chunk.text.length;
1355
+ }
1356
+ }
1357
+ }
1358
+ }
1359
+
1360
+ if (hasCursorInChunk) {
1361
+ layoutLines.push({
1362
+ text: chunk.text,
1363
+ hasCursor: true,
1364
+ cursorPos: adjustedCursorPos,
1365
+ });
1366
+ } else {
1367
+ layoutLines.push({
1368
+ text: chunk.text,
1369
+ hasCursor: false,
1370
+ });
1371
+ }
1372
+ }
1373
+ }
1374
+ }
1375
+
1376
+ return layoutLines;
1377
+ }
1378
+
1379
+ getText(): string {
1380
+ return this.#state.lines.join("\n");
1381
+ }
1382
+
1383
+ #expandPasteMarkers(text: string): string {
1384
+ let result = text;
1385
+ for (const [pasteId, pasteContent] of this.#pastes) {
1386
+ const markerRegex = new RegExp(`\\[paste #${pasteId}( (\\+\\d+ lines|\\d+ chars))?\\]`, "g");
1387
+ result = result.replace(markerRegex, () => pasteContent);
1388
+ }
1389
+ return result;
1390
+ }
1391
+
1392
+ /**
1393
+ * Get text with paste markers expanded to their actual content.
1394
+ * Use this when you need the full content (e.g., for external editor).
1395
+ */
1396
+ getExpandedText(): string {
1397
+ return this.#expandPasteMarkers(this.#state.lines.join("\n"));
1398
+ }
1399
+
1400
+ getLines(): string[] {
1401
+ return [...this.#state.lines];
1402
+ }
1403
+
1404
+ getCursor(): { line: number; col: number } {
1405
+ return { line: this.#state.cursorLine, col: this.#state.cursorCol };
1406
+ }
1407
+
1408
+ moveToLineStart(): void {
1409
+ this.#moveToLineStart();
1410
+ }
1411
+
1412
+ moveToLineEnd(): void {
1413
+ this.#moveToLineEnd();
1414
+ }
1415
+
1416
+ moveToMessageStart(): void {
1417
+ this.#moveToMessageStart();
1418
+ }
1419
+
1420
+ moveToMessageEnd(): void {
1421
+ this.#moveToMessageEnd();
1422
+ }
1423
+
1424
+ /**
1425
+ * Undo the last meaningful edit while ignoring transient text that is still present at the cursor.
1426
+ * Used for command-like autocomplete actions whose typed trigger should not count as the edit being undone.
1427
+ */
1428
+ undoPastTransientText(transientText: string): void {
1429
+ if (transientText.length === 0) {
1430
+ this.#applyUndo();
1431
+ return;
1432
+ }
1433
+
1434
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
1435
+ const transientStartCol = this.#state.cursorCol - transientText.length;
1436
+ if (transientStartCol < 0 || currentLine.slice(transientStartCol, this.#state.cursorCol) !== transientText) {
1437
+ this.#applyUndo();
1438
+ return;
1439
+ }
1440
+
1441
+ const beforeTransient = currentLine.slice(0, transientStartCol);
1442
+ const afterTransient = currentLine.slice(this.#state.cursorCol);
1443
+ this.#historyIndex = -1;
1444
+ this.#resetKillSequence();
1445
+ this.#preferredVisualCol = null;
1446
+ this.#state.lines[this.#state.cursorLine] = beforeTransient + afterTransient;
1447
+ this.#setCursorCol(transientStartCol);
1448
+
1449
+ while (true) {
1450
+ const snapshot = this.#undoStack.at(-1);
1451
+ if (
1452
+ !snapshot ||
1453
+ !this.#matchesTransientUndoSnapshot(
1454
+ snapshot,
1455
+ transientText,
1456
+ transientStartCol,
1457
+ beforeTransient,
1458
+ afterTransient,
1459
+ )
1460
+ ) {
1461
+ break;
1462
+ }
1463
+ this.#undoStack.pop();
1464
+ }
1465
+
1466
+ if (this.#undoStack.length === 0) {
1467
+ if (this.onChange) {
1468
+ this.onChange(this.getText());
1469
+ }
1470
+ return;
1471
+ }
1472
+
1473
+ this.#applyUndo();
1474
+ }
1475
+
1476
+ setText(text: string): void {
1477
+ this.#historyIndex = -1; // Exit history browsing mode
1478
+ this.#resetKillSequence();
1479
+ this.#setTextInternal(text);
1480
+ }
1481
+
1482
+ #exitHistoryForEditing(): void {
1483
+ if (this.#historyIndex === -1) return;
1484
+ if (this.#state.cursorLine === 0 && this.#state.cursorCol === 0) {
1485
+ this.#state.cursorLine = this.#state.lines.length - 1;
1486
+ const line = this.#state.lines[this.#state.cursorLine] || "";
1487
+ this.#setCursorCol(line.length);
1488
+ }
1489
+ this.#historyIndex = -1;
1490
+ }
1491
+
1492
+ /** Insert text at the current cursor position */
1493
+ insertText(text: string): void {
1494
+ this.#exitHistoryForEditing();
1495
+ this.#insertTextAtCursor(text);
1496
+ }
1497
+
1498
+ // All the editor methods from before...
1499
+ #insertCharacter(char: string): void {
1500
+ this.#exitHistoryForEditing();
1501
+ this.#resetKillSequence();
1502
+ this.#recordUndoState();
1503
+
1504
+ const line = this.#state.lines[this.#state.cursorLine] || "";
1505
+
1506
+ const before = line.slice(0, this.#state.cursorCol);
1507
+ const after = line.slice(this.#state.cursorCol);
1508
+
1509
+ this.#state.lines[this.#state.cursorLine] = before + char + after;
1510
+ this.#setCursorCol(this.#state.cursorCol + char.length);
1511
+
1512
+ if (this.onChange) {
1513
+ this.onChange(this.getText());
1514
+ }
1515
+
1516
+ // Synchronous inline replacement (e.g. emoji shortcodes `:joy:` → 😂).
1517
+ // Runs before autocomplete trigger so the popup doesn't briefly chase a
1518
+ // prefix that's about to be rewritten.
1519
+ if (char.length === 1 && this.#autocompleteProvider?.trySyncInlineReplace) {
1520
+ const replaceLine = this.#state.lines[this.#state.cursorLine] || "";
1521
+ const textBeforeCursor = replaceLine.slice(0, this.#state.cursorCol);
1522
+ const replacement = this.#autocompleteProvider.trySyncInlineReplace(textBeforeCursor);
1523
+ if (replacement) {
1524
+ const before = replaceLine.slice(0, this.#state.cursorCol - replacement.replaceLen);
1525
+ const after = replaceLine.slice(this.#state.cursorCol);
1526
+ this.#state.lines[this.#state.cursorLine] = before + replacement.insert + after;
1527
+ this.#setCursorCol(before.length + replacement.insert.length);
1528
+ if (this.onChange) {
1529
+ this.onChange(this.getText());
1530
+ }
1531
+ if (this.#autocompleteState) {
1532
+ this.#cancelAutocomplete();
1533
+ this.onAutocompleteUpdate?.();
1534
+ }
1535
+ return;
1536
+ }
1537
+ }
1538
+
1539
+ // Check if we should trigger or update autocomplete
1540
+ if (!this.#autocompleteState) {
1541
+ // Auto-trigger for "/" at the start of a line (slash commands)
1542
+ if (char === "/" && this.#isAtStartOfSubmittedMessage()) {
1543
+ this.#tryTriggerAutocomplete();
1544
+ }
1545
+ // Auto-trigger for "@" file reference (fuzzy search)
1546
+ else if (char === "@") {
1547
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
1548
+ const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
1549
+ // Only trigger if @ is after whitespace or at start of line
1550
+ const charBeforeAt = textBeforeCursor[textBeforeCursor.length - 2];
1551
+ if (textBeforeCursor.length === 1 || charBeforeAt === " " || charBeforeAt === "\t") {
1552
+ this.#tryTriggerAutocomplete();
1553
+ }
1554
+ }
1555
+ // Auto-trigger for "#" prompt actions anywhere in the current token
1556
+ else if (char === "#") {
1557
+ this.#tryTriggerAutocomplete();
1558
+ }
1559
+ // Also auto-trigger when typing letters/path chars in a completable context
1560
+ else if (/[a-zA-Z0-9.\-_/]/.test(char)) {
1561
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
1562
+ const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
1563
+ // Check if we're in a slash command (with or without space for arguments)
1564
+ if (this.#isInSubmittedSlashCommandContext()) {
1565
+ this.#tryTriggerAutocomplete();
1566
+ }
1567
+ // Check if we're in an @ file reference context
1568
+ else if (textBeforeCursor.match(/(?:^|[\s])@[^\s]*$/)) {
1569
+ this.#tryTriggerAutocomplete();
1570
+ }
1571
+ // Check if we're in a # prompt action context
1572
+ else if (textBeforeCursor.match(/#[^\s#]*$/)) {
1573
+ this.#tryTriggerAutocomplete();
1574
+ }
1575
+ // Check if we're in a :emoji shortcode context
1576
+ else if (textBeforeCursor.match(/(?:^|[\s([{>]):[a-zA-Z0-9_+-]*$/)) {
1577
+ this.#tryTriggerAutocomplete();
1578
+ }
1579
+ // Check if we're typing an internal URL scheme (e.g. local://, skill://)
1580
+ else if (this.#textTriggersUrlAutocomplete(textBeforeCursor)) {
1581
+ this.#tryTriggerAutocomplete();
1582
+ }
1583
+ }
1584
+ } else {
1585
+ this.#debouncedUpdateAutocomplete();
1586
+ }
1587
+ }
1588
+
1589
+ #handlePaste(pastedText: string): void {
1590
+ this.#historyIndex = -1; // Exit history browsing mode
1591
+ this.#resetKillSequence();
1592
+ this.#recordUndoState();
1593
+
1594
+ this.#withUndoSuspended(() => {
1595
+ // Some terminals (e.g. tmux popups with extended-keys-format=csi-u) re-encode
1596
+ // control bytes inside bracketed paste as CSI-u Ctrl+<letter> sequences
1597
+ // (ESC [ <codepoint> ; 5 u). Decode those back to their literal byte so the
1598
+ // per-char filter below preserves newlines instead of stripping ESC and
1599
+ // leaking the printable tail (e.g. "[106;5u") into the editor.
1600
+ const decodedText = pastedText.replace(/\x1b\[(\d+);5u/g, (match, code) => {
1601
+ const cp = Number(code);
1602
+ if (cp >= 97 && cp <= 122) return String.fromCharCode(cp - 96);
1603
+ if (cp >= 65 && cp <= 90) return String.fromCharCode(cp - 64);
1604
+ return match;
1605
+ });
1606
+
1607
+ // Clean the pasted text. NFC-normalize so macOS Finder drag-drops of
1608
+ // Korean filenames (which arrive as NFD: e.g. `ᄒ`+`ᅪ` instead of `화`)
1609
+ // land in the buffer as the same precomposed syllables a terminal
1610
+ // renders — without this, cursor column accounting drifts by
1611
+ // `(NFD cells − NFC cells)` and the visible glyph desyncs from the
1612
+ // hardware cursor. Matches the `Input` component's prior fix; this
1613
+ // is the same fix on the real Prometheus prompt component (`Editor`).
1614
+ const cleanText = decodedText.replace(/\r\n?/g, "\n").normalize("NFC");
1615
+
1616
+ // Convert tabs to spaces (4 spaces per tab)
1617
+ const tabExpandedText = cleanText.replace(/\t/g, " ");
1618
+
1619
+ // Filter out non-printable characters except newlines
1620
+ let filteredText = tabExpandedText
1621
+ .split("")
1622
+ .filter(char => char === "\n" || char.charCodeAt(0) >= 32)
1623
+ .join("");
1624
+
1625
+ // If pasting a file path (starts with /, ~, or .) and the character before
1626
+ // the cursor is a word character, prepend a space for better readability
1627
+ if (/^[/~.]/.test(filteredText)) {
1628
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
1629
+ const charBeforeCursor = this.#state.cursorCol > 0 ? currentLine[this.#state.cursorCol - 1] : "";
1630
+ if (charBeforeCursor && /\w/.test(charBeforeCursor)) {
1631
+ filteredText = ` ${filteredText}`;
1632
+ }
1633
+ }
1634
+
1635
+ // Split into lines
1636
+ const pastedLines = filteredText.split("\n");
1637
+
1638
+ // Check if this is a large paste (> 10 lines or > 1000 characters)
1639
+ const totalChars = filteredText.length;
1640
+ if (pastedLines.length > 10 || totalChars > 1000) {
1641
+ // Store the paste and insert a marker
1642
+ this.#pasteCounter++;
1643
+ const pasteId = this.#pasteCounter;
1644
+ this.#pastes.set(pasteId, filteredText);
1645
+
1646
+ // Insert marker like "[paste #1 +123 lines]" or "[paste #1 1234 chars]"
1647
+ const marker =
1648
+ pastedLines.length > 10
1649
+ ? `[paste #${pasteId} +${pastedLines.length} lines]`
1650
+ : `[paste #${pasteId} ${totalChars} chars]`;
1651
+ this.#insertTextAtCursor(marker);
1652
+
1653
+ return;
1654
+ }
1655
+
1656
+ if (pastedLines.length === 1) {
1657
+ // Single line - insert character by character to trigger autocomplete
1658
+ for (const char of filteredText) {
1659
+ this.#insertCharacter(char);
1660
+ }
1661
+ return;
1662
+ }
1663
+
1664
+ // Multi-line paste - use insertTextAtCursor for proper handling
1665
+ this.#insertTextAtCursor(filteredText);
1666
+ });
1667
+ }
1668
+
1669
+ #addNewLine(): void {
1670
+ this.#historyIndex = -1; // Exit history browsing mode
1671
+ this.#resetKillSequence();
1672
+ this.#recordUndoState();
1673
+
1674
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
1675
+
1676
+ const before = currentLine.slice(0, this.#state.cursorCol);
1677
+ const after = currentLine.slice(this.#state.cursorCol);
1678
+
1679
+ // Split current line
1680
+ this.#state.lines[this.#state.cursorLine] = before;
1681
+ this.#state.lines.splice(this.#state.cursorLine + 1, 0, after);
1682
+
1683
+ // Move cursor to start of new line
1684
+ this.#state.cursorLine++;
1685
+ this.#setCursorCol(0);
1686
+
1687
+ if (this.onChange) {
1688
+ this.onChange(this.getText());
1689
+ }
1690
+ }
1691
+
1692
+ #shouldSubmitOnBackslashEnter(data: string, kb: KeybindingsManager): boolean {
1693
+ if (this.disableSubmit) return false;
1694
+ if (!matchesKey(data, "enter")) return false;
1695
+ const submitKeys = kb.getKeys("tui.input.submit");
1696
+ const hasShiftEnter = submitKeys.includes("shift+enter") || submitKeys.includes("shift+return");
1697
+ if (!hasShiftEnter) return false;
1698
+
1699
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
1700
+ return this.#state.cursorCol > 0 && currentLine[this.#state.cursorCol - 1] === "\\";
1701
+ }
1702
+
1703
+ #submitValue(): void {
1704
+ this.#resetKillSequence();
1705
+
1706
+ const result = this.#expandPasteMarkers(this.#state.lines.join("\n")).trim();
1707
+
1708
+ this.#state = { lines: [""], cursorLine: 0, cursorCol: 0 };
1709
+ this.#pastes.clear();
1710
+ this.#pasteCounter = 0;
1711
+ this.#historyIndex = -1;
1712
+ this.#scrollOffset = 0;
1713
+ this.#undoStack.length = 0;
1714
+
1715
+ if (this.onChange) this.onChange("");
1716
+ if (this.onSubmit) this.onSubmit(result);
1717
+ }
1718
+
1719
+ #handleBackspace(): void {
1720
+ this.#historyIndex = -1; // Exit history browsing mode
1721
+ this.#resetKillSequence();
1722
+ this.#recordUndoState();
1723
+
1724
+ if (this.#state.cursorCol > 0) {
1725
+ // Delete grapheme before cursor (handles emojis, combining characters, etc.)
1726
+ const line = this.#state.lines[this.#state.cursorLine] || "";
1727
+ const beforeCursor = line.slice(0, this.#state.cursorCol);
1728
+
1729
+ // Find the last grapheme in the text before cursor
1730
+ const graphemes = [...segmenter.segment(beforeCursor)];
1731
+ const lastGrapheme = graphemes[graphemes.length - 1];
1732
+ const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;
1733
+
1734
+ const before = line.slice(0, this.#state.cursorCol - graphemeLength);
1735
+ const after = line.slice(this.#state.cursorCol);
1736
+
1737
+ this.#state.lines[this.#state.cursorLine] = before + after;
1738
+ this.#setCursorCol(this.#state.cursorCol - graphemeLength);
1739
+ } else if (this.#state.cursorLine > 0) {
1740
+ // Merge with previous line
1741
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
1742
+ const previousLine = this.#state.lines[this.#state.cursorLine - 1] || "";
1743
+
1744
+ this.#state.lines[this.#state.cursorLine - 1] = previousLine + currentLine;
1745
+ this.#state.lines.splice(this.#state.cursorLine, 1);
1746
+
1747
+ this.#state.cursorLine--;
1748
+ this.#setCursorCol(previousLine.length);
1749
+ }
1750
+
1751
+ if (this.onChange) {
1752
+ this.onChange(this.getText());
1753
+ }
1754
+
1755
+ // Update or re-trigger autocomplete after backspace
1756
+ if (this.#autocompleteState) {
1757
+ this.#debouncedUpdateAutocomplete();
1758
+ } else {
1759
+ // If autocomplete was cancelled (no matches), re-trigger if we're in a completable context
1760
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
1761
+ const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
1762
+ // Slash command context
1763
+ if (this.#isInSubmittedSlashCommandContext()) {
1764
+ this.#tryTriggerAutocomplete();
1765
+ }
1766
+ // @ file reference context
1767
+ else if (textBeforeCursor.match(/(?:^|[\s])@[^\s]*$/)) {
1768
+ this.#tryTriggerAutocomplete();
1769
+ }
1770
+ // # prompt action context
1771
+ else if (textBeforeCursor.match(/#[^\s#]*$/)) {
1772
+ this.#tryTriggerAutocomplete();
1773
+ }
1774
+ // internal URL scheme context (e.g. local://, skill://)
1775
+ else if (this.#textTriggersUrlAutocomplete(textBeforeCursor)) {
1776
+ this.#tryTriggerAutocomplete();
1777
+ }
1778
+ }
1779
+ }
1780
+
1781
+ /**
1782
+ * Set cursor column and clear preferredVisualCol.
1783
+ * Use this for all non-vertical cursor movements to reset sticky column behavior.
1784
+ */
1785
+ #setCursorCol(col: number): void {
1786
+ this.#state.cursorCol = col;
1787
+ this.#preferredVisualCol = null;
1788
+ }
1789
+
1790
+ /**
1791
+ * Move cursor to a target visual line, applying sticky column logic.
1792
+ * Shared by moveCursor() and pageScroll().
1793
+ */
1794
+ #moveToVisualLine(
1795
+ visualLines: Array<{ logicalLine: number; startCol: number; length: number }>,
1796
+ currentVisualLine: number,
1797
+ targetVisualLine: number,
1798
+ ): void {
1799
+ const currentVL = visualLines[currentVisualLine];
1800
+ const targetVL = visualLines[targetVisualLine];
1801
+
1802
+ if (currentVL && targetVL) {
1803
+ const currentVisualCol = this.#state.cursorCol - currentVL.startCol;
1804
+
1805
+ // For non-last segments, clamp to length-1 to stay within the segment
1806
+ const isLastSourceSegment =
1807
+ currentVisualLine === visualLines.length - 1 ||
1808
+ visualLines[currentVisualLine + 1]?.logicalLine !== currentVL.logicalLine;
1809
+ const sourceMaxVisualCol = isLastSourceSegment ? currentVL.length : Math.max(0, currentVL.length - 1);
1810
+
1811
+ const isLastTargetSegment =
1812
+ targetVisualLine === visualLines.length - 1 ||
1813
+ visualLines[targetVisualLine + 1]?.logicalLine !== targetVL.logicalLine;
1814
+ const targetMaxVisualCol = isLastTargetSegment ? targetVL.length : Math.max(0, targetVL.length - 1);
1815
+
1816
+ const moveToVisualCol = this.#computeVerticalMoveColumn(
1817
+ currentVisualCol,
1818
+ sourceMaxVisualCol,
1819
+ targetMaxVisualCol,
1820
+ );
1821
+
1822
+ // Set cursor position
1823
+ this.#state.cursorLine = targetVL.logicalLine;
1824
+ const targetCol = targetVL.startCol + moveToVisualCol;
1825
+ const logicalLine = this.#state.lines[targetVL.logicalLine] || "";
1826
+ this.#state.cursorCol = Math.min(targetCol, logicalLine.length);
1827
+ }
1828
+ }
1829
+
1830
+ /**
1831
+ * Compute the target visual column for vertical cursor movement.
1832
+ * Implements the sticky column decision table.
1833
+ */
1834
+ #computeVerticalMoveColumn(
1835
+ currentVisualCol: number,
1836
+ sourceMaxVisualCol: number,
1837
+ targetMaxVisualCol: number,
1838
+ ): number {
1839
+ const hasPreferred = this.#preferredVisualCol !== null;
1840
+ const cursorInMiddle = currentVisualCol < sourceMaxVisualCol;
1841
+ const targetTooShort = targetMaxVisualCol < currentVisualCol;
1842
+
1843
+ if (!hasPreferred || cursorInMiddle) {
1844
+ if (targetTooShort) {
1845
+ this.#preferredVisualCol = currentVisualCol;
1846
+ return targetMaxVisualCol;
1847
+ }
1848
+ this.#preferredVisualCol = null;
1849
+ return currentVisualCol;
1850
+ }
1851
+
1852
+ const targetCantFitPreferred = targetMaxVisualCol < this.#preferredVisualCol!;
1853
+ if (targetTooShort || targetCantFitPreferred) {
1854
+ return targetMaxVisualCol;
1855
+ }
1856
+
1857
+ const result = this.#preferredVisualCol!;
1858
+ this.#preferredVisualCol = null;
1859
+ return result;
1860
+ }
1861
+
1862
+ #moveToLineStart(): void {
1863
+ this.#resetKillSequence();
1864
+ this.#setCursorCol(0);
1865
+ }
1866
+
1867
+ #moveToLineEnd(): void {
1868
+ this.#resetKillSequence();
1869
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
1870
+ this.#setCursorCol(currentLine.length);
1871
+ }
1872
+
1873
+ #moveToMessageStart(): void {
1874
+ this.#resetKillSequence();
1875
+ this.#state.cursorLine = 0;
1876
+ this.#setCursorCol(0);
1877
+ }
1878
+
1879
+ #moveToMessageEnd(): void {
1880
+ this.#resetKillSequence();
1881
+ this.#state.cursorLine = this.#state.lines.length - 1;
1882
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
1883
+ this.#setCursorCol(currentLine.length);
1884
+ }
1885
+
1886
+ #resetKillSequence(): void {
1887
+ this.#lastAction = null;
1888
+ }
1889
+
1890
+ #withUndoSuspended<T>(fn: () => T): T {
1891
+ const wasSuspended = this.#suspendUndo;
1892
+ this.#suspendUndo = true;
1893
+ try {
1894
+ return fn();
1895
+ } finally {
1896
+ this.#suspendUndo = wasSuspended;
1897
+ }
1898
+ }
1899
+
1900
+ #recordUndoState(): void {
1901
+ if (this.#suspendUndo) return;
1902
+ this.#undoStack.push(structuredClone(this.#state));
1903
+ }
1904
+
1905
+ #applyUndo(): void {
1906
+ const snapshot = this.#undoStack.pop();
1907
+ if (!snapshot) return;
1908
+
1909
+ this.#historyIndex = -1;
1910
+ this.#resetKillSequence();
1911
+ this.#preferredVisualCol = null;
1912
+ Object.assign(this.#state, snapshot);
1913
+
1914
+ if (this.onChange) {
1915
+ this.onChange(this.getText());
1916
+ }
1917
+
1918
+ if (this.#autocompleteState) {
1919
+ this.#debouncedUpdateAutocomplete();
1920
+ } else {
1921
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
1922
+ const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
1923
+ if (this.#isInSubmittedSlashCommandContext()) {
1924
+ this.#tryTriggerAutocomplete();
1925
+ } else if (textBeforeCursor.match(/(?:^|[\s])@[^\s]*$/)) {
1926
+ this.#tryTriggerAutocomplete();
1927
+ } else if (textBeforeCursor.match(/#[^\s#]*$/)) {
1928
+ this.#tryTriggerAutocomplete();
1929
+ } else if (this.#textTriggersUrlAutocomplete(textBeforeCursor)) {
1930
+ this.#tryTriggerAutocomplete();
1931
+ }
1932
+ }
1933
+ }
1934
+
1935
+ #matchesTransientUndoSnapshot(
1936
+ snapshot: EditorState,
1937
+ transientText: string,
1938
+ transientStartCol: number,
1939
+ beforeTransient: string,
1940
+ afterTransient: string,
1941
+ ): boolean {
1942
+ if (snapshot.cursorLine !== this.#state.cursorLine) return false;
1943
+ if (snapshot.lines.length !== this.#state.lines.length) return false;
1944
+
1945
+ const transientLength = snapshot.cursorCol - transientStartCol;
1946
+ if (transientLength < 0 || transientLength >= transientText.length) return false;
1947
+
1948
+ for (let i = 0; i < snapshot.lines.length; i++) {
1949
+ if (i === this.#state.cursorLine) continue;
1950
+ if (snapshot.lines[i] !== this.#state.lines[i]) return false;
1951
+ }
1952
+
1953
+ return (
1954
+ snapshot.lines[snapshot.cursorLine] ===
1955
+ beforeTransient + transientText.slice(0, transientLength) + afterTransient
1956
+ );
1957
+ }
1958
+
1959
+ #recordKill(text: string, direction: "forward" | "backward", accumulate = this.#lastAction === "kill"): void {
1960
+ if (!text) return;
1961
+ this.#killRing.push(text, { prepend: direction === "backward", accumulate });
1962
+ this.#lastAction = "kill";
1963
+ }
1964
+
1965
+ #insertTextAtCursor(text: string): void {
1966
+ this.#historyIndex = -1;
1967
+ this.#resetKillSequence();
1968
+ this.#recordUndoState();
1969
+
1970
+ const normalized = text.replace(/\r\n?/g, "\n");
1971
+ const lines = normalized.split("\n");
1972
+
1973
+ if (lines.length === 1) {
1974
+ const line = this.#state.lines[this.#state.cursorLine] || "";
1975
+ const before = line.slice(0, this.#state.cursorCol);
1976
+ const after = line.slice(this.#state.cursorCol);
1977
+ this.#state.lines[this.#state.cursorLine] = before + normalized + after;
1978
+ this.#setCursorCol(this.#state.cursorCol + normalized.length);
1979
+ } else {
1980
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
1981
+ const beforeCursor = currentLine.slice(0, this.#state.cursorCol);
1982
+ const afterCursor = currentLine.slice(this.#state.cursorCol);
1983
+
1984
+ const newLines: string[] = [];
1985
+ for (let i = 0; i < this.#state.cursorLine; i++) {
1986
+ newLines.push(this.#state.lines[i] || "");
1987
+ }
1988
+
1989
+ newLines.push(beforeCursor + (lines[0] || ""));
1990
+ for (let i = 1; i < lines.length - 1; i++) {
1991
+ newLines.push(lines[i] || "");
1992
+ }
1993
+ newLines.push((lines[lines.length - 1] || "") + afterCursor);
1994
+
1995
+ for (let i = this.#state.cursorLine + 1; i < this.#state.lines.length; i++) {
1996
+ newLines.push(this.#state.lines[i] || "");
1997
+ }
1998
+
1999
+ this.#state.lines = newLines;
2000
+ this.#state.cursorLine += lines.length - 1;
2001
+ this.#setCursorCol((lines[lines.length - 1] || "").length);
2002
+ }
2003
+
2004
+ if (this.onChange) {
2005
+ this.onChange(this.getText());
2006
+ }
2007
+ }
2008
+
2009
+ #yankFromKillRing(): void {
2010
+ const text = this.#killRing.peek();
2011
+ if (!text) return;
2012
+ this.#insertTextAtCursor(text);
2013
+ this.#lastAction = "yank";
2014
+ }
2015
+
2016
+ #yankPop(): void {
2017
+ if (this.#lastAction !== "yank") return;
2018
+ if (this.#killRing.length <= 1) return;
2019
+
2020
+ this.#historyIndex = -1;
2021
+ this.#recordUndoState();
2022
+
2023
+ this.#withUndoSuspended(() => {
2024
+ if (!this.#deleteYankedText()) return;
2025
+ this.#killRing.rotate();
2026
+ const text = this.#killRing.peek();
2027
+ if (text) {
2028
+ this.#insertTextAtCursor(text);
2029
+ }
2030
+ });
2031
+
2032
+ this.#lastAction = "yank";
2033
+ }
2034
+
2035
+ /**
2036
+ * Delete the most recently yanked text from the buffer.
2037
+ *
2038
+ * This is a best-effort operation and assumes the cursor is still positioned
2039
+ * at the end of the yanked text.
2040
+ */
2041
+ #deleteYankedText(): boolean {
2042
+ const yankedText = this.#killRing.peek();
2043
+ if (!yankedText) return false;
2044
+
2045
+ const yankLines = yankedText.split("\n");
2046
+ const endLine = this.#state.cursorLine;
2047
+ const endCol = this.#state.cursorCol;
2048
+ const startLine = endLine - (yankLines.length - 1);
2049
+ if (startLine < 0) return false;
2050
+
2051
+ if (yankLines.length === 1) {
2052
+ const line = this.#state.lines[endLine] ?? "";
2053
+ const startCol = endCol - yankedText.length;
2054
+ if (startCol < 0) return false;
2055
+ if (line.slice(startCol, endCol) !== yankedText) return false;
2056
+
2057
+ this.#state.lines[endLine] = line.slice(0, startCol) + line.slice(endCol);
2058
+ this.#state.cursorLine = endLine;
2059
+ this.#setCursorCol(startCol);
2060
+ return true;
2061
+ }
2062
+
2063
+ const firstInserted = yankLines[0] ?? "";
2064
+ const lastInserted = yankLines[yankLines.length - 1] ?? "";
2065
+ const firstLineText = this.#state.lines[startLine] ?? "";
2066
+ const lastLineText = this.#state.lines[endLine] ?? "";
2067
+
2068
+ if (!firstLineText.endsWith(firstInserted)) return false;
2069
+ if (endCol !== lastInserted.length) return false;
2070
+ if (lastLineText.slice(0, endCol) !== lastInserted) return false;
2071
+
2072
+ const startCol = firstLineText.length - firstInserted.length;
2073
+ if (startCol < 0) return false;
2074
+
2075
+ const suffix = lastLineText.slice(endCol);
2076
+ const newLine = firstLineText.slice(0, startCol) + suffix;
2077
+
2078
+ this.#state.lines.splice(startLine, yankLines.length, newLine);
2079
+ this.#state.cursorLine = startLine;
2080
+ this.#setCursorCol(startCol);
2081
+ return true;
2082
+ }
2083
+
2084
+ #deleteToStartOfLine(): void {
2085
+ this.#historyIndex = -1; // Exit history browsing mode
2086
+ this.#recordUndoState();
2087
+
2088
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2089
+ let deletedText = "";
2090
+
2091
+ if (this.#state.cursorCol > 0) {
2092
+ // Delete from start of line up to cursor
2093
+ deletedText = currentLine.slice(0, this.#state.cursorCol);
2094
+ this.#state.lines[this.#state.cursorLine] = currentLine.slice(this.#state.cursorCol);
2095
+ this.#setCursorCol(0);
2096
+ } else if (this.#state.cursorLine > 0) {
2097
+ // At start of line - merge with previous line
2098
+ deletedText = "\n";
2099
+ const previousLine = this.#state.lines[this.#state.cursorLine - 1] || "";
2100
+ this.#state.lines[this.#state.cursorLine - 1] = previousLine + currentLine;
2101
+ this.#state.lines.splice(this.#state.cursorLine, 1);
2102
+ this.#state.cursorLine--;
2103
+ this.#setCursorCol(previousLine.length);
2104
+ }
2105
+
2106
+ this.#recordKill(deletedText, "backward");
2107
+
2108
+ if (this.onChange) {
2109
+ this.onChange(this.getText());
2110
+ }
2111
+ }
2112
+
2113
+ #deleteToEndOfLine(): void {
2114
+ this.#historyIndex = -1; // Exit history browsing mode
2115
+ this.#recordUndoState();
2116
+
2117
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2118
+ let deletedText = "";
2119
+
2120
+ if (this.#state.cursorCol < currentLine.length) {
2121
+ // Delete from cursor to end of line
2122
+ deletedText = currentLine.slice(this.#state.cursorCol);
2123
+ this.#state.lines[this.#state.cursorLine] = currentLine.slice(0, this.#state.cursorCol);
2124
+ } else if (this.#state.cursorLine < this.#state.lines.length - 1) {
2125
+ // At end of line - merge with next line
2126
+ const nextLine = this.#state.lines[this.#state.cursorLine + 1] || "";
2127
+ deletedText = "\n";
2128
+ this.#state.lines[this.#state.cursorLine] = currentLine + nextLine;
2129
+ this.#state.lines.splice(this.#state.cursorLine + 1, 1);
2130
+ }
2131
+
2132
+ this.#recordKill(deletedText, "forward");
2133
+
2134
+ if (this.onChange) {
2135
+ this.onChange(this.getText());
2136
+ }
2137
+ }
2138
+
2139
+ #deleteWordBackwards(): void {
2140
+ this.#historyIndex = -1; // Exit history browsing mode
2141
+ this.#recordUndoState();
2142
+
2143
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2144
+
2145
+ // If at start of line, behave like backspace at column 0 (merge with previous line)
2146
+ if (this.#state.cursorCol === 0) {
2147
+ if (this.#state.cursorLine > 0) {
2148
+ this.#recordKill("\n", "backward");
2149
+ const previousLine = this.#state.lines[this.#state.cursorLine - 1] || "";
2150
+ this.#state.lines[this.#state.cursorLine - 1] = previousLine + currentLine;
2151
+ this.#state.lines.splice(this.#state.cursorLine, 1);
2152
+ this.#state.cursorLine--;
2153
+ this.#setCursorCol(previousLine.length);
2154
+ }
2155
+ } else {
2156
+ const oldCursorCol = this.#state.cursorCol;
2157
+ this.#moveWordBackwards();
2158
+ const deleteFrom = this.#state.cursorCol;
2159
+ this.#setCursorCol(oldCursorCol);
2160
+
2161
+ const deletedText = currentLine.slice(deleteFrom, oldCursorCol);
2162
+ this.#state.lines[this.#state.cursorLine] =
2163
+ currentLine.slice(0, deleteFrom) + currentLine.slice(this.#state.cursorCol);
2164
+ this.#setCursorCol(deleteFrom);
2165
+ this.#recordKill(deletedText, "backward");
2166
+ }
2167
+
2168
+ if (this.onChange) {
2169
+ this.onChange(this.getText());
2170
+ }
2171
+ }
2172
+
2173
+ #deleteWordForwards(): void {
2174
+ this.#historyIndex = -1; // Exit history browsing mode
2175
+ this.#recordUndoState();
2176
+
2177
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2178
+
2179
+ if (this.#state.cursorCol >= currentLine.length) {
2180
+ if (this.#state.cursorLine < this.#state.lines.length - 1) {
2181
+ this.#recordKill("\n", "forward");
2182
+ const nextLine = this.#state.lines[this.#state.cursorLine + 1] || "";
2183
+ this.#state.lines[this.#state.cursorLine] = currentLine + nextLine;
2184
+ this.#state.lines.splice(this.#state.cursorLine + 1, 1);
2185
+ }
2186
+ } else {
2187
+ const oldCursorCol = this.#state.cursorCol;
2188
+ this.#moveWordForwards();
2189
+ const deleteTo = this.#state.cursorCol;
2190
+ this.#setCursorCol(oldCursorCol);
2191
+
2192
+ const deletedText = currentLine.slice(oldCursorCol, deleteTo);
2193
+ this.#state.lines[this.#state.cursorLine] = currentLine.slice(0, oldCursorCol) + currentLine.slice(deleteTo);
2194
+ this.#recordKill(deletedText, "forward");
2195
+ }
2196
+
2197
+ if (this.onChange) {
2198
+ this.onChange(this.getText());
2199
+ }
2200
+ }
2201
+
2202
+ #handleForwardDelete(): void {
2203
+ this.#historyIndex = -1; // Exit history browsing mode
2204
+ this.#resetKillSequence();
2205
+ this.#recordUndoState();
2206
+
2207
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2208
+
2209
+ if (this.#state.cursorCol < currentLine.length) {
2210
+ // Delete grapheme at cursor position (handles emojis, combining characters, etc.)
2211
+ const afterCursor = currentLine.slice(this.#state.cursorCol);
2212
+
2213
+ // Find the first grapheme at cursor
2214
+ const graphemes = [...segmenter.segment(afterCursor)];
2215
+ const firstGrapheme = graphemes[0];
2216
+ const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;
2217
+
2218
+ const before = currentLine.slice(0, this.#state.cursorCol);
2219
+ const after = currentLine.slice(this.#state.cursorCol + graphemeLength);
2220
+ this.#state.lines[this.#state.cursorLine] = before + after;
2221
+ } else if (this.#state.cursorLine < this.#state.lines.length - 1) {
2222
+ // At end of line - merge with next line
2223
+ const nextLine = this.#state.lines[this.#state.cursorLine + 1] || "";
2224
+ this.#state.lines[this.#state.cursorLine] = currentLine + nextLine;
2225
+ this.#state.lines.splice(this.#state.cursorLine + 1, 1);
2226
+ }
2227
+
2228
+ if (this.onChange) {
2229
+ this.onChange(this.getText());
2230
+ }
2231
+
2232
+ // Update or re-trigger autocomplete after forward delete
2233
+ if (this.#autocompleteState) {
2234
+ this.#debouncedUpdateAutocomplete();
2235
+ } else {
2236
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2237
+ const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol);
2238
+ // Slash command context
2239
+ if (this.#isInSubmittedSlashCommandContext()) {
2240
+ this.#tryTriggerAutocomplete();
2241
+ }
2242
+ // @ file reference context
2243
+ else if (textBeforeCursor.match(/(?:^|[\s])@[^\s]*$/)) {
2244
+ this.#tryTriggerAutocomplete();
2245
+ }
2246
+ // # prompt action context
2247
+ else if (textBeforeCursor.match(/#[^\s#]*$/)) {
2248
+ this.#tryTriggerAutocomplete();
2249
+ }
2250
+ // internal URL scheme context (e.g. local://, skill://)
2251
+ else if (this.#textTriggersUrlAutocomplete(textBeforeCursor)) {
2252
+ this.#tryTriggerAutocomplete();
2253
+ }
2254
+ }
2255
+ }
2256
+
2257
+ /**
2258
+ * Build a mapping from visual lines to logical positions.
2259
+ * Returns an array where each element represents a visual line with:
2260
+ * - logicalLine: index into this.#state.lines
2261
+ * - startCol: starting column in the logical line
2262
+ * - length: length of this visual line segment
2263
+ */
2264
+ #buildVisualLineMap(width: number): Array<{ logicalLine: number; startCol: number; length: number }> {
2265
+ const visualLines: Array<{ logicalLine: number; startCol: number; length: number }> = [];
2266
+
2267
+ for (let i = 0; i < this.#state.lines.length; i++) {
2268
+ const line = this.#state.lines[i] || "";
2269
+ const lineVisWidth = visibleWidth(line);
2270
+ if (line.length === 0) {
2271
+ // Empty line still takes one visual line
2272
+ visualLines.push({ logicalLine: i, startCol: 0, length: 0 });
2273
+ } else if (lineVisWidth <= width) {
2274
+ visualLines.push({ logicalLine: i, startCol: 0, length: line.length });
2275
+ } else {
2276
+ // Line needs wrapping - use word-aware wrapping
2277
+ const chunks = wordWrapLine(line, width);
2278
+ for (const chunk of chunks) {
2279
+ visualLines.push({
2280
+ logicalLine: i,
2281
+ startCol: chunk.startIndex,
2282
+ length: chunk.endIndex - chunk.startIndex,
2283
+ });
2284
+ }
2285
+ }
2286
+ }
2287
+
2288
+ return visualLines;
2289
+ }
2290
+
2291
+ /**
2292
+ * Find the visual line index for the current cursor position.
2293
+ */
2294
+ #findCurrentVisualLine(visualLines: Array<{ logicalLine: number; startCol: number; length: number }>): number {
2295
+ for (let i = 0; i < visualLines.length; i++) {
2296
+ const vl = visualLines[i];
2297
+ if (!vl) continue;
2298
+ if (vl.logicalLine === this.#state.cursorLine) {
2299
+ const colInSegment = this.#state.cursorCol - vl.startCol;
2300
+ // Cursor is in this segment if it's within range
2301
+ // For the last segment of a logical line, cursor can be at length (end position)
2302
+ const isLastSegmentOfLine =
2303
+ i === visualLines.length - 1 || visualLines[i + 1]?.logicalLine !== vl.logicalLine;
2304
+ if (colInSegment >= 0 && (colInSegment < vl.length || (isLastSegmentOfLine && colInSegment <= vl.length))) {
2305
+ return i;
2306
+ }
2307
+ }
2308
+ }
2309
+ // Fallback: return last visual line
2310
+ return visualLines.length - 1;
2311
+ }
2312
+
2313
+ #moveCursor(deltaLine: number, deltaCol: number): void {
2314
+ this.#resetKillSequence();
2315
+ const visualLines = this.#buildVisualLineMap(this.#lastLayoutWidth);
2316
+ const currentVisualLine = this.#findCurrentVisualLine(visualLines);
2317
+
2318
+ if (deltaLine !== 0) {
2319
+ const targetVisualLine = currentVisualLine + deltaLine;
2320
+
2321
+ if (targetVisualLine >= 0 && targetVisualLine < visualLines.length) {
2322
+ this.#moveToVisualLine(visualLines, currentVisualLine, targetVisualLine);
2323
+ }
2324
+ }
2325
+
2326
+ if (deltaCol !== 0) {
2327
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2328
+
2329
+ if (deltaCol > 0) {
2330
+ // Moving right - move by one grapheme (handles emojis, combining characters, etc.)
2331
+ if (this.#state.cursorCol < currentLine.length) {
2332
+ const afterCursor = currentLine.slice(this.#state.cursorCol);
2333
+ const graphemes = [...segmenter.segment(afterCursor)];
2334
+ const firstGrapheme = graphemes[0];
2335
+ this.#setCursorCol(this.#state.cursorCol + (firstGrapheme ? firstGrapheme.segment.length : 1));
2336
+ } else if (this.#state.cursorLine < this.#state.lines.length - 1) {
2337
+ // Wrap to start of next logical line
2338
+ this.#state.cursorLine++;
2339
+ this.#setCursorCol(0);
2340
+ } else {
2341
+ // At end of last line - can't move, but set preferredVisualCol for up/down navigation
2342
+ const currentVL = visualLines[currentVisualLine];
2343
+ if (currentVL) {
2344
+ this.#preferredVisualCol = this.#state.cursorCol - currentVL.startCol;
2345
+ }
2346
+ }
2347
+ } else {
2348
+ // Moving left - move by one grapheme (handles emojis, combining characters, etc.)
2349
+ if (this.#state.cursorCol > 0) {
2350
+ const beforeCursor = currentLine.slice(0, this.#state.cursorCol);
2351
+ const graphemes = [...segmenter.segment(beforeCursor)];
2352
+ const lastGrapheme = graphemes[graphemes.length - 1];
2353
+ this.#setCursorCol(this.#state.cursorCol - (lastGrapheme ? lastGrapheme.segment.length : 1));
2354
+ } else if (this.#state.cursorLine > 0) {
2355
+ // Wrap to end of previous logical line
2356
+ this.#state.cursorLine--;
2357
+ const prevLine = this.#state.lines[this.#state.cursorLine] || "";
2358
+ this.#setCursorCol(prevLine.length);
2359
+ }
2360
+ }
2361
+ }
2362
+ }
2363
+
2364
+ #pageScroll(direction: -1 | 1): void {
2365
+ this.#resetKillSequence();
2366
+ const visualLines = this.#buildVisualLineMap(this.#lastLayoutWidth);
2367
+ const currentVisualLine = this.#findCurrentVisualLine(visualLines);
2368
+ const step = this.#getPageScrollStep(visualLines.length);
2369
+ const targetVisualLine = Math.max(0, Math.min(visualLines.length - 1, currentVisualLine + direction * step));
2370
+ if (targetVisualLine === currentVisualLine) return;
2371
+ this.#moveToVisualLine(visualLines, currentVisualLine, targetVisualLine);
2372
+ }
2373
+
2374
+ #moveWordBackwards(): void {
2375
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2376
+
2377
+ // If at start of line, move to end of previous line
2378
+ if (this.#state.cursorCol === 0) {
2379
+ if (this.#state.cursorLine > 0) {
2380
+ this.#state.cursorLine--;
2381
+ const prevLine = this.#state.lines[this.#state.cursorLine] || "";
2382
+ this.#setCursorCol(prevLine.length);
2383
+ }
2384
+ return;
2385
+ }
2386
+
2387
+ this.#setCursorCol(moveWordLeft(currentLine, this.#state.cursorCol));
2388
+ }
2389
+
2390
+ /**
2391
+ * Jump to the first occurrence of a character in the specified direction.
2392
+ * Multi-line search. Case-sensitive. Skips the current cursor position.
2393
+ */
2394
+ #jumpToChar(char: string, direction: "forward" | "backward"): void {
2395
+ this.#resetKillSequence();
2396
+ const isForward = direction === "forward";
2397
+ const lines = this.#state.lines;
2398
+
2399
+ const end = isForward ? lines.length : -1;
2400
+ const step = isForward ? 1 : -1;
2401
+
2402
+ for (let lineIdx = this.#state.cursorLine; lineIdx !== end; lineIdx += step) {
2403
+ const line = lines[lineIdx] || "";
2404
+ const isCurrentLine = lineIdx === this.#state.cursorLine;
2405
+
2406
+ // Current line: start after/before cursor; other lines: search full line
2407
+ const searchFrom = isCurrentLine
2408
+ ? isForward
2409
+ ? this.#state.cursorCol + 1
2410
+ : this.#state.cursorCol - 1
2411
+ : undefined;
2412
+
2413
+ const idx = isForward ? line.indexOf(char, searchFrom) : line.lastIndexOf(char, searchFrom);
2414
+
2415
+ if (idx !== -1) {
2416
+ this.#state.cursorLine = lineIdx;
2417
+ this.#setCursorCol(idx);
2418
+ return;
2419
+ }
2420
+ }
2421
+ // No match found - cursor stays in place
2422
+ }
2423
+
2424
+ #moveWordForwards(): void {
2425
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2426
+
2427
+ // If at end of line, move to start of next line
2428
+ if (this.#state.cursorCol >= currentLine.length) {
2429
+ if (this.#state.cursorLine < this.#state.lines.length - 1) {
2430
+ this.#state.cursorLine++;
2431
+ this.#setCursorCol(0);
2432
+ }
2433
+ return;
2434
+ }
2435
+
2436
+ this.#setCursorCol(moveWordRight(currentLine, this.#state.cursorCol));
2437
+ }
2438
+
2439
+ #hasOnlyWhitespaceBeforeCursorLine(): boolean {
2440
+ for (let i = 0; i < this.#state.cursorLine; i++) {
2441
+ if ((this.#state.lines[i] || "").trim() !== "") {
2442
+ return false;
2443
+ }
2444
+ }
2445
+ return true;
2446
+ }
2447
+
2448
+ // Slash commands execute only when the submitted prompt starts with the command.
2449
+ #isAtStartOfSubmittedMessage(): boolean {
2450
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2451
+ const beforeCursor = currentLine.slice(0, this.#state.cursorCol);
2452
+
2453
+ return this.#hasOnlyWhitespaceBeforeCursorLine() && (beforeCursor.trim() === "" || beforeCursor.trim() === "/");
2454
+ }
2455
+
2456
+ #isInSubmittedSlashCommandContext(): boolean {
2457
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2458
+ const beforeCursor = currentLine.slice(0, this.#state.cursorCol);
2459
+ return this.#hasOnlyWhitespaceBeforeCursorLine() && beforeCursor.trimStart().startsWith("/");
2460
+ }
2461
+
2462
+ #isSlashCommandNameAutocompleteSelection(): boolean {
2463
+ if (this.#autocompleteState !== "regular") {
2464
+ return false;
2465
+ }
2466
+
2467
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2468
+ const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol).trimStart();
2469
+ return (
2470
+ this.#isInSubmittedSlashCommandContext() && textBeforeCursor.startsWith("/") && !textBeforeCursor.includes(" ")
2471
+ );
2472
+ }
2473
+
2474
+ #isCompletedSlashCommandAtCursor(): boolean {
2475
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2476
+ if (this.#state.cursorCol !== currentLine.length) {
2477
+ return false;
2478
+ }
2479
+
2480
+ const textBeforeCursor = currentLine.slice(0, this.#state.cursorCol).trimStart();
2481
+ return this.#isInSubmittedSlashCommandContext() && /^\/\S+ $/.test(textBeforeCursor);
2482
+ }
2483
+
2484
+ // Autocomplete methods
2485
+ /**
2486
+ * Whether the text ending at the cursor looks like a `scheme://` URL token.
2487
+ * Generic by design: any scheme triggers a suggestion fetch and the active
2488
+ * provider decides whether it has candidates (returning none is a no-op).
2489
+ * MUST stay in sync with the token grammar in coding-agent's
2490
+ * `internal-url-autocomplete.ts`.
2491
+ */
2492
+ #textTriggersUrlAutocomplete(textBeforeCursor: string): boolean {
2493
+ return /(?:^|[\s"'`(<=])[a-z][a-z0-9+.-]*:\/{1,2}[^\s"'`()<>]*$/i.test(textBeforeCursor);
2494
+ }
2495
+
2496
+ async #tryTriggerAutocomplete(explicitTab: boolean = false): Promise<void> {
2497
+ if (!this.#autocompleteProvider) return;
2498
+ // Check if we should trigger file completion on Tab
2499
+ if (explicitTab) {
2500
+ const provider = this.#autocompleteProvider as CombinedAutocompleteProvider;
2501
+ const shouldTrigger =
2502
+ !provider.shouldTriggerFileCompletion ||
2503
+ provider.shouldTriggerFileCompletion(this.#state.lines, this.#state.cursorLine, this.#state.cursorCol);
2504
+ if (!shouldTrigger) {
2505
+ return;
2506
+ }
2507
+ }
2508
+
2509
+ const requestId = ++this.#autocompleteRequestId;
2510
+
2511
+ const suggestions = await this.#autocompleteProvider.getSuggestions(
2512
+ this.#state.lines,
2513
+ this.#state.cursorLine,
2514
+ this.#state.cursorCol,
2515
+ );
2516
+ if (requestId !== this.#autocompleteRequestId) return;
2517
+
2518
+ if (suggestions && Array.isArray(suggestions.items) && suggestions.items.length > 0) {
2519
+ this.#autocompletePrefix = suggestions.prefix;
2520
+ this.#autocompleteList = this.#createAutocompleteList(suggestions.prefix, suggestions.items);
2521
+ this.#autocompleteState = "regular";
2522
+ this.onAutocompleteUpdate?.();
2523
+ } else {
2524
+ this.#cancelAutocomplete();
2525
+ this.onAutocompleteUpdate?.();
2526
+ }
2527
+ }
2528
+ #createAutocompleteList(
2529
+ prefix: string,
2530
+ items: Array<{ value: string; label: string; description?: string }>,
2531
+ ): SelectList {
2532
+ const layout = prefix.startsWith("/") ? SLASH_COMMAND_SELECT_LIST_LAYOUT : AUTOCOMPLETE_SELECT_LIST_LAYOUT;
2533
+ return new SelectList(items, this.#autocompleteMaxVisible, this.#theme.selectList, layout);
2534
+ }
2535
+
2536
+ #handleTabCompletion(): void {
2537
+ if (!this.#autocompleteProvider) return;
2538
+
2539
+ const currentLine = this.#state.lines[this.#state.cursorLine] || "";
2540
+ const beforeCursor = currentLine.slice(0, this.#state.cursorCol);
2541
+
2542
+ // Check if we're in a slash command context
2543
+ if (this.#isInSubmittedSlashCommandContext() && !beforeCursor.trimStart().includes(" ")) {
2544
+ this.#handleSlashCommandCompletion();
2545
+ } else {
2546
+ this.#forceFileAutocomplete(true);
2547
+ }
2548
+ }
2549
+
2550
+ #handleSlashCommandCompletion(): void {
2551
+ this.#tryTriggerAutocomplete(true);
2552
+ }
2553
+
2554
+ /*
2555
+ https://github.com/EsotericSoftware/spine-runtimes/actions/runs/19536643416/job/559322883
2556
+ 17 this job fails with https://github.com/EsotericSoftware/spine-runtimes/actions/runs/19
2557
+ 536643416/job/55932288317 havea look at .gi
2558
+ */
2559
+ async #forceFileAutocomplete(explicitTab: boolean = false): Promise<void> {
2560
+ if (!this.#autocompleteProvider) return;
2561
+
2562
+ // Check if provider supports force file suggestions via runtime check
2563
+ const provider = this.#autocompleteProvider as {
2564
+ getForceFileSuggestions?: CombinedAutocompleteProvider["getForceFileSuggestions"];
2565
+ };
2566
+ if (typeof provider.getForceFileSuggestions !== "function") {
2567
+ await this.#tryTriggerAutocomplete(true);
2568
+ return;
2569
+ }
2570
+
2571
+ const requestId = ++this.#autocompleteRequestId;
2572
+ const suggestions = await provider.getForceFileSuggestions(
2573
+ this.#state.lines,
2574
+ this.#state.cursorLine,
2575
+ this.#state.cursorCol,
2576
+ );
2577
+ if (requestId !== this.#autocompleteRequestId) return;
2578
+
2579
+ if (suggestions && Array.isArray(suggestions.items) && suggestions.items.length > 0) {
2580
+ // If there's exactly one suggestion and this was an explicit Tab press, apply it immediately
2581
+ if (explicitTab && suggestions.items.length === 1) {
2582
+ const item = suggestions.items[0]!;
2583
+ const result = this.#autocompleteProvider.applyCompletion(
2584
+ this.#state.lines,
2585
+ this.#state.cursorLine,
2586
+ this.#state.cursorCol,
2587
+ item,
2588
+ suggestions.prefix,
2589
+ );
2590
+
2591
+ this.#state.lines = result.lines;
2592
+ this.#state.cursorLine = result.cursorLine;
2593
+ this.#setCursorCol(result.cursorCol);
2594
+
2595
+ if (this.onChange) {
2596
+ this.onChange(this.getText());
2597
+ }
2598
+ return;
2599
+ }
2600
+
2601
+ this.#autocompletePrefix = suggestions.prefix;
2602
+ this.#autocompleteList = this.#createAutocompleteList(suggestions.prefix, suggestions.items);
2603
+ this.#autocompleteState = "force";
2604
+ this.onAutocompleteUpdate?.();
2605
+ } else {
2606
+ this.#cancelAutocomplete();
2607
+ this.onAutocompleteUpdate?.();
2608
+ }
2609
+ }
2610
+
2611
+ #cancelAutocomplete(notifyCancel: boolean = false): void {
2612
+ const wasAutocompleting = this.#autocompleteState !== null;
2613
+ this.#clearAutocompleteTimeout();
2614
+ this.#autocompleteRequestId += 1;
2615
+ this.#autocompleteState = null;
2616
+ this.#autocompleteList = undefined;
2617
+ this.#autocompletePrefix = "";
2618
+ if (notifyCancel && wasAutocompleting) {
2619
+ this.onAutocompleteCancel?.();
2620
+ }
2621
+ }
2622
+
2623
+ isShowingAutocomplete(): boolean {
2624
+ return this.#autocompleteState !== null;
2625
+ }
2626
+
2627
+ async #updateAutocomplete(): Promise<void> {
2628
+ if (!this.#autocompleteState || !this.#autocompleteProvider) return;
2629
+
2630
+ // In force mode, use forceFileAutocomplete to get suggestions
2631
+ if (this.#autocompleteState === "force") {
2632
+ this.#forceFileAutocomplete();
2633
+ return;
2634
+ }
2635
+
2636
+ const requestId = ++this.#autocompleteRequestId;
2637
+
2638
+ const suggestions = await this.#autocompleteProvider.getSuggestions(
2639
+ this.#state.lines,
2640
+ this.#state.cursorLine,
2641
+ this.#state.cursorCol,
2642
+ );
2643
+ if (requestId !== this.#autocompleteRequestId) return;
2644
+
2645
+ if (suggestions && Array.isArray(suggestions.items) && suggestions.items.length > 0) {
2646
+ this.#autocompletePrefix = suggestions.prefix;
2647
+ // Always create new SelectList to ensure update
2648
+ this.#autocompleteList = this.#createAutocompleteList(suggestions.prefix, suggestions.items);
2649
+ this.onAutocompleteUpdate?.();
2650
+ } else {
2651
+ this.#cancelAutocomplete();
2652
+ this.onAutocompleteUpdate?.();
2653
+ }
2654
+ }
2655
+
2656
+ #debouncedUpdateAutocomplete(): void {
2657
+ if (this.#autocompleteTimeout) {
2658
+ clearTimeout(this.#autocompleteTimeout);
2659
+ }
2660
+ this.#autocompleteTimeout = setTimeout(() => {
2661
+ this.#updateAutocomplete();
2662
+ this.#autocompleteTimeout = undefined;
2663
+ }, 100);
2664
+ }
2665
+
2666
+ #clearAutocompleteTimeout(): void {
2667
+ if (this.#autocompleteTimeout) {
2668
+ clearTimeout(this.#autocompleteTimeout);
2669
+ this.#autocompleteTimeout = undefined;
2670
+ }
2671
+ }
2672
+
2673
+ /**
2674
+ * Get inline hint text to show as dim ghost text after the cursor.
2675
+ * Checks selected autocomplete item's hint first, then falls back to provider.
2676
+ */
2677
+ #getInlineHint(): string | null {
2678
+ // Check selected autocomplete item for a hint
2679
+ if (this.#autocompleteState && this.#autocompleteList) {
2680
+ const selected = this.#autocompleteList.getSelectedItem();
2681
+ return selected?.hint ?? null;
2682
+ }
2683
+
2684
+ // Fall back to provider's getInlineHint
2685
+ if (this.#autocompleteProvider?.getInlineHint) {
2686
+ return this.#autocompleteProvider.getInlineHint(
2687
+ this.#state.lines,
2688
+ this.#state.cursorLine,
2689
+ this.#state.cursorCol,
2690
+ );
2691
+ }
2692
+
2693
+ return null;
2694
+ }
2695
+ }