@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
package/src/vim.ts CHANGED
@@ -41,7 +41,17 @@ export type VimCommand =
41
41
  | { kind: "undo" }
42
42
  | { kind: "replace"; from: VimPosition; to: VimPosition; text: string }
43
43
  | { kind: "join"; fromLine: number; lines: number }
44
- | { kind: "indent"; fromLine: number; toLine: number; out: boolean };
44
+ | { kind: "indent"; fromLine: number; toLine: number; out: boolean }
45
+ | { kind: "retype"; record: VimInsertRecord; times: number; newline: boolean; overwrite: boolean };
46
+
47
+ /** What an Insert/Replace session typed, relative to where it started; `.` replays it. */
48
+ export interface VimInsertRecord {
49
+ /** Graphemes removed before the session's start cursor (Backspace past the insert point). */
50
+ deleteBefore: number;
51
+ /** Graphemes removed after it (forward delete). */
52
+ deleteAfter: number;
53
+ text: string;
54
+ }
45
55
 
46
56
  const segmenter = getSegmenter();
47
57
 
@@ -72,6 +82,77 @@ export function lastGraphemeStart(text: string): number {
72
82
  return prevGraphemeStart(text, text.length);
73
83
  }
74
84
 
85
+ export function countGraphemes(text: string): number {
86
+ let count = 0;
87
+ for (const _ of segmenter.segment(text)) count++;
88
+ return count;
89
+ }
90
+
91
+ /** Offset `count` graphemes forward or backward from `offset`, clamped to the text. */
92
+ export function stepGraphemes(text: string, offset: number, count: number, forward: boolean): number {
93
+ if (count <= 0) return offset;
94
+ const boundaries: number[] = [];
95
+ for (const seg of segmenter.segment(text)) boundaries.push(seg.index);
96
+ boundaries.push(text.length);
97
+ let i = 0;
98
+ while (i + 1 < boundaries.length && boundaries[i + 1]! <= offset) i++;
99
+ const last = boundaries.length - 1;
100
+ if (forward) return boundaries[Math.min(i + count, last)]!;
101
+ return boundaries[Math.max(i - count + (boundaries[i]! < offset ? 1 : 0), 0)]!;
102
+ }
103
+
104
+ /**
105
+ * What a session turned `before` into, anchored at its start `cursor`: the prefix match stops at the
106
+ * cursor and the suffix match never crosses it, so typing `aa` before an existing `a` records `aa`
107
+ * at the cursor rather than after the old one. Comparisons are whole graphemes, so a shared high
108
+ * surrogate cannot split an emoji into the replayed text.
109
+ */
110
+ export function diffInsertSession(before: string, cursor: number, after: string): VimInsertRecord {
111
+ const beforeBounds = graphemeBoundaries(before);
112
+ const afterBounds = graphemeBoundaries(after);
113
+ let cursorBound = 0;
114
+ while (cursorBound + 1 < beforeBounds.length && beforeBounds[cursorBound + 1]! <= cursor) cursorBound++;
115
+
116
+ let prefix = 0;
117
+ while (
118
+ prefix < cursorBound &&
119
+ prefix + 1 < afterBounds.length &&
120
+ before.slice(beforeBounds[prefix]!, beforeBounds[prefix + 1]!) ===
121
+ after.slice(afterBounds[prefix]!, afterBounds[prefix + 1]!)
122
+ ) {
123
+ prefix++;
124
+ }
125
+
126
+ const beforeEnd = beforeBounds.length - 1;
127
+ const afterEnd = afterBounds.length - 1;
128
+ let suffix = 0;
129
+ while (
130
+ beforeEnd - suffix > cursorBound &&
131
+ afterEnd - suffix > prefix &&
132
+ before.slice(beforeBounds[beforeEnd - suffix - 1]!, beforeBounds[beforeEnd - suffix]!) ===
133
+ after.slice(afterBounds[afterEnd - suffix - 1]!, afterBounds[afterEnd - suffix]!)
134
+ ) {
135
+ suffix++;
136
+ }
137
+
138
+ return {
139
+ deleteBefore: cursorBound - prefix,
140
+ deleteAfter: beforeEnd - suffix - cursorBound,
141
+ text: after.slice(afterBounds[prefix]!, afterBounds[afterEnd - suffix]!),
142
+ };
143
+ }
144
+
145
+ /** Inclusive start offsets plus `text.length`, one entry per grapheme boundary. */
146
+ function graphemeBoundaries(text: string): number[] {
147
+ const bounds = [0];
148
+ for (const seg of segmenter.segment(text)) {
149
+ const end = seg.index + seg.segment.length;
150
+ if (end !== bounds[bounds.length - 1]) bounds.push(end);
151
+ }
152
+ if (bounds[bounds.length - 1] !== text.length) bounds.push(text.length);
153
+ return bounds;
154
+ }
155
+
75
156
  function firstNonBlank(text: string): number {
76
157
  for (const seg of segmenter.segment(text)) {
77
158
  if (seg.segment.trim() !== "") return seg.index;
@@ -205,13 +286,13 @@ function quoteObject(text: string, col: number, quote: string, around: boolean):
205
286
  }
206
287
 
207
288
  /** Buffer flattened to one string plus the cursor's offset in it, so scans can cross lines. */
208
- function flatten(buf: VimBuffer): { text: string; cursor: number } {
289
+ export function flatten(buf: VimBuffer): { text: string; cursor: number } {
209
290
  let cursor = buf.cursorCol;
210
291
  for (let i = 0; i < buf.cursorLine; i++) cursor += (buf.lines[i] ?? "").length + 1;
211
292
  return { text: buf.lines.join("\n"), cursor };
212
293
  }
213
294
 
214
- function offsetToPos(lines: readonly string[], offset: number): VimPosition {
295
+ export function offsetToPos(lines: readonly string[], offset: number): VimPosition {
215
296
  let remaining = offset;
216
297
  for (let line = 0; line < lines.length; line++) {
217
298
  const width = (lines[line] ?? "").length;
@@ -396,6 +477,34 @@ interface Motion {
396
477
  linewise: boolean;
397
478
  }
398
479
 
480
+ /** Commands whose count repeats the typed text rather than a motion (`3.` after `ifoo` → `foofoofoo`). */
481
+ const INSERT_KEYS: Record<string, true> = { i: true, a: true, I: true, A: true, o: true, O: true, R: true };
482
+ const EDIT_KINDS: Partial<Record<VimCommand["kind"], true>> = {
483
+ delete: true,
484
+ replace: true,
485
+ join: true,
486
+ indent: true,
487
+ paste: true,
488
+ openLine: true,
489
+ };
490
+ const EMPTY_RECORD: VimInsertRecord = { deleteBefore: 0, deleteAfter: 0, text: "" };
491
+
492
+ /**
493
+ * The last change `.` repeats. Normal-mode changes replay their keys so motions and text objects
494
+ * re-resolve at the new cursor; Visual changes replay the selection's shape, as Vim does.
495
+ */
496
+ type VimChange =
497
+ | { kind: "keys"; keys: string[]; count: number | null; insert: VimInsertRecord | null }
498
+ | {
499
+ kind: "visual";
500
+ key: string;
501
+ linewise: boolean;
502
+ lines: number;
503
+ graphemes: number;
504
+ endCol: number;
505
+ insert: VimInsertRecord | null;
506
+ };
507
+
399
508
  export class VimState {
400
509
  mode: VimMode = "normal";
401
510
  /** Fixed end of a Visual selection; the cursor is the moving end. */
@@ -420,6 +529,12 @@ export class VimState {
420
529
  * end-of-line, which keeps `$j` on the end of each line. Every non-vertical command clears it.
421
530
  */
422
531
  #desiredCol: number | null = null;
532
+ /** Keys of the Normal-mode command being typed, count digits excluded. */
533
+ #cmdKeys: string[] = [];
534
+ /** Every count digit typed in that command, in order. */
535
+ #cmdCount = "";
536
+ #lastChange: VimChange | null = null;
537
+ #stagedChange: VimChange | null = null;
423
538
 
424
539
  /** True while a count, operator, `g`, text-object, find, or `r` prefix is half-typed — Escape cancels it. */
425
540
  get pending(): boolean {
@@ -452,6 +567,19 @@ export class VimState {
452
567
  this.#desiredCol = null;
453
568
  this.#clearPending();
454
569
  this.#lastFind = null;
570
+ this.#cmdKeys = [];
571
+ this.#cmdCount = "";
572
+ this.#lastChange = null;
573
+ this.#stagedChange = null;
574
+ }
575
+
576
+ /** `0` is the line-start motion unless it extends a count already being typed. */
577
+ #isCountKey(key: string): boolean {
578
+ return (
579
+ !this.#findPending &&
580
+ !this.#replaceChar &&
581
+ ((key >= "1" && key <= "9") || (key === "0" && this.#count.length > 0))
582
+ );
455
583
  }
456
584
 
457
585
  #clearPending(): void {
@@ -484,8 +612,143 @@ export class VimState {
484
612
  * through to its regular handling (and, for Escape in Normal mode, to the app's interrupt).
485
613
  */
486
614
  handleKey(key: string, buf: VimBuffer): VimCommand[] | null {
487
- if (key === "escape") return this.#handleEscape(buf);
615
+ if (key === "escape") {
616
+ this.#cmdKeys = [];
617
+ this.#cmdCount = "";
618
+ return this.#handleEscape(buf);
619
+ }
488
620
  if (this.mode === "insert") return null;
621
+ const recording = this.mode === "normal";
622
+ const dotIsTarget = this.#findPending !== null || this.#replaceChar;
623
+ if (recording) {
624
+ if (this.#isCountKey(key)) this.#cmdCount += key;
625
+ else this.#cmdKeys.push(key);
626
+ }
627
+ const commands = this.#dispatch(key, buf);
628
+ if (recording && !this.pending) {
629
+ // `#dispatch` may have entered Insert; widen past the narrowing from the early return above.
630
+ const mode = this.mode as VimMode;
631
+ const editing = mode === "insert" || mode === "replace";
632
+ // `.` is skipped only as the top-level repeat command; as an `f`/`t`/`r` target (`df.`)
633
+ // it completes a recordable edit.
634
+ if (
635
+ commands !== null &&
636
+ (key !== "." || dotIsTarget) &&
637
+ (editing || commands.some(c => EDIT_KINDS[c.kind] === true))
638
+ ) {
639
+ // Insert-family commands ignore their own count, so `.` must not multiply what they typed.
640
+ const insertKey = INSERT_KEYS[this.#cmdKeys[0] ?? ""] === true;
641
+ const change: VimChange = {
642
+ kind: "keys",
643
+ keys: this.#cmdKeys,
644
+ count: insertKey || this.#cmdCount === "" ? null : Number.parseInt(this.#cmdCount, 10),
645
+ insert: null,
646
+ };
647
+ if (editing) {
648
+ this.#lastChange = change;
649
+ this.#stagedChange = null;
650
+ } else {
651
+ this.#stagedChange = change;
652
+ }
653
+ }
654
+ this.#cmdKeys = [];
655
+ this.#cmdCount = "";
656
+ }
657
+ return commands;
658
+ }
659
+
660
+ /**
661
+ * Attach what an Insert/Replace session typed to the change that opened it. After an arrow key
662
+ * split the session, Vim's redo becomes a plain insert of the text typed since.
663
+ */
664
+ recordInsert(record: VimInsertRecord, afterArrow: boolean): void {
665
+ if (afterArrow) {
666
+ this.#lastChange = { kind: "keys", keys: ["i"], count: null, insert: record };
667
+ } else if (this.#lastChange !== null) {
668
+ this.#lastChange = { ...this.#lastChange, insert: record };
669
+ } else if (record.text !== "" || record.deleteBefore > 0 || record.deleteAfter > 0) {
670
+ this.#lastChange = { kind: "keys", keys: ["i"], count: null, insert: record };
671
+ }
672
+ }
673
+
674
+ /** Commit a staged change when the editor confirms text changed. */
675
+ commitChange(): void {
676
+ if (this.#stagedChange !== null) {
677
+ this.#lastChange = this.#stagedChange;
678
+ this.#stagedChange = null;
679
+ }
680
+ }
681
+
682
+ /** Discard a staged change when the command was a no-op (e.g. `J` on the last line). */
683
+ discardChange(): void {
684
+ this.#stagedChange = null;
685
+ }
686
+ #repeat(buf: VimBuffer, dotCount: number | null): VimCommand[] {
687
+ const change = this.#lastChange;
688
+ if (change === null) return [];
689
+ let commands: VimCommand[];
690
+ let count: number | null = null;
691
+ if (change.kind === "keys") {
692
+ count = dotCount ?? change.count;
693
+ commands = [];
694
+ // Every key but the last only arms a prefix and edits nothing, so one buffer snapshot holds.
695
+ for (const key of count === null ? change.keys : [...String(count), ...change.keys]) {
696
+ commands = this.#dispatch(key, buf) ?? [];
697
+ }
698
+ this.#lastChange = { ...change, count };
699
+ } else {
700
+ commands = this.#repeatVisual(change, buf);
701
+ }
702
+ if (this.mode === "insert" || this.mode === "replace") {
703
+ const insertKey = change.kind === "keys" && INSERT_KEYS[change.keys[0] ?? ""] === true;
704
+ commands.push({
705
+ kind: "retype",
706
+ record: change.insert ?? EMPTY_RECORD,
707
+ times: insertKey ? (count ?? 1) : 1,
708
+ newline: insertKey && change.kind === "keys" && (change.keys[0] === "o" || change.keys[0] === "O"),
709
+ overwrite: this.mode === "replace",
710
+ });
711
+ this.mode = "normal";
712
+ commands.push({ kind: "mode", mode: "normal" });
713
+ }
714
+ return commands;
715
+ }
716
+
717
+ /** Re-applies a Visual change over the same shape starting at the cursor; `.`'s count is ignored. */
718
+ #repeatVisual(change: Extract<VimChange, { kind: "visual" }>, buf: VimBuffer): VimCommand[] {
719
+ const fromLine = buf.cursorLine;
720
+ const toLine = Math.min(fromLine + change.lines - 1, buf.lines.length - 1);
721
+ const lineAt = (line: number) => buf.lines[line] ?? "";
722
+ const cursor = cursorOf(buf);
723
+ let from: VimPosition;
724
+ let to: VimPosition;
725
+ if (change.linewise) {
726
+ from = { line: fromLine, col: 0 };
727
+ to = { line: toLine, col: lineAt(toLine).length };
728
+ } else if (change.lines === 1) {
729
+ from = cursor;
730
+ to = { line: fromLine, col: stepGraphemes(lineAt(fromLine), buf.cursorCol, change.graphemes, true) };
731
+ } else {
732
+ from = cursor;
733
+ // `endCol` was measured on the original last line, so it can land mid-grapheme on a
734
+ // destination line with longer clusters; snap it up to a boundary or a replayed `d`
735
+ // would leave half an emoji behind.
736
+ const text = lineAt(toLine);
737
+ const target = Math.min(change.endCol, text.length);
738
+ let endCol = text.length;
739
+ for (const seg of segmenter.segment(text)) {
740
+ if (seg.index >= target) {
741
+ endCol = seg.index;
742
+ break;
743
+ }
744
+ endCol = seg.index + seg.segment.length;
745
+ }
746
+ to = { line: toLine, col: endCol };
747
+ }
748
+ return this.#visualEdit(change.key, from, to, change.linewise);
749
+ }
750
+
751
+ #dispatch(key: string, buf: VimBuffer): VimCommand[] | null {
489
752
  // The character after `f`/`t` is the target, including digits. Resolve it before the count prefix.
490
753
  if (this.#findPending) {
491
754
  const spec = this.#findPending;
@@ -527,8 +790,7 @@ export class VimState {
527
790
  ];
528
791
  }
529
792
 
530
- // Count prefix. `0` is the line-start motion unless it extends a count already being typed.
531
- if ((key >= "1" && key <= "9") || (key === "0" && this.#count.length > 0)) {
793
+ if (this.#isCountKey(key)) {
532
794
  this.#count += key;
533
795
  return [];
534
796
  }
@@ -963,6 +1225,14 @@ export class VimState {
963
1225
  case "u":
964
1226
  this.#takeCount();
965
1227
  return [{ kind: "undo" }];
1228
+ case ".": {
1229
+ if (this.#operator !== null) {
1230
+ this.#clearPending();
1231
+ return [];
1232
+ }
1233
+ const dotCount = this.#count.length > 0 ? this.#takeCount() : null;
1234
+ return this.#repeat(buf, dotCount);
1235
+ }
966
1236
  case "J": {
967
1237
  if (this.#operator !== null) {
968
1238
  this.#clearPending();
@@ -1019,34 +1289,55 @@ export class VimState {
1019
1289
  case "d":
1020
1290
  case "x":
1021
1291
  case "c":
1022
- case "s": {
1023
- const operator: VimOperator = key === "y" ? "y" : key === "c" || key === "s" ? "c" : "d";
1024
- const { from, to } = visualRange(buf, anchor, linewise);
1025
- this.#clearPending();
1026
- this.anchor = null;
1027
- // `#operate` switches to Insert itself for `c`; everything else drops back to Normal.
1028
- if (operator !== "c") this.mode = "normal";
1029
- const commands = this.#operate(operator, from, to, linewise);
1030
- return operator === "c" ? commands : [...commands, { kind: "mode", mode: "normal" }];
1031
- }
1292
+ case "s":
1032
1293
  case ">":
1033
1294
  case "<":
1034
1295
  case "J": {
1035
1296
  const { from, to } = visualRange(buf, anchor, linewise);
1036
1297
  this.#clearPending();
1037
1298
  this.anchor = null;
1038
- this.mode = "normal";
1039
- const edit: VimCommand =
1040
- key === "J"
1041
- ? { kind: "join", fromLine: from.line, lines: Math.max(2, to.line - from.line + 1) }
1042
- : { kind: "indent", fromLine: from.line, toLine: to.line, out: key === "<" };
1043
- return [edit, { kind: "mode", mode: "normal" }];
1299
+ if (key !== "y") {
1300
+ const change: VimChange = {
1301
+ kind: "visual",
1302
+ key,
1303
+ linewise,
1304
+ lines: to.line - from.line + 1,
1305
+ graphemes:
1306
+ from.line === to.line ? countGraphemes((buf.lines[from.line] ?? "").slice(from.col, to.col)) : 0,
1307
+ endCol: to.col,
1308
+ insert: null,
1309
+ };
1310
+ if (key === "c" || key === "s") {
1311
+ this.#lastChange = change;
1312
+ this.#stagedChange = null;
1313
+ } else {
1314
+ this.#stagedChange = change;
1315
+ }
1316
+ }
1317
+ return this.#visualEdit(key, from, to, linewise);
1044
1318
  }
1045
1319
  default:
1046
1320
  this.#clearPending();
1047
1321
  return [];
1048
1322
  }
1049
1323
  }
1324
+
1325
+ /** Apply a Visual-mode edit key to the span `from`–`to` (end-exclusive), live or from `.`. */
1326
+ #visualEdit(key: string, from: VimPosition, to: VimPosition, linewise: boolean): VimCommand[] {
1327
+ if (key === ">" || key === "<" || key === "J") {
1328
+ this.mode = "normal";
1329
+ const edit: VimCommand =
1330
+ key === "J"
1331
+ ? { kind: "join", fromLine: from.line, lines: Math.max(2, to.line - from.line + 1) }
1332
+ : { kind: "indent", fromLine: from.line, toLine: to.line, out: key === "<" };
1333
+ return [edit, { kind: "mode", mode: "normal" }];
1334
+ }
1335
+ const operator: VimOperator = key === "y" ? "y" : key === "c" || key === "s" ? "c" : "d";
1336
+ // `#operate` switches to Insert itself for `c`; everything else drops back to Normal.
1337
+ if (operator !== "c") this.mode = "normal";
1338
+ const commands = this.#operate(operator, from, to, linewise);
1339
+ return operator === "c" ? commands : [...commands, { kind: "mode", mode: "normal" }];
1340
+ }
1050
1341
  }
1051
1342
 
1052
1343
  /**