@fairyhunter13/opentui-core 0.1.114 → 0.1.115

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 (591) hide show
  1. package/3d/SpriteResourceManager.d.ts +74 -0
  2. package/3d/SpriteUtils.d.ts +13 -0
  3. package/3d/TextureUtils.d.ts +24 -0
  4. package/3d/ThreeRenderable.d.ts +40 -0
  5. package/3d/WGPURenderer.d.ts +61 -0
  6. package/3d/animation/ExplodingSpriteEffect.d.ts +71 -0
  7. package/3d/animation/PhysicsExplodingSpriteEffect.d.ts +76 -0
  8. package/3d/animation/SpriteAnimator.d.ts +124 -0
  9. package/3d/animation/SpriteParticleGenerator.d.ts +62 -0
  10. package/3d/canvas.d.ts +44 -0
  11. package/3d/index.d.ts +12 -0
  12. package/3d/physics/PlanckPhysicsAdapter.d.ts +19 -0
  13. package/3d/physics/RapierPhysicsAdapter.d.ts +19 -0
  14. package/3d/physics/physics-interface.d.ts +27 -0
  15. package/3d.d.ts +2 -0
  16. package/3d.js +34041 -0
  17. package/3d.js.map +155 -0
  18. package/LICENSE +21 -0
  19. package/NativeSpanFeed.d.ts +41 -0
  20. package/Renderable.d.ts +334 -0
  21. package/animation/Timeline.d.ts +126 -0
  22. package/ansi.d.ts +13 -0
  23. package/buffer.d.ts +111 -0
  24. package/console.d.ts +144 -0
  25. package/edit-buffer.d.ts +98 -0
  26. package/editor-view.d.ts +73 -0
  27. package/index-j4m38kjn.js +411 -0
  28. package/index-j4m38kjn.js.map +10 -0
  29. package/index-tse8gzh0.js +20614 -0
  30. package/index-tse8gzh0.js.map +67 -0
  31. package/index-vv2jcd4r.js +12299 -0
  32. package/index-vv2jcd4r.js.map +42 -0
  33. package/index.d.ts +23 -0
  34. package/index.js +478 -0
  35. package/index.js.map +9 -0
  36. package/lib/KeyHandler.d.ts +61 -0
  37. package/lib/RGBA.d.ts +25 -0
  38. package/lib/ascii.font.d.ts +508 -0
  39. package/lib/border.d.ts +51 -0
  40. package/lib/bunfs.d.ts +7 -0
  41. package/lib/clipboard.d.ts +17 -0
  42. package/lib/clock.d.ts +15 -0
  43. package/lib/data-paths.d.ts +26 -0
  44. package/lib/debounce.d.ts +42 -0
  45. package/lib/detect-links.d.ts +6 -0
  46. package/lib/env.d.ts +42 -0
  47. package/lib/extmarks-history.d.ts +17 -0
  48. package/lib/extmarks.d.ts +89 -0
  49. package/lib/hast-styled-text.d.ts +17 -0
  50. package/lib/index.d.ts +21 -0
  51. package/lib/keymapping.d.ts +25 -0
  52. package/lib/objects-in-viewport.d.ts +24 -0
  53. package/lib/output.capture.d.ts +24 -0
  54. package/lib/parse.keypress-kitty.d.ts +2 -0
  55. package/lib/parse.keypress.d.ts +26 -0
  56. package/lib/parse.mouse.d.ts +30 -0
  57. package/lib/paste.d.ts +7 -0
  58. package/lib/queue.d.ts +15 -0
  59. package/lib/renderable.validations.d.ts +12 -0
  60. package/lib/scroll-acceleration.d.ts +43 -0
  61. package/lib/selection.d.ts +63 -0
  62. package/lib/singleton.d.ts +7 -0
  63. package/lib/stdin-parser.d.ts +87 -0
  64. package/lib/styled-text.d.ts +63 -0
  65. package/lib/terminal-capability-detection.d.ts +30 -0
  66. package/lib/terminal-palette.d.ts +50 -0
  67. package/lib/tree-sitter/assets/update.d.ts +11 -0
  68. package/lib/tree-sitter/client.d.ts +47 -0
  69. package/lib/tree-sitter/default-parsers.d.ts +2 -0
  70. package/lib/tree-sitter/download-utils.d.ts +21 -0
  71. package/lib/tree-sitter/index.d.ts +8 -0
  72. package/lib/tree-sitter/parser.worker.d.ts +1 -0
  73. package/lib/tree-sitter/parsers-config.d.ts +53 -0
  74. package/lib/tree-sitter/resolve-ft.d.ts +5 -0
  75. package/lib/tree-sitter/types.d.ts +82 -0
  76. package/lib/tree-sitter-styled-text.d.ts +14 -0
  77. package/lib/validate-dir-name.d.ts +1 -0
  78. package/lib/yoga.options.d.ts +32 -0
  79. package/package.json +50 -62
  80. package/parser.worker.js +899 -0
  81. package/parser.worker.js.map +12 -0
  82. package/plugins/core-slot.d.ts +72 -0
  83. package/plugins/registry.d.ts +42 -0
  84. package/plugins/types.d.ts +34 -0
  85. package/post/effects.d.ts +147 -0
  86. package/post/filters.d.ts +65 -0
  87. package/post/matrices.d.ts +20 -0
  88. package/renderables/ASCIIFont.d.ts +52 -0
  89. package/renderables/Box.d.ts +81 -0
  90. package/renderables/Code.d.ts +78 -0
  91. package/renderables/Diff.d.ts +142 -0
  92. package/renderables/EditBufferRenderable.d.ts +237 -0
  93. package/renderables/FrameBuffer.d.ts +16 -0
  94. package/renderables/Input.d.ts +67 -0
  95. package/renderables/LineNumberRenderable.d.ts +78 -0
  96. package/renderables/Markdown.d.ts +185 -0
  97. package/renderables/ScrollBar.d.ts +77 -0
  98. package/renderables/ScrollBox.d.ts +124 -0
  99. package/renderables/Select.d.ts +115 -0
  100. package/renderables/Slider.d.ts +47 -0
  101. package/renderables/TabSelect.d.ts +96 -0
  102. package/renderables/Text.d.ts +36 -0
  103. package/renderables/TextBufferRenderable.d.ts +105 -0
  104. package/renderables/TextNode.d.ts +91 -0
  105. package/renderables/TextTable.d.ts +140 -0
  106. package/renderables/Textarea.d.ts +63 -0
  107. package/renderables/TimeToFirstDraw.d.ts +24 -0
  108. package/renderables/__tests__/renderable-test-utils.d.ts +12 -0
  109. package/renderables/composition/VRenderable.d.ts +16 -0
  110. package/renderables/composition/constructs.d.ts +35 -0
  111. package/renderables/composition/vnode.d.ts +46 -0
  112. package/renderables/index.d.ts +23 -0
  113. package/renderables/markdown-parser.d.ts +10 -0
  114. package/renderer.d.ts +419 -0
  115. package/runtime-plugin-support.d.ts +3 -0
  116. package/runtime-plugin-support.js +29 -0
  117. package/runtime-plugin-support.js.map +10 -0
  118. package/runtime-plugin.d.ts +16 -0
  119. package/runtime-plugin.js +16 -0
  120. package/runtime-plugin.js.map +9 -0
  121. package/syntax-style.d.ts +54 -0
  122. package/testing/manual-clock.d.ts +17 -0
  123. package/testing/mock-keys.d.ts +81 -0
  124. package/testing/mock-mouse.d.ts +38 -0
  125. package/testing/mock-tree-sitter-client.d.ts +23 -0
  126. package/testing/spy.d.ts +7 -0
  127. package/testing/test-recorder.d.ts +61 -0
  128. package/testing/test-renderer.d.ts +23 -0
  129. package/testing.d.ts +6 -0
  130. package/testing.js +697 -0
  131. package/testing.js.map +15 -0
  132. package/text-buffer-view.d.ts +42 -0
  133. package/text-buffer.d.ts +67 -0
  134. package/types.d.ts +139 -0
  135. package/utils.d.ts +14 -0
  136. package/zig-structs.d.ts +155 -0
  137. package/zig.d.ts +353 -0
  138. package/dev/keypress-debug-renderer.ts +0 -148
  139. package/dev/keypress-debug.ts +0 -43
  140. package/dev/print-env-vars.ts +0 -32
  141. package/dev/test-tmux-graphics-334.sh +0 -68
  142. package/dev/thai-debug-test.ts +0 -68
  143. package/docs/development.md +0 -144
  144. package/scripts/build.ts +0 -400
  145. package/scripts/publish.ts +0 -60
  146. package/src/3d/SpriteResourceManager.ts +0 -286
  147. package/src/3d/SpriteUtils.ts +0 -70
  148. package/src/3d/TextureUtils.ts +0 -196
  149. package/src/3d/ThreeRenderable.ts +0 -197
  150. package/src/3d/WGPURenderer.ts +0 -294
  151. package/src/3d/animation/ExplodingSpriteEffect.ts +0 -513
  152. package/src/3d/animation/PhysicsExplodingSpriteEffect.ts +0 -429
  153. package/src/3d/animation/SpriteAnimator.ts +0 -633
  154. package/src/3d/animation/SpriteParticleGenerator.ts +0 -435
  155. package/src/3d/canvas.ts +0 -464
  156. package/src/3d/index.ts +0 -12
  157. package/src/3d/physics/PlanckPhysicsAdapter.ts +0 -72
  158. package/src/3d/physics/RapierPhysicsAdapter.ts +0 -66
  159. package/src/3d/physics/physics-interface.ts +0 -31
  160. package/src/3d/shaders/supersampling.wgsl +0 -201
  161. package/src/3d.ts +0 -3
  162. package/src/NativeSpanFeed.ts +0 -300
  163. package/src/Renderable.ts +0 -1704
  164. package/src/__snapshots__/buffer.test.ts.snap +0 -28
  165. package/src/animation/Timeline.test.ts +0 -2709
  166. package/src/animation/Timeline.ts +0 -598
  167. package/src/ansi.ts +0 -18
  168. package/src/benchmark/attenuation-benchmark.ts +0 -81
  169. package/src/benchmark/colormatrix-benchmark.ts +0 -128
  170. package/src/benchmark/gain-benchmark.ts +0 -80
  171. package/src/benchmark/latest-all-bench-run.json +0 -707
  172. package/src/benchmark/latest-async-bench-run.json +0 -336
  173. package/src/benchmark/latest-default-bench-run.json +0 -657
  174. package/src/benchmark/latest-large-bench-run.json +0 -707
  175. package/src/benchmark/latest-quick-bench-run.json +0 -207
  176. package/src/benchmark/markdown-benchmark.ts +0 -1796
  177. package/src/benchmark/native-span-feed-async-benchmark.ts +0 -355
  178. package/src/benchmark/native-span-feed-benchmark.md +0 -56
  179. package/src/benchmark/native-span-feed-benchmark.ts +0 -596
  180. package/src/benchmark/native-span-feed-compare.ts +0 -280
  181. package/src/benchmark/renderer-benchmark.ts +0 -754
  182. package/src/benchmark/text-table-benchmark.ts +0 -948
  183. package/src/buffer.test.ts +0 -291
  184. package/src/buffer.ts +0 -554
  185. package/src/console.test.ts +0 -612
  186. package/src/console.ts +0 -1254
  187. package/src/edit-buffer.test.ts +0 -1769
  188. package/src/edit-buffer.ts +0 -411
  189. package/src/editor-view.test.ts +0 -1032
  190. package/src/editor-view.ts +0 -284
  191. package/src/examples/ascii-font-selection-demo.ts +0 -245
  192. package/src/examples/assets/Water_2_M_Normal.jpg +0 -0
  193. package/src/examples/assets/concrete.png +0 -0
  194. package/src/examples/assets/crate.png +0 -0
  195. package/src/examples/assets/crate_emissive.png +0 -0
  196. package/src/examples/assets/forrest_background.png +0 -0
  197. package/src/examples/assets/hast-example.json +0 -1018
  198. package/src/examples/assets/heart.png +0 -0
  199. package/src/examples/assets/main_char_heavy_attack.png +0 -0
  200. package/src/examples/assets/main_char_idle.png +0 -0
  201. package/src/examples/assets/main_char_jump_end.png +0 -0
  202. package/src/examples/assets/main_char_jump_landing.png +0 -0
  203. package/src/examples/assets/main_char_jump_start.png +0 -0
  204. package/src/examples/assets/main_char_run_loop.png +0 -0
  205. package/src/examples/assets/roughness_map.jpg +0 -0
  206. package/src/examples/build.ts +0 -115
  207. package/src/examples/code-demo.ts +0 -924
  208. package/src/examples/console-demo.ts +0 -358
  209. package/src/examples/core-plugin-slots-demo.ts +0 -759
  210. package/src/examples/diff-demo.ts +0 -701
  211. package/src/examples/draggable-three-demo.ts +0 -259
  212. package/src/examples/editor-demo.ts +0 -322
  213. package/src/examples/extmarks-demo.ts +0 -196
  214. package/src/examples/focus-restore-demo.ts +0 -310
  215. package/src/examples/fonts.ts +0 -245
  216. package/src/examples/fractal-shader-demo.ts +0 -268
  217. package/src/examples/framebuffer-demo.ts +0 -674
  218. package/src/examples/full-unicode-demo.ts +0 -241
  219. package/src/examples/golden-star-demo.ts +0 -933
  220. package/src/examples/grayscale-buffer-demo.ts +0 -249
  221. package/src/examples/hast-syntax-highlighting-demo.ts +0 -129
  222. package/src/examples/index.ts +0 -926
  223. package/src/examples/input-demo.ts +0 -377
  224. package/src/examples/input-select-layout-demo.ts +0 -425
  225. package/src/examples/install.sh +0 -143
  226. package/src/examples/keypress-debug-demo.ts +0 -452
  227. package/src/examples/lib/HexList.ts +0 -122
  228. package/src/examples/lib/PaletteGrid.ts +0 -125
  229. package/src/examples/lib/standalone-keys.ts +0 -25
  230. package/src/examples/lib/tab-controller.ts +0 -243
  231. package/src/examples/lights-phong-demo.ts +0 -290
  232. package/src/examples/link-demo.ts +0 -220
  233. package/src/examples/live-state-demo.ts +0 -480
  234. package/src/examples/markdown-demo.ts +0 -725
  235. package/src/examples/mouse-interaction-demo.ts +0 -428
  236. package/src/examples/nested-zindex-demo.ts +0 -357
  237. package/src/examples/opacity-example.ts +0 -235
  238. package/src/examples/opentui-demo.ts +0 -1057
  239. package/src/examples/physx-planck-2d-demo.ts +0 -623
  240. package/src/examples/physx-rapier-2d-demo.ts +0 -655
  241. package/src/examples/relative-positioning-demo.ts +0 -323
  242. package/src/examples/scroll-example.ts +0 -214
  243. package/src/examples/scrollbox-mouse-test.ts +0 -112
  244. package/src/examples/scrollbox-overlay-hit-test.ts +0 -206
  245. package/src/examples/select-demo.ts +0 -237
  246. package/src/examples/shader-cube-demo.ts +0 -1015
  247. package/src/examples/simple-layout-example.ts +0 -591
  248. package/src/examples/slider-demo.ts +0 -617
  249. package/src/examples/split-mode-demo.ts +0 -453
  250. package/src/examples/sprite-animation-demo.ts +0 -443
  251. package/src/examples/sprite-particle-generator-demo.ts +0 -486
  252. package/src/examples/static-sprite-demo.ts +0 -193
  253. package/src/examples/sticky-scroll-example.ts +0 -308
  254. package/src/examples/styled-text-demo.ts +0 -282
  255. package/src/examples/tab-select-demo.ts +0 -219
  256. package/src/examples/terminal-title.ts +0 -29
  257. package/src/examples/terminal.ts +0 -305
  258. package/src/examples/text-node-demo.ts +0 -416
  259. package/src/examples/text-selection-demo.ts +0 -377
  260. package/src/examples/text-table-demo.ts +0 -503
  261. package/src/examples/text-truncation-demo.ts +0 -481
  262. package/src/examples/text-wrap.ts +0 -757
  263. package/src/examples/texture-loading-demo.ts +0 -259
  264. package/src/examples/timeline-example.ts +0 -670
  265. package/src/examples/transparency-demo.ts +0 -400
  266. package/src/examples/vnode-composition-demo.ts +0 -404
  267. package/src/examples/wide-grapheme-overlay-demo.ts +0 -280
  268. package/src/index.ts +0 -24
  269. package/src/lib/KeyHandler.integration.test.ts +0 -292
  270. package/src/lib/KeyHandler.stopPropagation.test.ts +0 -289
  271. package/src/lib/KeyHandler.test.ts +0 -662
  272. package/src/lib/KeyHandler.ts +0 -222
  273. package/src/lib/RGBA.test.ts +0 -984
  274. package/src/lib/RGBA.ts +0 -204
  275. package/src/lib/ascii.font.ts +0 -330
  276. package/src/lib/border.test.ts +0 -83
  277. package/src/lib/border.ts +0 -170
  278. package/src/lib/bunfs.test.ts +0 -27
  279. package/src/lib/bunfs.ts +0 -18
  280. package/src/lib/clipboard.test.ts +0 -41
  281. package/src/lib/clipboard.ts +0 -47
  282. package/src/lib/clock.ts +0 -35
  283. package/src/lib/data-paths.test.ts +0 -133
  284. package/src/lib/data-paths.ts +0 -109
  285. package/src/lib/debounce.ts +0 -106
  286. package/src/lib/detect-links.test.ts +0 -98
  287. package/src/lib/detect-links.ts +0 -56
  288. package/src/lib/env.test.ts +0 -228
  289. package/src/lib/env.ts +0 -209
  290. package/src/lib/extmarks-history.ts +0 -51
  291. package/src/lib/extmarks-multiwidth.test.ts +0 -322
  292. package/src/lib/extmarks.test.ts +0 -3457
  293. package/src/lib/extmarks.ts +0 -843
  294. package/src/lib/fonts/block.json +0 -405
  295. package/src/lib/fonts/grid.json +0 -265
  296. package/src/lib/fonts/huge.json +0 -741
  297. package/src/lib/fonts/pallet.json +0 -314
  298. package/src/lib/fonts/shade.json +0 -591
  299. package/src/lib/fonts/slick.json +0 -321
  300. package/src/lib/fonts/tiny.json +0 -69
  301. package/src/lib/hast-styled-text.ts +0 -59
  302. package/src/lib/index.ts +0 -21
  303. package/src/lib/keymapping.test.ts +0 -317
  304. package/src/lib/keymapping.ts +0 -115
  305. package/src/lib/objects-in-viewport.test.ts +0 -787
  306. package/src/lib/objects-in-viewport.ts +0 -153
  307. package/src/lib/output.capture.ts +0 -58
  308. package/src/lib/parse.keypress-kitty.protocol.test.ts +0 -340
  309. package/src/lib/parse.keypress-kitty.test.ts +0 -663
  310. package/src/lib/parse.keypress-kitty.ts +0 -439
  311. package/src/lib/parse.keypress.test.ts +0 -1849
  312. package/src/lib/parse.keypress.ts +0 -397
  313. package/src/lib/parse.mouse.test.ts +0 -552
  314. package/src/lib/parse.mouse.ts +0 -232
  315. package/src/lib/paste.ts +0 -16
  316. package/src/lib/queue.ts +0 -65
  317. package/src/lib/renderable.validations.test.ts +0 -87
  318. package/src/lib/renderable.validations.ts +0 -83
  319. package/src/lib/scroll-acceleration.ts +0 -98
  320. package/src/lib/selection.ts +0 -240
  321. package/src/lib/singleton.ts +0 -28
  322. package/src/lib/stdin-parser.test.ts +0 -2290
  323. package/src/lib/stdin-parser.ts +0 -1810
  324. package/src/lib/styled-text.ts +0 -178
  325. package/src/lib/terminal-capability-detection.test.ts +0 -202
  326. package/src/lib/terminal-capability-detection.ts +0 -79
  327. package/src/lib/terminal-palette.test.ts +0 -878
  328. package/src/lib/terminal-palette.ts +0 -383
  329. package/src/lib/tree-sitter/assets/README.md +0 -118
  330. package/src/lib/tree-sitter/assets/update.ts +0 -334
  331. package/src/lib/tree-sitter/assets.d.ts +0 -9
  332. package/src/lib/tree-sitter/cache.test.ts +0 -273
  333. package/src/lib/tree-sitter/client.test.ts +0 -1165
  334. package/src/lib/tree-sitter/client.ts +0 -607
  335. package/src/lib/tree-sitter/default-parsers.ts +0 -86
  336. package/src/lib/tree-sitter/download-utils.ts +0 -148
  337. package/src/lib/tree-sitter/index.ts +0 -28
  338. package/src/lib/tree-sitter/parser.worker.ts +0 -1042
  339. package/src/lib/tree-sitter/parsers-config.ts +0 -81
  340. package/src/lib/tree-sitter/resolve-ft.test.ts +0 -55
  341. package/src/lib/tree-sitter/resolve-ft.ts +0 -189
  342. package/src/lib/tree-sitter/types.ts +0 -82
  343. package/src/lib/tree-sitter-styled-text.test.ts +0 -1253
  344. package/src/lib/tree-sitter-styled-text.ts +0 -306
  345. package/src/lib/validate-dir-name.ts +0 -55
  346. package/src/lib/yoga.options.test.ts +0 -628
  347. package/src/lib/yoga.options.ts +0 -346
  348. package/src/plugins/core-slot.ts +0 -579
  349. package/src/plugins/registry.ts +0 -402
  350. package/src/plugins/types.ts +0 -46
  351. package/src/post/effects.ts +0 -930
  352. package/src/post/filters.ts +0 -489
  353. package/src/post/matrices.ts +0 -288
  354. package/src/renderables/ASCIIFont.ts +0 -219
  355. package/src/renderables/Box.test.ts +0 -205
  356. package/src/renderables/Box.ts +0 -326
  357. package/src/renderables/Code.test.ts +0 -2062
  358. package/src/renderables/Code.ts +0 -357
  359. package/src/renderables/Diff.regression.test.ts +0 -226
  360. package/src/renderables/Diff.test.ts +0 -3101
  361. package/src/renderables/Diff.ts +0 -1211
  362. package/src/renderables/EditBufferRenderable.test.ts +0 -288
  363. package/src/renderables/EditBufferRenderable.ts +0 -1166
  364. package/src/renderables/FrameBuffer.ts +0 -47
  365. package/src/renderables/Input.test.ts +0 -1228
  366. package/src/renderables/Input.ts +0 -247
  367. package/src/renderables/LineNumberRenderable.ts +0 -724
  368. package/src/renderables/Markdown.ts +0 -1393
  369. package/src/renderables/ScrollBar.ts +0 -422
  370. package/src/renderables/ScrollBox.ts +0 -883
  371. package/src/renderables/Select.test.ts +0 -1033
  372. package/src/renderables/Select.ts +0 -524
  373. package/src/renderables/Slider.test.ts +0 -456
  374. package/src/renderables/Slider.ts +0 -342
  375. package/src/renderables/TabSelect.test.ts +0 -197
  376. package/src/renderables/TabSelect.ts +0 -455
  377. package/src/renderables/Text.selection-buffer.test.ts +0 -123
  378. package/src/renderables/Text.test.ts +0 -2660
  379. package/src/renderables/Text.ts +0 -147
  380. package/src/renderables/TextBufferRenderable.ts +0 -518
  381. package/src/renderables/TextNode.test.ts +0 -1058
  382. package/src/renderables/TextNode.ts +0 -325
  383. package/src/renderables/TextTable.test.ts +0 -1421
  384. package/src/renderables/TextTable.ts +0 -1344
  385. package/src/renderables/Textarea.ts +0 -430
  386. package/src/renderables/TimeToFirstDraw.ts +0 -89
  387. package/src/renderables/__snapshots__/Code.test.ts.snap +0 -13
  388. package/src/renderables/__snapshots__/Diff.test.ts.snap +0 -785
  389. package/src/renderables/__snapshots__/Text.test.ts.snap +0 -421
  390. package/src/renderables/__snapshots__/TextTable.test.ts.snap +0 -215
  391. package/src/renderables/__tests__/LineNumberRenderable.scrollbox-simple.test.ts +0 -144
  392. package/src/renderables/__tests__/LineNumberRenderable.scrollbox.test.ts +0 -816
  393. package/src/renderables/__tests__/LineNumberRenderable.test.ts +0 -1865
  394. package/src/renderables/__tests__/LineNumberRenderable.wrapping.test.ts +0 -85
  395. package/src/renderables/__tests__/Markdown.code-colors.test.ts +0 -242
  396. package/src/renderables/__tests__/Markdown.test.ts +0 -2518
  397. package/src/renderables/__tests__/MultiRenderable.selection.test.ts +0 -87
  398. package/src/renderables/__tests__/Textarea.buffer.test.ts +0 -682
  399. package/src/renderables/__tests__/Textarea.destroyed-events.test.ts +0 -675
  400. package/src/renderables/__tests__/Textarea.editing.test.ts +0 -2041
  401. package/src/renderables/__tests__/Textarea.error-handling.test.ts +0 -35
  402. package/src/renderables/__tests__/Textarea.events.test.ts +0 -738
  403. package/src/renderables/__tests__/Textarea.highlights.test.ts +0 -590
  404. package/src/renderables/__tests__/Textarea.keybinding.test.ts +0 -3149
  405. package/src/renderables/__tests__/Textarea.paste.test.ts +0 -357
  406. package/src/renderables/__tests__/Textarea.rendering.test.ts +0 -1866
  407. package/src/renderables/__tests__/Textarea.scroll.test.ts +0 -733
  408. package/src/renderables/__tests__/Textarea.selection.test.ts +0 -1590
  409. package/src/renderables/__tests__/Textarea.stress.test.ts +0 -670
  410. package/src/renderables/__tests__/Textarea.undo-redo.test.ts +0 -383
  411. package/src/renderables/__tests__/Textarea.visual-lines.test.ts +0 -310
  412. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.code.test.ts.snap +0 -221
  413. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox-simple.test.ts.snap +0 -89
  414. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox.test.ts.snap +0 -457
  415. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.test.ts.snap +0 -158
  416. package/src/renderables/__tests__/__snapshots__/Textarea.rendering.test.ts.snap +0 -387
  417. package/src/renderables/__tests__/markdown-parser.test.ts +0 -217
  418. package/src/renderables/__tests__/renderable-test-utils.ts +0 -60
  419. package/src/renderables/composition/README.md +0 -8
  420. package/src/renderables/composition/VRenderable.ts +0 -32
  421. package/src/renderables/composition/constructs.ts +0 -127
  422. package/src/renderables/composition/vnode.ts +0 -289
  423. package/src/renderables/index.ts +0 -23
  424. package/src/renderables/markdown-parser.ts +0 -66
  425. package/src/renderer.ts +0 -2681
  426. package/src/runtime-plugin-support.ts +0 -39
  427. package/src/runtime-plugin.ts +0 -615
  428. package/src/syntax-style.test.ts +0 -841
  429. package/src/syntax-style.ts +0 -257
  430. package/src/testing/README.md +0 -210
  431. package/src/testing/capture-spans.test.ts +0 -194
  432. package/src/testing/integration.test.ts +0 -276
  433. package/src/testing/manual-clock.ts +0 -117
  434. package/src/testing/mock-keys.test.ts +0 -1378
  435. package/src/testing/mock-keys.ts +0 -457
  436. package/src/testing/mock-mouse.test.ts +0 -218
  437. package/src/testing/mock-mouse.ts +0 -247
  438. package/src/testing/mock-tree-sitter-client.ts +0 -73
  439. package/src/testing/spy.ts +0 -13
  440. package/src/testing/test-recorder.test.ts +0 -415
  441. package/src/testing/test-recorder.ts +0 -145
  442. package/src/testing/test-renderer.ts +0 -132
  443. package/src/testing.ts +0 -7
  444. package/src/tests/__snapshots__/absolute-positioning.snapshot.test.ts.snap +0 -481
  445. package/src/tests/__snapshots__/renderable.snapshot.test.ts.snap +0 -19
  446. package/src/tests/__snapshots__/scrollbox.test.ts.snap +0 -29
  447. package/src/tests/absolute-positioning.snapshot.test.ts +0 -638
  448. package/src/tests/allocator-stats.test.ts +0 -38
  449. package/src/tests/destroy-during-render.test.ts +0 -200
  450. package/src/tests/destroy-on-exit.fixture.ts +0 -36
  451. package/src/tests/destroy-on-exit.test.ts +0 -41
  452. package/src/tests/hover-cursor.test.ts +0 -98
  453. package/src/tests/native-span-feed-async.test.ts +0 -173
  454. package/src/tests/native-span-feed-close.test.ts +0 -120
  455. package/src/tests/native-span-feed-coverage.test.ts +0 -227
  456. package/src/tests/native-span-feed-edge-cases.test.ts +0 -352
  457. package/src/tests/native-span-feed-use-after-free.test.ts +0 -45
  458. package/src/tests/opacity.test.ts +0 -123
  459. package/src/tests/renderable.snapshot.test.ts +0 -524
  460. package/src/tests/renderable.test.ts +0 -1281
  461. package/src/tests/renderer.clock.test.ts +0 -158
  462. package/src/tests/renderer.console-startup.test.ts +0 -185
  463. package/src/tests/renderer.control.test.ts +0 -425
  464. package/src/tests/renderer.core-slot-binding.test.ts +0 -952
  465. package/src/tests/renderer.cursor.test.ts +0 -26
  466. package/src/tests/renderer.destroy-during-render.test.ts +0 -147
  467. package/src/tests/renderer.focus-restore.test.ts +0 -257
  468. package/src/tests/renderer.focus.test.ts +0 -294
  469. package/src/tests/renderer.idle.test.ts +0 -219
  470. package/src/tests/renderer.input.test.ts +0 -2237
  471. package/src/tests/renderer.kitty-flags.test.ts +0 -195
  472. package/src/tests/renderer.mouse.test.ts +0 -1274
  473. package/src/tests/renderer.palette.test.ts +0 -629
  474. package/src/tests/renderer.selection.test.ts +0 -49
  475. package/src/tests/renderer.slot-registry.test.ts +0 -684
  476. package/src/tests/renderer.useMouse.test.ts +0 -47
  477. package/src/tests/runtime-plugin-node-modules-cycle.fixture.ts +0 -76
  478. package/src/tests/runtime-plugin-node-modules-mjs.fixture.ts +0 -43
  479. package/src/tests/runtime-plugin-node-modules-no-bare-rewrite.fixture.ts +0 -67
  480. package/src/tests/runtime-plugin-node-modules-package-type-cache.fixture.ts +0 -72
  481. package/src/tests/runtime-plugin-node-modules-runtime-specifier.fixture.ts +0 -44
  482. package/src/tests/runtime-plugin-node-modules-scoped-package-bare-rewrite.fixture.ts +0 -85
  483. package/src/tests/runtime-plugin-path-alias.fixture.ts +0 -43
  484. package/src/tests/runtime-plugin-resolve-roots.fixture.ts +0 -65
  485. package/src/tests/runtime-plugin-support.fixture.ts +0 -11
  486. package/src/tests/runtime-plugin-support.test.ts +0 -19
  487. package/src/tests/runtime-plugin-windows-file-url.fixture.ts +0 -30
  488. package/src/tests/runtime-plugin.fixture.ts +0 -40
  489. package/src/tests/runtime-plugin.test.ts +0 -354
  490. package/src/tests/scrollbox-culling-bug.test.ts +0 -114
  491. package/src/tests/scrollbox-hitgrid-resize.test.ts +0 -136
  492. package/src/tests/scrollbox-hitgrid.test.ts +0 -909
  493. package/src/tests/scrollbox.test.ts +0 -1530
  494. package/src/tests/wrap-resize-perf.test.ts +0 -276
  495. package/src/tests/yoga-setters.test.ts +0 -921
  496. package/src/text-buffer-view.test.ts +0 -705
  497. package/src/text-buffer-view.ts +0 -189
  498. package/src/text-buffer.test.ts +0 -347
  499. package/src/text-buffer.ts +0 -250
  500. package/src/types.ts +0 -161
  501. package/src/utils.ts +0 -88
  502. package/src/zig/ansi.zig +0 -268
  503. package/src/zig/bench/README.md +0 -50
  504. package/src/zig/bench/buffer-draw-text-buffer_bench.zig +0 -887
  505. package/src/zig/bench/edit-buffer_bench.zig +0 -476
  506. package/src/zig/bench/native-span-feed_bench.zig +0 -100
  507. package/src/zig/bench/rope-markers_bench.zig +0 -713
  508. package/src/zig/bench/rope_bench.zig +0 -514
  509. package/src/zig/bench/styled-text_bench.zig +0 -470
  510. package/src/zig/bench/text-buffer-coords_bench.zig +0 -362
  511. package/src/zig/bench/text-buffer-view_bench.zig +0 -459
  512. package/src/zig/bench/text-chunk-graphemes_bench.zig +0 -273
  513. package/src/zig/bench/utf8_bench.zig +0 -799
  514. package/src/zig/bench-utils.zig +0 -431
  515. package/src/zig/bench.zig +0 -217
  516. package/src/zig/buffer-methods.zig +0 -211
  517. package/src/zig/buffer.zig +0 -2281
  518. package/src/zig/build.zig +0 -289
  519. package/src/zig/build.zig.zon +0 -16
  520. package/src/zig/edit-buffer.zig +0 -825
  521. package/src/zig/editor-view.zig +0 -802
  522. package/src/zig/event-bus.zig +0 -13
  523. package/src/zig/event-emitter.zig +0 -65
  524. package/src/zig/file-logger.zig +0 -92
  525. package/src/zig/grapheme.zig +0 -599
  526. package/src/zig/lib.zig +0 -1854
  527. package/src/zig/link.zig +0 -333
  528. package/src/zig/logger.zig +0 -43
  529. package/src/zig/mem-registry.zig +0 -125
  530. package/src/zig/native-span-feed-bench-lib.zig +0 -7
  531. package/src/zig/native-span-feed.zig +0 -708
  532. package/src/zig/renderer.zig +0 -1393
  533. package/src/zig/rope.zig +0 -1220
  534. package/src/zig/syntax-style.zig +0 -161
  535. package/src/zig/terminal.zig +0 -987
  536. package/src/zig/test.zig +0 -72
  537. package/src/zig/tests/README.md +0 -18
  538. package/src/zig/tests/buffer-methods_test.zig +0 -1109
  539. package/src/zig/tests/buffer_test.zig +0 -2557
  540. package/src/zig/tests/edit-buffer-history_test.zig +0 -271
  541. package/src/zig/tests/edit-buffer_test.zig +0 -1689
  542. package/src/zig/tests/editor-view_test.zig +0 -3299
  543. package/src/zig/tests/event-emitter_test.zig +0 -249
  544. package/src/zig/tests/grapheme_test.zig +0 -1304
  545. package/src/zig/tests/link_test.zig +0 -190
  546. package/src/zig/tests/mem-registry_test.zig +0 -473
  547. package/src/zig/tests/memory_leak_regression_test.zig +0 -159
  548. package/src/zig/tests/native-span-feed_test.zig +0 -1264
  549. package/src/zig/tests/renderer_test.zig +0 -1017
  550. package/src/zig/tests/rope-nested_test.zig +0 -712
  551. package/src/zig/tests/rope_fuzz_test.zig +0 -238
  552. package/src/zig/tests/rope_test.zig +0 -2362
  553. package/src/zig/tests/segment-merge.test.zig +0 -148
  554. package/src/zig/tests/syntax-style_test.zig +0 -557
  555. package/src/zig/tests/terminal_test.zig +0 -754
  556. package/src/zig/tests/text-buffer-drawing_test.zig +0 -3237
  557. package/src/zig/tests/text-buffer-highlights_test.zig +0 -666
  558. package/src/zig/tests/text-buffer-iterators_test.zig +0 -776
  559. package/src/zig/tests/text-buffer-segment_test.zig +0 -320
  560. package/src/zig/tests/text-buffer-selection_test.zig +0 -1035
  561. package/src/zig/tests/text-buffer-selection_viewport_test.zig +0 -358
  562. package/src/zig/tests/text-buffer-view_test.zig +0 -3649
  563. package/src/zig/tests/text-buffer_test.zig +0 -2191
  564. package/src/zig/tests/unicode-width-map.zon +0 -3909
  565. package/src/zig/tests/utf8_no_zwj_test.zig +0 -260
  566. package/src/zig/tests/utf8_test.zig +0 -4057
  567. package/src/zig/tests/utf8_wcwidth_cursor_test.zig +0 -267
  568. package/src/zig/tests/utf8_wcwidth_test.zig +0 -357
  569. package/src/zig/tests/word-wrap-editing_test.zig +0 -498
  570. package/src/zig/tests/wrap-cache-perf_test.zig +0 -113
  571. package/src/zig/text-buffer-iterators.zig +0 -499
  572. package/src/zig/text-buffer-segment.zig +0 -404
  573. package/src/zig/text-buffer-view.zig +0 -1371
  574. package/src/zig/text-buffer.zig +0 -1180
  575. package/src/zig/utf8.zig +0 -1948
  576. package/src/zig/utils.zig +0 -9
  577. package/src/zig-structs.ts +0 -261
  578. package/src/zig.ts +0 -3884
  579. package/tsconfig.build.json +0 -24
  580. package/tsconfig.json +0 -27
  581. /package/{src/lib/tree-sitter/assets → assets}/javascript/highlights.scm +0 -0
  582. /package/{src/lib/tree-sitter/assets → assets}/javascript/tree-sitter-javascript.wasm +0 -0
  583. /package/{src/lib/tree-sitter/assets → assets}/markdown/highlights.scm +0 -0
  584. /package/{src/lib/tree-sitter/assets → assets}/markdown/injections.scm +0 -0
  585. /package/{src/lib/tree-sitter/assets → assets}/markdown/tree-sitter-markdown.wasm +0 -0
  586. /package/{src/lib/tree-sitter/assets → assets}/markdown_inline/highlights.scm +0 -0
  587. /package/{src/lib/tree-sitter/assets → assets}/markdown_inline/tree-sitter-markdown_inline.wasm +0 -0
  588. /package/{src/lib/tree-sitter/assets → assets}/typescript/highlights.scm +0 -0
  589. /package/{src/lib/tree-sitter/assets → assets}/typescript/tree-sitter-typescript.wasm +0 -0
  590. /package/{src/lib/tree-sitter/assets → assets}/zig/highlights.scm +0 -0
  591. /package/{src/lib/tree-sitter/assets → assets}/zig/tree-sitter-zig.wasm +0 -0
