@pencil-agent/nano-pencil 2.0.0-beta.2 → 2.0.0-beta.3

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 (179) hide show
  1. package/dist/build-meta.json +3 -3
  2. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-continuations.d.ts +17 -0
  3. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-continuations.js +60 -0
  4. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-stream-events.d.ts +19 -0
  5. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-stream-events.js +55 -0
  6. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-results.d.ts +10 -0
  7. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-results.js +137 -0
  8. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-summaries.d.ts +22 -0
  9. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop-tool-summaries.js +64 -0
  10. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop.d.ts +26 -0
  11. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-loop.js +913 -0
  12. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-run-result.d.ts +9 -0
  13. package/dist/node_modules/@pencil-agent/agent-core/dist/agent-run-result.js +32 -0
  14. package/dist/node_modules/@pencil-agent/agent-core/dist/agent.d.ts +215 -0
  15. package/dist/node_modules/@pencil-agent/agent-core/dist/agent.js +522 -0
  16. package/dist/node_modules/@pencil-agent/agent-core/dist/errors.d.ts +62 -0
  17. package/dist/node_modules/@pencil-agent/agent-core/dist/errors.js +146 -0
  18. package/dist/node_modules/@pencil-agent/agent-core/dist/index.d.ts +14 -0
  19. package/dist/node_modules/@pencil-agent/agent-core/dist/index.js +19 -0
  20. package/dist/node_modules/@pencil-agent/agent-core/dist/proxy.d.ts +91 -0
  21. package/dist/node_modules/@pencil-agent/agent-core/dist/proxy.js +279 -0
  22. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-agent-loop.d.ts +15 -0
  23. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-agent-loop.js +625 -0
  24. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-streaming-tool-executor.d.ts +33 -0
  25. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-streaming-tool-executor.js +189 -0
  26. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-tool-orchestration.d.ts +35 -0
  27. package/dist/node_modules/@pencil-agent/agent-core/dist/structured-adaptive-tool-orchestration.js +319 -0
  28. package/dist/node_modules/@pencil-agent/agent-core/dist/types.d.ts +417 -0
  29. package/dist/node_modules/@pencil-agent/agent-core/dist/types.js +13 -0
  30. package/dist/node_modules/@pencil-agent/agent-core/package.json +28 -0
  31. package/dist/node_modules/@pencil-agent/ai/dist/api-registry.d.ts +27 -0
  32. package/dist/node_modules/@pencil-agent/ai/dist/api-registry.js +152 -0
  33. package/dist/node_modules/@pencil-agent/ai/dist/cli.d.ts +2 -0
  34. package/dist/node_modules/@pencil-agent/ai/dist/cli.js +121 -0
  35. package/dist/node_modules/@pencil-agent/ai/dist/config-path.d.ts +1 -0
  36. package/dist/node_modules/@pencil-agent/ai/dist/config-path.js +17 -0
  37. package/dist/node_modules/@pencil-agent/ai/dist/debug-logger.d.ts +94 -0
  38. package/dist/node_modules/@pencil-agent/ai/dist/debug-logger.js +218 -0
  39. package/dist/node_modules/@pencil-agent/ai/dist/env-api-keys.d.ts +8 -0
  40. package/dist/node_modules/@pencil-agent/ai/dist/env-api-keys.js +107 -0
  41. package/dist/node_modules/@pencil-agent/ai/dist/env.d.ts +7 -0
  42. package/dist/node_modules/@pencil-agent/ai/dist/env.js +7 -0
  43. package/dist/node_modules/@pencil-agent/ai/dist/events.d.ts +8 -0
  44. package/dist/node_modules/@pencil-agent/ai/dist/events.js +7 -0
  45. package/dist/node_modules/@pencil-agent/ai/dist/index.d.ts +27 -0
  46. package/dist/node_modules/@pencil-agent/ai/dist/index.js +20 -0
  47. package/dist/node_modules/@pencil-agent/ai/dist/json.d.ts +7 -0
  48. package/dist/node_modules/@pencil-agent/ai/dist/json.js +7 -0
  49. package/dist/node_modules/@pencil-agent/ai/dist/models.d.ts +31 -0
  50. package/dist/node_modules/@pencil-agent/ai/dist/models.generated.d.ts +15159 -0
  51. package/dist/node_modules/@pencil-agent/ai/dist/models.generated.js +14928 -0
  52. package/dist/node_modules/@pencil-agent/ai/dist/models.js +60 -0
  53. package/dist/node_modules/@pencil-agent/ai/dist/overflow.d.ts +7 -0
  54. package/dist/node_modules/@pencil-agent/ai/dist/overflow.js +7 -0
  55. package/dist/node_modules/@pencil-agent/ai/dist/providers/amazon-bedrock.d.ts +20 -0
  56. package/dist/node_modules/@pencil-agent/ai/dist/providers/amazon-bedrock.js +606 -0
  57. package/dist/node_modules/@pencil-agent/ai/dist/providers/anthropic.d.ts +38 -0
  58. package/dist/node_modules/@pencil-agent/ai/dist/providers/anthropic.js +737 -0
  59. package/dist/node_modules/@pencil-agent/ai/dist/providers/azure-openai-responses.d.ts +21 -0
  60. package/dist/node_modules/@pencil-agent/ai/dist/providers/azure-openai-responses.js +193 -0
  61. package/dist/node_modules/@pencil-agent/ai/dist/providers/github-copilot-headers.d.ts +13 -0
  62. package/dist/node_modules/@pencil-agent/ai/dist/providers/github-copilot-headers.js +34 -0
  63. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-gemini-cli.d.ts +79 -0
  64. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-gemini-cli.js +753 -0
  65. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-shared.d.ts +70 -0
  66. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-shared.js +311 -0
  67. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-vertex.d.ts +20 -0
  68. package/dist/node_modules/@pencil-agent/ai/dist/providers/google-vertex.js +380 -0
  69. package/dist/node_modules/@pencil-agent/ai/dist/providers/google.d.ts +18 -0
  70. package/dist/node_modules/@pencil-agent/ai/dist/providers/google.js +360 -0
  71. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-codex-responses.d.ts +8 -0
  72. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-codex-responses.js +704 -0
  73. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-completions.d.ts +20 -0
  74. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-completions.js +870 -0
  75. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses-shared.d.ts +22 -0
  76. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses-shared.js +432 -0
  77. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses.d.ts +19 -0
  78. package/dist/node_modules/@pencil-agent/ai/dist/providers/openai-responses.js +207 -0
  79. package/dist/node_modules/@pencil-agent/ai/dist/providers/register-builtins.d.ts +8 -0
  80. package/dist/node_modules/@pencil-agent/ai/dist/providers/register-builtins.js +86 -0
  81. package/dist/node_modules/@pencil-agent/ai/dist/providers/simple-options.d.ts +13 -0
  82. package/dist/node_modules/@pencil-agent/ai/dist/providers/simple-options.js +40 -0
  83. package/dist/node_modules/@pencil-agent/ai/dist/providers/transform-messages.d.ts +13 -0
  84. package/dist/node_modules/@pencil-agent/ai/dist/providers/transform-messages.js +175 -0
  85. package/dist/node_modules/@pencil-agent/ai/dist/registry.d.ts +8 -0
  86. package/dist/node_modules/@pencil-agent/ai/dist/registry.js +8 -0
  87. package/dist/node_modules/@pencil-agent/ai/dist/schema.d.ts +10 -0
  88. package/dist/node_modules/@pencil-agent/ai/dist/schema.js +9 -0
  89. package/dist/node_modules/@pencil-agent/ai/dist/stream.d.ts +25 -0
  90. package/dist/node_modules/@pencil-agent/ai/dist/stream.js +324 -0
  91. package/dist/node_modules/@pencil-agent/ai/dist/types.d.ts +306 -0
  92. package/dist/node_modules/@pencil-agent/ai/dist/types.js +7 -0
  93. package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream-types.d.ts +12 -0
  94. package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream-types.js +7 -0
  95. package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream.d.ts +31 -0
  96. package/dist/node_modules/@pencil-agent/ai/dist/utils/event-stream.js +98 -0
  97. package/dist/node_modules/@pencil-agent/ai/dist/utils/http-proxy.d.ts +13 -0
  98. package/dist/node_modules/@pencil-agent/ai/dist/utils/http-proxy.js +20 -0
  99. package/dist/node_modules/@pencil-agent/ai/dist/utils/json-parse.d.ts +14 -0
  100. package/dist/node_modules/@pencil-agent/ai/dist/utils/json-parse.js +34 -0
  101. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/anthropic.d.ts +22 -0
  102. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/anthropic.js +109 -0
  103. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/decode-credential.d.ts +12 -0
  104. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/decode-credential.js +25 -0
  105. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/github-copilot.d.ts +35 -0
  106. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/github-copilot.js +286 -0
  107. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-antigravity.d.ts +31 -0
  108. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-antigravity.js +378 -0
  109. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-gemini-cli.d.ts +31 -0
  110. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/google-gemini-cli.js +483 -0
  111. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/index.d.ts +60 -0
  112. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/index.js +131 -0
  113. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/openai-codex.d.ts +39 -0
  114. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/openai-codex.js +385 -0
  115. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/pkce.d.ts +18 -0
  116. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/pkce.js +36 -0
  117. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/types.d.ts +52 -0
  118. package/dist/node_modules/@pencil-agent/ai/dist/utils/oauth/types.js +7 -0
  119. package/dist/node_modules/@pencil-agent/ai/dist/utils/overflow.d.ts +57 -0
  120. package/dist/node_modules/@pencil-agent/ai/dist/utils/overflow.js +120 -0
  121. package/dist/node_modules/@pencil-agent/ai/dist/utils/sanitize-unicode.d.ts +16 -0
  122. package/dist/node_modules/@pencil-agent/ai/dist/utils/sanitize-unicode.js +20 -0
  123. package/dist/node_modules/@pencil-agent/ai/dist/utils/typebox-helpers.d.ts +22 -0
  124. package/dist/node_modules/@pencil-agent/ai/dist/utils/typebox-helpers.js +26 -0
  125. package/dist/node_modules/@pencil-agent/ai/dist/utils/validation.d.ts +23 -0
  126. package/dist/node_modules/@pencil-agent/ai/dist/utils/validation.js +78 -0
  127. package/dist/node_modules/@pencil-agent/ai/package.json +94 -0
  128. package/dist/node_modules/@pencil-agent/tui/dist/autocomplete.d.ts +62 -0
  129. package/dist/node_modules/@pencil-agent/tui/dist/autocomplete.js +624 -0
  130. package/dist/node_modules/@pencil-agent/tui/dist/components/box.d.ts +27 -0
  131. package/dist/node_modules/@pencil-agent/tui/dist/components/box.js +109 -0
  132. package/dist/node_modules/@pencil-agent/tui/dist/components/cancellable-loader.d.ts +27 -0
  133. package/dist/node_modules/@pencil-agent/tui/dist/components/cancellable-loader.js +40 -0
  134. package/dist/node_modules/@pencil-agent/tui/dist/components/editor.d.ts +218 -0
  135. package/dist/node_modules/@pencil-agent/tui/dist/components/editor.js +1697 -0
  136. package/dist/node_modules/@pencil-agent/tui/dist/components/image.d.ts +33 -0
  137. package/dist/node_modules/@pencil-agent/tui/dist/components/image.js +74 -0
  138. package/dist/node_modules/@pencil-agent/tui/dist/components/input.d.ts +42 -0
  139. package/dist/node_modules/@pencil-agent/tui/dist/components/input.js +438 -0
  140. package/dist/node_modules/@pencil-agent/tui/dist/components/loader.d.ts +26 -0
  141. package/dist/node_modules/@pencil-agent/tui/dist/components/loader.js +54 -0
  142. package/dist/node_modules/@pencil-agent/tui/dist/components/markdown.d.ts +100 -0
  143. package/dist/node_modules/@pencil-agent/tui/dist/components/markdown.js +634 -0
  144. package/dist/node_modules/@pencil-agent/tui/dist/components/select-list.d.ts +37 -0
  145. package/dist/node_modules/@pencil-agent/tui/dist/components/select-list.js +157 -0
  146. package/dist/node_modules/@pencil-agent/tui/dist/components/settings-list.d.ts +55 -0
  147. package/dist/node_modules/@pencil-agent/tui/dist/components/settings-list.js +190 -0
  148. package/dist/node_modules/@pencil-agent/tui/dist/components/spacer.d.ts +17 -0
  149. package/dist/node_modules/@pencil-agent/tui/dist/components/spacer.js +28 -0
  150. package/dist/node_modules/@pencil-agent/tui/dist/components/text.d.ts +24 -0
  151. package/dist/node_modules/@pencil-agent/tui/dist/components/text.js +94 -0
  152. package/dist/node_modules/@pencil-agent/tui/dist/components/truncated-text.d.ts +18 -0
  153. package/dist/node_modules/@pencil-agent/tui/dist/components/truncated-text.js +56 -0
  154. package/dist/node_modules/@pencil-agent/tui/dist/editor-component.d.ts +51 -0
  155. package/dist/node_modules/@pencil-agent/tui/dist/editor-component.js +7 -0
  156. package/dist/node_modules/@pencil-agent/tui/dist/fuzzy.d.ts +32 -0
  157. package/dist/node_modules/@pencil-agent/tui/dist/fuzzy.js +152 -0
  158. package/dist/node_modules/@pencil-agent/tui/dist/index.d.ts +28 -0
  159. package/dist/node_modules/@pencil-agent/tui/dist/index.js +37 -0
  160. package/dist/node_modules/@pencil-agent/tui/dist/keybindings.d.ts +44 -0
  161. package/dist/node_modules/@pencil-agent/tui/dist/keybindings.js +119 -0
  162. package/dist/node_modules/@pencil-agent/tui/dist/keys.d.ts +149 -0
  163. package/dist/node_modules/@pencil-agent/tui/dist/keys.js +948 -0
  164. package/dist/node_modules/@pencil-agent/tui/dist/kill-ring.d.ts +33 -0
  165. package/dist/node_modules/@pencil-agent/tui/dist/kill-ring.js +49 -0
  166. package/dist/node_modules/@pencil-agent/tui/dist/stdin-buffer.d.ts +38 -0
  167. package/dist/node_modules/@pencil-agent/tui/dist/stdin-buffer.js +307 -0
  168. package/dist/node_modules/@pencil-agent/tui/dist/terminal-image.d.ts +73 -0
  169. package/dist/node_modules/@pencil-agent/tui/dist/terminal-image.js +287 -0
  170. package/dist/node_modules/@pencil-agent/tui/dist/terminal.d.ts +86 -0
  171. package/dist/node_modules/@pencil-agent/tui/dist/terminal.js +266 -0
  172. package/dist/node_modules/@pencil-agent/tui/dist/tui.d.ts +219 -0
  173. package/dist/node_modules/@pencil-agent/tui/dist/tui.js +1001 -0
  174. package/dist/node_modules/@pencil-agent/tui/dist/undo-stack.d.ts +22 -0
  175. package/dist/node_modules/@pencil-agent/tui/dist/undo-stack.js +30 -0
  176. package/dist/node_modules/@pencil-agent/tui/dist/utils.d.ts +83 -0
  177. package/dist/node_modules/@pencil-agent/tui/dist/utils.js +811 -0
  178. package/dist/node_modules/@pencil-agent/tui/package.json +37 -0
  179. package/package.json +3 -2
