@ashx-j/lunr-tui 0.2.26 → 0.2.28

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.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Mario Zechner
4
+ Copyright (c) 2026 ashx-j
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # @earendil-works/pi-tui
1
+ # @ashx-j/lunr-tui
2
2
 
3
3
  Minimal terminal UI framework with differential rendering and synchronized output for flicker-free interactive CLI applications.
4
4
 
@@ -16,7 +16,7 @@ Minimal terminal UI framework with differential rendering and synchronized outpu
16
16
  ## Quick Start
17
17
 
18
18
  ```typescript
19
- import { TUI, Text, Editor, ProcessTerminal, matchesKey } from "@earendil-works/pi-tui";
19
+ import { TUI, Text, Editor, ProcessTerminal, matchesKey } from "@ashx-j/lunr-tui";
20
20
 
21
21
  // Create terminal
22
22
  const terminal = new ProcessTerminal();
@@ -173,7 +173,7 @@ The TUI appends a full SGR reset and OSC 8 reset at the end of each rendered lin
173
173
  Components that display a text cursor and need IME (Input Method Editor) support should implement the `Focusable` interface:
174
174
 
175
175
  ```typescript
176
- import { CURSOR_MARKER, type Component, type Focusable } from "@earendil-works/pi-tui";
176
+ import { CURSOR_MARKER, type Component, type Focusable } from "@ashx-j/lunr-tui";
177
177
 
