@linxiraos/pi-tui 1.4.4 → 1.4.7

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 (363) hide show
  1. package/CHANGELOG.md +208 -207
  2. package/README.md +34 -34
  3. package/dist/types/app-keybindings.d.ts +20 -0
  4. package/dist/types/apps/session-picker.d.ts +5 -0
  5. package/dist/types/charts/chart-plan.d.ts +9 -7
  6. package/dist/types/chat/assistant-message.d.ts +15 -3
  7. package/dist/types/chat/bash-execution.d.ts +2 -0
  8. package/dist/types/chat/display-preferences.d.ts +2 -0
  9. package/dist/types/chat/extension-types.d.ts +31 -0
  10. package/dist/types/chat/reply-write.d.ts +48 -0
  11. package/dist/types/chat/table-chart.d.ts +4 -3
  12. package/dist/types/chat/tool-execution.d.ts +1 -2
  13. package/dist/types/chat/transcript-outline.d.ts +7 -0
  14. package/dist/types/chat/transcript-render-helpers.d.ts +2 -0
  15. package/dist/types/chrome/context-map.d.ts +15 -0
  16. package/dist/types/chrome/context-presentation.d.ts +48 -0
  17. package/dist/types/chrome/context-view-model.d.ts +77 -0
  18. package/dist/types/chrome/index.d.ts +1 -0
  19. package/dist/types/chrome/transcript-notice.d.ts +35 -0
  20. package/dist/types/chrome/transcript-status.d.ts +20 -8
  21. package/dist/types/components/editor.d.ts +8 -0
  22. package/dist/types/components/image.d.ts +20 -0
  23. package/dist/types/components/loader.d.ts +1 -1
  24. package/dist/types/components/markdown.d.ts +7 -0
  25. package/dist/types/native/blobs.d.ts +11 -1
  26. package/dist/types/native/picker.d.ts +4 -4
  27. package/dist/types/overlays/advisor-config.d.ts +6 -0
  28. package/dist/types/overlays/agent-hub-projection.d.ts +3 -1
  29. package/dist/types/overlays/annotation-overlay.d.ts +7 -1
  30. package/dist/types/overlays/annotation-types.d.ts +28 -0
  31. package/dist/types/overlays/ask-dialog.d.ts +12 -3
  32. package/dist/types/overlays/btw-history-panel.d.ts +16 -2
  33. package/dist/types/overlays/btw-history.d.ts +7 -0
  34. package/dist/types/overlays/btw-panel.d.ts +2 -0
  35. package/dist/types/overlays/context-viewer-format.d.ts +42 -0
  36. package/dist/types/overlays/context-viewer-overview.d.ts +70 -0
  37. package/dist/types/overlays/context-viewer.d.ts +25 -0
  38. package/dist/types/overlays/hook-input.d.ts +1 -0
  39. package/dist/types/overlays/login-dialog.d.ts +5 -1
  40. package/dist/types/overlays/mcp-add-wizard.d.ts +2 -0
  41. package/dist/types/overlays/model-browser.d.ts +10 -1
  42. package/dist/types/overlays/oauth-selector.d.ts +23 -0
  43. package/dist/types/overlays/pause-screen.d.ts +13 -3
  44. package/dist/types/overlays/plan-review-overlay.d.ts +2 -0
  45. package/dist/types/overlays/rewind-selector.d.ts +1 -1
  46. package/dist/types/overlays/running-subagent-badge.d.ts +7 -1
  47. package/dist/types/overlays/session-selector.d.ts +22 -2
  48. package/dist/types/prompt/composer.d.ts +43 -0
  49. package/dist/types/prompt/interactive-context-helpers.d.ts +10 -3
  50. package/dist/types/prompt/macos-spelling.d.ts +2 -2
  51. package/dist/types/render/extension-links.d.ts +65 -0
  52. package/dist/types/render/hyperlink.d.ts +38 -7
  53. package/dist/types/render/index.d.ts +1 -0
  54. package/dist/types/render/output-pane.d.ts +1 -1
  55. package/dist/types/render/render-utils.d.ts +7 -1
  56. package/dist/types/setup/scenes/glyph.d.ts +1 -1
  57. package/dist/types/setup/scenes/model.d.ts +1 -1
  58. package/dist/types/setup/scenes/outro.d.ts +5 -4
  59. package/dist/types/setup/scenes/sign-in.d.ts +13 -8
  60. package/dist/types/setup/scenes/types.d.ts +16 -1
  61. package/dist/types/setup/startup-splash.d.ts +1 -1
  62. package/dist/types/setup/wizard-overlay.d.ts +11 -7
  63. package/dist/types/setup/wizard.d.ts +5 -1
  64. package/dist/types/status-line/component.d.ts +3 -3
  65. package/dist/types/status-line/context-usage.d.ts +42 -35
  66. package/dist/types/status-line/host.d.ts +6 -2
  67. package/dist/types/terminal-capabilities.d.ts +7 -0
  68. package/dist/types/theme/symbols.d.ts +1 -1
  69. package/dist/types/theme/theme.d.ts +0 -23
  70. package/dist/types/tools/ask.d.ts +4 -0
  71. package/dist/types/tools/bash-interactive.d.ts +6 -0
  72. package/dist/types/tools/bash.d.ts +2 -0
  73. package/dist/types/tools/default-renderer.d.ts +6 -4
  74. package/dist/types/tools/edit.d.ts +3 -1
  75. package/dist/types/tools/fetch.d.ts +1 -0
  76. package/dist/types/tools/github.d.ts +3 -0
  77. package/dist/types/tools/grouped-file-output.d.ts +2 -0
  78. package/dist/types/tools/irc.d.ts +6 -0
  79. package/dist/types/tools/json-tree.d.ts +23 -4
  80. package/dist/types/tools/lsp.d.ts +2 -0
  81. package/dist/types/tools/mcp.d.ts +7 -6
  82. package/dist/types/tools/native-view.d.ts +1 -1
  83. package/dist/types/tools/read.d.ts +3 -0
  84. package/dist/types/tools/renderer.d.ts +2 -0
  85. package/dist/types/tools/streaming-output.d.ts +11 -0
  86. package/dist/types/tools/task.d.ts +9 -1
  87. package/dist/types/tools/url-scheme-host.d.ts +17 -0
  88. package/dist/types/tools/wait.d.ts +16 -2
  89. package/dist/types/tools/yield.d.ts +1 -1
  90. package/dist/types/tui.d.ts +9 -2
  91. package/dist/types/vim.d.ts +39 -0
  92. package/package.json +10 -14
  93. package/src/app-keybindings.d.ts +20 -0
  94. package/src/app-keybindings.ts +20 -0
  95. package/src/apps/session-picker.ts +7 -0
  96. package/src/charts/chart-plan.d.ts +9 -7
  97. package/src/charts/chart-plan.ts +20 -50
  98. package/src/chat/advisor-message.ts +2 -0
  99. package/src/chat/assistant-message.d.ts +15 -3
  100. package/src/chat/assistant-message.ts +137 -19
  101. package/src/chat/background-tan-message.ts +4 -0
  102. package/src/chat/bash-execution.d.ts +2 -0
  103. package/src/chat/bash-execution.ts +13 -1
  104. package/src/chat/chat-transcript-builder.ts +58 -28
  105. package/src/chat/compaction-summary-message.ts +2 -1
  106. package/src/chat/display-preferences.d.ts +2 -0
  107. package/src/chat/display-preferences.ts +3 -0
  108. package/src/chat/extension-types.d.ts +31 -0
  109. package/src/chat/extension-types.ts +34 -0
  110. package/src/chat/read-tool-group.ts +1 -2
  111. package/src/chat/reply-write.d.ts +48 -0
  112. package/src/chat/reply-write.ts +127 -0
  113. package/src/chat/skill-message.ts +2 -6
  114. package/src/chat/table-chart.d.ts +4 -3
  115. package/src/chat/table-chart.ts +10 -4
  116. package/src/chat/tool-execution.d.ts +1 -2
  117. package/src/chat/tool-execution.ts +20 -13
  118. package/src/chat/transcript-outline.d.ts +7 -0
  119. package/src/chat/transcript-outline.ts +27 -0
  120. package/src/chat/transcript-render-helpers.d.ts +2 -0
  121. package/src/chat/transcript-render-helpers.ts +66 -24
  122. package/src/chrome/context-map.ts +62 -0
  123. package/src/chrome/context-presentation.ts +26 -0
  124. package/src/chrome/context-view-model.ts +73 -0
  125. package/src/chrome/index.ts +1 -0
  126. package/src/chrome/transcript-container.ts +89 -59
  127. package/src/chrome/transcript-notice.d.ts +35 -0
  128. package/src/chrome/transcript-notice.ts +83 -0
  129. package/src/chrome/transcript-status.d.ts +20 -8
  130. package/src/chrome/transcript-status.ts +29 -29
  131. package/src/components/editor.d.ts +8 -0
  132. package/src/components/editor.ts +465 -14
  133. package/src/components/image.d.ts +20 -0
  134. package/src/components/image.ts +83 -18
  135. package/src/components/loader.d.ts +1 -1
  136. package/src/components/loader.ts +1 -1
  137. package/src/components/markdown.d.ts +7 -0
  138. package/src/components/markdown.ts +225 -13
  139. package/src/hotkeys-markdown.ts +2 -0
  140. package/src/native/backend.ts +5 -66
  141. package/src/native/blobs.d.ts +11 -1
  142. package/src/native/blobs.ts +5 -3
  143. package/src/native/picker.d.ts +4 -4
  144. package/src/native/picker.ts +4 -4
  145. package/src/overlays/advisor-config.ts +10 -12
  146. package/src/overlays/agent-hub-projection.d.ts +3 -1
  147. package/src/overlays/agent-hub-projection.ts +6 -5
  148. package/src/overlays/agent-hub.ts +51 -42
  149. package/src/overlays/annotation-overlay.ts +125 -37
  150. package/src/overlays/annotation-types.ts +24 -0
  151. package/src/overlays/ask-dialog.ts +125 -35
  152. package/src/overlays/btw-history-panel.ts +124 -12
  153. package/src/overlays/btw-history.ts +11 -0
  154. package/src/overlays/btw-panel.ts +8 -4
  155. package/src/overlays/context-viewer-format.ts +145 -0
  156. package/src/overlays/context-viewer-overview.ts +381 -0
  157. package/src/overlays/context-viewer.ts +728 -0
  158. package/src/overlays/hook-input.ts +2 -0
  159. package/src/overlays/hook-selector.ts +2 -2
  160. package/src/overlays/login-dialog.ts +31 -5
  161. package/src/overlays/mcp-add-wizard.ts +6 -1
  162. package/src/overlays/model-browser.ts +37 -0
  163. package/src/overlays/model-hub.ts +1 -7
  164. package/src/overlays/model-picker.ts +1 -10
  165. package/src/overlays/oauth-selector.ts +44 -6
  166. package/src/overlays/pause-screen.ts +166 -33
  167. package/src/overlays/plan-review-overlay.ts +13 -2
  168. package/src/overlays/rewind-selector.ts +29 -12
  169. package/src/overlays/running-subagent-badge.ts +12 -1
  170. package/src/overlays/session-selector.ts +313 -42
  171. package/src/overlays/settings-defs.ts +1 -0
  172. package/src/overlays/usage-dashboard.ts +20 -14
  173. package/src/prompt/composer.ts +216 -1
  174. package/src/prompt/custom-editor.ts +6 -1
  175. package/src/prompt/emoji-autocomplete.ts +0 -2
  176. package/src/prompt/interactive-context-helpers.ts +29 -4
  177. package/src/prompt/macos-spelling.ts +24 -15
  178. package/src/render/extension-links.d.ts +65 -0
  179. package/src/render/extension-links.ts +214 -0
  180. package/src/render/hyperlink.d.ts +38 -7
  181. package/src/render/hyperlink.ts +102 -23
  182. package/src/render/index.d.ts +1 -0
  183. package/src/render/index.ts +1 -0
  184. package/src/render/output-pane.d.ts +1 -1
  185. package/src/render/output-pane.ts +60 -17
  186. package/src/render/render-utils.d.ts +7 -1
  187. package/src/render/render-utils.ts +11 -2
  188. package/src/setup/scenes/composer.ts +2 -0
  189. package/src/setup/scenes/glyph.ts +3 -1
  190. package/src/setup/scenes/model.ts +192 -29
  191. package/src/setup/scenes/outro.ts +33 -15
  192. package/src/setup/scenes/sign-in.ts +194 -392
  193. package/src/setup/scenes/theme.ts +2 -0
  194. package/src/setup/scenes/types.ts +18 -1
  195. package/src/setup/startup-splash.ts +8 -7
  196. package/src/setup/wizard-overlay.ts +164 -35
  197. package/src/setup/wizard.ts +11 -2
  198. package/src/status-line/component.ts +10 -14
  199. package/src/status-line/context-usage.ts +122 -597
  200. package/src/status-line/host.ts +6 -2
  201. package/src/terminal-capabilities.d.ts +7 -0
  202. package/src/terminal-capabilities.ts +10 -0
  203. package/src/terminal.ts +24 -2
  204. package/src/theme/defaults.json +1 -0
  205. package/src/theme/loader.ts +1 -1
  206. package/src/theme/symbols.d.ts +1 -1
  207. package/src/theme/symbols.ts +16 -0
  208. package/src/theme/theme.d.ts +0 -23
  209. package/src/theme/theme.ts +2 -75
  210. package/src/tools/ask.d.ts +4 -0
  211. package/src/tools/ask.ts +4 -0
  212. package/src/tools/ast-grep.ts +3 -2
  213. package/src/tools/bash-interactive.d.ts +49 -0
  214. package/src/tools/bash-interactive.ts +6 -1
  215. package/src/tools/bash.d.ts +2 -0
  216. package/src/tools/bash.ts +7 -23
  217. package/src/tools/default-renderer.d.ts +6 -4
  218. package/src/tools/default-renderer.ts +9 -6
  219. package/src/tools/edit.d.ts +3 -1
  220. package/src/tools/edit.ts +8 -4
  221. package/src/tools/eval.ts +14 -4
  222. package/src/tools/fetch.d.ts +1 -0
  223. package/src/tools/fetch.ts +1 -0
  224. package/src/tools/find.ts +3 -10
  225. package/src/tools/github.d.ts +3 -0
  226. package/src/tools/github.ts +23 -3
  227. package/src/tools/glob.ts +4 -3
  228. package/src/tools/grep.ts +3 -4
  229. package/src/tools/grouped-file-output.d.ts +2 -0
  230. package/src/tools/grouped-file-output.ts +8 -2
  231. package/src/tools/irc.d.ts +6 -0
  232. package/src/tools/irc.ts +6 -0
  233. package/src/tools/job-widget.ts +13 -1
  234. package/src/tools/json-tree.d.ts +23 -4
  235. package/src/tools/json-tree.ts +132 -42
  236. package/src/tools/lsp.d.ts +2 -0
  237. package/src/tools/lsp.ts +36 -15
  238. package/src/tools/mcp.d.ts +7 -6
  239. package/src/tools/mcp.ts +41 -15
  240. package/src/tools/native-view.d.ts +1 -1
  241. package/src/tools/native-view.ts +3 -3
  242. package/src/tools/read.d.ts +3 -0
  243. package/src/tools/read.ts +3 -1
  244. package/src/tools/renderer.d.ts +2 -0
  245. package/src/tools/renderer.ts +2 -0
  246. package/src/tools/streaming-output.d.ts +11 -0
  247. package/src/tools/streaming-output.ts +41 -0
  248. package/src/tools/task.d.ts +9 -1
  249. package/src/tools/task.ts +59 -16
  250. package/src/tools/url-scheme-host.d.ts +17 -0
  251. package/src/tools/url-scheme-host.ts +21 -0
  252. package/src/tools/vibe.ts +14 -3
  253. package/src/tools/wait.d.ts +16 -2
  254. package/src/tools/wait.ts +16 -2
  255. package/src/tools/yield.d.ts +1 -1
  256. package/src/tools/yield.ts +18 -9
  257. package/src/tui.d.ts +9 -2
  258. package/src/tui.ts +102 -34
  259. package/src/vim.d.ts +39 -0
  260. package/src/vim.ts +313 -22
  261. package/dist/types/theme/defaults/index.d.ts +0 -10289
  262. package/src/theme/defaults/alabaster.json +0 -103
  263. package/src/theme/defaults/amethyst.json +0 -106
  264. package/src/theme/defaults/anthracite.json +0 -103
  265. package/src/theme/defaults/basalt.json +0 -101
  266. package/src/theme/defaults/birch.json +0 -105
  267. package/src/theme/defaults/dark-abyss.json +0 -101
  268. package/src/theme/defaults/dark-arctic.json +0 -114
  269. package/src/theme/defaults/dark-aurora.json +0 -105
  270. package/src/theme/defaults/dark-catppuccin.json +0 -117
  271. package/src/theme/defaults/dark-cavern.json +0 -101
  272. package/src/theme/defaults/dark-celestial.json +0 -116
  273. package/src/theme/defaults/dark-copper.json +0 -105
  274. package/src/theme/defaults/dark-cosmos.json +0 -100
  275. package/src/theme/defaults/dark-cyberpunk.json +0 -112
  276. package/src/theme/defaults/dark-dracula.json +0 -108
  277. package/src/theme/defaults/dark-eclipse.json +0 -101
  278. package/src/theme/defaults/dark-ember.json +0 -105
  279. package/src/theme/defaults/dark-equinox.json +0 -100
  280. package/src/theme/defaults/dark-forest.json +0 -106
  281. package/src/theme/defaults/dark-github.json +0 -115
  282. package/src/theme/defaults/dark-gruvbox.json +0 -122
  283. package/src/theme/defaults/dark-lavender.json +0 -105
  284. package/src/theme/defaults/dark-lunar.json +0 -99
  285. package/src/theme/defaults/dark-midnight.json +0 -105
  286. package/src/theme/defaults/dark-monochrome.json +0 -104
  287. package/src/theme/defaults/dark-monokai.json +0 -108
  288. package/src/theme/defaults/dark-nebula.json +0 -100
  289. package/src/theme/defaults/dark-neon-noir.json +0 -110
  290. package/src/theme/defaults/dark-nord.json +0 -108
  291. package/src/theme/defaults/dark-ocean.json +0 -111
  292. package/src/theme/defaults/dark-one.json +0 -110
  293. package/src/theme/defaults/dark-poimandres.json +0 -125
  294. package/src/theme/defaults/dark-rainforest.json +0 -101
  295. package/src/theme/defaults/dark-reef.json +0 -101
  296. package/src/theme/defaults/dark-retro.json +0 -102
  297. package/src/theme/defaults/dark-rose-pine.json +0 -106
  298. package/src/theme/defaults/dark-sakura.json +0 -105
  299. package/src/theme/defaults/dark-slate.json +0 -105
  300. package/src/theme/defaults/dark-solarized.json +0 -107
  301. package/src/theme/defaults/dark-solstice.json +0 -100
  302. package/src/theme/defaults/dark-starfall.json +0 -101
  303. package/src/theme/defaults/dark-sunset.json +0 -109
  304. package/src/theme/defaults/dark-swamp.json +0 -100
  305. package/src/theme/defaults/dark-synthwave.json +0 -113
  306. package/src/theme/defaults/dark-taiga.json +0 -101
  307. package/src/theme/defaults/dark-terminal.json +0 -105
  308. package/src/theme/defaults/dark-tokyo-night.json +0 -111
  309. package/src/theme/defaults/dark-tundra.json +0 -101
  310. package/src/theme/defaults/dark-twilight.json +0 -101
  311. package/src/theme/defaults/dark-volcanic.json +0 -101
  312. package/src/theme/defaults/graphite.json +0 -102
  313. package/src/theme/defaults/index.d.ts +0 -10289
  314. package/src/theme/defaults/index.ts +0 -203
  315. package/src/theme/defaults/light-arctic.json +0 -117
  316. package/src/theme/defaults/light-aurora-day.json +0 -101
  317. package/src/theme/defaults/light-canyon.json +0 -101
  318. package/src/theme/defaults/light-catppuccin.json +0 -116
  319. package/src/theme/defaults/light-cirrus.json +0 -100
  320. package/src/theme/defaults/light-coral.json +0 -105
  321. package/src/theme/defaults/light-cyberpunk.json +0 -106
  322. package/src/theme/defaults/light-dawn.json +0 -100
  323. package/src/theme/defaults/light-dunes.json +0 -101
  324. package/src/theme/defaults/light-eucalyptus.json +0 -105
  325. package/src/theme/defaults/light-forest.json +0 -110
  326. package/src/theme/defaults/light-frost.json +0 -105
  327. package/src/theme/defaults/light-github.json +0 -125
  328. package/src/theme/defaults/light-glacier.json +0 -101
  329. package/src/theme/defaults/light-gruvbox.json +0 -118
  330. package/src/theme/defaults/light-haze.json +0 -100
  331. package/src/theme/defaults/light-honeycomb.json +0 -105
  332. package/src/theme/defaults/light-lagoon.json +0 -101
  333. package/src/theme/defaults/light-lavender.json +0 -105
  334. package/src/theme/defaults/light-meadow.json +0 -101
  335. package/src/theme/defaults/light-mint.json +0 -105
  336. package/src/theme/defaults/light-monochrome.json +0 -111
  337. package/src/theme/defaults/light-ocean.json +0 -109
  338. package/src/theme/defaults/light-one.json +0 -109
  339. package/src/theme/defaults/light-opal.json +0 -101
  340. package/src/theme/defaults/light-orchard.json +0 -101
  341. package/src/theme/defaults/light-paper.json +0 -105
  342. package/src/theme/defaults/light-poimandres.json +0 -125
  343. package/src/theme/defaults/light-prism.json +0 -100
  344. package/src/theme/defaults/light-retro.json +0 -108
  345. package/src/theme/defaults/light-sand.json +0 -105
  346. package/src/theme/defaults/light-savanna.json +0 -101
  347. package/src/theme/defaults/light-solarized.json +0 -112
  348. package/src/theme/defaults/light-soleil.json +0 -100
  349. package/src/theme/defaults/light-sunset.json +0 -109
  350. package/src/theme/defaults/light-synthwave.json +0 -108
  351. package/src/theme/defaults/light-tokyo-night.json +0 -121
  352. package/src/theme/defaults/light-wetland.json +0 -101
  353. package/src/theme/defaults/light-zenith.json +0 -99
  354. package/src/theme/defaults/limestone.json +0 -104
  355. package/src/theme/defaults/mahogany.json +0 -107
  356. package/src/theme/defaults/marble.json +0 -103
  357. package/src/theme/defaults/obsidian.json +0 -101
  358. package/src/theme/defaults/onyx.json +0 -101
  359. package/src/theme/defaults/pearl.json +0 -103
  360. package/src/theme/defaults/porcelain.json +0 -101
  361. package/src/theme/defaults/quartz.json +0 -106
  362. package/src/theme/defaults/sandstone.json +0 -105
  363. package/src/theme/defaults/titanium.json +0 -100