@@ -0,0 +1,1697 @@
1
+ /**
2
+ * [WHO]: TextChunk, wordWrapLine, EditorTheme, EditorOptions, Editor
3
+ * [FROM]: Depends on ../keybindings.js, ../keys.js, ../kill-ring.js, ../tui.js, ../undo-stack.js
4
+ * [TO]: Consumed by core/lib/tui/src/index.ts
5
+ * [HERE]: core/lib/tui/src/components/editor.ts -
6
+ */
7
+ import { getEditorKeybindings } from "../keybindings.js";
8
+ import { matchesKey } from "../keys.js";
9
+ import { KillRing } from "../kill-ring.js";
10
+ import { CURSOR_MARKER } from "../tui.js";
11
+ import { UndoStack } from "../undo-stack.js";
12
+ import { getSegmenter, isPunctuationChar, isWhitespaceChar, visibleWidth } from "../utils.js";
13
+ import { SelectList } from "./select-list.js";
14
+ const segmenter = getSegmenter();
15
+ /**
16
+ * Split a line into word-wrapped chunks.
17
+ * Wraps at word boundaries when possible, falling back to character-level
18
+ * wrapping for words longer than the available width.
19
+ *
20
+ * @param line - The text line to wrap
21
+ * @param maxWidth - Maximum visible width per chunk
22
+ * @returns Array of chunks with text and position information
23
+ */
24
+ export function wordWrapLine(line, maxWidth) {
25
+ if (!line || maxWidth <= 0) {
26
+ return [{ text: "", startIndex: 0, endIndex: 0 }];
27
+ }
28
+ const lineWidth = visibleWidth(line);
29
+ if (lineWidth <= maxWidth) {
30
+ return [{ text: line, startIndex: 0, endIndex: line.length }];
31
+ }
32
+ const chunks = [];
33
+ const segments = [...segmenter.segment(line)];
34
+ let currentWidth = 0;
35
+ let chunkStart = 0;
36
+ // Wrap opportunity: the position after the last whitespace before a non-whitespace
37
+ // grapheme, i.e. where a line break is allowed.
38
+ let wrapOppIndex = -1;
39
+ let wrapOppWidth = 0;
40
+ for (let i = 0; i < segments.length; i++) {
41
+ const seg = segments[i];
42
+ const grapheme = seg.segment;
43
+ const gWidth = visibleWidth(grapheme);
44
+ const charIndex = seg.index;
45
+ const isWs = isWhitespaceChar(grapheme);
46
+ // Overflow check before advancing.
47
+ if (currentWidth + gWidth > maxWidth) {
48
+ if (wrapOppIndex >= 0) {
49
+ // Backtrack to last wrap opportunity.
50
+ chunks.push({ text: line.slice(chunkStart, wrapOppIndex), startIndex: chunkStart, endIndex: wrapOppIndex });
51
+ chunkStart = wrapOppIndex;
52
+ currentWidth -= wrapOppWidth;
53
+ }
54
+ else if (chunkStart < charIndex) {
55
+ // No wrap opportunity: force-break at current position.
56
+ chunks.push({ text: line.slice(chunkStart, charIndex), startIndex: chunkStart, endIndex: charIndex });
57
+ chunkStart = charIndex;
58
+ currentWidth = 0;
59
+ }
60
+ wrapOppIndex = -1;
61
+ }
62
+ // Advance.
63
+ currentWidth += gWidth;
64
+ // Record wrap opportunity: whitespace followed by non-whitespace.
65
+ // Multiple spaces join (no break between them); the break point is
66
+ // after the last space before the next word.
67
+ const next = segments[i + 1];
68
+ if (isWs && next && !isWhitespaceChar(next.segment)) {
69
+ wrapOppIndex = next.index;
70
+ wrapOppWidth = currentWidth;
71
+ }
72
+ }
73
+ // Push final chunk.
74
+ chunks.push({ text: line.slice(chunkStart), startIndex: chunkStart, endIndex: line.length });
75
+ return chunks;
76
+ }
77
+ // Kitty CSI-u sequences for printable keys, including optional shifted/base codepoints.
78
+ const KITTY_CSI_U_REGEX = /^\x1b\[(\d+)(?::(\d*))?(?::(\d+))?(?:;(\d+))?(?::(\d+))?u$/;
79
+ const KITTY_MOD_SHIFT = 1;
80
+ const KITTY_MOD_ALT = 2;
81
+ const KITTY_MOD_CTRL = 4;
82
+ // Decode a printable CSI-u sequence, preferring the shifted key when present.
83
+ function decodeKittyPrintable(data) {
84
+ const match = data.match(KITTY_CSI_U_REGEX);
85
+ if (!match)
86
+ return undefined;
87
+ // CSI-u groups: <codepoint>[:<shifted>[:<base>]];<mod>u
88
+ const codepoint = Number.parseInt(match[1] ?? "", 10);
89
+ if (!Number.isFinite(codepoint))
90
+ return undefined;
91
+ const shiftedKey = match[2] && match[2].length > 0 ? Number.parseInt(match[2], 10) : undefined;
92
+ const modValue = match[4] ? Number.parseInt(match[4], 10) : 1;
93
+ // Modifiers are 1-indexed in CSI-u; normalize to our bitmask.
94
+ const modifier = Number.isFinite(modValue) ? modValue - 1 : 0;
95
+ // Ignore CSI-u sequences used for Alt/Ctrl shortcuts.
96
+ if (modifier & (KITTY_MOD_ALT | KITTY_MOD_CTRL))
97
+ return undefined;
98
+ // Prefer the shifted keycode when Shift is held.
99
+ let effectiveCodepoint = codepoint;
100
+ if (modifier & KITTY_MOD_SHIFT && typeof shiftedKey === "number") {
101
+ effectiveCodepoint = shiftedKey;
102
+ }
103
+ // Drop control characters or invalid codepoints.
104
+ if (!Number.isFinite(effectiveCodepoint) || effectiveCodepoint < 32)
105
+ return undefined;
106
+ try {
107
+ return String.fromCodePoint(effectiveCodepoint);
108
+ }
109
+ catch {
110
+ return undefined;
111
+ }
112
+ }
113
+ export class Editor {
114
+ state = {
115
+ lines: [""],
116
+ cursorLine: 0,
117
+ cursorCol: 0,
118
+ };
119
+ /** Focusable interface - set by TUI when focus changes */
120
+ focused = false;
121
+ tui;
122
+ theme;
123
+ paddingX = 0;
124
+ // Store last render width for cursor navigation
125
+ lastWidth = 80;
126
+ // Vertical scrolling support
127
+ scrollOffset = 0;
128
+ // Border color (can be changed dynamically)
129
+ borderColor;
130
+ // Autocomplete support
131
+ autocompleteProvider;
132
+ autocompleteList;
133
+ autocompleteState = null;
134
+ autocompletePrefix = "";
135
+ autocompleteMaxVisible = 5;
136
+ // Paste tracking for large pastes
137
+ pastes = new Map();
138
+ pasteCounter = 0;
139
+ // Bracketed paste mode buffering
140
+ pasteBuffer = "";
141
+ isInPaste = false;
142
+ // Prompt history for up/down navigation
143
+ history = [];
144
+ historyIndex = -1; // -1 = not browsing, 0 = most recent, 1 = older, etc.
145
+ // Kill ring for Emacs-style kill/yank operations
146
+ killRing = new KillRing();
147
+ lastAction = null;
148
+ // Character jump mode
149
+ jumpMode = null;
150
+ // Preferred visual column for vertical cursor movement (sticky column)
151
+ preferredVisualCol = null;
152
+ // Undo support
153
+ undoStack = new UndoStack();
154
+ onSubmit;
155
+ onChange;
156
+ disableSubmit = false;
157
+ /** Placeholder text shown when editor is empty */
158
+ placeholder = "";
159
+ constructor(tui, theme, options = {}) {
160
+ this.tui = tui;
161
+ this.theme = theme;
162
+ this.borderColor = theme.borderColor;
163
+ const paddingX = options.paddingX ?? 0;
164
+ this.paddingX = Number.isFinite(paddingX) ? Math.max(0, Math.floor(paddingX)) : 0;
165
+ const maxVisible = options.autocompleteMaxVisible ?? 5;
166
+ this.autocompleteMaxVisible = Number.isFinite(maxVisible) ? Math.max(3, Math.min(20, Math.floor(maxVisible))) : 5;
167
+ }
168
+ getPaddingX() {
169
+ return this.paddingX;
170
+ }
171
+ setPaddingX(padding) {
172
+ const newPadding = Number.isFinite(padding) ? Math.max(0, Math.floor(padding)) : 0;
173
+ if (this.paddingX !== newPadding) {
174
+ this.paddingX = newPadding;
175
+ this.tui.requestRender();
176
+ }
177
+ }
178
+ getAutocompleteMaxVisible() {
179
+ return this.autocompleteMaxVisible;
180
+ }
181
+ setAutocompleteMaxVisible(maxVisible) {
182
+ const newMaxVisible = Number.isFinite(maxVisible) ? Math.max(3, Math.min(20, Math.floor(maxVisible))) : 5;
183
+ if (this.autocompleteMaxVisible !== newMaxVisible) {
184
+ this.autocompleteMaxVisible = newMaxVisible;
185
+ this.tui.requestRender();
186
+ }
187
+ }
188
+ setAutocompleteProvider(provider) {
189
+ this.autocompleteProvider = provider;
190
+ }
191
+ /**
192
+ * Add a prompt to history for up/down arrow navigation.
193
+ * Called after successful submission.
194
+ */
195
+ addToHistory(text) {
196
+ const trimmed = text.trim();
197
+ if (!trimmed)
198
+ return;
199
+ // Don't add consecutive duplicates
200
+ if (this.history.length > 0 && this.history[0] === trimmed)
201
+ return;
202
+ this.history.unshift(trimmed);
203
+ // Limit history size
204
+ if (this.history.length > 100) {
205
+ this.history.pop();
206
+ }
207
+ }
208
+ isEditorEmpty() {
209
+ return this.state.lines.length === 1 && this.state.lines[0] === "";
210
+ }
211
+ isOnFirstVisualLine() {
212
+ const visualLines = this.buildVisualLineMap(this.lastWidth);
213
+ const currentVisualLine = this.findCurrentVisualLine(visualLines);
214
+ return currentVisualLine === 0;
215
+ }
216
+ /**
217
+ * Public: whether the cursor is on the first visual line (accounts for soft
218
+ * wrapping). Consumers use this to decide whether ↑ should leave the editor
219
+ * (e.g. into an attachments bar above it) rather than move the cursor.
220
+ */
221
+ isCursorOnFirstVisualLine() {
222
+ return this.isOnFirstVisualLine();
223
+ }
224
+ isOnLastVisualLine() {
225
+ const visualLines = this.buildVisualLineMap(this.lastWidth);
226
+ const currentVisualLine = this.findCurrentVisualLine(visualLines);
227
+ return currentVisualLine === visualLines.length - 1;
228
+ }
229
+ navigateHistory(direction) {
230
+ this.lastAction = null;
231
+ if (this.history.length === 0)
232
+ return;
233
+ const newIndex = this.historyIndex - direction; // Up(-1) increases index, Down(1) decreases
234
+ if (newIndex < -1 || newIndex >= this.history.length)
235
+ return;
236
+ // Capture state when first entering history browsing mode
237
+ if (this.historyIndex === -1 && newIndex >= 0) {
238
+ this.pushUndoSnapshot();
239
+ }
240
+ this.historyIndex = newIndex;
241
+ if (this.historyIndex === -1) {
242
+ // Returned to "current" state - clear editor
243
+ this.setTextInternal("");
244
+ }
245
+ else {
246
+ this.setTextInternal(this.history[this.historyIndex] || "");
247
+ }
248
+ }
249
+ /** Internal setText that doesn't reset history state - used by navigateHistory */
250
+ setTextInternal(text) {
251
+ const lines = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
252
+ this.state.lines = lines.length === 0 ? [""] : lines;
253
+ this.state.cursorLine = this.state.lines.length - 1;
254
+ this.setCursorCol(this.state.lines[this.state.cursorLine]?.length || 0);
255
+ // Reset scroll - render() will adjust to show cursor
256
+ this.scrollOffset = 0;
257
+ if (this.onChange) {
258
+ this.onChange(this.getText());
259
+ }
260
+ }
261
+ invalidate() {
262
+ // No cached state to invalidate currently
263
+ }
264
+ render(width) {
265
+ const maxPadding = Math.max(0, Math.floor((width - 1) / 2));
266
+ const paddingX = Math.min(this.paddingX, maxPadding);
267
+ const contentWidth = Math.max(1, width - paddingX * 2);
268
+ // Layout width: with padding the cursor can overflow into it,
269
+ // without padding we reserve 1 column for the cursor.
270
+ const layoutWidth = Math.max(1, contentWidth - (paddingX ? 0 : 1));
271
+ // Store for cursor navigation (must match wrapping width)
272
+ this.lastWidth = layoutWidth;
273
+ const horizontal = this.borderColor("─");
274
+ // Layout the text
275
+ const layoutLines = this.layoutText(layoutWidth);
276
+ // Calculate max visible lines: 30% of terminal height, minimum 5 lines
277
+ const terminalRows = this.tui.terminal.rows;
278
+ const maxVisibleLines = Math.max(5, Math.floor(terminalRows * 0.3));
279
+ // Find the cursor line index in layoutLines
280
+ let cursorLineIndex = layoutLines.findIndex((line) => line.hasCursor);
281
+ if (cursorLineIndex === -1)
282
+ cursorLineIndex = 0;
283
+ // Adjust scroll offset to keep cursor visible
284
+ if (cursorLineIndex < this.scrollOffset) {
285
+ this.scrollOffset = cursorLineIndex;
286
+ }
287
+ else if (cursorLineIndex >= this.scrollOffset + maxVisibleLines) {
288
+ this.scrollOffset = cursorLineIndex - maxVisibleLines + 1;
289
+ }
290
+ // Clamp scroll offset to valid range
291
+ const maxScrollOffset = Math.max(0, layoutLines.length - maxVisibleLines);
292
+ this.scrollOffset = Math.max(0, Math.min(this.scrollOffset, maxScrollOffset));
293
+ // Get visible lines slice
294
+ const visibleLines = layoutLines.slice(this.scrollOffset, this.scrollOffset + maxVisibleLines);
295
+ const result = [];
296
+ const leftPadding = " ".repeat(paddingX);
297
+ const rightPadding = leftPadding;
298
+ // Render top border (with scroll indicator if scrolled down)
299
+ if (this.scrollOffset > 0) {
300
+ const indicator = `─── ↑ ${this.scrollOffset} more `;
301
+ const remaining = width - visibleWidth(indicator);
302
+ result.push(this.borderColor(indicator + "─".repeat(Math.max(0, remaining))));
303
+ }
304
+ else {
305
+ result.push(horizontal.repeat(width));
306
+ }
307
+ // Render each visible layout line
308
+ // Emit hardware cursor marker only when focused and not showing autocomplete
309
+ const emitCursorMarker = this.focused && !this.autocompleteState;
310
+ for (const layoutLine of visibleLines) {
311
+ let displayText = layoutLine.text;
312
+ let lineVisibleWidth = visibleWidth(layoutLine.text);
313
+ let cursorInPadding = false;
314
+ // Add cursor if this line has it
315
+ if (layoutLine.hasCursor && layoutLine.cursorPos !== undefined) {
316
+ const before = displayText.slice(0, layoutLine.cursorPos);
317
+ const after = displayText.slice(layoutLine.cursorPos);
318
+ // Hardware cursor marker (zero-width, emitted before fake cursor for IME positioning)
319
+ const marker = emitCursorMarker ? CURSOR_MARKER : "";
320
+ if (after.length > 0) {
321
+ // Cursor is on a character (grapheme) - replace it with highlighted version
322
+ // Get the first grapheme from 'after'
323
+ const afterGraphemes = [...segmenter.segment(after)];
324
+ const firstGrapheme = afterGraphemes[0]?.segment || "";
325
+ const restAfter = after.slice(firstGrapheme.length);
326
+ const cursor = `\x1b[7m${firstGrapheme}\x1b[0m`;
327
+ displayText = before + marker + cursor + restAfter;
328
+ // lineVisibleWidth stays the same - we're replacing, not adding
329
+ }
330
+ else {
331
+ // Cursor is at the end - add highlighted space
332
+ const cursor = "\x1b[7m \x1b[0m";
333
+ displayText = before + marker + cursor;
334
+ lineVisibleWidth = lineVisibleWidth + 1;
335
+ // If cursor overflows content width into the padding, flag it
336
+ if (lineVisibleWidth > contentWidth && paddingX > 0) {
337
+ cursorInPadding = true;
338
+ }
339
+ }
340
+ }
341
+ // Calculate padding based on actual visible width
342
+ const padding = " ".repeat(Math.max(0, contentWidth - lineVisibleWidth));
343
+ const lineRightPadding = cursorInPadding ? rightPadding.slice(1) : rightPadding;
344
+ // Render the line (no side borders, just horizontal lines above and below)
345
+ result.push(`${leftPadding}${displayText}${padding}${lineRightPadding}`);
346
+ }
347
+ // Render bottom border (with scroll indicator if more content below)
348
+ const linesBelow = layoutLines.length - (this.scrollOffset + visibleLines.length);
349
+ if (linesBelow > 0) {
350
+ const indicator = `─── ↓ ${linesBelow} more `;
351
+ const remaining = width - visibleWidth(indicator);
352
+ result.push(this.borderColor(indicator + "─".repeat(Math.max(0, remaining))));
353
+ }
354
+ else {
355
+ result.push(horizontal.repeat(width));
356
+ }
357
+ // Add autocomplete list if active
358
+ if (this.autocompleteState && this.autocompleteList) {
359
+ const autocompleteResult = this.autocompleteList.render(contentWidth);
360
+ for (const line of autocompleteResult) {
361
+ const lineWidth = visibleWidth(line);
362
+ const linePadding = " ".repeat(Math.max(0, contentWidth - lineWidth));
363
+ result.push(`${leftPadding}${line}${linePadding}${rightPadding}`);
364
+ }
365
+ }
366
+ return result;
367
+ }
368
+ handleInput(data) {
369
+ const kb = getEditorKeybindings();
370
+ // Handle character jump mode (awaiting next character to jump to)
371
+ if (this.jumpMode !== null) {
372
+ // Cancel if the hotkey is pressed again
373
+ if (kb.matches(data, "jumpForward") || kb.matches(data, "jumpBackward")) {
374
+ this.jumpMode = null;
375
+ return;
376
+ }
377
+ if (data.charCodeAt(0) >= 32) {
378
+ // Printable character - perform the jump
379
+ const direction = this.jumpMode;
380
+ this.jumpMode = null;
381
+ this.jumpToChar(data, direction);
382
+ return;
383
+ }
384
+ // Control character - cancel and fall through to normal handling
385
+ this.jumpMode = null;
386
+ }
387
+ // Handle bracketed paste mode
388
+ if (data.includes("\x1b[200~")) {
389
+ this.isInPaste = true;
390
+ this.pasteBuffer = "";
391
+ data = data.replace("\x1b[200~", "");
392
+ }
393
+ if (this.isInPaste) {
394
+ this.pasteBuffer += data;
395
+ const endIndex = this.pasteBuffer.indexOf("\x1b[201~");
396
+ if (endIndex !== -1) {
397
+ const pasteContent = this.pasteBuffer.substring(0, endIndex);
398
+ if (pasteContent.length > 0) {
399
+ this.handlePaste(pasteContent);
400
+ }
401
+ this.isInPaste = false;
402
+ const remaining = this.pasteBuffer.substring(endIndex + 6);
403
+ this.pasteBuffer = "";
404
+ if (remaining.length > 0) {
405
+ this.handleInput(remaining);
406
+ }
407
+ return;
408
+ }
409
+ return;
410
+ }
411
+ // Ctrl+C - let parent handle (exit/clear)
412
+ if (kb.matches(data, "copy")) {
413
+ return;
414
+ }
415
+ // Undo
416
+ if (kb.matches(data, "undo")) {
417
+ this.undo();
418
+ return;
419
+ }
420
+ // Handle autocomplete mode
421
+ if (this.autocompleteState && this.autocompleteList) {
422
+ if (kb.matches(data, "selectCancel")) {
423
+ this.cancelAutocomplete();
424
+ return;
425
+ }
426
+ if (kb.matches(data, "selectUp") || kb.matches(data, "selectDown")) {
427
+ this.autocompleteList.handleInput(data);
428
+ return;
429
+ }
430
+ if (kb.matches(data, "tab")) {
431
+ const selected = this.autocompleteList.getSelectedItem();
432
+ if (selected && this.autocompleteProvider) {
433
+ this.pushUndoSnapshot();
434
+ this.lastAction = null;
435
+ const result = this.autocompleteProvider.applyCompletion(this.state.lines, this.state.cursorLine, this.state.cursorCol, selected, this.autocompletePrefix);
436
+ this.state.lines = result.lines;
437
+ this.state.cursorLine = result.cursorLine;
438
+ this.setCursorCol(result.cursorCol);
439
+ this.cancelAutocomplete();
440
+ if (this.onChange)
441
+ this.onChange(this.getText());
442
+ }
443
+ return;
444
+ }
445
+ if (kb.matches(data, "selectConfirm")) {
446
+ const selected = this.autocompleteList.getSelectedItem();
447
+ if (selected && this.autocompleteProvider) {
448
+ this.pushUndoSnapshot();
449
+ this.lastAction = null;
450
+ const result = this.autocompleteProvider.applyCompletion(this.state.lines, this.state.cursorLine, this.state.cursorCol, selected, this.autocompletePrefix);
451
+ this.state.lines = result.lines;
452
+ this.state.cursorLine = result.cursorLine;
453
+ this.setCursorCol(result.cursorCol);
454
+ if (this.autocompletePrefix.startsWith("/")) {
455
+ this.cancelAutocomplete();
456
+ // Fall through to submit
457
+ }
458
+ else {
459
+ this.cancelAutocomplete();
460
+ if (this.onChange)
461
+ this.onChange(this.getText());
462
+ return;
463
+ }
464
+ }
465
+ }
466
+ }
467
+ // Tab - trigger completion
468
+ if (kb.matches(data, "tab") && !this.autocompleteState) {
469
+ this.handleTabCompletion();
470
+ return;
471
+ }
472
+ // Deletion actions
473
+ if (kb.matches(data, "deleteToLineEnd")) {
474
+ this.deleteToEndOfLine();
475
+ return;
476
+ }
477
+ if (kb.matches(data, "deleteToLineStart")) {
478
+ this.deleteToStartOfLine();
479
+ return;
480
+ }
481
+ if (kb.matches(data, "deleteWordBackward")) {
482
+ this.deleteWordBackwards();
483
+ return;
484
+ }
485
+ if (kb.matches(data, "deleteWordForward")) {
486
+ this.deleteWordForward();
487
+ return;
488
+ }
489
+ if (kb.matches(data, "deleteCharBackward") || matchesKey(data, "shift+backspace")) {
490
+ this.handleBackspace();
491
+ return;
492
+ }
493
+ if (kb.matches(data, "deleteCharForward") || matchesKey(data, "shift+delete")) {
494
+ this.handleForwardDelete();
495
+ return;
496
+ }
497
+ // Kill ring actions
498
+ if (kb.matches(data, "yank")) {
499
+ this.yank();
500
+ return;
501
+ }
502
+ if (kb.matches(data, "yankPop")) {
503
+ this.yankPop();
504
+ return;
505
+ }
506
+ // Cursor movement actions
507
+ if (kb.matches(data, "cursorLineStart")) {
508
+ this.moveToLineStart();
509
+ return;
510
+ }
511
+ if (kb.matches(data, "cursorLineEnd")) {
512
+ this.moveToLineEnd();
513
+ return;
514
+ }
515
+ if (kb.matches(data, "cursorWordLeft")) {
516
+ this.moveWordBackwards();
517
+ return;
518
+ }
519
+ if (kb.matches(data, "cursorWordRight")) {
520
+ this.moveWordForwards();
521
+ return;
522
+ }
523
+ // New line
524
+ if (kb.matches(data, "newLine") ||
525
+ (data.charCodeAt(0) === 10 && data.length > 1) ||
526
+ data === "\x1b\r" ||
527
+ data === "\x1b[13;2~" ||
528
+ (data.length > 1 && data.includes("\x1b") && data.includes("\r")) ||
529
+ (data === "\n" && data.length === 1)) {
530
+ if (this.shouldSubmitOnBackslashEnter(data, kb)) {
531
+ this.handleBackspace();
532
+ this.submitValue();
533
+ return;
534
+ }
535
+ this.addNewLine();
536
+ return;
537
+ }
538
+ // Submit (Enter)
539
+ if (kb.matches(data, "submit")) {
540
+ if (this.disableSubmit)
541
+ return;
542
+ // Workaround for terminals without Shift+Enter support:
543
+ // If char before cursor is \, delete it and insert newline instead of submitting.
544
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
545
+ if (this.state.cursorCol > 0 && currentLine[this.state.cursorCol - 1] === "\\") {
546
+ this.handleBackspace();
547
+ this.addNewLine();
548
+ return;
549
+ }
550
+ this.submitValue();
551
+ return;
552
+ }
553
+ // Arrow key navigation (with history support)
554
+ if (kb.matches(data, "cursorUp")) {
555
+ if (this.isEditorEmpty()) {
556
+ this.navigateHistory(-1);
557
+ }
558
+ else if (this.historyIndex > -1 && this.isOnFirstVisualLine()) {
559
+ this.navigateHistory(-1);
560
+ }
561
+ else if (this.isOnFirstVisualLine()) {
562
+ // Already at top - jump to start of line
563
+ this.moveToLineStart();
564
+ }
565
+ else {
566
+ this.moveCursor(-1, 0);
567
+ }
568
+ return;
569
+ }
570
+ if (kb.matches(data, "cursorDown")) {
571
+ if (this.historyIndex > -1 && this.isOnLastVisualLine()) {
572
+ this.navigateHistory(1);
573
+ }
574
+ else if (this.isOnLastVisualLine()) {
575
+ // Already at bottom - jump to end of line
576
+ this.moveToLineEnd();
577
+ }
578
+ else {
579
+ this.moveCursor(1, 0);
580
+ }
581
+ return;
582
+ }
583
+ if (kb.matches(data, "cursorRight")) {
584
+ this.moveCursor(0, 1);
585
+ return;
586
+ }
587
+ if (kb.matches(data, "cursorLeft")) {
588
+ this.moveCursor(0, -1);
589
+ return;
590
+ }
591
+ // Page up/down - scroll by page and move cursor
592
+ if (kb.matches(data, "pageUp")) {
593
+ this.pageScroll(-1);
594
+ return;
595
+ }
596
+ if (kb.matches(data, "pageDown")) {
597
+ this.pageScroll(1);
598
+ return;
599
+ }
600
+ // Character jump mode triggers
601
+ if (kb.matches(data, "jumpForward")) {
602
+ this.jumpMode = "forward";
603
+ return;
604
+ }
605
+ if (kb.matches(data, "jumpBackward")) {
606
+ this.jumpMode = "backward";
607
+ return;
608
+ }
609
+ // Shift+Space - insert regular space
610
+ if (matchesKey(data, "shift+space")) {
611
+ this.insertCharacter(" ");
612
+ return;
613
+ }
614
+ const kittyPrintable = decodeKittyPrintable(data);
615
+ if (kittyPrintable !== undefined) {
616
+ this.insertCharacter(kittyPrintable);
617
+ return;
618
+ }
619
+ // Regular characters
620
+ if (data.charCodeAt(0) >= 32) {
621
+ this.insertCharacter(data);
622
+ }
623
+ }
624
+ layoutText(contentWidth) {
625
+ const layoutLines = [];
626
+ if (this.state.lines.length === 0 || (this.state.lines.length === 1 && this.state.lines[0] === "")) {
627
+ // Empty editor - show placeholder if available
628
+ const placeholderText = this.placeholder || "";
629
+ layoutLines.push({
630
+ text: placeholderText,
631
+ hasCursor: true,
632
+ cursorPos: 0,
633
+ isPlaceholder: placeholderText !== "",
634
+ });
635
+ return layoutLines;
636
+ }
637
+ // Process each logical line
638
+ for (let i = 0; i < this.state.lines.length; i++) {
639
+ const line = this.state.lines[i] || "";
640
+ const isCurrentLine = i === this.state.cursorLine;
641
+ const lineVisibleWidth = visibleWidth(line);
642
+ if (lineVisibleWidth <= contentWidth) {
643
+ // Line fits in one layout line
644
+ if (isCurrentLine) {
645
+ layoutLines.push({
646
+ text: line,
647
+ hasCursor: true,
648
+ cursorPos: this.state.cursorCol,
649
+ });
650
+ }
651
+ else {
652
+ layoutLines.push({
653
+ text: line,
654
+ hasCursor: false,
655
+ });
656
+ }
657
+ }
658
+ else {
659
+ // Line needs wrapping - use word-aware wrapping
660
+ const chunks = wordWrapLine(line, contentWidth);
661
+ for (let chunkIndex = 0; chunkIndex < chunks.length; chunkIndex++) {
662
+ const chunk = chunks[chunkIndex];
663
+ if (!chunk)
664
+ continue;
665
+ const cursorPos = this.state.cursorCol;
666
+ const isLastChunk = chunkIndex === chunks.length - 1;
667
+ // Determine if cursor is in this chunk
668
+ // For word-wrapped chunks, we need to handle the case where
669
+ // cursor might be in trimmed whitespace at end of chunk
670
+ let hasCursorInChunk = false;
671
+ let adjustedCursorPos = 0;
672
+ if (isCurrentLine) {
673
+ if (isLastChunk) {
674
+ // Last chunk: cursor belongs here if >= startIndex
675
+ hasCursorInChunk = cursorPos >= chunk.startIndex;
676
+ adjustedCursorPos = cursorPos - chunk.startIndex;
677
+ }
678
+ else {
679
+ // Non-last chunk: cursor belongs here if in range [startIndex, endIndex)
680
+ // But we need to handle the visual position in the trimmed text
681
+ hasCursorInChunk = cursorPos >= chunk.startIndex && cursorPos < chunk.endIndex;
682
+ if (hasCursorInChunk) {
683
+ adjustedCursorPos = cursorPos - chunk.startIndex;
684
+ // Clamp to text length (in case cursor was in trimmed whitespace)
685
+ if (adjustedCursorPos > chunk.text.length) {
686
+ adjustedCursorPos = chunk.text.length;
687
+ }
688
+ }
689
+ }
690
+ }
691
+ if (hasCursorInChunk) {
692
+ layoutLines.push({
693
+ text: chunk.text,
694
+ hasCursor: true,
695
+ cursorPos: adjustedCursorPos,
696
+ });
697
+ }
698
+ else {
699
+ layoutLines.push({
700
+ text: chunk.text,
701
+ hasCursor: false,
702
+ });
703
+ }
704
+ }
705
+ }
706
+ }
707
+ return layoutLines;
708
+ }
709
+ getText() {
710
+ return this.state.lines.join("\n");
711
+ }
712
+ /**
713
+ * Get text with paste markers expanded to their actual content.
714
+ * Use this when you need the full content (e.g., for external editor).
715
+ */
716
+ getExpandedText() {
717
+ let result = this.state.lines.join("\n");
718
+ for (const [pasteId, pasteContent] of this.pastes) {
719
+ const markerRegex = new RegExp(`\\[paste #${pasteId}( (\\+\\d+ lines|\\d+ chars))?\\]`, "g");
720
+ result = result.replace(markerRegex, pasteContent);
721
+ }
722
+ return result;
723
+ }
724
+ getLines() {
725
+ return [...this.state.lines];
726
+ }
727
+ getCursor() {
728
+ return { line: this.state.cursorLine, col: this.state.cursorCol };
729
+ }
730
+ setText(text) {
731
+ this.lastAction = null;
732
+ this.historyIndex = -1; // Exit history browsing mode
733
+ // Push undo snapshot if content differs (makes programmatic changes undoable)
734
+ if (this.getText() !== text) {
735
+ this.pushUndoSnapshot();
736
+ }
737
+ this.setTextInternal(text);
738
+ }
739
+ /**
740
+ * Insert text at the current cursor position.
741
+ * Used for programmatic insertion (e.g., clipboard image markers).
742
+ * This is atomic for undo - single undo restores entire pre-insert state.
743
+ */
744
+ insertTextAtCursor(text) {
745
+ if (!text)
746
+ return;
747
+ this.pushUndoSnapshot();
748
+ this.lastAction = null;
749
+ this.historyIndex = -1;
750
+ this.insertTextAtCursorInternal(text);
751
+ }
752
+ /**
753
+ * Internal text insertion at cursor. Handles single and multi-line text.
754
+ * Does not push undo snapshots or trigger autocomplete - caller is responsible.
755
+ * Normalizes line endings and calls onChange once at the end.
756
+ */
757
+ insertTextAtCursorInternal(text) {
758
+ if (!text)
759
+ return;
760
+ // Normalize line endings
761
+ const normalized = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
762
+ const insertedLines = normalized.split("\n");
763
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
764
+ const beforeCursor = currentLine.slice(0, this.state.cursorCol);
765
+ const afterCursor = currentLine.slice(this.state.cursorCol);
766
+ if (insertedLines.length === 1) {
767
+ // Single line - insert at cursor position
768
+ this.state.lines[this.state.cursorLine] = beforeCursor + normalized + afterCursor;
769
+ this.setCursorCol(this.state.cursorCol + normalized.length);
770
+ }
771
+ else {
772
+ // Multi-line insertion
773
+ this.state.lines = [
774
+ // All lines before current line
775
+ ...this.state.lines.slice(0, this.state.cursorLine),
776
+ // The first inserted line merged with text before cursor
777
+ beforeCursor + insertedLines[0],
778
+ // All middle inserted lines
779
+ ...insertedLines.slice(1, -1),
780
+ // The last inserted line with text after cursor
781
+ insertedLines[insertedLines.length - 1] + afterCursor,
782
+ // All lines after current line
783
+ ...this.state.lines.slice(this.state.cursorLine + 1),
784
+ ];
785
+ this.state.cursorLine += insertedLines.length - 1;
786
+ this.setCursorCol((insertedLines[insertedLines.length - 1] || "").length);
787
+ }
788
+ if (this.onChange) {
789
+ this.onChange(this.getText());
790
+ }
791
+ }
792
+ // All the editor methods from before...
793
+ insertCharacter(char, skipUndoCoalescing) {
794
+ this.historyIndex = -1; // Exit history browsing mode
795
+ // Undo coalescing (fish-style):
796
+ // - Consecutive word chars coalesce into one undo unit
797
+ // - Space captures state before itself (so undo removes space+following word together)
798
+ // - Each space is separately undoable
799
+ // Skip coalescing when called from atomic operations (e.g., handlePaste)
800
+ if (!skipUndoCoalescing) {
801
+ if (isWhitespaceChar(char) || this.lastAction !== "type-word") {
802
+ this.pushUndoSnapshot();
803
+ }
804
+ this.lastAction = "type-word";
805
+ }
806
+ const line = this.state.lines[this.state.cursorLine] || "";
807
+ const before = line.slice(0, this.state.cursorCol);
808
+ const after = line.slice(this.state.cursorCol);
809
+ this.state.lines[this.state.cursorLine] = before + char + after;
810
+ this.setCursorCol(this.state.cursorCol + char.length);
811
+ if (this.onChange) {
812
+ this.onChange(this.getText());
813
+ }
814
+ // Check if we should trigger or update autocomplete
815
+ if (!this.autocompleteState) {
816
+ // Auto-trigger for "/" at the start of a line (slash commands)
817
+ if (char === "/" && this.isAtStartOfMessage()) {
818
+ this.tryTriggerAutocomplete();
819
+ }
820
+ // Auto-trigger for "@" file reference (fuzzy search)
821
+ else if (char === "@") {
822
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
823
+ const textBeforeCursor = currentLine.slice(0, this.state.cursorCol);
824
+ // Only trigger if @ is after whitespace or at start of line
825
+ const charBeforeAt = textBeforeCursor[textBeforeCursor.length - 2];
826
+ if (textBeforeCursor.length === 1 || charBeforeAt === " " || charBeforeAt === "\t") {
827
+ this.tryTriggerAutocomplete();
828
+ }
829
+ }
830
+ // Also auto-trigger when typing letters in a slash command context
831
+ else if (/[a-zA-Z0-9.\-_]/.test(char)) {
832
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
833
+ const textBeforeCursor = currentLine.slice(0, this.state.cursorCol);
834
+ // Check if we're in a slash command (with or without space for arguments)
835
+ if (this.isInSlashCommandContext(textBeforeCursor)) {
836
+ this.tryTriggerAutocomplete();
837
+ }
838
+ // Check if we're in an @ file reference context
839
+ else if (textBeforeCursor.match(/(?:^|[\s])@[^\s]*$/)) {
840
+ this.tryTriggerAutocomplete();
841
+ }
842
+ }
843
+ }
844
+ else {
845
+ this.updateAutocomplete();
846
+ }
847
+ }
848
+ handlePaste(pastedText) {
849
+ this.historyIndex = -1; // Exit history browsing mode
850
+ this.lastAction = null;
851
+ this.pushUndoSnapshot();
852
+ // Clean the pasted text
853
+ const cleanText = pastedText.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
854
+ // Convert tabs to spaces (4 spaces per tab)
855
+ const tabExpandedText = cleanText.replace(/\t/g, " ");
856
+ // Filter out non-printable characters except newlines
857
+ let filteredText = tabExpandedText
858
+ .split("")
859
+ .filter((char) => char === "\n" || char.charCodeAt(0) >= 32)
860
+ .join("");
861
+ // If pasting a file path (starts with /, ~, or .) and the character before
862
+ // the cursor is a word character, prepend a space for better readability
863
+ if (/^[/~.]/.test(filteredText)) {
864
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
865
+ const charBeforeCursor = this.state.cursorCol > 0 ? currentLine[this.state.cursorCol - 1] : "";
866
+ if (charBeforeCursor && /\w/.test(charBeforeCursor)) {
867
+ filteredText = ` ${filteredText}`;
868
+ }
869
+ }
870
+ // Split into lines to check for large paste
871
+ const pastedLines = filteredText.split("\n");
872
+ // Check if this is a large paste (> 10 lines or > 1000 characters)
873
+ const totalChars = filteredText.length;
874
+ if (pastedLines.length > 10 || totalChars > 1000) {
875
+ // Store the paste and insert a marker
876
+ this.pasteCounter++;
877
+ const pasteId = this.pasteCounter;
878
+ this.pastes.set(pasteId, filteredText);
879
+ // Insert marker like "[paste #1 +123 lines]" or "[paste #1 1234 chars]"
880
+ const marker = pastedLines.length > 10
881
+ ? `[paste #${pasteId} +${pastedLines.length} lines]`
882
+ : `[paste #${pasteId} ${totalChars} chars]`;
883
+ this.insertTextAtCursorInternal(marker);
884
+ return;
885
+ }
886
+ if (pastedLines.length === 1) {
887
+ // Single line - insert character by character to trigger autocomplete
888
+ for (const char of filteredText) {
889
+ this.insertCharacter(char, true);
890
+ }
891
+ return;
892
+ }
893
+ // Multi-line paste - use direct state manipulation
894
+ this.insertTextAtCursorInternal(filteredText);
895
+ }
896
+ addNewLine() {
897
+ this.historyIndex = -1; // Exit history browsing mode
898
+ this.lastAction = null;
899
+ this.pushUndoSnapshot();
900
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
901
+ const before = currentLine.slice(0, this.state.cursorCol);
902
+ const after = currentLine.slice(this.state.cursorCol);
903
+ // Split current line
904
+ this.state.lines[this.state.cursorLine] = before;
905
+ this.state.lines.splice(this.state.cursorLine + 1, 0, after);
906
+ // Move cursor to start of new line
907
+ this.state.cursorLine++;
908
+ this.setCursorCol(0);
909
+ if (this.onChange) {
910
+ this.onChange(this.getText());
911
+ }
912
+ }
913
+ shouldSubmitOnBackslashEnter(data, kb) {
914
+ if (this.disableSubmit)
915
+ return false;
916
+ if (!matchesKey(data, "enter"))
917
+ return false;
918
+ const submitKeys = kb.getKeys("submit");
919
+ const hasShiftEnter = submitKeys.includes("shift+enter") || submitKeys.includes("shift+return");
920
+ if (!hasShiftEnter)
921
+ return false;
922
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
923
+ return this.state.cursorCol > 0 && currentLine[this.state.cursorCol - 1] === "\\";
924
+ }
925
+ submitValue() {
926
+ let result = this.state.lines.join("\n").trim();
927
+ for (const [pasteId, pasteContent] of this.pastes) {
928
+ const markerRegex = new RegExp(`\\[paste #${pasteId}( (\\+\\d+ lines|\\d+ chars))?\\]`, "g");
929
+ result = result.replace(markerRegex, pasteContent);
930
+ }
931
+ this.state = { lines: [""], cursorLine: 0, cursorCol: 0 };
932
+ this.pastes.clear();
933
+ this.pasteCounter = 0;
934
+ this.historyIndex = -1;
935
+ this.scrollOffset = 0;
936
+ this.undoStack.clear();
937
+ this.lastAction = null;
938
+ if (this.onChange)
939
+ this.onChange("");
940
+ if (this.onSubmit) {
941
+ this.onSubmit(result);
942
+ }
943
+ }
944
+ handleBackspace() {
945
+ this.historyIndex = -1; // Exit history browsing mode
946
+ this.lastAction = null;
947
+ if (this.state.cursorCol > 0) {
948
+ this.pushUndoSnapshot();
949
+ // Delete grapheme before cursor (handles emojis, combining characters, etc.)
950
+ const line = this.state.lines[this.state.cursorLine] || "";
951
+ const beforeCursor = line.slice(0, this.state.cursorCol);
952
+ // Find the last grapheme in the text before cursor
953
+ const graphemes = [...segmenter.segment(beforeCursor)];
954
+ const lastGrapheme = graphemes[graphemes.length - 1];
955
+ const graphemeLength = lastGrapheme ? lastGrapheme.segment.length : 1;
956
+ const before = line.slice(0, this.state.cursorCol - graphemeLength);
957
+ const after = line.slice(this.state.cursorCol);
958
+ this.state.lines[this.state.cursorLine] = before + after;
959
+ this.setCursorCol(this.state.cursorCol - graphemeLength);
960
+ }
961
+ else if (this.state.cursorLine > 0) {
962
+ this.pushUndoSnapshot();
963
+ // Merge with previous line
964
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
965
+ const previousLine = this.state.lines[this.state.cursorLine - 1] || "";
966
+ this.state.lines[this.state.cursorLine - 1] = previousLine + currentLine;
967
+ this.state.lines.splice(this.state.cursorLine, 1);
968
+ this.state.cursorLine--;
969
+ this.setCursorCol(previousLine.length);
970
+ }
971
+ if (this.onChange) {
972
+ this.onChange(this.getText());
973
+ }
974
+ // Update or re-trigger autocomplete after backspace
975
+ if (this.autocompleteState) {
976
+ this.updateAutocomplete();
977
+ }
978
+ else {
979
+ // If autocomplete was cancelled (no matches), re-trigger if we're in a completable context
980
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
981
+ const textBeforeCursor = currentLine.slice(0, this.state.cursorCol);
982
+ // Slash command context
983
+ if (this.isInSlashCommandContext(textBeforeCursor)) {
984
+ this.tryTriggerAutocomplete();
985
+ }
986
+ // @ file reference context
987
+ else if (textBeforeCursor.match(/(?:^|[\s])@[^\s]*$/)) {
988
+ this.tryTriggerAutocomplete();
989
+ }
990
+ }
991
+ }
992
+ /**
993
+ * Set cursor column and clear preferredVisualCol.
994
+ * Use this for all non-vertical cursor movements to reset sticky column behavior.
995
+ */
996
+ setCursorCol(col) {
997
+ this.state.cursorCol = col;
998
+ this.preferredVisualCol = null;
999
+ }
1000
+ /**
1001
+ * Move cursor to a target visual line, applying sticky column logic.
1002
+ * Shared by moveCursor() and pageScroll().
1003
+ */
1004
+ moveToVisualLine(visualLines, currentVisualLine, targetVisualLine) {
1005
+ const currentVL = visualLines[currentVisualLine];
1006
+ const targetVL = visualLines[targetVisualLine];
1007
+ if (currentVL && targetVL) {
1008
+ const currentVisualCol = this.state.cursorCol - currentVL.startCol;
1009
+ // For non-last segments, clamp to length-1 to stay within the segment
1010
+ const isLastSourceSegment = currentVisualLine === visualLines.length - 1 ||
1011
+ visualLines[currentVisualLine + 1]?.logicalLine !== currentVL.logicalLine;
1012
+ const sourceMaxVisualCol = isLastSourceSegment ? currentVL.length : Math.max(0, currentVL.length - 1);
1013
+ const isLastTargetSegment = targetVisualLine === visualLines.length - 1 ||
1014
+ visualLines[targetVisualLine + 1]?.logicalLine !== targetVL.logicalLine;
1015
+ const targetMaxVisualCol = isLastTargetSegment ? targetVL.length : Math.max(0, targetVL.length - 1);
1016
+ const moveToVisualCol = this.computeVerticalMoveColumn(currentVisualCol, sourceMaxVisualCol, targetMaxVisualCol);
1017
+ // Set cursor position
1018
+ this.state.cursorLine = targetVL.logicalLine;
1019
+ const targetCol = targetVL.startCol + moveToVisualCol;
1020
+ const logicalLine = this.state.lines[targetVL.logicalLine] || "";
1021
+ this.state.cursorCol = Math.min(targetCol, logicalLine.length);
1022
+ }
1023
+ }
1024
+ /**
1025
+ * Compute the target visual column for vertical cursor movement.
1026
+ * Implements the sticky column decision table:
1027
+ *
1028
+ * | P | S | T | U | Scenario | Set Preferred | Move To |
1029
+ * |---|---|---|---| ---------------------------------------------------- |---------------|-------------|
1030
+ * | 0 | * | 0 | - | Start nav, target fits | null | current |
1031
+ * | 0 | * | 1 | - | Start nav, target shorter | current | target end |
1032
+ * | 1 | 0 | 0 | 0 | Clamped, target fits preferred | null | preferred |
1033
+ * | 1 | 0 | 0 | 1 | Clamped, target longer but still can't fit preferred | keep | target end |
1034
+ * | 1 | 0 | 1 | - | Clamped, target even shorter | keep | target end |
1035
+ * | 1 | 1 | 0 | - | Rewrapped, target fits current | null | current |
1036
+ * | 1 | 1 | 1 | - | Rewrapped, target shorter than current | current | target end |
1037
+ *
1038
+ * Where:
1039
+ * - P = preferred col is set
1040
+ * - S = cursor in middle of source line (not clamped to end)
1041
+ * - T = target line shorter than current visual col
1042
+ * - U = target line shorter than preferred col
1043
+ */
1044
+ computeVerticalMoveColumn(currentVisualCol, sourceMaxVisualCol, targetMaxVisualCol) {
1045
+ const hasPreferred = this.preferredVisualCol !== null; // P
1046
+ const cursorInMiddle = currentVisualCol < sourceMaxVisualCol; // S
1047
+ const targetTooShort = targetMaxVisualCol < currentVisualCol; // T
1048
+ if (!hasPreferred || cursorInMiddle) {
1049
+ if (targetTooShort) {
1050
+ // Cases 2 and 7
1051
+ this.preferredVisualCol = currentVisualCol;
1052
+ return targetMaxVisualCol;
1053
+ }
1054
+ // Cases 1 and 6
1055
+ this.preferredVisualCol = null;
1056
+ return currentVisualCol;
1057
+ }
1058
+ const targetCantFitPreferred = targetMaxVisualCol < this.preferredVisualCol; // U
1059
+ if (targetTooShort || targetCantFitPreferred) {
1060
+ // Cases 4 and 5
1061
+ return targetMaxVisualCol;
1062
+ }
1063
+ // Case 3
1064
+ const result = this.preferredVisualCol;
1065
+ this.preferredVisualCol = null;
1066
+ return result;
1067
+ }
1068
+ moveToLineStart() {
1069
+ this.lastAction = null;
1070
+ this.setCursorCol(0);
1071
+ }
1072
+ moveToLineEnd() {
1073
+ this.lastAction = null;
1074
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1075
+ this.setCursorCol(currentLine.length);
1076
+ }
1077
+ deleteToStartOfLine() {
1078
+ this.historyIndex = -1; // Exit history browsing mode
1079
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1080
+ if (this.state.cursorCol > 0) {
1081
+ this.pushUndoSnapshot();
1082
+ // Calculate text to be deleted and save to kill ring (backward deletion = prepend)
1083
+ const deletedText = currentLine.slice(0, this.state.cursorCol);
1084
+ this.killRing.push(deletedText, { prepend: true, accumulate: this.lastAction === "kill" });
1085
+ this.lastAction = "kill";
1086
+ // Delete from start of line up to cursor
1087
+ this.state.lines[this.state.cursorLine] = currentLine.slice(this.state.cursorCol);
1088
+ this.setCursorCol(0);
1089
+ }
1090
+ else if (this.state.cursorLine > 0) {
1091
+ this.pushUndoSnapshot();
1092
+ // At start of line - merge with previous line, treating newline as deleted text
1093
+ this.killRing.push("\n", { prepend: true, accumulate: this.lastAction === "kill" });
1094
+ this.lastAction = "kill";
1095
+ const previousLine = this.state.lines[this.state.cursorLine - 1] || "";
1096
+ this.state.lines[this.state.cursorLine - 1] = previousLine + currentLine;
1097
+ this.state.lines.splice(this.state.cursorLine, 1);
1098
+ this.state.cursorLine--;
1099
+ this.setCursorCol(previousLine.length);
1100
+ }
1101
+ if (this.onChange) {
1102
+ this.onChange(this.getText());
1103
+ }
1104
+ }
1105
+ deleteToEndOfLine() {
1106
+ this.historyIndex = -1; // Exit history browsing mode
1107
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1108
+ if (this.state.cursorCol < currentLine.length) {
1109
+ this.pushUndoSnapshot();
1110
+ // Calculate text to be deleted and save to kill ring (forward deletion = append)
1111
+ const deletedText = currentLine.slice(this.state.cursorCol);
1112
+ this.killRing.push(deletedText, { prepend: false, accumulate: this.lastAction === "kill" });
1113
+ this.lastAction = "kill";
1114
+ // Delete from cursor to end of line
1115
+ this.state.lines[this.state.cursorLine] = currentLine.slice(0, this.state.cursorCol);
1116
+ }
1117
+ else if (this.state.cursorLine < this.state.lines.length - 1) {
1118
+ this.pushUndoSnapshot();
1119
+ // At end of line - merge with next line, treating newline as deleted text
1120
+ this.killRing.push("\n", { prepend: false, accumulate: this.lastAction === "kill" });
1121
+ this.lastAction = "kill";
1122
+ const nextLine = this.state.lines[this.state.cursorLine + 1] || "";
1123
+ this.state.lines[this.state.cursorLine] = currentLine + nextLine;
1124
+ this.state.lines.splice(this.state.cursorLine + 1, 1);
1125
+ }
1126
+ if (this.onChange) {
1127
+ this.onChange(this.getText());
1128
+ }
1129
+ }
1130
+ deleteWordBackwards() {
1131
+ this.historyIndex = -1; // Exit history browsing mode
1132
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1133
+ // If at start of line, behave like backspace at column 0 (merge with previous line)
1134
+ if (this.state.cursorCol === 0) {
1135
+ if (this.state.cursorLine > 0) {
1136
+ this.pushUndoSnapshot();
1137
+ // Treat newline as deleted text (backward deletion = prepend)
1138
+ this.killRing.push("\n", { prepend: true, accumulate: this.lastAction === "kill" });
1139
+ this.lastAction = "kill";
1140
+ const previousLine = this.state.lines[this.state.cursorLine - 1] || "";
1141
+ this.state.lines[this.state.cursorLine - 1] = previousLine + currentLine;
1142
+ this.state.lines.splice(this.state.cursorLine, 1);
1143
+ this.state.cursorLine--;
1144
+ this.setCursorCol(previousLine.length);
1145
+ }
1146
+ }
1147
+ else {
1148
+ this.pushUndoSnapshot();
1149
+ // Save lastAction before cursor movement (moveWordBackwards resets it)
1150
+ const wasKill = this.lastAction === "kill";
1151
+ const oldCursorCol = this.state.cursorCol;
1152
+ this.moveWordBackwards();
1153
+ const deleteFrom = this.state.cursorCol;
1154
+ this.setCursorCol(oldCursorCol);
1155
+ const deletedText = currentLine.slice(deleteFrom, this.state.cursorCol);
1156
+ this.killRing.push(deletedText, { prepend: true, accumulate: wasKill });
1157
+ this.lastAction = "kill";
1158
+ this.state.lines[this.state.cursorLine] =
1159
+ currentLine.slice(0, deleteFrom) + currentLine.slice(this.state.cursorCol);
1160
+ this.setCursorCol(deleteFrom);
1161
+ }
1162
+ if (this.onChange) {
1163
+ this.onChange(this.getText());
1164
+ }
1165
+ }
1166
+ deleteWordForward() {
1167
+ this.historyIndex = -1; // Exit history browsing mode
1168
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1169
+ // If at end of line, merge with next line (delete the newline)
1170
+ if (this.state.cursorCol >= currentLine.length) {
1171
+ if (this.state.cursorLine < this.state.lines.length - 1) {
1172
+ this.pushUndoSnapshot();
1173
+ // Treat newline as deleted text (forward deletion = append)
1174
+ this.killRing.push("\n", { prepend: false, accumulate: this.lastAction === "kill" });
1175
+ this.lastAction = "kill";
1176
+ const nextLine = this.state.lines[this.state.cursorLine + 1] || "";
1177
+ this.state.lines[this.state.cursorLine] = currentLine + nextLine;
1178
+ this.state.lines.splice(this.state.cursorLine + 1, 1);
1179
+ }
1180
+ }
1181
+ else {
1182
+ this.pushUndoSnapshot();
1183
+ // Save lastAction before cursor movement (moveWordForwards resets it)
1184
+ const wasKill = this.lastAction === "kill";
1185
+ const oldCursorCol = this.state.cursorCol;
1186
+ this.moveWordForwards();
1187
+ const deleteTo = this.state.cursorCol;
1188
+ this.setCursorCol(oldCursorCol);
1189
+ const deletedText = currentLine.slice(this.state.cursorCol, deleteTo);
1190
+ this.killRing.push(deletedText, { prepend: false, accumulate: wasKill });
1191
+ this.lastAction = "kill";
1192
+ this.state.lines[this.state.cursorLine] =
1193
+ currentLine.slice(0, this.state.cursorCol) + currentLine.slice(deleteTo);
1194
+ }
1195
+ if (this.onChange) {
1196
+ this.onChange(this.getText());
1197
+ }
1198
+ }
1199
+ handleForwardDelete() {
1200
+ this.historyIndex = -1; // Exit history browsing mode
1201
+ this.lastAction = null;
1202
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1203
+ if (this.state.cursorCol < currentLine.length) {
1204
+ this.pushUndoSnapshot();
1205
+ // Delete grapheme at cursor position (handles emojis, combining characters, etc.)
1206
+ const afterCursor = currentLine.slice(this.state.cursorCol);
1207
+ // Find the first grapheme at cursor
1208
+ const graphemes = [...segmenter.segment(afterCursor)];
1209
+ const firstGrapheme = graphemes[0];
1210
+ const graphemeLength = firstGrapheme ? firstGrapheme.segment.length : 1;
1211
+ const before = currentLine.slice(0, this.state.cursorCol);
1212
+ const after = currentLine.slice(this.state.cursorCol + graphemeLength);
1213
+ this.state.lines[this.state.cursorLine] = before + after;
1214
+ }
1215
+ else if (this.state.cursorLine < this.state.lines.length - 1) {
1216
+ this.pushUndoSnapshot();
1217
+ // At end of line - merge with next line
1218
+ const nextLine = this.state.lines[this.state.cursorLine + 1] || "";
1219
+ this.state.lines[this.state.cursorLine] = currentLine + nextLine;
1220
+ this.state.lines.splice(this.state.cursorLine + 1, 1);
1221
+ }
1222
+ if (this.onChange) {
1223
+ this.onChange(this.getText());
1224
+ }
1225
+ // Update or re-trigger autocomplete after forward delete
1226
+ if (this.autocompleteState) {
1227
+ this.updateAutocomplete();
1228
+ }
1229
+ else {
1230
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1231
+ const textBeforeCursor = currentLine.slice(0, this.state.cursorCol);
1232
+ // Slash command context
1233
+ if (this.isInSlashCommandContext(textBeforeCursor)) {
1234
+ this.tryTriggerAutocomplete();
1235
+ }
1236
+ // @ file reference context
1237
+ else if (textBeforeCursor.match(/(?:^|[\s])@[^\s]*$/)) {
1238
+ this.tryTriggerAutocomplete();
1239
+ }
1240
+ }
1241
+ }
1242
+ /**
1243
+ * Build a mapping from visual lines to logical positions.
1244
+ * Returns an array where each element represents a visual line with:
1245
+ * - logicalLine: index into this.state.lines
1246
+ * - startCol: starting column in the logical line
1247
+ * - length: length of this visual line segment
1248
+ */
1249
+ buildVisualLineMap(width) {
1250
+ const visualLines = [];
1251
+ for (let i = 0; i < this.state.lines.length; i++) {
1252
+ const line = this.state.lines[i] || "";
1253
+ const lineVisWidth = visibleWidth(line);
1254
+ if (line.length === 0) {
1255
+ // Empty line still takes one visual line
1256
+ visualLines.push({ logicalLine: i, startCol: 0, length: 0 });
1257
+ }
1258
+ else if (lineVisWidth <= width) {
1259
+ visualLines.push({ logicalLine: i, startCol: 0, length: line.length });
1260
+ }
1261
+ else {
1262
+ // Line needs wrapping - use word-aware wrapping
1263
+ const chunks = wordWrapLine(line, width);
1264
+ for (const chunk of chunks) {
1265
+ visualLines.push({
1266
+ logicalLine: i,
1267
+ startCol: chunk.startIndex,
1268
+ length: chunk.endIndex - chunk.startIndex,
1269
+ });
1270
+ }
1271
+ }
1272
+ }
1273
+ return visualLines;
1274
+ }
1275
+ /**
1276
+ * Find the visual line index for the current cursor position.
1277
+ */
1278
+ findCurrentVisualLine(visualLines) {
1279
+ for (let i = 0; i < visualLines.length; i++) {
1280
+ const vl = visualLines[i];
1281
+ if (!vl)
1282
+ continue;
1283
+ if (vl.logicalLine === this.state.cursorLine) {
1284
+ const colInSegment = this.state.cursorCol - vl.startCol;
1285
+ // Cursor is in this segment if it's within range
1286
+ // For the last segment of a logical line, cursor can be at length (end position)
1287
+ const isLastSegmentOfLine = i === visualLines.length - 1 || visualLines[i + 1]?.logicalLine !== vl.logicalLine;
1288
+ if (colInSegment >= 0 && (colInSegment < vl.length || (isLastSegmentOfLine && colInSegment <= vl.length))) {
1289
+ return i;
1290
+ }
1291
+ }
1292
+ }
1293
+ // Fallback: return last visual line
1294
+ return visualLines.length - 1;
1295
+ }
1296
+ moveCursor(deltaLine, deltaCol) {
1297
+ this.lastAction = null;
1298
+ const visualLines = this.buildVisualLineMap(this.lastWidth);
1299
+ const currentVisualLine = this.findCurrentVisualLine(visualLines);
1300
+ if (deltaLine !== 0) {
1301
+ const targetVisualLine = currentVisualLine + deltaLine;
1302
+ if (targetVisualLine >= 0 && targetVisualLine < visualLines.length) {
1303
+ this.moveToVisualLine(visualLines, currentVisualLine, targetVisualLine);
1304
+ }
1305
+ }
1306
+ if (deltaCol !== 0) {
1307
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1308
+ if (deltaCol > 0) {
1309
+ // Moving right - move by one grapheme (handles emojis, combining characters, etc.)
1310
+ if (this.state.cursorCol < currentLine.length) {
1311
+ const afterCursor = currentLine.slice(this.state.cursorCol);
1312
+ const graphemes = [...segmenter.segment(afterCursor)];
1313
+ const firstGrapheme = graphemes[0];
1314
+ this.setCursorCol(this.state.cursorCol + (firstGrapheme ? firstGrapheme.segment.length : 1));
1315
+ }
1316
+ else if (this.state.cursorLine < this.state.lines.length - 1) {
1317
+ // Wrap to start of next logical line
1318
+ this.state.cursorLine++;
1319
+ this.setCursorCol(0);
1320
+ }
1321
+ else {
1322
+ // At end of last line - can't move, but set preferredVisualCol for up/down navigation
1323
+ const currentVL = visualLines[currentVisualLine];
1324
+ if (currentVL) {
1325
+ this.preferredVisualCol = this.state.cursorCol - currentVL.startCol;
1326
+ }
1327
+ }
1328
+ }
1329
+ else {
1330
+ // Moving left - move by one grapheme (handles emojis, combining characters, etc.)
1331
+ if (this.state.cursorCol > 0) {
1332
+ const beforeCursor = currentLine.slice(0, this.state.cursorCol);
1333
+ const graphemes = [...segmenter.segment(beforeCursor)];
1334
+ const lastGrapheme = graphemes[graphemes.length - 1];
1335
+ this.setCursorCol(this.state.cursorCol - (lastGrapheme ? lastGrapheme.segment.length : 1));
1336
+ }
1337
+ else if (this.state.cursorLine > 0) {
1338
+ // Wrap to end of previous logical line
1339
+ this.state.cursorLine--;
1340
+ const prevLine = this.state.lines[this.state.cursorLine] || "";
1341
+ this.setCursorCol(prevLine.length);
1342
+ }
1343
+ }
1344
+ }
1345
+ }
1346
+ /**
1347
+ * Scroll by a page (direction: -1 for up, 1 for down).
1348
+ * Moves cursor by the page size while keeping it in bounds.
1349
+ */
1350
+ pageScroll(direction) {
1351
+ this.lastAction = null;
1352
+ const terminalRows = this.tui.terminal.rows;
1353
+ const pageSize = Math.max(5, Math.floor(terminalRows * 0.3));
1354
+ const visualLines = this.buildVisualLineMap(this.lastWidth);
1355
+ const currentVisualLine = this.findCurrentVisualLine(visualLines);
1356
+ const targetVisualLine = Math.max(0, Math.min(visualLines.length - 1, currentVisualLine + direction * pageSize));
1357
+ this.moveToVisualLine(visualLines, currentVisualLine, targetVisualLine);
1358
+ }
1359
+ moveWordBackwards() {
1360
+ this.lastAction = null;
1361
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1362
+ // If at start of line, move to end of previous line
1363
+ if (this.state.cursorCol === 0) {
1364
+ if (this.state.cursorLine > 0) {
1365
+ this.state.cursorLine--;
1366
+ const prevLine = this.state.lines[this.state.cursorLine] || "";
1367
+ this.setCursorCol(prevLine.length);
1368
+ }
1369
+ return;
1370
+ }
1371
+ const textBeforeCursor = currentLine.slice(0, this.state.cursorCol);
1372
+ const graphemes = [...segmenter.segment(textBeforeCursor)];
1373
+ let newCol = this.state.cursorCol;
1374
+ // Skip trailing whitespace
1375
+ while (graphemes.length > 0 && isWhitespaceChar(graphemes[graphemes.length - 1]?.segment || "")) {
1376
+ newCol -= graphemes.pop()?.segment.length || 0;
1377
+ }
1378
+ if (graphemes.length > 0) {
1379
+ const lastGrapheme = graphemes[graphemes.length - 1]?.segment || "";
1380
+ if (isPunctuationChar(lastGrapheme)) {
1381
+ // Skip punctuation run
1382
+ while (graphemes.length > 0 && isPunctuationChar(graphemes[graphemes.length - 1]?.segment || "")) {
1383
+ newCol -= graphemes.pop()?.segment.length || 0;
1384
+ }
1385
+ }
1386
+ else {
1387
+ // Skip word run
1388
+ while (graphemes.length > 0 &&
1389
+ !isWhitespaceChar(graphemes[graphemes.length - 1]?.segment || "") &&
1390
+ !isPunctuationChar(graphemes[graphemes.length - 1]?.segment || "")) {
1391
+ newCol -= graphemes.pop()?.segment.length || 0;
1392
+ }
1393
+ }
1394
+ }
1395
+ this.setCursorCol(newCol);
1396
+ }
1397
+ /**
1398
+ * Yank (paste) the most recent kill ring entry at cursor position.
1399
+ */
1400
+ yank() {
1401
+ if (this.killRing.length === 0)
1402
+ return;
1403
+ this.pushUndoSnapshot();
1404
+ const text = this.killRing.peek();
1405
+ this.insertYankedText(text);
1406
+ this.lastAction = "yank";
1407
+ }
1408
+ /**
1409
+ * Cycle through kill ring (only works immediately after yank or yank-pop).
1410
+ * Replaces the last yanked text with the previous entry in the ring.
1411
+ */
1412
+ yankPop() {
1413
+ // Only works if we just yanked and have more than one entry
1414
+ if (this.lastAction !== "yank" || this.killRing.length <= 1)
1415
+ return;
1416
+ this.pushUndoSnapshot();
1417
+ // Delete the previously yanked text (still at end of ring before rotation)
1418
+ this.deleteYankedText();
1419
+ // Rotate the ring: move end to front
1420
+ this.killRing.rotate();
1421
+ // Insert the new most recent entry (now at end after rotation)
1422
+ const text = this.killRing.peek();
1423
+ this.insertYankedText(text);
1424
+ this.lastAction = "yank";
1425
+ }
1426
+ /**
1427
+ * Insert text at cursor position (used by yank operations).
1428
+ */
1429
+ insertYankedText(text) {
1430
+ this.historyIndex = -1; // Exit history browsing mode
1431
+ const lines = text.split("\n");
1432
+ if (lines.length === 1) {
1433
+ // Single line - insert at cursor
1434
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1435
+ const before = currentLine.slice(0, this.state.cursorCol);
1436
+ const after = currentLine.slice(this.state.cursorCol);
1437
+ this.state.lines[this.state.cursorLine] = before + text + after;
1438
+ this.setCursorCol(this.state.cursorCol + text.length);
1439
+ }
1440
+ else {
1441
+ // Multi-line insert
1442
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1443
+ const before = currentLine.slice(0, this.state.cursorCol);
1444
+ const after = currentLine.slice(this.state.cursorCol);
1445
+ // First line merges with text before cursor
1446
+ this.state.lines[this.state.cursorLine] = before + (lines[0] || "");
1447
+ // Insert middle lines
1448
+ for (let i = 1; i < lines.length - 1; i++) {
1449
+ this.state.lines.splice(this.state.cursorLine + i, 0, lines[i] || "");
1450
+ }
1451
+ // Last line merges with text after cursor
1452
+ const lastLineIndex = this.state.cursorLine + lines.length - 1;
1453
+ this.state.lines.splice(lastLineIndex, 0, (lines[lines.length - 1] || "") + after);
1454
+ // Update cursor position
1455
+ this.state.cursorLine = lastLineIndex;
1456
+ this.setCursorCol((lines[lines.length - 1] || "").length);
1457
+ }
1458
+ if (this.onChange) {
1459
+ this.onChange(this.getText());
1460
+ }
1461
+ }
1462
+ /**
1463
+ * Delete the previously yanked text (used by yank-pop).
1464
+ * The yanked text is derived from killRing[end] since it hasn't been rotated yet.
1465
+ */
1466
+ deleteYankedText() {
1467
+ const yankedText = this.killRing.peek();
1468
+ if (!yankedText)
1469
+ return;
1470
+ const yankLines = yankedText.split("\n");
1471
+ if (yankLines.length === 1) {
1472
+ // Single line - delete backward from cursor
1473
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1474
+ const deleteLen = yankedText.length;
1475
+ const before = currentLine.slice(0, this.state.cursorCol - deleteLen);
1476
+ const after = currentLine.slice(this.state.cursorCol);
1477
+ this.state.lines[this.state.cursorLine] = before + after;
1478
+ this.setCursorCol(this.state.cursorCol - deleteLen);
1479
+ }
1480
+ else {
1481
+ // Multi-line delete - cursor is at end of last yanked line
1482
+ const startLine = this.state.cursorLine - (yankLines.length - 1);
1483
+ const startCol = (this.state.lines[startLine] || "").length - (yankLines[0] || "").length;
1484
+ // Get text after cursor on current line
1485
+ const afterCursor = (this.state.lines[this.state.cursorLine] || "").slice(this.state.cursorCol);
1486
+ // Get text before yank start position
1487
+ const beforeYank = (this.state.lines[startLine] || "").slice(0, startCol);
1488
+ // Remove all lines from startLine to cursorLine and replace with merged line
1489
+ this.state.lines.splice(startLine, yankLines.length, beforeYank + afterCursor);
1490
+ // Update cursor
1491
+ this.state.cursorLine = startLine;
1492
+ this.setCursorCol(startCol);
1493
+ }
1494
+ if (this.onChange) {
1495
+ this.onChange(this.getText());
1496
+ }
1497
+ }
1498
+ pushUndoSnapshot() {
1499
+ this.undoStack.push(this.state);
1500
+ }
1501
+ undo() {
1502
+ this.historyIndex = -1; // Exit history browsing mode
1503
+ const snapshot = this.undoStack.pop();
1504
+ if (!snapshot)
1505
+ return;
1506
+ Object.assign(this.state, snapshot);
1507
+ this.lastAction = null;
1508
+ this.preferredVisualCol = null;
1509
+ if (this.onChange) {
1510
+ this.onChange(this.getText());
1511
+ }
1512
+ }
1513
+ /**
1514
+ * Jump to the first occurrence of a character in the specified direction.
1515
+ * Multi-line search. Case-sensitive. Skips the current cursor position.
1516
+ */
1517
+ jumpToChar(char, direction) {
1518
+ this.lastAction = null;
1519
+ const isForward = direction === "forward";
1520
+ const lines = this.state.lines;
1521
+ const end = isForward ? lines.length : -1;
1522
+ const step = isForward ? 1 : -1;
1523
+ for (let lineIdx = this.state.cursorLine; lineIdx !== end; lineIdx += step) {
1524
+ const line = lines[lineIdx] || "";
1525
+ const isCurrentLine = lineIdx === this.state.cursorLine;
1526
+ // Current line: start after/before cursor; other lines: search full line
1527
+ const searchFrom = isCurrentLine
1528
+ ? isForward
1529
+ ? this.state.cursorCol + 1
1530
+ : this.state.cursorCol - 1
1531
+ : undefined;
1532
+ const idx = isForward ? line.indexOf(char, searchFrom) : line.lastIndexOf(char, searchFrom);
1533
+ if (idx !== -1) {
1534
+ this.state.cursorLine = lineIdx;
1535
+ this.setCursorCol(idx);
1536
+ return;
1537
+ }
1538
+ }
1539
+ // No match found - cursor stays in place
1540
+ }
1541
+ moveWordForwards() {
1542
+ this.lastAction = null;
1543
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1544
+ // If at end of line, move to start of next line
1545
+ if (this.state.cursorCol >= currentLine.length) {
1546
+ if (this.state.cursorLine < this.state.lines.length - 1) {
1547
+ this.state.cursorLine++;
1548
+ this.setCursorCol(0);
1549
+ }
1550
+ return;
1551
+ }
1552
+ const textAfterCursor = currentLine.slice(this.state.cursorCol);
1553
+ const segments = segmenter.segment(textAfterCursor);
1554
+ const iterator = segments[Symbol.iterator]();
1555
+ let next = iterator.next();
1556
+ let newCol = this.state.cursorCol;
1557
+ // Skip leading whitespace
1558
+ while (!next.done && isWhitespaceChar(next.value.segment)) {
1559
+ newCol += next.value.segment.length;
1560
+ next = iterator.next();
1561
+ }
1562
+ if (!next.done) {
1563
+ const firstGrapheme = next.value.segment;
1564
+ if (isPunctuationChar(firstGrapheme)) {
1565
+ // Skip punctuation run
1566
+ while (!next.done && isPunctuationChar(next.value.segment)) {
1567
+ newCol += next.value.segment.length;
1568
+ next = iterator.next();
1569
+ }
1570
+ }
1571
+ else {
1572
+ // Skip word run
1573
+ while (!next.done && !isWhitespaceChar(next.value.segment) && !isPunctuationChar(next.value.segment)) {
1574
+ newCol += next.value.segment.length;
1575
+ next = iterator.next();
1576
+ }
1577
+ }
1578
+ }
1579
+ this.setCursorCol(newCol);
1580
+ }
1581
+ // Slash menu only allowed on the first line of the editor
1582
+ isSlashMenuAllowed() {
1583
+ return this.state.cursorLine === 0;
1584
+ }
1585
+ // Helper method to check if cursor is at start of message (for slash command detection)
1586
+ isAtStartOfMessage() {
1587
+ if (!this.isSlashMenuAllowed())
1588
+ return false;
1589
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1590
+ const beforeCursor = currentLine.slice(0, this.state.cursorCol);
1591
+ return beforeCursor.trim() === "" || beforeCursor.trim() === "/";
1592
+ }
1593
+ isInSlashCommandContext(textBeforeCursor) {
1594
+ return this.isSlashMenuAllowed() && textBeforeCursor.trimStart().startsWith("/");
1595
+ }
1596
+ // Autocomplete methods
1597
+ tryTriggerAutocomplete(explicitTab = false) {
1598
+ if (!this.autocompleteProvider)
1599
+ return;
1600
+ // Check if we should trigger file completion on Tab
1601
+ if (explicitTab) {
1602
+ const provider = this.autocompleteProvider;
1603
+ const shouldTrigger = !provider.shouldTriggerFileCompletion ||
1604
+ provider.shouldTriggerFileCompletion(this.state.lines, this.state.cursorLine, this.state.cursorCol);
1605
+ if (!shouldTrigger) {
1606
+ return;
1607
+ }
1608
+ }
1609
+ const suggestions = this.autocompleteProvider.getSuggestions(this.state.lines, this.state.cursorLine, this.state.cursorCol);
1610
+ if (suggestions && suggestions.items.length > 0) {
1611
+ this.autocompletePrefix = suggestions.prefix;
1612
+ this.autocompleteList = new SelectList(suggestions.items, this.autocompleteMaxVisible, this.theme.selectList);
1613
+ this.autocompleteState = "regular";
1614
+ }
1615
+ else {
1616
+ this.cancelAutocomplete();
1617
+ }
1618
+ }
1619
+ handleTabCompletion() {
1620
+ if (!this.autocompleteProvider)
1621
+ return;
1622
+ const currentLine = this.state.lines[this.state.cursorLine] || "";
1623
+ const beforeCursor = currentLine.slice(0, this.state.cursorCol);
1624
+ // Check if we're in a slash command context
1625
+ if (this.isInSlashCommandContext(beforeCursor) && !beforeCursor.trimStart().includes(" ")) {
1626
+ this.handleSlashCommandCompletion();
1627
+ }
1628
+ else {
1629
+ this.forceFileAutocomplete(true);
1630
+ }
1631
+ }
1632
+ handleSlashCommandCompletion() {
1633
+ this.tryTriggerAutocomplete(true);
1634
+ }
1635
+ /*
1636
+ https://github.com/EsotericSoftware/spine-runtimes/actions/runs/19536643416/job/559322883
1637
+ 17 this job fails with https://github.com/EsotericSoftware/spine-runtimes/actions/runs/19
1638
+ 536643416/job/55932288317 havea look at .gi
1639
+ */
1640
+ forceFileAutocomplete(explicitTab = false) {
1641
+ if (!this.autocompleteProvider)
1642
+ return;
1643
+ // Check if provider supports force file suggestions via runtime check
1644
+ const provider = this.autocompleteProvider;
1645
+ if (typeof provider.getForceFileSuggestions !== "function") {
1646
+ this.tryTriggerAutocomplete(true);
1647
+ return;
1648
+ }
1649
+ const suggestions = provider.getForceFileSuggestions(this.state.lines, this.state.cursorLine, this.state.cursorCol);
1650
+ if (suggestions && suggestions.items.length > 0) {
1651
+ // If there's exactly one suggestion, apply it immediately
1652
+ if (explicitTab && suggestions.items.length === 1) {
1653
+ const item = suggestions.items[0];
1654
+ this.pushUndoSnapshot();
1655
+ this.lastAction = null;
1656
+ const result = this.autocompleteProvider.applyCompletion(this.state.lines, this.state.cursorLine, this.state.cursorCol, item, suggestions.prefix);
1657
+ this.state.lines = result.lines;
1658
+ this.state.cursorLine = result.cursorLine;
1659
+ this.setCursorCol(result.cursorCol);
1660
+ if (this.onChange)
1661
+ this.onChange(this.getText());
1662
+ return;
1663
+ }
1664
+ this.autocompletePrefix = suggestions.prefix;
1665
+ this.autocompleteList = new SelectList(suggestions.items, this.autocompleteMaxVisible, this.theme.selectList);
1666
+ this.autocompleteState = "force";
1667
+ }
1668
+ else {
1669
+ this.cancelAutocomplete();
1670
+ }
1671
+ }
1672
+ cancelAutocomplete() {
1673
+ this.autocompleteState = null;
1674
+ this.autocompleteList = undefined;
1675
+ this.autocompletePrefix = "";
1676
+ }
1677
+ isShowingAutocomplete() {
1678
+ return this.autocompleteState !== null;
1679
+ }
1680
+ updateAutocomplete() {
1681
+ if (!this.autocompleteState || !this.autocompleteProvider)
1682
+ return;
1683
+ if (this.autocompleteState === "force") {
1684
+ this.forceFileAutocomplete();
1685
+ return;
1686
+ }
1687
+ const suggestions = this.autocompleteProvider.getSuggestions(this.state.lines, this.state.cursorLine, this.state.cursorCol);
1688
+ if (suggestions && suggestions.items.length > 0) {
1689
+ this.autocompletePrefix = suggestions.prefix;
1690
+ // Always create new SelectList to ensure update
1691
+ this.autocompleteList = new SelectList(suggestions.items, this.autocompleteMaxVisible, this.theme.selectList);
1692
+ }
1693
+ else {
1694
+ this.cancelAutocomplete();
1695
+ }
1696
+ }
1697
+ }