@dungle-scrubs/tallow 0.8.21 → 0.8.23

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