@xincli/opentui-core 0.4.6 → 0.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 (619) hide show
  1. package/LICENSE +21 -0
  2. package/NativeSpanFeed.d.ts +52 -0
  3. package/README.md +0 -0
  4. package/Renderable.d.ts +346 -0
  5. package/animation/Timeline.d.ts +126 -0
  6. package/ansi.d.ts +17 -0
  7. package/audio.d.ts +89 -0
  8. package/buffer.d.ts +113 -0
  9. package/console.d.ts +146 -0
  10. package/edit-buffer.d.ts +97 -0
  11. package/editor-view.d.ts +72 -0
  12. package/index-m1y9e40w.js +10045 -0
  13. package/index-m1y9e40w.js.map +32 -0
  14. package/index-sqk4ajdf.js +16121 -0
  15. package/index-sqk4ajdf.js.map +58 -0
  16. package/index.d.ts +24 -0
  17. package/index.js +11709 -0
  18. package/index.js.map +35 -0
  19. package/lib/KeyHandler.d.ts +61 -0
  20. package/lib/RGBA.d.ts +42 -0
  21. package/lib/ascii.font.d.ts +508 -0
  22. package/lib/border.d.ts +51 -0
  23. package/lib/bunfs.d.ts +7 -0
  24. package/lib/clipboard.d.ts +15 -0
  25. package/lib/clock.d.ts +15 -0
  26. package/lib/data-paths.d.ts +26 -0
  27. package/lib/debounce.d.ts +42 -0
  28. package/lib/detect-links.d.ts +6 -0
  29. package/lib/env.d.ts +42 -0
  30. package/lib/extmarks-history.d.ts +17 -0
  31. package/lib/extmarks.d.ts +90 -0
  32. package/lib/hast-styled-text.d.ts +17 -0
  33. package/lib/index.d.ts +22 -0
  34. package/lib/keybinding.internal.d.ts +33 -0
  35. package/lib/objects-in-viewport.d.ts +24 -0
  36. package/lib/output.capture.d.ts +24 -0
  37. package/lib/parse.keypress-kitty.d.ts +3 -0
  38. package/lib/parse.keypress.d.ts +27 -0
  39. package/lib/parse.mouse.d.ts +30 -0
  40. package/lib/paste.d.ts +7 -0
  41. package/lib/queue.d.ts +15 -0
  42. package/lib/render-geometry.d.ts +8 -0
  43. package/lib/renderable.validations.d.ts +12 -0
  44. package/lib/scroll-acceleration.d.ts +43 -0
  45. package/lib/selection.d.ts +64 -0
  46. package/lib/singleton.d.ts +8 -0
  47. package/lib/stdin-parser.d.ts +90 -0
  48. package/lib/styled-text.d.ts +63 -0
  49. package/lib/terminal-capability-detection.d.ts +32 -0
  50. package/lib/terminal-palette.d.ts +65 -0
  51. package/lib/tree-sitter/assets/update.d.ts +12 -0
  52. package/lib/tree-sitter/client.d.ts +65 -0
  53. package/lib/tree-sitter/default-parsers.d.ts +2 -0
  54. package/lib/tree-sitter/download-utils.d.ts +21 -0
  55. package/lib/tree-sitter/index.d.ts +7 -0
  56. package/lib/tree-sitter/parser.worker.d.ts +1 -0
  57. package/lib/tree-sitter/parsers-config.d.ts +53 -0
  58. package/lib/tree-sitter/resolve-ft.d.ts +5 -0
  59. package/lib/tree-sitter/types.d.ts +185 -0
  60. package/lib/tree-sitter/update-assets.d.ts +3 -0
  61. package/lib/tree-sitter/update-assets.js +379 -0
  62. package/lib/tree-sitter/update-assets.js.map +12 -0
  63. package/lib/tree-sitter-styled-text.d.ts +16 -0
  64. package/lib/validate-dir-name.d.ts +1 -0
  65. package/lib/yoga.options.d.ts +32 -0
  66. package/native-event-worker-repro.worker.d.ts +1 -0
  67. package/package.json +52 -74
  68. package/parser.worker.js +1399 -0
  69. package/parser.worker.js.map +20 -0
  70. package/platform/ffi.d.ts +129 -0
  71. package/platform/runtime.d.ts +14 -0
  72. package/platform/test.d.ts +12 -0
  73. package/platform/worker.d.ts +26 -0
  74. package/platform/worker.node-test.d.ts +1 -0
  75. package/plugins/core-slot.d.ts +72 -0
  76. package/plugins/registry.d.ts +42 -0
  77. package/plugins/types.d.ts +34 -0
  78. package/post/effects.d.ts +147 -0
  79. package/post/filters.d.ts +65 -0
  80. package/post/matrices.d.ts +20 -0
  81. package/renderables/ASCIIFont.d.ts +52 -0
  82. package/renderables/Box.d.ts +85 -0
  83. package/renderables/Code.d.ts +97 -0
  84. package/renderables/Diff.d.ts +147 -0
  85. package/renderables/EditBufferRenderable.d.ts +238 -0
  86. package/renderables/FrameBuffer.d.ts +16 -0
  87. package/renderables/Input.d.ts +72 -0
  88. package/renderables/LineNumberRenderable.d.ts +78 -0
  89. package/renderables/Markdown.d.ts +255 -0
  90. package/renderables/ScrollBar.d.ts +77 -0
  91. package/renderables/ScrollBox.d.ts +128 -0
  92. package/renderables/Select.d.ts +122 -0
  93. package/renderables/Slider.d.ts +47 -0
  94. package/renderables/TabSelect.d.ts +98 -0
  95. package/renderables/Text.d.ts +36 -0
  96. package/renderables/TextBufferRenderable.d.ts +107 -0
  97. package/renderables/TextNode.d.ts +92 -0
  98. package/renderables/TextTable.d.ts +154 -0
  99. package/renderables/Textarea.d.ts +65 -0
  100. package/renderables/TimeToFirstDraw.d.ts +24 -0
  101. package/renderables/__tests__/renderable-test-utils.d.ts +12 -0
  102. package/renderables/composition/VRenderable.d.ts +16 -0
  103. package/renderables/composition/constructs.d.ts +35 -0
  104. package/renderables/composition/vnode.d.ts +46 -0
  105. package/renderables/index.d.ts +23 -0
  106. package/renderables/markdown-parser.d.ts +11 -0
  107. package/renderer-theme-mode.d.ts +29 -0
  108. package/renderer.d.ts +601 -0
  109. package/runtime-plugin-support-configure.d.ts +4 -0
  110. package/runtime-plugin-support-configure.js +36 -0
  111. package/runtime-plugin-support-configure.js.map +1 -0
  112. package/runtime-plugin-support-configure.node.js +15 -0
  113. package/runtime-plugin-support.d.ts +3 -0
  114. package/runtime-plugin-support.js +5 -0
  115. package/runtime-plugin-support.js.map +1 -0
  116. package/runtime-plugin-support.node.js +15 -0
  117. package/runtime-plugin.d.ts +16 -0
  118. package/runtime-plugin.js +473 -0
  119. package/runtime-plugin.js.map +1 -0
  120. package/runtime-plugin.node.js +15 -0
  121. package/syntax-style.d.ts +61 -0
  122. package/testing/bun-test-node.d.ts +87 -0
  123. package/testing/manual-clock.d.ts +17 -0
  124. package/testing/mock-keys.d.ts +81 -0
  125. package/testing/mock-mouse.d.ts +38 -0
  126. package/testing/mock-tree-sitter-client.d.ts +27 -0
  127. package/testing/spy.d.ts +7 -0
  128. package/testing/terminal-capabilities.d.ts +7 -0
  129. package/testing/test-recorder.d.ts +61 -0
  130. package/testing/test-renderer.d.ts +54 -0
  131. package/testing/test-streams.d.ts +12 -0
  132. package/testing.d.ts +8 -0
  133. package/testing.js +1004 -0
  134. package/testing.js.map +18 -0
  135. package/tests/yoga-upstream/tools/MeasureCounter.d.ts +16 -0
  136. package/tests/yoga-upstream/tools/utils.d.ts +11 -0
  137. package/text-buffer-view.d.ts +42 -0
  138. package/text-buffer.d.ts +66 -0
  139. package/types.d.ts +175 -0
  140. package/utils.d.ts +14 -0
  141. package/yoga.d.ts +437 -0
  142. package/yoga.js +193 -0
  143. package/yoga.js.map +9 -0
  144. package/zig-structs.d.ts +256 -0
  145. package/zig.d.ts +496 -0
  146. package/dev/keypress-debug-renderer.ts +0 -148
  147. package/dev/keypress-debug.ts +0 -43
  148. package/dev/print-env-vars.ts +0 -32
  149. package/dev/test-tmux-graphics-334.sh +0 -68
  150. package/dev/test-tmux-theme-queries.sh +0 -151
  151. package/dev/thai-debug-test.ts +0 -68
  152. package/dev/theme-mode.ts +0 -290
  153. package/docs/development.md +0 -144
  154. package/prebuilt/aarch64-android/libopentui.so +0 -0
  155. package/scripts/build-android.ts +0 -226
  156. package/scripts/build-native-termux.sh +0 -525
  157. package/scripts/build.ts +0 -530
  158. package/scripts/dist-test.ts +0 -312
  159. package/scripts/package-prebuilt.ts +0 -110
  160. package/scripts/publish.ts +0 -60
  161. package/scripts/test-node-hook.mjs +0 -16
  162. package/scripts/test-node.ts +0 -241
  163. package/scripts/vendor-deps.sh +0 -73
  164. package/src/NativeSpanFeed.ts +0 -375
  165. package/src/Renderable.ts +0 -1878
  166. package/src/__snapshots__/buffer.test.ts.snap +0 -28
  167. package/src/animation/Timeline.test.ts +0 -2709
  168. package/src/animation/Timeline.ts +0 -598
  169. package/src/ansi.ts +0 -22
  170. package/src/audio.ts +0 -479
  171. package/src/benchmark/attenuation-benchmark.ts +0 -81
  172. package/src/benchmark/audio-playback-benchmark.ts +0 -344
  173. package/src/benchmark/box-draw-benchmark.ts +0 -1042
  174. package/src/benchmark/colormatrix-benchmark.ts +0 -123
  175. package/src/benchmark/gain-benchmark.ts +0 -80
  176. package/src/benchmark/latest-all-bench-run.json +0 -707
  177. package/src/benchmark/latest-async-bench-run.json +0 -336
  178. package/src/benchmark/latest-default-bench-run.json +0 -657
  179. package/src/benchmark/latest-large-bench-run.json +0 -707
  180. package/src/benchmark/latest-quick-bench-run.json +0 -207
  181. package/src/benchmark/layout-benchmark.ts +0 -2547
  182. package/src/benchmark/markdown-benchmark.ts +0 -1796
  183. package/src/benchmark/native-span-feed-async-benchmark.ts +0 -355
  184. package/src/benchmark/native-span-feed-benchmark.md +0 -56
  185. package/src/benchmark/native-span-feed-benchmark.ts +0 -596
  186. package/src/benchmark/native-span-feed-compare.ts +0 -280
  187. package/src/benchmark/render-traversal-benchmark.ts +0 -928
  188. package/src/benchmark/text-buffer-render-benchmark.ts +0 -874
  189. package/src/benchmark/text-table-benchmark.ts +0 -948
  190. package/src/buffer.test.ts +0 -292
  191. package/src/buffer.ts +0 -564
  192. package/src/console.test.ts +0 -629
  193. package/src/console.ts +0 -1253
  194. package/src/edit-buffer.test.ts +0 -1852
  195. package/src/edit-buffer.ts +0 -417
  196. package/src/editor-view.test.ts +0 -1056
  197. package/src/editor-view.ts +0 -290
  198. package/src/index.ts +0 -25
  199. package/src/lib/KeyHandler.integration.test.ts +0 -292
  200. package/src/lib/KeyHandler.stopPropagation.test.ts +0 -289
  201. package/src/lib/KeyHandler.test.ts +0 -662
  202. package/src/lib/KeyHandler.ts +0 -222
  203. package/src/lib/RGBA.test.ts +0 -60
  204. package/src/lib/RGBA.ts +0 -343
  205. package/src/lib/ascii.font.ts +0 -330
  206. package/src/lib/border.test.ts +0 -83
  207. package/src/lib/border.ts +0 -170
  208. package/src/lib/bunfs.test.ts +0 -27
  209. package/src/lib/bunfs.ts +0 -18
  210. package/src/lib/clipboard.test.ts +0 -63
  211. package/src/lib/clipboard.ts +0 -41
  212. package/src/lib/clock.ts +0 -35
  213. package/src/lib/data-paths.test.ts +0 -133
  214. package/src/lib/data-paths.ts +0 -109
  215. package/src/lib/debounce.ts +0 -106
  216. package/src/lib/detect-links.test.ts +0 -98
  217. package/src/lib/detect-links.ts +0 -56
  218. package/src/lib/env.test.ts +0 -228
  219. package/src/lib/env.ts +0 -209
  220. package/src/lib/extmarks-history.ts +0 -51
  221. package/src/lib/extmarks-multiwidth.test.ts +0 -324
  222. package/src/lib/extmarks.test.ts +0 -3547
  223. package/src/lib/extmarks.ts +0 -861
  224. package/src/lib/fonts/block.json +0 -405
  225. package/src/lib/fonts/grid.json +0 -265
  226. package/src/lib/fonts/huge.json +0 -741
  227. package/src/lib/fonts/pallet.json +0 -314
  228. package/src/lib/fonts/shade.json +0 -591
  229. package/src/lib/fonts/slick.json +0 -321
  230. package/src/lib/fonts/tiny.json +0 -69
  231. package/src/lib/hast-styled-text.ts +0 -59
  232. package/src/lib/index.ts +0 -22
  233. package/src/lib/keybinding.internal.test.ts +0 -342
  234. package/src/lib/keybinding.internal.ts +0 -182
  235. package/src/lib/objects-in-viewport.test.ts +0 -789
  236. package/src/lib/objects-in-viewport.ts +0 -153
  237. package/src/lib/output.capture.ts +0 -58
  238. package/src/lib/parse.keypress-kitty.protocol.test.ts +0 -347
  239. package/src/lib/parse.keypress-kitty.test.ts +0 -712
  240. package/src/lib/parse.keypress-kitty.ts +0 -470
  241. package/src/lib/parse.keypress.test.ts +0 -2020
  242. package/src/lib/parse.keypress.ts +0 -480
  243. package/src/lib/parse.mouse.test.ts +0 -552
  244. package/src/lib/parse.mouse.ts +0 -232
  245. package/src/lib/paste.ts +0 -18
  246. package/src/lib/queue.ts +0 -65
  247. package/src/lib/render-geometry.ts +0 -36
  248. package/src/lib/renderable.validations.test.ts +0 -87
  249. package/src/lib/renderable.validations.ts +0 -83
  250. package/src/lib/scroll-acceleration.ts +0 -98
  251. package/src/lib/selection.ts +0 -261
  252. package/src/lib/singleton.ts +0 -34
  253. package/src/lib/stdin-parser.test.ts +0 -2439
  254. package/src/lib/stdin-parser.ts +0 -2007
  255. package/src/lib/styled-text.ts +0 -178
  256. package/src/lib/terminal-capability-detection.test.ts +0 -226
  257. package/src/lib/terminal-capability-detection.ts +0 -98
  258. package/src/lib/terminal-palette.test.ts +0 -954
  259. package/src/lib/terminal-palette.ts +0 -441
  260. package/src/lib/tree-sitter/assets/README.md +0 -119
  261. package/src/lib/tree-sitter/assets/update.ts +0 -360
  262. package/src/lib/tree-sitter/assets.d.ts +0 -9
  263. package/src/lib/tree-sitter/cache.test.ts +0 -291
  264. package/src/lib/tree-sitter/client.test.ts +0 -1434
  265. package/src/lib/tree-sitter/client.ts +0 -847
  266. package/src/lib/tree-sitter/default-parsers.ts +0 -133
  267. package/src/lib/tree-sitter/download-utils.ts +0 -148
  268. package/src/lib/tree-sitter/index.ts +0 -75
  269. package/src/lib/tree-sitter/parser.worker.ts +0 -1098
  270. package/src/lib/tree-sitter/parsers-config.ts +0 -81
  271. package/src/lib/tree-sitter/resolve-ft.test.ts +0 -55
  272. package/src/lib/tree-sitter/resolve-ft.ts +0 -189
  273. package/src/lib/tree-sitter/types.ts +0 -132
  274. package/src/lib/tree-sitter/update-assets.ts +0 -10
  275. package/src/lib/tree-sitter-styled-text.test.ts +0 -1253
  276. package/src/lib/tree-sitter-styled-text.ts +0 -314
  277. package/src/lib/validate-dir-name.ts +0 -55
  278. package/src/lib/yoga.options.test.ts +0 -628
  279. package/src/lib/yoga.options.ts +0 -346
  280. package/src/native-event-worker-repro.test.ts +0 -48
  281. package/src/native-event-worker-repro.worker.ts +0 -9
  282. package/src/native-handle.test.ts +0 -221
  283. package/src/platform/ffi.test.ts +0 -516
  284. package/src/platform/ffi.ts +0 -653
  285. package/src/platform/runtime.test.ts +0 -52
  286. package/src/platform/runtime.ts +0 -121
  287. package/src/platform/test.ts +0 -45
  288. package/src/platform/worker-handler-cleanup.fixture.ts +0 -12
  289. package/src/platform/worker-onmessage-startup.fixture.ts +0 -10
  290. package/src/platform/worker-startup.fixture.ts +0 -9
  291. package/src/platform/worker.node-test.ts +0 -153
  292. package/src/platform/worker.ts +0 -425
  293. package/src/plugins/core-slot.ts +0 -579
  294. package/src/plugins/registry.ts +0 -402
  295. package/src/plugins/types.ts +0 -46
  296. package/src/post/effects.ts +0 -948
  297. package/src/post/filters.ts +0 -512
  298. package/src/post/matrices.ts +0 -288
  299. package/src/renderables/ASCIIFont.ts +0 -219
  300. package/src/renderables/Box.test.ts +0 -474
  301. package/src/renderables/Box.ts +0 -353
  302. package/src/renderables/Code.test.ts +0 -2388
  303. package/src/renderables/Code.ts +0 -555
  304. package/src/renderables/Diff.regression.test.ts +0 -381
  305. package/src/renderables/Diff.test.ts +0 -3212
  306. package/src/renderables/Diff.ts +0 -1278
  307. package/src/renderables/EditBufferRenderable.test.ts +0 -288
  308. package/src/renderables/EditBufferRenderable.ts +0 -1152
  309. package/src/renderables/FrameBuffer.ts +0 -47
  310. package/src/renderables/Input.test.ts +0 -1310
  311. package/src/renderables/Input.ts +0 -273
  312. package/src/renderables/LineNumberRenderable.ts +0 -725
  313. package/src/renderables/Markdown.ts +0 -2144
  314. package/src/renderables/ScrollBar.ts +0 -423
  315. package/src/renderables/ScrollBox.ts +0 -903
  316. package/src/renderables/Select.test.ts +0 -1134
  317. package/src/renderables/Select.ts +0 -535
  318. package/src/renderables/Slider.test.ts +0 -456
  319. package/src/renderables/Slider.ts +0 -342
  320. package/src/renderables/TabSelect.test.ts +0 -238
  321. package/src/renderables/TabSelect.ts +0 -447
  322. package/src/renderables/Text.selection-buffer.test.ts +0 -118
  323. package/src/renderables/Text.test.ts +0 -2662
  324. package/src/renderables/Text.ts +0 -147
  325. package/src/renderables/TextBufferRenderable.ts +0 -504
  326. package/src/renderables/TextNode.test.ts +0 -1137
  327. package/src/renderables/TextNode.ts +0 -379
  328. package/src/renderables/TextTable.test.ts +0 -1411
  329. package/src/renderables/TextTable.ts +0 -1434
  330. package/src/renderables/Textarea.ts +0 -425
  331. package/src/renderables/TimeToFirstDraw.ts +0 -89
  332. package/src/renderables/__snapshots__/Code.test.ts.snap +0 -13
  333. package/src/renderables/__snapshots__/Diff.test.ts.snap +0 -785
  334. package/src/renderables/__snapshots__/Text.test.ts.snap +0 -421
  335. package/src/renderables/__snapshots__/TextTable.test.ts.snap +0 -215
  336. package/src/renderables/__tests__/Code.test.ts +0 -55
  337. package/src/renderables/__tests__/LineNumberRenderable.scrollbox-simple.test.ts +0 -144
  338. package/src/renderables/__tests__/LineNumberRenderable.scrollbox.test.ts +0 -827
  339. package/src/renderables/__tests__/LineNumberRenderable.test.ts +0 -1906
  340. package/src/renderables/__tests__/LineNumberRenderable.wrapping.test.ts +0 -85
  341. package/src/renderables/__tests__/Markdown.code-colors.test.ts +0 -288
  342. package/src/renderables/__tests__/Markdown.test.ts +0 -4175
  343. package/src/renderables/__tests__/MultiRenderable.selection.test.ts +0 -87
  344. package/src/renderables/__tests__/Textarea.buffer.test.ts +0 -683
  345. package/src/renderables/__tests__/Textarea.destroyed-events.test.ts +0 -675
  346. package/src/renderables/__tests__/Textarea.editing.test.ts +0 -2120
  347. package/src/renderables/__tests__/Textarea.error-handling.test.ts +0 -35
  348. package/src/renderables/__tests__/Textarea.events.test.ts +0 -738
  349. package/src/renderables/__tests__/Textarea.highlights.test.ts +0 -590
  350. package/src/renderables/__tests__/Textarea.keybinding.test.ts +0 -3275
  351. package/src/renderables/__tests__/Textarea.paste.test.ts +0 -357
  352. package/src/renderables/__tests__/Textarea.rendering.test.ts +0 -1927
  353. package/src/renderables/__tests__/Textarea.scroll.test.ts +0 -733
  354. package/src/renderables/__tests__/Textarea.selection.test.ts +0 -1589
  355. package/src/renderables/__tests__/Textarea.stress.test.ts +0 -670
  356. package/src/renderables/__tests__/Textarea.undo-redo.test.ts +0 -383
  357. package/src/renderables/__tests__/Textarea.visual-lines.test.ts +0 -310
  358. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.code.test.ts.snap +0 -221
  359. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox-simple.test.ts.snap +0 -89
  360. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox.test.ts.snap +0 -457
  361. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.test.ts.snap +0 -158
  362. package/src/renderables/__tests__/__snapshots__/Textarea.rendering.test.ts.snap +0 -387
  363. package/src/renderables/__tests__/markdown-parser.test.ts +0 -241
  364. package/src/renderables/__tests__/renderable-test-utils.ts +0 -60
  365. package/src/renderables/composition/README.md +0 -8
  366. package/src/renderables/composition/VRenderable.ts +0 -32
  367. package/src/renderables/composition/constructs.ts +0 -127
  368. package/src/renderables/composition/vnode.ts +0 -277
  369. package/src/renderables/index.ts +0 -23
  370. package/src/renderables/markdown-parser.ts +0 -79
  371. package/src/renderer-theme-mode.ts +0 -199
  372. package/src/renderer.ts +0 -4975
  373. package/src/runtime-plugin-support-configure.ts +0 -63
  374. package/src/runtime-plugin-support.ts +0 -13
  375. package/src/runtime-plugin.ts +0 -626
  376. package/src/specs/terminal-startup.md +0 -64
  377. package/src/syntax-style.test.ts +0 -841
  378. package/src/syntax-style.ts +0 -274
  379. package/src/testing/README.md +0 -219
  380. package/src/testing/bun-test-node.ts +0 -880
  381. package/src/testing/capture-spans.test.ts +0 -216
  382. package/src/testing/integration.test.ts +0 -276
  383. package/src/testing/manual-clock.ts +0 -117
  384. package/src/testing/mock-keys.test.ts +0 -1378
  385. package/src/testing/mock-keys.ts +0 -457
  386. package/src/testing/mock-mouse.test.ts +0 -218
  387. package/src/testing/mock-mouse.ts +0 -247
  388. package/src/testing/mock-tree-sitter-client.ts +0 -81
  389. package/src/testing/spy.ts +0 -13
  390. package/src/testing/terminal-capabilities.ts +0 -47
  391. package/src/testing/test-recorder.test.ts +0 -410
  392. package/src/testing/test-recorder.ts +0 -133
  393. package/src/testing/test-renderer.ts +0 -375
  394. package/src/testing/test-renderer.wait.test.ts +0 -278
  395. package/src/testing/test-streams.ts +0 -31
  396. package/src/testing.ts +0 -9
  397. package/src/tests/__snapshots__/absolute-positioning.snapshot.test.ts.snap +0 -481
  398. package/src/tests/__snapshots__/renderable.snapshot.test.ts.snap +0 -19
  399. package/src/tests/__snapshots__/scrollbox.test.ts.snap +0 -29
  400. package/src/tests/absolute-positioning.snapshot.test.ts +0 -638
  401. package/src/tests/allocator-stats.test.ts +0 -53
  402. package/src/tests/audio.test.ts +0 -303
  403. package/src/tests/destroy-during-render.test.ts +0 -200
  404. package/src/tests/destroy-on-exit.fixture.ts +0 -36
  405. package/src/tests/destroy-on-exit.test.ts +0 -62
  406. package/src/tests/ffi-borrowed-pointer-callsites.test.ts +0 -153
  407. package/src/tests/hover-cursor.test.ts +0 -98
  408. package/src/tests/native-backed-measurement-lifecycle.test.ts +0 -133
  409. package/src/tests/native-backed-measurement-parity.test.ts +0 -322
  410. package/src/tests/native-span-feed-async.test.ts +0 -173
  411. package/src/tests/native-span-feed-close.test.ts +0 -120
  412. package/src/tests/native-span-feed-coverage.test.ts +0 -227
  413. package/src/tests/native-span-feed-edge-cases.test.ts +0 -384
  414. package/src/tests/native-span-feed-use-after-free.test.ts +0 -45
  415. package/src/tests/opacity.test.ts +0 -123
  416. package/src/tests/renderable.snapshot.test.ts +0 -524
  417. package/src/tests/renderable.test.ts +0 -1851
  418. package/src/tests/renderer.clock.test.ts +0 -270
  419. package/src/tests/renderer.console-startup.test.ts +0 -2151
  420. package/src/tests/renderer.control.test.ts +0 -458
  421. package/src/tests/renderer.core-slot-binding.test.ts +0 -962
  422. package/src/tests/renderer.cursor.test.ts +0 -26
  423. package/src/tests/renderer.custom-stdout.test.ts +0 -1112
  424. package/src/tests/renderer.destroy-during-render.test.ts +0 -147
  425. package/src/tests/renderer.focus-restore.test.ts +0 -257
  426. package/src/tests/renderer.focus.test.ts +0 -329
  427. package/src/tests/renderer.idle.test.ts +0 -219
  428. package/src/tests/renderer.input.test.ts +0 -2615
  429. package/src/tests/renderer.kitty-flags.test.ts +0 -195
  430. package/src/tests/renderer.lifecycle.test.ts +0 -34
  431. package/src/tests/renderer.mouse.test.ts +0 -1315
  432. package/src/tests/renderer.notifications.test.ts +0 -27
  433. package/src/tests/renderer.palette.test.ts +0 -1199
  434. package/src/tests/renderer.remote.test.ts +0 -247
  435. package/src/tests/renderer.render-stats.test.ts +0 -146
  436. package/src/tests/renderer.scrollback-surface.test.ts +0 -474
  437. package/src/tests/renderer.selection.test.ts +0 -138
  438. package/src/tests/renderer.slot-registry.test.ts +0 -684
  439. package/src/tests/renderer.tracker.test.ts +0 -202
  440. package/src/tests/renderer.useMouse.test.ts +0 -83
  441. package/src/tests/runtime-plugin-node-modules-cycle.fixture.ts +0 -76
  442. package/src/tests/runtime-plugin-node-modules-import-like-string.fixture.ts +0 -48
  443. package/src/tests/runtime-plugin-node-modules-mjs.fixture.ts +0 -43
  444. package/src/tests/runtime-plugin-node-modules-no-bare-rewrite.fixture.ts +0 -67
  445. package/src/tests/runtime-plugin-node-modules-package-type-cache.fixture.ts +0 -72
  446. package/src/tests/runtime-plugin-node-modules-runtime-specifier.fixture.ts +0 -44
  447. package/src/tests/runtime-plugin-node-modules-scoped-package-bare-rewrite.fixture.ts +0 -85
  448. package/src/tests/runtime-plugin-path-alias.fixture.ts +0 -43
  449. package/src/tests/runtime-plugin-resolve-roots.fixture.ts +0 -65
  450. package/src/tests/runtime-plugin-support-configure.fixture.ts +0 -31
  451. package/src/tests/runtime-plugin-support-late-addition.fixture.ts +0 -17
  452. package/src/tests/runtime-plugin-support.fixture.ts +0 -11
  453. package/src/tests/runtime-plugin-support.test.ts +0 -54
  454. package/src/tests/runtime-plugin-windows-file-url.fixture.ts +0 -30
  455. package/src/tests/runtime-plugin.fixture.ts +0 -40
  456. package/src/tests/runtime-plugin.test.ts +0 -369
  457. package/src/tests/scrollbox-culling-bug.test.ts +0 -114
  458. package/src/tests/scrollbox-hitgrid-resize.test.ts +0 -136
  459. package/src/tests/scrollbox-hitgrid.test.ts +0 -909
  460. package/src/tests/scrollbox.test.ts +0 -1895
  461. package/src/tests/wrap-resize-perf.test.ts +0 -276
  462. package/src/tests/yoga-api-coverage.test.ts +0 -409
  463. package/src/tests/yoga-callback-stress.test.ts +0 -122
  464. package/src/tests/yoga-ffi.test.ts +0 -117
  465. package/src/tests/yoga-setters.test.ts +0 -921
  466. package/src/tests/yoga-upstream/YGAlignBaselineTest.test.ts +0 -124
  467. package/src/tests/yoga-upstream/YGComputedBorderTest.test.ts +0 -27
  468. package/src/tests/yoga-upstream/YGComputedMarginTest.test.ts +0 -27
  469. package/src/tests/yoga-upstream/YGComputedPaddingTest.test.ts +0 -27
  470. package/src/tests/yoga-upstream/YGDirtiedTest.test.ts +0 -157
  471. package/src/tests/yoga-upstream/YGErrataTest.test.ts +0 -40
  472. package/src/tests/yoga-upstream/YGFlexBasisAuto.test.ts +0 -23
  473. package/src/tests/yoga-upstream/YGHasNewLayout.test.ts +0 -81
  474. package/src/tests/yoga-upstream/YGMeasureCacheTest.test.ts +0 -31
  475. package/src/tests/yoga-upstream/YGMeasureTest.test.ts +0 -70
  476. package/src/tests/yoga-upstream/generated/YGAbsolutePositionTest.test.ts +0 -1864
  477. package/src/tests/yoga-upstream/generated/YGAlignContentTest.test.ts +0 -5829
  478. package/src/tests/yoga-upstream/generated/YGAlignItemsTest.test.ts +0 -2444
  479. package/src/tests/yoga-upstream/generated/YGAlignSelfTest.test.ts +0 -293
  480. package/src/tests/yoga-upstream/generated/YGAndroidNewsFeed.test.ts +0 -315
  481. package/src/tests/yoga-upstream/generated/YGAspectRatioTest.test.ts +0 -231
  482. package/src/tests/yoga-upstream/generated/YGAutoTest.test.ts +0 -347
  483. package/src/tests/yoga-upstream/generated/YGBorderTest.test.ts +0 -256
  484. package/src/tests/yoga-upstream/generated/YGBoxSizingTest.test.ts +0 -2747
  485. package/src/tests/yoga-upstream/generated/YGDimensionTest.test.ts +0 -129
  486. package/src/tests/yoga-upstream/generated/YGDisplayContentsTest.test.ts +0 -109
  487. package/src/tests/yoga-upstream/generated/YGDisplayTest.test.ts +0 -1097
  488. package/src/tests/yoga-upstream/generated/YGFlexDirectionTest.test.ts +0 -4562
  489. package/src/tests/yoga-upstream/generated/YGFlexTest.test.ts +0 -677
  490. package/src/tests/yoga-upstream/generated/YGFlexWrapTest.test.ts +0 -2071
  491. package/src/tests/yoga-upstream/generated/YGGapTest.test.ts +0 -3303
  492. package/src/tests/yoga-upstream/generated/YGIntrinsicSizeTest.test.ts +0 -582
  493. package/src/tests/yoga-upstream/generated/YGJustifyContentTest.test.ts +0 -2150
  494. package/src/tests/yoga-upstream/generated/YGMarginTest.test.ts +0 -1963
  495. package/src/tests/yoga-upstream/generated/YGMinMaxDimensionTest.test.ts +0 -1446
  496. package/src/tests/yoga-upstream/generated/YGPaddingTest.test.ts +0 -354
  497. package/src/tests/yoga-upstream/generated/YGPercentageTest.test.ts +0 -1760
  498. package/src/tests/yoga-upstream/generated/YGRoundingTest.test.ts +0 -1161
  499. package/src/tests/yoga-upstream/generated/YGSizeOverflowTest.test.ts +0 -207
  500. package/src/tests/yoga-upstream/generated/YGStaticPositionTest.test.ts +0 -6338
  501. package/src/tests/yoga-upstream/tools/MeasureCounter.ts +0 -54
  502. package/src/tests/yoga-upstream/tools/utils.ts +0 -81
  503. package/src/text-buffer-view.test.ts +0 -722
  504. package/src/text-buffer-view.ts +0 -193
  505. package/src/text-buffer.test.ts +0 -512
  506. package/src/text-buffer.ts +0 -250
  507. package/src/types.ts +0 -199
  508. package/src/utils.ts +0 -88
  509. package/src/yoga.ts +0 -1146
  510. package/src/zig/ansi.zig +0 -502
  511. package/src/zig/audio.zig +0 -1206
  512. package/src/zig/bench/README.md +0 -50
  513. package/src/zig/bench/buffer-color-blending_bench.zig +0 -350
  514. package/src/zig/bench/buffer-draw-box_bench.zig +0 -786
  515. package/src/zig/bench/buffer-draw-text-buffer_bench.zig +0 -894
  516. package/src/zig/bench/edit-buffer_bench.zig +0 -476
  517. package/src/zig/bench/editor-view_bench.zig +0 -169
  518. package/src/zig/bench/native-span-feed_bench.zig +0 -107
  519. package/src/zig/bench/rope-markers_bench.zig +0 -713
  520. package/src/zig/bench/rope_bench.zig +0 -514
  521. package/src/zig/bench/styled-text_bench.zig +0 -476
  522. package/src/zig/bench/text-buffer-coords_bench.zig +0 -362
  523. package/src/zig/bench/text-buffer-view_bench.zig +0 -459
  524. package/src/zig/bench/text-chunk-graphemes_bench.zig +0 -272
  525. package/src/zig/bench/utf8_bench.zig +0 -799
  526. package/src/zig/bench-utils.zig +0 -431
  527. package/src/zig/bench.zig +0 -223
  528. package/src/zig/buffer-methods.zig +0 -215
  529. package/src/zig/buffer.zig +0 -2515
  530. package/src/zig/build.zig +0 -700
  531. package/src/zig/build.zig.zon +0 -19
  532. package/src/zig/edit-buffer.zig +0 -900
  533. package/src/zig/editor-view.zig +0 -812
  534. package/src/zig/event-bus.zig +0 -28
  535. package/src/zig/event-emitter.zig +0 -65
  536. package/src/zig/file-logger.zig +0 -92
  537. package/src/zig/grapheme.zig +0 -838
  538. package/src/zig/handles.zig +0 -267
  539. package/src/zig/lib.zig +0 -2813
  540. package/src/zig/link.zig +0 -349
  541. package/src/zig/logger.zig +0 -43
  542. package/src/zig/mem-registry.zig +0 -126
  543. package/src/zig/miniaudio_shim.c +0 -21
  544. package/src/zig/native-renderable.zig +0 -112
  545. package/src/zig/native-span-feed-bench-lib.zig +0 -7
  546. package/src/zig/native-span-feed.zig +0 -747
  547. package/src/zig/renderer-output.zig +0 -677
  548. package/src/zig/renderer.zig +0 -2106
  549. package/src/zig/rope.zig +0 -1220
  550. package/src/zig/split-scrollback.zig +0 -88
  551. package/src/zig/syntax-style.zig +0 -174
  552. package/src/zig/terminal.zig +0 -1652
  553. package/src/zig/test.zig +0 -82
  554. package/src/zig/tests/README.md +0 -18
  555. package/src/zig/tests/ansi_test.zig +0 -18
  556. package/src/zig/tests/audio_test.zig +0 -439
  557. package/src/zig/tests/buffer-methods_test.zig +0 -1111
  558. package/src/zig/tests/buffer_test.zig +0 -2926
  559. package/src/zig/tests/edit-buffer-history_test.zig +0 -299
  560. package/src/zig/tests/edit-buffer_test.zig +0 -1688
  561. package/src/zig/tests/editor-view_test.zig +0 -3454
  562. package/src/zig/tests/event-emitter_test.zig +0 -249
  563. package/src/zig/tests/grapheme_test.zig +0 -1329
  564. package/src/zig/tests/handles_test.zig +0 -123
  565. package/src/zig/tests/link_test.zig +0 -220
  566. package/src/zig/tests/mem-registry_test.zig +0 -473
  567. package/src/zig/tests/memory_leak_regression_test.zig +0 -234
  568. package/src/zig/tests/native-renderable_test.zig +0 -16
  569. package/src/zig/tests/native-span-feed_test.zig +0 -1266
  570. package/src/zig/tests/renderer_test.zig +0 -2604
  571. package/src/zig/tests/rope-nested_test.zig +0 -712
  572. package/src/zig/tests/rope_fuzz_test.zig +0 -238
  573. package/src/zig/tests/rope_test.zig +0 -2362
  574. package/src/zig/tests/segment-merge.test.zig +0 -148
  575. package/src/zig/tests/split-scrollback_test.zig +0 -70
  576. package/src/zig/tests/syntax-style_test.zig +0 -558
  577. package/src/zig/tests/terminal_test.zig +0 -1489
  578. package/src/zig/tests/test-renderer.zig +0 -126
  579. package/src/zig/tests/text-buffer-drawing_test.zig +0 -3291
  580. package/src/zig/tests/text-buffer-highlights_test.zig +0 -706
  581. package/src/zig/tests/text-buffer-iterators_test.zig +0 -776
  582. package/src/zig/tests/text-buffer-segment_test.zig +0 -320
  583. package/src/zig/tests/text-buffer-selection_test.zig +0 -1056
  584. package/src/zig/tests/text-buffer-selection_viewport_test.zig +0 -358
  585. package/src/zig/tests/text-buffer-view_test.zig +0 -3672
  586. package/src/zig/tests/text-buffer_test.zig +0 -2191
  587. package/src/zig/tests/unicode-width-map.zon +0 -3909
  588. package/src/zig/tests/utf8_no_zwj_test.zig +0 -260
  589. package/src/zig/tests/utf8_test.zig +0 -4060
  590. package/src/zig/tests/utf8_wcwidth_cursor_test.zig +0 -267
  591. package/src/zig/tests/utf8_wcwidth_test.zig +0 -357
  592. package/src/zig/tests/word-wrap-editing_test.zig +0 -498
  593. package/src/zig/tests/wrap-cache-perf_test.zig +0 -113
  594. package/src/zig/tests/yoga_test.zig +0 -65
  595. package/src/zig/text-buffer-iterators.zig +0 -496
  596. package/src/zig/text-buffer-segment.zig +0 -403
  597. package/src/zig/text-buffer-view.zig +0 -1428
  598. package/src/zig/text-buffer.zig +0 -1272
  599. package/src/zig/utf8.zig +0 -1954
  600. package/src/zig/utils.zig +0 -12
  601. package/src/zig/uucode-stub.zig +0 -76
  602. package/src/zig/vendor/miniaudio/miniaudio.h +0 -93468
  603. package/src/zig/yoga.zig +0 -658
  604. package/src/zig-structs.ts +0 -381
  605. package/src/zig.ts +0 -5260
  606. package/tsconfig.build.json +0 -17
  607. package/tsconfig.json +0 -27
  608. package/tsconfig.node-test.json +0 -138
  609. /package/{src/lib/tree-sitter/assets → assets}/javascript/highlights.scm +0 -0
  610. /package/{src/lib/tree-sitter/assets → assets}/javascript/tree-sitter-javascript.wasm +0 -0
  611. /package/{src/lib/tree-sitter/assets → assets}/markdown/highlights.scm +0 -0
  612. /package/{src/lib/tree-sitter/assets → assets}/markdown/injections.scm +0 -0
  613. /package/{src/lib/tree-sitter/assets → assets}/markdown/tree-sitter-markdown.wasm +0 -0
  614. /package/{src/lib/tree-sitter/assets → assets}/markdown_inline/highlights.scm +0 -0
  615. /package/{src/lib/tree-sitter/assets → assets}/markdown_inline/tree-sitter-markdown_inline.wasm +0 -0
  616. /package/{src/lib/tree-sitter/assets → assets}/typescript/highlights.scm +0 -0
  617. /package/{src/lib/tree-sitter/assets → assets}/typescript/tree-sitter-typescript.wasm +0 -0
  618. /package/{src/lib/tree-sitter/assets → assets}/zig/highlights.scm +0 -0
  619. /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 (allocator: Allocator, ctx: ?*anyopaque, 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(allocator, self.ctx, 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
- }