@wayofmono/wo-tui 1.0.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.
@@ -0,0 +1,2292 @@
1
+ import type { AutocompleteProvider, AutocompleteSuggestions } from "../autocomplete.js";
2
+ import { getKeybindings } from "../keybindings.js";
3
+ import { decodePrintableKey, matchesKey } from "../keys.js";
4
+ import { KillRing } from "../kill-ring.js";
5
+ import { type Component, CURSOR_MARKER, type Focusable, type TUI } from "../tui.js";
6
+ import { UndoStack } from "../undo-stack.js";
7
+ import { getSegmenter, isPunctuationChar, isWhitespaceChar, truncateToWidth, visibleWidth } from "../utils.js";
8
+ import { SelectList, type SelectListLayoutOptions, type SelectListTheme } from "./select-list.js";
9
+
10
+ const baseSegmenter = getSegmenter();
11
+
12
+ /** Regex matching paste markers like `[paste #1 +123 lines]` or `[paste #2 1234 chars]`. */
13
+ const PASTE_MARKER_REGEX = /\[paste #(\d+)( (\+\d+ lines|\d+ chars))?\]/g;
14
+
15
+ /** Non-global version for single-segment testing. */
16
+ const PASTE_MARKER_SINGLE = /^\[paste #(\d+)( (\+\d+ lines|\d+ chars))?\]$/;
17
+
18
+ /** Check if a segment is a paste marker (i.e. was merged by segmentWithMarkers). */
19
+ function isPasteMarker(segment: string): boolean {
20
+ return segment.length >= 10 && PASTE_MARKER_SINGLE.test(segment);
21
+ }
22
+
23
+ /**
24
+ * A segmenter that wraps Intl.Segmenter and merges graphemes that fall
25
+ * within paste markers into single atomic segments. This makes cursor
26
+ * movement, deletion, word-wrap, etc. treat paste markers as single units.
27
+ *
28
+ * Only markers whose numeric ID exists in `validIds` are merged.
29
+ */
30
+ function segmentWithMarkers(text: string, validIds: Set<number>): Iterable<Intl.SegmentData> {
31
+ // Fast path: no paste markers in the text or no valid IDs.
32
+ if (validIds.size === 0 || !text.includes("[paste #")) {
33
+ return baseSegmenter.segment(text);
34
+ }
35
+
36
+ // Find all marker spans with valid IDs.
37
+ const markers: Array<{ start: number; end: number }> = [];
38
+ for (const m of text.matchAll(PASTE_MARKER_REGEX)) {
39
+ const id = Number.parseInt(m[1]!, 10);
40
+ if (!validIds.has(id)) continue;
41
+ markers.push({ start: m.index, end: m.index + m[0].length });
42
+ }
43
+ if (markers.length === 0) {
44
+ return baseSegmenter.segment(text);
45
+ }
46
+
47
+ // Build merged segment list.
48
+ const baseSegments = baseSegmenter.segment(text);
49
+ const result: Intl.SegmentData[] = [];
50
+ let markerIdx = 0;
51
+
52
+ for (const seg of baseSegments) {
53
+ // Skip past markers that are entirely before this segment.
54
+ while (markerIdx < markers.length && markers[markerIdx]!.end <= seg.index) {
55
+ markerIdx++;
56
+ }
57
+
58
+ const marker = markerIdx < markers.length ? markers[markerIdx]! : null;
59
+
60
+ if (marker && seg.index >= marker.start && seg.index < marker.end) {
61
+ // This segment falls inside a marker.
62
+ // If this is the first segment of the marker, emit a merged segment.
63
+ if (seg.index === marker.start) {
64
+ const markerText = text.slice(marker.start, marker.end);
65
+ result.push({
66
+ segment: markerText,
67
+ index: marker.start,
68
+ input: text,
69
+ });
70
+ }
71
+ // Otherwise skip (already merged into the first segment).
72
+ } else {
73
+ result.push(seg);
74
+ }
75
+ }
76
+
77
+ return result;
78
+ }
79
+
80
+ /**
81
+ * Represents a chunk of text for word-wrap layout.
82
+ * Tracks both the text content and its position in the original line.
83
+ */
84
+ export interface TextChunk {
85
+ text: string;
86
+ startIndex: number;
87
+ endIndex: number;
88
+ }
89
+
90
+ /**
91
+ * Split a line into word-wrapped chunks.
92
+ * Wraps at word boundaries when possible, falling back to character-level
93
+ * wrapping for words longer than the available width.
94
+ *
95
+ * @param line - The text line to wrap
96
+ * @param maxWidth - Maximum visible width per chunk
97
+ * @param preSegmented - Optional pre-segmented graphemes (e.g. with paste-marker awareness).
98
+ * When omitted the default Intl.Segmenter is used.
99
+ * @returns Array of chunks with text and position information
100
+ */
101
+ export function wordWrapLine(line: string, maxWidth: number, preSegmented?: Intl.SegmentData[]): TextChunk[] {
102
+ if (!line || maxWidth <= 0) {
103
+ return [{ text: "", startIndex: 0, endIndex: 0 }];
104
+ }
105
+
106
+ const lineWidth = visibleWidth(line);
107
+ if (lineWidth <= maxWidth) {
108
+ return [{ text: line, startIndex: 0, endIndex: line.length }];
109
+ }
110
+
111
+ const chunks: TextChunk[] = [];
112
+ const segments = preSegmented ?? [...baseSegmenter.segment(line)];
113
+
114
+ let currentWidth = 0;
115
+ let chunkStart = 0;
116
+
117
+ // Wrap opportunity: the position after the last whitespace before a non-whitespace
118
+ // grapheme, i.e. where a line break is allowed.
119
+ let wrapOppIndex = -1;
120
+ let wrapOppWidth = 0;
121
+
122
+ for (let i = 0; i < segments.length; i++) {
123
+ const seg = segments[i]!;
124
+ const grapheme = seg.segment;
125
+ const gWidth = visibleWidth(grapheme);
126
+ const charIndex = seg.index;
127
+ const isWs = !isPasteMarker(grapheme) && isWhitespaceChar(grapheme);
128
+
129
+ // Overflow check before advancing.
130
+ if (currentWidth + gWidth > maxWidth) {
131
+ if (wrapOppIndex >= 0 && currentWidth - wrapOppWidth + gWidth <= maxWidth) {
132
+ // Backtrack to last wrap opportunity (the remaining content
133
+ // plus the current grapheme still fits within maxWidth).
134
+ chunks.push({ text: line.slice(chunkStart, wrapOppIndex), startIndex: chunkStart, endIndex: wrapOppIndex });
135
+ chunkStart = wrapOppIndex;
136
+ currentWidth -= wrapOppWidth;
137
+ } else if (chunkStart < charIndex) {
138
+ // No viable wrap opportunity: force-break at current position.
139
+ // This also handles the case where backtracking to a word
140
+ // boundary wouldn't help because the remaining content plus
141
+ // the current grapheme (e.g. a wide character) still exceeds
142
+ // maxWidth.
143
+ chunks.push({ text: line.slice(chunkStart, charIndex), startIndex: chunkStart, endIndex: charIndex });
144
+ chunkStart = charIndex;
145
+ currentWidth = 0;
146
+ }
147
+ wrapOppIndex = -1;
148
+ }
149
+
150
+ if (gWidth > maxWidth) {
151
+ // Single atomic segment wider than maxWidth (e.g. paste marker
152
+ // in a narrow terminal). Re-wrap it at grapheme granularity.
153
+
154
+ // The segment remains logically atomic for cursor
155
+ // movement / editing — the split is purely visual for word-wrap layout.
156
+ const subChunks = wordWrapLine(grapheme, maxWidth);
157
+ for (let j = 0; j < subChunks.length - 1; j++) {
158
+ const sc = subChunks[j]!;
159
+ chunks.push({ text: sc.text, startIndex: charIndex + sc.startIndex, endIndex: charIndex + sc.endIndex });
160
+ }
161
+ const last = subChunks[subChunks.length - 1]!;
162
+ chunkStart = charIndex + last.startIndex;
163
+ currentWidth = visibleWidth(last.text);
164
+ wrapOppIndex = -1;
165
+ continue;
166
+ }
167
+
168
+ // Advance.
169
+ currentWidth += gWidth;
170
+
171
+ // Record wrap opportunity: whitespace followed by non-whitespace.
172
+ // Multiple spaces join (no break between them); the break point is
173
+ // after the last space before the next word.
174
+ const next = segments[i + 1];
175
+ if (isWs && next && (isPasteMarker(next.segment) || !isWhitespaceChar(next.segment))) {
176
+ wrapOppIndex = next.index;
177
+ wrapOppWidth = currentWidth;
178
+ }
179
+ }
180
+
181
+ // Push final chunk.
182
+ chunks.push({ text: line.slice(chunkStart), startIndex: chunkStart, endIndex: line.length });
183
+
184
+ return chunks;
185
+ }
186
+
187
+ // Kitty CSI-u sequences for printable keys, including optional shifted/base codepoints.
188
+ interface EditorState {
189
+ lines: string[];
190
+ cursorLine: number;
191
+ cursorCol: number;
192
+ }
193
+
194
+ interface LayoutLine {
195
+ text: string;
196
+ hasCursor: boolean;
197
+ cursorPos?: number;
198
+ }
199
+
200
+ export interface EditorTheme {
201
+ borderColor: (str: string) => string;
202
+ selectList: SelectListTheme;
203
+ }
204
+
205
+ export interface EditorOptions {
206
+ paddingX?: number;
207
+ autocompleteMaxVisible?: number;
208
+ }
209
+
210
+ const SLASH_COMMAND_SELECT_LIST_LAYOUT: SelectListLayoutOptions = {
211
+ minPrimaryColumnWidth: 12,
212
+ maxPrimaryColumnWidth: 32,
213
+ };
214
+
215
+ const ATTACHMENT_AUTOCOMPLETE_DEBOUNCE_MS = 20;
216
+
217
+ export class Editor implements Component, Focusable {
218
+ private state: EditorState = {
219
+ lines: [""],
220
+ cursorLine: 0,
221
+ cursorCol: 0,
222
+ };
223
+
224
+ /** Focusable interface - set by TUI when focus changes */
225
+ focused: boolean = false;
226
+
227
+ protected tui: TUI;
228
+ private theme: EditorTheme;
229
+ private paddingX: number = 0;
230
+
231
+ // Store last render width for cursor navigation
232
+ private lastWidth: number = 80;
233
+
234
+ // Vertical scrolling support
235
+ private scrollOffset: number = 0;
236
+
237
+ // Border color (can be changed dynamically)
238
+ public borderColor: (str: string) => string;
239
+
240
+ // Autocomplete support
241
+ private autocompleteProvider?: AutocompleteProvider;
242
+ private autocompleteList?: SelectList;
243
+ private autocompleteState: "regular" | "force" | null = null;
244
+ private autocompletePrefix: string = "";
245
+ private autocompleteMaxVisible: number = 5;
246
+ private autocompleteAbort?: AbortController;
247
+ private autocompleteDebounceTimer?: ReturnType<typeof setTimeout>;
248
+ private autocompleteRequestTask: Promise<void> = Promise.resolve();
249
+ private autocompleteStartToken: number = 0;
250
+ private autocompleteRequestId: number = 0;
251
+
252
+ // Paste tracking for large pastes
253
+ private pastes: Map<number, string> = new Map();
254
+ private pasteCounter: number = 0;
255
+
256
+ // Bracketed paste mode buffering
257
+ private pasteBuffer: string = "";
258
+ private isInPaste: boolean = false;
259
+
260
+ // Prompt history for up/down navigation
261
+ private history: string[] = [];
262
+ private historyIndex: number = -1; // -1 = not browsing, 0 = most recent, 1 = older, etc.
263
+
264
+ // Kill ring for Emacs-style kill/yank operations
265
+ private killRing = new KillRing();
266
+ private lastAction: "kill" | "yank" | "type-word" | null = null;
267
+
268
+ // Character jump mode
269
+ private jumpMode: "forward" | "backward" | null = null;
270
+
271
+ // Preferred visual column for vertical cursor movement (sticky column)
272
+ private preferredVisualCol: number | null = null;
273
+
274
+ // When the cursor is snapped to the start of an atomic segment, e.g. a
275
+ // paste marker, cursorCol no longer reflects where the cursor would have
276
+ // landed. This field stores the pre-snap cursorCol so that the next
277
+ // vertical move can resolve it to a visual column on whatever VL it belongs
278
+ // to.
279
+ private snappedFromCursorCol: number | null = null;
280
+
281
+ // Undo support
282
+ private undoStack = new UndoStack<EditorState>();
283
+
284
+ public onSubmit?: (text: string) => void;
285
+ public onChange?: (text: string) => void;
286
+ public disableSubmit: boolean = false;
287
+
288
+ constructor(tui: TUI, theme: EditorTheme, options: EditorOptions = {}) {
289
+ this.tui = tui;
290
+ this.theme = theme;
291
+ this.borderColor = theme.borderColor;
292
+ const paddingX = options.paddingX ?? 0;
293
+ this.paddingX = Number.isFinite(paddingX) ? Math.max(0, Math.floor(paddingX)) : 0;
294
+ const maxVisible = options.autocompleteMaxVisible ?? 5;
295
+ this.autocompleteMaxVisible = Number.isFinite(maxVisible) ? Math.max(3, Math.min(20, Math.floor(maxVisible))) : 5;
296
+ }
297
+
298
+ /** Set of currently valid paste IDs, for marker-aware segmentation. */
299
+ private validPasteIds(): Set<number> {
300
+ return new Set(this.pastes.keys());
301
+ }
302
+
303
+ /** Segment text with paste-marker awareness, only merging markers with valid IDs. */
304
+ private segment(text: string): Iterable<Intl.SegmentData> {
305
+ return segmentWithMarkers(text, this.validPasteIds());
306
+ }
307
+
308
+ getPaddingX(): number {
309
+ return this.paddingX;
310
+ }
311
+
312
+ setPaddingX(padding: number): void {
313
+ const newPadding = Number.isFinite(padding) ? Math.max(0, Math.floor(padding)) : 0;
314
+ if (this.paddingX !== newPadding) {
315
+ this.paddingX = newPadding;
316
+ this.tui.requestRender();
317
+ }
318
+ }
319
+
320
+ getAutocompleteMaxVisible(): number {
321
+ return this.autocompleteMaxVisible;
322
+ }
323
+
324
+ setAutocompleteMaxVisible(maxVisible: number): void {
325
+ const newMaxVisible = Number.isFinite(maxVisible) ? Math.max(3, Math.min(20, Math.floor(maxVisible))) : 5;
326
+ if (this.autocompleteMaxVisible !== newMaxVisible) {
327
+ this.autocompleteMaxVisible = newMaxVisible;
328
+ this.tui.requestRender();
329
+ }
330
+ }
331
+
332
+ setAutocompleteProvider(provider: AutocompleteProvider): void {
333
+ this.cancelAutocomplete();
334
+ this.autocompleteProvider = provider;
335
+ }
336
+
337
+ /**
338
+ * Add a prompt to history for up/down arrow navigation.
339
+ * Called after successful submission.
340
+ */
341
+ addToHistory(text: string): void {
342
+ const trimmed = text.trim();
343
+ if (!trimmed) return;
344
+ // Don't add consecutive duplicates
345
+ if (this.history.length > 0 && this.history[0] === trimmed) return;
346
+ this.history.unshift(trimmed);
347
+ // Limit history size
348
+ if (this.history.length > 100) {
349
+ this.history.pop();
350
+ }
351
+ }
352
+
353
+ private isEditorEmpty(): boolean {
354
+ return this.state.lines.length === 1 && this.state.lines[0] === "";
355
+ }
356
+
357
+ private isOnFirstVisualLine(): boolean {
358
+ const visualLines = this.buildVisualLineMap(this.lastWidth);
359
+ const currentVisualLine = this.findCurrentVisualLine(visualLines);
360
+ return currentVisualLine === 0;
361
+ }
362
+
363
+ private isOnLastVisualLine(): boolean {
364
+ const visualLines = this.buildVisualLineMap(this.lastWidth);
365
+ const currentVisualLine = this.findCurrentVisualLine(visualLines);
366
+ return currentVisualLine === visualLines.length - 1;
367
+ }
368
+
369
+ private navigateHistory(direction: 1 | -1): void {
370
+ this.lastAction = null;
371
+ if (this.history.length === 0) return;
372
+
373
+ const newIndex = this.historyIndex - direction; // Up(-1) increases index, Down(1) decreases
374
+ if (newIndex < -1 || newIndex >= this.history.length) return;
375
+
376
+ // Capture state when first entering history browsing mode
377
+ if (this.historyIndex === -1 && newIndex >= 0) {
378
+ this.pushUndoSnapshot();
379
+ }
380
+
381
+ this.historyIndex = newIndex;
382
+
383
+ if (this.historyIndex === -1) {
384
+ // Returned to "current" state - clear editor
385
+ this.setTextInternal("");
386
+ } else {
387
+ this.setTextInternal(this.history[this.historyIndex] || "");
388
+ }
389
+ }
390
+
391
+ /** Internal setText that doesn't reset history state - used by navigateHistory */
392
+ private setTextInternal(text: string): void {
393
+ const lines = text.split("\n");
394
+ this.state.lines = lines.length === 0 ? [""] : lines;
395
+ this.state.cursorLine = this.state.lines.length - 1;
396
+ this.setCursorCol(this.state.lines[this.state.cursorLine]?.length || 0);
397
+ // Reset scroll - render() will adjust to show cursor
398
+ this.scrollOffset = 0;
399
+
400
+ if (this.onChange) {
401
+ this.onChange(this.getText());
402
+ }
403
+ }
404
+
405
+ invalidate(): void {
406
+ // No cached state to invalidate currently
407
+ }
408
+
409
+ render(width: number): string[] {
410
+ const maxPadding = Math.max(0, Math.floor((width - 1) / 2));
411
+ const paddingX = Math.min(this.paddingX, maxPadding);
412
+ const contentWidth = Math.max(1, width - paddingX * 2);
413
+
414
+ // Layout width: with padding the cursor can overflow into it,
415
+ // without padding we reserve 1 column for the cursor.
416
+ const layoutWidth = Math.max(1, contentWidth - (paddingX ? 0 : 1));
417
+
418
+ // Store for cursor navigation (must match wrapping width)
419
+ this.lastWidth = layoutWidth;
420
+
421
+ const horizontal = this.borderColor("─");
422
+
423
+ // Layout the text
424
+ const layoutLines = this.layoutText(layoutWidth);
425
+
426
+ // Calculate max visible lines: 30% of terminal height, minimum 5 lines
427
+ const terminalRows = this.tui.terminal.rows;
428
+ const maxVisibleLines = Math.max(5, Math.floor(terminalRows * 0.3));
429
+
430
+ // Find the cursor line index in layoutLines
431
+ let cursorLineIndex = layoutLines.findIndex((line) => line.hasCursor);
432
+ if (cursorLineIndex === -1) cursorLineIndex = 0;
433
+
434
+ // Adjust scroll offset to keep cursor visible
435
+ if (cursorLineIndex < this.scrollOffset) {
436
+ this.scrollOffset = cursorLineIndex;
437
+ } else if (cursorLineIndex >= this.scrollOffset + maxVisibleLines) {
438
+ this.scrollOffset = cursorLineIndex - maxVisibleLines + 1;
439
+ }
440
+
441
+ // Clamp scroll offset to valid range
442
+ const maxScrollOffset = Math.max(0, layoutLines.length - maxVisibleLines);
443
+ this.scrollOffset = Math.max(0, Math.min(this.scrollOffset, maxScrollOffset));
444
+
445
+ // Get visible lines slice
446
+ const visibleLines = layoutLines.slice(this.scrollOffset, this.scrollOffset + maxVisibleLines);
447
+
448
+ const result: string[] = [];
449
+ const leftPadding = " ".repeat(paddingX);
450
+ const rightPadding = leftPadding;
451
+
452
+ // Render top border (with scroll indicator if scrolled down)
453
+ if (this.scrollOffset > 0) {
454
+ const indicator = `─── ↑ ${this.scrollOffset} more `;
455
+ const remaining = width - visibleWidth(indicator);
456
+ if (remaining >= 0) {
457
+ result.push(this.borderColor(indicator + "─".repeat(remaining)));
458
+ } else {
459
+ result.push(this.borderColor(truncateToWidth(indicator, width)));
460
+ }
461
+ } else {
462
+ result.push(horizontal.repeat(width));
463
+ }
464
+
465
+ // Render each visible layout line
466
+ // Emit hardware cursor marker only when focused and not showing autocomplete
467
+ const emitCursorMarker = this.focused && !this.autocompleteState;
468
+
469
+ for (const layoutLine of visibleLines) {
470
+ let displayText = layoutLine.text;
471
+ let lineVisibleWidth = visibleWidth(layoutLine.text);
472
+ let cursorInPadding = false;
473
+
474
+ // Add cursor if this line has it
475
+ if (layoutLine.hasCursor && layoutLine.cursorPos !== undefined) {
476
+ const before = displayText.slice(0, layoutLine.cursorPos);
477
+ const after = displayText.slice(layoutLine.cursorPos);
478
+
479
+ // Hardware cursor marker (zero-width, emitted before fake cursor for IME positioning)
480
+ const marker = emitCursorMarker ? CURSOR_MARKER : "";
481
+
482
+ if (after.length > 0) {
483
+ // Cursor is on a character (grapheme) - replace it with highlighted version
484
+ // Get the first grapheme from 'after'
485
+ const afterGraphemes = [...this.segment(after)];
486
+ const firstGrapheme = afterGraphemes[0]?.segment || "";
487
+ const restAfter = after.slice(firstGrapheme.length);
488
+ const cursor = `\x1b[7m${firstGrapheme}\x1b[0m`;
489
+ displayText = before + marker + cursor + restAfter;
490
+ // lineVisibleWidth stays the same - we're replacing, not adding
491
+ } else {
492
+ // Cursor is at the end - add highlighted space
493
+ const cursor = "\x1b[7m \x1b[0m";
494
+ displayText = before + marker + cursor;
495
+ lineVisibleWidth = lineVisibleWidth + 1;
496
+ // If cursor overflows content width into the padding, flag it
497
+ if (lineVisibleWidth > contentWidth && paddingX > 0) {
498
+ cursorInPadding = true;
499
+ }
500
+ }
501
+ }
502
+
503
+ // Calculate padding based on actual visible width
504
+ const padding = " ".repeat(Math.max(0, contentWidth - lineVisibleWidth));
505
+ const lineRightPadding = cursorInPadding ? rightPadding.slice(1) : rightPadding;
506
+
507
+ // Render the line (no side borders, just horizontal lines above and below)
508
+ result.push(`${leftPadding}${displayText}${padding}${lineRightPadding}`);
509
+ }
510
+
511
+ // Render bottom border (with scroll indicator if more content below)
512
+ const linesBelow = layoutLines.length - (this.scrollOffset + visibleLines.length);
513
+ if (linesBelow > 0) {
514
+ const indicator = `─── ↓ ${linesBelow} more `;
515
+ const remaining = width - visibleWidth(indicator);
516
+ result.push(this.borderColor(indicator + "─".repeat(Math.max(0, remaining))));
517
+ } else {
518
+ result.push(horizontal.repeat(width));
519
+ }
520
+
521
+ // Add autocomplete list if active
522
+ if (this.autocompleteState && this.autocompleteList) {
523
+ const autocompleteResult = this.autocompleteList.render(contentWidth);
524
+ for (const line of autocompleteResult) {
525
+ const lineWidth = visibleWidth(line);
526
+ const linePadding = " ".repeat(Math.max(0, contentWidth - lineWidth));
527
+ result.push(`${leftPadding}${line}${linePadding}${rightPadding}`);
528
+ }
529
+ }
530
+
531
+ return result;
532
+ }
533
+
534
+ handleInput(data: string): void {
535
+ const kb = getKeybindings();
536
+
537
+ // Handle character jump mode (awaiting next character to jump to)
538
+ if (this.jumpMode !== null) {
539
+ // Cancel if the hotkey is pressed again
540
+ if (kb.matches(data, "tui.editor.jumpForward") || kb.matches(data, "tui.editor.jumpBackward")) {
541
+ this.jumpMode = null;
542
+ return;
543
+ }
544
+
545
+ const printable = decodePrintableKey(data) ?? (data.charCodeAt(0) >= 32 ? data : undefined);
546
+ if (printable !== undefined) {
547
+ // Printable character - perform the jump
548
+ const direction = this.jumpMode;
549
+ this.jumpMode = null;
550
+ this.jumpToChar(printable, direction);
551
+ return;
552
+ }
553
+
554
+ // Control character - cancel and fall through to normal handling
555
+ this.jumpMode = null;
556
+ }
557
+
558
+ // Handle bracketed paste mode
559
+ if (data.includes("\x1b[200~")) {
560
+ this.isInPaste = true;
561
+ this.pasteBuffer = "";
562
+ data = data.replace("\x1b[200~", "");
563
+ }
564
+
565
+ if (this.isInPaste) {
566
+ this.pasteBuffer += data;
567
+ const endIndex = this.pasteBuffer.indexOf("\x1b[201~");
568
+ if (endIndex !== -1) {
569
+ const pasteContent = this.pasteBuffer.substring(0, endIndex);
570
+ if (pasteContent.length > 0) {
571
+ this.handlePaste(pasteContent);
572
+ }
573
+ this.isInPaste = false;
574
+ const remaining = this.pasteBuffer.substring(endIndex + 6);
575
+ this.pasteBuffer = "";
576
+ if (remaining.length > 0) {
577
+ this.handleInput(remaining);
578
+ }
579
+ return;
580
+ }
581
+ return;
582
+ }
583
+
584
+ // Ctrl+C - let parent handle (exit/clear)
585
+ if (kb.matches(data, "tui.input.copy")) {
586
+ return;
587
+ }
588
+
589
+ // Undo
590
+ if (kb.matches(data, "tui.editor.undo")) {
591
+ this.undo();
592
+ return;
593
+ }
594
+
595
+ // Handle autocomplete mode
596
+ if (this.autocompleteState && this.autocompleteList) {
597
+ if (kb.matches(data, "tui.select.cancel")) {
598
+ this.cancelAutocomplete();
599
+ return;
600
+ }
601
+
602
+ if (kb.matches(data, "tui.select.up") || kb.matches(data, "tui.select.down")) {
603
+ this.autocompleteList.handleInput(data);
604
+ return;
605
+ }
606
+
607
+ if (kb.matches(data, "tui.input.tab")) {
608
+ const selected = this.autocompleteList.getSelectedItem();
609
+ if (selected && this.autocompleteProvider) {
610
+ this.pushUndoSnapshot();
611
+ this.lastAction = null;
612
+ const result = this.autocompleteProvider.applyCompletion(
613
+ this.state.lines,
614
+ this.state.cursorLine,
615
+ this.state.cursorCol,
616
+ selected,
617
+ this.autocompletePrefix,
618
+ );
619
+ this.state.lines = result.lines;
620
+ this.state.cursorLine = result.cursorLine;
621
+ this.setCursorCol(result.cursorCol);
622
+ this.cancelAutocomplete();
623
+ if (this.onChange) this.onChange(this.getText());
624
+ }
625
+ return;
626
+ }
627
+
628
+ if (kb.matches(data, "tui.select.confirm")) {
629
+ const selected = this.autocompleteList.getSelectedItem();
630
+ if (selected && this.autocompleteProvider) {
631
+ this.pushUndoSnapshot();
632
+ this.lastAction = null;
633
+ const result = this.autocompleteProvider.applyCompletion(
634
+ this.state.lines,
635
+ this.state.cursorLine,
636
+ this.state.cursorCol,
637
+ selected,
638
+ this.autocompletePrefix,
639
+ );
640
+ this.state.lines = result.lines;
641
+ this.state.cursorLine = result.cursorLine;
642
+ this.setCursorCol(result.cursorCol);
643
+
644
+ if (this.autocompletePrefix.startsWith("/")) {
645
+ this.cancelAutocomplete();
646
+ // Fall through to submit
647
+ } else {
648
+ this.cancelAutocomplete();
649
+ if (this.onChange) this.onChange(this.getText());
650
+ return;
651
+ }
652
+ }
653
+ }
654
+ }
655
+
656
+ // Tab - trigger completion
657
+ if (kb.matches(data, "tui.input.tab") && !this.autocompleteState) {
658
+ this.handleTabCompletion();
659
+ return;
660
+ }
661
+
662
+ // Deletion actions
663
+ if (kb.matches(data, "tui.editor.deleteToLineEnd")) {
664
+ this.deleteToEndOfLine();
665
+ return;
666
+ }
667
+ if (kb.matches(data, "tui.editor.deleteToLineStart")) {
668
+ this.deleteToStartOfLine();
669
+ return;
670
+ }
671
+ if (kb.matches(data, "tui.editor.deleteWordBackward")) {
672
+ this.deleteWordBackwards();
673
+ return;
674
+ }
675
+ if (kb.matches(data, "tui.editor.deleteWordForward")) {
676
+ this.deleteWordForward();
677
+ return;
678
+ }
679
+ if (kb.matches(data, "tui.editor.deleteCharBackward") || matchesKey(data, "shift+backspace")) {
680
+ this.handleBackspace();
681
+ return;
682
+ }
683
+ if (kb.matches(data, "tui.editor.deleteCharForward") || matchesKey(data, "shift+delete")) {
684
+ this.handleForwardDelete();
685
+ return;
686
+ }
687
+
688
+ // Kill ring actions
689
+ if (kb.matches(data, "tui.editor.yank")) {
690
+ this.yank();
691
+ return;
692
+ }
693
+ if (kb.matches(data, "tui.editor.yankPop")) {
694
+ this.yankPop();
695
+ return;
696
+ }
697
+
698
+ // Cursor movement actions
699
+ if (kb.matches(data, "tui.editor.cursorLineStart")) {
700
+ this.moveToLineStart();
701
+ return;
702
+ }
703
+ if (kb.matches(data, "tui.editor.cursorLineEnd")) {
704
+ this.moveToLineEnd();
705
+ return;
706
+ }
707
+ if (kb.matches(data, "tui.editor.cursorWordLeft")) {
708
+ this.moveWordBackwards();
709
+ return;
710
+ }
711
+ if (kb.matches(data, "tui.editor.cursorWordRight")) {
712
+ this.moveWordForwards();
713
+ return;
714
+ }
715
+
716
+ // New line
717
+ if (
718
+ kb.matches(data, "tui.input.newLine") ||
719
+ (data.charCodeAt(0) === 10 && data.length > 1) ||
720
+ data === "\x1b\r" ||
721
+ data === "\x1b[13;2~" ||
722
+ (data.length > 1 && data.includes("\x1b") && data.includes("\r")) ||
723
+ (data === "\n" && data.length === 1)
724
+ ) {
725
+ if (this.shouldSubmitOnBackslashEnter(data, kb)) {
726
+ this.handleBackspace();
727
+ this.submitValue();
728
+ return;
729
+ }
730
+ this.addNewLine();
731
+ return;
732
+ }
733
+
734
+ // Submit (Enter)
735
+ if (kb.matches(data, "tui.input.submit")) {
736
+ if (this.disableSubmit) return;
737
+
738
+ // Workaround for terminals without Shift+Enter support:
739
+ // If char before cursor is \, delete it and insert newline instead of submitting.
740
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
741
+ if (this.state.cursorCol > 0 && currentLine[this.state.cursorCol - 1] === "\\") {
742
+ this.handleBackspace();
743
+ this.addNewLine();
744
+ return;
745
+ }
746
+
747
+ this.submitValue();
748
+ return;
749
+ }
750
+
751
+ // Arrow key navigation (with history support)
752
+ if (kb.matches(data, "tui.editor.cursorUp")) {
753
+ if (this.isEditorEmpty()) {
754
+ this.navigateHistory(-1);
755
+ } else if (this.historyIndex > -1 && this.isOnFirstVisualLine()) {
756
+ this.navigateHistory(-1);
757
+ } else if (this.isOnFirstVisualLine()) {
758
+ // Already at top - jump to start of line
759
+ this.moveToLineStart();
760
+ } else {
761
+ this.moveCursor(-1, 0);
762
+ }
763
+ return;
764
+ }
765
+ if (kb.matches(data, "tui.editor.cursorDown")) {
766
+ if (this.historyIndex > -1 && this.isOnLastVisualLine()) {
767
+ this.navigateHistory(1);
768
+ } else if (this.isOnLastVisualLine()) {
769
+ // Already at bottom - jump to end of line
770
+ this.moveToLineEnd();
771
+ } else {
772
+ this.moveCursor(1, 0);
773
+ }
774
+ return;
775
+ }
776
+ if (kb.matches(data, "tui.editor.cursorRight")) {
777
+ this.moveCursor(0, 1);
778
+ return;
779
+ }
780
+ if (kb.matches(data, "tui.editor.cursorLeft")) {
781
+ this.moveCursor(0, -1);
782
+ return;
783
+ }
784
+
785
+ // Page up/down - scroll by page and move cursor
786
+ if (kb.matches(data, "tui.editor.pageUp")) {
787
+ this.pageScroll(-1);
788
+ return;
789
+ }
790
+ if (kb.matches(data, "tui.editor.pageDown")) {
791
+ this.pageScroll(1);
792
+ return;
793
+ }
794
+
795
+ // Character jump mode triggers
796
+ if (kb.matches(data, "tui.editor.jumpForward")) {
797
+ this.jumpMode = "forward";
798
+ return;
799
+ }
800
+ if (kb.matches(data, "tui.editor.jumpBackward")) {
801
+ this.jumpMode = "backward";
802
+ return;
803
+ }
804
+
805
+ // Shift+Space - insert regular space
806
+ if (matchesKey(data, "shift+space")) {
807
+ this.insertCharacter(" ");
808
+ return;
809
+ }
810
+
811
+ const printable = decodePrintableKey(data);
812
+ if (printable !== undefined) {
813
+ this.insertCharacter(printable);
814
+ return;
815
+ }
816
+
817
+ // Regular characters
818
+ if (data.charCodeAt(0) >= 32) {
819
+ this.insertCharacter(data);
820
+ }
821
+ }
822
+
823
+ private layoutText(contentWidth: number): LayoutLine[] {
824
+ const layoutLines: LayoutLine[] = [];
825
+
826
+ if (this.state.lines.length === 0 || (this.state.lines.length === 1 && this.state.lines[0] === "")) {
827
+ // Empty editor
828
+ layoutLines.push({
829
+ text: "",
830
+ hasCursor: true,
831
+ cursorPos: 0,
832
+ });
833
+ return layoutLines;
834
+ }
835
+
836
+ // Process each logical line
837
+ for (let i = 0; i < this.state.lines.length; i++) {
838
+ const line = this.state.lines[i] || "";
839
+ const isCurrentLine = i === this.state.cursorLine;
840
+ const lineVisibleWidth = visibleWidth(line);
841
+
842
+ if (lineVisibleWidth <= contentWidth) {
843
+ // Line fits in one layout line
844
+ if (isCurrentLine) {
845
+ layoutLines.push({
846
+ text: line,
847
+ hasCursor: true,
848
+ cursorPos: this.state.cursorCol,
849
+ });
850
+ } else {
851
+ layoutLines.push({
852
+ text: line,
853
+ hasCursor: false,
854
+ });
855
+ }
856
+ } else {
857
+ // Line needs wrapping - use word-aware wrapping
858
+ const chunks = wordWrapLine(line, contentWidth, [...this.segment(line)]);
859
+
860
+ for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
861
+ const chunk = chunks[chunkIndex];
862
+ if (!chunk) continue;
863
+
864
+ const cursorPos = this.state.cursorCol;
865
+ const isLastChunk = chunkIndex === chunks.length - 1;
866
+
867
+ // Determine if cursor is in this chunk
868
+ // For word-wrapped chunks, we need to handle the case where
869
+ // cursor might be in trimmed whitespace at end of chunk
870
+ let hasCursorInChunk = false;
871
+ let adjustedCursorPos = 0;
872
+
873
+ if (isCurrentLine) {
874
+ if (isLastChunk) {
875
+ // Last chunk: cursor belongs here if >= startIndex
876
+ hasCursorInChunk = cursorPos >= chunk.startIndex;
877
+ adjustedCursorPos = cursorPos - chunk.startIndex;
878
+ } else {
879
+ // Non-last chunk: cursor belongs here if in range [startIndex, endIndex)
880
+ // But we need to handle the visual position in the trimmed text
881
+ hasCursorInChunk = cursorPos >= chunk.startIndex && cursorPos < chunk.endIndex;
882
+ if (hasCursorInChunk) {
883
+ adjustedCursorPos = cursorPos - chunk.startIndex;
884
+ // Clamp to text length (in case cursor was in trimmed whitespace)
885
+ if (adjustedCursorPos > chunk.text.length) {
886
+ adjustedCursorPos = chunk.text.length;
887
+ }
888
+ }
889
+ }
890
+ }
891
+
892
+ if (hasCursorInChunk) {
893
+ layoutLines.push({
894
+ text: chunk.text,
895
+ hasCursor: true,
896
+ cursorPos: adjustedCursorPos,
897
+ });
898
+ } else {
899
+ layoutLines.push({
900
+ text: chunk.text,
901
+ hasCursor: false,
902
+ });
903
+ }
904
+ }
905
+ }
906
+ }
907
+
908
+ return layoutLines;
909
+ }
910
+
911
+ getText(): string {
912
+ return this.state.lines.join("\n");
913
+ }
914
+
915
+ private expandPasteMarkers(text: string): string {
916
+ let result = text;
917
+ for (const [pasteId, pasteContent] of this.pastes) {
918
+ const markerRegex = new RegExp(`\\[paste #${pasteId}( (\\+\\d+ lines|\\d+ chars))?\\]`, "g");
919
+ result = result.replace(markerRegex, () => pasteContent);
920
+ }
921
+ return result;
922
+ }
923
+
924
+ /**
925
+ * Get text with paste markers expanded to their actual content.
926
+ * Use this when you need the full content (e.g., for external editor).
927
+ */
928
+ getExpandedText(): string {
929
+ return this.expandPasteMarkers(this.state.lines.join("\n"));
930
+ }
931
+
932
+ getLines(): string[] {
933
+ return [...this.state.lines];
934
+ }
935
+
936
+ getCursor(): { line: number; col: number } {
937
+ return { line: this.state.cursorLine, col: this.state.cursorCol };
938
+ }
939
+
940
+ setText(text: string): void {
941
+ this.cancelAutocomplete();
942
+ this.lastAction = null;
943
+ this.historyIndex = -1; // Exit history browsing mode
944
+ const normalized = this.normalizeText(text);
945
+ // Push undo snapshot if content differs (makes programmatic changes undoable)
946
+ if (this.getText() !== normalized) {
947
+ this.pushUndoSnapshot();
948
+ }
949
+ this.setTextInternal(normalized);
950
+ }
951
+
952
+ /**
953
+ * Insert text at the current cursor position.
954
+ * Used for programmatic insertion (e.g., clipboard image markers).
955
+ * This is atomic for undo - single undo restores entire pre-insert state.
956
+ */
957
+ insertTextAtCursor(text: string): void {
958
+ if (!text) return;
959
+ this.cancelAutocomplete();
960
+ this.pushUndoSnapshot();
961
+ this.lastAction = null;
962
+ this.historyIndex = -1;
963
+ this.insertTextAtCursorInternal(text);
964
+ }
965
+
966
+ /**
967
+ * Normalize text for editor storage:
968
+ * - Normalize line endings (\r\n and \r -> \n)
969
+ * - Expand tabs to 4 spaces
970
+ */
971
+ private normalizeText(text: string): string {
972
+ return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\t/g, " ");
973
+ }
974
+
975
+ /**
976
+ * Internal text insertion at cursor. Handles single and multi-line text.
977
+ * Does not push undo snapshots or trigger autocomplete - caller is responsible.
978
+ * Normalizes line endings and calls onChange once at the end.
979
+ */
980
+ private insertTextAtCursorInternal(text: string): void {
981
+ if (!text) return;
982
+
983
+ // Normalize line endings and tabs
984
+ const normalized = this.normalizeText(text);
985
+ const insertedLines = normalized.split("\n");
986
+
987
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
988
+ const beforeCursor = currentLine.slice(0, this.state.cursorCol);
989
+ const afterCursor = currentLine.slice(this.state.cursorCol);
990
+
991
+ if (insertedLines.length === 1) {
992
+ // Single line - insert at cursor position
993
+ this.state.lines[this.state.cursorLine] = beforeCursor + normalized + afterCursor;
994
+ this.setCursorCol(this.state.cursorCol + normalized.length);
995
+ } else {
996
+ // Multi-line insertion
997
+ this.state.lines = [
998
+ // All lines before current line
999
+ ...this.state.lines.slice(0, this.state.cursorLine),
1000
+
1001
+ // The first inserted line merged with text before cursor
1002
+ beforeCursor + insertedLines[0],
1003
+
1004
+ // All middle inserted lines
1005
+ ...insertedLines.slice(1, -1),
1006
+
1007
+ // The last inserted line with text after cursor
1008
+ insertedLines[insertedLines.length - 1] + afterCursor,
1009
+
1010
+ // All lines after current line
1011
+ ...this.state.lines.slice(this.state.cursorLine + 1),
1012
+ ];
1013
+
1014
+ this.state.cursorLine += insertedLines.length - 1;
1015
+ this.setCursorCol((insertedLines[insertedLines.length - 1] || "").length);
1016
+ }
1017
+
1018
+ if (this.onChange) {
1019
+ this.onChange(this.getText());
1020
+ }
1021
+ }
1022
+
1023
+ // All the editor methods from before...
1024
+ private insertCharacter(char: string, skipUndoCoalescing?: boolean): void {
1025
+ this.historyIndex = -1; // Exit history browsing mode
1026
+
1027
+ // Undo coalescing (fish-style):
1028
+ // - Consecutive word chars coalesce into one undo unit
1029
+ // - Space captures state before itself (so undo removes space+following word together)
1030
+ // - Each space is separately undoable
1031
+ // Skip coalescing when called from atomic operations (e.g., handlePaste)
1032
+ if (!skipUndoCoalescing) {
1033
+ if (isWhitespaceChar(char) || this.lastAction !== "type-word") {
1034
+ this.pushUndoSnapshot();
1035
+ }
1036
+ this.lastAction = "type-word";
1037
+ }
1038
+
1039
+ const line = this.state.lines[this.state.cursorLine] || "";
1040
+
1041
+ const before = line.slice(0, this.state.cursorCol);
1042
+ const after = line.slice(this.state.cursorCol);
1043
+
1044
+ this.state.lines[this.state.cursorLine] = before + char + after;
1045
+ this.setCursorCol(this.state.cursorCol + char.length);
1046
+
1047
+ if (this.onChange) {
1048
+ this.onChange(this.getText());
1049
+ }
1050
+
1051
+ // Check if we should trigger or update autocomplete
1052
+ if (!this.autocompleteState) {
1053
+ // Auto-trigger for "/" at the start of a line (slash commands)
1054
+ if (char === "/" && this.isAtStartOfMessage()) {
1055
+ this.tryTriggerAutocomplete();
1056
+ }
1057
+ // Auto-trigger for symbol-based completion like @ or # at token boundaries
1058
+ else if (char === "@" || char === "#") {
1059
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1060
+ const textBeforeCursor = currentLine.slice(0, this.state.cursorCol);
1061
+ const charBeforeSymbol = textBeforeCursor[textBeforeCursor.length - 2];
1062
+ if (textBeforeCursor.length === 1 || charBeforeSymbol === " " || charBeforeSymbol === "\t") {
1063
+ this.tryTriggerAutocomplete();
1064
+ }
1065
+ }
1066
+ // Also auto-trigger when typing letters in a slash command or symbol completion context
1067
+ else if (/[a-zA-Z0-9.\-_]/.test(char)) {
1068
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1069
+ const textBeforeCursor = currentLine.slice(0, this.state.cursorCol);
1070
+ // Check if we're in a slash command (with or without space for arguments)
1071
+ if (this.isInSlashCommandContext(textBeforeCursor)) {
1072
+ this.tryTriggerAutocomplete();
1073
+ }
1074
+ // Check if we're in a symbol-based completion context like @ or #
1075
+ else if (textBeforeCursor.match(/(?:^|[\s])[@#][^\s]*$/)) {
1076
+ this.tryTriggerAutocomplete();
1077
+ }
1078
+ }
1079
+ } else {
1080
+ this.updateAutocomplete();
1081
+ }
1082
+ }
1083
+
1084
+ private handlePaste(pastedText: string): void {
1085
+ this.cancelAutocomplete();
1086
+ this.historyIndex = -1; // Exit history browsing mode
1087
+ this.lastAction = null;
1088
+
1089
+ this.pushUndoSnapshot();
1090
+
1091
+ // Some terminals (e.g. tmux popups with extended-keys-format=csi-u) re-encode
1092
+ // control bytes inside bracketed paste as CSI-u Ctrl+<letter> sequences
1093
+ // (ESC [ <codepoint> ; 5 u). Decode those back to their literal byte so the
1094
+ // per-char filter below preserves newlines instead of stripping ESC and
1095
+ // leaking the printable tail (e.g. "[106;5u") into the editor.
1096
+ const decodedText = pastedText.replace(/\x1b\[(\d+);5u/g, (match, code) => {
1097
+ const cp = Number(code);
1098
+ if (cp >= 97 && cp <= 122) return String.fromCharCode(cp - 96);
1099
+ if (cp >= 65 && cp <= 90) return String.fromCharCode(cp - 64);
1100
+ return match;
1101
+ });
1102
+
1103
+ // Clean the pasted text: normalize line endings, expand tabs
1104
+ const cleanText = this.normalizeText(decodedText);
1105
+
1106
+ // Filter out non-printable characters except newlines
1107
+ let filteredText = cleanText
1108
+ .split("")
1109
+ .filter((char) => char === "\n" || char.charCodeAt(0) >= 32)
1110
+ .join("");
1111
+
1112
+ // If pasting a file path (starts with /, ~, or .) and the character before
1113
+ // the cursor is a word character, prepend a space for better readability
1114
+ if (/^[/~.]/.test(filteredText)) {
1115
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1116
+ const charBeforeCursor = this.state.cursorCol > 0 ? currentLine[this.state.cursorCol - 1] : "";
1117
+ if (charBeforeCursor && /\w/.test(charBeforeCursor)) {
1118
+ filteredText = ` ${filteredText}`;
1119
+ }
1120
+ }
1121
+
1122
+ // Split into lines to check for large paste
1123
+ const pastedLines = filteredText.split("\n");
1124
+
1125
+ // Check if this is a large paste (> 10 lines or > 1000 characters)
1126
+ const totalChars = filteredText.length;
1127
+ if (pastedLines.length > 10 || totalChars > 1000) {
1128
+ // Store the paste and insert a marker
1129
+ this.pasteCounter++;
1130
+ const pasteId = this.pasteCounter;
1131
+ this.pastes.set(pasteId, filteredText);
1132
+
1133
+ // Insert marker like "[paste #1 +123 lines]" or "[paste #1 1234 chars]"
1134
+ const marker =
1135
+ pastedLines.length > 10
1136
+ ? `[paste #${pasteId} +${pastedLines.length} lines]`
1137
+ : `[paste #${pasteId} ${totalChars} chars]`;
1138
+ this.insertTextAtCursorInternal(marker);
1139
+ return;
1140
+ }
1141
+
1142
+ if (pastedLines.length === 1) {
1143
+ // Single line - insert atomically (do not trigger autocomplete during paste)
1144
+ this.insertTextAtCursorInternal(filteredText);
1145
+ return;
1146
+ }
1147
+
1148
+ // Multi-line paste - use direct state manipulation
1149
+ this.insertTextAtCursorInternal(filteredText);
1150
+ }
1151
+
1152
+ private addNewLine(): void {
1153
+ this.cancelAutocomplete();
1154
+ this.historyIndex = -1; // Exit history browsing mode
1155
+ this.lastAction = null;
1156
+
1157
+ this.pushUndoSnapshot();
1158
+
1159
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1160
+
1161
+ const before = currentLine.slice(0, this.state.cursorCol);
1162
+ const after = currentLine.slice(this.state.cursorCol);
1163
+
1164
+ // Split current line
1165
+ this.state.lines[this.state.cursorLine] = before;
1166
+ this.state.lines.splice(this.state.cursorLine + 1, 0, after);
1167
+
1168
+ // Move cursor to start of new line
1169
+ this.state.cursorLine++;
1170
+ this.setCursorCol(0);
1171
+
1172
+ if (this.onChange) {
1173
+ this.onChange(this.getText());
1174
+ }
1175
+ }
1176
+
1177
+ private shouldSubmitOnBackslashEnter(data: string, kb: ReturnType<typeof getKeybindings>): boolean {
1178
+ if (this.disableSubmit) return false;
1179
+ if (!matchesKey(data, "enter")) return false;
1180
+ const submitKeys = kb.getKeys("tui.input.submit");
1181
+ const hasShiftEnter = submitKeys.includes("shift+enter") || submitKeys.includes("shift+return");
1182
+ if (!hasShiftEnter) return false;
1183
+
1184
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1185
+ return this.state.cursorCol > 0 && currentLine[this.state.cursorCol - 1] === "\\";
1186
+ }
1187
+
1188
+ private submitValue(): void {
1189
+ this.cancelAutocomplete();
1190
+ const result = this.expandPasteMarkers(this.state.lines.join("\n")).trim();
1191
+
1192
+ this.state = { lines: [""], cursorLine: 0, cursorCol: 0 };
1193
+ this.pastes.clear();
1194
+ this.pasteCounter = 0;
1195
+ this.historyIndex = -1;
1196
+ this.scrollOffset = 0;
1197
+ this.undoStack.clear();
1198
+ this.lastAction = null;
1199
+
1200
+ if (this.onChange) this.onChange("");
1201
+ if (this.onSubmit) this.onSubmit(result);
1202
+ }
1203
+
1204
+ private handleBackspace(): void {
1205
+ this.historyIndex = -1; // Exit history browsing mode
1206
+ this.lastAction = null;
1207
+
1208
+ if (this.state.cursorCol > 0) {
1209
+ this.pushUndoSnapshot();
1210
+
1211
+ // Delete grapheme before cursor (handles emojis, combining characters, etc.)
1212
+ const line = this.state.lines[this.state.cursorLine] || "";
1213
+ const beforeCursor = line.slice(0, this.state.cursorCol);
1214
+
1215
+ // Find the last grapheme in the text before cursor
1216
+ const graphemes = [...this.segment(beforeCursor)];
1217
+ const lastGrapheme = graphemes[graphemes.length - 1];
1218
+ const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;
1219
+
1220
+ const before = line.slice(0, this.state.cursorCol - graphemeLength);
1221
+ const after = line.slice(this.state.cursorCol);
1222
+
1223
+ this.state.lines[this.state.cursorLine] = before + after;
1224
+ this.setCursorCol(this.state.cursorCol - graphemeLength);
1225
+ } else if (this.state.cursorLine > 0) {
1226
+ this.pushUndoSnapshot();
1227
+
1228
+ // Merge with previous line
1229
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1230
+ const previousLine = this.state.lines[this.state.cursorLine - 1] || "";
1231
+
1232
+ this.state.lines[this.state.cursorLine - 1] = previousLine + currentLine;
1233
+ this.state.lines.splice(this.state.cursorLine, 1);
1234
+
1235
+ this.state.cursorLine--;
1236
+ this.setCursorCol(previousLine.length);
1237
+ }
1238
+
1239
+ if (this.onChange) {
1240
+ this.onChange(this.getText());
1241
+ }
1242
+
1243
+ // Update or re-trigger autocomplete after backspace
1244
+ if (this.autocompleteState) {
1245
+ this.updateAutocomplete();
1246
+ } else {
1247
+ // If autocomplete was cancelled (no matches), re-trigger if we're in a completable context
1248
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1249
+ const textBeforeCursor = currentLine.slice(0, this.state.cursorCol);
1250
+ // Slash command context
1251
+ if (this.isInSlashCommandContext(textBeforeCursor)) {
1252
+ this.tryTriggerAutocomplete();
1253
+ }
1254
+ // Symbol-based completion context like @ or #
1255
+ else if (textBeforeCursor.match(/(?:^|[\s])[@#][^\s]*$/)) {
1256
+ this.tryTriggerAutocomplete();
1257
+ }
1258
+ }
1259
+ }
1260
+
1261
+ /**
1262
+ * Set cursor column and clear preferredVisualCol.
1263
+ * Use this for all non-vertical cursor movements to reset sticky column behavior.
1264
+ */
1265
+ private setCursorCol(col: number): void {
1266
+ this.state.cursorCol = col;
1267
+ this.preferredVisualCol = null;
1268
+ this.snappedFromCursorCol = null;
1269
+ }
1270
+
1271
+ /**
1272
+ * Move cursor to a target visual line, applying sticky column logic.
1273
+ * Shared by moveCursor() and pageScroll().
1274
+ */
1275
+ private moveToVisualLine(
1276
+ visualLines: Array<{ logicalLine: number; startCol: number; length: number }>,
1277
+ currentVisualLine: number,
1278
+ targetVisualLine: number,
1279
+ ): void {
1280
+ const currentVL = visualLines[currentVisualLine];
1281
+ const targetVL = visualLines[targetVisualLine];
1282
+ if (!(currentVL && targetVL)) return;
1283
+
1284
+ // When the cursor was snapped to a segment start, resolve the pre-snap
1285
+ // position against the VL it belongs to. This gives the correct visual
1286
+ // column even after a resize reshuffles VLs.
1287
+ let currentVisualCol: number;
1288
+ if (this.snappedFromCursorCol !== null) {
1289
+ const vlIndex = this.findVisualLineAt(visualLines, currentVL.logicalLine, this.snappedFromCursorCol);
1290
+ currentVisualCol = this.snappedFromCursorCol - visualLines[vlIndex].startCol;
1291
+ } else {
1292
+ currentVisualCol = this.state.cursorCol - currentVL.startCol;
1293
+ }
1294
+
1295
+ // For non-last segments, clamp to length-1 to stay within the segment
1296
+ const isLastSourceSegment =
1297
+ currentVisualLine === visualLines.length - 1 ||
1298
+ visualLines[currentVisualLine + 1]?.logicalLine !== currentVL.logicalLine;
1299
+ const sourceMaxVisualCol = isLastSourceSegment ? currentVL.length : Math.max(0, currentVL.length - 1);
1300
+
1301
+ const isLastTargetSegment =
1302
+ targetVisualLine === visualLines.length - 1 ||
1303
+ visualLines[targetVisualLine + 1]?.logicalLine !== targetVL.logicalLine;
1304
+ const targetMaxVisualCol = isLastTargetSegment ? targetVL.length : Math.max(0, targetVL.length - 1);
1305
+
1306
+ const moveToVisualCol = this.computeVerticalMoveColumn(currentVisualCol, sourceMaxVisualCol, targetMaxVisualCol);
1307
+
1308
+ // Set cursor position
1309
+ this.state.cursorLine = targetVL.logicalLine;
1310
+ const targetCol = targetVL.startCol + moveToVisualCol;
1311
+ const logicalLine = this.state.lines[targetVL.logicalLine] || "";
1312
+ this.state.cursorCol = Math.min(targetCol, logicalLine.length);
1313
+
1314
+ // Snap cursor to atomic segment boundary (e.g. paste markers)
1315
+ // so the cursor never lands in the middle of a multi-grapheme unit.
1316
+ // Single-grapheme segments don't need snapping.
1317
+ const segments = [...this.segment(logicalLine)];
1318
+ for (const seg of segments) {
1319
+ if (seg.index > this.state.cursorCol) break;
1320
+ if (seg.segment.length <= 1) continue;
1321
+ if (this.state.cursorCol < seg.index + seg.segment.length) {
1322
+ const isContinuation = seg.index < targetVL.startCol;
1323
+ const isMovingDown = targetVisualLine > currentVisualLine;
1324
+
1325
+ if (isContinuation && isMovingDown) {
1326
+ // The segment started on a previous visual line, and we
1327
+ // already visited it on the way down. Skip all remaining
1328
+ // continuation VLs and land on the first VL past it.
1329
+ const segEnd = seg.index + seg.segment.length;
1330
+ let next = targetVisualLine + 1;
1331
+ while (
1332
+ next < visualLines.length &&
1333
+ visualLines[next].logicalLine === targetVL.logicalLine &&
1334
+ visualLines[next].startCol < segEnd
1335
+ ) {
1336
+ next++;
1337
+ }
1338
+ if (next < visualLines.length) {
1339
+ this.moveToVisualLine(visualLines, currentVisualLine, next);
1340
+ return;
1341
+ }
1342
+ }
1343
+
1344
+ // Snap to the start of the segment so it gets highlighted.
1345
+ // Store the pre-snap position so the next vertical move can
1346
+ // resolve it to the correct visual column.
1347
+ this.snappedFromCursorCol = this.state.cursorCol;
1348
+ this.state.cursorCol = seg.index;
1349
+ return;
1350
+ }
1351
+ }
1352
+
1353
+ // No snap occurred – we moved out of the atomic segment.
1354
+ this.snappedFromCursorCol = null;
1355
+ }
1356
+
1357
+ /**
1358
+ * Compute the target visual column for vertical cursor movement.
1359
+ * Implements the sticky column decision table:
1360
+ *
1361
+ * | P | S | T | U | Scenario | Set Preferred | Move To |
1362
+ * |---|---|---|---| ---------------------------------------------------- |---------------|-------------|
1363
+ * | 0 | * | 0 | - | Start nav, target fits | null | current |
1364
+ * | 0 | * | 1 | - | Start nav, target shorter | current | target end |
1365
+ * | 1 | 0 | 0 | 0 | Clamped, target fits preferred | null | preferred |
1366
+ * | 1 | 0 | 0 | 1 | Clamped, target longer but still can't fit preferred | keep | target end |
1367
+ * | 1 | 0 | 1 | - | Clamped, target even shorter | keep | target end |
1368
+ * | 1 | 1 | 0 | - | Rewrapped, target fits current | null | current |
1369
+ * | 1 | 1 | 1 | - | Rewrapped, target shorter than current | current | target end |
1370
+ *
1371
+ * Where:
1372
+ * - P = preferred col is set
1373
+ * - S = cursor in middle of source line (not clamped to end)
1374
+ * - T = target line shorter than current visual col
1375
+ * - U = target line shorter than preferred col
1376
+ */
1377
+ private computeVerticalMoveColumn(
1378
+ currentVisualCol: number,
1379
+ sourceMaxVisualCol: number,
1380
+ targetMaxVisualCol: number,
1381
+ ): number {
1382
+ const hasPreferred = this.preferredVisualCol !== null; // P
1383
+ const cursorInMiddle = currentVisualCol < sourceMaxVisualCol; // S
1384
+ const targetTooShort = targetMaxVisualCol < currentVisualCol; // T
1385
+
1386
+ if (!hasPreferred || cursorInMiddle) {
1387
+ if (targetTooShort) {
1388
+ // Cases 2 and 7
1389
+ this.preferredVisualCol = currentVisualCol;
1390
+ return targetMaxVisualCol;
1391
+ }
1392
+
1393
+ // Cases 1 and 6
1394
+ this.preferredVisualCol = null;
1395
+ return currentVisualCol;
1396
+ }
1397
+
1398
+ const targetCantFitPreferred = targetMaxVisualCol < this.preferredVisualCol!; // U
1399
+ if (targetTooShort || targetCantFitPreferred) {
1400
+ // Cases 4 and 5
1401
+ return targetMaxVisualCol;
1402
+ }
1403
+
1404
+ // Case 3
1405
+ const result = this.preferredVisualCol!;
1406
+ this.preferredVisualCol = null;
1407
+ return result;
1408
+ }
1409
+
1410
+ private moveToLineStart(): void {
1411
+ this.lastAction = null;
1412
+ this.setCursorCol(0);
1413
+ }
1414
+
1415
+ private moveToLineEnd(): void {
1416
+ this.lastAction = null;
1417
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1418
+ this.setCursorCol(currentLine.length);
1419
+ }
1420
+
1421
+ private deleteToStartOfLine(): void {
1422
+ this.historyIndex = -1; // Exit history browsing mode
1423
+
1424
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1425
+
1426
+ if (this.state.cursorCol > 0) {
1427
+ this.pushUndoSnapshot();
1428
+
1429
+ // Calculate text to be deleted and save to kill ring (backward deletion = prepend)
1430
+ const deletedText = currentLine.slice(0, this.state.cursorCol);
1431
+ this.killRing.push(deletedText, { prepend: true, accumulate: this.lastAction === "kill" });
1432
+ this.lastAction = "kill";
1433
+
1434
+ // Delete from start of line up to cursor
1435
+ this.state.lines[this.state.cursorLine] = currentLine.slice(this.state.cursorCol);
1436
+ this.setCursorCol(0);
1437
+ } else if (this.state.cursorLine > 0) {
1438
+ this.pushUndoSnapshot();
1439
+
1440
+ // At start of line - merge with previous line, treating newline as deleted text
1441
+ this.killRing.push("\n", { prepend: true, accumulate: this.lastAction === "kill" });
1442
+ this.lastAction = "kill";
1443
+
1444
+ const previousLine = this.state.lines[this.state.cursorLine - 1] || "";
1445
+ this.state.lines[this.state.cursorLine - 1] = previousLine + currentLine;
1446
+ this.state.lines.splice(this.state.cursorLine, 1);
1447
+ this.state.cursorLine--;
1448
+ this.setCursorCol(previousLine.length);
1449
+ }
1450
+
1451
+ if (this.onChange) {
1452
+ this.onChange(this.getText());
1453
+ }
1454
+ }
1455
+
1456
+ private deleteToEndOfLine(): void {
1457
+ this.historyIndex = -1; // Exit history browsing mode
1458
+
1459
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1460
+
1461
+ if (this.state.cursorCol < currentLine.length) {
1462
+ this.pushUndoSnapshot();
1463
+
1464
+ // Calculate text to be deleted and save to kill ring (forward deletion = append)
1465
+ const deletedText = currentLine.slice(this.state.cursorCol);
1466
+ this.killRing.push(deletedText, { prepend: false, accumulate: this.lastAction === "kill" });
1467
+ this.lastAction = "kill";
1468
+
1469
+ // Delete from cursor to end of line
1470
+ this.state.lines[this.state.cursorLine] = currentLine.slice(0, this.state.cursorCol);
1471
+ } else if (this.state.cursorLine < this.state.lines.length - 1) {
1472
+ this.pushUndoSnapshot();
1473
+
1474
+ // At end of line - merge with next line, treating newline as deleted text
1475
+ this.killRing.push("\n", { prepend: false, accumulate: this.lastAction === "kill" });
1476
+ this.lastAction = "kill";
1477
+
1478
+ const nextLine = this.state.lines[this.state.cursorLine + 1] || "";
1479
+ this.state.lines[this.state.cursorLine] = currentLine + nextLine;
1480
+ this.state.lines.splice(this.state.cursorLine + 1, 1);
1481
+ }
1482
+
1483
+ if (this.onChange) {
1484
+ this.onChange(this.getText());
1485
+ }
1486
+ }
1487
+
1488
+ private deleteWordBackwards(): void {
1489
+ this.historyIndex = -1; // Exit history browsing mode
1490
+
1491
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1492
+
1493
+ // If at start of line, behave like backspace at column 0 (merge with previous line)
1494
+ if (this.state.cursorCol === 0) {
1495
+ if (this.state.cursorLine > 0) {
1496
+ this.pushUndoSnapshot();
1497
+
1498
+ // Treat newline as deleted text (backward deletion = prepend)
1499
+ this.killRing.push("\n", { prepend: true, accumulate: this.lastAction === "kill" });
1500
+ this.lastAction = "kill";
1501
+
1502
+ const previousLine = this.state.lines[this.state.cursorLine - 1] || "";
1503
+ this.state.lines[this.state.cursorLine - 1] = previousLine + currentLine;
1504
+ this.state.lines.splice(this.state.cursorLine, 1);
1505
+ this.state.cursorLine--;
1506
+ this.setCursorCol(previousLine.length);
1507
+ }
1508
+ } else {
1509
+ this.pushUndoSnapshot();
1510
+
1511
+ // Save lastAction before cursor movement (moveWordBackwards resets it)
1512
+ const wasKill = this.lastAction === "kill";
1513
+
1514
+ const oldCursorCol = this.state.cursorCol;
1515
+ this.moveWordBackwards();
1516
+ const deleteFrom = this.state.cursorCol;
1517
+ this.setCursorCol(oldCursorCol);
1518
+
1519
+ const deletedText = currentLine.slice(deleteFrom, this.state.cursorCol);
1520
+ this.killRing.push(deletedText, { prepend: true, accumulate: wasKill });
1521
+ this.lastAction = "kill";
1522
+
1523
+ this.state.lines[this.state.cursorLine] =
1524
+ currentLine.slice(0, deleteFrom) + currentLine.slice(this.state.cursorCol);
1525
+ this.setCursorCol(deleteFrom);
1526
+ }
1527
+
1528
+ if (this.onChange) {
1529
+ this.onChange(this.getText());
1530
+ }
1531
+ }
1532
+
1533
+ private deleteWordForward(): void {
1534
+ this.historyIndex = -1; // Exit history browsing mode
1535
+
1536
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1537
+
1538
+ // If at end of line, merge with next line (delete the newline)
1539
+ if (this.state.cursorCol >= currentLine.length) {
1540
+ if (this.state.cursorLine < this.state.lines.length - 1) {
1541
+ this.pushUndoSnapshot();
1542
+
1543
+ // Treat newline as deleted text (forward deletion = append)
1544
+ this.killRing.push("\n", { prepend: false, accumulate: this.lastAction === "kill" });
1545
+ this.lastAction = "kill";
1546
+
1547
+ const nextLine = this.state.lines[this.state.cursorLine + 1] || "";
1548
+ this.state.lines[this.state.cursorLine] = currentLine + nextLine;
1549
+ this.state.lines.splice(this.state.cursorLine + 1, 1);
1550
+ }
1551
+ } else {
1552
+ this.pushUndoSnapshot();
1553
+
1554
+ // Save lastAction before cursor movement (moveWordForwards resets it)
1555
+ const wasKill = this.lastAction === "kill";
1556
+
1557
+ const oldCursorCol = this.state.cursorCol;
1558
+ this.moveWordForwards();
1559
+ const deleteTo = this.state.cursorCol;
1560
+ this.setCursorCol(oldCursorCol);
1561
+
1562
+ const deletedText = currentLine.slice(this.state.cursorCol, deleteTo);
1563
+ this.killRing.push(deletedText, { prepend: false, accumulate: wasKill });
1564
+ this.lastAction = "kill";
1565
+
1566
+ this.state.lines[this.state.cursorLine] =
1567
+ currentLine.slice(0, this.state.cursorCol) + currentLine.slice(deleteTo);
1568
+ }
1569
+
1570
+ if (this.onChange) {
1571
+ this.onChange(this.getText());
1572
+ }
1573
+ }
1574
+
1575
+ private handleForwardDelete(): void {
1576
+ this.historyIndex = -1; // Exit history browsing mode
1577
+ this.lastAction = null;
1578
+
1579
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1580
+
1581
+ if (this.state.cursorCol < currentLine.length) {
1582
+ this.pushUndoSnapshot();
1583
+
1584
+ // Delete grapheme at cursor position (handles emojis, combining characters, etc.)
1585
+ const afterCursor = currentLine.slice(this.state.cursorCol);
1586
+
1587
+ // Find the first grapheme at cursor
1588
+ const graphemes = [...this.segment(afterCursor)];
1589
+ const firstGrapheme = graphemes[0];
1590
+ const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;
1591
+
1592
+ const before = currentLine.slice(0, this.state.cursorCol);
1593
+ const after = currentLine.slice(this.state.cursorCol + graphemeLength);
1594
+ this.state.lines[this.state.cursorLine] = before + after;
1595
+ } else if (this.state.cursorLine < this.state.lines.length - 1) {
1596
+ this.pushUndoSnapshot();
1597
+
1598
+ // At end of line - merge with next line
1599
+ const nextLine = this.state.lines[this.state.cursorLine + 1] || "";
1600
+ this.state.lines[this.state.cursorLine] = currentLine + nextLine;
1601
+ this.state.lines.splice(this.state.cursorLine + 1, 1);
1602
+ }
1603
+
1604
+ if (this.onChange) {
1605
+ this.onChange(this.getText());
1606
+ }
1607
+
1608
+ // Update or re-trigger autocomplete after forward delete
1609
+ if (this.autocompleteState) {
1610
+ this.updateAutocomplete();
1611
+ } else {
1612
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1613
+ const textBeforeCursor = currentLine.slice(0, this.state.cursorCol);
1614
+ // Slash command context
1615
+ if (this.isInSlashCommandContext(textBeforeCursor)) {
1616
+ this.tryTriggerAutocomplete();
1617
+ }
1618
+ // Symbol-based completion context like @ or #
1619
+ else if (textBeforeCursor.match(/(?:^|[\s])[@#][^\s]*$/)) {
1620
+ this.tryTriggerAutocomplete();
1621
+ }
1622
+ }
1623
+ }
1624
+
1625
+ /**
1626
+ * Build a mapping from visual lines to logical positions.
1627
+ * Returns an array where each element represents a visual line with:
1628
+ * - logicalLine: index into this.state.lines
1629
+ * - startCol: starting column in the logical line
1630
+ * - length: length of this visual line segment
1631
+ */
1632
+ private buildVisualLineMap(width: number): Array<{ logicalLine: number; startCol: number; length: number }> {
1633
+ const visualLines: Array<{ logicalLine: number; startCol: number; length: number }> = [];
1634
+
1635
+ for (let i = 0; i < this.state.lines.length; i++) {
1636
+ const line = this.state.lines[i] || "";
1637
+ const lineVisWidth = visibleWidth(line);
1638
+ if (line.length === 0) {
1639
+ // Empty line still takes one visual line
1640
+ visualLines.push({ logicalLine: i, startCol: 0, length: 0 });
1641
+ } else if (lineVisWidth <= width) {
1642
+ visualLines.push({ logicalLine: i, startCol: 0, length: line.length });
1643
+ } else {
1644
+ // Line needs wrapping - use word-aware wrapping
1645
+ const chunks = wordWrapLine(line, width, [...this.segment(line)]);
1646
+ for (const chunk of chunks) {
1647
+ visualLines.push({
1648
+ logicalLine: i,
1649
+ startCol: chunk.startIndex,
1650
+ length: chunk.endIndex - chunk.startIndex,
1651
+ });
1652
+ }
1653
+ }
1654
+ }
1655
+
1656
+ return visualLines;
1657
+ }
1658
+
1659
+ /**
1660
+ * Find the visual line index that contains the given logical position.
1661
+ */
1662
+ private findVisualLineAt(
1663
+ visualLines: Array<{ logicalLine: number; startCol: number; length: number }>,
1664
+ line: number,
1665
+ col: number,
1666
+ ): number {
1667
+ for (let i = 0; i < visualLines.length; i++) {
1668
+ const vl = visualLines[i];
1669
+ if (!vl || vl.logicalLine !== line) continue;
1670
+ const offset = col - vl.startCol;
1671
+ // Cursor is in this segment if it's within range. For the last
1672
+ // segment of a logical line, cursor can be at length (end position)
1673
+ const isLastSegmentOfLine = i === visualLines.length - 1 || visualLines[i + 1]?.logicalLine !== vl.logicalLine;
1674
+ if (offset >= 0 && (offset < vl.length || (isLastSegmentOfLine && offset === vl.length))) {
1675
+ return i;
1676
+ }
1677
+ }
1678
+ return visualLines.length - 1;
1679
+ }
1680
+
1681
+ /**
1682
+ * Find the visual line index for the current cursor position.
1683
+ */
1684
+ private findCurrentVisualLine(
1685
+ visualLines: Array<{ logicalLine: number; startCol: number; length: number }>,
1686
+ ): number {
1687
+ return this.findVisualLineAt(visualLines, this.state.cursorLine, this.state.cursorCol);
1688
+ }
1689
+
1690
+ private moveCursor(deltaLine: number, deltaCol: number): void {
1691
+ this.lastAction = null;
1692
+ const visualLines = this.buildVisualLineMap(this.lastWidth);
1693
+ const currentVisualLine = this.findCurrentVisualLine(visualLines);
1694
+
1695
+ if (deltaLine !== 0) {
1696
+ const targetVisualLine = currentVisualLine + deltaLine;
1697
+
1698
+ if (targetVisualLine >= 0 && targetVisualLine < visualLines.length) {
1699
+ this.moveToVisualLine(visualLines, currentVisualLine, targetVisualLine);
1700
+ }
1701
+ }
1702
+
1703
+ if (deltaCol !== 0) {
1704
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1705
+
1706
+ if (deltaCol > 0) {
1707
+ // Moving right - move by one grapheme (handles emojis, combining characters, etc.)
1708
+ if (this.state.cursorCol < currentLine.length) {
1709
+ const afterCursor = currentLine.slice(this.state.cursorCol);
1710
+ const graphemes = [...this.segment(afterCursor)];
1711
+ const firstGrapheme = graphemes[0];
1712
+ this.setCursorCol(this.state.cursorCol + (firstGrapheme ? firstGrapheme.segment.length : 1));
1713
+ } else if (this.state.cursorLine < this.state.lines.length - 1) {
1714
+ // Wrap to start of next logical line
1715
+ this.state.cursorLine++;
1716
+ this.setCursorCol(0);
1717
+ } else {
1718
+ // At end of last line - can't move, but set preferredVisualCol for up/down navigation
1719
+ const currentVL = visualLines[currentVisualLine];
1720
+ if (currentVL) {
1721
+ this.preferredVisualCol = this.state.cursorCol - currentVL.startCol;
1722
+ }
1723
+ }
1724
+ } else {
1725
+ // Moving left - move by one grapheme (handles emojis, combining characters, etc.)
1726
+ if (this.state.cursorCol > 0) {
1727
+ const beforeCursor = currentLine.slice(0, this.state.cursorCol);
1728
+ const graphemes = [...this.segment(beforeCursor)];
1729
+ const lastGrapheme = graphemes[graphemes.length - 1];
1730
+ this.setCursorCol(this.state.cursorCol - (lastGrapheme ? lastGrapheme.segment.length : 1));
1731
+ } else if (this.state.cursorLine > 0) {
1732
+ // Wrap to end of previous logical line
1733
+ this.state.cursorLine--;
1734
+ const prevLine = this.state.lines[this.state.cursorLine] || "";
1735
+ this.setCursorCol(prevLine.length);
1736
+ }
1737
+ }
1738
+ }
1739
+ }
1740
+
1741
+ /**
1742
+ * Scroll by a page (direction: -1 for up, 1 for down).
1743
+ * Moves cursor by the page size while keeping it in bounds.
1744
+ */
1745
+ private pageScroll(direction: -1 | 1): void {
1746
+ this.lastAction = null;
1747
+ const terminalRows = this.tui.terminal.rows;
1748
+ const pageSize = Math.max(5, Math.floor(terminalRows * 0.3));
1749
+
1750
+ const visualLines = this.buildVisualLineMap(this.lastWidth);
1751
+ const currentVisualLine = this.findCurrentVisualLine(visualLines);
1752
+ const targetVisualLine = Math.max(0, Math.min(visualLines.length - 1, currentVisualLine + direction * pageSize));
1753
+
1754
+ this.moveToVisualLine(visualLines, currentVisualLine, targetVisualLine);
1755
+ }
1756
+
1757
+ private moveWordBackwards(): void {
1758
+ this.lastAction = null;
1759
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1760
+
1761
+ // If at start of line, move to end of previous line
1762
+ if (this.state.cursorCol === 0) {
1763
+ if (this.state.cursorLine > 0) {
1764
+ this.state.cursorLine--;
1765
+ const prevLine = this.state.lines[this.state.cursorLine] || "";
1766
+ this.setCursorCol(prevLine.length);
1767
+ }
1768
+ return;
1769
+ }
1770
+
1771
+ const textBeforeCursor = currentLine.slice(0, this.state.cursorCol);
1772
+ const graphemes = [...this.segment(textBeforeCursor)];
1773
+ let newCol = this.state.cursorCol;
1774
+
1775
+ // Skip trailing whitespace
1776
+ while (
1777
+ graphemes.length > 0 &&
1778
+ !isPasteMarker(graphemes[graphemes.length - 1]?.segment || "") &&
1779
+ isWhitespaceChar(graphemes[graphemes.length - 1]?.segment || "")
1780
+ ) {
1781
+ newCol -= graphemes.pop()?.segment.length || 0;
1782
+ }
1783
+
1784
+ if (graphemes.length > 0) {
1785
+ const lastGrapheme = graphemes[graphemes.length - 1]?.segment || "";
1786
+ if (isPasteMarker(lastGrapheme)) {
1787
+ // Paste marker is a single atomic word
1788
+ newCol -= graphemes.pop()?.segment.length || 0;
1789
+ } else if (isPunctuationChar(lastGrapheme)) {
1790
+ // Skip punctuation run
1791
+ while (
1792
+ graphemes.length > 0 &&
1793
+ isPunctuationChar(graphemes[graphemes.length - 1]?.segment || "") &&
1794
+ !isPasteMarker(graphemes[graphemes.length - 1]?.segment || "")
1795
+ ) {
1796
+ newCol -= graphemes.pop()?.segment.length || 0;
1797
+ }
1798
+ } else {
1799
+ // Skip word run
1800
+ while (
1801
+ graphemes.length > 0 &&
1802
+ !isWhitespaceChar(graphemes[graphemes.length - 1]?.segment || "") &&
1803
+ !isPunctuationChar(graphemes[graphemes.length - 1]?.segment || "") &&
1804
+ !isPasteMarker(graphemes[graphemes.length - 1]?.segment || "")
1805
+ ) {
1806
+ newCol -= graphemes.pop()?.segment.length || 0;
1807
+ }
1808
+ }
1809
+ }
1810
+
1811
+ this.setCursorCol(newCol);
1812
+ }
1813
+
1814
+ /**
1815
+ * Yank (paste) the most recent kill ring entry at cursor position.
1816
+ */
1817
+ private yank(): void {
1818
+ if (this.killRing.length === 0) return;
1819
+
1820
+ this.pushUndoSnapshot();
1821
+
1822
+ const text = this.killRing.peek()!;
1823
+ this.insertYankedText(text);
1824
+
1825
+ this.lastAction = "yank";
1826
+ }
1827
+
1828
+ /**
1829
+ * Cycle through kill ring (only works immediately after yank or yank-pop).
1830
+ * Replaces the last yanked text with the previous entry in the ring.
1831
+ */
1832
+ private yankPop(): void {
1833
+ // Only works if we just yanked and have more than one entry
1834
+ if (this.lastAction !== "yank" || this.killRing.length <= 1) return;
1835
+
1836
+ this.pushUndoSnapshot();
1837
+
1838
+ // Delete the previously yanked text (still at end of ring before rotation)
1839
+ this.deleteYankedText();
1840
+
1841
+ // Rotate the ring: move end to front
1842
+ this.killRing.rotate();
1843
+
1844
+ // Insert the new most recent entry (now at end after rotation)
1845
+ const text = this.killRing.peek()!;
1846
+ this.insertYankedText(text);
1847
+
1848
+ this.lastAction = "yank";
1849
+ }
1850
+
1851
+ /**
1852
+ * Insert text at cursor position (used by yank operations).
1853
+ */
1854
+ private insertYankedText(text: string): void {
1855
+ this.historyIndex = -1; // Exit history browsing mode
1856
+ const lines = text.split("\n");
1857
+
1858
+ if (lines.length === 1) {
1859
+ // Single line - insert at cursor
1860
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1861
+ const before = currentLine.slice(0, this.state.cursorCol);
1862
+ const after = currentLine.slice(this.state.cursorCol);
1863
+ this.state.lines[this.state.cursorLine] = before + text + after;
1864
+ this.setCursorCol(this.state.cursorCol + text.length);
1865
+ } else {
1866
+ // Multi-line insert
1867
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1868
+ const before = currentLine.slice(0, this.state.cursorCol);
1869
+ const after = currentLine.slice(this.state.cursorCol);
1870
+
1871
+ // First line merges with text before cursor
1872
+ this.state.lines[this.state.cursorLine] = before + (lines[0] || "");
1873
+
1874
+ // Insert middle lines
1875
+ for (let i = 1; i < lines.length - 1; i++) {
1876
+ this.state.lines.splice(this.state.cursorLine + i, 0, lines[i] || "");
1877
+ }
1878
+
1879
+ // Last line merges with text after cursor
1880
+ const lastLineIndex = this.state.cursorLine + lines.length - 1;
1881
+ this.state.lines.splice(lastLineIndex, 0, (lines[lines.length - 1] || "") + after);
1882
+
1883
+ // Update cursor position
1884
+ this.state.cursorLine = lastLineIndex;
1885
+ this.setCursorCol((lines[lines.length - 1] || "").length);
1886
+ }
1887
+
1888
+ if (this.onChange) {
1889
+ this.onChange(this.getText());
1890
+ }
1891
+ }
1892
+
1893
+ /**
1894
+ * Delete the previously yanked text (used by yank-pop).
1895
+ * The yanked text is derived from killRing[end] since it hasn't been rotated yet.
1896
+ */
1897
+ private deleteYankedText(): void {
1898
+ const yankedText = this.killRing.peek();
1899
+ if (!yankedText) return;
1900
+
1901
+ const yankLines = yankedText.split("\n");
1902
+
1903
+ if (yankLines.length === 1) {
1904
+ // Single line - delete backward from cursor
1905
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1906
+ const deleteLen = yankedText.length;
1907
+ const before = currentLine.slice(0, this.state.cursorCol - deleteLen);
1908
+ const after = currentLine.slice(this.state.cursorCol);
1909
+ this.state.lines[this.state.cursorLine] = before + after;
1910
+ this.setCursorCol(this.state.cursorCol - deleteLen);
1911
+ } else {
1912
+ // Multi-line delete - cursor is at end of last yanked line
1913
+ const startLine = this.state.cursorLine - (yankLines.length - 1);
1914
+ const startCol = (this.state.lines[startLine] || "").length - (yankLines[0] || "").length;
1915
+
1916
+ // Get text after cursor on current line
1917
+ const afterCursor = (this.state.lines[this.state.cursorLine] || "").slice(this.state.cursorCol);
1918
+
1919
+ // Get text before yank start position
1920
+ const beforeYank = (this.state.lines[startLine] || "").slice(0, startCol);
1921
+
1922
+ // Remove all lines from startLine to cursorLine and replace with merged line
1923
+ this.state.lines.splice(startLine, yankLines.length, beforeYank + afterCursor);
1924
+
1925
+ // Update cursor
1926
+ this.state.cursorLine = startLine;
1927
+ this.setCursorCol(startCol);
1928
+ }
1929
+
1930
+ if (this.onChange) {
1931
+ this.onChange(this.getText());
1932
+ }
1933
+ }
1934
+
1935
+ private pushUndoSnapshot(): void {
1936
+ this.undoStack.push(this.state);
1937
+ }
1938
+
1939
+ private undo(): void {
1940
+ this.historyIndex = -1; // Exit history browsing mode
1941
+ const snapshot = this.undoStack.pop();
1942
+ if (!snapshot) return;
1943
+ Object.assign(this.state, snapshot);
1944
+ this.lastAction = null;
1945
+ this.preferredVisualCol = null;
1946
+ if (this.onChange) {
1947
+ this.onChange(this.getText());
1948
+ }
1949
+ }
1950
+
1951
+ /**
1952
+ * Jump to the first occurrence of a character in the specified direction.
1953
+ * Multi-line search. Case-sensitive. Skips the current cursor position.
1954
+ */
1955
+ private jumpToChar(char: string, direction: "forward" | "backward"): void {
1956
+ this.lastAction = null;
1957
+ const isForward = direction === "forward";
1958
+ const lines = this.state.lines;
1959
+
1960
+ const end = isForward ? lines.length : -1;
1961
+ const step = isForward ? 1 : -1;
1962
+
1963
+ for (let lineIdx = this.state.cursorLine; lineIdx !== end; lineIdx += step) {
1964
+ const line = lines[lineIdx] || "";
1965
+ const isCurrentLine = lineIdx === this.state.cursorLine;
1966
+
1967
+ // Current line: start after/before cursor; other lines: search full line
1968
+ const searchFrom = isCurrentLine
1969
+ ? isForward
1970
+ ? this.state.cursorCol + 1
1971
+ : this.state.cursorCol - 1
1972
+ : undefined;
1973
+
1974
+ const idx = isForward ? line.indexOf(char, searchFrom) : line.lastIndexOf(char, searchFrom);
1975
+
1976
+ if (idx !== -1) {
1977
+ this.state.cursorLine = lineIdx;
1978
+ this.setCursorCol(idx);
1979
+ return;
1980
+ }
1981
+ }
1982
+ // No match found - cursor stays in place
1983
+ }
1984
+
1985
+ private moveWordForwards(): void {
1986
+ this.lastAction = null;
1987
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1988
+
1989
+ // If at end of line, move to start of next line
1990
+ if (this.state.cursorCol >= currentLine.length) {
1991
+ if (this.state.cursorLine < this.state.lines.length - 1) {
1992
+ this.state.cursorLine++;
1993
+ this.setCursorCol(0);
1994
+ }
1995
+ return;
1996
+ }
1997
+
1998
+ const textAfterCursor = currentLine.slice(this.state.cursorCol);
1999
+ const segments = this.segment(textAfterCursor);
2000
+ const iterator = segments[Symbol.iterator]();
2001
+ let next = iterator.next();
2002
+ let newCol = this.state.cursorCol;
2003
+
2004
+ // Skip leading whitespace
2005
+ while (!next.done && !isPasteMarker(next.value.segment) && isWhitespaceChar(next.value.segment)) {
2006
+ newCol += next.value.segment.length;
2007
+ next = iterator.next();
2008
+ }
2009
+
2010
+ if (!next.done) {
2011
+ const firstGrapheme = next.value.segment;
2012
+ if (isPasteMarker(firstGrapheme)) {
2013
+ // Paste marker is a single atomic word
2014
+ newCol += firstGrapheme.length;
2015
+ } else if (isPunctuationChar(firstGrapheme)) {
2016
+ // Skip punctuation run
2017
+ while (!next.done && isPunctuationChar(next.value.segment) && !isPasteMarker(next.value.segment)) {
2018
+ newCol += next.value.segment.length;
2019
+ next = iterator.next();
2020
+ }
2021
+ } else {
2022
+ // Skip word run
2023
+ while (
2024
+ !next.done &&
2025
+ !isWhitespaceChar(next.value.segment) &&
2026
+ !isPunctuationChar(next.value.segment) &&
2027
+ !isPasteMarker(next.value.segment)
2028
+ ) {
2029
+ newCol += next.value.segment.length;
2030
+ next = iterator.next();
2031
+ }
2032
+ }
2033
+ }
2034
+
2035
+ this.setCursorCol(newCol);
2036
+ }
2037
+
2038
+ // Slash menu only allowed on the first line of the editor
2039
+ private isSlashMenuAllowed(): boolean {
2040
+ return this.state.cursorLine === 0;
2041
+ }
2042
+
2043
+ // Helper method to check if cursor is at start of message (for slash command detection)
2044
+ private isAtStartOfMessage(): boolean {
2045
+ if (!this.isSlashMenuAllowed()) return false;
2046
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
2047
+ const beforeCursor = currentLine.slice(0, this.state.cursorCol);
2048
+ return beforeCursor.trim() === "" || beforeCursor.trim() === "/";
2049
+ }
2050
+
2051
+ private isInSlashCommandContext(textBeforeCursor: string): boolean {
2052
+ return this.isSlashMenuAllowed() && textBeforeCursor.trimStart().startsWith("/");
2053
+ }
2054
+
2055
+ // Autocomplete methods
2056
+ /**
2057
+ * Find the best autocomplete item index for the given prefix.
2058
+ * Returns -1 if no match is found.
2059
+ *
2060
+ * Match priority:
2061
+ * 1. Exact match (prefix === item.value) -> always selected
2062
+ * 2. Prefix match -> first item whose value starts with prefix
2063
+ * 3. No match -> -1 (keep default highlight)
2064
+ *
2065
+ * Matching is case-sensitive and checks item.value only.
2066
+ */
2067
+ private getBestAutocompleteMatchIndex(items: Array<{ value: string; label: string }>, prefix: string): number {
2068
+ if (!prefix) return -1;
2069
+
2070
+ let firstPrefixIndex = -1;
2071
+
2072
+ for (let i = 0; i < items.length; i++) {
2073
+ const value = items[i]!.value;
2074
+ if (value === prefix) {
2075
+ return i; // Exact match always wins
2076
+ }
2077
+ if (firstPrefixIndex === -1 && value.startsWith(prefix)) {
2078
+ firstPrefixIndex = i;
2079
+ }
2080
+ }
2081
+
2082
+ return firstPrefixIndex;
2083
+ }
2084
+
2085
+ private createAutocompleteList(
2086
+ prefix: string,
2087
+ items: Array<{ value: string; label: string; description?: string }>,
2088
+ ): SelectList {
2089
+ const layout = prefix.startsWith("/") ? SLASH_COMMAND_SELECT_LIST_LAYOUT : undefined;
2090
+ return new SelectList(items, this.autocompleteMaxVisible, this.theme.selectList, layout);
2091
+ }
2092
+
2093
+ private tryTriggerAutocomplete(explicitTab: boolean = false): void {
2094
+ this.requestAutocomplete({ force: false, explicitTab });
2095
+ }
2096
+
2097
+ private handleTabCompletion(): void {
2098
+ if (!this.autocompleteProvider) return;
2099
+
2100
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
2101
+ const beforeCursor = currentLine.slice(0, this.state.cursorCol);
2102
+
2103
+ if (this.isInSlashCommandContext(beforeCursor) && !beforeCursor.trimStart().includes(" ")) {
2104
+ this.handleSlashCommandCompletion();
2105
+ } else {
2106
+ this.forceFileAutocomplete(true);
2107
+ }
2108
+ }
2109
+
2110
+ private handleSlashCommandCompletion(): void {
2111
+ this.requestAutocomplete({ force: false, explicitTab: true });
2112
+ }
2113
+
2114
+ private forceFileAutocomplete(explicitTab: boolean = false): void {
2115
+ this.requestAutocomplete({ force: true, explicitTab });
2116
+ }
2117
+
2118
+ private requestAutocomplete(options: { force: boolean; explicitTab: boolean }): void {
2119
+ if (!this.autocompleteProvider) return;
2120
+
2121
+ if (options.force) {
2122
+ const shouldTrigger =
2123
+ !this.autocompleteProvider.shouldTriggerFileCompletion ||
2124
+ this.autocompleteProvider.shouldTriggerFileCompletion(
2125
+ this.state.lines,
2126
+ this.state.cursorLine,
2127
+ this.state.cursorCol,
2128
+ );
2129
+ if (!shouldTrigger) {
2130
+ return;
2131
+ }
2132
+ }
2133
+
2134
+ this.cancelAutocompleteRequest();
2135
+ const startToken = ++this.autocompleteStartToken;
2136
+
2137
+ const debounceMs = this.getAutocompleteDebounceMs(options);
2138
+ if (debounceMs > 0) {
2139
+ this.autocompleteDebounceTimer = setTimeout(() => {
2140
+ this.autocompleteDebounceTimer = undefined;
2141
+ void this.startAutocompleteRequest(startToken, options);
2142
+ }, debounceMs);
2143
+ return;
2144
+ }
2145
+
2146
+ void this.startAutocompleteRequest(startToken, options);
2147
+ }
2148
+
2149
+ private async startAutocompleteRequest(
2150
+ startToken: number,
2151
+ options: { force: boolean; explicitTab: boolean },
2152
+ ): Promise<void> {
2153
+ const previousTask = this.autocompleteRequestTask;
2154
+ this.autocompleteRequestTask = (async () => {
2155
+ await previousTask;
2156
+ if (startToken !== this.autocompleteStartToken || !this.autocompleteProvider) {
2157
+ return;
2158
+ }
2159
+
2160
+ const controller = new AbortController();
2161
+ this.autocompleteAbort = controller;
2162
+ const requestId = ++this.autocompleteRequestId;
2163
+ const snapshotText = this.getText();
2164
+ const snapshotLine = this.state.cursorLine;
2165
+ const snapshotCol = this.state.cursorCol;
2166
+
2167
+ await this.runAutocompleteRequest(requestId, controller, snapshotText, snapshotLine, snapshotCol, options);
2168
+ })();
2169
+ await this.autocompleteRequestTask;
2170
+ }
2171
+
2172
+ private getAutocompleteDebounceMs(options: { force: boolean; explicitTab: boolean }): number {
2173
+ if (options.explicitTab || options.force) {
2174
+ return 0;
2175
+ }
2176
+
2177
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
2178
+ const textBeforeCursor = currentLine.slice(0, this.state.cursorCol);
2179
+ const isSymbolAutocompleteContext = /(?:^|[ \t])(?:@(?:"[^"]*|[^\s]*)|#[^\s]*)$/.test(textBeforeCursor);
2180
+ return isSymbolAutocompleteContext ? ATTACHMENT_AUTOCOMPLETE_DEBOUNCE_MS : 0;
2181
+ }
2182
+
2183
+ private async runAutocompleteRequest(
2184
+ requestId: number,
2185
+ controller: AbortController,
2186
+ snapshotText: string,
2187
+ snapshotLine: number,
2188
+ snapshotCol: number,
2189
+ options: { force: boolean; explicitTab: boolean },
2190
+ ): Promise<void> {
2191
+ if (!this.autocompleteProvider) return;
2192
+
2193
+ const suggestions = await this.autocompleteProvider.getSuggestions(
2194
+ this.state.lines,
2195
+ this.state.cursorLine,
2196
+ this.state.cursorCol,
2197
+ { signal: controller.signal, force: options.force },
2198
+ );
2199
+
2200
+ if (!this.isAutocompleteRequestCurrent(requestId, controller, snapshotText, snapshotLine, snapshotCol)) {
2201
+ return;
2202
+ }
2203
+
2204
+ this.autocompleteAbort = undefined;
2205
+
2206
+ if (!suggestions || !Array.isArray(suggestions.items) || suggestions.items.length === 0) {
2207
+ this.cancelAutocomplete();
2208
+ this.tui.requestRender();
2209
+ return;
2210
+ }
2211
+
2212
+ if (options.force && options.explicitTab && suggestions.items.length === 1) {
2213
+ const item = suggestions.items[0]!;
2214
+ this.pushUndoSnapshot();
2215
+ this.lastAction = null;
2216
+ const result = this.autocompleteProvider.applyCompletion(
2217
+ this.state.lines,
2218
+ this.state.cursorLine,
2219
+ this.state.cursorCol,
2220
+ item,
2221
+ suggestions.prefix,
2222
+ );
2223
+ this.state.lines = result.lines;
2224
+ this.state.cursorLine = result.cursorLine;
2225
+ this.setCursorCol(result.cursorCol);
2226
+ if (this.onChange) this.onChange(this.getText());
2227
+ this.tui.requestRender();
2228
+ return;
2229
+ }
2230
+
2231
+ this.applyAutocompleteSuggestions(suggestions, options.force ? "force" : "regular");
2232
+ this.tui.requestRender();
2233
+ }
2234
+
2235
+ private isAutocompleteRequestCurrent(
2236
+ requestId: number,
2237
+ controller: AbortController,
2238
+ snapshotText: string,
2239
+ snapshotLine: number,
2240
+ snapshotCol: number,
2241
+ ): boolean {
2242
+ return (
2243
+ !controller.signal.aborted &&
2244
+ requestId === this.autocompleteRequestId &&
2245
+ this.getText() === snapshotText &&
2246
+ this.state.cursorLine === snapshotLine &&
2247
+ this.state.cursorCol === snapshotCol
2248
+ );
2249
+ }
2250
+
2251
+ private applyAutocompleteSuggestions(suggestions: AutocompleteSuggestions, state: "regular" | "force"): void {
2252
+ this.autocompletePrefix = suggestions.prefix;
2253
+ this.autocompleteList = this.createAutocompleteList(suggestions.prefix, suggestions.items);
2254
+
2255
+ const bestMatchIndex = this.getBestAutocompleteMatchIndex(suggestions.items, suggestions.prefix);
2256
+ if (bestMatchIndex >= 0) {
2257
+ this.autocompleteList.setSelectedIndex(bestMatchIndex);
2258
+ }
2259
+
2260
+ this.autocompleteState = state;
2261
+ }
2262
+
2263
+ private cancelAutocompleteRequest(): void {
2264
+ this.autocompleteStartToken += 1;
2265
+ if (this.autocompleteDebounceTimer) {
2266
+ clearTimeout(this.autocompleteDebounceTimer);
2267
+ this.autocompleteDebounceTimer = undefined;
2268
+ }
2269
+ this.autocompleteAbort?.abort();
2270
+ this.autocompleteAbort = undefined;
2271
+ }
2272
+
2273
+ private clearAutocompleteUi(): void {
2274
+ this.autocompleteState = null;
2275
+ this.autocompleteList = undefined;
2276
+ this.autocompletePrefix = "";
2277
+ }
2278
+
2279
+ private cancelAutocomplete(): void {
2280
+ this.cancelAutocompleteRequest();
2281
+ this.clearAutocompleteUi();
2282
+ }
2283
+
2284
+ public isShowingAutocomplete(): boolean {
2285
+ return this.autocompleteState !== null;
2286
+ }
2287
+
2288
+ private updateAutocomplete(): void {
2289
+ if (!this.autocompleteState || !this.autocompleteProvider) return;
2290
+ this.requestAutocomplete({ force: this.autocompleteState === "force", explicitTab: false });
2291
+ }
2292
+ }