@@ -42,9 +42,16 @@ import {
42
42
  visibleWidth,
43
43
  } from "../utils";
44
44
  import {
45
+ countGraphemes,
46
+ diffInsertSession,
47
+ flatten,
45
48
  lastGraphemeStart,
46
49
  nextGraphemeStart,
50
+ offsetToPos,
51
+ prevGraphemeStart,
52
+ stepGraphemes,
47
53
  type VimCommand,
54
+ type VimInsertRecord,
48
55
  type VimMode,
49
56
  type VimPosition,
50
57
  VimState,
@@ -460,6 +467,154 @@ const DEFAULT_PAGE_SCROLL_LINES = 10;
460
467
 
461
468
  const MAX_UNDO_STACK = 100;
462
469
 
470
+ /** Undo-stack position to fold later snapshots back to; `shifts` tracks MAX_UNDO_STACK eviction. */
471
+ interface UndoMark {
472
+ depth: number;
473
+ shifts: number;
474
+ }
475
+
476
+ /**
477
+ * Tracked Insert/Replace session: `[0, leftEnd)` is surviving pre-session text on the left and
478
+ * `[rightStart, len)` on the right (flat-buffer offsets), so the gap is what the session typed.
479
+ * `delBefore`/`delAfter` count pre-session graphemes deleted around the anchor — tracked
480
+ * incrementally because a final-text diff cannot see a delete that was retyped identically.
481
+ */
482
+ interface VimSessionState {
483
+ mark: UndoMark;
484
+ replace: boolean;
485
+ /** Flat text at segment start (diff fallback and arrowed-split baseline). */
486
+ text: string;
487
+ /** Flat cursor at segment start. */
488
+ cursor: number;
489
+ arrowed: boolean;
490
+ leftEnd: number;
491
+ rightStart: number;
492
+ delBefore: number;
493
+ delAfter: number;
494
+ /** False after an edit path we cannot classify (native host edits); falls back to a final diff. */
495
+ tracked: boolean;
496
+ }
497
+
498
+ /** Length of `lines.join("\n")` without building it. */
499
+ function flatLength(lines: readonly string[]): number {
500
+ let length = lines.length - 1;
501
+ for (const line of lines) length += line.length;
502
+ return length;
503
+ }
504
+
505
+ /** `lines.join("\n").slice(start, end)` without joining the whole buffer. */
506
+ function sliceFlat(lines: readonly string[], start: number, end: number): string {
507
+ let out = "";
508
+ let offset = 0;
509
+ for (let i = 0; i < lines.length && offset < end; i++) {
510
+ const line = lines[i]!;
511
+ const lineEnd = offset + line.length;
512
+ if (lineEnd >= start) {
513
+ out += line.slice(Math.max(0, start - offset), end - offset);
514
+ if (i < lines.length - 1 && lineEnd < end) out += "\n";
515
+ }
516
+ offset = lineEnd + 1;
517
+ }
518
+ return out;
519
+ }
520
+
521
+ /**
522
+ * Common prefix and non-overlapping common suffix of `before.join("\n")` and `after.join("\n")` —
523
+ * the split a flat char scan gives — without joining either buffer. Lines an edit left alone keep
524
+ * their string identity, so they are skipped whole and a keystroke costs O(lines), not O(chars).
525
+ */
526
+ function diffFlatLines(
527
+ before: readonly string[],
528
+ after: readonly string[],
529
+ beforeLength: number,
530
+ afterLength: number,
531
+ ): { prefix: number; suffix: number } {
532
+ const prefixMax = Math.min(beforeLength, afterLength);
533
+ let prefix = 0;
534
+ let bl = 0;
535
+ let bc = 0;
536
+ let al = 0;
537
+ let ac = 0;
538
+ // `prefix < prefixMax` guarantees both sides still have a character; past a line's end it is "\n".
539
+ while (prefix < prefixMax) {
540
+ const bLine = before[bl]!;
541
+ const aLine = after[al]!;
542
+ if (
543
+ bc === 0 &&
544
+ ac === 0 &&
545
+ bl < before.length - 1 &&
546
+ al < after.length - 1 &&
547
+ prefix + bLine.length + 1 <= prefixMax &&
548
+ bLine === aLine
549
+ ) {
550
+ prefix += bLine.length + 1;
551
+ bl++;
552
+ al++;
553
+ continue;
554
+ }
555
+ const b = bc < bLine.length ? bLine.charCodeAt(bc) : 10;
556
+ const a = ac < aLine.length ? aLine.charCodeAt(ac) : 10;
557
+ if (a !== b) break;
558
+ prefix++;
559
+ if (bc < bLine.length) {
560
+ bc++;
561
+ } else {
562
+ bl++;
563
+ bc = 0;
564
+ }
565
+ if (ac < aLine.length) {
566
+ ac++;
567
+ } else {
568
+ al++;
569
+ ac = 0;
570
+ }
571
+ }
572
+
573
+ const suffixMax = Math.min(beforeLength - prefix, afterLength - prefix);
574
+ let suffix = 0;
575
+ bl = before.length - 1;
576
+ al = after.length - 1;
577
+ bc = before[bl]!.length;
578
+ ac = after[al]!.length;
579
+ // Positions sit just past the next character to compare; before a line's start it is "\n".
580
+ while (suffix < suffixMax) {
581
+ const bLine = before[bl]!;
582
+ const aLine = after[al]!;
583
+ if (
584
+ bc === bLine.length &&
585
+ ac === aLine.length &&
586
+ bl > 0 &&
587
+ al > 0 &&
588
+ suffix + bLine.length + 1 <= suffixMax &&
589
+ bLine === aLine
590
+ ) {
591
+ suffix += bLine.length + 1;
592
+ bl--;
593
+ al--;
594
+ bc = before[bl]!.length;
595
+ ac = after[al]!.length;
596
+ continue;
597
+ }
598
+ const b = bc > 0 ? bLine.charCodeAt(bc - 1) : 10;
599
+ const a = ac > 0 ? aLine.charCodeAt(ac - 1) : 10;
600
+ if (a !== b) break;
601
+ suffix++;
602
+ if (bc > 0) {
603
+ bc--;
604
+ } else {
605
+ bl--;
606
+ bc = before[bl]!.length;
607
+ }
608
+ if (ac > 0) {
609
+ ac--;
610
+ } else {
611
+ al--;
612
+ ac = after[al]!.length;
613
+ }
614
+ }
615
+ return { prefix, suffix };
616
+ }
617
+
463
618
  /**
464
619
  * Typed characters that replace the provisional space a Tab word accept adds, so
465
620
  * they attach to the accepted word: closing punctuation, and `-` for compounds
@@ -621,6 +776,14 @@ export class Editor implements Component, Focusable {
621
776
  describeDecorations: ((lines: readonly string[]) => readonly TspEditorDecoration[]) | undefined;
622
777
  /** TSP code language of the buffer (`python`, `bash`): the terminal highlights it in the mono face. */
623
778
  describeLanguage: (() => string | undefined) | undefined;
779
+ /** Label the TSP field to disable host-side editing while another input owner handles raw keys. */
780
+ nativeKeyPassthrough = false;
781
+ /**
782
+ * Ask the TSP terminal to keep what is typed into this field and draw it at
783
+ * the caret until this clears, then send it: set while startup cannot read
784
+ * input yet (the prompt composer's deferred-input start).
785
+ */
786
+ nativeTypeahead = false;
624
787
  /**
625
788
  * TSP chrome around the `editor` node (the prompt composer's chips, mode
626
789
  * chip, send/stop): the root role, the children with `input` placed where
@@ -676,6 +839,8 @@ export class Editor implements Component, Focusable {
676
839
  #lastAction: "kill" | "yank" | "type-word" | "accept-word" | "replace" | null = null;
677
840
  /** Graphemes overwritten in the current `R` session, so Backspace can restore them. */
678
841
  #replaceLog: { line: number; col: number; removed: string; text: string }[] = [];
842
+ /** Insert/Replace session a Vim command opened; Escape hands its text to `.` and folds its undo steps into one. */
843
+ #vimSession: VimSessionState | null = null;
679
844
 
680
845
  // Character jump mode
681
846
  #jumpMode: "forward" | "backward" | null = null;
@@ -748,6 +913,13 @@ export class Editor implements Component, Focusable {
748
913
  // Undo stack for editor state changes
749
914
  #undoStack: EditorState[] = [];
750
915
  #suspendUndo = false;
916
+ /** Snapshots evicted from the front of {@link #undoStack}, so {@link UndoMark}s stay aligned. */
917
+ #undoShifts = 0;
918
+ /**
919
+ * Undo mark whose first snapshot `u` must still reach: the open Insert/Replace segment, or the
920
+ * Normal-mode key being applied (so a counted `.` stays one undo step past the stack cap).
921
+ */
922
+ #pinnedUndo: UndoMark | null = null;
751
923
 
752
924
  onSubmit?: (text: string) => void | Promise<void>;
753
925
  onAltEnter?: (text: string) => void;
@@ -917,8 +1089,15 @@ export class Editor implements Component, Focusable {
917
1089
  * never left in a state where ordinary typing does nothing. */
918
1090
  setVimMode(enabled: boolean): void {
919
1091
  if (enabled === (this.#vim !== null)) return;
1092
+ this.#vimSession = null;
1093
+ this.#pinnedUndo = null;
920
1094
  this.#vim = enabled ? new VimState() : null;
921
- if (this.#vim) this.#vim.mode = "insert";
1095
+ if (this.#vim) {
1096
+ this.#vim.mode = "insert";
1097
+ const mark = this.#undoMark();
1098
+ this.#pinnedUndo = mark;
1099
+ this.#startVimSession(mark, false);
1100
+ }
922
1101
  this.invalidate();
923
1102
  }
924
1103
 
@@ -1096,6 +1275,7 @@ export class Editor implements Component, Focusable {
1096
1275
  }
1097
1276
  /** Internal setText that doesn't reset history state - used by navigateHistory */
1098
1277
  #setTextInternal(text: string, cursorAnchor: HistoryCursorAnchor = "end"): void {
1278
+ this.#endVimSession();
1099
1279
  this.#undoStack.length = 0;
1100
1280
  const lines = sanitizeLoadedText(text).split("\n");
1101
1281
  this.#state.lines = lines.length === 0 ? [""] : lines;
@@ -1112,6 +1292,11 @@ export class Editor implements Component, Focusable {
1112
1292
  this.#state.cursorLine = this.#state.lines.length - 1;
1113
1293
  this.#setCursorCol(this.#state.lines[this.#state.cursorLine]?.length || 0);
1114
1294
  }
1295
+ if (this.#vim !== null && (this.#vim.mode === "insert" || this.#vim.mode === "replace")) {
1296
+ const mark = this.#undoMark();
1297
+ this.#pinnedUndo = mark;
1298
+ this.#startVimSession(mark, this.#vim.mode === "replace");
1299
+ }
1115
1300
  this.#notifyChange();
1116
1301
  }
1117
1302
 
@@ -1717,9 +1902,10 @@ export class Editor implements Component, Focusable {
1717
1902
  decor: decor.decor,
1718
1903
  ghost: ghost ? plainText(ghost) : undefined,
1719
1904
  placeholder: nativePlaceholder || undefined,
1720
- mode: vim ? VIM_MODE_LABELS[vim.mode] : undefined,
1905
+ mode: this.nativeKeyPassthrough ? "PTY" : vim ? VIM_MODE_LABELS[vim.mode] : undefined,
1721
1906
  lang: this.describeLanguage?.(),
1722
1907
  maxLines: this.#maxHeight,
1908
+ typeahead: this.nativeTypeahead || undefined,
1723
1909
  };
1724
1910
  let editor = this.#nativeEditor;
1725
1911
  if (!editor || !sameProps(editor.props, props)) {
@@ -1783,8 +1969,62 @@ export class Editor implements Component, Focusable {
1783
1969
  // so a fragmented paste stream can never grow the call stack.
1784
1970
  let next: string | undefined = data;
1785
1971
  while (next !== undefined && next.length > 0) {
1972
+ const session = this.#vimSession;
1973
+ const revision = this.#textRevision;
1974
+ const line = this.#state.cursorLine;
1975
+ const col = this.#state.cursorCol;
1976
+ // A line-array copy is pointer-sized per line; the edit diff below skips unchanged lines whole.
1977
+ const linesBefore =
1978
+ session !== null && session.tracked && !session.replace ? this.#state.lines.slice() : undefined;
1786
1979
  next = this.#handleInputChunk(next);
1980
+ // A bare cursor move inside one Insert/Replace session splits it, like Vim's arrow keys.
1981
+ if (
1982
+ session !== null &&
1983
+ session === this.#vimSession &&
1984
+ this.#textRevision === revision &&
1985
+ (line !== this.#state.cursorLine || col !== this.#state.cursorCol)
1986
+ ) {
1987
+ this.#breakVimSession();
1988
+ } else if (
1989
+ session !== null &&
1990
+ session === this.#vimSession &&
1991
+ session.tracked &&
1992
+ !session.replace &&
1993
+ linesBefore !== undefined &&
1994
+ this.#textRevision !== revision
1995
+ ) {
1996
+ this.#trackVimSessionEdit(session, linesBefore);
1997
+ }
1998
+ }
1999
+ }
2000
+
2001
+ /**
2002
+ * Fold one chunk's buffer change into the session window. Counting original deletions here keeps
2003
+ * `deleteBefore`/`deleteAfter` exact even when typed text coincides with what was deleted
2004
+ * (Backspace then retyping the same grapheme), which a final-text diff cannot recover.
2005
+ */
2006
+ #trackVimSessionEdit(session: VimSessionState, linesBefore: readonly string[]): void {
2007
+ const linesAfter = this.#state.lines;
2008
+ const beforeLength = flatLength(linesBefore);
2009
+ const afterLength = flatLength(linesAfter);
2010
+ const { prefix: p, suffix: q } = diffFlatLines(linesBefore, linesAfter, beforeLength, afterLength);
2011
+ const removedEnd = beforeLength - q;
2012
+ const insLen = afterLength - p - q;
2013
+ const leftCut = Math.max(0, Math.min(removedEnd, session.leftEnd) - p);
2014
+ const rightCut = Math.max(0, removedEnd - Math.max(p, session.rightStart));
2015
+ const typedCut = removedEnd - p - leftCut - rightCut;
2016
+ if (insLen > 0 && p < session.leftEnd - leftCut) {
2017
+ // Insertion inside surviving original text can't be told apart from typed text; the
2018
+ // finalize diff still produces a bounded record.
2019
+ session.tracked = false;
2020
+ return;
1787
2021
  }
2022
+ if (leftCut > 0) session.delBefore += countGraphemes(sliceFlat(linesBefore, p, p + leftCut));
2023
+ if (rightCut > 0) {
2024
+ session.delAfter += countGraphemes(sliceFlat(linesBefore, removedEnd - rightCut, removedEnd));
2025
+ }
2026
+ session.leftEnd -= leftCut;
2027
+ session.rightStart = session.rightStart - leftCut - typedCut + insLen;
1788
2028
  }
1789
2029
 
1790
2030
  /** Process one input chunk. Returns the unconsumed tail of a completed paste, if any. */
@@ -2205,10 +2445,12 @@ export class Editor implements Component, Focusable {
2205
2445
  // Word left
2206
2446
  this.#resetKillSequence();
2207
2447
  this.#moveWordBackwards();
2448
+ this.#dismissAutocomplete();
2208
2449
  } else if (kb.matchesCanonical(canonical, "tui.editor.cursorWordRight")) {
2209
2450
  // Word right
2210
2451
  this.#resetKillSequence();
2211
2452
  this.#moveWordForwards();
2453
+ this.#dismissAutocomplete();
2212
2454
  }
2213
2455
  // Arrow keys
2214
2456
  else if (kb.matchesCanonical(canonical, "tui.editor.cursorUp")) {
@@ -2326,9 +2568,31 @@ export class Editor implements Component, Focusable {
2326
2568
  const before = vim.mode;
2327
2569
  const pendingBefore = vim.pendingText;
2328
2570
  const selectedLinesBefore = this.vimSelectedLines;
2571
+ const mark = this.#undoMark();
2572
+ const pinHere = this.#vimSession === null;
2573
+ if (pinHere) this.#pinnedUndo = mark;
2574
+ const revisionBefore = this.#textRevision;
2329
2575
  const commands = vim.handleKey(key, this.#state);
2330
- if (commands === null) return false;
2576
+ if (commands === null) {
2577
+ if (pinHere) this.#pinnedUndo = null;
2578
+ return false;
2579
+ }
2580
+ const editing = (mode: VimMode) => mode === "insert" || mode === "replace";
2581
+ // Before applying: Escape's commands would otherwise clear the replace log this reads.
2582
+ if (editing(before) && !editing(vim.mode)) this.#endVimSession();
2331
2583
  this.#applyVimCommands(commands);
2584
+ if (this.#textRevision !== revisionBefore) {
2585
+ vim.commitChange();
2586
+ } else {
2587
+ vim.discardChange();
2588
+ }
2589
+ // The session baseline follows `cw`'s delete or `o`'s new line, but its undo mark precedes them,
2590
+ // so one `u` restores the whole change. Every other key (`.` too) is a single undo step.
2591
+ if (!editing(before) && editing(vim.mode)) this.#startVimSession(mark, vim.mode === "replace");
2592
+ else if (this.#vimSession === null) {
2593
+ this.#collapseUndoSince(mark);
2594
+ this.#pinnedUndo = null;
2595
+ }
2332
2596
  // Pending and selection size are mode chrome too: hosts echo `2d` and the Visual line count
2333
2597
  // beside the mode name, and extending a selection changes neither the mode nor the pending
2334
2598
  // command — so all three have to be compared or the indicator goes stale mid-selection.
@@ -2340,6 +2604,89 @@ export class Editor implements Component, Focusable {
2340
2604
  return true;
2341
2605
  }
2342
2606
 
2607
+ #undoMark(): UndoMark {
2608
+ return { depth: this.#undoStack.length, shifts: this.#undoShifts };
2609
+ }
2610
+
2611
+ /** Keep only the first snapshot pushed since `mark`, folding everything after it into one undo step. */
2612
+ #collapseUndoSince(mark: UndoMark): void {
2613
+ const keep = Math.max(0, mark.depth - (this.#undoShifts - mark.shifts)) + 1;
2614
+ if (this.#undoStack.length > keep) this.#undoStack.length = keep;
2615
+ }
2616
+
2617
+ #startVimSession(mark: UndoMark, replace: boolean): void {
2618
+ // A stale "type-word" would coalesce the session's first keystroke into the pre-session
2619
+ // undo step, so `u` would swallow text the session never touched.
2620
+ this.#resetKillSequence();
2621
+ const { text, cursor } = flatten(this.#state);
2622
+ this.#vimSession = {
2623
+ mark,
2624
+ replace,
2625
+ text,
2626
+ cursor,
2627
+ arrowed: false,
2628
+ leftEnd: cursor,
2629
+ rightStart: cursor,
2630
+ delBefore: 0,
2631
+ delAfter: 0,
2632
+ tracked: true,
2633
+ };
2634
+ }
2635
+
2636
+ /** Hand the current session segment's text to `.` and fold its undo steps. */
2637
+ #finalizeVimSegment(): void {
2638
+ const session = this.#vimSession;
2639
+ const vim = this.#vim;
2640
+ if (session === null || vim === null) return;
2641
+ const record: VimInsertRecord = session.replace
2642
+ ? { deleteBefore: 0, deleteAfter: 0, text: this.#replaceLog.map(entry => entry.text).join("") }
2643
+ : session.tracked
2644
+ ? {
2645
+ deleteBefore: session.delBefore,
2646
+ deleteAfter: session.delAfter,
2647
+ text: this.#state.lines.join("\n").slice(session.leftEnd, session.rightStart),
2648
+ }
2649
+ : diffInsertSession(session.text, session.cursor, this.#state.lines.join("\n"));
2650
+ if (!session.arrowed) vim.recordInsert(record, false);
2651
+ // An empty segment after an arrow keeps the earlier change, as Vim does.
2652
+ else if (record.text !== "" || record.deleteBefore > 0 || record.deleteAfter > 0) vim.recordInsert(record, true);
2653
+ this.#collapseUndoSince(session.mark);
2654
+ }
2655
+
2656
+ #endVimSession(): void {
2657
+ this.#finalizeVimSegment();
2658
+ this.#vimSession = null;
2659
+ this.#pinnedUndo = null;
2660
+ // A spent log would otherwise leak into the next Replace session's record.
2661
+ this.#replaceLog.length = 0;
2662
+ }
2663
+
2664
+ /** An arrow key inside Insert/Replace: close the segment and start a fresh one at the new cursor. */
2665
+ #breakVimSession(): void {
2666
+ const session = this.#vimSession;
2667
+ if (session === null) return;
2668
+ this.#finalizeVimSegment();
2669
+ this.#resetKillSequence();
2670
+ // Replace-mode Backspace cannot restore across a cursor move, and old overwrites must not leak
2671
+ // into the next segment's record.
2672
+ this.#replaceLog.length = 0;
2673
+ const { text, cursor } = flatten(this.#state);
2674
+ const mark = this.#undoMark();
2675
+ this.#vimSession = {
2676
+ ...session,
2677
+ mark,
2678
+ text,
2679
+ cursor,
2680
+ arrowed: true,
2681
+ leftEnd: cursor,
2682
+ rightStart: cursor,
2683
+ delBefore: 0,
2684
+ delAfter: 0,
2685
+ tracked: true,
2686
+ };
2687
+ this.#pinnedUndo = mark;
2688
+ }
2689
+
2343
2690
  #applyVimCommands(commands: readonly VimCommand[]): void {
2344
2691
  for (const command of commands) {
2345
2692
  // A delete or other edit in replace mode invalidates Backspace restores. Motions do not.
@@ -2391,6 +2738,9 @@ export class Editor implements Component, Focusable {
2391
2738
  case "indent":
2392
2739
  this.#indentVimLines(command.fromLine, command.toLine, command.out);
2393
2740
  break;
2741
+ case "retype":
2742
+ this.#retypeVimInsert(command);
2743
+ break;
2394
2744
  }
2395
2745
  }
2396
2746
  this.#clampVimCursor();
@@ -2419,7 +2769,7 @@ export class Editor implements Component, Focusable {
2419
2769
  // makes the logged columns meaningless; start a fresh undo step and a fresh log.
2420
2770
  if (this.#lastAction !== "replace") {
2421
2771
  this.#recordUndoState();
2422
- this.#replaceLog.length = 0;
2772
+ if (!this.#vimSession?.replace) this.#replaceLog.length = 0;
2423
2773
  }
2424
2774
  this.#lastAction = "replace";
2425
2775
  // Editing a recalled prompt makes it a draft, so Up/Down at an edge move the cursor again
@@ -2557,6 +2907,23 @@ export class Editor implements Component, Focusable {
2557
2907
 
2558
2908
  // A range that cuts through an atomic placeholder (`[Image #1, 800x600]`) swallows the whole
2559
2909
  // token instead of leaving a corrupt fragment — the same rule backspace follows.
2910
+ const { start, end } = this.#expandVimRange(from, to);
2911
+
2912
+ const removed = this.#sliceRange(start, end, false);
2913
+ if (!removed) return;
2914
+ this.#recordUndoState();
2915
+ this.#killRing.push(removed, { prepend: false });
2916
+ const head = (lines[start.line] ?? "").slice(0, start.col);
2917
+ const tail = (lines[Math.min(end.line, lines.length - 1)] ?? "").slice(end.col);
2918
+ lines.splice(start.line, Math.min(end.line, lines.length - 1) - start.line + 1, head + tail);
2919
+ this.#state.cursorLine = start.line;
2920
+ this.#setCursorCol(start.col);
2921
+ this.#afterVimEdit();
2922
+ }
2923
+
2924
+ /** Widen a charwise range so it never cuts through an atomic placeholder token. */
2925
+ #expandVimRange(from: VimPosition, to: VimPosition): { start: VimPosition; end: VimPosition } {
2926
+ const lines = this.#state.lines;
2560
2927
  let start = from;
2561
2928
  let end = to;
2562
2929
  if (start.line === end.line) {
@@ -2574,16 +2941,54 @@ export class Editor implements Component, Focusable {
2574
2941
  if (endToken !== undefined && endToken.end > end.col) end = { line: end.line, col: endToken.end };
2575
2942
  }
2576
2943
  }
2944
+ return { start, end };
2945
+ }
2577
2946
 
2578
- const removed = this.#sliceRange(start, end, false);
2579
- if (!removed) return;
2947
+ /** `.` after an Insert/Replace session: type its text again, `times` copies, then Escape's step back. */
2948
+ #retypeVimInsert(command: Extract<VimCommand, { kind: "retype" }>): void {
2949
+ for (let n = 0; n < command.times; n++) {
2950
+ // `3.` after `ofoo`: one new line per copy, stacking downward (Vim's `3Ofoo` does too).
2951
+ if (n > 0 && command.newline) this.#openVimLine(true);
2952
+ if (command.overwrite) {
2953
+ for (const seg of segmenter.segment(command.record.text)) {
2954
+ if (seg.segment === "\n" || seg.segment === "\r\n") this.#addNewLine();
2955
+ else this.#overwriteReplaceGrapheme(seg.segment);
2956
+ }
2957
+ } else {
2958
+ this.#spliceVimInsert(command.record);
2959
+ }
2960
+ }
2961
+ if (command.overwrite) {
2962
+ this.#replaceLog.length = 0;
2963
+ this.#resetKillSequence();
2964
+ }
2965
+ const line = this.#state.lines[this.#state.cursorLine] ?? "";
2966
+ this.#setCursorCol(prevGraphemeStart(line, this.#state.cursorCol));
2967
+ }
2968
+
2969
+ /**
2970
+ * Replay one Insert session at the cursor. Built directly rather than through the typing path,
2971
+ * which would pop autocomplete from Normal mode and feed the kill ring.
2972
+ */
2973
+ #spliceVimInsert(record: VimInsertRecord): void {
2974
+ const lines = this.#state.lines;
2975
+ const { text: flat, cursor } = flatten(this.#state);
2976
+ const { start, end } = this.#expandVimRange(
2977
+ offsetToPos(lines, stepGraphemes(flat, cursor, record.deleteBefore, false)),
2978
+ offsetToPos(lines, stepGraphemes(flat, cursor, record.deleteAfter, true)),
2979
+ );
2980
+ if (record.text === "" && start.line === end.line && start.col === end.col) return;
2580
2981
  this.#recordUndoState();
2581
- this.#killRing.push(removed, { prepend: false });
2582
2982
  const head = (lines[start.line] ?? "").slice(0, start.col);
2583
- const tail = (lines[Math.min(end.line, lines.length - 1)] ?? "").slice(end.col);
2584
- lines.splice(start.line, Math.min(end.line, lines.length - 1) - start.line + 1, head + tail);
2585
- this.#state.cursorLine = start.line;
2586
- this.#setCursorCol(start.col);
2983
+ const tail = (lines[end.line] ?? "").slice(end.col);
2984
+ const parts = record.text.split("\n");
2985
+ const rows = parts.slice();
2986
+ rows[0] = head + rows[0];
2987
+ rows[rows.length - 1] += tail;
2988
+ lines.splice(start.line, end.line - start.line + 1, ...rows);
2989
+ this.#state.cursorLine = start.line + parts.length - 1;
2990
+ this.#setCursorCol((parts.length === 1 ? head.length : 0) + (parts.at(-1) ?? "").length);
2991
+ this.#lastAction = null;
2587
2992
  this.#afterVimEdit();
2588
2993
  }
2589
2994
 
@@ -3092,17 +3497,25 @@ export class Editor implements Component, Focusable {
3092
3497
  if (!resolved) return;
3093
3498
  this.#resetKillSequence();
3094
3499
  if (resolved.changed) {
3500
+ if (this.#vimSession) this.#vimSession.tracked = false;
3095
3501
  this.#historyIndex = -1;
3096
3502
  this.#recordUndoState();
3097
3503
  this.#state.lines = resolved.text.split("\n");
3098
3504
  }
3099
3505
  const lines = this.#state.lines;
3506
+ const priorLine = this.#state.cursorLine;
3507
+ const priorCol = this.#state.cursorCol;
3100
3508
  let line = 0;
3101
3509
  let col = resolved.cursor;
3102
3510
  while (line < lines.length - 1 && col > lines[line]!.length) col -= lines[line++]!.length + 1;
3103
3511
  this.#state.cursorLine = line;
3104
3512
  this.#setCursorCol(col);
3105
- if (!resolved.changed) return;
3513
+ if (!resolved.changed) {
3514
+ if (this.#vimSession !== null && (priorLine !== line || priorCol !== col)) {
3515
+ this.#breakVimSession();
3516
+ }
3517
+ return;
3518
+ }
3106
3519
  this.#notifyChange(resolved.text);
3107
3520
  this.#retriggerAutocompleteAtCursor();
3108
3521
  }
@@ -3503,6 +3916,9 @@ export class Editor implements Component, Focusable {
3503
3916
  const dropSpace = this.#hasProvisionalSpace();
3504
3917
  this.#historyIndex = -1; // Exit history browsing mode
3505
3918
  this.#resetKillSequence();
3919
+ // The cursor leaves the trigger token's line, so any open popup is stale:
3920
+ // keeping it would float on the new line and Enter would submit past it.
3921
+ this.#dismissAutocomplete();
3506
3922
  this.#recordUndoState();
3507
3923
 
3508
3924
  const currentLine = this.#state.lines[this.#state.cursorLine] || "";
@@ -3518,6 +3934,9 @@ export class Editor implements Component, Focusable {
3518
3934
  // Move cursor to start of new line
3519
3935
  this.#state.cursorLine++;
3520
3936
  this.#setCursorCol(0);
3937
+ if (this.#vimSession?.replace) {
3938
+ this.#replaceLog.push({ line: this.#state.cursorLine - 1, col: splitCol, removed: "", text: "\n" });
3939
+ }
3521
3940
 
3522
3941
  this.#notifyChange();
3523
3942
  }
@@ -3540,6 +3959,7 @@ export class Editor implements Component, Focusable {
3540
3959
  }
3541
3960
 
3542
3961
  #submitValue(): void {
3962
+ this.#endVimSession();
3543
3963
  this.#resetKillSequence();
3544
3964
 
3545
3965
  const result = this.#expandPasteMarkers(this.#state.lines.join("\n")).trim();
@@ -3550,6 +3970,11 @@ export class Editor implements Component, Focusable {
3550
3970
  this.#scrollOffset = 0;
3551
3971
  this.#undoStack.length = 0;
3552
3972
 
3973
+ if (this.#vim !== null && (this.#vim.mode === "insert" || this.#vim.mode === "replace")) {
3974
+ const mark = this.#undoMark();
3975
+ this.#pinnedUndo = mark;
3976
+ this.#startVimSession(mark, this.#vim.mode === "replace");
3977
+ }
3553
3978
  this.#notifyChange("");
3554
3979
  if (this.onSubmit) this.onSubmit(result);
3555
3980
  }
@@ -3795,23 +4220,27 @@ export class Editor implements Component, Focusable {
3795
4220
 
3796
4221
  #moveToLineStart(): void {
3797
4222
  this.#resetKillSequence();
4223
+ this.#dismissAutocomplete();
3798
4224
  this.#setCursorCol(0);
3799
4225
  }
3800
4226
 
3801
4227
  #moveToLineEnd(): void {
3802
4228
  this.#resetKillSequence();
4229
+ this.#dismissAutocomplete();
3803
4230
  const currentLine = this.#state.lines[this.#state.cursorLine] || "";
3804
4231
  this.#setCursorCol(currentLine.length);
3805
4232
  }
3806
4233
 
3807
4234
  #moveToMessageStart(): void {
3808
4235
  this.#resetKillSequence();
4236
+ this.#dismissAutocomplete();
3809
4237
  this.#state.cursorLine = 0;
3810
4238
  this.#setCursorCol(0);
3811
4239
  }
3812
4240
 
3813
4241
  #moveToMessageEnd(): void {
3814
4242
  this.#resetKillSequence();
4243
+ this.#dismissAutocomplete();
3815
4244
  this.#state.cursorLine = this.#state.lines.length - 1;
3816
4245
  const currentLine = this.#state.lines[this.#state.cursorLine] || "";
3817
4246
  this.#setCursorCol(currentLine.length);
@@ -3841,9 +4270,17 @@ export class Editor implements Component, Focusable {
3841
4270
  cursorLine: this.#state.cursorLine,
3842
4271
  cursorCol: this.#state.cursorCol,
3843
4272
  });
3844
- if (this.#undoStack.length > MAX_UNDO_STACK) {
3845
- this.#undoStack.shift();
4273
+ if (this.#undoStack.length <= MAX_UNDO_STACK) return;
4274
+ const pin = this.#pinnedUndo;
4275
+ const anchorIndex = pin === null ? -1 : pin.depth - (this.#undoShifts - pin.shifts);
4276
+ if (anchorIndex === 0 && this.#undoStack.length > 1) {
4277
+ // Front entry is the open change's restore point. Drop the snapshot after it, and don't
4278
+ // bump #undoShifts: index 0 did not move, so the mark still points at it.
4279
+ this.#undoStack.splice(1, 1);
4280
+ return;
3846
4281
  }
4282
+ this.#undoStack.shift();
4283
+ this.#undoShifts++;
3847
4284
  }
3848
4285
 
3849
4286
  #applyUndo(): void {
@@ -4283,6 +4720,7 @@ export class Editor implements Component, Focusable {
4283
4720
 
4284
4721
  #moveCursor(deltaLine: number, deltaCol: number): void {
4285
4722
  this.#resetKillSequence();
4723
+ this.#dismissAutocomplete();
4286
4724
  const visualLines = this.#buildVisualLineMap(this.#caretRowWidth);
4287
4725
  const currentVisualLine = this.#findCurrentVisualLine(visualLines);
4288
4726
 
@@ -4335,6 +4773,7 @@ export class Editor implements Component, Focusable {
4335
4773
 
4336
4774
  #pageScroll(direction: -1 | 1): void {
4337
4775
  this.#resetKillSequence();
4776
+ this.#dismissAutocomplete();
4338
4777
  const visualLines = this.#buildVisualLineMap(this.#caretRowWidth);
4339
4778
  const currentVisualLine = this.#findCurrentVisualLine(visualLines);
4340
4779
  const step = this.#getPageScrollStep(visualLines.length);
@@ -4365,6 +4804,7 @@ export class Editor implements Component, Focusable {
4365
4804
  */
4366
4805
  #jumpToChar(char: string, direction: "forward" | "backward"): void {
4367
4806
  this.#resetKillSequence();
4807
+ this.#dismissAutocomplete();
4368
4808
  const isForward = direction === "forward";
4369
4809
  const lines = this.#state.lines;
4370
4810
 
@@ -4781,6 +5221,17 @@ export class Editor implements Component, Focusable {
4781
5221
  }
4782
5222
  }
4783
5223
 
5224
+ /**
5225
+ * Close the popup when the cursor leaves the token it was opened for (newline,
5226
+ * pure cursor motion). Its prefix no longer matches the text before the cursor,
5227
+ * so it could neither accept nor refresh and would linger stale.
5228
+ */
5229
+ #dismissAutocomplete(): void {
5230
+ if (this.#autocompleteState === null) return;
5231
+ this.#cancelAutocomplete();
5232
+ this.onAutocompleteUpdate?.();
5233
+ }
5234
+
4784
5235
  /**
4785
5236
  * Whether an autocomplete popup is on screen. An `@` list narrowed to no match
4786
5237
  * while its refresh is pending stays open internally but is hidden, so it does