178
178
  class MyInput implements Component, Focusable {
179
179
  focused: boolean = false; // Set by TUI when focus changes
@@ -197,7 +197,7 @@ The cursor remains hidden by default. This keeps the fake cursor rendering, whil
197
197
  **Container components with embedded inputs:** When a container component (dialog, selector, etc.) contains an `Input` or `Editor` child, the container must implement `Focusable` and propagate the focus state to the child:
198
198
 
199
199
  ```typescript
200
- import { Container, type Focusable, Input } from "@earendil-works/pi-tui";
200
+ import { Container, type Focusable, Input } from "@ashx-j/lunr-tui";
201
201
 
202
202
  class SearchDialog extends Container implements Focusable {
203
203
  private searchInput: Input;
@@ -544,7 +544,7 @@ Supported formats: PNG, JPEG, GIF, WebP. Dimensions are parsed from the image he
544
544
  Supports both slash commands and file paths.
545
545
 
546
546
  ```typescript
547
- import { CombinedAutocompleteProvider } from "@earendil-works/pi-tui";
547
+ import { CombinedAutocompleteProvider } from "@ashx-j/lunr-tui";
548
548
 
549
549
  const provider = new CombinedAutocompleteProvider(
550
550
  [
@@ -569,7 +569,7 @@ editor.setAutocompleteProvider(provider);
569
569
  Use `matchesKey()` with the `Key` helper for detecting keyboard input (supports Kitty keyboard protocol):
570
570
 
571
571
  ```typescript
572
- import { matchesKey, Key } from "@earendil-works/pi-tui";
572
+ import { matchesKey, Key } from "@ashx-j/lunr-tui";
573
573
 
574
574
  if (matchesKey(data, Key.ctrl("c"))) {
575
575
  process.exit(0);
@@ -627,7 +627,7 @@ interface Terminal {
627
627
  ## Utilities
628
628
 
629
629
  ```typescript
630
- import { visibleWidth, truncateToWidth, wrapTextWithAnsi } from "@earendil-works/pi-tui";
630
+ import { visibleWidth, truncateToWidth, wrapTextWithAnsi } from "@ashx-j/lunr-tui";
631
631
 
632
632
  // Get visible width of string (ignoring ANSI codes)
633
633
  const width = visibleWidth("\x1b[31mHello\x1b[0m"); // 5
@@ -652,8 +652,8 @@ When creating custom components, **each line returned by `render()` must not exc
652
652
  Use `matchesKey()` with the `Key` helper for keyboard input:
653
653
 
654
654
  ```typescript
655
- import { matchesKey, Key, truncateToWidth } from "@earendil-works/pi-tui";
656
- import type { Component } from "@earendil-works/pi-tui";
655
+ import { matchesKey, Key, truncateToWidth } from "@ashx-j/lunr-tui";
656
+ import type { Component } from "@ashx-j/lunr-tui";
657
657
 
658
658
  class MyInteractiveComponent implements Component {
659
659
  private selectedIndex = 0;
@@ -688,8 +688,8 @@ class MyInteractiveComponent implements Component {
688
688
  Use the provided utilities to ensure lines fit:
689
689
 
690
690
  ```typescript
691
- import { visibleWidth, truncateToWidth } from "@earendil-works/pi-tui";
692
- import type { Component } from "@earendil-works/pi-tui";
691
+ import { visibleWidth, truncateToWidth } from "@ashx-j/lunr-tui";
692
+ import type { Component } from "@ashx-j/lunr-tui";
693
693
 
694
694
  class MyComponent implements Component {
695
695
  private text: string;
@@ -12,7 +12,8 @@ export declare function formatImageMarker(id: number): string;
12
12
  export declare function collectImageMarkerIds(text: string): number[];
13
13
  /**
14
14
  * Represents a chunk of text for word-wrap layout.
15
- * Tracks both the text content and its position in the original line.
15
+ * Tracks display text and its position in the original line. An indivisible
16
+ * grapheme that cannot fit uses a placeholder with the original source range.
16
17
  */
17
18
  export interface TextChunk {
18
19
  text: string;
@@ -1 +1 @@
1
- {"version":3,"file":"editor.d.ts","sourceRoot":"","sources":["../../src/components/editor.ts"],"names":[],"mappings":"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","sourcesContent":["import type { AutocompleteProvider, AutocompleteSuggestions } from \"../autocomplete.ts\";\nimport { getKeybindings } from \"../keybindings.ts\";\nimport { decodePrintableKey, matchesKey } from \"../keys.ts\";\nimport { KillRing } from \"../kill-ring.ts\";\nimport { type Component, CURSOR_MARKER, type Focusable, type TUI } from \"../tui.ts\";\nimport { UndoStack } from \"../undo-stack.ts\";\nimport {\n\tcjkBreakRegex,\n\tgetGraphemeSegmenter,\n\tgetWordSegmenter,\n\tisWhitespaceChar,\n\ttruncateToWidth,\n\tvisibleWidth,\n} from \"../utils.ts\";\nimport { findWordBackward, findWordForward } from \"../word-navigation.ts\";\nimport { SelectList, type SelectListLayoutOptions, type SelectListTheme } from \"./select-list.ts\";\n\nconst graphemeSegmenter = getGraphemeSegmenter();\nconst wordSegmenter = getWordSegmenter();\n\n/** Regex matching paste markers like `[paste #1 +123 lines]` or `[paste #2 1234 chars]`. */\nconst PASTE_MARKER_REGEX = /\\[paste #(\\d+)( (\\+\\d+ lines|\\d+ chars))?\\]/g;\n\n/** Non-global version for single-segment testing. */\nconst PASTE_MARKER_SINGLE = /^\\[paste #(\\d+)( (\\+\\d+ lines|\\d+ chars))?\\]$/;\n\n/** Regex matching clipboard image chips like `[image_1]`. */\nexport const IMAGE_MARKER_REGEX = /\\[image_(\\d+)\\]/g;\n\n/** Non-global version for single-segment testing. */\nconst IMAGE_MARKER_SINGLE = /^\\[image_(\\d+)\\]$/;\n\nexport type EditorImageAttachment = {\n\tid: number;\n\tpath: string;\n\tmimeType: string;\n};\n\nexport function formatImageMarker(id: number): string {\n\treturn `[image_${id}]`;\n}\n\nexport function collectImageMarkerIds(text: string): number[] {\n\tconst ids: number[] = [];\n\tconst seen = new Set<number>();\n\tfor (const match of text.matchAll(IMAGE_MARKER_REGEX)) {\n\t\tconst id = Number.parseInt(match[1]!, 10);\n\t\tif (seen.has(id)) continue;\n\t\tseen.add(id);\n\t\tids.push(id);\n\t}\n\treturn ids;\n}\n\n/** Check if a segment is a paste marker (i.e. was merged by segmentWithMarkers). */\nfunction isPasteMarker(segment: string): boolean {\n\treturn segment.length >= 10 && PASTE_MARKER_SINGLE.test(segment);\n}\n\nfunction isImageMarker(segment: string): boolean {\n\treturn segment.length >= 9 && IMAGE_MARKER_SINGLE.test(segment);\n}\n\nfunction isAtomicMarker(segment: string): boolean {\n\treturn isPasteMarker(segment) || isImageMarker(segment);\n}\n\ntype MarkerFamily = {\n\tregex: RegExp;\n\tvalidIds: Set<number>;\n\tneedle: string;\n};\n\n/**\n * A segmenter that wraps Intl.Segmenter and merges graphemes that fall\n * within paste/image markers into single atomic segments. This makes cursor\n * movement, deletion, word-wrap, etc. treat those markers as single units.\n *\n * Only markers whose numeric ID exists in a family's `validIds` are merged.\n */\nfunction segmentWithMarkers(\n\ttext: string,\n\tbaseSegmenter: Intl.Segmenter,\n\tfamilies: MarkerFamily[],\n): Iterable<Intl.SegmentData> {\n\tconst markers: Array<{ start: number; end: number }> = [];\n\tfor (const family of families) {\n\t\tif (family.validIds.size === 0 || !text.includes(family.needle)) continue;\n\t\tfor (const m of text.matchAll(family.regex)) {\n\t\t\tconst id = Number.parseInt(m[1]!, 10);\n\t\t\tif (!family.validIds.has(id)) continue;\n\t\t\tmarkers.push({ start: m.index, end: m.index + m[0].length });\n\t\t}\n\t}\n\tif (markers.length === 0) {\n\t\treturn baseSegmenter.segment(text);\n\t}\n\tmarkers.sort((a, b) => a.start - b.start || a.end - b.end);\n\n\t// Build merged segment list.\n\tconst baseSegments = baseSegmenter.segment(text);\n\tconst result: Intl.SegmentData[] = [];\n\tlet markerIdx = 0;\n\n\tfor (const seg of baseSegments) {\n\t\t// Skip past markers that are entirely before this segment.\n\t\twhile (markerIdx < markers.length && markers[markerIdx]!.end <= seg.index) {\n\t\t\tmarkerIdx++;\n\t\t}\n\n\t\tconst marker = markerIdx < markers.length ? markers[markerIdx]! : null;\n\n\t\tif (marker && seg.index >= marker.start && seg.index < marker.end) {\n\t\t\t// This segment falls inside a marker.\n\t\t\t// If this is the first segment of the marker, emit a merged segment.\n\t\t\tif (seg.index === marker.start) {\n\t\t\t\tconst markerText = text.slice(marker.start, marker.end);\n\t\t\t\tresult.push({\n\t\t\t\t\tsegment: markerText,\n\t\t\t\t\tindex: marker.start,\n\t\t\t\t\tinput: text,\n\t\t\t\t});\n\t\t\t}\n\t\t\t// Otherwise skip (already merged into the first segment).\n\t\t} else {\n\t\t\tresult.push(seg);\n\t\t}\n\t}\n\n\treturn result;\n}\n\n/**\n * Represents a chunk of text for word-wrap layout.\n * Tracks both the text content and its position in the original line.\n */\nexport interface TextChunk {\n\ttext: string;\n\tstartIndex: number;\n\tendIndex: number;\n}\n\n/**\n * Split a line into word-wrapped chunks.\n * Wraps at word boundaries when possible, falling back to character-level\n * wrapping for words longer than the available width.\n *\n * @param line - The text line to wrap\n * @param maxWidth - Maximum visible width per chunk\n * @param preSegmented - Optional pre-segmented graphemes (e.g. with paste-marker awareness).\n * When omitted the default Intl.Segmenter is used.\n * @returns Array of chunks with text and position information\n */\nexport function wordWrapLine(line: string, maxWidth: number, preSegmented?: Intl.SegmentData[]): TextChunk[] {\n\tif (!line || maxWidth <= 0) {\n\t\treturn [{ text: \"\", startIndex: 0, endIndex: 0 }];\n\t}\n\n\tconst lineWidth = visibleWidth(line);\n\tif (lineWidth <= maxWidth) {\n\t\treturn [{ text: line, startIndex: 0, endIndex: line.length }];\n\t}\n\n\tconst chunks: TextChunk[] = [];\n\tconst segments = preSegmented ?? [...graphemeSegmenter.segment(line)];\n\n\tlet currentWidth = 0;\n\tlet chunkStart = 0;\n\n\t// Wrap opportunity: the position after the last whitespace before a non-whitespace\n\t// grapheme, i.e. where a line break is allowed.\n\tlet wrapOppIndex = -1;\n\tlet wrapOppWidth = 0;\n\n\tfor (let i = 0; i < segments.length; i++) {\n\t\tconst seg = segments[i]!;\n\t\tconst grapheme = seg.segment;\n\t\tconst gWidth = visibleWidth(grapheme);\n\t\tconst charIndex = seg.index;\n\t\tconst isWs = !isAtomicMarker(grapheme) && isWhitespaceChar(grapheme);\n\n\t\t// Overflow check before advancing.\n\t\tif (currentWidth + gWidth > maxWidth) {\n\t\t\tif (wrapOppIndex >= 0 && currentWidth - wrapOppWidth + gWidth <= maxWidth) {\n\t\t\t\t// Backtrack to last wrap opportunity (the remaining content\n\t\t\t\t// plus the current grapheme still fits within maxWidth).\n\t\t\t\tchunks.push({ text: line.slice(chunkStart, wrapOppIndex), startIndex: chunkStart, endIndex: wrapOppIndex });\n\t\t\t\tchunkStart = wrapOppIndex;\n\t\t\t\tcurrentWidth -= wrapOppWidth;\n\t\t\t} else if (chunkStart < charIndex) {\n\t\t\t\t// No viable wrap opportunity: force-break at current position.\n\t\t\t\t// This also handles the case where backtracking to a word\n\t\t\t\t// boundary wouldn't help because the remaining content plus\n\t\t\t\t// the current grapheme (e.g. a wide character) still exceeds\n\t\t\t\t// maxWidth.\n\t\t\t\tchunks.push({ text: line.slice(chunkStart, charIndex), startIndex: chunkStart, endIndex: charIndex });\n\t\t\t\tchunkStart = charIndex;\n\t\t\t\tcurrentWidth = 0;\n\t\t\t}\n\t\t\twrapOppIndex = -1;\n\t\t}\n\n\t\tif (gWidth > maxWidth) {\n\t\t\t// Single atomic segment wider than maxWidth (e.g. paste marker\n\t\t\t// in a narrow terminal). Re-wrap it at grapheme granularity.\n\n\t\t\t// The segment remains logically atomic for cursor\n\t\t\t// movement / editing — the split is purely visual for word-wrap layout.\n\t\t\tconst subChunks = wordWrapLine(grapheme, maxWidth);\n\t\t\tfor (let j = 0; j < subChunks.length - 1; j++) {\n\t\t\t\tconst sc = subChunks[j]!;\n\t\t\t\tchunks.push({ text: sc.text, startIndex: charIndex + sc.startIndex, endIndex: charIndex + sc.endIndex });\n\t\t\t}\n\t\t\tconst last = subChunks[subChunks.length - 1]!;\n\t\t\tchunkStart = charIndex + last.startIndex;\n\t\t\tcurrentWidth = visibleWidth(last.text);\n\t\t\twrapOppIndex = -1;\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Advance.\n\t\tcurrentWidth += gWidth;\n\n\t\t// Record wrap opportunity: whitespace followed by non-whitespace\n\t\t// (multiple spaces join; the break point is after the last space),\n\t\t// or at a boundary where either side is CJK (CJK allows breaking\n\t\t// between any adjacent characters).\n\t\tconst next = segments[i + 1];\n\t\tif (isWs && next && (isAtomicMarker(next.segment) || !isWhitespaceChar(next.segment))) {\n\t\t\twrapOppIndex = next.index;\n\t\t\twrapOppWidth = currentWidth;\n\t\t} else if (!isWs && next && !isWhitespaceChar(next.segment)) {\n\t\t\tconst isCjk = !isAtomicMarker(grapheme) && cjkBreakRegex.test(grapheme);\n\t\t\tconst nextIsCjk = !isAtomicMarker(next.segment) && cjkBreakRegex.test(next.segment);\n\t\t\tif (isCjk || nextIsCjk) {\n\t\t\t\twrapOppIndex = next.index;\n\t\t\t\twrapOppWidth = currentWidth;\n\t\t\t}\n\t\t}\n\t}\n\n\t// Push final chunk.\n\tchunks.push({ text: line.slice(chunkStart), startIndex: chunkStart, endIndex: line.length });\n\n\treturn chunks;\n}\n\n// Kitty CSI-u sequences for printable keys, including optional shifted/base codepoints.\ninterface EditorState {\n\tlines: string[];\n\tcursorLine: number;\n\tcursorCol: number;\n}\n\ninterface LayoutLine {\n\ttext: string;\n\thasCursor: boolean;\n\tcursorPos?: number;\n}\n\nexport interface EditorTheme {\n\tborderColor: (str: string) => string;\n\tselectList: SelectListTheme;\n}\n\nexport interface EditorOptions {\n\tpaddingX?: number;\n\tautocompleteMaxVisible?: number;\n}\n\nconst SLASH_COMMAND_SELECT_LIST_LAYOUT: SelectListLayoutOptions = {\n\tminPrimaryColumnWidth: 12,\n\tmaxPrimaryColumnWidth: 32,\n};\n\nconst ATTACHMENT_AUTOCOMPLETE_DEBOUNCE_MS = 20;\nconst DEFAULT_AUTOCOMPLETE_TRIGGER_CHARACTERS = [\"@\", \"#\"];\n\nfunction escapeCharacterClass(value: string): string {\n\treturn value.replace(/[\\\\^$.*+?()[\\]{}|-]/g, \"\\\\$&\");\n}\n\nfunction buildTriggerPattern(triggerCharacters: string[]): RegExp {\n\treturn new RegExp(`(?:^|[\\\\s])[${triggerCharacters.map(escapeCharacterClass).join(\"\")}][^\\\\s]*$`);\n}\n\nfunction buildDebouncePattern(triggerCharacters: string[]): RegExp {\n\tconst escapedWithoutAt = triggerCharacters.filter((character) => character !== \"@\").map(escapeCharacterClass);\n\treturn new RegExp(`(?:^|[ \\\\t])(?:@(?:\"[^\"]*|[^\\\\s]*)|[${escapedWithoutAt.join(\"\")}][^\\\\s]*)$`);\n}\n\nexport class Editor implements Component, Focusable {\n\tprivate state: EditorState = {\n\t\tlines: [\"\"],\n\t\tcursorLine: 0,\n\t\tcursorCol: 0,\n\t};\n\n\t/** Focusable interface - set by TUI when focus changes */\n\tfocused: boolean = false;\n\n\tprotected tui: TUI;\n\tprivate theme: EditorTheme;\n\tprivate paddingX: number = 0;\n\n\t// Store last render width for cursor navigation\n\tprivate lastWidth: number = 80;\n\n\t// Vertical scrolling support\n\tprivate scrollOffset: number = 0;\n\n\t// Border color (can be changed dynamically)\n\tpublic borderColor: (str: string) => string;\n\n\t// Autocomplete support\n\tprivate autocompleteProvider?: AutocompleteProvider;\n\tprivate autocompleteTriggerCharacters = [...DEFAULT_AUTOCOMPLETE_TRIGGER_CHARACTERS];\n\tprivate autocompleteTriggerPattern = buildTriggerPattern(this.autocompleteTriggerCharacters);\n\tprivate autocompleteDebouncePattern = buildDebouncePattern(this.autocompleteTriggerCharacters);\n\tprivate autocompleteList?: SelectList;\n\tprivate autocompleteState: \"regular\" | \"force\" | null = null;\n\tprivate autocompletePrefix: string = \"\";\n\tprivate autocompleteMaxVisible: number = 5;\n\tprivate autocompleteAbort?: AbortController;\n\tprivate autocompleteDebounceTimer?: ReturnType<typeof setTimeout>;\n\tprivate autocompleteRequestTask: Promise<void> = Promise.resolve();\n\tprivate autocompleteStartToken: number = 0;\n\tprivate autocompleteRequestId: number = 0;\n\n\t// Paste tracking for large pastes\n\tprivate pastes: Map<number, string> = new Map();\n\tprivate pasteCounter: number = 0;\n\n\t// Clipboard image chips: `[image_n]` stays in the editor; bytes live off-screen.\n\tprivate images: Map<number, EditorImageAttachment> = new Map();\n\tprivate imageCounter: number = 0;\n\tprivate submittedImages: EditorImageAttachment[] = [];\n\n\t// Bracketed paste mode buffering\n\tprivate pasteBuffer: string = \"\";\n\tprivate isInPaste: boolean = false;\n\n\t// Prompt history for up/down navigation\n\tprivate history: string[] = [];\n\tprivate historyIndex: number = -1; // -1 = not browsing, 0 = most recent, 1 = older, etc.\n\tprivate historyDraft: EditorState | null = null;\n\n\t// Kill ring for Emacs-style kill/yank operations\n\tprivate killRing = new KillRing();\n\tprivate lastAction: \"kill\" | \"yank\" | \"type-word\" | null = null;\n\n\t// Character jump mode\n\tprivate jumpMode: \"forward\" | \"backward\" | null = null;\n\n\t// Preferred visual column for vertical cursor movement (sticky column)\n\tprivate preferredVisualCol: number | null = null;\n\n\t// When the cursor is snapped to the start of an atomic segment, e.g. a\n\t// paste marker, cursorCol no longer reflects where the cursor would have\n\t// landed. This field stores the pre-snap cursorCol so that the next\n\t// vertical move can resolve it to a visual column on whatever VL it belongs\n\t// to.\n\tprivate snappedFromCursorCol: number | null = null;\n\n\t// Undo support\n\tprivate undoStack = new UndoStack<EditorState>();\n\n\tpublic onSubmit?: (text: string) => void;\n\tpublic onChange?: (text: string) => void;\n\tpublic disableSubmit: boolean = false;\n\n\tconstructor(tui: TUI, theme: EditorTheme, options: EditorOptions = {}) {\n\t\tthis.tui = tui;\n\t\tthis.theme = theme;\n\t\tthis.borderColor = theme.borderColor;\n\t\tconst paddingX = options.paddingX ?? 0;\n\t\tthis.paddingX = Number.isFinite(paddingX) ? Math.max(0, Math.floor(paddingX)) : 0;\n\t\tconst maxVisible = options.autocompleteMaxVisible ?? 5;\n\t\tthis.autocompleteMaxVisible = Number.isFinite(maxVisible) ? Math.max(3, Math.min(20, Math.floor(maxVisible))) : 5;\n\t}\n\n\t/** Set of currently valid paste IDs, for marker-aware segmentation. */\n\tprivate validPasteIds(): Set<number> {\n\t\treturn new Set(this.pastes.keys());\n\t}\n\n\t/** Set of currently valid image IDs, for marker-aware segmentation. */\n\tprivate validImageIds(): Set<number> {\n\t\treturn new Set(this.images.keys());\n\t}\n\n\tprivate markerFamilies(): MarkerFamily[] {\n\t\treturn [\n\t\t\t{ regex: PASTE_MARKER_REGEX, validIds: this.validPasteIds(), needle: \"[paste #\" },\n\t\t\t{ regex: IMAGE_MARKER_REGEX, validIds: this.validImageIds(), needle: \"[image_\" },\n\t\t];\n\t}\n\n\t/** Segment text with paste/image-marker awareness, only merging markers with valid IDs. */\n\tprivate segment(text: string, mode: \"word\" | \"grapheme\"): Iterable<Intl.SegmentData> {\n\t\treturn segmentWithMarkers(text, mode === \"word\" ? wordSegmenter : graphemeSegmenter, this.markerFamilies());\n\t}\n\n\tgetPaddingX(): number {\n\t\treturn this.paddingX;\n\t}\n\n\tsetPaddingX(padding: number): void {\n\t\tconst newPadding = Number.isFinite(padding) ? Math.max(0, Math.floor(padding)) : 0;\n\t\tif (this.paddingX !== newPadding) {\n\t\t\tthis.paddingX = newPadding;\n\t\t\tthis.tui.requestRender();\n\t\t}\n\t}\n\n\tgetAutocompleteMaxVisible(): number {\n\t\treturn this.autocompleteMaxVisible;\n\t}\n\n\tsetAutocompleteMaxVisible(maxVisible: number): void {\n\t\tconst newMaxVisible = Number.isFinite(maxVisible) ? Math.max(3, Math.min(20, Math.floor(maxVisible))) : 5;\n\t\tif (this.autocompleteMaxVisible !== newMaxVisible) {\n\t\t\tthis.autocompleteMaxVisible = newMaxVisible;\n\t\t\tthis.tui.requestRender();\n\t\t}\n\t}\n\n\tsetAutocompleteProvider(provider: AutocompleteProvider): void {\n\t\tthis.cancelAutocomplete();\n\t\tthis.autocompleteProvider = provider;\n\t\tthis.setAutocompleteTriggerCharacters(provider.triggerCharacters ?? []);\n\t}\n\n\t/**\n\t * Add a prompt to history for up/down arrow navigation.\n\t * Called after successful submission.\n\t */\n\taddToHistory(text: string): void {\n\t\tconst trimmed = text.trim();\n\t\tif (!trimmed) return;\n\t\t// Don't add consecutive duplicates\n\t\tif (this.history.length > 0 && this.history[0] === trimmed) return;\n\t\tthis.history.unshift(trimmed);\n\t\t// Limit history size\n\t\tif (this.history.length > 100) {\n\t\t\tthis.history.pop();\n\t\t}\n\t}\n\n\tprivate isEditorEmpty(): boolean {\n\t\treturn this.state.lines.length === 1 && this.state.lines[0] === \"\";\n\t}\n\n\tprivate isOnFirstVisualLine(): boolean {\n\t\tconst visualLines = this.buildVisualLineMap(this.lastWidth);\n\t\tconst currentVisualLine = this.findCurrentVisualLine(visualLines);\n\t\treturn currentVisualLine === 0;\n\t}\n\n\tprivate isOnLastVisualLine(): boolean {\n\t\tconst visualLines = this.buildVisualLineMap(this.lastWidth);\n\t\tconst currentVisualLine = this.findCurrentVisualLine(visualLines);\n\t\treturn currentVisualLine === visualLines.length - 1;\n\t}\n\n\tprivate navigateHistory(direction: 1 | -1): void {\n\t\tthis.lastAction = null;\n\t\tif (this.history.length === 0) return;\n\n\t\tconst newIndex = this.historyIndex - direction; // Up(-1) increases index, Down(1) decreases\n\t\tif (newIndex < -1 || newIndex >= this.history.length) return;\n\n\t\t// Capture state when first entering history browsing mode\n\t\tif (this.historyIndex === -1 && newIndex >= 0) {\n\t\t\tthis.pushUndoSnapshot();\n\t\t\tthis.historyDraft = structuredClone(this.state);\n\t\t}\n\n\t\tthis.historyIndex = newIndex;\n\n\t\tif (this.historyIndex === -1) {\n\t\t\tconst draft = this.historyDraft;\n\t\t\tthis.historyDraft = null;\n\t\t\tif (draft) {\n\t\t\t\tthis.state = draft;\n\t\t\t\tthis.preferredVisualCol = null;\n\t\t\t\tthis.snappedFromCursorCol = null;\n\t\t\t\tthis.scrollOffset = 0;\n\t\t\t\tif (this.onChange) this.onChange(this.getText());\n\t\t\t} else {\n\t\t\t\tthis.setTextInternal(\"\");\n\t\t\t}\n\t\t} else {\n\t\t\tthis.setTextInternal(this.history[this.historyIndex] || \"\", direction === -1 ? \"start\" : \"end\");\n\t\t}\n\t}\n\n\tprivate exitHistoryBrowsing(): void {\n\t\tthis.historyIndex = -1;\n\t\tthis.historyDraft = null;\n\t}\n\n\t/** Internal setText that doesn't reset history state - used by navigateHistory */\n\tprivate setTextInternal(text: string, cursorPlacement: \"start\" | \"end\" = \"end\"): void {\n\t\tconst lines = text.split(\"\\n\");\n\t\tthis.state.lines = lines.length === 0 ? [\"\"] : lines;\n\t\tthis.state.cursorLine = cursorPlacement === \"start\" ? 0 : this.state.lines.length - 1;\n\t\tthis.setCursorCol(cursorPlacement === \"start\" ? 0 : this.state.lines[this.state.cursorLine]?.length || 0);\n\t\t// Reset scroll - render() will adjust to show cursor\n\t\tthis.scrollOffset = 0;\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tinvalidate(): void {\n\t\t// No cached state to invalidate currently\n\t}\n\n\trender(width: number): string[] {\n\t\tconst maxPadding = Math.max(0, Math.floor((width - 1) / 2));\n\t\tconst paddingX = Math.min(this.paddingX, maxPadding);\n\t\tconst contentWidth = Math.max(1, width - paddingX * 2);\n\n\t\t// Layout width: with padding the cursor can overflow into it,\n\t\t// without padding we reserve 1 column for the cursor.\n\t\tconst layoutWidth = Math.max(1, contentWidth - (paddingX ? 0 : 1));\n\n\t\t// Store for cursor navigation (must match wrapping width)\n\t\tthis.lastWidth = layoutWidth;\n\n\t\tconst horizontal = this.borderColor(\"─\");\n\n\t\t// Layout the text\n\t\tconst layoutLines = this.layoutText(layoutWidth);\n\n\t\t// Calculate max visible lines: 30% of terminal height, minimum 5 lines\n\t\tconst terminalRows = this.tui.terminal.rows;\n\t\tconst maxVisibleLines = Math.max(5, Math.floor(terminalRows * 0.3));\n\n\t\t// Find the cursor line index in layoutLines\n\t\tlet cursorLineIndex = layoutLines.findIndex((line) => line.hasCursor);\n\t\tif (cursorLineIndex === -1) cursorLineIndex = 0;\n\n\t\t// Adjust scroll offset to keep cursor visible\n\t\tif (cursorLineIndex < this.scrollOffset) {\n\t\t\tthis.scrollOffset = cursorLineIndex;\n\t\t} else if (cursorLineIndex >= this.scrollOffset + maxVisibleLines) {\n\t\t\tthis.scrollOffset = cursorLineIndex - maxVisibleLines + 1;\n\t\t}\n\n\t\t// Clamp scroll offset to valid range\n\t\tconst maxScrollOffset = Math.max(0, layoutLines.length - maxVisibleLines);\n\t\tthis.scrollOffset = Math.max(0, Math.min(this.scrollOffset, maxScrollOffset));\n\n\t\t// Get visible lines slice\n\t\tconst visibleLines = layoutLines.slice(this.scrollOffset, this.scrollOffset + maxVisibleLines);\n\n\t\tconst result: string[] = [];\n\t\tconst leftPadding = \" \".repeat(paddingX);\n\t\tconst rightPadding = leftPadding;\n\n\t\t// Render top border (with scroll indicator if scrolled down)\n\t\tif (this.scrollOffset > 0) {\n\t\t\tconst indicator = `─── ↑ ${this.scrollOffset} more `;\n\t\t\tconst remaining = width - visibleWidth(indicator);\n\t\t\tif (remaining >= 0) {\n\t\t\t\tresult.push(this.borderColor(indicator + \"─\".repeat(remaining)));\n\t\t\t} else {\n\t\t\t\tresult.push(this.borderColor(truncateToWidth(indicator, width)));\n\t\t\t}\n\t\t} else {\n\t\t\tresult.push(horizontal.repeat(width));\n\t\t}\n\n\t\t// Render each visible layout line\n\t\t// Emit hardware cursor marker when focused so TUI can position the\n\t\t// hardware cursor for IME candidate-window placement even while\n\t\t// autocomplete (e.g. slash-command menu) is visible.\n\t\tconst emitCursorMarker = this.focused;\n\n\t\tfor (const layoutLine of visibleLines) {\n\t\t\tlet displayText = layoutLine.text;\n\t\t\tlet lineVisibleWidth = visibleWidth(layoutLine.text);\n\t\t\tlet cursorInPadding = false;\n\n\t\t\t// Add cursor if this line has it\n\t\t\tif (layoutLine.hasCursor && layoutLine.cursorPos !== undefined) {\n\t\t\t\tconst before = displayText.slice(0, layoutLine.cursorPos);\n\t\t\t\tconst after = displayText.slice(layoutLine.cursorPos);\n\n\t\t\t\t// Hardware cursor marker (zero-width, emitted before fake cursor for IME positioning)\n\t\t\t\tconst marker = emitCursorMarker ? CURSOR_MARKER : \"\";\n\n\t\t\t\tif (after.length > 0) {\n\t\t\t\t\t// Cursor is on a character (grapheme) - replace it with highlighted version\n\t\t\t\t\t// Get the first grapheme from 'after'\n\t\t\t\t\tconst afterGraphemes = [...this.segment(after, \"grapheme\")];\n\t\t\t\t\tconst firstGrapheme = afterGraphemes[0]?.segment || \"\";\n\t\t\t\t\tconst restAfter = after.slice(firstGrapheme.length);\n\t\t\t\t\tconst cursor = `\\x1b[7m${firstGrapheme}\\x1b[0m`;\n\t\t\t\t\tdisplayText = before + marker + cursor + restAfter;\n\t\t\t\t\t// lineVisibleWidth stays the same - we're replacing, not adding\n\t\t\t\t} else {\n\t\t\t\t\t// Cursor is at the end - add highlighted space\n\t\t\t\t\tconst cursor = \"\\x1b[7m \\x1b[0m\";\n\t\t\t\t\tdisplayText = before + marker + cursor;\n\t\t\t\t\tlineVisibleWidth = lineVisibleWidth + 1;\n\t\t\t\t\t// If cursor overflows content width into the padding, flag it\n\t\t\t\t\tif (lineVisibleWidth > contentWidth && paddingX > 0) {\n\t\t\t\t\t\tcursorInPadding = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Calculate padding based on actual visible width\n\t\t\tconst padding = \" \".repeat(Math.max(0, contentWidth - lineVisibleWidth));\n\t\t\tconst lineRightPadding = cursorInPadding ? rightPadding.slice(1) : rightPadding;\n\n\t\t\t// Render the line (no side borders, just horizontal lines above and below)\n\t\t\tresult.push(`${leftPadding}${displayText}${padding}${lineRightPadding}`);\n\t\t}\n\n\t\t// Render bottom border (with scroll indicator if more content below)\n\t\tconst linesBelow = layoutLines.length - (this.scrollOffset + visibleLines.length);\n\t\tif (linesBelow > 0) {\n\t\t\tconst indicator = `─── ↓ ${linesBelow} more `;\n\t\t\tconst remaining = width - visibleWidth(indicator);\n\t\t\tresult.push(this.borderColor(indicator + \"─\".repeat(Math.max(0, remaining))));\n\t\t} else {\n\t\t\tresult.push(horizontal.repeat(width));\n\t\t}\n\n\t\t// Add autocomplete list if active\n\t\tif (this.autocompleteState && this.autocompleteList) {\n\t\t\tconst autocompleteResult = this.autocompleteList.render(contentWidth);\n\t\t\tfor (const line of autocompleteResult) {\n\t\t\t\tconst lineWidth = visibleWidth(line);\n\t\t\t\tconst linePadding = \" \".repeat(Math.max(0, contentWidth - lineWidth));\n\t\t\t\tresult.push(`${leftPadding}${line}${linePadding}${rightPadding}`);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\thandleInput(data: string): void {\n\t\tconst kb = getKeybindings();\n\n\t\t// Handle character jump mode (awaiting next character to jump to)\n\t\tif (this.jumpMode !== null) {\n\t\t\t// Cancel if the hotkey is pressed again\n\t\t\tif (kb.matches(data, \"tui.editor.jumpForward\") || kb.matches(data, \"tui.editor.jumpBackward\")) {\n\t\t\t\tthis.jumpMode = null;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst printable = decodePrintableKey(data) ?? (data.charCodeAt(0) >= 32 ? data : undefined);\n\t\t\tif (printable !== undefined) {\n\t\t\t\t// Printable character - perform the jump\n\t\t\t\tconst direction = this.jumpMode;\n\t\t\t\tthis.jumpMode = null;\n\t\t\t\tthis.jumpToChar(printable, direction);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Control character - cancel and fall through to normal handling\n\t\t\tthis.jumpMode = null;\n\t\t}\n\n\t\t// Handle bracketed paste mode\n\t\tif (data.includes(\"\\x1b[200~\")) {\n\t\t\tthis.isInPaste = true;\n\t\t\tthis.pasteBuffer = \"\";\n\t\t\tdata = data.replace(\"\\x1b[200~\", \"\");\n\t\t}\n\n\t\tif (this.isInPaste) {\n\t\t\tthis.pasteBuffer += data;\n\t\t\tconst endIndex = this.pasteBuffer.indexOf(\"\\x1b[201~\");\n\t\t\tif (endIndex !== -1) {\n\t\t\t\tconst pasteContent = this.pasteBuffer.substring(0, endIndex);\n\t\t\t\tif (pasteContent.length > 0) {\n\t\t\t\t\tthis.handlePaste(pasteContent);\n\t\t\t\t}\n\t\t\t\tthis.isInPaste = false;\n\t\t\t\tconst remaining = this.pasteBuffer.substring(endIndex + 6);\n\t\t\t\tthis.pasteBuffer = \"\";\n\t\t\t\tif (remaining.length > 0) {\n\t\t\t\t\tthis.handleInput(remaining);\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\t// Ctrl+C - let parent handle (exit/clear)\n\t\tif (kb.matches(data, \"tui.input.copy\")) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Undo\n\t\tif (kb.matches(data, \"tui.editor.undo\")) {\n\t\t\tthis.undo();\n\t\t\treturn;\n\t\t}\n\n\t\t// Handle autocomplete mode\n\t\tif (this.autocompleteState && this.autocompleteList) {\n\t\t\tif (kb.matches(data, \"tui.select.cancel\")) {\n\t\t\t\tthis.cancelAutocomplete();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (kb.matches(data, \"tui.select.up\") || kb.matches(data, \"tui.select.down\")) {\n\t\t\t\tthis.autocompleteList.handleInput(data);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (kb.matches(data, \"tui.input.tab\")) {\n\t\t\t\tconst selected = this.autocompleteList.getSelectedItem();\n\t\t\t\tif (selected && this.autocompleteProvider) {\n\t\t\t\t\tthis.pushUndoSnapshot();\n\t\t\t\t\tthis.lastAction = null;\n\t\t\t\t\tconst result = this.autocompleteProvider.applyCompletion(\n\t\t\t\t\t\tthis.state.lines,\n\t\t\t\t\t\tthis.state.cursorLine,\n\t\t\t\t\t\tthis.state.cursorCol,\n\t\t\t\t\t\tselected,\n\t\t\t\t\t\tthis.autocompletePrefix,\n\t\t\t\t\t);\n\t\t\t\t\tthis.state.lines = result.lines;\n\t\t\t\t\tthis.state.cursorLine = result.cursorLine;\n\t\t\t\t\tthis.setCursorCol(result.cursorCol);\n\t\t\t\t\tthis.cancelAutocomplete();\n\t\t\t\t\tif (this.onChange) this.onChange(this.getText());\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (kb.matches(data, \"tui.select.confirm\")) {\n\t\t\t\tconst selected = this.autocompleteList.getSelectedItem();\n\t\t\t\tif (selected && this.autocompleteProvider) {\n\t\t\t\t\tthis.pushUndoSnapshot();\n\t\t\t\t\tthis.lastAction = null;\n\t\t\t\t\tconst result = this.autocompleteProvider.applyCompletion(\n\t\t\t\t\t\tthis.state.lines,\n\t\t\t\t\t\tthis.state.cursorLine,\n\t\t\t\t\t\tthis.state.cursorCol,\n\t\t\t\t\t\tselected,\n\t\t\t\t\t\tthis.autocompletePrefix,\n\t\t\t\t\t);\n\t\t\t\t\tthis.state.lines = result.lines;\n\t\t\t\t\tthis.state.cursorLine = result.cursorLine;\n\t\t\t\t\tthis.setCursorCol(result.cursorCol);\n\n\t\t\t\t\tif (this.autocompletePrefix.startsWith(\"/\")) {\n\t\t\t\t\t\tthis.cancelAutocomplete();\n\t\t\t\t\t\t// Fall through to submit\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthis.cancelAutocomplete();\n\t\t\t\t\t\tif (this.onChange) this.onChange(this.getText());\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Tab - trigger completion\n\t\tif (kb.matches(data, \"tui.input.tab\") && !this.autocompleteState) {\n\t\t\tthis.handleTabCompletion();\n\t\t\treturn;\n\t\t}\n\n\t\t// Deletion actions\n\t\tif (kb.matches(data, \"tui.editor.deleteToLineEnd\")) {\n\t\t\tthis.deleteToEndOfLine();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.deleteToLineStart\")) {\n\t\t\tthis.deleteToStartOfLine();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.deleteWordBackward\")) {\n\t\t\tthis.deleteWordBackwards();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.deleteWordForward\")) {\n\t\t\tthis.deleteWordForward();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.deleteCharBackward\") || matchesKey(data, \"shift+backspace\")) {\n\t\t\tthis.handleBackspace();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.deleteCharForward\") || matchesKey(data, \"shift+delete\")) {\n\t\t\tthis.handleForwardDelete();\n\t\t\treturn;\n\t\t}\n\n\t\t// Kill ring actions\n\t\tif (kb.matches(data, \"tui.editor.yank\")) {\n\t\t\tthis.yank();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.yankPop\")) {\n\t\t\tthis.yankPop();\n\t\t\treturn;\n\t\t}\n\n\t\t// Cursor movement actions\n\t\tif (kb.matches(data, \"tui.editor.cursorLineStart\")) {\n\t\t\tthis.moveToLineStart();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.cursorLineEnd\")) {\n\t\t\tthis.moveToLineEnd();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.cursorWordLeft\")) {\n\t\t\tthis.moveWordBackwards();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.cursorWordRight\")) {\n\t\t\tthis.moveWordForwards();\n\t\t\treturn;\n\t\t}\n\n\t\t// New line\n\t\tif (\n\t\t\tkb.matches(data, \"tui.input.newLine\") ||\n\t\t\t(data.charCodeAt(0) === 10 && data.length > 1) ||\n\t\t\tdata === \"\\x1b\\r\" ||\n\t\t\tdata === \"\\x1b[13;2~\" ||\n\t\t\t(data.length > 1 && data.includes(\"\\x1b\") && data.includes(\"\\r\")) ||\n\t\t\t(data === \"\\n\" && data.length === 1)\n\t\t) {\n\t\t\tif (this.shouldSubmitOnBackslashEnter(data, kb)) {\n\t\t\t\tthis.handleBackspace();\n\t\t\t\tthis.submitValue();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tthis.addNewLine();\n\t\t\treturn;\n\t\t}\n\n\t\t// Submit (Enter)\n\t\tif (kb.matches(data, \"tui.input.submit\")) {\n\t\t\tif (this.disableSubmit) return;\n\n\t\t\t// Workaround for terminals without Shift+Enter support:\n\t\t\t// If char before cursor is \\, delete it and insert newline instead of submitting.\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tif (this.state.cursorCol > 0 && currentLine[this.state.cursorCol - 1] === \"\\\\\") {\n\t\t\t\tthis.handleBackspace();\n\t\t\t\tthis.addNewLine();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis.submitValue();\n\t\t\treturn;\n\t\t}\n\n\t\t// Arrow key navigation (with history support)\n\t\tif (kb.matches(data, \"tui.editor.cursorUp\")) {\n\t\t\tif (\n\t\t\t\tthis.isOnFirstVisualLine() &&\n\t\t\t\t(this.isEditorEmpty() || this.historyIndex > -1 || this.state.cursorCol === 0)\n\t\t\t) {\n\t\t\t\tthis.navigateHistory(-1);\n\t\t\t} else if (this.isOnFirstVisualLine()) {\n\t\t\t\t// Already at top - jump to start of line\n\t\t\t\tthis.moveToLineStart();\n\t\t\t} else {\n\t\t\t\tthis.moveCursor(-1, 0);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.cursorDown\")) {\n\t\t\tif (this.historyIndex > -1 && this.isOnLastVisualLine()) {\n\t\t\t\tthis.navigateHistory(1);\n\t\t\t} else if (this.isOnLastVisualLine()) {\n\t\t\t\t// Already at bottom - jump to end of line\n\t\t\t\tthis.moveToLineEnd();\n\t\t\t} else {\n\t\t\t\tthis.moveCursor(1, 0);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.cursorRight\")) {\n\t\t\tthis.moveCursor(0, 1);\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.cursorLeft\")) {\n\t\t\tthis.moveCursor(0, -1);\n\t\t\treturn;\n\t\t}\n\n\t\t// Page up/down - scroll by page and move cursor\n\t\tif (kb.matches(data, \"tui.editor.pageUp\")) {\n\t\t\tthis.pageScroll(-1);\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.pageDown\")) {\n\t\t\tthis.pageScroll(1);\n\t\t\treturn;\n\t\t}\n\n\t\t// Character jump mode triggers\n\t\tif (kb.matches(data, \"tui.editor.jumpForward\")) {\n\t\t\tthis.jumpMode = \"forward\";\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.jumpBackward\")) {\n\t\t\tthis.jumpMode = \"backward\";\n\t\t\treturn;\n\t\t}\n\n\t\t// Shift+Space - insert regular space\n\t\tif (matchesKey(data, \"shift+space\")) {\n\t\t\tthis.insertCharacter(\" \");\n\t\t\treturn;\n\t\t}\n\n\t\tconst printable = decodePrintableKey(data);\n\t\tif (printable !== undefined) {\n\t\t\tthis.insertCharacter(printable);\n\t\t\treturn;\n\t\t}\n\n\t\t// Regular characters\n\t\tif (data.charCodeAt(0) >= 32) {\n\t\t\tthis.insertCharacter(data);\n\t\t}\n\t}\n\n\tprivate layoutText(contentWidth: number): LayoutLine[] {\n\t\tconst layoutLines: LayoutLine[] = [];\n\n\t\tif (this.state.lines.length === 0 || (this.state.lines.length === 1 && this.state.lines[0] === \"\")) {\n\t\t\t// Empty editor\n\t\t\tlayoutLines.push({\n\t\t\t\ttext: \"\",\n\t\t\t\thasCursor: true,\n\t\t\t\tcursorPos: 0,\n\t\t\t});\n\t\t\treturn layoutLines;\n\t\t}\n\n\t\t// Process each logical line\n\t\tfor (let i = 0; i < this.state.lines.length; i++) {\n\t\t\tconst line = this.state.lines[i] || \"\";\n\t\t\tconst isCurrentLine = i === this.state.cursorLine;\n\t\t\tconst lineVisibleWidth = visibleWidth(line);\n\n\t\t\tif (lineVisibleWidth <= contentWidth) {\n\t\t\t\t// Line fits in one layout line\n\t\t\t\tif (isCurrentLine) {\n\t\t\t\t\tlayoutLines.push({\n\t\t\t\t\t\ttext: line,\n\t\t\t\t\t\thasCursor: true,\n\t\t\t\t\t\tcursorPos: this.state.cursorCol,\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tlayoutLines.push({\n\t\t\t\t\t\ttext: line,\n\t\t\t\t\t\thasCursor: false,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Line needs wrapping - use word-aware wrapping\n\t\t\t\tconst chunks = wordWrapLine(line, contentWidth, [...this.segment(line, \"grapheme\")]);\n\n\t\t\t\tfor (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {\n\t\t\t\t\tconst chunk = chunks[chunkIndex];\n\t\t\t\t\tif (!chunk) continue;\n\n\t\t\t\t\tconst cursorPos = this.state.cursorCol;\n\t\t\t\t\tconst isLastChunk = chunkIndex === chunks.length - 1;\n\n\t\t\t\t\t// Determine if cursor is in this chunk\n\t\t\t\t\t// For word-wrapped chunks, we need to handle the case where\n\t\t\t\t\t// cursor might be in trimmed whitespace at end of chunk\n\t\t\t\t\tlet hasCursorInChunk = false;\n\t\t\t\t\tlet adjustedCursorPos = 0;\n\n\t\t\t\t\tif (isCurrentLine) {\n\t\t\t\t\t\tif (isLastChunk) {\n\t\t\t\t\t\t\t// Last chunk: cursor belongs here if >= startIndex\n\t\t\t\t\t\t\thasCursorInChunk = cursorPos >= chunk.startIndex;\n\t\t\t\t\t\t\tadjustedCursorPos = cursorPos - chunk.startIndex;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Non-last chunk: cursor belongs here if in range [startIndex, endIndex)\n\t\t\t\t\t\t\t// But we need to handle the visual position in the trimmed text\n\t\t\t\t\t\t\thasCursorInChunk = cursorPos >= chunk.startIndex && cursorPos < chunk.endIndex;\n\t\t\t\t\t\t\tif (hasCursorInChunk) {\n\t\t\t\t\t\t\t\tadjustedCursorPos = cursorPos - chunk.startIndex;\n\t\t\t\t\t\t\t\t// Clamp to text length (in case cursor was in trimmed whitespace)\n\t\t\t\t\t\t\t\tif (adjustedCursorPos > chunk.text.length) {\n\t\t\t\t\t\t\t\t\tadjustedCursorPos = chunk.text.length;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (hasCursorInChunk) {\n\t\t\t\t\t\tlayoutLines.push({\n\t\t\t\t\t\t\ttext: chunk.text,\n\t\t\t\t\t\t\thasCursor: true,\n\t\t\t\t\t\t\tcursorPos: adjustedCursorPos,\n\t\t\t\t\t\t});\n\t\t\t\t\t} else {\n\t\t\t\t\t\tlayoutLines.push({\n\t\t\t\t\t\t\ttext: chunk.text,\n\t\t\t\t\t\t\thasCursor: false,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn layoutLines;\n\t}\n\n\tgetText(): string {\n\t\treturn this.state.lines.join(\"\\n\");\n\t}\n\n\tprivate expandPasteMarkers(text: string): string {\n\t\tlet result = text;\n\t\tfor (const [pasteId, pasteContent] of this.pastes) {\n\t\t\tconst markerRegex = new RegExp(`\\\\[paste #${pasteId}( (\\\\+\\\\d+ lines|\\\\d+ chars))?\\\\]`, \"g\");\n\t\t\tresult = result.replace(markerRegex, () => pasteContent);\n\t\t}\n\t\treturn result;\n\t}\n\n\t/**\n\t * Get text with paste markers expanded to their actual content.\n\t * Use this when you need the full content (e.g., for external editor).\n\t */\n\tgetExpandedText(): string {\n\t\treturn this.expandPasteMarkers(this.state.lines.join(\"\\n\"));\n\t}\n\n\tgetLines(): string[] {\n\t\treturn [...this.state.lines];\n\t}\n\n\tgetCursor(): { line: number; col: number } {\n\t\treturn { line: this.state.cursorLine, col: this.state.cursorCol };\n\t}\n\n\tsetText(text: string): void {\n\t\tthis.cancelAutocomplete();\n\t\tthis.lastAction = null;\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.pastes.clear();\n\t\tthis.pasteCounter = 0;\n\t\tthis.images.clear();\n\t\tthis.imageCounter = 0;\n\t\t// Keep submittedImages: submitValue() snapshots chips, then setText(\"\") runs.\n\t\tconst normalized = this.normalizeText(text);\n\t\t// Push undo snapshot if content differs (makes programmatic changes undoable)\n\t\tif (this.getText() !== normalized) {\n\t\t\tthis.pushUndoSnapshot();\n\t\t}\n\t\tthis.setTextInternal(normalized);\n\t}\n\n\t/**\n\t * Insert text at the current cursor position.\n\t * Used for programmatic insertion (e.g., clipboard image markers).\n\t * This is atomic for undo - single undo restores entire pre-insert state.\n\t */\n\tinsertTextAtCursor(text: string): void {\n\t\tif (!text) return;\n\t\tthis.cancelAutocomplete();\n\t\tthis.pushUndoSnapshot();\n\t\tthis.lastAction = null;\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.insertTextAtCursorInternal(text);\n\t}\n\n\t/**\n\t * Insert an atomic `[image_n]` chip and remember the off-screen file.\n\t * IDs stay stable for the current draft. The next unused number is used.\n\t */\n\tinsertImageMarker(attachment: { path: string; mimeType: string }): number {\n\t\tthis.cancelAutocomplete();\n\t\tthis.pushUndoSnapshot();\n\t\tthis.lastAction = null;\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.imageCounter++;\n\t\tconst id = this.imageCounter;\n\t\tthis.images.set(id, { id, path: attachment.path, mimeType: attachment.mimeType });\n\t\tthis.insertTextAtCursorInternal(formatImageMarker(id));\n\t\treturn id;\n\t}\n\n\t/**\n\t * Restore previously submitted image chips into the current draft.\n\t * Used by /edit so `[image_n]` stays a chip instead of becoming a path.\n\t */\n\trestoreImageMarkers(attachments: EditorImageAttachment[]): void {\n\t\tthis.images.clear();\n\t\tthis.imageCounter = 0;\n\t\tfor (const attachment of attachments) {\n\t\t\tthis.images.set(attachment.id, { ...attachment });\n\t\t\tif (attachment.id > this.imageCounter) this.imageCounter = attachment.id;\n\t\t}\n\t}\n\n\t/** Pending image chips still in the editor, in first-appearance order. */\n\tgetPendingImages(): EditorImageAttachment[] {\n\t\tconst text = this.getText();\n\t\tconst pending: EditorImageAttachment[] = [];\n\t\tfor (const id of collectImageMarkerIds(text)) {\n\t\t\tconst attachment = this.images.get(id);\n\t\t\tif (attachment) pending.push(attachment);\n\t\t}\n\t\treturn pending;\n\t}\n\n\t/** Take and clear pending image chips. Call after the text has been submitted. */\n\ttakePendingImages(): EditorImageAttachment[] {\n\t\tconst pending = this.submittedImages.length > 0 ? this.submittedImages : this.getPendingImages();\n\t\tthis.submittedImages = [];\n\t\tthis.images.clear();\n\t\tthis.imageCounter = 0;\n\t\treturn pending;\n\t}\n\n\t/**\n\t * Normalize text for editor storage:\n\t * - Normalize line endings (\\r\\n and \\r -> \\n)\n\t * - Expand tabs to 4 spaces\n\t */\n\tprivate normalizeText(text: string): string {\n\t\treturn text.replace(/\\r\\n/g, \"\\n\").replace(/\\r/g, \"\\n\").replace(/\\t/g, \" \");\n\t}\n\n\t/**\n\t * Internal text insertion at cursor. Handles single and multi-line text.\n\t * Does not push undo snapshots or trigger autocomplete - caller is responsible.\n\t * Normalizes line endings and calls onChange once at the end.\n\t */\n\tprivate insertTextAtCursorInternal(text: string): void {\n\t\tif (!text) return;\n\n\t\t// Normalize line endings and tabs\n\t\tconst normalized = this.normalizeText(text);\n\t\tconst insertedLines = normalized.split(\"\\n\");\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\tconst beforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\tconst afterCursor = currentLine.slice(this.state.cursorCol);\n\n\t\tif (insertedLines.length === 1) {\n\t\t\t// Single line - insert at cursor position\n\t\t\tthis.state.lines[this.state.cursorLine] = beforeCursor + normalized + afterCursor;\n\t\t\tthis.setCursorCol(this.state.cursorCol + normalized.length);\n\t\t} else {\n\t\t\t// Multi-line insertion\n\t\t\tthis.state.lines = [\n\t\t\t\t// All lines before current line\n\t\t\t\t...this.state.lines.slice(0, this.state.cursorLine),\n\n\t\t\t\t// The first inserted line merged with text before cursor\n\t\t\t\tbeforeCursor + insertedLines[0],\n\n\t\t\t\t// All middle inserted lines\n\t\t\t\t...insertedLines.slice(1, -1),\n\n\t\t\t\t// The last inserted line with text after cursor\n\t\t\t\tinsertedLines[insertedLines.length - 1] + afterCursor,\n\n\t\t\t\t// All lines after current line\n\t\t\t\t...this.state.lines.slice(this.state.cursorLine + 1),\n\t\t\t];\n\n\t\t\tthis.state.cursorLine += insertedLines.length - 1;\n\t\t\tthis.setCursorCol((insertedLines[insertedLines.length - 1] || \"\").length);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\t// All the editor methods from before...\n\tprivate insertCharacter(char: string, skipUndoCoalescing?: boolean): void {\n\t\tthis.exitHistoryBrowsing();\n\n\t\t// Undo coalescing (fish-style):\n\t\t// - Consecutive word chars coalesce into one undo unit\n\t\t// - Space captures state before itself (so undo removes space+following word together)\n\t\t// - Each space is separately undoable\n\t\t// Skip coalescing when called from atomic operations (e.g., handlePaste)\n\t\tif (!skipUndoCoalescing) {\n\t\t\tif (isWhitespaceChar(char) || this.lastAction !== \"type-word\") {\n\t\t\t\tthis.pushUndoSnapshot();\n\t\t\t}\n\t\t\tthis.lastAction = \"type-word\";\n\t\t}\n\n\t\tconst line = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\tconst before = line.slice(0, this.state.cursorCol);\n\t\tconst after = line.slice(this.state.cursorCol);\n\n\t\tthis.state.lines[this.state.cursorLine] = before + char + after;\n\t\tthis.setCursorCol(this.state.cursorCol + char.length);\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\n\t\t// Check if we should trigger or update autocomplete\n\t\tif (!this.autocompleteState) {\n\t\t\t// Auto-trigger for \"/\" at the start of a line (slash commands)\n\t\t\tif (char === \"/\" && this.isAtStartOfMessage()) {\n\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t}\n\t\t\t// Auto-trigger for symbol-based completion like @, #, or provider triggers at token boundaries\n\t\t\telse if (this.autocompleteTriggerCharacters.includes(char)) {\n\t\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\t\tconst textBeforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\t\t\tconst charBeforeSymbol = textBeforeCursor[textBeforeCursor.length - 2];\n\t\t\t\tif (textBeforeCursor.length === 1 || charBeforeSymbol === \" \" || charBeforeSymbol === \"\\t\") {\n\t\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Also auto-trigger when typing letters in a slash command or symbol completion context\n\t\t\telse if (/[a-zA-Z0-9.\\-_]/.test(char)) {\n\t\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\t\tconst textBeforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\t\t\t// Check if we're in a slash command (with or without space for arguments)\n\t\t\t\tif (this.isInSlashCommandContext(textBeforeCursor)) {\n\t\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t\t}\n\t\t\t\t// Check if we're in a symbol-based completion context like @, #, or provider triggers\n\t\t\t\telse if (this.autocompleteTriggerPattern.test(textBeforeCursor)) {\n\t\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tthis.updateAutocomplete();\n\t\t}\n\t}\n\n\tprivate handlePaste(pastedText: string): void {\n\t\tthis.cancelAutocomplete();\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.lastAction = null;\n\n\t\tthis.pushUndoSnapshot();\n\n\t\t// Some terminals (e.g. tmux popups with extended-keys-format=csi-u) re-encode\n\t\t// control bytes inside bracketed paste as CSI-u Ctrl+<letter> sequences\n\t\t// (ESC [ <codepoint> ; 5 u). Decode those back to their literal byte so the\n\t\t// per-char filter below preserves newlines instead of stripping ESC and\n\t\t// leaking the printable tail (e.g. \"[106;5u\") into the editor.\n\t\tconst decodedText = pastedText.replace(/\\x1b\\[(\\d+);5u/g, (match, code) => {\n\t\t\tconst cp = Number(code);\n\t\t\tif (cp >= 97 && cp <= 122) return String.fromCharCode(cp - 96);\n\t\t\tif (cp >= 65 && cp <= 90) return String.fromCharCode(cp - 64);\n\t\t\treturn match;\n\t\t});\n\n\t\t// Clean the pasted text: normalize line endings, expand tabs\n\t\tconst cleanText = this.normalizeText(decodedText);\n\n\t\t// Filter out non-printable characters except newlines\n\t\tlet filteredText = cleanText\n\t\t\t.split(\"\")\n\t\t\t.filter((char) => char === \"\\n\" || char.charCodeAt(0) >= 32)\n\t\t\t.join(\"\");\n\n\t\t// If pasting a file path (starts with /, ~, or .) and the character before\n\t\t// the cursor is a word character, prepend a space for better readability\n\t\tif (/^[/~.]/.test(filteredText)) {\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst charBeforeCursor = this.state.cursorCol > 0 ? currentLine[this.state.cursorCol - 1] : \"\";\n\t\t\tif (charBeforeCursor && /\\w/.test(charBeforeCursor)) {\n\t\t\t\tfilteredText = ` ${filteredText}`;\n\t\t\t}\n\t\t}\n\n\t\t// Split into lines to check for large paste\n\t\tconst pastedLines = filteredText.split(\"\\n\");\n\n\t\t// Check if this is a large paste (> 10 lines or > 1000 characters)\n\t\tconst totalChars = filteredText.length;\n\t\tif (pastedLines.length > 10 || totalChars > 1000) {\n\t\t\t// Store the paste and insert a marker\n\t\t\tthis.pasteCounter++;\n\t\t\tconst pasteId = this.pasteCounter;\n\t\t\tthis.pastes.set(pasteId, filteredText);\n\n\t\t\t// Insert marker like \"[paste #1 +123 lines]\" or \"[paste #1 1234 chars]\"\n\t\t\tconst marker =\n\t\t\t\tpastedLines.length > 10\n\t\t\t\t\t? `[paste #${pasteId} +${pastedLines.length} lines]`\n\t\t\t\t\t: `[paste #${pasteId} ${totalChars} chars]`;\n\t\t\tthis.insertTextAtCursorInternal(marker);\n\t\t\treturn;\n\t\t}\n\n\t\tif (pastedLines.length === 1) {\n\t\t\t// Single line - insert atomically (do not trigger autocomplete during paste)\n\t\t\tthis.insertTextAtCursorInternal(filteredText);\n\t\t\treturn;\n\t\t}\n\n\t\t// Multi-line paste - use direct state manipulation\n\t\tthis.insertTextAtCursorInternal(filteredText);\n\t}\n\n\tprivate addNewLine(): void {\n\t\tthis.cancelAutocomplete();\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.lastAction = null;\n\n\t\tthis.pushUndoSnapshot();\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\tconst before = currentLine.slice(0, this.state.cursorCol);\n\t\tconst after = currentLine.slice(this.state.cursorCol);\n\n\t\t// Split current line\n\t\tthis.state.lines[this.state.cursorLine] = before;\n\t\tthis.state.lines.splice(this.state.cursorLine + 1, 0, after);\n\n\t\t// Move cursor to start of new line\n\t\tthis.state.cursorLine++;\n\t\tthis.setCursorCol(0);\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tprivate shouldSubmitOnBackslashEnter(data: string, kb: ReturnType<typeof getKeybindings>): boolean {\n\t\tif (this.disableSubmit) return false;\n\t\tif (!matchesKey(data, \"enter\")) return false;\n\t\tconst submitKeys = kb.getKeys(\"tui.input.submit\");\n\t\tconst hasShiftEnter = submitKeys.includes(\"shift+enter\") || submitKeys.includes(\"shift+return\");\n\t\tif (!hasShiftEnter) return false;\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\treturn this.state.cursorCol > 0 && currentLine[this.state.cursorCol - 1] === \"\\\\\";\n\t}\n\n\tprivate submitValue(): void {\n\t\tthis.cancelAutocomplete();\n\t\tconst result = this.expandPasteMarkers(this.state.lines.join(\"\\n\")).trim();\n\t\t// Snapshot before the editor is cleared so onSubmit can still attach files.\n\t\tthis.submittedImages = this.getPendingImages();\n\n\t\tthis.state = { lines: [\"\"], cursorLine: 0, cursorCol: 0 };\n\t\tthis.pastes.clear();\n\t\tthis.pasteCounter = 0;\n\t\tthis.images.clear();\n\t\tthis.imageCounter = 0;\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.scrollOffset = 0;\n\t\tthis.undoStack.clear();\n\t\tthis.lastAction = null;\n\n\t\tif (this.onChange) this.onChange(\"\");\n\t\tif (this.onSubmit) this.onSubmit(result);\n\t}\n\n\tprivate handleBackspace(): void {\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.lastAction = null;\n\n\t\tif (this.state.cursorCol > 0) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Delete grapheme before cursor (handles emojis, combining characters, etc.)\n\t\t\tlet line = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst beforeCursor = line.slice(0, this.state.cursorCol);\n\n\t\t\t// Find the last grapheme in the text before cursor\n\t\t\tconst graphemes = [...this.segment(beforeCursor, \"grapheme\")];\n\t\t\tconst lastGrapheme = graphemes[graphemes.length - 1];\n\t\t\tconst graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;\n\t\t\tconst isPastedSegmented = PASTE_MARKER_SINGLE.exec(lastGrapheme.segment);\n\t\t\tconst isImageSegmented = IMAGE_MARKER_SINGLE.exec(lastGrapheme.segment);\n\n\t\t\tif (isPastedSegmented) {\n\t\t\t\t// This contains the id part e.g 4 from [paste #4 +123 lines]\n\t\t\t\tconst targetId = Number(isPastedSegmented[1]);\n\t\t\t\tthis.pastes.delete(targetId);\n\t\t\t\tthis.pasteCounter--;\n\n\t\t\t\t// We got to update id of markers which are greater than the removed one\n\t\t\t\tthis.state.lines = this.state.lines.map((line) =>\n\t\t\t\t\tline.replace(PASTE_MARKER_REGEX, (fullMatch, idGroup, suffixGroup) => {\n\t\t\t\t\t\tconst x = Number(idGroup);\n\t\t\t\t\t\tif (x <= targetId) return fullMatch;\n\n\t\t\t\t\t\t// [paste #3] become [paste #2] if we remove [paste #1]\n\t\t\t\t\t\tconst newText = `[paste #${x - 1}${suffixGroup}]`;\n\t\t\t\t\t\tthis.pastes.set(x - 1, this.pastes.get(x) ?? newText);\n\t\t\t\t\t\tthis.pastes.delete(x);\n\t\t\t\t\t\treturn newText;\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t} else if (isImageSegmented) {\n\t\t\t\tconst targetId = Number(isImageSegmented[1]);\n\t\t\t\tthis.images.delete(targetId);\n\t\t\t}\n\n\t\t\tline = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\t\tconst before = line.slice(0, this.state.cursorCol - graphemeLength);\n\t\t\tconst after = line.slice(this.state.cursorCol);\n\n\t\t\tthis.state.lines[this.state.cursorLine] = before + after;\n\t\t\tthis.setCursorCol(this.state.cursorCol - graphemeLength);\n\t\t} else if (this.state.cursorLine > 0) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Merge with previous line\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst previousLine = this.state.lines[this.state.cursorLine - 1] || \"\";\n\n\t\t\tthis.state.lines[this.state.cursorLine - 1] = previousLine + currentLine;\n\t\t\tthis.state.lines.splice(this.state.cursorLine, 1);\n\n\t\t\tthis.state.cursorLine--;\n\t\t\tthis.setCursorCol(previousLine.length);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\n\t\t// Update or re-trigger autocomplete after backspace\n\t\tif (this.autocompleteState) {\n\t\t\tthis.updateAutocomplete();\n\t\t} else {\n\t\t\t// If autocomplete was cancelled (no matches), re-trigger if we're in a completable context\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst textBeforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\t\t// Slash command context\n\t\t\tif (this.isInSlashCommandContext(textBeforeCursor)) {\n\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t}\n\t\t\t// Symbol-based completion context like @, #, or provider triggers\n\t\t\telse if (this.autocompleteTriggerPattern.test(textBeforeCursor)) {\n\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Set cursor column and clear preferredVisualCol.\n\t * Use this for all non-vertical cursor movements to reset sticky column behavior.\n\t */\n\tprivate setCursorCol(col: number): void {\n\t\tthis.state.cursorCol = col;\n\t\tthis.preferredVisualCol = null;\n\t\tthis.snappedFromCursorCol = null;\n\t}\n\n\t/**\n\t * Move cursor to a target visual line, applying sticky column logic.\n\t * Shared by moveCursor() and pageScroll().\n\t */\n\tprivate moveToVisualLine(\n\t\tvisualLines: Array<{ logicalLine: number; startCol: number; length: number }>,\n\t\tcurrentVisualLine: number,\n\t\ttargetVisualLine: number,\n\t): void {\n\t\tconst currentVL = visualLines[currentVisualLine];\n\t\tconst targetVL = visualLines[targetVisualLine];\n\t\tif (!(currentVL && targetVL)) return;\n\n\t\t// When the cursor was snapped to a segment start, resolve the pre-snap\n\t\t// position against the VL it belongs to. This gives the correct visual\n\t\t// column even after a resize reshuffles VLs.\n\t\tlet currentVisualCol: number;\n\t\tif (this.snappedFromCursorCol !== null) {\n\t\t\tconst vlIndex = this.findVisualLineAt(visualLines, currentVL.logicalLine, this.snappedFromCursorCol);\n\t\t\tcurrentVisualCol = this.snappedFromCursorCol - visualLines[vlIndex].startCol;\n\t\t} else {\n\t\t\tcurrentVisualCol = this.state.cursorCol - currentVL.startCol;\n\t\t}\n\n\t\t// For non-last segments, clamp to length-1 to stay within the segment\n\t\tconst isLastSourceSegment =\n\t\t\tcurrentVisualLine === visualLines.length - 1 ||\n\t\t\tvisualLines[currentVisualLine + 1]?.logicalLine !== currentVL.logicalLine;\n\t\tconst sourceMaxVisualCol = isLastSourceSegment ? currentVL.length : Math.max(0, currentVL.length - 1);\n\n\t\tconst isLastTargetSegment =\n\t\t\ttargetVisualLine === visualLines.length - 1 ||\n\t\t\tvisualLines[targetVisualLine + 1]?.logicalLine !== targetVL.logicalLine;\n\t\tconst targetMaxVisualCol = isLastTargetSegment ? targetVL.length : Math.max(0, targetVL.length - 1);\n\n\t\tconst moveToVisualCol = this.computeVerticalMoveColumn(currentVisualCol, sourceMaxVisualCol, targetMaxVisualCol);\n\n\t\t// Set cursor position\n\t\tthis.state.cursorLine = targetVL.logicalLine;\n\t\tconst targetCol = targetVL.startCol + moveToVisualCol;\n\t\tconst logicalLine = this.state.lines[targetVL.logicalLine] || \"\";\n\t\tthis.state.cursorCol = Math.min(targetCol, logicalLine.length);\n\n\t\t// Snap cursor to atomic segment boundary (e.g. paste markers)\n\t\t// so the cursor never lands in the middle of a multi-grapheme unit.\n\t\t// Single-grapheme segments don't need snapping.\n\t\tconst segments = [...this.segment(logicalLine, \"grapheme\")];\n\t\tfor (const seg of segments) {\n\t\t\tif (seg.index > this.state.cursorCol) break;\n\t\t\tif (seg.segment.length <= 1) continue;\n\t\t\tif (this.state.cursorCol < seg.index + seg.segment.length) {\n\t\t\t\tconst isContinuation = seg.index < targetVL.startCol;\n\t\t\t\tconst isMovingDown = targetVisualLine > currentVisualLine;\n\n\t\t\t\tif (isContinuation && isMovingDown) {\n\t\t\t\t\t// The segment started on a previous visual line, and we\n\t\t\t\t\t// already visited it on the way down. Skip all remaining\n\t\t\t\t\t// continuation VLs and land on the first VL past it.\n\t\t\t\t\tconst segEnd = seg.index + seg.segment.length;\n\t\t\t\t\tlet next = targetVisualLine + 1;\n\t\t\t\t\twhile (\n\t\t\t\t\t\tnext < visualLines.length &&\n\t\t\t\t\t\tvisualLines[next].logicalLine === targetVL.logicalLine &&\n\t\t\t\t\t\tvisualLines[next].startCol < segEnd\n\t\t\t\t\t) {\n\t\t\t\t\t\tnext++;\n\t\t\t\t\t}\n\t\t\t\t\tif (next < visualLines.length) {\n\t\t\t\t\t\tthis.moveToVisualLine(visualLines, currentVisualLine, next);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Snap to the start of the segment so it gets highlighted.\n\t\t\t\t// Store the pre-snap position so the next vertical move can\n\t\t\t\t// resolve it to the correct visual column.\n\t\t\t\tthis.snappedFromCursorCol = this.state.cursorCol;\n\t\t\t\tthis.state.cursorCol = seg.index;\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\t// No snap occurred – we moved out of the atomic segment.\n\t\tthis.snappedFromCursorCol = null;\n\t}\n\n\t/**\n\t * Compute the target visual column for vertical cursor movement.\n\t * Implements the sticky column decision table:\n\t *\n\t * | P | S | T | U | Scenario | Set Preferred | Move To |\n\t * |---|---|---|---| ---------------------------------------------------- |---------------|-------------|\n\t * | 0 | * | 0 | - | Start nav, target fits | null | current |\n\t * | 0 | * | 1 | - | Start nav, target shorter | current | target end |\n\t * | 1 | 0 | 0 | 0 | Clamped, target fits preferred | null | preferred |\n\t * | 1 | 0 | 0 | 1 | Clamped, target longer but still can't fit preferred | keep | target end |\n\t * | 1 | 0 | 1 | - | Clamped, target even shorter | keep | target end |\n\t * | 1 | 1 | 0 | - | Rewrapped, target fits current | null | current |\n\t * | 1 | 1 | 1 | - | Rewrapped, target shorter than current | current | target end |\n\t *\n\t * Where:\n\t * - P = preferred col is set\n\t * - S = cursor in middle of source line (not clamped to end)\n\t * - T = target line shorter than current visual col\n\t * - U = target line shorter than preferred col\n\t */\n\tprivate computeVerticalMoveColumn(\n\t\tcurrentVisualCol: number,\n\t\tsourceMaxVisualCol: number,\n\t\ttargetMaxVisualCol: number,\n\t): number {\n\t\tconst hasPreferred = this.preferredVisualCol !== null; // P\n\t\tconst cursorInMiddle = currentVisualCol < sourceMaxVisualCol; // S\n\t\tconst targetTooShort = targetMaxVisualCol < currentVisualCol; // T\n\n\t\tif (!hasPreferred || cursorInMiddle) {\n\t\t\tif (targetTooShort) {\n\t\t\t\t// Cases 2 and 7\n\t\t\t\tthis.preferredVisualCol = currentVisualCol;\n\t\t\t\treturn targetMaxVisualCol;\n\t\t\t}\n\n\t\t\t// Cases 1 and 6\n\t\t\tthis.preferredVisualCol = null;\n\t\t\treturn currentVisualCol;\n\t\t}\n\n\t\tconst targetCantFitPreferred = targetMaxVisualCol < this.preferredVisualCol!; // U\n\t\tif (targetTooShort || targetCantFitPreferred) {\n\t\t\t// Cases 4 and 5\n\t\t\treturn targetMaxVisualCol;\n\t\t}\n\n\t\t// Case 3\n\t\tconst result = this.preferredVisualCol!;\n\t\tthis.preferredVisualCol = null;\n\t\treturn result;\n\t}\n\n\tprivate moveToLineStart(): void {\n\t\tthis.lastAction = null;\n\t\tthis.setCursorCol(0);\n\t}\n\n\tprivate moveToLineEnd(): void {\n\t\tthis.lastAction = null;\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\tthis.setCursorCol(currentLine.length);\n\t}\n\n\tprivate deleteToStartOfLine(): void {\n\t\tthis.exitHistoryBrowsing();\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\tif (this.state.cursorCol > 0) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Calculate text to be deleted and save to kill ring (backward deletion = prepend)\n\t\t\tconst deletedText = currentLine.slice(0, this.state.cursorCol);\n\t\t\tthis.killRing.push(deletedText, { prepend: true, accumulate: this.lastAction === \"kill\" });\n\t\t\tthis.lastAction = \"kill\";\n\n\t\t\t// Delete from start of line up to cursor\n\t\t\tthis.state.lines[this.state.cursorLine] = currentLine.slice(this.state.cursorCol);\n\t\t\tthis.setCursorCol(0);\n\t\t} else if (this.state.cursorLine > 0) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// At start of line - merge with previous line, treating newline as deleted text\n\t\t\tthis.killRing.push(\"\\n\", { prepend: true, accumulate: this.lastAction === \"kill\" });\n\t\t\tthis.lastAction = \"kill\";\n\n\t\t\tconst previousLine = this.state.lines[this.state.cursorLine - 1] || \"\";\n\t\t\tthis.state.lines[this.state.cursorLine - 1] = previousLine + currentLine;\n\t\t\tthis.state.lines.splice(this.state.cursorLine, 1);\n\t\t\tthis.state.cursorLine--;\n\t\t\tthis.setCursorCol(previousLine.length);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tprivate deleteToEndOfLine(): void {\n\t\tthis.exitHistoryBrowsing();\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\tif (this.state.cursorCol < currentLine.length) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Calculate text to be deleted and save to kill ring (forward deletion = append)\n\t\t\tconst deletedText = currentLine.slice(this.state.cursorCol);\n\t\t\tthis.killRing.push(deletedText, { prepend: false, accumulate: this.lastAction === \"kill\" });\n\t\t\tthis.lastAction = \"kill\";\n\n\t\t\t// Delete from cursor to end of line\n\t\t\tthis.state.lines[this.state.cursorLine] = currentLine.slice(0, this.state.cursorCol);\n\t\t} else if (this.state.cursorLine < this.state.lines.length - 1) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// At end of line - merge with next line, treating newline as deleted text\n\t\t\tthis.killRing.push(\"\\n\", { prepend: false, accumulate: this.lastAction === \"kill\" });\n\t\t\tthis.lastAction = \"kill\";\n\n\t\t\tconst nextLine = this.state.lines[this.state.cursorLine + 1] || \"\";\n\t\t\tthis.state.lines[this.state.cursorLine] = currentLine + nextLine;\n\t\t\tthis.state.lines.splice(this.state.cursorLine + 1, 1);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tprivate deleteWordBackwards(): void {\n\t\tthis.exitHistoryBrowsing();\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\t// If at start of line, behave like backspace at column 0 (merge with previous line)\n\t\tif (this.state.cursorCol === 0) {\n\t\t\tif (this.state.cursorLine > 0) {\n\t\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t\t// Treat newline as deleted text (backward deletion = prepend)\n\t\t\t\tthis.killRing.push(\"\\n\", { prepend: true, accumulate: this.lastAction === \"kill\" });\n\t\t\t\tthis.lastAction = \"kill\";\n\n\t\t\t\tconst previousLine = this.state.lines[this.state.cursorLine - 1] || \"\";\n\t\t\t\tthis.state.lines[this.state.cursorLine - 1] = previousLine + currentLine;\n\t\t\t\tthis.state.lines.splice(this.state.cursorLine, 1);\n\t\t\t\tthis.state.cursorLine--;\n\t\t\t\tthis.setCursorCol(previousLine.length);\n\t\t\t}\n\t\t} else {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Save lastAction before cursor movement (moveWordBackwards resets it)\n\t\t\tconst wasKill = this.lastAction === \"kill\";\n\n\t\t\tconst oldCursorCol = this.state.cursorCol;\n\t\t\tthis.moveWordBackwards();\n\t\t\tconst deleteFrom = this.state.cursorCol;\n\t\t\tthis.setCursorCol(oldCursorCol);\n\n\t\t\tconst deletedText = currentLine.slice(deleteFrom, this.state.cursorCol);\n\t\t\tthis.killRing.push(deletedText, { prepend: true, accumulate: wasKill });\n\t\t\tthis.lastAction = \"kill\";\n\n\t\t\tthis.state.lines[this.state.cursorLine] =\n\t\t\t\tcurrentLine.slice(0, deleteFrom) + currentLine.slice(this.state.cursorCol);\n\t\t\tthis.setCursorCol(deleteFrom);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tprivate deleteWordForward(): void {\n\t\tthis.exitHistoryBrowsing();\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\t// If at end of line, merge with next line (delete the newline)\n\t\tif (this.state.cursorCol >= currentLine.length) {\n\t\t\tif (this.state.cursorLine < this.state.lines.length - 1) {\n\t\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t\t// Treat newline as deleted text (forward deletion = append)\n\t\t\t\tthis.killRing.push(\"\\n\", { prepend: false, accumulate: this.lastAction === \"kill\" });\n\t\t\t\tthis.lastAction = \"kill\";\n\n\t\t\t\tconst nextLine = this.state.lines[this.state.cursorLine + 1] || \"\";\n\t\t\t\tthis.state.lines[this.state.cursorLine] = currentLine + nextLine;\n\t\t\t\tthis.state.lines.splice(this.state.cursorLine + 1, 1);\n\t\t\t}\n\t\t} else {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Save lastAction before cursor movement (moveWordForwards resets it)\n\t\t\tconst wasKill = this.lastAction === \"kill\";\n\n\t\t\tconst oldCursorCol = this.state.cursorCol;\n\t\t\tthis.moveWordForwards();\n\t\t\tconst deleteTo = this.state.cursorCol;\n\t\t\tthis.setCursorCol(oldCursorCol);\n\n\t\t\tconst deletedText = currentLine.slice(this.state.cursorCol, deleteTo);\n\t\t\tthis.killRing.push(deletedText, { prepend: false, accumulate: wasKill });\n\t\t\tthis.lastAction = \"kill\";\n\n\t\t\tthis.state.lines[this.state.cursorLine] =\n\t\t\t\tcurrentLine.slice(0, this.state.cursorCol) + currentLine.slice(deleteTo);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tprivate handleForwardDelete(): void {\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.lastAction = null;\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\tif (this.state.cursorCol < currentLine.length) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Delete grapheme at cursor position (handles emojis, combining characters, etc.)\n\t\t\tconst afterCursor = currentLine.slice(this.state.cursorCol);\n\n\t\t\t// Find the first grapheme at cursor\n\t\t\tconst graphemes = [...this.segment(afterCursor, \"grapheme\")];\n\t\t\tconst firstGrapheme = graphemes[0];\n\t\t\tconst graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;\n\t\t\tconst deletedImage = firstGrapheme ? IMAGE_MARKER_SINGLE.exec(firstGrapheme.segment) : null;\n\t\t\tif (deletedImage) {\n\t\t\t\tthis.images.delete(Number(deletedImage[1]));\n\t\t\t}\n\n\t\t\tconst before = currentLine.slice(0, this.state.cursorCol);\n\t\t\tconst after = currentLine.slice(this.state.cursorCol + graphemeLength);\n\t\t\tthis.state.lines[this.state.cursorLine] = before + after;\n\t\t} else if (this.state.cursorLine < this.state.lines.length - 1) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// At end of line - merge with next line\n\t\t\tconst nextLine = this.state.lines[this.state.cursorLine + 1] || \"\";\n\t\t\tthis.state.lines[this.state.cursorLine] = currentLine + nextLine;\n\t\t\tthis.state.lines.splice(this.state.cursorLine + 1, 1);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\n\t\t// Update or re-trigger autocomplete after forward delete\n\t\tif (this.autocompleteState) {\n\t\t\tthis.updateAutocomplete();\n\t\t} else {\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst textBeforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\t\t// Slash command context\n\t\t\tif (this.isInSlashCommandContext(textBeforeCursor)) {\n\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t}\n\t\t\t// Symbol-based completion context like @, #, or provider triggers\n\t\t\telse if (this.autocompleteTriggerPattern.test(textBeforeCursor)) {\n\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Build a mapping from visual lines to logical positions.\n\t * Returns an array where each element represents a visual line with:\n\t * - logicalLine: index into this.state.lines\n\t * - startCol: starting column in the logical line\n\t * - length: length of this visual line segment\n\t */\n\tprivate buildVisualLineMap(width: number): Array<{ logicalLine: number; startCol: number; length: number }> {\n\t\tconst visualLines: Array<{ logicalLine: number; startCol: number; length: number }> = [];\n\n\t\tfor (let i = 0; i < this.state.lines.length; i++) {\n\t\t\tconst line = this.state.lines[i] || \"\";\n\t\t\tconst lineVisWidth = visibleWidth(line);\n\t\t\tif (line.length === 0) {\n\t\t\t\t// Empty line still takes one visual line\n\t\t\t\tvisualLines.push({ logicalLine: i, startCol: 0, length: 0 });\n\t\t\t} else if (lineVisWidth <= width) {\n\t\t\t\tvisualLines.push({ logicalLine: i, startCol: 0, length: line.length });\n\t\t\t} else {\n\t\t\t\t// Line needs wrapping - use word-aware wrapping\n\t\t\t\tconst chunks = wordWrapLine(line, width, [...this.segment(line, \"grapheme\")]);\n\t\t\t\tfor (const chunk of chunks) {\n\t\t\t\t\tvisualLines.push({\n\t\t\t\t\t\tlogicalLine: i,\n\t\t\t\t\t\tstartCol: chunk.startIndex,\n\t\t\t\t\t\tlength: chunk.endIndex - chunk.startIndex,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn visualLines;\n\t}\n\n\t/**\n\t * Find the visual line index that contains the given logical position.\n\t */\n\tprivate findVisualLineAt(\n\t\tvisualLines: Array<{ logicalLine: number; startCol: number; length: number }>,\n\t\tline: number,\n\t\tcol: number,\n\t): number {\n\t\tfor (let i = 0; i < visualLines.length; i++) {\n\t\t\tconst vl = visualLines[i];\n\t\t\tif (!vl || vl.logicalLine !== line) continue;\n\t\t\tconst offset = col - vl.startCol;\n\t\t\t// Cursor is in this segment if it's within range. For the last\n\t\t\t// segment of a logical line, cursor can be at length (end position)\n\t\t\tconst isLastSegmentOfLine = i === visualLines.length - 1 || visualLines[i + 1]?.logicalLine !== vl.logicalLine;\n\t\t\tif (offset >= 0 && (offset < vl.length || (isLastSegmentOfLine && offset === vl.length))) {\n\t\t\t\treturn i;\n\t\t\t}\n\t\t}\n\t\treturn visualLines.length - 1;\n\t}\n\n\t/**\n\t * Find the visual line index for the current cursor position.\n\t */\n\tprivate findCurrentVisualLine(\n\t\tvisualLines: Array<{ logicalLine: number; startCol: number; length: number }>,\n\t): number {\n\t\treturn this.findVisualLineAt(visualLines, this.state.cursorLine, this.state.cursorCol);\n\t}\n\n\tprivate moveCursor(deltaLine: number, deltaCol: number): void {\n\t\tthis.lastAction = null;\n\t\tconst visualLines = this.buildVisualLineMap(this.lastWidth);\n\t\tconst currentVisualLine = this.findCurrentVisualLine(visualLines);\n\n\t\tif (deltaLine !== 0) {\n\t\t\tconst targetVisualLine = currentVisualLine + deltaLine;\n\n\t\t\tif (targetVisualLine >= 0 && targetVisualLine < visualLines.length) {\n\t\t\t\tthis.moveToVisualLine(visualLines, currentVisualLine, targetVisualLine);\n\t\t\t}\n\t\t}\n\n\t\tif (deltaCol !== 0) {\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\t\tif (deltaCol > 0) {\n\t\t\t\t// Moving right - move by one grapheme (handles emojis, combining characters, etc.)\n\t\t\t\tif (this.state.cursorCol < currentLine.length) {\n\t\t\t\t\tconst afterCursor = currentLine.slice(this.state.cursorCol);\n\t\t\t\t\tconst graphemes = [...this.segment(afterCursor, \"grapheme\")];\n\t\t\t\t\tconst firstGrapheme = graphemes[0];\n\t\t\t\t\tthis.setCursorCol(this.state.cursorCol + (firstGrapheme ? firstGrapheme.segment.length : 1));\n\t\t\t\t} else if (this.state.cursorLine < this.state.lines.length - 1) {\n\t\t\t\t\t// Wrap to start of next logical line\n\t\t\t\t\tthis.state.cursorLine++;\n\t\t\t\t\tthis.setCursorCol(0);\n\t\t\t\t} else {\n\t\t\t\t\t// At end of last line - can't move, but set preferredVisualCol for up/down navigation\n\t\t\t\t\tconst currentVL = visualLines[currentVisualLine];\n\t\t\t\t\tif (currentVL) {\n\t\t\t\t\t\tthis.preferredVisualCol = this.state.cursorCol - currentVL.startCol;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Moving left - move by one grapheme (handles emojis, combining characters, etc.)\n\t\t\t\tif (this.state.cursorCol > 0) {\n\t\t\t\t\tconst beforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\t\t\t\tconst graphemes = [...this.segment(beforeCursor, \"grapheme\")];\n\t\t\t\t\tconst lastGrapheme = graphemes[graphemes.length - 1];\n\t\t\t\t\tthis.setCursorCol(this.state.cursorCol - (lastGrapheme ? lastGrapheme.segment.length : 1));\n\t\t\t\t} else if (this.state.cursorLine > 0) {\n\t\t\t\t\t// Wrap to end of previous logical line\n\t\t\t\t\tthis.state.cursorLine--;\n\t\t\t\t\tconst prevLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\t\t\tthis.setCursorCol(prevLine.length);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Keep an open autocomplete picker in sync with the new cursor\n\t\t// position: cursor movement changes the text before the cursor, so a\n\t\t// picker computed for the old position is stale. Re-query so it\n\t\t// refreshes — or closes when the new position yields no suggestions —\n\t\t// mirroring insertCharacter()/handleBackspace(). Without this, arrowing\n\t\t// left from `/cmd ` back into the command name leaves the argument\n\t\t// picker showing against a `/cmd` prefix (and a Tab there would\n\t\t// concatenate the stale suggestion onto the partial command name).\n\t\tif (this.autocompleteState) {\n\t\t\tthis.updateAutocomplete();\n\t\t}\n\t}\n\n\t/**\n\t * Scroll by a page (direction: -1 for up, 1 for down).\n\t * Moves cursor by the page size while keeping it in bounds.\n\t */\n\tprivate pageScroll(direction: -1 | 1): void {\n\t\tthis.lastAction = null;\n\t\tconst terminalRows = this.tui.terminal.rows;\n\t\tconst pageSize = Math.max(5, Math.floor(terminalRows * 0.3));\n\n\t\tconst visualLines = this.buildVisualLineMap(this.lastWidth);\n\t\tconst currentVisualLine = this.findCurrentVisualLine(visualLines);\n\t\tconst targetVisualLine = Math.max(0, Math.min(visualLines.length - 1, currentVisualLine + direction * pageSize));\n\n\t\tthis.moveToVisualLine(visualLines, currentVisualLine, targetVisualLine);\n\t}\n\n\tprivate moveWordBackwards(): void {\n\t\tthis.lastAction = null;\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\t// If at start of line, move to end of previous line\n\t\tif (this.state.cursorCol === 0) {\n\t\t\tif (this.state.cursorLine > 0) {\n\t\t\t\tthis.state.cursorLine--;\n\t\t\t\tconst prevLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\t\tthis.setCursorCol(prevLine.length);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tthis.setCursorCol(\n\t\t\tfindWordBackward(currentLine, this.state.cursorCol, {\n\t\t\t\tsegment: (text) => this.segment(text, \"word\"),\n\t\t\t\tisAtomicSegment: isAtomicMarker,\n\t\t\t}),\n\t\t);\n\t}\n\n\t/**\n\t * Yank (paste) the most recent kill ring entry at cursor position.\n\t */\n\tprivate yank(): void {\n\t\tif (this.killRing.length === 0) return;\n\n\t\tthis.pushUndoSnapshot();\n\n\t\tconst text = this.killRing.peek()!;\n\t\tthis.insertYankedText(text);\n\n\t\tthis.lastAction = \"yank\";\n\t}\n\n\t/**\n\t * Cycle through kill ring (only works immediately after yank or yank-pop).\n\t * Replaces the last yanked text with the previous entry in the ring.\n\t */\n\tprivate yankPop(): void {\n\t\t// Only works if we just yanked and have more than one entry\n\t\tif (this.lastAction !== \"yank\" || this.killRing.length <= 1) return;\n\n\t\tthis.pushUndoSnapshot();\n\n\t\t// Delete the previously yanked text (still at end of ring before rotation)\n\t\tthis.deleteYankedText();\n\n\t\t// Rotate the ring: move end to front\n\t\tthis.killRing.rotate();\n\n\t\t// Insert the new most recent entry (now at end after rotation)\n\t\tconst text = this.killRing.peek()!;\n\t\tthis.insertYankedText(text);\n\n\t\tthis.lastAction = \"yank\";\n\t}\n\n\t/**\n\t * Insert text at cursor position (used by yank operations).\n\t */\n\tprivate insertYankedText(text: string): void {\n\t\tthis.exitHistoryBrowsing();\n\t\tconst lines = text.split(\"\\n\");\n\n\t\tif (lines.length === 1) {\n\t\t\t// Single line - insert at cursor\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst before = currentLine.slice(0, this.state.cursorCol);\n\t\t\tconst after = currentLine.slice(this.state.cursorCol);\n\t\t\tthis.state.lines[this.state.cursorLine] = before + text + after;\n\t\t\tthis.setCursorCol(this.state.cursorCol + text.length);\n\t\t} else {\n\t\t\t// Multi-line insert\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst before = currentLine.slice(0, this.state.cursorCol);\n\t\t\tconst after = currentLine.slice(this.state.cursorCol);\n\n\t\t\t// First line merges with text before cursor\n\t\t\tthis.state.lines[this.state.cursorLine] = before + (lines[0] || \"\");\n\n\t\t\t// Insert middle lines\n\t\t\tfor (let i = 1; i < lines.length - 1; i++) {\n\t\t\t\tthis.state.lines.splice(this.state.cursorLine + i, 0, lines[i] || \"\");\n\t\t\t}\n\n\t\t\t// Last line merges with text after cursor\n\t\t\tconst lastLineIndex = this.state.cursorLine + lines.length - 1;\n\t\t\tthis.state.lines.splice(lastLineIndex, 0, (lines[lines.length - 1] || \"\") + after);\n\n\t\t\t// Update cursor position\n\t\t\tthis.state.cursorLine = lastLineIndex;\n\t\t\tthis.setCursorCol((lines[lines.length - 1] || \"\").length);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\t/**\n\t * Delete the previously yanked text (used by yank-pop).\n\t * The yanked text is derived from killRing[end] since it hasn't been rotated yet.\n\t */\n\tprivate deleteYankedText(): void {\n\t\tconst yankedText = this.killRing.peek();\n\t\tif (!yankedText) return;\n\n\t\tconst yankLines = yankedText.split(\"\\n\");\n\n\t\tif (yankLines.length === 1) {\n\t\t\t// Single line - delete backward from cursor\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst deleteLen = yankedText.length;\n\t\t\tconst before = currentLine.slice(0, this.state.cursorCol - deleteLen);\n\t\t\tconst after = currentLine.slice(this.state.cursorCol);\n\t\t\tthis.state.lines[this.state.cursorLine] = before + after;\n\t\t\tthis.setCursorCol(this.state.cursorCol - deleteLen);\n\t\t} else {\n\t\t\t// Multi-line delete - cursor is at end of last yanked line\n\t\t\tconst startLine = this.state.cursorLine - (yankLines.length - 1);\n\t\t\tconst startCol = (this.state.lines[startLine] || \"\").length - (yankLines[0] || \"\").length;\n\n\t\t\t// Get text after cursor on current line\n\t\t\tconst afterCursor = (this.state.lines[this.state.cursorLine] || \"\").slice(this.state.cursorCol);\n\n\t\t\t// Get text before yank start position\n\t\t\tconst beforeYank = (this.state.lines[startLine] || \"\").slice(0, startCol);\n\n\t\t\t// Remove all lines from startLine to cursorLine and replace with merged line\n\t\t\tthis.state.lines.splice(startLine, yankLines.length, beforeYank + afterCursor);\n\n\t\t\t// Update cursor\n\t\t\tthis.state.cursorLine = startLine;\n\t\t\tthis.setCursorCol(startCol);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tprivate pushUndoSnapshot(): void {\n\t\tthis.undoStack.push(this.state);\n\t}\n\n\tprivate undo(): void {\n\t\tthis.exitHistoryBrowsing();\n\t\tconst snapshot = this.undoStack.pop();\n\t\tif (!snapshot) return;\n\t\tObject.assign(this.state, snapshot);\n\t\tthis.lastAction = null;\n\t\tthis.preferredVisualCol = null;\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\t/**\n\t * Jump to the first occurrence of a character in the specified direction.\n\t * Multi-line search. Case-sensitive. Skips the current cursor position.\n\t */\n\tprivate jumpToChar(char: string, direction: \"forward\" | \"backward\"): void {\n\t\tthis.lastAction = null;\n\t\tconst isForward = direction === \"forward\";\n\t\tconst lines = this.state.lines;\n\n\t\tconst end = isForward ? lines.length : -1;\n\t\tconst step = isForward ? 1 : -1;\n\n\t\tfor (let lineIdx = this.state.cursorLine; lineIdx !== end; lineIdx += step) {\n\t\t\tconst line = lines[lineIdx] || \"\";\n\t\t\tconst isCurrentLine = lineIdx === this.state.cursorLine;\n\n\t\t\t// Current line: start after/before cursor; other lines: search full line\n\t\t\tconst searchFrom = isCurrentLine\n\t\t\t\t? isForward\n\t\t\t\t\t? this.state.cursorCol + 1\n\t\t\t\t\t: this.state.cursorCol - 1\n\t\t\t\t: undefined;\n\n\t\t\tconst idx = isForward ? line.indexOf(char, searchFrom) : line.lastIndexOf(char, searchFrom);\n\n\t\t\tif (idx !== -1) {\n\t\t\t\tthis.state.cursorLine = lineIdx;\n\t\t\t\tthis.setCursorCol(idx);\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\t// No match found - cursor stays in place\n\t}\n\n\tprivate moveWordForwards(): void {\n\t\tthis.lastAction = null;\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\t// If at end of line, move to start of next line\n\t\tif (this.state.cursorCol >= currentLine.length) {\n\t\t\tif (this.state.cursorLine < this.state.lines.length - 1) {\n\t\t\t\tthis.state.cursorLine++;\n\t\t\t\tthis.setCursorCol(0);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tthis.setCursorCol(\n\t\t\tfindWordForward(currentLine, this.state.cursorCol, {\n\t\t\t\tsegment: (text) => this.segment(text, \"word\"),\n\t\t\t\tisAtomicSegment: isAtomicMarker,\n\t\t\t}),\n\t\t);\n\t}\n\n\t// Slash menu only allowed on the first line of the editor\n\tprivate isSlashMenuAllowed(): boolean {\n\t\treturn this.state.cursorLine === 0;\n\t}\n\n\t// Helper method to check if cursor is at start of message (for slash command detection)\n\tprivate isAtStartOfMessage(): boolean {\n\t\tif (!this.isSlashMenuAllowed()) return false;\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\tconst beforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\treturn beforeCursor.trim() === \"\" || beforeCursor.trim() === \"/\";\n\t}\n\n\tprivate isInSlashCommandContext(textBeforeCursor: string): boolean {\n\t\treturn this.isSlashMenuAllowed() && textBeforeCursor.trimStart().startsWith(\"/\");\n\t}\n\n\t// Autocomplete methods\n\t/**\n\t * Find the best autocomplete item index for the given prefix.\n\t * Returns -1 if no match is found.\n\t *\n\t * Match priority:\n\t * 1. Exact match (prefix === item.value) -> always selected\n\t * 2. Prefix match -> first item whose value starts with prefix\n\t * 3. No match -> -1 (keep default highlight)\n\t *\n\t * Matching is case-sensitive and checks item.value only.\n\t */\n\tprivate getBestAutocompleteMatchIndex(items: Array<{ value: string; label: string }>, prefix: string): number {\n\t\tif (!prefix) return -1;\n\n\t\tlet firstPrefixIndex = -1;\n\n\t\tfor (let i = 0; i < items.length; i++) {\n\t\t\tconst value = items[i]!.value;\n\t\t\tif (value === prefix) {\n\t\t\t\treturn i; // Exact match always wins\n\t\t\t}\n\t\t\tif (firstPrefixIndex === -1 && value.startsWith(prefix)) {\n\t\t\t\tfirstPrefixIndex = i;\n\t\t\t}\n\t\t}\n\n\t\treturn firstPrefixIndex;\n\t}\n\n\tprivate createAutocompleteList(\n\t\tprefix: string,\n\t\titems: Array<{ value: string; label: string; description?: string }>,\n\t): SelectList {\n\t\tconst layout = prefix.startsWith(\"/\") ? SLASH_COMMAND_SELECT_LIST_LAYOUT : undefined;\n\t\treturn new SelectList(items, this.autocompleteMaxVisible, this.theme.selectList, layout);\n\t}\n\n\tprivate tryTriggerAutocomplete(explicitTab: boolean = false): void {\n\t\tthis.requestAutocomplete({ force: false, explicitTab });\n\t}\n\n\tprivate handleTabCompletion(): void {\n\t\tif (!this.autocompleteProvider) return;\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\tconst beforeCursor = currentLine.slice(0, this.state.cursorCol);\n\n\t\tif (this.isInSlashCommandContext(beforeCursor) && !beforeCursor.trimStart().includes(\" \")) {\n\t\t\tthis.handleSlashCommandCompletion();\n\t\t} else {\n\t\t\tthis.forceFileAutocomplete(true);\n\t\t}\n\t}\n\n\tprivate handleSlashCommandCompletion(): void {\n\t\tthis.requestAutocomplete({ force: false, explicitTab: true });\n\t}\n\n\tprivate forceFileAutocomplete(explicitTab: boolean = false): void {\n\t\tthis.requestAutocomplete({ force: true, explicitTab });\n\t}\n\n\tprivate requestAutocomplete(options: { force: boolean; explicitTab: boolean }): void {\n\t\tif (!this.autocompleteProvider) return;\n\n\t\tif (options.force) {\n\t\t\tconst shouldTrigger =\n\t\t\t\t!this.autocompleteProvider.shouldTriggerFileCompletion ||\n\t\t\t\tthis.autocompleteProvider.shouldTriggerFileCompletion(\n\t\t\t\t\tthis.state.lines,\n\t\t\t\t\tthis.state.cursorLine,\n\t\t\t\t\tthis.state.cursorCol,\n\t\t\t\t);\n\t\t\tif (!shouldTrigger) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\tthis.cancelAutocompleteRequest();\n\t\tconst startToken = ++this.autocompleteStartToken;\n\n\t\tconst debounceMs = this.getAutocompleteDebounceMs(options);\n\t\tif (debounceMs > 0) {\n\t\t\tthis.autocompleteDebounceTimer = setTimeout(() => {\n\t\t\t\tthis.autocompleteDebounceTimer = undefined;\n\t\t\t\tvoid this.startAutocompleteRequest(startToken, options);\n\t\t\t}, debounceMs);\n\t\t\treturn;\n\t\t}\n\n\t\tvoid this.startAutocompleteRequest(startToken, options);\n\t}\n\n\tprivate async startAutocompleteRequest(\n\t\tstartToken: number,\n\t\toptions: { force: boolean; explicitTab: boolean },\n\t): Promise<void> {\n\t\tconst previousTask = this.autocompleteRequestTask;\n\t\tthis.autocompleteRequestTask = (async () => {\n\t\t\tawait previousTask;\n\t\t\tif (startToken !== this.autocompleteStartToken || !this.autocompleteProvider) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst controller = new AbortController();\n\t\t\tthis.autocompleteAbort = controller;\n\t\t\tconst requestId = ++this.autocompleteRequestId;\n\t\t\tconst snapshotText = this.getText();\n\t\t\tconst snapshotLine = this.state.cursorLine;\n\t\t\tconst snapshotCol = this.state.cursorCol;\n\n\t\t\tawait this.runAutocompleteRequest(requestId, controller, snapshotText, snapshotLine, snapshotCol, options);\n\t\t})();\n\t\tawait this.autocompleteRequestTask;\n\t}\n\n\tprivate setAutocompleteTriggerCharacters(triggerCharacters: string[]): void {\n\t\tconst next = [...DEFAULT_AUTOCOMPLETE_TRIGGER_CHARACTERS];\n\t\tfor (const character of triggerCharacters) {\n\t\t\tif (character.length !== 1 || character === \"/\" || isWhitespaceChar(character) || next.includes(character)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tnext.push(character);\n\t\t}\n\t\tthis.autocompleteTriggerCharacters = next;\n\t\tthis.autocompleteTriggerPattern = buildTriggerPattern(next);\n\t\tthis.autocompleteDebouncePattern = buildDebouncePattern(next);\n\t}\n\n\tprivate getAutocompleteDebounceMs(options: { force: boolean; explicitTab: boolean }): number {\n\t\tif (options.explicitTab || options.force) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\tconst textBeforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\treturn this.autocompleteDebouncePattern.test(textBeforeCursor) ? ATTACHMENT_AUTOCOMPLETE_DEBOUNCE_MS : 0;\n\t}\n\n\tprivate async runAutocompleteRequest(\n\t\trequestId: number,\n\t\tcontroller: AbortController,\n\t\tsnapshotText: string,\n\t\tsnapshotLine: number,\n\t\tsnapshotCol: number,\n\t\toptions: { force: boolean; explicitTab: boolean },\n\t): Promise<void> {\n\t\tif (!this.autocompleteProvider) return;\n\n\t\tconst suggestions = await this.autocompleteProvider.getSuggestions(\n\t\t\tthis.state.lines,\n\t\t\tthis.state.cursorLine,\n\t\t\tthis.state.cursorCol,\n\t\t\t{ signal: controller.signal, force: options.force },\n\t\t);\n\n\t\tif (!this.isAutocompleteRequestCurrent(requestId, controller, snapshotText, snapshotLine, snapshotCol)) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.autocompleteAbort = undefined;\n\n\t\tif (!suggestions || !Array.isArray(suggestions.items) || suggestions.items.length === 0) {\n\t\t\tthis.cancelAutocomplete();\n\t\t\tthis.tui.requestRender();\n\t\t\treturn;\n\t\t}\n\n\t\tif (options.force && options.explicitTab && suggestions.items.length === 1) {\n\t\t\tconst item = suggestions.items[0]!;\n\t\t\tthis.pushUndoSnapshot();\n\t\t\tthis.lastAction = null;\n\t\t\tconst result = this.autocompleteProvider.applyCompletion(\n\t\t\t\tthis.state.lines,\n\t\t\t\tthis.state.cursorLine,\n\t\t\t\tthis.state.cursorCol,\n\t\t\t\titem,\n\t\t\t\tsuggestions.prefix,\n\t\t\t);\n\t\t\tthis.state.lines = result.lines;\n\t\t\tthis.state.cursorLine = result.cursorLine;\n\t\t\tthis.setCursorCol(result.cursorCol);\n\t\t\tif (this.onChange) this.onChange(this.getText());\n\t\t\tthis.tui.requestRender();\n\t\t\treturn;\n\t\t}\n\n\t\tthis.applyAutocompleteSuggestions(suggestions, options.force ? \"force\" : \"regular\");\n\t\tthis.tui.requestRender();\n\t}\n\n\tprivate isAutocompleteRequestCurrent(\n\t\trequestId: number,\n\t\tcontroller: AbortController,\n\t\tsnapshotText: string,\n\t\tsnapshotLine: number,\n\t\tsnapshotCol: number,\n\t): boolean {\n\t\treturn (\n\t\t\t!controller.signal.aborted &&\n\t\t\trequestId === this.autocompleteRequestId &&\n\t\t\tthis.getText() === snapshotText &&\n\t\t\tthis.state.cursorLine === snapshotLine &&\n\t\t\tthis.state.cursorCol === snapshotCol\n\t\t);\n\t}\n\n\tprivate applyAutocompleteSuggestions(suggestions: AutocompleteSuggestions, state: \"regular\" | \"force\"): void {\n\t\tthis.autocompletePrefix = suggestions.prefix;\n\t\tthis.autocompleteList = this.createAutocompleteList(suggestions.prefix, suggestions.items);\n\n\t\tconst bestMatchIndex = this.getBestAutocompleteMatchIndex(suggestions.items, suggestions.prefix);\n\t\tif (bestMatchIndex >= 0) {\n\t\t\tthis.autocompleteList.setSelectedIndex(bestMatchIndex);\n\t\t}\n\n\t\tthis.autocompleteState = state;\n\t}\n\n\tprivate cancelAutocompleteRequest(): void {\n\t\tthis.autocompleteStartToken += 1;\n\t\tif (this.autocompleteDebounceTimer) {\n\t\t\tclearTimeout(this.autocompleteDebounceTimer);\n\t\t\tthis.autocompleteDebounceTimer = undefined;\n\t\t}\n\t\tthis.autocompleteAbort?.abort();\n\t\tthis.autocompleteAbort = undefined;\n\t}\n\n\tprivate clearAutocompleteUi(): void {\n\t\tthis.autocompleteState = null;\n\t\tthis.autocompleteList = undefined;\n\t\tthis.autocompletePrefix = \"\";\n\t}\n\n\tprivate cancelAutocomplete(): void {\n\t\tthis.cancelAutocompleteRequest();\n\t\tthis.clearAutocompleteUi();\n\t}\n\n\tpublic isShowingAutocomplete(): boolean {\n\t\treturn this.autocompleteState !== null;\n\t}\n\n\tprivate updateAutocomplete(): void {\n\t\tif (!this.autocompleteState || !this.autocompleteProvider) return;\n\t\tthis.requestAutocomplete({ force: this.autocompleteState === \"force\", explicitTab: false });\n\t}\n}\n"]}
1
+ {"version":3,"file":"editor.d.ts","sourceRoot":"","sources":["../../src/components/editor.ts"],"names":[],"mappings":"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","sourcesContent":["import type { AutocompleteProvider, AutocompleteSuggestions } from \"../autocomplete.ts\";\nimport { getKeybindings } from \"../keybindings.ts\";\nimport { decodePrintableKey, matchesKey } from \"../keys.ts\";\nimport { KillRing } from \"../kill-ring.ts\";\nimport { type Component, CURSOR_MARKER, type Focusable, type TUI } from \"../tui.ts\";\nimport { UndoStack } from \"../undo-stack.ts\";\nimport {\n\tcjkBreakRegex,\n\tgetGraphemeSegmenter,\n\tgetWordSegmenter,\n\tisWhitespaceChar,\n\ttruncateToWidth,\n\tvisibleWidth,\n} from \"../utils.ts\";\nimport { findWordBackward, findWordForward } from \"../word-navigation.ts\";\nimport { SelectList, type SelectListLayoutOptions, type SelectListTheme } from \"./select-list.ts\";\n\nconst graphemeSegmenter = getGraphemeSegmenter();\nconst wordSegmenter = getWordSegmenter();\n\n/** Regex matching paste markers like `[paste #1 +123 lines]` or `[paste #2 1234 chars]`. */\nconst PASTE_MARKER_REGEX = /\\[paste #(\\d+)( (\\+\\d+ lines|\\d+ chars))?\\]/g;\n\n/** Non-global version for single-segment testing. */\nconst PASTE_MARKER_SINGLE = /^\\[paste #(\\d+)( (\\+\\d+ lines|\\d+ chars))?\\]$/;\n\n/** Regex matching clipboard image chips like `[image_1]`. */\nexport const IMAGE_MARKER_REGEX = /\\[image_(\\d+)\\]/g;\n\n/** Non-global version for single-segment testing. */\nconst IMAGE_MARKER_SINGLE = /^\\[image_(\\d+)\\]$/;\n\nexport type EditorImageAttachment = {\n\tid: number;\n\tpath: string;\n\tmimeType: string;\n};\n\nexport function formatImageMarker(id: number): string {\n\treturn `[image_${id}]`;\n}\n\nexport function collectImageMarkerIds(text: string): number[] {\n\tconst ids: number[] = [];\n\tconst seen = new Set<number>();\n\tfor (const match of text.matchAll(IMAGE_MARKER_REGEX)) {\n\t\tconst id = Number.parseInt(match[1]!, 10);\n\t\tif (seen.has(id)) continue;\n\t\tseen.add(id);\n\t\tids.push(id);\n\t}\n\treturn ids;\n}\n\n/** Check if a segment is a paste marker (i.e. was merged by segmentWithMarkers). */\nfunction isPasteMarker(segment: string): boolean {\n\treturn segment.length >= 10 && PASTE_MARKER_SINGLE.test(segment);\n}\n\nfunction isImageMarker(segment: string): boolean {\n\treturn segment.length >= 9 && IMAGE_MARKER_SINGLE.test(segment);\n}\n\nfunction isAtomicMarker(segment: string): boolean {\n\treturn isPasteMarker(segment) || isImageMarker(segment);\n}\n\ntype MarkerFamily = {\n\tregex: RegExp;\n\tvalidIds: Set<number>;\n\tneedle: string;\n};\n\n/**\n * A segmenter that wraps Intl.Segmenter and merges graphemes that fall\n * within paste/image markers into single atomic segments. This makes cursor\n * movement, deletion, word-wrap, etc. treat those markers as single units.\n *\n * Only markers whose numeric ID exists in a family's `validIds` are merged.\n */\nfunction segmentWithMarkers(\n\ttext: string,\n\tbaseSegmenter: Intl.Segmenter,\n\tfamilies: MarkerFamily[],\n): Iterable<Intl.SegmentData> {\n\tconst markers: Array<{ start: number; end: number }> = [];\n\tfor (const family of families) {\n\t\tif (family.validIds.size === 0 || !text.includes(family.needle)) continue;\n\t\tfor (const m of text.matchAll(family.regex)) {\n\t\t\tconst id = Number.parseInt(m[1]!, 10);\n\t\t\tif (!family.validIds.has(id)) continue;\n\t\t\tmarkers.push({ start: m.index, end: m.index + m[0].length });\n\t\t}\n\t}\n\tif (markers.length === 0) {\n\t\treturn baseSegmenter.segment(text);\n\t}\n\tmarkers.sort((a, b) => a.start - b.start || a.end - b.end);\n\n\t// Build merged segment list.\n\tconst baseSegments = baseSegmenter.segment(text);\n\tconst result: Intl.SegmentData[] = [];\n\tlet markerIdx = 0;\n\n\tfor (const seg of baseSegments) {\n\t\t// Skip past markers that are entirely before this segment.\n\t\twhile (markerIdx < markers.length && markers[markerIdx]!.end <= seg.index) {\n\t\t\tmarkerIdx++;\n\t\t}\n\n\t\tconst marker = markerIdx < markers.length ? markers[markerIdx]! : null;\n\n\t\tif (marker && seg.index >= marker.start && seg.index < marker.end) {\n\t\t\t// This segment falls inside a marker.\n\t\t\t// If this is the first segment of the marker, emit a merged segment.\n\t\t\tif (seg.index === marker.start) {\n\t\t\t\tconst markerText = text.slice(marker.start, marker.end);\n\t\t\t\tresult.push({\n\t\t\t\t\tsegment: markerText,\n\t\t\t\t\tindex: marker.start,\n\t\t\t\t\tinput: text,\n\t\t\t\t});\n\t\t\t}\n\t\t\t// Otherwise skip (already merged into the first segment).\n\t\t} else {\n\t\t\tresult.push(seg);\n\t\t}\n\t}\n\n\treturn result;\n}\n\n/**\n * Represents a chunk of text for word-wrap layout.\n * Tracks display text and its position in the original line. An indivisible\n * grapheme that cannot fit uses a placeholder with the original source range.\n */\nexport interface TextChunk {\n\ttext: string;\n\tstartIndex: number;\n\tendIndex: number;\n}\n\n/**\n * Split a line into word-wrapped chunks.\n * Wraps at word boundaries when possible, falling back to character-level\n * wrapping for words longer than the available width.\n *\n * @param line - The text line to wrap\n * @param maxWidth - Maximum visible width per chunk\n * @param preSegmented - Optional pre-segmented graphemes (e.g. with paste-marker awareness).\n * When omitted the default Intl.Segmenter is used.\n * @returns Array of chunks with text and position information\n */\nexport function wordWrapLine(line: string, maxWidth: number, preSegmented?: Intl.SegmentData[]): TextChunk[] {\n\tif (!line || maxWidth <= 0) {\n\t\treturn [{ text: \"\", startIndex: 0, endIndex: line.length }];\n\t}\n\n\tconst lineWidth = visibleWidth(line);\n\tif (lineWidth <= maxWidth) {\n\t\treturn [{ text: line, startIndex: 0, endIndex: line.length }];\n\t}\n\n\tconst chunks: TextChunk[] = [];\n\tconst segments = preSegmented ?? [...graphemeSegmenter.segment(line)];\n\n\tlet currentWidth = 0;\n\tlet chunkStart = 0;\n\n\t// Wrap opportunity: the position after the last whitespace before a non-whitespace\n\t// grapheme, i.e. where a line break is allowed.\n\tlet wrapOppIndex = -1;\n\tlet wrapOppWidth = 0;\n\n\tfor (let i = 0; i < segments.length; i++) {\n\t\tconst seg = segments[i]!;\n\t\tconst grapheme = seg.segment;\n\t\tconst gWidth = visibleWidth(grapheme);\n\t\tconst charIndex = seg.index;\n\t\tconst isWs = !isAtomicMarker(grapheme) && isWhitespaceChar(grapheme);\n\n\t\t// Overflow check before advancing.\n\t\tif (currentWidth + gWidth > maxWidth) {\n\t\t\tif (wrapOppIndex >= 0 && currentWidth - wrapOppWidth + gWidth <= maxWidth) {\n\t\t\t\t// Backtrack to last wrap opportunity (the remaining content\n\t\t\t\t// plus the current grapheme still fits within maxWidth).\n\t\t\t\tchunks.push({ text: line.slice(chunkStart, wrapOppIndex), startIndex: chunkStart, endIndex: wrapOppIndex });\n\t\t\t\tchunkStart = wrapOppIndex;\n\t\t\t\tcurrentWidth -= wrapOppWidth;\n\t\t\t} else if (chunkStart < charIndex) {\n\t\t\t\t// No viable wrap opportunity: force-break at current position.\n\t\t\t\t// This also handles the case where backtracking to a word\n\t\t\t\t// boundary wouldn't help because the remaining content plus\n\t\t\t\t// the current grapheme (e.g. a wide character) still exceeds\n\t\t\t\t// maxWidth.\n\t\t\t\tchunks.push({ text: line.slice(chunkStart, charIndex), startIndex: chunkStart, endIndex: charIndex });\n\t\t\t\tchunkStart = charIndex;\n\t\t\t\tcurrentWidth = 0;\n\t\t\t}\n\t\t\twrapOppIndex = -1;\n\t\t}\n\n\t\tif (gWidth > maxWidth) {\n\t\t\tconst subSegments = [...graphemeSegmenter.segment(grapheme)];\n\t\t\tif (subSegments.length === 1) {\n\t\t\t\t// Keep the source range even when this grapheme cannot occupy its two cells.\n\t\t\t\tchunks.push({ text: \"�\", startIndex: charIndex, endIndex: charIndex + grapheme.length });\n\t\t\t\tchunkStart = charIndex + grapheme.length;\n\t\t\t\tcurrentWidth = 0;\n\t\t\t\twrapOppIndex = -1;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\t// Split a multi-grapheme atomic marker visually, while keeping editing atomic.\n\t\t\tconst subChunks = wordWrapLine(grapheme, maxWidth, subSegments);\n\t\t\tfor (let j = 0; j < subChunks.length - 1; j++) {\n\t\t\t\tconst sc = subChunks[j]!;\n\t\t\t\tchunks.push({ text: sc.text, startIndex: charIndex + sc.startIndex, endIndex: charIndex + sc.endIndex });\n\t\t\t}\n\t\t\tconst last = subChunks[subChunks.length - 1]!;\n\t\t\tchunkStart = charIndex + last.startIndex;\n\t\t\tcurrentWidth = visibleWidth(last.text);\n\t\t\twrapOppIndex = -1;\n\t\t\tcontinue;\n\t\t}\n\n\t\t// Advance.\n\t\tcurrentWidth += gWidth;\n\n\t\t// Record wrap opportunity: whitespace followed by non-whitespace\n\t\t// (multiple spaces join; the break point is after the last space),\n\t\t// or at a boundary where either side is CJK (CJK allows breaking\n\t\t// between any adjacent characters).\n\t\tconst next = segments[i + 1];\n\t\tif (isWs && next && (isAtomicMarker(next.segment) || !isWhitespaceChar(next.segment))) {\n\t\t\twrapOppIndex = next.index;\n\t\t\twrapOppWidth = currentWidth;\n\t\t} else if (!isWs && next && !isWhitespaceChar(next.segment)) {\n\t\t\tconst isCjk = !isAtomicMarker(grapheme) && cjkBreakRegex.test(grapheme);\n\t\t\tconst nextIsCjk = !isAtomicMarker(next.segment) && cjkBreakRegex.test(next.segment);\n\t\t\tif (isCjk || nextIsCjk) {\n\t\t\t\twrapOppIndex = next.index;\n\t\t\t\twrapOppWidth = currentWidth;\n\t\t\t}\n\t\t}\n\t}\n\n\t// Push final chunk.\n\tif (chunkStart < line.length || chunks.length === 0) {\n\t\tchunks.push({ text: line.slice(chunkStart), startIndex: chunkStart, endIndex: line.length });\n\t}\n\n\treturn chunks;\n}\n\n// Kitty CSI-u sequences for printable keys, including optional shifted/base codepoints.\ninterface EditorState {\n\tlines: string[];\n\tcursorLine: number;\n\tcursorCol: number;\n}\n\ninterface EditorSnapshot {\n\tstate: EditorState;\n\timages: ReadonlyMap<number, EditorImageAttachment>;\n}\n\ninterface LayoutLine {\n\ttext: string;\n\thasCursor: boolean;\n\tcursorPos?: number;\n}\n\nexport interface EditorTheme {\n\tborderColor: (str: string) => string;\n\tselectList: SelectListTheme;\n}\n\nexport interface EditorOptions {\n\tpaddingX?: number;\n\tautocompleteMaxVisible?: number;\n}\n\nconst SLASH_COMMAND_SELECT_LIST_LAYOUT: SelectListLayoutOptions = {\n\tminPrimaryColumnWidth: 12,\n\tmaxPrimaryColumnWidth: 32,\n};\n\nconst ATTACHMENT_AUTOCOMPLETE_DEBOUNCE_MS = 20;\nconst DEFAULT_AUTOCOMPLETE_TRIGGER_CHARACTERS = [\"@\", \"#\"];\n\nfunction escapeCharacterClass(value: string): string {\n\treturn value.replace(/[\\\\^$.*+?()[\\]{}|-]/g, \"\\\\$&\");\n}\n\nfunction buildTriggerPattern(triggerCharacters: string[]): RegExp {\n\treturn new RegExp(`(?:^|[\\\\s])[${triggerCharacters.map(escapeCharacterClass).join(\"\")}][^\\\\s]*$`);\n}\n\nfunction buildDebouncePattern(triggerCharacters: string[]): RegExp {\n\tconst escapedWithoutAt = triggerCharacters.filter((character) => character !== \"@\").map(escapeCharacterClass);\n\treturn new RegExp(`(?:^|[ \\\\t])(?:@(?:\"[^\"]*|[^\\\\s]*)|[${escapedWithoutAt.join(\"\")}][^\\\\s]*)$`);\n}\n\nexport class Editor implements Component, Focusable {\n\tprivate state: EditorState = {\n\t\tlines: [\"\"],\n\t\tcursorLine: 0,\n\t\tcursorCol: 0,\n\t};\n\n\t/** Focusable interface - set by TUI when focus changes */\n\tfocused: boolean = false;\n\n\tprotected tui: TUI;\n\tprivate theme: EditorTheme;\n\tprivate paddingX: number = 0;\n\n\t// Store last render width for cursor navigation\n\tprivate lastWidth: number = 80;\n\n\t// Vertical scrolling support\n\tprivate scrollOffset: number = 0;\n\n\t// Border color (can be changed dynamically)\n\tpublic borderColor: (str: string) => string;\n\n\t// Autocomplete support\n\tprivate autocompleteProvider?: AutocompleteProvider;\n\tprivate autocompleteTriggerCharacters = [...DEFAULT_AUTOCOMPLETE_TRIGGER_CHARACTERS];\n\tprivate autocompleteTriggerPattern = buildTriggerPattern(this.autocompleteTriggerCharacters);\n\tprivate autocompleteDebouncePattern = buildDebouncePattern(this.autocompleteTriggerCharacters);\n\tprivate autocompleteList?: SelectList;\n\tprivate autocompleteState: \"regular\" | \"force\" | null = null;\n\tprivate autocompletePrefix: string = \"\";\n\tprivate autocompleteMaxVisible: number = 5;\n\tprivate autocompleteAbort?: AbortController;\n\tprivate autocompleteDebounceTimer?: ReturnType<typeof setTimeout>;\n\tprivate autocompleteRequestTask: Promise<void> = Promise.resolve();\n\tprivate autocompleteStartToken: number = 0;\n\tprivate autocompleteRequestId: number = 0;\n\n\t// Paste tracking for large pastes\n\tprivate pastes: Map<number, string> = new Map();\n\tprivate pasteCounter: number = 0;\n\n\t// Clipboard image chips: `[image_n]` stays in the editor; bytes live off-screen.\n\tprivate images: ReadonlyMap<number, EditorImageAttachment> = new Map();\n\tprivate imageCounter: number = 0;\n\tprivate submittedImages: EditorImageAttachment[] = [];\n\n\t// Bracketed paste mode buffering\n\tprivate pasteBuffer: string = \"\";\n\tprivate isInPaste: boolean = false;\n\n\t// Prompt history for up/down navigation\n\tprivate history: string[] = [];\n\tprivate historyIndex: number = -1; // -1 = not browsing, 0 = most recent, 1 = older, etc.\n\tprivate historyDraft: EditorState | null = null;\n\n\t// Kill ring for Emacs-style kill/yank operations\n\tprivate killRing = new KillRing();\n\tprivate lastAction: \"kill\" | \"yank\" | \"type-word\" | null = null;\n\n\t// Character jump mode\n\tprivate jumpMode: \"forward\" | \"backward\" | null = null;\n\n\t// Preferred visual column for vertical cursor movement (sticky column)\n\tprivate preferredVisualCol: number | null = null;\n\n\t// When the cursor is snapped to the start of an atomic segment, e.g. a\n\t// paste marker, cursorCol no longer reflects where the cursor would have\n\t// landed. This field stores the pre-snap cursorCol so that the next\n\t// vertical move can resolve it to a visual column on whatever VL it belongs\n\t// to.\n\tprivate snappedFromCursorCol: number | null = null;\n\n\t// Undo support\n\t// Attachment maps are copied only when membership changes; snapshots share file metadata.\n\tprivate undoStack = new UndoStack<EditorSnapshot>((snapshot) => ({\n\t\tstate: structuredClone(snapshot.state),\n\t\timages: snapshot.images,\n\t}));\n\n\tpublic onSubmit?: (text: string) => void;\n\tpublic onChange?: (text: string) => void;\n\tpublic disableSubmit: boolean = false;\n\n\tconstructor(tui: TUI, theme: EditorTheme, options: EditorOptions = {}) {\n\t\tthis.tui = tui;\n\t\tthis.theme = theme;\n\t\tthis.borderColor = theme.borderColor;\n\t\tconst paddingX = options.paddingX ?? 0;\n\t\tthis.paddingX = Number.isFinite(paddingX) ? Math.max(0, Math.floor(paddingX)) : 0;\n\t\tconst maxVisible = options.autocompleteMaxVisible ?? 5;\n\t\tthis.autocompleteMaxVisible = Number.isFinite(maxVisible) ? Math.max(3, Math.min(20, Math.floor(maxVisible))) : 5;\n\t}\n\n\t/** Set of currently valid paste IDs, for marker-aware segmentation. */\n\tprivate validPasteIds(): Set<number> {\n\t\treturn new Set(this.pastes.keys());\n\t}\n\n\t/** Set of currently valid image IDs, for marker-aware segmentation. */\n\tprivate validImageIds(): Set<number> {\n\t\treturn new Set(this.images.keys());\n\t}\n\n\tprivate markerFamilies(): MarkerFamily[] {\n\t\treturn [\n\t\t\t{ regex: PASTE_MARKER_REGEX, validIds: this.validPasteIds(), needle: \"[paste #\" },\n\t\t\t{ regex: IMAGE_MARKER_REGEX, validIds: this.validImageIds(), needle: \"[image_\" },\n\t\t];\n\t}\n\n\t/** Segment text with paste/image-marker awareness, only merging markers with valid IDs. */\n\tprivate segment(text: string, mode: \"word\" | \"grapheme\"): Iterable<Intl.SegmentData> {\n\t\treturn segmentWithMarkers(text, mode === \"word\" ? wordSegmenter : graphemeSegmenter, this.markerFamilies());\n\t}\n\n\tgetPaddingX(): number {\n\t\treturn this.paddingX;\n\t}\n\n\tsetPaddingX(padding: number): void {\n\t\tconst newPadding = Number.isFinite(padding) ? Math.max(0, Math.floor(padding)) : 0;\n\t\tif (this.paddingX !== newPadding) {\n\t\t\tthis.paddingX = newPadding;\n\t\t\tthis.tui.requestRender();\n\t\t}\n\t}\n\n\tgetAutocompleteMaxVisible(): number {\n\t\treturn this.autocompleteMaxVisible;\n\t}\n\n\tsetAutocompleteMaxVisible(maxVisible: number): void {\n\t\tconst newMaxVisible = Number.isFinite(maxVisible) ? Math.max(3, Math.min(20, Math.floor(maxVisible))) : 5;\n\t\tif (this.autocompleteMaxVisible !== newMaxVisible) {\n\t\t\tthis.autocompleteMaxVisible = newMaxVisible;\n\t\t\tthis.tui.requestRender();\n\t\t}\n\t}\n\n\tsetAutocompleteProvider(provider: AutocompleteProvider): void {\n\t\tthis.cancelAutocomplete();\n\t\tthis.autocompleteProvider = provider;\n\t\tthis.setAutocompleteTriggerCharacters(provider.triggerCharacters ?? []);\n\t}\n\n\t/**\n\t * Add a prompt to history for up/down arrow navigation.\n\t * Called after successful submission.\n\t */\n\taddToHistory(text: string): void {\n\t\tconst trimmed = text.trim();\n\t\tif (!trimmed) return;\n\t\t// Don't add consecutive duplicates\n\t\tif (this.history.length > 0 && this.history[0] === trimmed) return;\n\t\tthis.history.unshift(trimmed);\n\t\t// Limit history size\n\t\tif (this.history.length > 100) {\n\t\t\tthis.history.pop();\n\t\t}\n\t}\n\n\tprivate isEditorEmpty(): boolean {\n\t\treturn this.state.lines.length === 1 && this.state.lines[0] === \"\";\n\t}\n\n\tprivate isOnFirstVisualLine(): boolean {\n\t\tconst visualLines = this.buildVisualLineMap(this.lastWidth);\n\t\tconst currentVisualLine = this.findCurrentVisualLine(visualLines);\n\t\treturn currentVisualLine === 0;\n\t}\n\n\tprivate isOnLastVisualLine(): boolean {\n\t\tconst visualLines = this.buildVisualLineMap(this.lastWidth);\n\t\tconst currentVisualLine = this.findCurrentVisualLine(visualLines);\n\t\treturn currentVisualLine === visualLines.length - 1;\n\t}\n\n\tprivate navigateHistory(direction: 1 | -1): void {\n\t\tthis.lastAction = null;\n\t\tif (this.history.length === 0) return;\n\n\t\tconst newIndex = this.historyIndex - direction; // Up(-1) increases index, Down(1) decreases\n\t\tif (newIndex < -1 || newIndex >= this.history.length) return;\n\n\t\t// Capture state when first entering history browsing mode\n\t\tif (this.historyIndex === -1 && newIndex >= 0) {\n\t\t\tthis.pushUndoSnapshot();\n\t\t\tthis.historyDraft = structuredClone(this.state);\n\t\t}\n\n\t\tthis.historyIndex = newIndex;\n\n\t\tif (this.historyIndex === -1) {\n\t\t\tconst draft = this.historyDraft;\n\t\t\tthis.historyDraft = null;\n\t\t\tif (draft) {\n\t\t\t\tthis.state = draft;\n\t\t\t\tthis.preferredVisualCol = null;\n\t\t\t\tthis.snappedFromCursorCol = null;\n\t\t\t\tthis.scrollOffset = 0;\n\t\t\t\tif (this.onChange) this.onChange(this.getText());\n\t\t\t} else {\n\t\t\t\tthis.setTextInternal(\"\");\n\t\t\t}\n\t\t} else {\n\t\t\tthis.setTextInternal(this.history[this.historyIndex] || \"\", direction === -1 ? \"start\" : \"end\");\n\t\t}\n\t}\n\n\tprivate exitHistoryBrowsing(): void {\n\t\tthis.historyIndex = -1;\n\t\tthis.historyDraft = null;\n\t}\n\n\t/** Internal setText that doesn't reset history state - used by navigateHistory */\n\tprivate setTextInternal(text: string, cursorPlacement: \"start\" | \"end\" = \"end\"): void {\n\t\tconst lines = text.split(\"\\n\");\n\t\tthis.state.lines = lines.length === 0 ? [\"\"] : lines;\n\t\tthis.state.cursorLine = cursorPlacement === \"start\" ? 0 : this.state.lines.length - 1;\n\t\tthis.setCursorCol(cursorPlacement === \"start\" ? 0 : this.state.lines[this.state.cursorLine]?.length || 0);\n\t\t// Reset scroll - render() will adjust to show cursor\n\t\tthis.scrollOffset = 0;\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tinvalidate(): void {\n\t\t// No cached state to invalidate currently\n\t}\n\n\trender(width: number): string[] {\n\t\tif (width <= 0) return [];\n\t\tconst maxPadding = Math.max(0, Math.floor((width - 1) / 2));\n\t\tconst paddingX = Math.min(this.paddingX, maxPadding);\n\t\tconst contentWidth = Math.max(1, width - paddingX * 2);\n\n\t\t// Layout width: with padding the cursor can overflow into it,\n\t\t// without padding we reserve 1 column for the cursor.\n\t\tconst layoutWidth = Math.max(1, contentWidth - (paddingX ? 0 : 1));\n\n\t\t// Store for cursor navigation (must match wrapping width)\n\t\tthis.lastWidth = layoutWidth;\n\n\t\tconst horizontal = this.borderColor(\"─\");\n\n\t\t// Layout the text\n\t\tconst layoutLines = this.layoutText(layoutWidth);\n\n\t\t// Calculate max visible lines: 30% of terminal height, minimum 5 lines\n\t\tconst terminalRows = this.tui.terminal.rows;\n\t\tconst maxVisibleLines = Math.max(5, Math.floor(terminalRows * 0.3));\n\n\t\t// Find the cursor line index in layoutLines\n\t\tlet cursorLineIndex = layoutLines.findIndex((line) => line.hasCursor);\n\t\tif (cursorLineIndex === -1) cursorLineIndex = 0;\n\n\t\t// Adjust scroll offset to keep cursor visible\n\t\tif (cursorLineIndex < this.scrollOffset) {\n\t\t\tthis.scrollOffset = cursorLineIndex;\n\t\t} else if (cursorLineIndex >= this.scrollOffset + maxVisibleLines) {\n\t\t\tthis.scrollOffset = cursorLineIndex - maxVisibleLines + 1;\n\t\t}\n\n\t\t// Clamp scroll offset to valid range\n\t\tconst maxScrollOffset = Math.max(0, layoutLines.length - maxVisibleLines);\n\t\tthis.scrollOffset = Math.max(0, Math.min(this.scrollOffset, maxScrollOffset));\n\n\t\t// Get visible lines slice\n\t\tconst visibleLines = layoutLines.slice(this.scrollOffset, this.scrollOffset + maxVisibleLines);\n\n\t\tconst result: string[] = [];\n\t\tconst leftPadding = \" \".repeat(paddingX);\n\t\tconst rightPadding = leftPadding;\n\n\t\t// Render top border (with scroll indicator if scrolled down)\n\t\tif (this.scrollOffset > 0) {\n\t\t\tconst indicator = `─── ↑ ${this.scrollOffset} more `;\n\t\t\tconst remaining = width - visibleWidth(indicator);\n\t\t\tif (remaining >= 0) {\n\t\t\t\tresult.push(this.borderColor(indicator + \"─\".repeat(remaining)));\n\t\t\t} else {\n\t\t\t\tresult.push(this.borderColor(truncateToWidth(indicator, width)));\n\t\t\t}\n\t\t} else {\n\t\t\tresult.push(horizontal.repeat(width));\n\t\t}\n\n\t\t// Render each visible layout line\n\t\t// Emit hardware cursor marker when focused so TUI can position the\n\t\t// hardware cursor for IME candidate-window placement even while\n\t\t// autocomplete (e.g. slash-command menu) is visible.\n\t\tconst emitCursorMarker = this.focused;\n\n\t\tfor (const layoutLine of visibleLines) {\n\t\t\tlet displayText = layoutLine.text;\n\t\t\tlet lineVisibleWidth = visibleWidth(layoutLine.text);\n\t\t\tlet cursorInPadding = false;\n\n\t\t\t// Add cursor if this line has it\n\t\t\tif (layoutLine.hasCursor && layoutLine.cursorPos !== undefined) {\n\t\t\t\tconst before = displayText.slice(0, layoutLine.cursorPos);\n\t\t\t\tconst after = displayText.slice(layoutLine.cursorPos);\n\n\t\t\t\t// Hardware cursor marker (zero-width, emitted before fake cursor for IME positioning)\n\t\t\t\tconst marker = emitCursorMarker ? CURSOR_MARKER : \"\";\n\n\t\t\t\tif (after.length > 0) {\n\t\t\t\t\t// Cursor is on a character (grapheme) - replace it with highlighted version\n\t\t\t\t\t// Get the first grapheme from 'after'\n\t\t\t\t\tconst afterGraphemes = [...this.segment(after, \"grapheme\")];\n\t\t\t\t\tconst firstGrapheme = afterGraphemes[0]?.segment || \"\";\n\t\t\t\t\tconst restAfter = after.slice(firstGrapheme.length);\n\t\t\t\t\tconst cursor = `\\x1b[7m${firstGrapheme}\\x1b[0m`;\n\t\t\t\t\tdisplayText = before + marker + cursor + restAfter;\n\t\t\t\t\t// lineVisibleWidth stays the same - we're replacing, not adding\n\t\t\t\t} else {\n\t\t\t\t\t// Cursor is at the end - add highlighted space\n\t\t\t\t\tconst cursor = \"\\x1b[7m \\x1b[0m\";\n\t\t\t\t\tdisplayText = before + marker + cursor;\n\t\t\t\t\tlineVisibleWidth = lineVisibleWidth + 1;\n\t\t\t\t\t// If cursor overflows content width into the padding, flag it\n\t\t\t\t\tif (lineVisibleWidth > contentWidth && paddingX > 0) {\n\t\t\t\t\t\tcursorInPadding = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Calculate padding based on actual visible width\n\t\t\tconst padding = \" \".repeat(Math.max(0, contentWidth - lineVisibleWidth));\n\t\t\tconst lineRightPadding = cursorInPadding ? rightPadding.slice(1) : rightPadding;\n\n\t\t\t// Render the line (no side borders, just horizontal lines above and below)\n\t\t\tresult.push(`${leftPadding}${displayText}${padding}${lineRightPadding}`);\n\t\t}\n\n\t\t// Render bottom border (with scroll indicator if more content below)\n\t\tconst linesBelow = layoutLines.length - (this.scrollOffset + visibleLines.length);\n\t\tif (linesBelow > 0) {\n\t\t\tconst indicator = `─── ↓ ${linesBelow} more `;\n\t\t\tconst remaining = width - visibleWidth(indicator);\n\t\t\tresult.push(this.borderColor(truncateToWidth(indicator + \"─\".repeat(Math.max(0, remaining)), width, \"\")));\n\t\t} else {\n\t\t\tresult.push(horizontal.repeat(width));\n\t\t}\n\n\t\t// Add autocomplete list if active\n\t\tif (this.autocompleteState && this.autocompleteList) {\n\t\t\tconst autocompleteResult = this.autocompleteList.render(contentWidth);\n\t\t\tfor (const line of autocompleteResult) {\n\t\t\t\tconst lineWidth = visibleWidth(line);\n\t\t\t\tconst linePadding = \" \".repeat(Math.max(0, contentWidth - lineWidth));\n\t\t\t\tresult.push(`${leftPadding}${line}${linePadding}${rightPadding}`);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\thandleInput(data: string): void {\n\t\tconst kb = getKeybindings();\n\n\t\t// Handle character jump mode (awaiting next character to jump to)\n\t\tif (this.jumpMode !== null) {\n\t\t\t// Cancel if the hotkey is pressed again\n\t\t\tif (kb.matches(data, \"tui.editor.jumpForward\") || kb.matches(data, \"tui.editor.jumpBackward\")) {\n\t\t\t\tthis.jumpMode = null;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst printable = decodePrintableKey(data) ?? (data.charCodeAt(0) >= 32 ? data : undefined);\n\t\t\tif (printable !== undefined) {\n\t\t\t\t// Printable character - perform the jump\n\t\t\t\tconst direction = this.jumpMode;\n\t\t\t\tthis.jumpMode = null;\n\t\t\t\tthis.jumpToChar(printable, direction);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// Control character - cancel and fall through to normal handling\n\t\t\tthis.jumpMode = null;\n\t\t}\n\n\t\t// Handle bracketed paste mode\n\t\tif (data.includes(\"\\x1b[200~\")) {\n\t\t\tthis.isInPaste = true;\n\t\t\tthis.pasteBuffer = \"\";\n\t\t\tdata = data.replace(\"\\x1b[200~\", \"\");\n\t\t}\n\n\t\tif (this.isInPaste) {\n\t\t\tthis.pasteBuffer += data;\n\t\t\tconst endIndex = this.pasteBuffer.indexOf(\"\\x1b[201~\");\n\t\t\tif (endIndex !== -1) {\n\t\t\t\tconst pasteContent = this.pasteBuffer.substring(0, endIndex);\n\t\t\t\tif (pasteContent.length > 0) {\n\t\t\t\t\tthis.handlePaste(pasteContent);\n\t\t\t\t}\n\t\t\t\tthis.isInPaste = false;\n\t\t\t\tconst remaining = this.pasteBuffer.substring(endIndex + 6);\n\t\t\t\tthis.pasteBuffer = \"\";\n\t\t\t\tif (remaining.length > 0) {\n\t\t\t\t\tthis.handleInput(remaining);\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\t// Ctrl+C - let parent handle (exit/clear)\n\t\tif (kb.matches(data, \"tui.input.copy\")) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Undo\n\t\tif (kb.matches(data, \"tui.editor.undo\")) {\n\t\t\tthis.undo();\n\t\t\treturn;\n\t\t}\n\n\t\t// Handle autocomplete mode\n\t\tif (this.autocompleteState && this.autocompleteList) {\n\t\t\tif (kb.matches(data, \"tui.select.cancel\")) {\n\t\t\t\tthis.cancelAutocomplete();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (kb.matches(data, \"tui.select.up\") || kb.matches(data, \"tui.select.down\")) {\n\t\t\t\tthis.autocompleteList.handleInput(data);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (kb.matches(data, \"tui.input.tab\")) {\n\t\t\t\tconst selected = this.autocompleteList.getSelectedItem();\n\t\t\t\tif (selected && this.autocompleteProvider) {\n\t\t\t\t\tthis.pushUndoSnapshot();\n\t\t\t\t\tthis.lastAction = null;\n\t\t\t\t\tconst result = this.autocompleteProvider.applyCompletion(\n\t\t\t\t\t\tthis.state.lines,\n\t\t\t\t\t\tthis.state.cursorLine,\n\t\t\t\t\t\tthis.state.cursorCol,\n\t\t\t\t\t\tselected,\n\t\t\t\t\t\tthis.autocompletePrefix,\n\t\t\t\t\t);\n\t\t\t\t\tthis.state.lines = result.lines;\n\t\t\t\t\tthis.state.cursorLine = result.cursorLine;\n\t\t\t\t\tthis.setCursorCol(result.cursorCol);\n\t\t\t\t\tthis.cancelAutocomplete();\n\t\t\t\t\tif (this.onChange) this.onChange(this.getText());\n\t\t\t\t}\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (kb.matches(data, \"tui.select.confirm\")) {\n\t\t\t\tconst selected = this.autocompleteList.getSelectedItem();\n\t\t\t\tif (selected && this.autocompleteProvider) {\n\t\t\t\t\tthis.pushUndoSnapshot();\n\t\t\t\t\tthis.lastAction = null;\n\t\t\t\t\tconst result = this.autocompleteProvider.applyCompletion(\n\t\t\t\t\t\tthis.state.lines,\n\t\t\t\t\t\tthis.state.cursorLine,\n\t\t\t\t\t\tthis.state.cursorCol,\n\t\t\t\t\t\tselected,\n\t\t\t\t\t\tthis.autocompletePrefix,\n\t\t\t\t\t);\n\t\t\t\t\tthis.state.lines = result.lines;\n\t\t\t\t\tthis.state.cursorLine = result.cursorLine;\n\t\t\t\t\tthis.setCursorCol(result.cursorCol);\n\n\t\t\t\t\tif (this.autocompletePrefix.startsWith(\"/\")) {\n\t\t\t\t\t\tthis.cancelAutocomplete();\n\t\t\t\t\t\t// Fall through to submit\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthis.cancelAutocomplete();\n\t\t\t\t\t\tif (this.onChange) this.onChange(this.getText());\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Tab - trigger completion\n\t\tif (kb.matches(data, \"tui.input.tab\") && !this.autocompleteState) {\n\t\t\tthis.handleTabCompletion();\n\t\t\treturn;\n\t\t}\n\n\t\t// Deletion actions\n\t\tif (kb.matches(data, \"tui.editor.deleteToLineEnd\")) {\n\t\t\tthis.deleteToEndOfLine();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.deleteToLineStart\")) {\n\t\t\tthis.deleteToStartOfLine();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.deleteWordBackward\")) {\n\t\t\tthis.deleteWordBackwards();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.deleteWordForward\")) {\n\t\t\tthis.deleteWordForward();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.deleteCharBackward\") || matchesKey(data, \"shift+backspace\")) {\n\t\t\tthis.handleBackspace();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.deleteCharForward\") || matchesKey(data, \"shift+delete\")) {\n\t\t\tthis.handleForwardDelete();\n\t\t\treturn;\n\t\t}\n\n\t\t// Kill ring actions\n\t\tif (kb.matches(data, \"tui.editor.yank\")) {\n\t\t\tthis.yank();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.yankPop\")) {\n\t\t\tthis.yankPop();\n\t\t\treturn;\n\t\t}\n\n\t\t// Cursor movement actions\n\t\tif (kb.matches(data, \"tui.editor.cursorLineStart\")) {\n\t\t\tthis.moveToLineStart();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.cursorLineEnd\")) {\n\t\t\tthis.moveToLineEnd();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.cursorWordLeft\")) {\n\t\t\tthis.moveWordBackwards();\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.cursorWordRight\")) {\n\t\t\tthis.moveWordForwards();\n\t\t\treturn;\n\t\t}\n\n\t\t// New line\n\t\tif (\n\t\t\tkb.matches(data, \"tui.input.newLine\") ||\n\t\t\t(data.charCodeAt(0) === 10 && data.length > 1) ||\n\t\t\tdata === \"\\x1b\\r\" ||\n\t\t\tdata === \"\\x1b[13;2~\" ||\n\t\t\t(data.length > 1 && data.includes(\"\\x1b\") && data.includes(\"\\r\")) ||\n\t\t\t(data === \"\\n\" && data.length === 1)\n\t\t) {\n\t\t\tif (this.shouldSubmitOnBackslashEnter(data, kb)) {\n\t\t\t\tthis.handleBackspace();\n\t\t\t\tthis.submitValue();\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tthis.addNewLine();\n\t\t\treturn;\n\t\t}\n\n\t\t// Submit (Enter)\n\t\tif (kb.matches(data, \"tui.input.submit\")) {\n\t\t\tif (this.disableSubmit) return;\n\n\t\t\t// Workaround for terminals without Shift+Enter support:\n\t\t\t// If char before cursor is \\, delete it and insert newline instead of submitting.\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tif (this.state.cursorCol > 0 && currentLine[this.state.cursorCol - 1] === \"\\\\\") {\n\t\t\t\tthis.handleBackspace();\n\t\t\t\tthis.addNewLine();\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis.submitValue();\n\t\t\treturn;\n\t\t}\n\n\t\t// Arrow key navigation (with history support)\n\t\tif (kb.matches(data, \"tui.editor.cursorUp\")) {\n\t\t\tif (\n\t\t\t\tthis.isOnFirstVisualLine() &&\n\t\t\t\t(this.isEditorEmpty() || this.historyIndex > -1 || this.state.cursorCol === 0)\n\t\t\t) {\n\t\t\t\tthis.navigateHistory(-1);\n\t\t\t} else if (this.isOnFirstVisualLine()) {\n\t\t\t\t// Already at top - jump to start of line\n\t\t\t\tthis.moveToLineStart();\n\t\t\t} else {\n\t\t\t\tthis.moveCursor(-1, 0);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.cursorDown\")) {\n\t\t\tif (this.historyIndex > -1 && this.isOnLastVisualLine()) {\n\t\t\t\tthis.navigateHistory(1);\n\t\t\t} else if (this.isOnLastVisualLine()) {\n\t\t\t\t// Already at bottom - jump to end of line\n\t\t\t\tthis.moveToLineEnd();\n\t\t\t} else {\n\t\t\t\tthis.moveCursor(1, 0);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.cursorRight\")) {\n\t\t\tthis.moveCursor(0, 1);\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.cursorLeft\")) {\n\t\t\tthis.moveCursor(0, -1);\n\t\t\treturn;\n\t\t}\n\n\t\t// Page up/down - scroll by page and move cursor\n\t\tif (kb.matches(data, \"tui.editor.pageUp\")) {\n\t\t\tthis.pageScroll(-1);\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.pageDown\")) {\n\t\t\tthis.pageScroll(1);\n\t\t\treturn;\n\t\t}\n\n\t\t// Character jump mode triggers\n\t\tif (kb.matches(data, \"tui.editor.jumpForward\")) {\n\t\t\tthis.jumpMode = \"forward\";\n\t\t\treturn;\n\t\t}\n\t\tif (kb.matches(data, \"tui.editor.jumpBackward\")) {\n\t\t\tthis.jumpMode = \"backward\";\n\t\t\treturn;\n\t\t}\n\n\t\t// Shift+Space - insert regular space\n\t\tif (matchesKey(data, \"shift+space\")) {\n\t\t\tthis.insertCharacter(\" \");\n\t\t\treturn;\n\t\t}\n\n\t\tconst printable = decodePrintableKey(data);\n\t\tif (printable !== undefined) {\n\t\t\tthis.insertCharacter(printable);\n\t\t\treturn;\n\t\t}\n\n\t\t// Regular characters\n\t\tif (data.charCodeAt(0) >= 32) {\n\t\t\tthis.insertCharacter(data);\n\t\t}\n\t}\n\n\tprivate layoutText(contentWidth: number): LayoutLine[] {\n\t\tconst layoutLines: LayoutLine[] = [];\n\n\t\tif (this.state.lines.length === 0 || (this.state.lines.length === 1 && this.state.lines[0] === \"\")) {\n\t\t\t// Empty editor\n\t\t\tlayoutLines.push({\n\t\t\t\ttext: \"\",\n\t\t\t\thasCursor: true,\n\t\t\t\tcursorPos: 0,\n\t\t\t});\n\t\t\treturn layoutLines;\n\t\t}\n\n\t\t// Process each logical line\n\t\tfor (let i = 0; i < this.state.lines.length; i++) {\n\t\t\tconst line = this.state.lines[i] || \"\";\n\t\t\tconst isCurrentLine = i === this.state.cursorLine;\n\t\t\tconst lineVisibleWidth = visibleWidth(line);\n\n\t\t\tif (lineVisibleWidth <= contentWidth) {\n\t\t\t\t// Line fits in one layout line\n\t\t\t\tif (isCurrentLine) {\n\t\t\t\t\tlayoutLines.push({\n\t\t\t\t\t\ttext: line,\n\t\t\t\t\t\thasCursor: true,\n\t\t\t\t\t\tcursorPos: this.state.cursorCol,\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tlayoutLines.push({\n\t\t\t\t\t\ttext: line,\n\t\t\t\t\t\thasCursor: false,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Line needs wrapping - use word-aware wrapping\n\t\t\t\tconst chunks = wordWrapLine(line, contentWidth, [...this.segment(line, \"grapheme\")]);\n\n\t\t\t\tfor (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {\n\t\t\t\t\tconst chunk = chunks[chunkIndex];\n\t\t\t\t\tif (!chunk) continue;\n\n\t\t\t\t\tconst cursorPos = this.state.cursorCol;\n\t\t\t\t\tconst isLastChunk = chunkIndex === chunks.length - 1;\n\n\t\t\t\t\t// Determine if cursor is in this chunk\n\t\t\t\t\t// For word-wrapped chunks, we need to handle the case where\n\t\t\t\t\t// cursor might be in trimmed whitespace at end of chunk\n\t\t\t\t\tlet hasCursorInChunk = false;\n\t\t\t\t\tlet adjustedCursorPos = 0;\n\n\t\t\t\t\tif (isCurrentLine) {\n\t\t\t\t\t\tif (isLastChunk) {\n\t\t\t\t\t\t\t// Last chunk: cursor belongs here if >= startIndex\n\t\t\t\t\t\t\thasCursorInChunk = cursorPos >= chunk.startIndex;\n\t\t\t\t\t\t\tadjustedCursorPos = Math.min(cursorPos - chunk.startIndex, chunk.text.length);\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// Non-last chunk: cursor belongs here if in range [startIndex, endIndex)\n\t\t\t\t\t\t\t// But we need to handle the visual position in the trimmed text\n\t\t\t\t\t\t\thasCursorInChunk = cursorPos >= chunk.startIndex && cursorPos < chunk.endIndex;\n\t\t\t\t\t\t\tif (hasCursorInChunk) {\n\t\t\t\t\t\t\t\tadjustedCursorPos = Math.min(cursorPos - chunk.startIndex, chunk.text.length);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (hasCursorInChunk) {\n\t\t\t\t\t\tlayoutLines.push({\n\t\t\t\t\t\t\ttext: chunk.text,\n\t\t\t\t\t\t\thasCursor: true,\n\t\t\t\t\t\t\tcursorPos: adjustedCursorPos,\n\t\t\t\t\t\t});\n\t\t\t\t\t} else {\n\t\t\t\t\t\tlayoutLines.push({\n\t\t\t\t\t\t\ttext: chunk.text,\n\t\t\t\t\t\t\thasCursor: false,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// At one column there is no spare cell for an end cursor.\n\t\tif (contentWidth === 1) {\n\t\t\tconst cursorLine = layoutLines.find((line) => line.hasCursor);\n\t\t\tif (cursorLine?.text && cursorLine.cursorPos === cursorLine.text.length) {\n\t\t\t\tcursorLine.hasCursor = false;\n\t\t\t\tlayoutLines.splice(layoutLines.indexOf(cursorLine) + 1, 0, { text: \"\", hasCursor: true, cursorPos: 0 });\n\t\t\t}\n\t\t}\n\n\t\treturn layoutLines;\n\t}\n\n\tgetText(): string {\n\t\treturn this.state.lines.join(\"\\n\");\n\t}\n\n\tprivate expandPasteMarkers(text: string): string {\n\t\tlet result = text;\n\t\tfor (const [pasteId, pasteContent] of this.pastes) {\n\t\t\tconst markerRegex = new RegExp(`\\\\[paste #${pasteId}( (\\\\+\\\\d+ lines|\\\\d+ chars))?\\\\]`, \"g\");\n\t\t\tresult = result.replace(markerRegex, () => pasteContent);\n\t\t}\n\t\treturn result;\n\t}\n\n\t/**\n\t * Get text with paste markers expanded to their actual content.\n\t * Use this when you need the full content (e.g., for external editor).\n\t */\n\tgetExpandedText(): string {\n\t\treturn this.expandPasteMarkers(this.state.lines.join(\"\\n\"));\n\t}\n\n\tgetLines(): string[] {\n\t\treturn [...this.state.lines];\n\t}\n\n\tgetCursor(): { line: number; col: number } {\n\t\treturn { line: this.state.cursorLine, col: this.state.cursorCol };\n\t}\n\n\tsetText(text: string): void {\n\t\tthis.cancelAutocomplete();\n\t\tthis.lastAction = null;\n\t\tthis.exitHistoryBrowsing();\n\t\tconst normalized = this.normalizeText(text);\n\t\tif (this.getText() !== normalized) {\n\t\t\tthis.pushUndoSnapshot();\n\t\t}\n\t\tthis.pastes.clear();\n\t\tthis.pasteCounter = 0;\n\t\tthis.images = new Map();\n\t\t// Keep submittedImages: submitValue() snapshots chips, then setText(\"\") runs.\n\t\tthis.setTextInternal(normalized);\n\t}\n\n\t/**\n\t * Insert text at the current cursor position.\n\t * Used for programmatic insertion (e.g., clipboard image markers).\n\t * This is atomic for undo - single undo restores entire pre-insert state.\n\t */\n\tinsertTextAtCursor(text: string): void {\n\t\tif (!text) return;\n\t\tthis.cancelAutocomplete();\n\t\tthis.pushUndoSnapshot();\n\t\tthis.lastAction = null;\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.insertTextAtCursorInternal(text);\n\t}\n\n\t/**\n\t * Insert an atomic `[image_n]` chip and remember the off-screen file.\n\t * IDs stay stable for the current draft. The next unused number is used.\n\t */\n\tinsertImageMarker(attachment: { path: string; mimeType: string }): number {\n\t\tthis.cancelAutocomplete();\n\t\tthis.pushUndoSnapshot();\n\t\tthis.lastAction = null;\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.imageCounter++;\n\t\tconst id = this.imageCounter;\n\t\tconst images = new Map(this.images);\n\t\timages.set(id, { id, path: attachment.path, mimeType: attachment.mimeType });\n\t\tthis.images = images;\n\t\tthis.insertTextAtCursorInternal(formatImageMarker(id));\n\t\treturn id;\n\t}\n\n\t/**\n\t * Restore previously submitted image chips into the current draft.\n\t * Used by /edit so `[image_n]` stays a chip instead of becoming a path.\n\t */\n\trestoreImageMarkers(attachments: EditorImageAttachment[]): void {\n\t\tconst images = new Map<number, EditorImageAttachment>();\n\t\tthis.imageCounter = 0;\n\t\tfor (const attachment of attachments) {\n\t\t\timages.set(attachment.id, { ...attachment });\n\t\t\tif (attachment.id > this.imageCounter) this.imageCounter = attachment.id;\n\t\t}\n\t\tthis.images = images;\n\t}\n\n\t/** Pending image chips still in the editor, in first-appearance order. */\n\tgetPendingImages(): EditorImageAttachment[] {\n\t\tconst text = this.getText();\n\t\tconst pending: EditorImageAttachment[] = [];\n\t\tfor (const id of collectImageMarkerIds(text)) {\n\t\t\tconst attachment = this.images.get(id);\n\t\t\tif (attachment) pending.push(attachment);\n\t\t}\n\t\treturn pending;\n\t}\n\n\t/** Take and clear pending image chips. Call after the text has been submitted. */\n\ttakePendingImages(): EditorImageAttachment[] {\n\t\tconst pending = this.submittedImages.length > 0 ? this.submittedImages : this.getPendingImages();\n\t\tthis.submittedImages = [];\n\t\tthis.images = new Map();\n\t\tthis.imageCounter = 0;\n\t\treturn pending;\n\t}\n\n\t/**\n\t * Normalize text for editor storage:\n\t * - Normalize line endings (\\r\\n and \\r -> \\n)\n\t * - Expand tabs to 4 spaces\n\t */\n\tprivate normalizeText(text: string): string {\n\t\treturn text.replace(/\\r\\n/g, \"\\n\").replace(/\\r/g, \"\\n\").replace(/\\t/g, \" \");\n\t}\n\n\t/**\n\t * Internal text insertion at cursor. Handles single and multi-line text.\n\t * Does not push undo snapshots or trigger autocomplete - caller is responsible.\n\t * Normalizes line endings and calls onChange once at the end.\n\t */\n\tprivate insertTextAtCursorInternal(text: string): void {\n\t\tif (!text) return;\n\n\t\t// Normalize line endings and tabs\n\t\tconst normalized = this.normalizeText(text);\n\t\tconst insertedLines = normalized.split(\"\\n\");\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\tconst beforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\tconst afterCursor = currentLine.slice(this.state.cursorCol);\n\n\t\tif (insertedLines.length === 1) {\n\t\t\t// Single line - insert at cursor position\n\t\t\tthis.state.lines[this.state.cursorLine] = beforeCursor + normalized + afterCursor;\n\t\t\tthis.setCursorCol(this.state.cursorCol + normalized.length);\n\t\t} else {\n\t\t\t// Multi-line insertion\n\t\t\tthis.state.lines = [\n\t\t\t\t// All lines before current line\n\t\t\t\t...this.state.lines.slice(0, this.state.cursorLine),\n\n\t\t\t\t// The first inserted line merged with text before cursor\n\t\t\t\tbeforeCursor + insertedLines[0],\n\n\t\t\t\t// All middle inserted lines\n\t\t\t\t...insertedLines.slice(1, -1),\n\n\t\t\t\t// The last inserted line with text after cursor\n\t\t\t\tinsertedLines[insertedLines.length - 1] + afterCursor,\n\n\t\t\t\t// All lines after current line\n\t\t\t\t...this.state.lines.slice(this.state.cursorLine + 1),\n\t\t\t];\n\n\t\t\tthis.state.cursorLine += insertedLines.length - 1;\n\t\t\tthis.setCursorCol((insertedLines[insertedLines.length - 1] || \"\").length);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\t// All the editor methods from before...\n\tprivate insertCharacter(char: string, skipUndoCoalescing?: boolean): void {\n\t\tthis.exitHistoryBrowsing();\n\n\t\t// Undo coalescing (fish-style):\n\t\t// - Consecutive word chars coalesce into one undo unit\n\t\t// - Space captures state before itself (so undo removes space+following word together)\n\t\t// - Each space is separately undoable\n\t\t// Skip coalescing when called from atomic operations (e.g., handlePaste)\n\t\tif (!skipUndoCoalescing) {\n\t\t\tif (isWhitespaceChar(char) || this.lastAction !== \"type-word\") {\n\t\t\t\tthis.pushUndoSnapshot();\n\t\t\t}\n\t\t\tthis.lastAction = \"type-word\";\n\t\t}\n\n\t\tconst line = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\tconst before = line.slice(0, this.state.cursorCol);\n\t\tconst after = line.slice(this.state.cursorCol);\n\n\t\tthis.state.lines[this.state.cursorLine] = before + char + after;\n\t\tthis.setCursorCol(this.state.cursorCol + char.length);\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\n\t\t// Check if we should trigger or update autocomplete\n\t\tif (!this.autocompleteState) {\n\t\t\t// Auto-trigger for \"/\" at the start of a line (slash commands)\n\t\t\tif (char === \"/\" && this.isAtStartOfMessage()) {\n\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t}\n\t\t\t// Auto-trigger for symbol-based completion like @, #, or provider triggers at token boundaries\n\t\t\telse if (this.autocompleteTriggerCharacters.includes(char)) {\n\t\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\t\tconst textBeforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\t\t\tconst charBeforeSymbol = textBeforeCursor[textBeforeCursor.length - 2];\n\t\t\t\tif (textBeforeCursor.length === 1 || charBeforeSymbol === \" \" || charBeforeSymbol === \"\\t\") {\n\t\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Also auto-trigger when typing letters in a slash command or symbol completion context\n\t\t\telse if (/[a-zA-Z0-9.\\-_]/.test(char)) {\n\t\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\t\tconst textBeforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\t\t\t// Check if we're in a slash command (with or without space for arguments)\n\t\t\t\tif (this.isInSlashCommandContext(textBeforeCursor)) {\n\t\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t\t}\n\t\t\t\t// Check if we're in a symbol-based completion context like @, #, or provider triggers\n\t\t\t\telse if (this.autocompleteTriggerPattern.test(textBeforeCursor)) {\n\t\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tthis.updateAutocomplete();\n\t\t}\n\t}\n\n\tprivate handlePaste(pastedText: string): void {\n\t\tthis.cancelAutocomplete();\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.lastAction = null;\n\n\t\tthis.pushUndoSnapshot();\n\n\t\t// Some terminals (e.g. tmux popups with extended-keys-format=csi-u) re-encode\n\t\t// control bytes inside bracketed paste as CSI-u Ctrl+<letter> sequences\n\t\t// (ESC [ <codepoint> ; 5 u). Decode those back to their literal byte so the\n\t\t// per-char filter below preserves newlines instead of stripping ESC and\n\t\t// leaking the printable tail (e.g. \"[106;5u\") into the editor.\n\t\tconst decodedText = pastedText.replace(/\\x1b\\[(\\d+);5u/g, (match, code) => {\n\t\t\tconst cp = Number(code);\n\t\t\tif (cp >= 97 && cp <= 122) return String.fromCharCode(cp - 96);\n\t\t\tif (cp >= 65 && cp <= 90) return String.fromCharCode(cp - 64);\n\t\t\treturn match;\n\t\t});\n\n\t\t// Clean the pasted text: normalize line endings, expand tabs\n\t\tconst cleanText = this.normalizeText(decodedText);\n\n\t\t// Filter out non-printable characters except newlines\n\t\tlet filteredText = cleanText\n\t\t\t.split(\"\")\n\t\t\t.filter((char) => char === \"\\n\" || char.charCodeAt(0) >= 32)\n\t\t\t.join(\"\");\n\n\t\t// If pasting a file path (starts with /, ~, or .) and the character before\n\t\t// the cursor is a word character, prepend a space for better readability\n\t\tif (/^[/~.]/.test(filteredText)) {\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst charBeforeCursor = this.state.cursorCol > 0 ? currentLine[this.state.cursorCol - 1] : \"\";\n\t\t\tif (charBeforeCursor && /\\w/.test(charBeforeCursor)) {\n\t\t\t\tfilteredText = ` ${filteredText}`;\n\t\t\t}\n\t\t}\n\n\t\t// Split into lines to check for large paste\n\t\tconst pastedLines = filteredText.split(\"\\n\");\n\n\t\t// Check if this is a large paste (> 10 lines or > 1000 characters)\n\t\tconst totalChars = filteredText.length;\n\t\tif (pastedLines.length > 10 || totalChars > 1000) {\n\t\t\t// Store the paste and insert a marker\n\t\t\tthis.pasteCounter++;\n\t\t\tconst pasteId = this.pasteCounter;\n\t\t\tthis.pastes.set(pasteId, filteredText);\n\n\t\t\t// Insert marker like \"[paste #1 +123 lines]\" or \"[paste #1 1234 chars]\"\n\t\t\tconst marker =\n\t\t\t\tpastedLines.length > 10\n\t\t\t\t\t? `[paste #${pasteId} +${pastedLines.length} lines]`\n\t\t\t\t\t: `[paste #${pasteId} ${totalChars} chars]`;\n\t\t\tthis.insertTextAtCursorInternal(marker);\n\t\t\treturn;\n\t\t}\n\n\t\tif (pastedLines.length === 1) {\n\t\t\t// Single line - insert atomically (do not trigger autocomplete during paste)\n\t\t\tthis.insertTextAtCursorInternal(filteredText);\n\t\t\treturn;\n\t\t}\n\n\t\t// Multi-line paste - use direct state manipulation\n\t\tthis.insertTextAtCursorInternal(filteredText);\n\t}\n\n\tprivate addNewLine(): void {\n\t\tthis.cancelAutocomplete();\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.lastAction = null;\n\n\t\tthis.pushUndoSnapshot();\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\tconst before = currentLine.slice(0, this.state.cursorCol);\n\t\tconst after = currentLine.slice(this.state.cursorCol);\n\n\t\t// Split current line\n\t\tthis.state.lines[this.state.cursorLine] = before;\n\t\tthis.state.lines.splice(this.state.cursorLine + 1, 0, after);\n\n\t\t// Move cursor to start of new line\n\t\tthis.state.cursorLine++;\n\t\tthis.setCursorCol(0);\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tprivate shouldSubmitOnBackslashEnter(data: string, kb: ReturnType<typeof getKeybindings>): boolean {\n\t\tif (this.disableSubmit) return false;\n\t\tif (!matchesKey(data, \"enter\")) return false;\n\t\tconst submitKeys = kb.getKeys(\"tui.input.submit\");\n\t\tconst hasShiftEnter = submitKeys.includes(\"shift+enter\") || submitKeys.includes(\"shift+return\");\n\t\tif (!hasShiftEnter) return false;\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\treturn this.state.cursorCol > 0 && currentLine[this.state.cursorCol - 1] === \"\\\\\";\n\t}\n\n\tprivate submitValue(): void {\n\t\tthis.cancelAutocomplete();\n\t\tconst result = this.expandPasteMarkers(this.state.lines.join(\"\\n\")).trim();\n\t\t// Snapshot before the editor is cleared so onSubmit can still attach files.\n\t\tthis.submittedImages = this.getPendingImages();\n\n\t\tthis.state = { lines: [\"\"], cursorLine: 0, cursorCol: 0 };\n\t\tthis.pastes.clear();\n\t\tthis.pasteCounter = 0;\n\t\tthis.images = new Map();\n\t\tthis.imageCounter = 0;\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.scrollOffset = 0;\n\t\tthis.undoStack.clear();\n\t\tthis.lastAction = null;\n\n\t\tif (this.onChange) this.onChange(\"\");\n\t\tif (this.onSubmit) this.onSubmit(result);\n\t}\n\n\tprivate handleBackspace(): void {\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.lastAction = null;\n\n\t\tif (this.state.cursorCol > 0) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Delete grapheme before cursor (handles emojis, combining characters, etc.)\n\t\t\tlet line = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst beforeCursor = line.slice(0, this.state.cursorCol);\n\n\t\t\t// Find the last grapheme in the text before cursor\n\t\t\tconst graphemes = [...this.segment(beforeCursor, \"grapheme\")];\n\t\t\tconst lastGrapheme = graphemes[graphemes.length - 1];\n\t\t\tconst graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;\n\t\t\tconst isPastedSegmented = PASTE_MARKER_SINGLE.exec(lastGrapheme.segment);\n\t\t\tconst isImageSegmented = IMAGE_MARKER_SINGLE.exec(lastGrapheme.segment);\n\n\t\t\tif (isPastedSegmented) {\n\t\t\t\t// This contains the id part e.g 4 from [paste #4 +123 lines]\n\t\t\t\tconst targetId = Number(isPastedSegmented[1]);\n\t\t\t\tthis.pastes.delete(targetId);\n\t\t\t\tthis.pasteCounter--;\n\n\t\t\t\t// We got to update id of markers which are greater than the removed one\n\t\t\t\tthis.state.lines = this.state.lines.map((line) =>\n\t\t\t\t\tline.replace(PASTE_MARKER_REGEX, (fullMatch, idGroup, suffixGroup) => {\n\t\t\t\t\t\tconst x = Number(idGroup);\n\t\t\t\t\t\tif (x <= targetId) return fullMatch;\n\n\t\t\t\t\t\t// [paste #3] become [paste #2] if we remove [paste #1]\n\t\t\t\t\t\tconst newText = `[paste #${x - 1}${suffixGroup}]`;\n\t\t\t\t\t\tthis.pastes.set(x - 1, this.pastes.get(x) ?? newText);\n\t\t\t\t\t\tthis.pastes.delete(x);\n\t\t\t\t\t\treturn newText;\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t} else if (isImageSegmented) {\n\t\t\t\tconst targetId = Number(isImageSegmented[1]);\n\t\t\t\tconst images = new Map(this.images);\n\t\t\t\timages.delete(targetId);\n\t\t\t\tthis.images = images;\n\t\t\t}\n\n\t\t\tline = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\t\tconst before = line.slice(0, this.state.cursorCol - graphemeLength);\n\t\t\tconst after = line.slice(this.state.cursorCol);\n\n\t\t\tthis.state.lines[this.state.cursorLine] = before + after;\n\t\t\tthis.setCursorCol(this.state.cursorCol - graphemeLength);\n\t\t} else if (this.state.cursorLine > 0) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Merge with previous line\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst previousLine = this.state.lines[this.state.cursorLine - 1] || \"\";\n\n\t\t\tthis.state.lines[this.state.cursorLine - 1] = previousLine + currentLine;\n\t\t\tthis.state.lines.splice(this.state.cursorLine, 1);\n\n\t\t\tthis.state.cursorLine--;\n\t\t\tthis.setCursorCol(previousLine.length);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\n\t\t// Update or re-trigger autocomplete after backspace\n\t\tif (this.autocompleteState) {\n\t\t\tthis.updateAutocomplete();\n\t\t} else {\n\t\t\t// If autocomplete was cancelled (no matches), re-trigger if we're in a completable context\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst textBeforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\t\t// Slash command context\n\t\t\tif (this.isInSlashCommandContext(textBeforeCursor)) {\n\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t}\n\t\t\t// Symbol-based completion context like @, #, or provider triggers\n\t\t\telse if (this.autocompleteTriggerPattern.test(textBeforeCursor)) {\n\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Set cursor column and clear preferredVisualCol.\n\t * Use this for all non-vertical cursor movements to reset sticky column behavior.\n\t */\n\tprivate setCursorCol(col: number): void {\n\t\tthis.state.cursorCol = col;\n\t\tthis.preferredVisualCol = null;\n\t\tthis.snappedFromCursorCol = null;\n\t}\n\n\t/**\n\t * Move cursor to a target visual line, applying sticky column logic.\n\t * Shared by moveCursor() and pageScroll().\n\t */\n\tprivate moveToVisualLine(\n\t\tvisualLines: Array<{ logicalLine: number; startCol: number; length: number }>,\n\t\tcurrentVisualLine: number,\n\t\ttargetVisualLine: number,\n\t): void {\n\t\tconst currentVL = visualLines[currentVisualLine];\n\t\tconst targetVL = visualLines[targetVisualLine];\n\t\tif (!(currentVL && targetVL)) return;\n\n\t\t// When the cursor was snapped to a segment start, resolve the pre-snap\n\t\t// position against the VL it belongs to. This gives the correct visual\n\t\t// column even after a resize reshuffles VLs.\n\t\tlet currentVisualCol: number;\n\t\tif (this.snappedFromCursorCol !== null) {\n\t\t\tconst vlIndex = this.findVisualLineAt(visualLines, currentVL.logicalLine, this.snappedFromCursorCol);\n\t\t\tcurrentVisualCol = this.snappedFromCursorCol - visualLines[vlIndex].startCol;\n\t\t} else {\n\t\t\tcurrentVisualCol = this.state.cursorCol - currentVL.startCol;\n\t\t}\n\n\t\t// For non-last segments, clamp to length-1 to stay within the segment\n\t\tconst isLastSourceSegment =\n\t\t\tcurrentVisualLine === visualLines.length - 1 ||\n\t\t\tvisualLines[currentVisualLine + 1]?.logicalLine !== currentVL.logicalLine;\n\t\tconst sourceMaxVisualCol = isLastSourceSegment ? currentVL.length : Math.max(0, currentVL.length - 1);\n\n\t\tconst isLastTargetSegment =\n\t\t\ttargetVisualLine === visualLines.length - 1 ||\n\t\t\tvisualLines[targetVisualLine + 1]?.logicalLine !== targetVL.logicalLine;\n\t\tconst targetMaxVisualCol = isLastTargetSegment ? targetVL.length : Math.max(0, targetVL.length - 1);\n\n\t\tconst moveToVisualCol = this.computeVerticalMoveColumn(currentVisualCol, sourceMaxVisualCol, targetMaxVisualCol);\n\n\t\t// Set cursor position\n\t\tthis.state.cursorLine = targetVL.logicalLine;\n\t\tconst targetCol = targetVL.startCol + moveToVisualCol;\n\t\tconst logicalLine = this.state.lines[targetVL.logicalLine] || \"\";\n\t\tthis.state.cursorCol = Math.min(targetCol, logicalLine.length);\n\n\t\t// Snap cursor to atomic segment boundary (e.g. paste markers)\n\t\t// so the cursor never lands in the middle of a multi-grapheme unit.\n\t\t// Single-grapheme segments don't need snapping.\n\t\tconst segments = [...this.segment(logicalLine, \"grapheme\")];\n\t\tfor (const seg of segments) {\n\t\t\tif (seg.index > this.state.cursorCol) break;\n\t\t\tif (seg.segment.length <= 1) continue;\n\t\t\tif (this.state.cursorCol < seg.index + seg.segment.length) {\n\t\t\t\tconst isContinuation = seg.index < targetVL.startCol;\n\t\t\t\tconst isMovingDown = targetVisualLine > currentVisualLine;\n\n\t\t\t\tif (isContinuation && isMovingDown) {\n\t\t\t\t\t// The segment started on a previous visual line, and we\n\t\t\t\t\t// already visited it on the way down. Skip all remaining\n\t\t\t\t\t// continuation VLs and land on the first VL past it.\n\t\t\t\t\tconst segEnd = seg.index + seg.segment.length;\n\t\t\t\t\tlet next = targetVisualLine + 1;\n\t\t\t\t\twhile (\n\t\t\t\t\t\tnext < visualLines.length &&\n\t\t\t\t\t\tvisualLines[next].logicalLine === targetVL.logicalLine &&\n\t\t\t\t\t\tvisualLines[next].startCol < segEnd\n\t\t\t\t\t) {\n\t\t\t\t\t\tnext++;\n\t\t\t\t\t}\n\t\t\t\t\tif (next < visualLines.length) {\n\t\t\t\t\t\tthis.moveToVisualLine(visualLines, currentVisualLine, next);\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Snap to the start of the segment so it gets highlighted.\n\t\t\t\t// Store the pre-snap position so the next vertical move can\n\t\t\t\t// resolve it to the correct visual column.\n\t\t\t\tthis.snappedFromCursorCol = this.state.cursorCol;\n\t\t\t\tthis.state.cursorCol = seg.index;\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\t// No snap occurred – we moved out of the atomic segment.\n\t\tthis.snappedFromCursorCol = null;\n\t}\n\n\t/**\n\t * Compute the target visual column for vertical cursor movement.\n\t * Implements the sticky column decision table:\n\t *\n\t * | P | S | T | U | Scenario | Set Preferred | Move To |\n\t * |---|---|---|---| ---------------------------------------------------- |---------------|-------------|\n\t * | 0 | * | 0 | - | Start nav, target fits | null | current |\n\t * | 0 | * | 1 | - | Start nav, target shorter | current | target end |\n\t * | 1 | 0 | 0 | 0 | Clamped, target fits preferred | null | preferred |\n\t * | 1 | 0 | 0 | 1 | Clamped, target longer but still can't fit preferred | keep | target end |\n\t * | 1 | 0 | 1 | - | Clamped, target even shorter | keep | target end |\n\t * | 1 | 1 | 0 | - | Rewrapped, target fits current | null | current |\n\t * | 1 | 1 | 1 | - | Rewrapped, target shorter than current | current | target end |\n\t *\n\t * Where:\n\t * - P = preferred col is set\n\t * - S = cursor in middle of source line (not clamped to end)\n\t * - T = target line shorter than current visual col\n\t * - U = target line shorter than preferred col\n\t */\n\tprivate computeVerticalMoveColumn(\n\t\tcurrentVisualCol: number,\n\t\tsourceMaxVisualCol: number,\n\t\ttargetMaxVisualCol: number,\n\t): number {\n\t\tconst hasPreferred = this.preferredVisualCol !== null; // P\n\t\tconst cursorInMiddle = currentVisualCol < sourceMaxVisualCol; // S\n\t\tconst targetTooShort = targetMaxVisualCol < currentVisualCol; // T\n\n\t\tif (!hasPreferred || cursorInMiddle) {\n\t\t\tif (targetTooShort) {\n\t\t\t\t// Cases 2 and 7\n\t\t\t\tthis.preferredVisualCol = currentVisualCol;\n\t\t\t\treturn targetMaxVisualCol;\n\t\t\t}\n\n\t\t\t// Cases 1 and 6\n\t\t\tthis.preferredVisualCol = null;\n\t\t\treturn currentVisualCol;\n\t\t}\n\n\t\tconst targetCantFitPreferred = targetMaxVisualCol < this.preferredVisualCol!; // U\n\t\tif (targetTooShort || targetCantFitPreferred) {\n\t\t\t// Cases 4 and 5\n\t\t\treturn targetMaxVisualCol;\n\t\t}\n\n\t\t// Case 3\n\t\tconst result = this.preferredVisualCol!;\n\t\tthis.preferredVisualCol = null;\n\t\treturn result;\n\t}\n\n\tprivate moveToLineStart(): void {\n\t\tthis.lastAction = null;\n\t\tthis.setCursorCol(0);\n\t}\n\n\tprivate moveToLineEnd(): void {\n\t\tthis.lastAction = null;\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\tthis.setCursorCol(currentLine.length);\n\t}\n\n\tprivate deleteToStartOfLine(): void {\n\t\tthis.exitHistoryBrowsing();\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\tif (this.state.cursorCol > 0) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Calculate text to be deleted and save to kill ring (backward deletion = prepend)\n\t\t\tconst deletedText = currentLine.slice(0, this.state.cursorCol);\n\t\t\tthis.killRing.push(deletedText, { prepend: true, accumulate: this.lastAction === \"kill\" });\n\t\t\tthis.lastAction = \"kill\";\n\n\t\t\t// Delete from start of line up to cursor\n\t\t\tthis.state.lines[this.state.cursorLine] = currentLine.slice(this.state.cursorCol);\n\t\t\tthis.setCursorCol(0);\n\t\t} else if (this.state.cursorLine > 0) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// At start of line - merge with previous line, treating newline as deleted text\n\t\t\tthis.killRing.push(\"\\n\", { prepend: true, accumulate: this.lastAction === \"kill\" });\n\t\t\tthis.lastAction = \"kill\";\n\n\t\t\tconst previousLine = this.state.lines[this.state.cursorLine - 1] || \"\";\n\t\t\tthis.state.lines[this.state.cursorLine - 1] = previousLine + currentLine;\n\t\t\tthis.state.lines.splice(this.state.cursorLine, 1);\n\t\t\tthis.state.cursorLine--;\n\t\t\tthis.setCursorCol(previousLine.length);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tprivate deleteToEndOfLine(): void {\n\t\tthis.exitHistoryBrowsing();\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\tif (this.state.cursorCol < currentLine.length) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Calculate text to be deleted and save to kill ring (forward deletion = append)\n\t\t\tconst deletedText = currentLine.slice(this.state.cursorCol);\n\t\t\tthis.killRing.push(deletedText, { prepend: false, accumulate: this.lastAction === \"kill\" });\n\t\t\tthis.lastAction = \"kill\";\n\n\t\t\t// Delete from cursor to end of line\n\t\t\tthis.state.lines[this.state.cursorLine] = currentLine.slice(0, this.state.cursorCol);\n\t\t} else if (this.state.cursorLine < this.state.lines.length - 1) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// At end of line - merge with next line, treating newline as deleted text\n\t\t\tthis.killRing.push(\"\\n\", { prepend: false, accumulate: this.lastAction === \"kill\" });\n\t\t\tthis.lastAction = \"kill\";\n\n\t\t\tconst nextLine = this.state.lines[this.state.cursorLine + 1] || \"\";\n\t\t\tthis.state.lines[this.state.cursorLine] = currentLine + nextLine;\n\t\t\tthis.state.lines.splice(this.state.cursorLine + 1, 1);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tprivate deleteWordBackwards(): void {\n\t\tthis.exitHistoryBrowsing();\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\t// If at start of line, behave like backspace at column 0 (merge with previous line)\n\t\tif (this.state.cursorCol === 0) {\n\t\t\tif (this.state.cursorLine > 0) {\n\t\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t\t// Treat newline as deleted text (backward deletion = prepend)\n\t\t\t\tthis.killRing.push(\"\\n\", { prepend: true, accumulate: this.lastAction === \"kill\" });\n\t\t\t\tthis.lastAction = \"kill\";\n\n\t\t\t\tconst previousLine = this.state.lines[this.state.cursorLine - 1] || \"\";\n\t\t\t\tthis.state.lines[this.state.cursorLine - 1] = previousLine + currentLine;\n\t\t\t\tthis.state.lines.splice(this.state.cursorLine, 1);\n\t\t\t\tthis.state.cursorLine--;\n\t\t\t\tthis.setCursorCol(previousLine.length);\n\t\t\t}\n\t\t} else {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Save lastAction before cursor movement (moveWordBackwards resets it)\n\t\t\tconst wasKill = this.lastAction === \"kill\";\n\n\t\t\tconst oldCursorCol = this.state.cursorCol;\n\t\t\tthis.moveWordBackwards();\n\t\t\tconst deleteFrom = this.state.cursorCol;\n\t\t\tthis.setCursorCol(oldCursorCol);\n\n\t\t\tconst deletedText = currentLine.slice(deleteFrom, this.state.cursorCol);\n\t\t\tthis.killRing.push(deletedText, { prepend: true, accumulate: wasKill });\n\t\t\tthis.lastAction = \"kill\";\n\n\t\t\tthis.state.lines[this.state.cursorLine] =\n\t\t\t\tcurrentLine.slice(0, deleteFrom) + currentLine.slice(this.state.cursorCol);\n\t\t\tthis.setCursorCol(deleteFrom);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tprivate deleteWordForward(): void {\n\t\tthis.exitHistoryBrowsing();\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\t// If at end of line, merge with next line (delete the newline)\n\t\tif (this.state.cursorCol >= currentLine.length) {\n\t\t\tif (this.state.cursorLine < this.state.lines.length - 1) {\n\t\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t\t// Treat newline as deleted text (forward deletion = append)\n\t\t\t\tthis.killRing.push(\"\\n\", { prepend: false, accumulate: this.lastAction === \"kill\" });\n\t\t\t\tthis.lastAction = \"kill\";\n\n\t\t\t\tconst nextLine = this.state.lines[this.state.cursorLine + 1] || \"\";\n\t\t\t\tthis.state.lines[this.state.cursorLine] = currentLine + nextLine;\n\t\t\t\tthis.state.lines.splice(this.state.cursorLine + 1, 1);\n\t\t\t}\n\t\t} else {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Save lastAction before cursor movement (moveWordForwards resets it)\n\t\t\tconst wasKill = this.lastAction === \"kill\";\n\n\t\t\tconst oldCursorCol = this.state.cursorCol;\n\t\t\tthis.moveWordForwards();\n\t\t\tconst deleteTo = this.state.cursorCol;\n\t\t\tthis.setCursorCol(oldCursorCol);\n\n\t\t\tconst deletedText = currentLine.slice(this.state.cursorCol, deleteTo);\n\t\t\tthis.killRing.push(deletedText, { prepend: false, accumulate: wasKill });\n\t\t\tthis.lastAction = \"kill\";\n\n\t\t\tthis.state.lines[this.state.cursorLine] =\n\t\t\t\tcurrentLine.slice(0, this.state.cursorCol) + currentLine.slice(deleteTo);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tprivate handleForwardDelete(): void {\n\t\tthis.exitHistoryBrowsing();\n\t\tthis.lastAction = null;\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\tif (this.state.cursorCol < currentLine.length) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// Delete grapheme at cursor position (handles emojis, combining characters, etc.)\n\t\t\tconst afterCursor = currentLine.slice(this.state.cursorCol);\n\n\t\t\t// Find the first grapheme at cursor\n\t\t\tconst graphemes = [...this.segment(afterCursor, \"grapheme\")];\n\t\t\tconst firstGrapheme = graphemes[0];\n\t\t\tconst graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;\n\t\t\tconst deletedImage = firstGrapheme ? IMAGE_MARKER_SINGLE.exec(firstGrapheme.segment) : null;\n\t\t\tif (deletedImage) {\n\t\t\t\tconst images = new Map(this.images);\n\t\t\t\timages.delete(Number(deletedImage[1]));\n\t\t\t\tthis.images = images;\n\t\t\t}\n\n\t\t\tconst before = currentLine.slice(0, this.state.cursorCol);\n\t\t\tconst after = currentLine.slice(this.state.cursorCol + graphemeLength);\n\t\t\tthis.state.lines[this.state.cursorLine] = before + after;\n\t\t} else if (this.state.cursorLine < this.state.lines.length - 1) {\n\t\t\tthis.pushUndoSnapshot();\n\n\t\t\t// At end of line - merge with next line\n\t\t\tconst nextLine = this.state.lines[this.state.cursorLine + 1] || \"\";\n\t\t\tthis.state.lines[this.state.cursorLine] = currentLine + nextLine;\n\t\t\tthis.state.lines.splice(this.state.cursorLine + 1, 1);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\n\t\t// Update or re-trigger autocomplete after forward delete\n\t\tif (this.autocompleteState) {\n\t\t\tthis.updateAutocomplete();\n\t\t} else {\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst textBeforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\t\t// Slash command context\n\t\t\tif (this.isInSlashCommandContext(textBeforeCursor)) {\n\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t}\n\t\t\t// Symbol-based completion context like @, #, or provider triggers\n\t\t\telse if (this.autocompleteTriggerPattern.test(textBeforeCursor)) {\n\t\t\t\tthis.tryTriggerAutocomplete();\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Build a mapping from visual lines to logical positions.\n\t * Returns an array where each element represents a visual line with:\n\t * - logicalLine: index into this.state.lines\n\t * - startCol: starting column in the logical line\n\t * - length: length of this visual line segment\n\t */\n\tprivate buildVisualLineMap(width: number): Array<{ logicalLine: number; startCol: number; length: number }> {\n\t\tconst visualLines: Array<{ logicalLine: number; startCol: number; length: number }> = [];\n\n\t\tfor (let i = 0; i < this.state.lines.length; i++) {\n\t\t\tconst line = this.state.lines[i] || \"\";\n\t\t\tconst lineVisWidth = visibleWidth(line);\n\t\t\tif (line.length === 0) {\n\t\t\t\t// Empty line still takes one visual line\n\t\t\t\tvisualLines.push({ logicalLine: i, startCol: 0, length: 0 });\n\t\t\t} else if (lineVisWidth <= width) {\n\t\t\t\tvisualLines.push({ logicalLine: i, startCol: 0, length: line.length });\n\t\t\t} else {\n\t\t\t\t// Line needs wrapping - use word-aware wrapping\n\t\t\t\tconst chunks = wordWrapLine(line, width, [...this.segment(line, \"grapheme\")]);\n\t\t\t\tfor (const chunk of chunks) {\n\t\t\t\t\tvisualLines.push({\n\t\t\t\t\t\tlogicalLine: i,\n\t\t\t\t\t\tstartCol: chunk.startIndex,\n\t\t\t\t\t\tlength: chunk.endIndex - chunk.startIndex,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn visualLines;\n\t}\n\n\t/**\n\t * Find the visual line index that contains the given logical position.\n\t */\n\tprivate findVisualLineAt(\n\t\tvisualLines: Array<{ logicalLine: number; startCol: number; length: number }>,\n\t\tline: number,\n\t\tcol: number,\n\t): number {\n\t\tfor (let i = 0; i < visualLines.length; i++) {\n\t\t\tconst vl = visualLines[i];\n\t\t\tif (!vl || vl.logicalLine !== line) continue;\n\t\t\tconst offset = col - vl.startCol;\n\t\t\t// Cursor is in this segment if it's within range. For the last\n\t\t\t// segment of a logical line, cursor can be at length (end position)\n\t\t\tconst isLastSegmentOfLine = i === visualLines.length - 1 || visualLines[i + 1]?.logicalLine !== vl.logicalLine;\n\t\t\tif (offset >= 0 && (offset < vl.length || (isLastSegmentOfLine && offset === vl.length))) {\n\t\t\t\treturn i;\n\t\t\t}\n\t\t}\n\t\treturn visualLines.length - 1;\n\t}\n\n\t/**\n\t * Find the visual line index for the current cursor position.\n\t */\n\tprivate findCurrentVisualLine(\n\t\tvisualLines: Array<{ logicalLine: number; startCol: number; length: number }>,\n\t): number {\n\t\treturn this.findVisualLineAt(visualLines, this.state.cursorLine, this.state.cursorCol);\n\t}\n\n\tprivate moveCursor(deltaLine: number, deltaCol: number): void {\n\t\tthis.lastAction = null;\n\t\tconst visualLines = this.buildVisualLineMap(this.lastWidth);\n\t\tconst currentVisualLine = this.findCurrentVisualLine(visualLines);\n\n\t\tif (deltaLine !== 0) {\n\t\t\tconst targetVisualLine = currentVisualLine + deltaLine;\n\n\t\t\tif (targetVisualLine >= 0 && targetVisualLine < visualLines.length) {\n\t\t\t\tthis.moveToVisualLine(visualLines, currentVisualLine, targetVisualLine);\n\t\t\t}\n\t\t}\n\n\t\tif (deltaCol !== 0) {\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\t\tif (deltaCol > 0) {\n\t\t\t\t// Moving right - move by one grapheme (handles emojis, combining characters, etc.)\n\t\t\t\tif (this.state.cursorCol < currentLine.length) {\n\t\t\t\t\tconst afterCursor = currentLine.slice(this.state.cursorCol);\n\t\t\t\t\tconst graphemes = [...this.segment(afterCursor, \"grapheme\")];\n\t\t\t\t\tconst firstGrapheme = graphemes[0];\n\t\t\t\t\tthis.setCursorCol(this.state.cursorCol + (firstGrapheme ? firstGrapheme.segment.length : 1));\n\t\t\t\t} else if (this.state.cursorLine < this.state.lines.length - 1) {\n\t\t\t\t\t// Wrap to start of next logical line\n\t\t\t\t\tthis.state.cursorLine++;\n\t\t\t\t\tthis.setCursorCol(0);\n\t\t\t\t} else {\n\t\t\t\t\t// At end of last line - can't move, but set preferredVisualCol for up/down navigation\n\t\t\t\t\tconst currentVL = visualLines[currentVisualLine];\n\t\t\t\t\tif (currentVL) {\n\t\t\t\t\t\tthis.preferredVisualCol = this.state.cursorCol - currentVL.startCol;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Moving left - move by one grapheme (handles emojis, combining characters, etc.)\n\t\t\t\tif (this.state.cursorCol > 0) {\n\t\t\t\t\tconst beforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\t\t\t\tconst graphemes = [...this.segment(beforeCursor, \"grapheme\")];\n\t\t\t\t\tconst lastGrapheme = graphemes[graphemes.length - 1];\n\t\t\t\t\tthis.setCursorCol(this.state.cursorCol - (lastGrapheme ? lastGrapheme.segment.length : 1));\n\t\t\t\t} else if (this.state.cursorLine > 0) {\n\t\t\t\t\t// Wrap to end of previous logical line\n\t\t\t\t\tthis.state.cursorLine--;\n\t\t\t\t\tconst prevLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\t\t\tthis.setCursorCol(prevLine.length);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Keep an open autocomplete picker in sync with the new cursor\n\t\t// position: cursor movement changes the text before the cursor, so a\n\t\t// picker computed for the old position is stale. Re-query so it\n\t\t// refreshes — or closes when the new position yields no suggestions —\n\t\t// mirroring insertCharacter()/handleBackspace(). Without this, arrowing\n\t\t// left from `/cmd ` back into the command name leaves the argument\n\t\t// picker showing against a `/cmd` prefix (and a Tab there would\n\t\t// concatenate the stale suggestion onto the partial command name).\n\t\tif (this.autocompleteState) {\n\t\t\tthis.updateAutocomplete();\n\t\t}\n\t}\n\n\t/**\n\t * Scroll by a page (direction: -1 for up, 1 for down).\n\t * Moves cursor by the page size while keeping it in bounds.\n\t */\n\tprivate pageScroll(direction: -1 | 1): void {\n\t\tthis.lastAction = null;\n\t\tconst terminalRows = this.tui.terminal.rows;\n\t\tconst pageSize = Math.max(5, Math.floor(terminalRows * 0.3));\n\n\t\tconst visualLines = this.buildVisualLineMap(this.lastWidth);\n\t\tconst currentVisualLine = this.findCurrentVisualLine(visualLines);\n\t\tconst targetVisualLine = Math.max(0, Math.min(visualLines.length - 1, currentVisualLine + direction * pageSize));\n\n\t\tthis.moveToVisualLine(visualLines, currentVisualLine, targetVisualLine);\n\t}\n\n\tprivate moveWordBackwards(): void {\n\t\tthis.lastAction = null;\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\t// If at start of line, move to end of previous line\n\t\tif (this.state.cursorCol === 0) {\n\t\t\tif (this.state.cursorLine > 0) {\n\t\t\t\tthis.state.cursorLine--;\n\t\t\t\tconst prevLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\t\tthis.setCursorCol(prevLine.length);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tthis.setCursorCol(\n\t\t\tfindWordBackward(currentLine, this.state.cursorCol, {\n\t\t\t\tsegment: (text) => this.segment(text, \"word\"),\n\t\t\t\tisAtomicSegment: isAtomicMarker,\n\t\t\t}),\n\t\t);\n\t}\n\n\t/**\n\t * Yank (paste) the most recent kill ring entry at cursor position.\n\t */\n\tprivate yank(): void {\n\t\tif (this.killRing.length === 0) return;\n\n\t\tthis.pushUndoSnapshot();\n\n\t\tconst text = this.killRing.peek()!;\n\t\tthis.insertYankedText(text);\n\n\t\tthis.lastAction = \"yank\";\n\t}\n\n\t/**\n\t * Cycle through kill ring (only works immediately after yank or yank-pop).\n\t * Replaces the last yanked text with the previous entry in the ring.\n\t */\n\tprivate yankPop(): void {\n\t\t// Only works if we just yanked and have more than one entry\n\t\tif (this.lastAction !== \"yank\" || this.killRing.length <= 1) return;\n\n\t\tthis.pushUndoSnapshot();\n\n\t\t// Delete the previously yanked text (still at end of ring before rotation)\n\t\tthis.deleteYankedText();\n\n\t\t// Rotate the ring: move end to front\n\t\tthis.killRing.rotate();\n\n\t\t// Insert the new most recent entry (now at end after rotation)\n\t\tconst text = this.killRing.peek()!;\n\t\tthis.insertYankedText(text);\n\n\t\tthis.lastAction = \"yank\";\n\t}\n\n\t/**\n\t * Insert text at cursor position (used by yank operations).\n\t */\n\tprivate insertYankedText(text: string): void {\n\t\tthis.exitHistoryBrowsing();\n\t\tconst lines = text.split(\"\\n\");\n\n\t\tif (lines.length === 1) {\n\t\t\t// Single line - insert at cursor\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst before = currentLine.slice(0, this.state.cursorCol);\n\t\t\tconst after = currentLine.slice(this.state.cursorCol);\n\t\t\tthis.state.lines[this.state.cursorLine] = before + text + after;\n\t\t\tthis.setCursorCol(this.state.cursorCol + text.length);\n\t\t} else {\n\t\t\t// Multi-line insert\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst before = currentLine.slice(0, this.state.cursorCol);\n\t\t\tconst after = currentLine.slice(this.state.cursorCol);\n\n\t\t\t// First line merges with text before cursor\n\t\t\tthis.state.lines[this.state.cursorLine] = before + (lines[0] || \"\");\n\n\t\t\t// Insert middle lines\n\t\t\tfor (let i = 1; i < lines.length - 1; i++) {\n\t\t\t\tthis.state.lines.splice(this.state.cursorLine + i, 0, lines[i] || \"\");\n\t\t\t}\n\n\t\t\t// Last line merges with text after cursor\n\t\t\tconst lastLineIndex = this.state.cursorLine + lines.length - 1;\n\t\t\tthis.state.lines.splice(lastLineIndex, 0, (lines[lines.length - 1] || \"\") + after);\n\n\t\t\t// Update cursor position\n\t\t\tthis.state.cursorLine = lastLineIndex;\n\t\t\tthis.setCursorCol((lines[lines.length - 1] || \"\").length);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\t/**\n\t * Delete the previously yanked text (used by yank-pop).\n\t * The yanked text is derived from killRing[end] since it hasn't been rotated yet.\n\t */\n\tprivate deleteYankedText(): void {\n\t\tconst yankedText = this.killRing.peek();\n\t\tif (!yankedText) return;\n\n\t\tconst yankLines = yankedText.split(\"\\n\");\n\n\t\tif (yankLines.length === 1) {\n\t\t\t// Single line - delete backward from cursor\n\t\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\t\tconst deleteLen = yankedText.length;\n\t\t\tconst before = currentLine.slice(0, this.state.cursorCol - deleteLen);\n\t\t\tconst after = currentLine.slice(this.state.cursorCol);\n\t\t\tthis.state.lines[this.state.cursorLine] = before + after;\n\t\t\tthis.setCursorCol(this.state.cursorCol - deleteLen);\n\t\t} else {\n\t\t\t// Multi-line delete - cursor is at end of last yanked line\n\t\t\tconst startLine = this.state.cursorLine - (yankLines.length - 1);\n\t\t\tconst startCol = (this.state.lines[startLine] || \"\").length - (yankLines[0] || \"\").length;\n\n\t\t\t// Get text after cursor on current line\n\t\t\tconst afterCursor = (this.state.lines[this.state.cursorLine] || \"\").slice(this.state.cursorCol);\n\n\t\t\t// Get text before yank start position\n\t\t\tconst beforeYank = (this.state.lines[startLine] || \"\").slice(0, startCol);\n\n\t\t\t// Remove all lines from startLine to cursorLine and replace with merged line\n\t\t\tthis.state.lines.splice(startLine, yankLines.length, beforeYank + afterCursor);\n\n\t\t\t// Update cursor\n\t\t\tthis.state.cursorLine = startLine;\n\t\t\tthis.setCursorCol(startCol);\n\t\t}\n\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\tprivate pushUndoSnapshot(): void {\n\t\tthis.undoStack.push({ state: this.state, images: this.images });\n\t}\n\n\tprivate undo(): void {\n\t\tthis.exitHistoryBrowsing();\n\t\tconst snapshot = this.undoStack.pop();\n\t\tif (!snapshot) return;\n\t\tObject.assign(this.state, snapshot.state);\n\t\tthis.images = snapshot.images;\n\t\tfor (const id of this.images.keys()) this.imageCounter = Math.max(this.imageCounter, id);\n\t\tthis.lastAction = null;\n\t\tthis.preferredVisualCol = null;\n\t\tif (this.onChange) {\n\t\t\tthis.onChange(this.getText());\n\t\t}\n\t}\n\n\t/**\n\t * Jump to the first occurrence of a character in the specified direction.\n\t * Multi-line search. Case-sensitive. Skips the current cursor position.\n\t */\n\tprivate jumpToChar(char: string, direction: \"forward\" | \"backward\"): void {\n\t\tthis.lastAction = null;\n\t\tconst isForward = direction === \"forward\";\n\t\tconst lines = this.state.lines;\n\n\t\tconst end = isForward ? lines.length : -1;\n\t\tconst step = isForward ? 1 : -1;\n\n\t\tfor (let lineIdx = this.state.cursorLine; lineIdx !== end; lineIdx += step) {\n\t\t\tconst line = lines[lineIdx] || \"\";\n\t\t\tconst isCurrentLine = lineIdx === this.state.cursorLine;\n\n\t\t\t// Current line: start after/before cursor; other lines: search full line\n\t\t\tconst searchFrom = isCurrentLine\n\t\t\t\t? isForward\n\t\t\t\t\t? this.state.cursorCol + 1\n\t\t\t\t\t: this.state.cursorCol - 1\n\t\t\t\t: undefined;\n\n\t\t\tconst idx = isForward ? line.indexOf(char, searchFrom) : line.lastIndexOf(char, searchFrom);\n\n\t\t\tif (idx !== -1) {\n\t\t\t\tthis.state.cursorLine = lineIdx;\n\t\t\t\tthis.setCursorCol(idx);\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\t// No match found - cursor stays in place\n\t}\n\n\tprivate moveWordForwards(): void {\n\t\tthis.lastAction = null;\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\n\t\t// If at end of line, move to start of next line\n\t\tif (this.state.cursorCol >= currentLine.length) {\n\t\t\tif (this.state.cursorLine < this.state.lines.length - 1) {\n\t\t\t\tthis.state.cursorLine++;\n\t\t\t\tthis.setCursorCol(0);\n\t\t\t}\n\t\t\treturn;\n\t\t}\n\n\t\tthis.setCursorCol(\n\t\t\tfindWordForward(currentLine, this.state.cursorCol, {\n\t\t\t\tsegment: (text) => this.segment(text, \"word\"),\n\t\t\t\tisAtomicSegment: isAtomicMarker,\n\t\t\t}),\n\t\t);\n\t}\n\n\t// Slash menu only allowed on the first line of the editor\n\tprivate isSlashMenuAllowed(): boolean {\n\t\treturn this.state.cursorLine === 0;\n\t}\n\n\t// Helper method to check if cursor is at start of message (for slash command detection)\n\tprivate isAtStartOfMessage(): boolean {\n\t\tif (!this.isSlashMenuAllowed()) return false;\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\tconst beforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\treturn beforeCursor.trim() === \"\" || beforeCursor.trim() === \"/\";\n\t}\n\n\tprivate isInSlashCommandContext(textBeforeCursor: string): boolean {\n\t\treturn this.isSlashMenuAllowed() && textBeforeCursor.trimStart().startsWith(\"/\");\n\t}\n\n\t// Autocomplete methods\n\t/**\n\t * Find the best autocomplete item index for the given prefix.\n\t * Returns -1 if no match is found.\n\t *\n\t * Match priority:\n\t * 1. Exact match (prefix === item.value) -> always selected\n\t * 2. Prefix match -> first item whose value starts with prefix\n\t * 3. No match -> -1 (keep default highlight)\n\t *\n\t * Matching is case-sensitive and checks item.value only.\n\t */\n\tprivate getBestAutocompleteMatchIndex(items: Array<{ value: string; label: string }>, prefix: string): number {\n\t\tif (!prefix) return -1;\n\n\t\tlet firstPrefixIndex = -1;\n\n\t\tfor (let i = 0; i < items.length; i++) {\n\t\t\tconst value = items[i]!.value;\n\t\t\tif (value === prefix) {\n\t\t\t\treturn i; // Exact match always wins\n\t\t\t}\n\t\t\tif (firstPrefixIndex === -1 && value.startsWith(prefix)) {\n\t\t\t\tfirstPrefixIndex = i;\n\t\t\t}\n\t\t}\n\n\t\treturn firstPrefixIndex;\n\t}\n\n\tprivate createAutocompleteList(\n\t\tprefix: string,\n\t\titems: Array<{ value: string; label: string; description?: string }>,\n\t): SelectList {\n\t\tconst layout = prefix.startsWith(\"/\") ? SLASH_COMMAND_SELECT_LIST_LAYOUT : undefined;\n\t\treturn new SelectList(items, this.autocompleteMaxVisible, this.theme.selectList, layout);\n\t}\n\n\tprivate tryTriggerAutocomplete(explicitTab: boolean = false): void {\n\t\tthis.requestAutocomplete({ force: false, explicitTab });\n\t}\n\n\tprivate handleTabCompletion(): void {\n\t\tif (!this.autocompleteProvider) return;\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\tconst beforeCursor = currentLine.slice(0, this.state.cursorCol);\n\n\t\tif (this.isInSlashCommandContext(beforeCursor) && !beforeCursor.trimStart().includes(\" \")) {\n\t\t\tthis.handleSlashCommandCompletion();\n\t\t} else {\n\t\t\tthis.forceFileAutocomplete(true);\n\t\t}\n\t}\n\n\tprivate handleSlashCommandCompletion(): void {\n\t\tthis.requestAutocomplete({ force: false, explicitTab: true });\n\t}\n\n\tprivate forceFileAutocomplete(explicitTab: boolean = false): void {\n\t\tthis.requestAutocomplete({ force: true, explicitTab });\n\t}\n\n\tprivate requestAutocomplete(options: { force: boolean; explicitTab: boolean }): void {\n\t\tif (!this.autocompleteProvider) return;\n\n\t\tif (options.force) {\n\t\t\tconst shouldTrigger =\n\t\t\t\t!this.autocompleteProvider.shouldTriggerFileCompletion ||\n\t\t\t\tthis.autocompleteProvider.shouldTriggerFileCompletion(\n\t\t\t\t\tthis.state.lines,\n\t\t\t\t\tthis.state.cursorLine,\n\t\t\t\t\tthis.state.cursorCol,\n\t\t\t\t);\n\t\t\tif (!shouldTrigger) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\tthis.cancelAutocompleteRequest();\n\t\tconst startToken = ++this.autocompleteStartToken;\n\n\t\tconst debounceMs = this.getAutocompleteDebounceMs(options);\n\t\tif (debounceMs > 0) {\n\t\t\tthis.autocompleteDebounceTimer = setTimeout(() => {\n\t\t\t\tthis.autocompleteDebounceTimer = undefined;\n\t\t\t\tvoid this.startAutocompleteRequest(startToken, options);\n\t\t\t}, debounceMs);\n\t\t\treturn;\n\t\t}\n\n\t\tvoid this.startAutocompleteRequest(startToken, options);\n\t}\n\n\tprivate async startAutocompleteRequest(\n\t\tstartToken: number,\n\t\toptions: { force: boolean; explicitTab: boolean },\n\t): Promise<void> {\n\t\tconst previousTask = this.autocompleteRequestTask;\n\t\tthis.autocompleteRequestTask = (async () => {\n\t\t\tawait previousTask;\n\t\t\tif (startToken !== this.autocompleteStartToken || !this.autocompleteProvider) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst controller = new AbortController();\n\t\t\tthis.autocompleteAbort = controller;\n\t\t\tconst requestId = ++this.autocompleteRequestId;\n\t\t\tconst snapshotText = this.getText();\n\t\t\tconst snapshotLine = this.state.cursorLine;\n\t\t\tconst snapshotCol = this.state.cursorCol;\n\n\t\t\tawait this.runAutocompleteRequest(requestId, controller, snapshotText, snapshotLine, snapshotCol, options);\n\t\t})();\n\t\tawait this.autocompleteRequestTask;\n\t}\n\n\tprivate setAutocompleteTriggerCharacters(triggerCharacters: string[]): void {\n\t\tconst next = [...DEFAULT_AUTOCOMPLETE_TRIGGER_CHARACTERS];\n\t\tfor (const character of triggerCharacters) {\n\t\t\tif (character.length !== 1 || character === \"/\" || isWhitespaceChar(character) || next.includes(character)) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tnext.push(character);\n\t\t}\n\t\tthis.autocompleteTriggerCharacters = next;\n\t\tthis.autocompleteTriggerPattern = buildTriggerPattern(next);\n\t\tthis.autocompleteDebouncePattern = buildDebouncePattern(next);\n\t}\n\n\tprivate getAutocompleteDebounceMs(options: { force: boolean; explicitTab: boolean }): number {\n\t\tif (options.explicitTab || options.force) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tconst currentLine = this.state.lines[this.state.cursorLine] || \"\";\n\t\tconst textBeforeCursor = currentLine.slice(0, this.state.cursorCol);\n\t\treturn this.autocompleteDebouncePattern.test(textBeforeCursor) ? ATTACHMENT_AUTOCOMPLETE_DEBOUNCE_MS : 0;\n\t}\n\n\tprivate async runAutocompleteRequest(\n\t\trequestId: number,\n\t\tcontroller: AbortController,\n\t\tsnapshotText: string,\n\t\tsnapshotLine: number,\n\t\tsnapshotCol: number,\n\t\toptions: { force: boolean; explicitTab: boolean },\n\t): Promise<void> {\n\t\tif (!this.autocompleteProvider) return;\n\n\t\tconst suggestions = await this.autocompleteProvider.getSuggestions(\n\t\t\tthis.state.lines,\n\t\t\tthis.state.cursorLine,\n\t\t\tthis.state.cursorCol,\n\t\t\t{ signal: controller.signal, force: options.force },\n\t\t);\n\n\t\tif (!this.isAutocompleteRequestCurrent(requestId, controller, snapshotText, snapshotLine, snapshotCol)) {\n\t\t\treturn;\n\t\t}\n\n\t\tthis.autocompleteAbort = undefined;\n\n\t\tif (!suggestions || !Array.isArray(suggestions.items) || suggestions.items.length === 0) {\n\t\t\tthis.cancelAutocomplete();\n\t\t\tthis.tui.requestRender();\n\t\t\treturn;\n\t\t}\n\n\t\tif (options.force && options.explicitTab && suggestions.items.length === 1) {\n\t\t\tconst item = suggestions.items[0]!;\n\t\t\tthis.pushUndoSnapshot();\n\t\t\tthis.lastAction = null;\n\t\t\tconst result = this.autocompleteProvider.applyCompletion(\n\t\t\t\tthis.state.lines,\n\t\t\t\tthis.state.cursorLine,\n\t\t\t\tthis.state.cursorCol,\n\t\t\t\titem,\n\t\t\t\tsuggestions.prefix,\n\t\t\t);\n\t\t\tthis.state.lines = result.lines;\n\t\t\tthis.state.cursorLine = result.cursorLine;\n\t\t\tthis.setCursorCol(result.cursorCol);\n\t\t\tif (this.onChange) this.onChange(this.getText());\n\t\t\tthis.tui.requestRender();\n\t\t\treturn;\n\t\t}\n\n\t\tthis.applyAutocompleteSuggestions(suggestions, options.force ? \"force\" : \"regular\");\n\t\tthis.tui.requestRender();\n\t}\n\n\tprivate isAutocompleteRequestCurrent(\n\t\trequestId: number,\n\t\tcontroller: AbortController,\n\t\tsnapshotText: string,\n\t\tsnapshotLine: number,\n\t\tsnapshotCol: number,\n\t): boolean {\n\t\treturn (\n\t\t\t!controller.signal.aborted &&\n\t\t\trequestId === this.autocompleteRequestId &&\n\t\t\tthis.getText() === snapshotText &&\n\t\t\tthis.state.cursorLine === snapshotLine &&\n\t\t\tthis.state.cursorCol === snapshotCol\n\t\t);\n\t}\n\n\tprivate applyAutocompleteSuggestions(suggestions: AutocompleteSuggestions, state: \"regular\" | \"force\"): void {\n\t\tthis.autocompletePrefix = suggestions.prefix;\n\t\tthis.autocompleteList = this.createAutocompleteList(suggestions.prefix, suggestions.items);\n\n\t\tconst bestMatchIndex = this.getBestAutocompleteMatchIndex(suggestions.items, suggestions.prefix);\n\t\tif (bestMatchIndex >= 0) {\n\t\t\tthis.autocompleteList.setSelectedIndex(bestMatchIndex);\n\t\t}\n\n\t\tthis.autocompleteState = state;\n\t}\n\n\tprivate cancelAutocompleteRequest(): void {\n\t\tthis.autocompleteStartToken += 1;\n\t\tif (this.autocompleteDebounceTimer) {\n\t\t\tclearTimeout(this.autocompleteDebounceTimer);\n\t\t\tthis.autocompleteDebounceTimer = undefined;\n\t\t}\n\t\tthis.autocompleteAbort?.abort();\n\t\tthis.autocompleteAbort = undefined;\n\t}\n\n\tprivate clearAutocompleteUi(): void {\n\t\tthis.autocompleteState = null;\n\t\tthis.autocompleteList = undefined;\n\t\tthis.autocompletePrefix = \"\";\n\t}\n\n\tprivate cancelAutocomplete(): void {\n\t\tthis.cancelAutocompleteRequest();\n\t\tthis.clearAutocompleteUi();\n\t}\n\n\tpublic isShowingAutocomplete(): boolean {\n\t\treturn this.autocompleteState !== null;\n\t}\n\n\tprivate updateAutocomplete(): void {\n\t\tif (!this.autocompleteState || !this.autocompleteProvider) return;\n\t\tthis.requestAutocomplete({ force: this.autocompleteState === \"force\", explicitTab: false });\n\t}\n}\n"]}