package/src/zig/rope.zig DELETED
@@ -1,1220 +0,0 @@
1
- const std = @import("std");
2
- const Allocator = std.mem.Allocator;
3
-
4
- /// This is a persistent/immutable rope - operations create new nodes without
5
- /// freeing old ones. Use an ArenaAllocator to avoid manual memory management:
6
- ///
7
- /// var arena = std.heap.ArenaAllocator.init(allocator);
8
- /// defer arena.deinit();
9
- /// var rope = try Rope(T).init(arena.allocator());
10
- ///
11
- /// TODO: Needs a startTransaction and endTransaction to track changes
12
- /// -> used to trigger a change event _after_ a batch of changes is complete (group as operation)
13
- /// -> used to group history operations (undo/redo), so not everything is a single history entry
14
- ///
15
- pub fn Rope(comptime T: type) type {
16
- return struct {
17
- const Self = @This();
18
-
19
- pub const max_imbalance = 7;
20
-
21
- pub const Config = struct {
22
- max_undo_depth: ?usize = null, // null = unlimited
23
- };
24
-
25
- const marker_enabled = @typeInfo(T) == .@"union" and @hasDecl(T, "MarkerTypes");
26
- const MarkerTagCount = if (marker_enabled) T.MarkerTypes.len else 0;
27
-
28
- const boundary_enabled = @hasDecl(T, "BoundaryAction");
29
- pub const BoundaryAction = if (boundary_enabled) T.BoundaryAction else struct {
30
- delete_left: bool = false,
31
- delete_right: bool = false,
32
- insert_between: []const T = &[_]T{},
33
- };
34
-
35
- pub const MarkerPosition = struct {
36
- leaf_index: u32,
37
- global_weight: u32,
38
- };
39
- pub const MarkerCache = if (marker_enabled) struct {
40
- // Flat arrays of positions for each marker type
41
- positions: std.AutoHashMap(std.meta.Tag(T), std.ArrayListUnmanaged(MarkerPosition)),
42
- version: u64, // Rope version when cache was built
43
- allocator: Allocator,
44
-
45
- pub fn init(allocator: Allocator) MarkerCache {
46
- return .{
47
- .positions = std.AutoHashMap(std.meta.Tag(T), std.ArrayListUnmanaged(MarkerPosition)).init(allocator),
48
- .version = std.math.maxInt(u64), // Sentinel: cache is invalid until first rebuild
49
- .allocator = allocator,
50
- };
51
- }
52
-
53
- pub fn deinit(self: *MarkerCache) void {
54
- var iter = self.positions.valueIterator();
55
- while (iter.next()) |list| {
56
- list.deinit(self.allocator);
57
- }
58
- self.positions.deinit();
59
- }
60
-
61
- fn clear(self: *MarkerCache) void {
62
- var iter = self.positions.valueIterator();
63
- while (iter.next()) |list| {
64
- list.clearRetainingCapacity();
65
- }
66
- }
67
- } else struct {
68
- pub fn init(_: Allocator) @This() {
69
- return .{};
70
- }
71
- pub fn deinit(_: *@This()) void {}
72
- fn clear(_: *@This()) void {}
73
- };
74
-
75
- pub const Metrics = struct {
76
- count: u32 = 0,
77
- depth: u32 = 1,
78
- custom: if (@hasDecl(T, "Metrics")) T.Metrics else void = if (@hasDecl(T, "Metrics")) .{} else {},
79
-
80
- marker_counts: if (marker_enabled) [MarkerTagCount]u32 else void = if (marker_enabled) [_]u32{0} ** MarkerTagCount else {},
81
-
82
- pub fn add(self: *Metrics, other: Metrics) void {
83
- self.count += other.count;
84
- self.depth = @max(self.depth, other.depth);
85
-
86
- if (@hasDecl(T, "Metrics")) {
87
- if (@hasDecl(T.Metrics, "add")) {
88
- self.custom.add(other.custom);
89
- }
90
- }
91
-
92
- if (marker_enabled) {
93
- inline for (&self.marker_counts, 0..) |*dst, i| {
94
- dst.* += other.marker_counts[i];
95
- }
96
- }
97
- }
98
-
99
- pub fn weight(self: *const Metrics) u32 {
100
- if (@hasDecl(T, "Metrics")) {
101
- if (@hasDecl(T.Metrics, "weight")) {
102
- return self.custom.weight();
103
- }
104
- }
105
- return self.count;
106
- }
107
- };
108
-
109
- pub const Branch = struct {
110
- left: *const Node,
111
- right: *const Node,
112
- left_metrics: Metrics,
113
- total_metrics: Metrics,
114
-
115
- fn is_balanced(self: *const Branch) bool {
116
- const left_weight = self.left.metrics().weight();
117
- const right_weight = self.right.metrics().weight();
118
- const total_weight = left_weight + right_weight;
119
-
120
- if (total_weight == 0) return true;
121
-
122
- const max_side = (total_weight * 3) / 4;
123
- return left_weight <= max_side and right_weight <= max_side;
124
- }
125
- };
126
-
127
- pub const Leaf = struct {
128
- data: T,
129
- is_sentinel: bool = false,
130
-
131
- fn metrics(self: *const Leaf) Metrics {
132
- var m = Metrics{
133
- .count = if (self.is_sentinel) 0 else 1,
134
- .depth = 1,
135
- };
136
-
137
- if (@hasDecl(T, "Metrics")) {
138
- if (@hasDecl(T, "measure")) {
139
- m.custom = self.data.measure();
140
- }
141
- }
142
-
143
- if (!self.is_sentinel and marker_enabled) {
144
- const tag = std.meta.activeTag(self.data);
145
- inline for (T.MarkerTypes, 0..) |mt, i| {
146
- if (tag == mt) {
147
- m.marker_counts[i] = 1;
148
- break;
149
- }
150
- }
151
- }
152
-
153
- return m;
154
- }
155
- };
156
-
157
- pub const Node = union(enum) {
158
- branch: Branch,
159
- leaf: Leaf,
160
-
161
- pub fn metrics(self: *const Node) Metrics {
162
- return switch (self.*) {
163
- .branch => |*b| b.total_metrics,
164
- .leaf => |*l| l.metrics(),
165
- };
166
- }
167
-
168
- pub fn depth(self: *const Node) u32 {
169
- return self.metrics().depth;
170
- }
171
-
172
- pub fn count(self: *const Node) u32 {
173
- return self.metrics().count;
174
- }
175
-
176
- pub fn is_balanced(self: *const Node) bool {
177
- return switch (self.*) {
178
- .branch => |*b| b.is_balanced(),
179
- .leaf => true,
180
- };
181
- }
182
-
183
- pub fn is_empty(self: *const Node) bool {
184
- return switch (self.*) {
185
- .branch => |*b| b.left.is_empty() and b.right.is_empty(),
186
- .leaf => |*l| {
187
- if (@hasDecl(T, "is_empty")) {
188
- return l.data.is_empty();
189
- }
190
- return false;
191
- },
192
- };
193
- }
194
-
195
- pub fn is_sentinel(self: *const Node, empty_leaf: *const Node) bool {
196
- return self == empty_leaf;
197
- }
198
-
199
- pub fn new_branch(allocator: Allocator, left: *const Node, right: *const Node) !*const Node {
200
- const node = try allocator.create(Node);
201
- errdefer allocator.destroy(node);
202
-
203
- const left_metrics = left.metrics();
204
- var total_metrics = Metrics{};
205
- total_metrics.add(left_metrics);
206
- total_metrics.add(right.metrics());
207
- total_metrics.depth += 1;
208
-
209
- node.* = .{ .branch = .{
210
- .left = left,
211
- .right = right,
212
- .left_metrics = left_metrics,
213
- .total_metrics = total_metrics,
214
- } };
215
-
216
- return node;
217
- }
218
-
219
- pub fn new_leaf(allocator: Allocator, data: T) !*const Node {
220
- const node = try allocator.create(Node);
221
- errdefer allocator.destroy(node);
222
-
223
- node.* = .{ .leaf = .{ .data = data } };
224
- return node;
225
- }
226
-
227
- pub fn get(self: *const Node, index: u32) ?*const T {
228
- return switch (self.*) {
229
- .branch => |*b| {
230
- const left_count = b.left_metrics.count;
231
- if (index < left_count) {
232
- return b.left.get(index);
233
- }
234
- return b.right.get(index - left_count);
235
- },
236
- .leaf => |*l| if (index == 0) &l.data else null,
237
- };
238
- }
239
-
240
- pub const WalkerFn = *const fn (ctx: *anyopaque, data: *const T, index: u32) WalkerResult;
241
-
242
- pub const WalkerResult = struct {
243
- keep_walking: bool = true,
244
- err: ?anyerror = null,
245
- };
246
-
247
- pub fn walk(self: *const Node, ctx: *anyopaque, f: WalkerFn, current_index: *u32) WalkerResult {
248
- return switch (self.*) {
249
- .branch => |*b| {
250
- const left_result = b.left.walk(ctx, f, current_index);
251
- if (!left_result.keep_walking or left_result.err != null) {
252
- return left_result;
253
- }
254
- return b.right.walk(ctx, f, current_index);
255
- },
256
- .leaf => |*l| {
257
- const result = f(ctx, &l.data, current_index.*);
258
- current_index.* += 1;
259
- return result;
260
- },
261
- };
262
- }
263
-
264
- pub fn walk_from(self: *const Node, start_index: u32, ctx: *anyopaque, f: WalkerFn) WalkerResult {
265
- var current_index: u32 = start_index;
266
- return self.walk_from_internal(start_index, ctx, f, &current_index);
267
- }
268
-
269
- fn walk_from_internal(self: *const Node, start_index: u32, ctx: *anyopaque, f: WalkerFn, current_index: *u32) WalkerResult {
270
- return switch (self.*) {
271
- .branch => |*b| {
272
- const left_count = b.left_metrics.count;
273
- if (start_index >= left_count) {
274
- return b.right.walk_from_internal(start_index - left_count, ctx, f, current_index);
275
- }
276
-
277
- const left_result = b.left.walk_from_internal(start_index, ctx, f, current_index);
278
- if (!left_result.keep_walking or left_result.err != null) {
279
- return left_result;
280
- }
281
- return b.right.walk(ctx, f, current_index);
282
- },
283
- .leaf => |*l| {
284
- if (start_index == 0) {
285
- const result = f(ctx, &l.data, current_index.*);
286
- current_index.* += 1;
287
- return result;
288
- }
289
- return .{};
290
- },
291
- };
292
- }
293
-
294
- fn collect(self: *const Node, list: *std.ArrayListUnmanaged(*const Node), allocator: Allocator) !void {
295
- switch (self.*) {
296
- .branch => |*b| {
297
- try b.left.collect(list, allocator);
298
- try b.right.collect(list, allocator);
299
- },
300
- .leaf => try list.append(allocator, self),
301
- }
302
- }
303
-
304
- fn merge_leaves(leaves: []*const Node, allocator: Allocator) error{OutOfMemory}!*const Node {
305
- const len = leaves.len;
306
- if (len == 0) return error.OutOfMemory; // Should not happen
307
- if (len == 1) return leaves[0];
308
- if (len == 2) return try Node.new_branch(allocator, leaves[0], leaves[1]);
309
-
310
- const mid = len / 2;
311
- return try Node.new_branch(
312
- allocator,
313
- try merge_leaves(leaves[0..mid], allocator),
314
- try merge_leaves(leaves[mid..], allocator),
315
- );
316
- }
317
-
318
- pub fn rebalance(self: *const Node, allocator: Allocator, tmp_allocator: Allocator) !*const Node {
319
- if (self.is_balanced()) return self;
320
-
321
- var leaves: std.ArrayListUnmanaged(*const Node) = .{};
322
- defer leaves.deinit(tmp_allocator);
323
-
324
- try leaves.ensureTotalCapacity(tmp_allocator, self.count());
325
- try self.collect(&leaves, tmp_allocator);
326
-
327
- return try merge_leaves(leaves.items, allocator);
328
- }
329
-
330
- /// Structural split at index - returns (left, right) without flattening
331
- pub fn split_at(node: *const Node, index: u32, allocator: Allocator, empty_leaf: *const Node) error{OutOfMemory}!struct { left: *const Node, right: *const Node } {
332
- return switch (node.*) {
333
- .leaf => {
334
- if (index == 0) {
335
- return .{ .left = empty_leaf, .right = node };
336
- } else {
337
- return .{ .left = node, .right = empty_leaf };
338
- }
339
- },
340
- .branch => |*b| {
341
- const left_count = b.left_metrics.count;
342
- if (index < left_count) {
343
- const result = try split_at(b.left, index, allocator, empty_leaf);
344
- const new_right = try join_balanced(result.right, b.right, allocator);
345
- return .{ .left = result.left, .right = new_right };
346
- } else if (index > left_count) {
347
- const result = try split_at(b.right, index - left_count, allocator, empty_leaf);
348
- const new_left = try join_balanced(b.left, result.left, allocator);
349
- return .{ .left = new_left, .right = result.right };
350
- } else {
351
- return .{ .left = b.left, .right = b.right };
352
- }
353
- },
354
- };
355
- }
356
-
357
- pub fn join_balanced(left: *const Node, right: *const Node, allocator: Allocator) error{OutOfMemory}!*const Node {
358
- const left_count = left.metrics().count;
359
- const right_count = right.metrics().count;
360
-
361
- if (left_count == 0) return right;
362
- if (right_count == 0) return left;
363
-
364
- const left_weight = left.metrics().weight();
365
- const right_weight = right.metrics().weight();
366
- const total_weight = left_weight + right_weight;
367
-
368
- if (total_weight > 0) {
369
- const max_side = (total_weight * 3) / 4;
370
- if (left_weight <= max_side and right_weight <= max_side) {
371
- return try new_branch(allocator, left, right);
372
- }
373
- }
374
-
375
- if (left_weight > right_weight * 3) {
376
- return switch (left.*) {
377
- .leaf => try new_branch(allocator, left, right),
378
- .branch => |*b| {
379
- const new_right = try join_balanced(b.right, right, allocator);
380
- return try new_branch(allocator, b.left, new_right);
381
- },
382
- };
383
- }
384
-
385
- return switch (right.*) {
386
- .leaf => try new_branch(allocator, left, right),
387
- .branch => |*b| {
388
- const new_left = try join_balanced(left, b.left, allocator);
389
- return try new_branch(allocator, new_left, b.right);
390
- },
391
- };
392
- }
393
-
394
- pub const LeafSplitResult = struct { left: T, right: T };
395
-
396
- pub const LeafSplitFn = struct {
397
- ctx: ?*anyopaque = null,
398
- splitFn: *const fn (ctx: ?*anyopaque, allocator: Allocator, leaf: *const T, weight_in_leaf: u32) error{ OutOfBounds, OutOfMemory }!LeafSplitResult,
399
-
400
- pub fn call(self: *const @This(), allocator: Allocator, leaf: *const T, weight: u32) error{ OutOfBounds, OutOfMemory }!LeafSplitResult {
401
- return self.splitFn(self.ctx, allocator, leaf, weight);
402
- }
403
- };
404
-
405
- pub fn split_at_weight(
406
- node: *const Node,
407
- target_weight: u32,
408
- allocator: Allocator,
409
- empty_leaf: *const Node,
410
- split_leaf_fn: *const LeafSplitFn,
411
- ) error{ OutOfMemory, OutOfBounds }!struct { left: *const Node, right: *const Node } {
412
- return switch (node.*) {
413
- .leaf => |*l| {
414
- const leaf_weight = node.metrics().weight();
415
-
416
- if (target_weight == 0) {
417
- return .{ .left = empty_leaf, .right = node };
418
- } else if (target_weight >= leaf_weight) {
419
- return .{ .left = node, .right = empty_leaf };
420
- }
421
-
422
- const split_result = try split_leaf_fn.call(allocator, &l.data, target_weight);
423
- const left_node = try new_leaf(allocator, split_result.left);
424
- const right_node = try new_leaf(allocator, split_result.right);
425
- return .{ .left = left_node, .right = right_node };
426
- },
427
- .branch => |*b| {
428
- const left_weight = b.left_metrics.weight();
429
-
430
- if (target_weight < left_weight) {
431
- const result = try split_at_weight(b.left, target_weight, allocator, empty_leaf, split_leaf_fn);
432
- const new_right = try join_balanced(result.right, b.right, allocator);
433
- return .{ .left = result.left, .right = new_right };
434
- } else if (target_weight > left_weight) {
435
- const result = try split_at_weight(b.right, target_weight - left_weight, allocator, empty_leaf, split_leaf_fn);
436
- const new_left = try join_balanced(b.left, result.left, allocator);
437
- return .{ .left = new_left, .right = result.right };
438
- } else {
439
- return .{ .left = b.left, .right = b.right };
440
- }
441
- },
442
- };
443
- }
444
- };
445
-
446
- pub const UndoNode = struct {
447
- root: *const Node,
448
- next: ?*UndoNode = null,
449
- branches: ?*UndoBranch = null,
450
- meta: []const u8,
451
- };
452
-
453
- pub const UndoBranch = struct {
454
- redo: *UndoNode,
455
- next: ?*UndoBranch,
456
- };
457
-
458
- root: *const Node,
459
- allocator: Allocator,
460
- empty_leaf: *const Node,
461
- undo_history: ?*UndoNode = null,
462
- redo_history: ?*UndoNode = null,
463
- curr_history: ?*UndoNode = null,
464
- config: Config = .{},
465
- undo_depth: usize = 0,
466
- version: u64 = 0,
467
- marker_cache: MarkerCache,
468
-
469
- pub fn init(allocator: Allocator) error{OutOfMemory}!Self {
470
- return initWithConfig(allocator, .{});
471
- }
472
-
473
- pub fn initWithConfig(allocator: Allocator, config: Config) error{OutOfMemory}!Self {
474
- const empty_data = if (@hasDecl(T, "empty"))
475
- T.empty()
476
- else
477
- std.mem.zeroes(T);
478
-
479
- const node = try allocator.create(Node);
480
- node.* = .{ .leaf = .{ .data = empty_data, .is_sentinel = true } };
481
-
482
- var self = Self{
483
- .root = node,
484
- .allocator = allocator,
485
- .empty_leaf = node,
486
- .config = config,
487
- .marker_cache = MarkerCache.init(allocator),
488
- };
489
-
490
- try self.applyEndsInvariant();
491
-
492
- return self;
493
- }
494
-
495
- pub fn from_item(allocator: Allocator, data: T) !Self {
496
- return from_itemWithConfig(allocator, data, .{});
497
- }
498
-
499
- pub fn from_itemWithConfig(allocator: Allocator, data: T, config: Config) !Self {
500
- const root = try Node.new_leaf(allocator, data);
501
- const empty_data = if (@hasDecl(T, "empty"))
502
- T.empty()
503
- else
504
- std.mem.zeroes(T);
505
-
506
- const empty_node = try allocator.create(Node);
507
- empty_node.* = .{ .leaf = .{ .data = empty_data, .is_sentinel = true } };
508
-
509
- return .{
510
- .root = root,
511
- .allocator = allocator,
512
- .empty_leaf = empty_node,
513
- .config = config,
514
- .marker_cache = MarkerCache.init(allocator),
515
- };
516
- }
517
-
518
- pub fn from_slice(allocator: Allocator, items: []const T) !Self {
519
- return from_sliceWithConfig(allocator, items, .{});
520
- }
521
-
522
- pub fn from_sliceWithConfig(allocator: Allocator, items: []const T, config: Config) !Self {
523
- if (items.len == 0) {
524
- return try initWithConfig(allocator, config);
525
- }
526
-
527
- var leaves: std.ArrayListUnmanaged(*const Node) = .{};
528
- defer leaves.deinit(allocator);
529
- try leaves.ensureTotalCapacity(allocator, items.len);
530
-
531
- for (items) |item| {
532
- const leaf = try Node.new_leaf(allocator, item);
533
- try leaves.append(allocator, leaf);
534
- }
535
-
536
- const root = try Node.merge_leaves(leaves.items, allocator);
537
- const empty_data = if (@hasDecl(T, "empty"))
538
- T.empty()
539
- else
540
- std.mem.zeroes(T);
541
-
542
- const empty_node = try allocator.create(Node);
543
- empty_node.* = .{ .leaf = .{ .data = empty_data, .is_sentinel = true } };
544
-
545
- return .{
546
- .root = root,
547
- .allocator = allocator,
548
- .empty_leaf = empty_node,
549
- .config = config,
550
- .marker_cache = MarkerCache.init(allocator),
551
- };
552
- }
553
-
554
- pub fn count(self: *const Self) u32 {
555
- return self.root.count();
556
- }
557
-
558
- pub fn get(self: *const Self, index: u32) ?*const T {
559
- return self.root.get(index);
560
- }
561
-
562
- pub fn walk(self: *const Self, ctx: *anyopaque, f: Node.WalkerFn) !void {
563
- var index: u32 = 0;
564
- const result = self.walkNode(self.root, ctx, f, &index);
565
- if (result.err) |e| return e;
566
- }
567
-
568
- fn walkNode(self: *const Self, node: *const Node, ctx: *anyopaque, f: Node.WalkerFn, current_index: *u32) Node.WalkerResult {
569
- return switch (node.*) {
570
- .branch => |*b| {
571
- const left_result = self.walkNode(b.left, ctx, f, current_index);
572
- if (!left_result.keep_walking or left_result.err != null) {
573
- return left_result;
574
- }
575
- return self.walkNode(b.right, ctx, f, current_index);
576
- },
577
- .leaf => |*l| {
578
- if (node.count() == 0) {
579
- return .{};
580
- }
581
- const result = f(ctx, &l.data, current_index.*);
582
- current_index.* += 1;
583
- return result;
584
- },
585
- };
586
- }
587
-
588
- pub fn walk_from(self: *const Self, start_index: u32, ctx: *anyopaque, f: Node.WalkerFn) !void {
589
- var current_index: u32 = 0;
590
- const result = self.walkFromNode(self.root, start_index, ctx, f, &current_index);
591
- if (result.err) |e| return e;
592
- }
593
-
594
- fn walkFromNode(self: *const Self, node: *const Node, start_index: u32, ctx: *anyopaque, f: Node.WalkerFn, current_index: *u32) Node.WalkerResult {
595
- return switch (node.*) {
596
- .branch => |*b| {
597
- const left_count = b.left_metrics.count;
598
- if (start_index >= left_count) {
599
- return self.walkFromNode(b.right, start_index - left_count, ctx, f, current_index);
600
- }
601
-
602
- const left_result = self.walkFromNode(b.left, start_index, ctx, f, current_index);
603
- if (!left_result.keep_walking or left_result.err != null) {
604
- return left_result;
605
- }
606
- return self.walkNode(b.right, ctx, f, current_index);
607
- },
608
- .leaf => |*l| {
609
- if (node.count() == 0) {
610
- return .{};
611
- }
612
- if (start_index == 0) {
613
- const result = f(ctx, &l.data, current_index.*);
614
- current_index.* += 1;
615
- return result;
616
- }
617
- return .{};
618
- },
619
- };
620
- }
621
-
622
- pub fn rebalance(self: *Self, tmp_allocator: Allocator) !void {
623
- self.root = try self.root.rebalance(self.allocator, tmp_allocator);
624
- }
625
-
626
- pub fn insert(self: *Self, index: u32, data: T) !void {
627
- try self.insert_slice(index, &[_]T{data});
628
- }
629
-
630
- pub fn delete(self: *Self, index: u32) !void {
631
- try self.delete_range(index, index + 1);
632
- }
633
-
634
- pub fn replace(self: *Self, index: u32, data: T) !void {
635
- if (index >= self.count()) return;
636
-
637
- try self.delete_range(index, index + 1);
638
- try self.insert_slice(index, &[_]T{data});
639
- }
640
-
641
- pub fn append(self: *Self, data: T) !void {
642
- try self.insert(self.count(), data);
643
- }
644
-
645
- pub fn prepend(self: *Self, data: T) !void {
646
- try self.insert(0, data);
647
- }
648
-
649
- pub fn concat(self: *Self, other: *const Self) !void {
650
- self.root = try self.joinWithBoundary(self.root, other.root);
651
- self.version += 1;
652
- }
653
-
654
- pub fn split(self: *Self, index: u32) !Self {
655
- const result = try Node.split_at(self.root, index, self.allocator, self.empty_leaf);
656
- self.root = result.left;
657
- self.version += 1;
658
- return Self{
659
- .root = result.right,
660
- .allocator = self.allocator,
661
- .empty_leaf = self.empty_leaf,
662
- .undo_history = null,
663
- .redo_history = null,
664
- .curr_history = null,
665
- .marker_cache = MarkerCache.init(self.allocator),
666
- };
667
- }
668
-
669
- pub fn slice(self: *const Self, start: u32, end: u32, allocator: Allocator) ![]T {
670
- if (start >= end) return &[_]T{};
671
-
672
- const SliceContext = struct {
673
- items: std.ArrayListUnmanaged(T),
674
- allocator: Allocator,
675
- start: u32,
676
- end: u32,
677
- current_index: u32 = 0,
678
-
679
- fn walker(ctx: *anyopaque, data: *const T, idx: u32) Node.WalkerResult {
680
- _ = idx;
681
- const context = @as(*@This(), @ptrCast(@alignCast(ctx)));
682
- if (context.current_index >= context.start and context.current_index < context.end) {
683
- context.items.append(context.allocator, data.*) catch |e| return .{ .err = e };
684
- }
685
- context.current_index += 1;
686
- if (context.current_index >= context.end) {
687
- return .{ .keep_walking = false };
688
- }
689
- return .{};
690
- }
691
- };
692
-
693
- var context = SliceContext{
694
- .items = .{},
695
- .allocator = allocator,
696
- .start = start,
697
- .end = end,
698
- };
699
- errdefer context.items.deinit(allocator);
700
-
701
- try self.walk(&context, SliceContext.walker);
702
- return context.items.toOwnedSlice(allocator);
703
- }
704
-
705
- pub fn delete_range(self: *Self, start: u32, end: u32) !void {
706
- if (start >= end) return;
707
-
708
- const first_split = try Node.split_at(self.root, start, self.allocator, self.empty_leaf);
709
- const second_split = try Node.split_at(first_split.right, end - start, self.allocator, self.empty_leaf);
710
-
711
- self.root = try self.joinWithBoundary(first_split.left, second_split.right);
712
-
713
- self.version += 1;
714
- try self.applyEndsInvariant();
715
- }
716
-
717
- pub fn insert_slice(self: *Self, index: u32, items: []const T) !void {
718
- if (items.len == 0) return;
719
-
720
- const insert_rope = try Self.from_slice(self.allocator, items);
721
-
722
- const split_result = try Node.split_at(self.root, index, self.allocator, self.empty_leaf);
723
-
724
- const left_joined = try self.joinWithBoundary(split_result.left, insert_rope.root);
725
- self.root = try self.joinWithBoundary(left_joined, split_result.right);
726
-
727
- self.version += 1;
728
- try self.applyEndsInvariant();
729
- }
730
-
731
- pub fn to_array(self: *const Self, allocator: Allocator) ![]T {
732
- const ToArrayContext = struct {
733
- items: std.ArrayListUnmanaged(T),
734
- allocator: Allocator,
735
-
736
- fn walker(ctx: *anyopaque, data: *const T, idx: u32) Node.WalkerResult {
737
- _ = idx;
738
- const context = @as(*@This(), @ptrCast(@alignCast(ctx)));
739
- context.items.append(context.allocator, data.*) catch |e| return .{ .err = e };
740
- return .{};
741
- }
742
- };
743
-
744
- var context = ToArrayContext{
745
- .items = .{},
746
- .allocator = allocator,
747
- };
748
- errdefer context.items.deinit(allocator);
749
-
750
- try self.walk(&context, ToArrayContext.walker);
751
- return context.items.toOwnedSlice(allocator);
752
- }
753
-
754
- pub fn toText(self: *const Self, allocator: Allocator) ![]u8 {
755
- var buffer: std.ArrayListUnmanaged(u8) = .{};
756
- errdefer buffer.deinit(allocator);
757
-
758
- try buffer.appendSlice(allocator, "[root");
759
- try nodeToText(self.root, &buffer, allocator);
760
- try buffer.append(allocator, ']');
761
-
762
- return buffer.toOwnedSlice(allocator);
763
- }
764
-
765
- fn nodeToText(node: *const Node, buffer: *std.ArrayListUnmanaged(u8), allocator: Allocator) !void {
766
- switch (node.*) {
767
- .branch => |*b| {
768
- try buffer.appendSlice(allocator, "[branch");
769
- try nodeToText(b.left, buffer, allocator);
770
- try nodeToText(b.right, buffer, allocator);
771
- try buffer.append(allocator, ']');
772
- },
773
- .leaf => |*l| {
774
- if (l.is_sentinel) {
775
- try buffer.appendSlice(allocator, "[empty]");
776
- return;
777
- }
778
-
779
- if (@typeInfo(T) == .@"union") {
780
- const tag = std.meta.activeTag(l.data);
781
- const tag_name = @tagName(tag);
782
-
783
- try buffer.append(allocator, '[');
784
- try buffer.appendSlice(allocator, tag_name);
785
-
786
- if (@hasDecl(T, "Metrics")) {
787
- const metrics = l.metrics();
788
- try buffer.append(allocator, ':');
789
- try buffer.writer(allocator).print("w{d}", .{metrics.weight()});
790
-
791
- if (@hasDecl(T.Metrics, "total_width")) {
792
- try buffer.writer(allocator).print(",tw{d}", .{metrics.custom.total_width});
793
- }
794
- if (@hasDecl(T.Metrics, "total_bytes")) {
795
- try buffer.writer(allocator).print(",b{d}", .{metrics.custom.total_bytes});
796
- }
797
- }
798
-
799
- try buffer.append(allocator, ']');
800
- } else {
801
- try buffer.appendSlice(allocator, "[leaf");
802
- if (@hasDecl(T, "Metrics")) {
803
- const metrics = l.metrics();
804
- try buffer.append(allocator, ':');
805
- try buffer.writer(allocator).print("w{d}", .{metrics.weight()});
806
- }
807
- try buffer.append(allocator, ']');
808
- }
809
- },
810
- }
811
- }
812
-
813
- pub fn totalWeight(self: *const Self) u32 {
814
- return self.root.metrics().weight();
815
- }
816
-
817
- pub fn splitByWeight(self: *Self, weight: u32, split_leaf_fn: *const Node.LeafSplitFn) !Self {
818
- const result = try Node.split_at_weight(self.root, weight, self.allocator, self.empty_leaf, split_leaf_fn);
819
- self.root = result.left;
820
- self.version += 1;
821
- return Self{
822
- .root = result.right,
823
- .allocator = self.allocator,
824
- .empty_leaf = self.empty_leaf,
825
- .undo_history = null,
826
- .redo_history = null,
827
- .curr_history = null,
828
- .marker_cache = MarkerCache.init(self.allocator),
829
- };
830
- }
831
-
832
- fn getLastLeaf(self: *const Self) ?*const T {
833
- if (self.count() == 0) return null;
834
- return self.get(self.count() - 1);
835
- }
836
-
837
- fn getFirstLeaf(self: *const Self) ?*const T {
838
- if (self.count() == 0) return null;
839
- return self.get(0);
840
- }
841
-
842
- fn getFirstLeafIn(node: *const Node) ?*const T {
843
- return switch (node.*) {
844
- .branch => |*b| getFirstLeafIn(b.left),
845
- .leaf => |*l| if (node.count() == 0) null else &l.data,
846
- };
847
- }
848
-
849
- fn getLastLeafIn(node: *const Node) ?*const T {
850
- return switch (node.*) {
851
- .branch => |*b| getLastLeafIn(b.right),
852
- .leaf => |*l| if (node.count() == 0) null else &l.data,
853
- };
854
- }
855
-
856
- fn dropFirst(node: *const Node, allocator: Allocator, empty_leaf: *const Node) error{OutOfMemory}!*const Node {
857
- if (node.count() == 0) return node;
858
- const split_result = try Node.split_at(node, 1, allocator, empty_leaf);
859
- return split_result.right;
860
- }
861
-
862
- fn dropLast(node: *const Node, allocator: Allocator, empty_leaf: *const Node) error{OutOfMemory}!*const Node {
863
- const cnt = node.count();
864
- if (cnt == 0) return node;
865
- const split_result = try Node.split_at(node, cnt - 1, allocator, empty_leaf);
866
- return split_result.left;
867
- }
868
-
869
- fn joinWithBoundary(self: *Self, left: *const Node, right: *const Node) error{OutOfMemory}!*const Node {
870
- if (!boundary_enabled or !@hasDecl(T, "rewriteBoundary")) {
871
- return try Node.join_balanced(left, right, self.allocator);
872
- }
873
-
874
- const l_last = getLastLeafIn(left);
875
- const r_first = getFirstLeafIn(right);
876
-
877
- if (@hasDecl(T, "canMerge") and @hasDecl(T, "merge")) {
878
- if (l_last != null and r_first != null) {
879
- if (T.canMerge(l_last.?, r_first.?)) {
880
- const merged = T.merge(self.allocator, l_last.?, r_first.?);
881
- const merged_leaf = try Node.new_leaf(self.allocator, merged);
882
-
883
- const L = try dropLast(left, self.allocator, self.empty_leaf);
884
- const R = try dropFirst(right, self.allocator, self.empty_leaf);
885
-
886
- const left_with_merged = try Node.join_balanced(L, merged_leaf, self.allocator);
887
- return try Node.join_balanced(left_with_merged, R, self.allocator);
888
- }
889
- }
890
- }
891
-
892
- const action = try T.rewriteBoundary(self.allocator, l_last, r_first);
893
-
894
- var L = left;
895
- var R = right;
896
-
897
- if (action.delete_left) {
898
- L = try dropLast(L, self.allocator, self.empty_leaf);
899
- }
900
- if (action.delete_right) {
901
- R = try dropFirst(R, self.allocator, self.empty_leaf);
902
- }
903
-
904
- if (action.insert_between.len > 0) {
905
- const insert_rope = try Self.from_slice(self.allocator, action.insert_between);
906
- const left_with_insert = try Node.join_balanced(L, insert_rope.root, self.allocator);
907
- return try Node.join_balanced(left_with_insert, R, self.allocator);
908
- }
909
-
910
- return try Node.join_balanced(L, R, self.allocator);
911
- }
912
-
913
- fn applyEndsInvariant(self: *Self) !void {
914
- if (!boundary_enabled or !@hasDecl(T, "rewriteEnds")) return;
915
-
916
- const first = self.getFirstLeaf();
917
- const last = self.getLastLeaf();
918
- const action = try T.rewriteEnds(self.allocator, first, last);
919
-
920
- // Handle deletion operations first
921
- if (action.delete_left and self.count() > 0) {
922
- const split_result = try Node.split_at(self.root, 1, self.allocator, self.empty_leaf);
923
- self.root = split_result.right;
924
- }
925
- if (action.delete_right and self.count() > 0) {
926
- const cnt = self.count();
927
- const split_result = try Node.split_at(self.root, cnt - 1, self.allocator, self.empty_leaf);
928
- self.root = split_result.left;
929
- }
930
-
931
- // Handle insertion
932
- if (action.insert_between.len > 0) {
933
- var leaves: std.ArrayListUnmanaged(*const Node) = .{};
934
- defer leaves.deinit(self.allocator);
935
- try leaves.ensureTotalCapacity(self.allocator, action.insert_between.len);
936
-
937
- for (action.insert_between) |item| {
938
- const leaf = try Node.new_leaf(self.allocator, item);
939
- try leaves.append(self.allocator, leaf);
940
- }
941
-
942
- const insert_root = try Node.merge_leaves(leaves.items, self.allocator);
943
- self.root = try Node.join_balanced(insert_root, self.root, self.allocator);
944
- }
945
- }
946
-
947
- pub fn deleteRangeByWeight(self: *Self, start: u32, end: u32, split_leaf_fn: *const Node.LeafSplitFn) !void {
948
- if (start >= end) return;
949
-
950
- const first_split = try Node.split_at_weight(self.root, start, self.allocator, self.empty_leaf, split_leaf_fn);
951
- const second_split = try Node.split_at_weight(first_split.right, end - start, self.allocator, self.empty_leaf, split_leaf_fn);
952
-
953
- self.root = try self.joinWithBoundary(first_split.left, second_split.right);
954
-
955
- self.version += 1;
956
- try self.applyEndsInvariant();
957
- }
958
-
959
- pub fn insertSliceByWeight(self: *Self, weight: u32, items: []const T, split_leaf_fn: *const Node.LeafSplitFn) !void {
960
- if (items.len == 0) return;
961
-
962
- const insert_rope = try Self.from_slice(self.allocator, items);
963
-
964
- const split_result = try Node.split_at_weight(self.root, weight, self.allocator, self.empty_leaf, split_leaf_fn);
965
-
966
- const left_joined = try self.joinWithBoundary(split_result.left, insert_rope.root);
967
- self.root = try self.joinWithBoundary(left_joined, split_result.right);
968
-
969
- self.version += 1;
970
- try self.applyEndsInvariant();
971
- }
972
-
973
- pub const WeightFindResult = struct { leaf: *const T, start_weight: u32 };
974
-
975
- pub fn findByWeight(self: *const Self, weight: u32) ?WeightFindResult {
976
- return self.findByWeightInNode(self.root, weight, 0);
977
- }
978
-
979
- fn findByWeightInNode(self: *const Self, node: *const Node, target_weight: u32, current_weight: u32) ?WeightFindResult {
980
- return switch (node.*) {
981
- .branch => |*b| {
982
- const left_weight = b.left_metrics.weight();
983
- if (target_weight < current_weight + left_weight) {
984
- return self.findByWeightInNode(b.left, target_weight, current_weight);
985
- }
986
- return self.findByWeightInNode(b.right, target_weight, current_weight + left_weight);
987
- },
988
- .leaf => |*l| {
989
- const leaf_weight = node.metrics().weight();
990
- if (target_weight < current_weight + leaf_weight) {
991
- return .{ .leaf = &l.data, .start_weight = current_weight };
992
- }
993
- return null;
994
- },
995
- };
996
- }
997
-
998
- /// Undo/Redo operations
999
- pub fn store_undo(self: *Self, meta: []const u8) !void {
1000
- const undo_node = try self.create_undo_node(self.root, meta);
1001
- self.push_undo(undo_node);
1002
- self.curr_history = null;
1003
- try self.push_redo_branch();
1004
- }
1005
-
1006
- fn create_undo_node(self: *const Self, root: *const Node, meta_: []const u8) !*UndoNode {
1007
- const undo_node = try self.allocator.create(UndoNode);
1008
- const meta = try self.allocator.dupe(u8, meta_);
1009
- undo_node.* = UndoNode{
1010
- .root = root,
1011
- .meta = meta,
1012
- };
1013
- return undo_node;
1014
- }
1015
-
1016
- fn push_undo(self: *Self, undo_node: *UndoNode) void {
1017
- const next = self.undo_history;
1018
- self.undo_history = undo_node;
1019
- undo_node.next = next;
1020
- self.undo_depth += 1;
1021
-
1022
- // Trim history if we exceed max_undo_depth
1023
- if (self.config.max_undo_depth) |max_depth| {
1024
- if (self.undo_depth > max_depth) {
1025
- self.trimUndoHistory(max_depth);
1026
- }
1027
- }
1028
- }
1029
-
1030
- fn trimUndoHistory(self: *Self, max_depth: usize) void {
1031
- var current = self.undo_history;
1032
- var depth_count: usize = 0;
1033
- var prev: ?*UndoNode = null;
1034
-
1035
- while (current) |node| {
1036
- depth_count += 1;
1037
- if (depth_count >= max_depth) {
1038
- // Cut off the rest of the history
1039
- if (prev) |p| {
1040
- p.next = null;
1041
- }
1042
- self.undo_depth = max_depth;
1043
- return;
1044
- }
1045
- prev = node;
1046
- current = node.next;
1047
- }
1048
- }
1049
-
1050
- fn push_redo(self: *Self, undo_node: *UndoNode) void {
1051
- const next = self.redo_history;
1052
- self.redo_history = undo_node;
1053
- undo_node.next = next;
1054
- }
1055
-
1056
- fn push_redo_branch(self: *Self) !void {
1057
- const r = self.redo_history orelse return;
1058
- const u = self.undo_history orelse return;
1059
- const next = u.branches;
1060
- const b = try self.allocator.create(UndoBranch);
1061
- b.* = .{
1062
- .redo = r,
1063
- .next = next,
1064
- };
1065
- u.branches = b;
1066
- self.redo_history = null;
1067
- }
1068
-
1069
- pub fn undo(self: *Self, meta: []const u8) ![]const u8 {
1070
- const r = self.curr_history orelse try self.create_undo_node(self.root, meta);
1071
- const h = self.undo_history orelse return error.Stop;
1072
- self.undo_history = h.next;
1073
- self.curr_history = h;
1074
- self.root = h.root;
1075
- self.version += 1;
1076
- self.push_redo(r);
1077
- if (self.undo_depth > 0) self.undo_depth -= 1;
1078
- return h.meta;
1079
- }
1080
-
1081
- pub fn redo(self: *Self) ![]const u8 {
1082
- const u = self.curr_history orelse return error.Stop;
1083
- const h = self.redo_history orelse return error.Stop;
1084
- if (u.root != self.root) return error.Stop;
1085
- self.redo_history = h.next;
1086
- self.curr_history = h;
1087
- self.root = h.root;
1088
- self.version += 1;
1089
- self.push_undo(u);
1090
- return h.meta;
1091
- }
1092
-
1093
- pub fn can_undo(self: *const Self) bool {
1094
- return self.undo_history != null;
1095
- }
1096
-
1097
- pub fn can_redo(self: *const Self) bool {
1098
- return self.redo_history != null and self.curr_history != null;
1099
- }
1100
-
1101
- pub fn clear_history(self: *Self) void {
1102
- self.undo_history = null;
1103
- self.redo_history = null;
1104
- self.curr_history = null;
1105
- self.undo_depth = 0;
1106
- }
1107
-
1108
- pub fn clear(self: *Self) void {
1109
- self.root = self.empty_leaf;
1110
- self.version += 1;
1111
- self.applyEndsInvariant() catch {};
1112
- }
1113
-
1114
- /// Replace the rope content with new items, using same structure as from_slice
1115
- /// This is useful for repeated setText operations without creating a new rope instance
1116
- pub fn setSegments(self: *Self, items: []const T) !void {
1117
- if (items.len == 0) {
1118
- self.root = self.empty_leaf;
1119
- self.version += 1;
1120
- try self.applyEndsInvariant();
1121
- return;
1122
- }
1123
-
1124
- var leaves: std.ArrayListUnmanaged(*const Node) = .{};
1125
- defer leaves.deinit(self.allocator);
1126
- try leaves.ensureTotalCapacity(self.allocator, items.len);
1127
-
1128
- for (items) |item| {
1129
- const leaf = try Node.new_leaf(self.allocator, item);
1130
- try leaves.append(self.allocator, leaf);
1131
- }
1132
-
1133
- self.root = try Node.merge_leaves(leaves.items, self.allocator);
1134
- self.version += 1;
1135
- }
1136
-
1137
- fn rebuildMarkerCache(self: *Self) !void {
1138
- if (!marker_enabled) return;
1139
-
1140
- self.marker_cache.clear();
1141
-
1142
- const RebuildContext = struct {
1143
- cache: *MarkerCache,
1144
- current_leaf: u32 = 0,
1145
- current_weight: u32 = 0,
1146
-
1147
- fn walker(ctx: *anyopaque, data: *const T, idx: u32) Node.WalkerResult {
1148
- _ = idx;
1149
- const context = @as(*@This(), @ptrCast(@alignCast(ctx)));
1150
-
1151
- const tag = std.meta.activeTag(data.*);
1152
-
1153
- var is_marker = false;
1154
- inline for (T.MarkerTypes) |mt| {
1155
- if (tag == mt) {
1156
- is_marker = true;
1157
- break;
1158
- }
1159
- }
1160
-
1161
- const leaf_weight = if (@hasDecl(T, "Metrics")) blk: {
1162
- if (@hasDecl(T, "measure")) {
1163
- const metrics = data.measure();
1164
- break :blk if (@hasDecl(T.Metrics, "weight")) metrics.weight() else 1;
1165
- }
1166
- break :blk 1;
1167
- } else 1;
1168
-
1169
- if (is_marker) {
1170
- const gop = context.cache.positions.getOrPut(tag) catch |e| {
1171
- return .{ .keep_walking = false, .err = e };
1172
- };
1173
- if (!gop.found_existing) {
1174
- gop.value_ptr.* = .{};
1175
- }
1176
-
1177
- gop.value_ptr.append(context.cache.allocator, .{
1178
- .leaf_index = context.current_leaf,
1179
- .global_weight = context.current_weight,
1180
- }) catch |e| {
1181
- return .{ .keep_walking = false, .err = e };
1182
- };
1183
- }
1184
-
1185
- context.current_leaf += 1;
1186
- context.current_weight += leaf_weight;
1187
- return .{};
1188
- }
1189
- };
1190
-
1191
- var ctx = RebuildContext{ .cache = &self.marker_cache };
1192
- try self.walk(&ctx, RebuildContext.walker);
1193
-
1194
- self.marker_cache.version = self.version;
1195
- }
1196
-
1197
- pub fn markerCount(self: *Self, tag: std.meta.Tag(T)) u32 {
1198
- if (!marker_enabled) return 0;
1199
-
1200
- if (self.marker_cache.version != self.version) {
1201
- self.rebuildMarkerCache() catch return 0;
1202
- }
1203
-
1204
- const list = self.marker_cache.positions.get(tag) orelse return 0;
1205
- return @intCast(list.items.len);
1206
- }
1207
-
1208
- pub fn getMarker(self: *Self, tag: std.meta.Tag(T), occurrence: u32) ?MarkerPosition {
1209
- if (!marker_enabled) return null;
1210
-
1211
- if (self.marker_cache.version != self.version) {
1212
- self.rebuildMarkerCache() catch return null;
1213
- }
1214
-
1215
- const list = self.marker_cache.positions.get(tag) orelse return null;
1216
- if (occurrence >= list.items.len) return null;
1217
- return list.items[occurrence];
1218
- }
1219
- };
1220
- }