@fairyhunter13/opentui-core 0.1.112 → 0.1.114

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/dev/keypress-debug-renderer.ts +148 -0
  2. package/dev/keypress-debug.ts +43 -0
  3. package/dev/print-env-vars.ts +32 -0
  4. package/dev/test-tmux-graphics-334.sh +68 -0
  5. package/dev/thai-debug-test.ts +68 -0
  6. package/docs/development.md +144 -0
  7. package/package.json +63 -51
  8. package/scripts/build.ts +400 -0
  9. package/scripts/publish.ts +60 -0
  10. package/src/3d/SpriteResourceManager.ts +286 -0
  11. package/src/3d/SpriteUtils.ts +70 -0
  12. package/src/3d/TextureUtils.ts +196 -0
  13. package/src/3d/ThreeRenderable.ts +197 -0
  14. package/src/3d/WGPURenderer.ts +294 -0
  15. package/src/3d/animation/ExplodingSpriteEffect.ts +513 -0
  16. package/src/3d/animation/PhysicsExplodingSpriteEffect.ts +429 -0
  17. package/src/3d/animation/SpriteAnimator.ts +633 -0
  18. package/src/3d/animation/SpriteParticleGenerator.ts +435 -0
  19. package/src/3d/canvas.ts +464 -0
  20. package/src/3d/index.ts +12 -0
  21. package/src/3d/physics/PlanckPhysicsAdapter.ts +72 -0
  22. package/src/3d/physics/RapierPhysicsAdapter.ts +66 -0
  23. package/src/3d/physics/physics-interface.ts +31 -0
  24. package/src/3d/shaders/supersampling.wgsl +201 -0
  25. package/src/3d.ts +3 -0
  26. package/src/NativeSpanFeed.ts +300 -0
  27. package/src/Renderable.ts +1704 -0
  28. package/src/__snapshots__/buffer.test.ts.snap +28 -0
  29. package/src/animation/Timeline.test.ts +2709 -0
  30. package/src/animation/Timeline.ts +598 -0
  31. package/src/ansi.ts +18 -0
  32. package/src/benchmark/attenuation-benchmark.ts +81 -0
  33. package/src/benchmark/colormatrix-benchmark.ts +128 -0
  34. package/src/benchmark/gain-benchmark.ts +80 -0
  35. package/src/benchmark/latest-all-bench-run.json +707 -0
  36. package/src/benchmark/latest-async-bench-run.json +336 -0
  37. package/src/benchmark/latest-default-bench-run.json +657 -0
  38. package/src/benchmark/latest-large-bench-run.json +707 -0
  39. package/src/benchmark/latest-quick-bench-run.json +207 -0
  40. package/src/benchmark/markdown-benchmark.ts +1796 -0
  41. package/src/benchmark/native-span-feed-async-benchmark.ts +355 -0
  42. package/src/benchmark/native-span-feed-benchmark.md +56 -0
  43. package/src/benchmark/native-span-feed-benchmark.ts +596 -0
  44. package/src/benchmark/native-span-feed-compare.ts +280 -0
  45. package/src/benchmark/renderer-benchmark.ts +754 -0
  46. package/src/benchmark/text-table-benchmark.ts +948 -0
  47. package/src/buffer.test.ts +291 -0
  48. package/src/buffer.ts +554 -0
  49. package/src/console.test.ts +612 -0
  50. package/src/console.ts +1254 -0
  51. package/src/edit-buffer.test.ts +1769 -0
  52. package/src/edit-buffer.ts +411 -0
  53. package/src/editor-view.test.ts +1032 -0
  54. package/src/editor-view.ts +284 -0
  55. package/src/examples/ascii-font-selection-demo.ts +245 -0
  56. package/src/examples/assets/Water_2_M_Normal.jpg +0 -0
  57. package/src/examples/assets/concrete.png +0 -0
  58. package/src/examples/assets/crate.png +0 -0
  59. package/src/examples/assets/crate_emissive.png +0 -0
  60. package/src/examples/assets/forrest_background.png +0 -0
  61. package/src/examples/assets/hast-example.json +1018 -0
  62. package/src/examples/assets/heart.png +0 -0
  63. package/src/examples/assets/main_char_heavy_attack.png +0 -0
  64. package/src/examples/assets/main_char_idle.png +0 -0
  65. package/src/examples/assets/main_char_jump_end.png +0 -0
  66. package/src/examples/assets/main_char_jump_landing.png +0 -0
  67. package/src/examples/assets/main_char_jump_start.png +0 -0
  68. package/src/examples/assets/main_char_run_loop.png +0 -0
  69. package/src/examples/assets/roughness_map.jpg +0 -0
  70. package/src/examples/build.ts +115 -0
  71. package/src/examples/code-demo.ts +924 -0
  72. package/src/examples/console-demo.ts +358 -0
  73. package/src/examples/core-plugin-slots-demo.ts +759 -0
  74. package/src/examples/diff-demo.ts +701 -0
  75. package/src/examples/draggable-three-demo.ts +259 -0
  76. package/src/examples/editor-demo.ts +322 -0
  77. package/src/examples/extmarks-demo.ts +196 -0
  78. package/src/examples/focus-restore-demo.ts +310 -0
  79. package/src/examples/fonts.ts +245 -0
  80. package/src/examples/fractal-shader-demo.ts +268 -0
  81. package/src/examples/framebuffer-demo.ts +674 -0
  82. package/src/examples/full-unicode-demo.ts +241 -0
  83. package/src/examples/golden-star-demo.ts +933 -0
  84. package/src/examples/grayscale-buffer-demo.ts +249 -0
  85. package/src/examples/hast-syntax-highlighting-demo.ts +129 -0
  86. package/src/examples/index.ts +926 -0
  87. package/src/examples/input-demo.ts +377 -0
  88. package/src/examples/input-select-layout-demo.ts +425 -0
  89. package/src/examples/install.sh +143 -0
  90. package/src/examples/keypress-debug-demo.ts +452 -0
  91. package/src/examples/lib/HexList.ts +122 -0
  92. package/src/examples/lib/PaletteGrid.ts +125 -0
  93. package/src/examples/lib/standalone-keys.ts +25 -0
  94. package/src/examples/lib/tab-controller.ts +243 -0
  95. package/src/examples/lights-phong-demo.ts +290 -0
  96. package/src/examples/link-demo.ts +220 -0
  97. package/src/examples/live-state-demo.ts +480 -0
  98. package/src/examples/markdown-demo.ts +725 -0
  99. package/src/examples/mouse-interaction-demo.ts +428 -0
  100. package/src/examples/nested-zindex-demo.ts +357 -0
  101. package/src/examples/opacity-example.ts +235 -0
  102. package/src/examples/opentui-demo.ts +1057 -0
  103. package/src/examples/physx-planck-2d-demo.ts +623 -0
  104. package/src/examples/physx-rapier-2d-demo.ts +655 -0
  105. package/src/examples/relative-positioning-demo.ts +323 -0
  106. package/src/examples/scroll-example.ts +214 -0
  107. package/src/examples/scrollbox-mouse-test.ts +112 -0
  108. package/src/examples/scrollbox-overlay-hit-test.ts +206 -0
  109. package/src/examples/select-demo.ts +237 -0
  110. package/src/examples/shader-cube-demo.ts +1015 -0
  111. package/src/examples/simple-layout-example.ts +591 -0
  112. package/src/examples/slider-demo.ts +617 -0
  113. package/src/examples/split-mode-demo.ts +453 -0
  114. package/src/examples/sprite-animation-demo.ts +443 -0
  115. package/src/examples/sprite-particle-generator-demo.ts +486 -0
  116. package/src/examples/static-sprite-demo.ts +193 -0
  117. package/src/examples/sticky-scroll-example.ts +308 -0
  118. package/src/examples/styled-text-demo.ts +282 -0
  119. package/src/examples/tab-select-demo.ts +219 -0
  120. package/src/examples/terminal-title.ts +29 -0
  121. package/src/examples/terminal.ts +305 -0
  122. package/src/examples/text-node-demo.ts +416 -0
  123. package/src/examples/text-selection-demo.ts +377 -0
  124. package/src/examples/text-table-demo.ts +503 -0
  125. package/src/examples/text-truncation-demo.ts +481 -0
  126. package/src/examples/text-wrap.ts +757 -0
  127. package/src/examples/texture-loading-demo.ts +259 -0
  128. package/src/examples/timeline-example.ts +670 -0
  129. package/src/examples/transparency-demo.ts +400 -0
  130. package/src/examples/vnode-composition-demo.ts +404 -0
  131. package/src/examples/wide-grapheme-overlay-demo.ts +280 -0
  132. package/src/index.ts +24 -0
  133. package/src/lib/KeyHandler.integration.test.ts +292 -0
  134. package/src/lib/KeyHandler.stopPropagation.test.ts +289 -0
  135. package/src/lib/KeyHandler.test.ts +662 -0
  136. package/src/lib/KeyHandler.ts +222 -0
  137. package/src/lib/RGBA.test.ts +984 -0
  138. package/src/lib/RGBA.ts +204 -0
  139. package/src/lib/ascii.font.ts +330 -0
  140. package/src/lib/border.test.ts +83 -0
  141. package/src/lib/border.ts +170 -0
  142. package/src/lib/bunfs.test.ts +27 -0
  143. package/src/lib/bunfs.ts +18 -0
  144. package/src/lib/clipboard.test.ts +41 -0
  145. package/src/lib/clipboard.ts +47 -0
  146. package/src/lib/clock.ts +35 -0
  147. package/src/lib/data-paths.test.ts +133 -0
  148. package/src/lib/data-paths.ts +109 -0
  149. package/src/lib/debounce.ts +106 -0
  150. package/src/lib/detect-links.test.ts +98 -0
  151. package/src/lib/detect-links.ts +56 -0
  152. package/src/lib/env.test.ts +228 -0
  153. package/src/lib/env.ts +209 -0
  154. package/src/lib/extmarks-history.ts +51 -0
  155. package/src/lib/extmarks-multiwidth.test.ts +322 -0
  156. package/src/lib/extmarks.test.ts +3457 -0
  157. package/src/lib/extmarks.ts +843 -0
  158. package/src/lib/fonts/block.json +405 -0
  159. package/src/lib/fonts/grid.json +265 -0
  160. package/src/lib/fonts/huge.json +741 -0
  161. package/src/lib/fonts/pallet.json +314 -0
  162. package/src/lib/fonts/shade.json +591 -0
  163. package/src/lib/fonts/slick.json +321 -0
  164. package/src/lib/fonts/tiny.json +69 -0
  165. package/src/lib/hast-styled-text.ts +59 -0
  166. package/src/lib/index.ts +21 -0
  167. package/src/lib/keymapping.test.ts +317 -0
  168. package/src/lib/keymapping.ts +115 -0
  169. package/src/lib/objects-in-viewport.test.ts +787 -0
  170. package/src/lib/objects-in-viewport.ts +153 -0
  171. package/src/lib/output.capture.ts +58 -0
  172. package/src/lib/parse.keypress-kitty.protocol.test.ts +340 -0
  173. package/src/lib/parse.keypress-kitty.test.ts +663 -0
  174. package/src/lib/parse.keypress-kitty.ts +439 -0
  175. package/src/lib/parse.keypress.test.ts +1849 -0
  176. package/src/lib/parse.keypress.ts +397 -0
  177. package/src/lib/parse.mouse.test.ts +552 -0
  178. package/src/lib/parse.mouse.ts +232 -0
  179. package/src/lib/paste.ts +16 -0
  180. package/src/lib/queue.ts +65 -0
  181. package/src/lib/renderable.validations.test.ts +87 -0
  182. package/src/lib/renderable.validations.ts +83 -0
  183. package/src/lib/scroll-acceleration.ts +98 -0
  184. package/src/lib/selection.ts +240 -0
  185. package/src/lib/singleton.ts +28 -0
  186. package/src/lib/stdin-parser.test.ts +2290 -0
  187. package/src/lib/stdin-parser.ts +1810 -0
  188. package/src/lib/styled-text.ts +178 -0
  189. package/src/lib/terminal-capability-detection.test.ts +202 -0
  190. package/src/lib/terminal-capability-detection.ts +79 -0
  191. package/src/lib/terminal-palette.test.ts +878 -0
  192. package/src/lib/terminal-palette.ts +383 -0
  193. package/src/lib/tree-sitter/assets/README.md +118 -0
  194. package/src/lib/tree-sitter/assets/update.ts +334 -0
  195. package/src/lib/tree-sitter/assets.d.ts +9 -0
  196. package/src/lib/tree-sitter/cache.test.ts +273 -0
  197. package/src/lib/tree-sitter/client.test.ts +1165 -0
  198. package/src/lib/tree-sitter/client.ts +607 -0
  199. package/src/lib/tree-sitter/default-parsers.ts +86 -0
  200. package/src/lib/tree-sitter/download-utils.ts +148 -0
  201. package/src/lib/tree-sitter/index.ts +28 -0
  202. package/src/lib/tree-sitter/parser.worker.ts +1042 -0
  203. package/src/lib/tree-sitter/parsers-config.ts +81 -0
  204. package/src/lib/tree-sitter/resolve-ft.test.ts +55 -0
  205. package/src/lib/tree-sitter/resolve-ft.ts +189 -0
  206. package/src/lib/tree-sitter/types.ts +82 -0
  207. package/src/lib/tree-sitter-styled-text.test.ts +1253 -0
  208. package/src/lib/tree-sitter-styled-text.ts +306 -0
  209. package/src/lib/validate-dir-name.ts +55 -0
  210. package/src/lib/yoga.options.test.ts +628 -0
  211. package/src/lib/yoga.options.ts +346 -0
  212. package/src/plugins/core-slot.ts +579 -0
  213. package/src/plugins/registry.ts +402 -0
  214. package/src/plugins/types.ts +46 -0
  215. package/src/post/effects.ts +930 -0
  216. package/src/post/filters.ts +489 -0
  217. package/src/post/matrices.ts +288 -0
  218. package/src/renderables/ASCIIFont.ts +219 -0
  219. package/src/renderables/Box.test.ts +205 -0
  220. package/src/renderables/Box.ts +326 -0
  221. package/src/renderables/Code.test.ts +2062 -0
  222. package/src/renderables/Code.ts +357 -0
  223. package/src/renderables/Diff.regression.test.ts +226 -0
  224. package/src/renderables/Diff.test.ts +3101 -0
  225. package/src/renderables/Diff.ts +1211 -0
  226. package/src/renderables/EditBufferRenderable.test.ts +288 -0
  227. package/src/renderables/EditBufferRenderable.ts +1166 -0
  228. package/src/renderables/FrameBuffer.ts +47 -0
  229. package/src/renderables/Input.test.ts +1228 -0
  230. package/src/renderables/Input.ts +247 -0
  231. package/src/renderables/LineNumberRenderable.ts +724 -0
  232. package/src/renderables/Markdown.ts +1393 -0
  233. package/src/renderables/ScrollBar.ts +422 -0
  234. package/src/renderables/ScrollBox.ts +883 -0
  235. package/src/renderables/Select.test.ts +1033 -0
  236. package/src/renderables/Select.ts +524 -0
  237. package/src/renderables/Slider.test.ts +456 -0
  238. package/src/renderables/Slider.ts +342 -0
  239. package/src/renderables/TabSelect.test.ts +197 -0
  240. package/src/renderables/TabSelect.ts +455 -0
  241. package/src/renderables/Text.selection-buffer.test.ts +123 -0
  242. package/src/renderables/Text.test.ts +2660 -0
  243. package/src/renderables/Text.ts +147 -0
  244. package/src/renderables/TextBufferRenderable.ts +518 -0
  245. package/src/renderables/TextNode.test.ts +1058 -0
  246. package/src/renderables/TextNode.ts +325 -0
  247. package/src/renderables/TextTable.test.ts +1421 -0
  248. package/src/renderables/TextTable.ts +1344 -0
  249. package/src/renderables/Textarea.ts +430 -0
  250. package/src/renderables/TimeToFirstDraw.ts +89 -0
  251. package/src/renderables/__snapshots__/Code.test.ts.snap +13 -0
  252. package/src/renderables/__snapshots__/Diff.test.ts.snap +785 -0
  253. package/src/renderables/__snapshots__/Text.test.ts.snap +421 -0
  254. package/src/renderables/__snapshots__/TextTable.test.ts.snap +215 -0
  255. package/src/renderables/__tests__/LineNumberRenderable.scrollbox-simple.test.ts +144 -0
  256. package/src/renderables/__tests__/LineNumberRenderable.scrollbox.test.ts +816 -0
  257. package/src/renderables/__tests__/LineNumberRenderable.test.ts +1865 -0
  258. package/src/renderables/__tests__/LineNumberRenderable.wrapping.test.ts +85 -0
  259. package/src/renderables/__tests__/Markdown.code-colors.test.ts +242 -0
  260. package/src/renderables/__tests__/Markdown.test.ts +2518 -0
  261. package/src/renderables/__tests__/MultiRenderable.selection.test.ts +87 -0
  262. package/src/renderables/__tests__/Textarea.buffer.test.ts +682 -0
  263. package/src/renderables/__tests__/Textarea.destroyed-events.test.ts +675 -0
  264. package/src/renderables/__tests__/Textarea.editing.test.ts +2041 -0
  265. package/src/renderables/__tests__/Textarea.error-handling.test.ts +35 -0
  266. package/src/renderables/__tests__/Textarea.events.test.ts +738 -0
  267. package/src/renderables/__tests__/Textarea.highlights.test.ts +590 -0
  268. package/src/renderables/__tests__/Textarea.keybinding.test.ts +3149 -0
  269. package/src/renderables/__tests__/Textarea.paste.test.ts +357 -0
  270. package/src/renderables/__tests__/Textarea.rendering.test.ts +1866 -0
  271. package/src/renderables/__tests__/Textarea.scroll.test.ts +733 -0
  272. package/src/renderables/__tests__/Textarea.selection.test.ts +1590 -0
  273. package/src/renderables/__tests__/Textarea.stress.test.ts +670 -0
  274. package/src/renderables/__tests__/Textarea.undo-redo.test.ts +383 -0
  275. package/src/renderables/__tests__/Textarea.visual-lines.test.ts +310 -0
  276. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.code.test.ts.snap +221 -0
  277. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox-simple.test.ts.snap +89 -0
  278. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox.test.ts.snap +457 -0
  279. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.test.ts.snap +158 -0
  280. package/src/renderables/__tests__/__snapshots__/Textarea.rendering.test.ts.snap +387 -0
  281. package/src/renderables/__tests__/markdown-parser.test.ts +217 -0
  282. package/src/renderables/__tests__/renderable-test-utils.ts +60 -0
  283. package/src/renderables/composition/README.md +8 -0
  284. package/src/renderables/composition/VRenderable.ts +32 -0
  285. package/src/renderables/composition/constructs.ts +127 -0
  286. package/src/renderables/composition/vnode.ts +289 -0
  287. package/src/renderables/index.ts +23 -0
  288. package/src/renderables/markdown-parser.ts +66 -0
  289. package/src/renderer.ts +2681 -0
  290. package/src/runtime-plugin-support.ts +39 -0
  291. package/src/runtime-plugin.ts +615 -0
  292. package/src/syntax-style.test.ts +841 -0
  293. package/src/syntax-style.ts +257 -0
  294. package/src/testing/README.md +210 -0
  295. package/src/testing/capture-spans.test.ts +194 -0
  296. package/src/testing/integration.test.ts +276 -0
  297. package/src/testing/manual-clock.ts +117 -0
  298. package/src/testing/mock-keys.test.ts +1378 -0
  299. package/src/testing/mock-keys.ts +457 -0
  300. package/src/testing/mock-mouse.test.ts +218 -0
  301. package/src/testing/mock-mouse.ts +247 -0
  302. package/src/testing/mock-tree-sitter-client.ts +73 -0
  303. package/src/testing/spy.ts +13 -0
  304. package/src/testing/test-recorder.test.ts +415 -0
  305. package/src/testing/test-recorder.ts +145 -0
  306. package/src/testing/test-renderer.ts +132 -0
  307. package/src/testing.ts +7 -0
  308. package/src/tests/__snapshots__/absolute-positioning.snapshot.test.ts.snap +481 -0
  309. package/src/tests/__snapshots__/renderable.snapshot.test.ts.snap +19 -0
  310. package/src/tests/__snapshots__/scrollbox.test.ts.snap +29 -0
  311. package/src/tests/absolute-positioning.snapshot.test.ts +638 -0
  312. package/src/tests/allocator-stats.test.ts +38 -0
  313. package/src/tests/destroy-during-render.test.ts +200 -0
  314. package/src/tests/destroy-on-exit.fixture.ts +36 -0
  315. package/src/tests/destroy-on-exit.test.ts +41 -0
  316. package/src/tests/hover-cursor.test.ts +98 -0
  317. package/src/tests/native-span-feed-async.test.ts +173 -0
  318. package/src/tests/native-span-feed-close.test.ts +120 -0
  319. package/src/tests/native-span-feed-coverage.test.ts +227 -0
  320. package/src/tests/native-span-feed-edge-cases.test.ts +352 -0
  321. package/src/tests/native-span-feed-use-after-free.test.ts +45 -0
  322. package/src/tests/opacity.test.ts +123 -0
  323. package/src/tests/renderable.snapshot.test.ts +524 -0
  324. package/src/tests/renderable.test.ts +1281 -0
  325. package/src/tests/renderer.clock.test.ts +158 -0
  326. package/src/tests/renderer.console-startup.test.ts +185 -0
  327. package/src/tests/renderer.control.test.ts +425 -0
  328. package/src/tests/renderer.core-slot-binding.test.ts +952 -0
  329. package/src/tests/renderer.cursor.test.ts +26 -0
  330. package/src/tests/renderer.destroy-during-render.test.ts +147 -0
  331. package/src/tests/renderer.focus-restore.test.ts +257 -0
  332. package/src/tests/renderer.focus.test.ts +294 -0
  333. package/src/tests/renderer.idle.test.ts +219 -0
  334. package/src/tests/renderer.input.test.ts +2237 -0
  335. package/src/tests/renderer.kitty-flags.test.ts +195 -0
  336. package/src/tests/renderer.mouse.test.ts +1274 -0
  337. package/src/tests/renderer.palette.test.ts +629 -0
  338. package/src/tests/renderer.selection.test.ts +49 -0
  339. package/src/tests/renderer.slot-registry.test.ts +684 -0
  340. package/src/tests/renderer.useMouse.test.ts +47 -0
  341. package/src/tests/runtime-plugin-node-modules-cycle.fixture.ts +76 -0
  342. package/src/tests/runtime-plugin-node-modules-mjs.fixture.ts +43 -0
  343. package/src/tests/runtime-plugin-node-modules-no-bare-rewrite.fixture.ts +67 -0
  344. package/src/tests/runtime-plugin-node-modules-package-type-cache.fixture.ts +72 -0
  345. package/src/tests/runtime-plugin-node-modules-runtime-specifier.fixture.ts +44 -0
  346. package/src/tests/runtime-plugin-node-modules-scoped-package-bare-rewrite.fixture.ts +85 -0
  347. package/src/tests/runtime-plugin-path-alias.fixture.ts +43 -0
  348. package/src/tests/runtime-plugin-resolve-roots.fixture.ts +65 -0
  349. package/src/tests/runtime-plugin-support.fixture.ts +11 -0
  350. package/src/tests/runtime-plugin-support.test.ts +19 -0
  351. package/src/tests/runtime-plugin-windows-file-url.fixture.ts +30 -0
  352. package/src/tests/runtime-plugin.fixture.ts +40 -0
  353. package/src/tests/runtime-plugin.test.ts +354 -0
  354. package/src/tests/scrollbox-culling-bug.test.ts +114 -0
  355. package/src/tests/scrollbox-hitgrid-resize.test.ts +136 -0
  356. package/src/tests/scrollbox-hitgrid.test.ts +909 -0
  357. package/src/tests/scrollbox.test.ts +1530 -0
  358. package/src/tests/wrap-resize-perf.test.ts +276 -0
  359. package/src/tests/yoga-setters.test.ts +921 -0
  360. package/src/text-buffer-view.test.ts +705 -0
  361. package/src/text-buffer-view.ts +189 -0
  362. package/src/text-buffer.test.ts +347 -0
  363. package/src/text-buffer.ts +250 -0
  364. package/src/types.ts +161 -0
  365. package/src/utils.ts +88 -0
  366. package/src/zig/ansi.zig +268 -0
  367. package/src/zig/bench/README.md +50 -0
  368. package/src/zig/bench/buffer-draw-text-buffer_bench.zig +887 -0
  369. package/src/zig/bench/edit-buffer_bench.zig +476 -0
  370. package/src/zig/bench/native-span-feed_bench.zig +100 -0
  371. package/src/zig/bench/rope-markers_bench.zig +713 -0
  372. package/src/zig/bench/rope_bench.zig +514 -0
  373. package/src/zig/bench/styled-text_bench.zig +470 -0
  374. package/src/zig/bench/text-buffer-coords_bench.zig +362 -0
  375. package/src/zig/bench/text-buffer-view_bench.zig +459 -0
  376. package/src/zig/bench/text-chunk-graphemes_bench.zig +273 -0
  377. package/src/zig/bench/utf8_bench.zig +799 -0
  378. package/src/zig/bench-utils.zig +431 -0
  379. package/src/zig/bench.zig +217 -0
  380. package/src/zig/buffer-methods.zig +211 -0
  381. package/src/zig/buffer.zig +2281 -0
  382. package/src/zig/build.zig +289 -0
  383. package/src/zig/build.zig.zon +16 -0
  384. package/src/zig/edit-buffer.zig +825 -0
  385. package/src/zig/editor-view.zig +802 -0
  386. package/src/zig/event-bus.zig +13 -0
  387. package/src/zig/event-emitter.zig +65 -0
  388. package/src/zig/file-logger.zig +92 -0
  389. package/src/zig/grapheme.zig +599 -0
  390. package/src/zig/lib.zig +1854 -0
  391. package/src/zig/link.zig +333 -0
  392. package/src/zig/logger.zig +43 -0
  393. package/src/zig/mem-registry.zig +125 -0
  394. package/src/zig/native-span-feed-bench-lib.zig +7 -0
  395. package/src/zig/native-span-feed.zig +708 -0
  396. package/src/zig/renderer.zig +1393 -0
  397. package/src/zig/rope.zig +1220 -0
  398. package/src/zig/syntax-style.zig +161 -0
  399. package/src/zig/terminal.zig +987 -0
  400. package/src/zig/test.zig +72 -0
  401. package/src/zig/tests/README.md +18 -0
  402. package/src/zig/tests/buffer-methods_test.zig +1109 -0
  403. package/src/zig/tests/buffer_test.zig +2557 -0
  404. package/src/zig/tests/edit-buffer-history_test.zig +271 -0
  405. package/src/zig/tests/edit-buffer_test.zig +1689 -0
  406. package/src/zig/tests/editor-view_test.zig +3299 -0
  407. package/src/zig/tests/event-emitter_test.zig +249 -0
  408. package/src/zig/tests/grapheme_test.zig +1304 -0
  409. package/src/zig/tests/link_test.zig +190 -0
  410. package/src/zig/tests/mem-registry_test.zig +473 -0
  411. package/src/zig/tests/memory_leak_regression_test.zig +159 -0
  412. package/src/zig/tests/native-span-feed_test.zig +1264 -0
  413. package/src/zig/tests/renderer_test.zig +1017 -0
  414. package/src/zig/tests/rope-nested_test.zig +712 -0
  415. package/src/zig/tests/rope_fuzz_test.zig +238 -0
  416. package/src/zig/tests/rope_test.zig +2362 -0
  417. package/src/zig/tests/segment-merge.test.zig +148 -0
  418. package/src/zig/tests/syntax-style_test.zig +557 -0
  419. package/src/zig/tests/terminal_test.zig +754 -0
  420. package/src/zig/tests/text-buffer-drawing_test.zig +3237 -0
  421. package/src/zig/tests/text-buffer-highlights_test.zig +666 -0
  422. package/src/zig/tests/text-buffer-iterators_test.zig +776 -0
  423. package/src/zig/tests/text-buffer-segment_test.zig +320 -0
  424. package/src/zig/tests/text-buffer-selection_test.zig +1035 -0
  425. package/src/zig/tests/text-buffer-selection_viewport_test.zig +358 -0
  426. package/src/zig/tests/text-buffer-view_test.zig +3649 -0
  427. package/src/zig/tests/text-buffer_test.zig +2191 -0
  428. package/src/zig/tests/unicode-width-map.zon +3909 -0
  429. package/src/zig/tests/utf8_no_zwj_test.zig +260 -0
  430. package/src/zig/tests/utf8_test.zig +4057 -0
  431. package/src/zig/tests/utf8_wcwidth_cursor_test.zig +267 -0
  432. package/src/zig/tests/utf8_wcwidth_test.zig +357 -0
  433. package/src/zig/tests/word-wrap-editing_test.zig +498 -0
  434. package/src/zig/tests/wrap-cache-perf_test.zig +113 -0
  435. package/src/zig/text-buffer-iterators.zig +499 -0
  436. package/src/zig/text-buffer-segment.zig +404 -0
  437. package/src/zig/text-buffer-view.zig +1371 -0
  438. package/src/zig/text-buffer.zig +1180 -0
  439. package/src/zig/utf8.zig +1948 -0
  440. package/src/zig/utils.zig +9 -0
  441. package/src/zig-structs.ts +261 -0
  442. package/src/zig.ts +3884 -0
  443. package/tsconfig.build.json +24 -0
  444. package/tsconfig.json +27 -0
  445. package/3d/SpriteResourceManager.d.ts +0 -74
  446. package/3d/SpriteUtils.d.ts +0 -13
  447. package/3d/TextureUtils.d.ts +0 -24
  448. package/3d/ThreeRenderable.d.ts +0 -40
  449. package/3d/WGPURenderer.d.ts +0 -61
  450. package/3d/animation/ExplodingSpriteEffect.d.ts +0 -71
  451. package/3d/animation/PhysicsExplodingSpriteEffect.d.ts +0 -76
  452. package/3d/animation/SpriteAnimator.d.ts +0 -124
  453. package/3d/animation/SpriteParticleGenerator.d.ts +0 -62
  454. package/3d/canvas.d.ts +0 -44
  455. package/3d/index.d.ts +0 -12
  456. package/3d/physics/PlanckPhysicsAdapter.d.ts +0 -19
  457. package/3d/physics/RapierPhysicsAdapter.d.ts +0 -19
  458. package/3d/physics/physics-interface.d.ts +0 -27
  459. package/3d.d.ts +0 -2
  460. package/3d.js +0 -34041
  461. package/3d.js.map +0 -155
  462. package/LICENSE +0 -21
  463. package/NativeSpanFeed.d.ts +0 -41
  464. package/Renderable.d.ts +0 -334
  465. package/animation/Timeline.d.ts +0 -126
  466. package/ansi.d.ts +0 -13
  467. package/buffer.d.ts +0 -111
  468. package/console.d.ts +0 -144
  469. package/edit-buffer.d.ts +0 -98
  470. package/editor-view.d.ts +0 -73
  471. package/index-8fks7yv1.js +0 -411
  472. package/index-8fks7yv1.js.map +0 -10
  473. package/index-egy5e2rs.js +0 -12267
  474. package/index-egy5e2rs.js.map +0 -42
  475. package/index-tse8gzh0.js +0 -20614
  476. package/index-tse8gzh0.js.map +0 -67
  477. package/index.d.ts +0 -23
  478. package/index.js +0 -478
  479. package/index.js.map +0 -9
  480. package/lib/KeyHandler.d.ts +0 -61
  481. package/lib/RGBA.d.ts +0 -25
  482. package/lib/ascii.font.d.ts +0 -508
  483. package/lib/border.d.ts +0 -51
  484. package/lib/bunfs.d.ts +0 -7
  485. package/lib/clipboard.d.ts +0 -17
  486. package/lib/clock.d.ts +0 -15
  487. package/lib/data-paths.d.ts +0 -26
  488. package/lib/debounce.d.ts +0 -42
  489. package/lib/detect-links.d.ts +0 -6
  490. package/lib/env.d.ts +0 -42
  491. package/lib/extmarks-history.d.ts +0 -17
  492. package/lib/extmarks.d.ts +0 -89
  493. package/lib/hast-styled-text.d.ts +0 -17
  494. package/lib/index.d.ts +0 -21
  495. package/lib/keymapping.d.ts +0 -25
  496. package/lib/objects-in-viewport.d.ts +0 -24
  497. package/lib/output.capture.d.ts +0 -24
  498. package/lib/parse.keypress-kitty.d.ts +0 -2
  499. package/lib/parse.keypress.d.ts +0 -26
  500. package/lib/parse.mouse.d.ts +0 -30
  501. package/lib/paste.d.ts +0 -7
  502. package/lib/queue.d.ts +0 -15
  503. package/lib/renderable.validations.d.ts +0 -12
  504. package/lib/scroll-acceleration.d.ts +0 -43
  505. package/lib/selection.d.ts +0 -63
  506. package/lib/singleton.d.ts +0 -7
  507. package/lib/stdin-parser.d.ts +0 -87
  508. package/lib/styled-text.d.ts +0 -63
  509. package/lib/terminal-capability-detection.d.ts +0 -30
  510. package/lib/terminal-palette.d.ts +0 -50
  511. package/lib/tree-sitter/assets/update.d.ts +0 -11
  512. package/lib/tree-sitter/client.d.ts +0 -47
  513. package/lib/tree-sitter/default-parsers.d.ts +0 -2
  514. package/lib/tree-sitter/download-utils.d.ts +0 -21
  515. package/lib/tree-sitter/index.d.ts +0 -8
  516. package/lib/tree-sitter/parser.worker.d.ts +0 -1
  517. package/lib/tree-sitter/parsers-config.d.ts +0 -53
  518. package/lib/tree-sitter/resolve-ft.d.ts +0 -5
  519. package/lib/tree-sitter/types.d.ts +0 -82
  520. package/lib/tree-sitter-styled-text.d.ts +0 -14
  521. package/lib/validate-dir-name.d.ts +0 -1
  522. package/lib/yoga.options.d.ts +0 -32
  523. package/parser.worker.js +0 -899
  524. package/parser.worker.js.map +0 -12
  525. package/plugins/core-slot.d.ts +0 -72
  526. package/plugins/registry.d.ts +0 -42
  527. package/plugins/types.d.ts +0 -34
  528. package/post/effects.d.ts +0 -147
  529. package/post/filters.d.ts +0 -65
  530. package/post/matrices.d.ts +0 -20
  531. package/renderables/ASCIIFont.d.ts +0 -52
  532. package/renderables/Box.d.ts +0 -81
  533. package/renderables/Code.d.ts +0 -78
  534. package/renderables/Diff.d.ts +0 -142
  535. package/renderables/EditBufferRenderable.d.ts +0 -237
  536. package/renderables/FrameBuffer.d.ts +0 -16
  537. package/renderables/Input.d.ts +0 -67
  538. package/renderables/LineNumberRenderable.d.ts +0 -78
  539. package/renderables/Markdown.d.ts +0 -185
  540. package/renderables/ScrollBar.d.ts +0 -77
  541. package/renderables/ScrollBox.d.ts +0 -124
  542. package/renderables/Select.d.ts +0 -115
  543. package/renderables/Slider.d.ts +0 -47
  544. package/renderables/TabSelect.d.ts +0 -96
  545. package/renderables/Text.d.ts +0 -36
  546. package/renderables/TextBufferRenderable.d.ts +0 -105
  547. package/renderables/TextNode.d.ts +0 -91
  548. package/renderables/TextTable.d.ts +0 -140
  549. package/renderables/Textarea.d.ts +0 -63
  550. package/renderables/TimeToFirstDraw.d.ts +0 -24
  551. package/renderables/__tests__/renderable-test-utils.d.ts +0 -12
  552. package/renderables/composition/VRenderable.d.ts +0 -16
  553. package/renderables/composition/constructs.d.ts +0 -35
  554. package/renderables/composition/vnode.d.ts +0 -46
  555. package/renderables/index.d.ts +0 -23
  556. package/renderables/markdown-parser.d.ts +0 -10
  557. package/renderer.d.ts +0 -419
  558. package/runtime-plugin-support.d.ts +0 -3
  559. package/runtime-plugin-support.js +0 -29
  560. package/runtime-plugin-support.js.map +0 -10
  561. package/runtime-plugin.d.ts +0 -16
  562. package/runtime-plugin.js +0 -16
  563. package/runtime-plugin.js.map +0 -9
  564. package/syntax-style.d.ts +0 -54
  565. package/testing/manual-clock.d.ts +0 -17
  566. package/testing/mock-keys.d.ts +0 -81
  567. package/testing/mock-mouse.d.ts +0 -38
  568. package/testing/mock-tree-sitter-client.d.ts +0 -23
  569. package/testing/spy.d.ts +0 -7
  570. package/testing/test-recorder.d.ts +0 -61
  571. package/testing/test-renderer.d.ts +0 -23
  572. package/testing.d.ts +0 -6
  573. package/testing.js +0 -697
  574. package/testing.js.map +0 -15
  575. package/text-buffer-view.d.ts +0 -42
  576. package/text-buffer.d.ts +0 -67
  577. package/types.d.ts +0 -139
  578. package/utils.d.ts +0 -14
  579. package/zig-structs.d.ts +0 -155
  580. package/zig.d.ts +0 -353
  581. /package/{assets → src/lib/tree-sitter/assets}/javascript/highlights.scm +0 -0
  582. /package/{assets → src/lib/tree-sitter/assets}/javascript/tree-sitter-javascript.wasm +0 -0
  583. /package/{assets → src/lib/tree-sitter/assets}/markdown/highlights.scm +0 -0
  584. /package/{assets → src/lib/tree-sitter/assets}/markdown/injections.scm +0 -0
  585. /package/{assets → src/lib/tree-sitter/assets}/markdown/tree-sitter-markdown.wasm +0 -0
  586. /package/{assets → src/lib/tree-sitter/assets}/markdown_inline/highlights.scm +0 -0
  587. /package/{assets → src/lib/tree-sitter/assets}/markdown_inline/tree-sitter-markdown_inline.wasm +0 -0
  588. /package/{assets → src/lib/tree-sitter/assets}/typescript/highlights.scm +0 -0
  589. /package/{assets → src/lib/tree-sitter/assets}/typescript/tree-sitter-typescript.wasm +0 -0
  590. /package/{assets → src/lib/tree-sitter/assets}/zig/highlights.scm +0 -0
  591. /package/{assets → src/lib/tree-sitter/assets}/zig/tree-sitter-zig.wasm +0 -0
@@ -0,0 +1,1180 @@
1
+ const std = @import("std");
2
+ const Allocator = std.mem.Allocator;
3
+ const seg_mod = @import("text-buffer-segment.zig");
4
+ const iter_mod = @import("text-buffer-iterators.zig");
5
+ const mem_registry_mod = @import("mem-registry.zig");
6
+ const ss = @import("syntax-style.zig");
7
+ const gp = @import("grapheme.zig");
8
+ const ansi = @import("ansi.zig");
9
+ const link = @import("link.zig");
10
+
11
+ const utf8 = @import("utf8.zig");
12
+ const utils = @import("utils.zig");
13
+
14
+ const logger = @import("logger.zig");
15
+
16
+ const Segment = seg_mod.Segment;
17
+ const UnifiedRope = seg_mod.UnifiedRope;
18
+ const LineInfo = iter_mod.LineInfo;
19
+
20
+ // Re-export types from segment module
21
+ pub const TextChunk = seg_mod.TextChunk;
22
+ pub const MemRegistry = mem_registry_mod.MemRegistry;
23
+ pub const RGBA = seg_mod.RGBA;
24
+ pub const TextSelection = seg_mod.TextSelection;
25
+ pub const TextBufferError = seg_mod.TextBufferError;
26
+ pub const Highlight = seg_mod.Highlight;
27
+ pub const StyleSpan = seg_mod.StyleSpan;
28
+ pub const WrapMode = seg_mod.WrapMode;
29
+ pub const ChunkFitResult = seg_mod.ChunkFitResult;
30
+ pub const GraphemeInfo = seg_mod.GraphemeInfo;
31
+
32
+ pub const SyntaxStyle = ss.SyntaxStyle;
33
+
34
+ pub const TextBuffer = UnifiedTextBuffer;
35
+
36
+ pub const StyledChunk = extern struct {
37
+ text_ptr: [*]const u8,
38
+ text_len: usize,
39
+ fg_ptr: ?[*]const f32,
40
+ bg_ptr: ?[*]const f32,
41
+ attributes: u32,
42
+ link_ptr: ?[*]const u8 = null,
43
+ link_len: usize = 0,
44
+ };
45
+
46
+ pub const UnifiedTextBuffer = struct {
47
+ const Self = @This();
48
+
49
+ mem_registry: MemRegistry,
50
+ default_fg: ?RGBA,
51
+ default_bg: ?RGBA,
52
+ default_attributes: ?u32,
53
+
54
+ allocator: Allocator,
55
+ global_allocator: Allocator,
56
+ arena: *std.heap.ArenaAllocator,
57
+
58
+ _rope: UnifiedRope,
59
+ syntax_style: ?*const SyntaxStyle,
60
+
61
+ pool: *gp.GraphemePool,
62
+ link_pool: *link.LinkPool,
63
+ link_tracker: ?link.LinkTracker,
64
+
65
+ width_method: utf8.WidthMethod,
66
+
67
+ view_dirty_flags: std.ArrayListUnmanaged(bool),
68
+ next_view_id: u32,
69
+ free_view_ids: std.ArrayListUnmanaged(u32),
70
+
71
+ /// Monotonic counter that increments on every content change. Views use this
72
+ /// to detect stale caches even after clearViewDirty() runs.
73
+ content_epoch: u64,
74
+
75
+ // Per-line highlight cache (invalidated on edits)
76
+ // Maps line_idx to highlights for that line
77
+ line_highlights: std.ArrayListUnmanaged(std.ArrayListUnmanaged(Highlight)),
78
+ line_spans: std.ArrayListUnmanaged(std.ArrayListUnmanaged(StyleSpan)),
79
+ highlight_batch_depth: u32,
80
+ dirty_span_lines: std.AutoHashMap(usize, void),
81
+
82
+ styled_text_mem_id: ?u8,
83
+ styled_buffer: ?[]u8,
84
+ styled_capacity: usize,
85
+
86
+ tab_width: u8,
87
+
88
+ pub const Defaults = struct {
89
+ fg: ?RGBA,
90
+ bg: ?RGBA,
91
+ attributes: ?u32,
92
+ };
93
+
94
+ /// Accessor: return default fg/bg/attributes as a struct.
95
+ pub fn defaults(self: *const Self) Defaults {
96
+ return .{
97
+ .fg = self.default_fg,
98
+ .bg = self.default_bg,
99
+ .attributes = self.default_attributes,
100
+ };
101
+ }
102
+
103
+ /// Accessor: return a const pointer to the mem registry.
104
+ pub fn memRegistry(self: *const Self) *const MemRegistry {
105
+ return &self.mem_registry;
106
+ }
107
+
108
+ /// Accessor: return the width method.
109
+ pub fn widthMethod(self: *const Self) utf8.WidthMethod {
110
+ return self.width_method;
111
+ }
112
+
113
+ /// Accessor: return tab width.
114
+ pub fn tabWidth(self: *const Self) u8 {
115
+ return self.tab_width;
116
+ }
117
+
118
+ /// Accessor: return the internal allocator.
119
+ pub fn getAllocator(self: *const Self) Allocator {
120
+ return self.allocator;
121
+ }
122
+
123
+ /// Accessor: return pointer to the rope (for edit-path callers).
124
+ /// Const-preserving: returns *UnifiedRope or *const UnifiedRope depending on receiver.
125
+ pub fn rope(self: anytype) blk: {
126
+ const T = @TypeOf(self);
127
+ break :blk if (T == *Self) *UnifiedRope else if (T == *const Self) *const UnifiedRope else @compileError("expected *Self or *const Self");
128
+ } {
129
+ return &self._rope;
130
+ }
131
+
132
+ /// Accessor: get line width at a given row.
133
+ pub fn lineWidthAt(self: *const Self, row: u32) u32 {
134
+ return iter_mod.lineWidthAt(@constCast(&self._rope), row);
135
+ }
136
+
137
+ /// Accessor: get maximum line width across all lines.
138
+ pub fn lineWidthColsMax(self: *const Self) u32 {
139
+ return iter_mod.getMaxLineWidth(&self._rope);
140
+ }
141
+
142
+ pub fn getGraphemeWidthAt(self: *const Self, row: u32, col: u32) u32 {
143
+ return iter_mod.getGraphemeWidthAt(@constCast(&self._rope), &self.mem_registry, row, col, self.tab_width, self.width_method);
144
+ }
145
+
146
+ pub fn getPrevGraphemeWidth(self: *const Self, row: u32, col: u32) u32 {
147
+ return iter_mod.getPrevGraphemeWidth(@constCast(&self._rope), &self.mem_registry, row, col, self.tab_width, self.width_method);
148
+ }
149
+
150
+ pub fn getWrapOffsetsFor(self: *const Self, chunk: *const TextChunk) TextBufferError![]const utf8.WrapBreak {
151
+ return chunk.getWrapOffsets(&self.mem_registry, self.allocator, self.width_method);
152
+ }
153
+
154
+ /// Accessor: walk all lines and segments via callbacks.
155
+ pub fn walkLinesAndSegments(
156
+ self: *const Self,
157
+ ctx: *anyopaque,
158
+ segment_callback: *const fn (ctx: *anyopaque, line_idx: u32, chunk: *const TextChunk, chunk_idx_in_line: u32) void,
159
+ line_end_callback: *const fn (ctx: *anyopaque, line_info: LineInfo) void,
160
+ ) void {
161
+ iter_mod.walkLinesAndSegments(&self._rope, ctx, segment_callback, line_end_callback);
162
+ }
163
+
164
+ pub fn init(
165
+ global_allocator: Allocator,
166
+ pool: *gp.GraphemePool,
167
+ link_pool: *link.LinkPool,
168
+ width_method: utf8.WidthMethod,
169
+ ) TextBufferError!*Self {
170
+ const self = global_allocator.create(Self) catch return TextBufferError.OutOfMemory;
171
+ errdefer global_allocator.destroy(self);
172
+
173
+ const internal_arena = global_allocator.create(std.heap.ArenaAllocator) catch return TextBufferError.OutOfMemory;
174
+ errdefer global_allocator.destroy(internal_arena);
175
+ internal_arena.* = std.heap.ArenaAllocator.init(global_allocator);
176
+
177
+ const internal_allocator = internal_arena.allocator();
178
+
179
+ const init_rope = UnifiedRope.init(internal_allocator) catch return TextBufferError.OutOfMemory;
180
+
181
+ var view_dirty_flags: std.ArrayListUnmanaged(bool) = .{};
182
+ errdefer view_dirty_flags.deinit(global_allocator);
183
+
184
+ var free_view_ids: std.ArrayListUnmanaged(u32) = .{};
185
+ errdefer free_view_ids.deinit(global_allocator);
186
+
187
+ var mem_registry = MemRegistry.init(global_allocator);
188
+ errdefer mem_registry.deinit();
189
+
190
+ var dirty_span_lines = std.AutoHashMap(usize, void).init(global_allocator);
191
+ errdefer dirty_span_lines.deinit();
192
+
193
+ self.* = .{
194
+ .mem_registry = mem_registry,
195
+ .default_fg = null,
196
+ .default_bg = null,
197
+ .default_attributes = null,
198
+ .allocator = internal_allocator,
199
+ .global_allocator = global_allocator,
200
+ .arena = internal_arena,
201
+ ._rope = init_rope,
202
+ .syntax_style = null,
203
+ .pool = pool,
204
+ .link_pool = link_pool,
205
+ .link_tracker = null,
206
+ .width_method = width_method,
207
+ .view_dirty_flags = view_dirty_flags,
208
+ .next_view_id = 0,
209
+ .free_view_ids = free_view_ids,
210
+ .content_epoch = 0,
211
+ .line_highlights = .{},
212
+ .line_spans = .{},
213
+ .highlight_batch_depth = 0,
214
+ .dirty_span_lines = dirty_span_lines,
215
+ .styled_text_mem_id = null,
216
+ .styled_buffer = null,
217
+ .styled_capacity = 0,
218
+ .tab_width = 2,
219
+ };
220
+
221
+ return self;
222
+ }
223
+
224
+ pub fn deinit(self: *Self) void {
225
+ if (self.syntax_style) |style| {
226
+ (@constCast(style)).offDestroy(@ptrCast(self), onSyntaxStyleDestroyed);
227
+ }
228
+
229
+ self.view_dirty_flags.deinit(self.global_allocator);
230
+ self.free_view_ids.deinit(self.global_allocator);
231
+
232
+ // Free highlight/span caches
233
+ for (self.line_highlights.items) |*hl_list| {
234
+ hl_list.deinit(self.global_allocator);
235
+ }
236
+ self.line_highlights.deinit(self.global_allocator);
237
+
238
+ for (self.line_spans.items) |*span_list| {
239
+ span_list.deinit(self.global_allocator);
240
+ }
241
+ self.line_spans.deinit(self.global_allocator);
242
+
243
+ // Free dirty span lines hashmap
244
+ self.dirty_span_lines.deinit();
245
+
246
+ // Free persistent styled text buffer
247
+ if (self.styled_buffer) |buf| {
248
+ self.global_allocator.free(buf);
249
+ }
250
+
251
+ if (self.link_tracker) |*tracker| {
252
+ tracker.deinit();
253
+ }
254
+
255
+ self.mem_registry.deinit();
256
+ self.arena.deinit();
257
+ self.global_allocator.destroy(self.arena);
258
+ self.global_allocator.destroy(self);
259
+ }
260
+
261
+ // View registration (same as original)
262
+ pub fn registerView(self: *Self) TextBufferError!u32 {
263
+ if (self.free_view_ids.items.len > 0) {
264
+ const id = self.free_view_ids.items[self.free_view_ids.items.len - 1];
265
+ _ = self.free_view_ids.pop();
266
+ self.view_dirty_flags.items[id] = true;
267
+ return id;
268
+ }
269
+
270
+ const id = self.next_view_id;
271
+ self.next_view_id += 1;
272
+ try self.view_dirty_flags.append(self.global_allocator, true);
273
+ return id;
274
+ }
275
+
276
+ pub fn unregisterView(self: *Self, view_id: u32) void {
277
+ if (view_id < self.view_dirty_flags.items.len) {
278
+ self.free_view_ids.append(self.global_allocator, view_id) catch {};
279
+ }
280
+ }
281
+
282
+ pub fn isViewDirty(self: *const Self, view_id: u32) bool {
283
+ if (view_id < self.view_dirty_flags.items.len) {
284
+ return self.view_dirty_flags.items[view_id];
285
+ }
286
+ return false;
287
+ }
288
+
289
+ pub fn clearViewDirty(self: *Self, view_id: u32) void {
290
+ if (view_id < self.view_dirty_flags.items.len) {
291
+ self.view_dirty_flags.items[view_id] = false;
292
+ }
293
+ }
294
+
295
+ /// Returns the current content epoch. Use this to detect buffer changes
296
+ /// independent of the dirty flag (other code paths may clear dirty).
297
+ pub fn getContentEpoch(self: *const Self) u64 {
298
+ return self.content_epoch;
299
+ }
300
+
301
+ fn markAllViewsDirty(self: *Self) void {
302
+ // Increment epoch first so views see the new value when checking caches.
303
+ // Use wrapping add for safety, though u64 won't overflow in practice.
304
+ self.content_epoch +%= 1;
305
+ for (self.view_dirty_flags.items) |*flag| {
306
+ flag.* = true;
307
+ }
308
+ }
309
+
310
+ pub fn markViewsDirty(self: *Self) void {
311
+ self.markAllViewsDirty();
312
+ }
313
+
314
+ // Basic queries using unified rope
315
+ pub fn getLength(self: *const Self) u32 {
316
+ const metrics = self._rope.root.metrics();
317
+ return metrics.custom.total_width;
318
+ }
319
+
320
+ pub fn getByteSize(self: *const Self) u32 {
321
+ const metrics = self._rope.root.metrics();
322
+ const total_bytes = metrics.custom.total_bytes;
323
+
324
+ // Add newlines between lines (line_count - 1)
325
+ const line_count = iter_mod.getLineCount(&self._rope);
326
+ if (line_count > 0) {
327
+ return total_bytes + (line_count - 1); // newlines
328
+ }
329
+ return total_bytes;
330
+ }
331
+
332
+ pub fn measureText(self: *const Self, text: []const u8) u32 {
333
+ // For grapheme-accurate width calculation (used by highlighting system),
334
+ // use utf8.calculateTextWidth which properly handles grapheme clusters
335
+ const is_ascii = utf8.isAsciiOnly(text);
336
+ return utf8.calculateTextWidth(text, self.tab_width, is_ascii, self.width_method);
337
+ }
338
+
339
+ /// Clear the text content without resetting arena or memory registry.
340
+ /// Preserves highlights, memory buffers, and arena allocations.
341
+ /// Use this for frequent text updates where undo/redo history should be preserved.
342
+ pub fn clear(self: *Self) void {
343
+ self.clearLinkRefs();
344
+ self._rope.clear();
345
+ self.markAllViewsDirty();
346
+ }
347
+
348
+ pub fn reset(self: *Self) void {
349
+ self.clearLinkRefs();
350
+
351
+ // Free highlight/span arrays (they use global_allocator, not arena)
352
+ for (self.line_highlights.items) |*hl_list| {
353
+ hl_list.deinit(self.global_allocator);
354
+ }
355
+ self.line_highlights.clearRetainingCapacity();
356
+
357
+ for (self.line_spans.items) |*span_list| {
358
+ span_list.deinit(self.global_allocator);
359
+ }
360
+ self.line_spans.clearRetainingCapacity();
361
+
362
+ // Free persistent styled text buffer
363
+ if (self.styled_buffer) |buf| {
364
+ self.global_allocator.free(buf);
365
+ }
366
+ self.styled_buffer = null;
367
+ self.styled_text_mem_id = null;
368
+ self.styled_capacity = 0;
369
+
370
+ // Now reset the arena (frees all the internal memory)
371
+ _ = self.arena.reset(if (self.arena.queryCapacity() > 0) .retain_capacity else .free_all);
372
+
373
+ self.mem_registry.clear();
374
+
375
+ self._rope = UnifiedRope.init(self.allocator) catch return;
376
+
377
+ self.markAllViewsDirty();
378
+ }
379
+
380
+ // Default colors/attributes
381
+ pub fn setDefaultFg(self: *Self, fg: ?RGBA) void {
382
+ self.default_fg = fg;
383
+ }
384
+
385
+ pub fn setDefaultBg(self: *Self, bg: ?RGBA) void {
386
+ self.default_bg = bg;
387
+ }
388
+
389
+ pub fn setDefaultAttributes(self: *Self, attributes: ?u32) void {
390
+ self.default_attributes = attributes;
391
+ }
392
+
393
+ pub fn resetDefaults(self: *Self) void {
394
+ self.default_fg = null;
395
+ self.default_bg = null;
396
+ self.default_attributes = null;
397
+ }
398
+
399
+ fn onSyntaxStyleDestroyed(ctx_ptr: *anyopaque) void {
400
+ const self = @as(*Self, @ptrCast(@alignCast(ctx_ptr)));
401
+ self.syntax_style = null;
402
+ }
403
+
404
+ pub fn setSyntaxStyle(self: *Self, syntax_style: ?*const SyntaxStyle) void {
405
+ if (self.syntax_style) |prev| {
406
+ (@constCast(prev)).offDestroy(@ptrCast(self), onSyntaxStyleDestroyed);
407
+ }
408
+ self.syntax_style = syntax_style;
409
+ if (syntax_style) |style| {
410
+ _ = (@constCast(style)).onDestroy(@ptrCast(self), onSyntaxStyleDestroyed) catch {};
411
+ }
412
+ }
413
+
414
+ pub fn getSyntaxStyle(self: *const Self) ?*const SyntaxStyle {
415
+ return self.syntax_style;
416
+ }
417
+
418
+ fn getLinkTracker(self: *Self) *link.LinkTracker {
419
+ if (self.link_tracker == null) {
420
+ self.link_tracker = link.LinkTracker.init(self.global_allocator, self.link_pool);
421
+ }
422
+
423
+ return &self.link_tracker.?;
424
+ }
425
+
426
+ fn clearLinkRefs(self: *Self) void {
427
+ if (self.link_tracker) |*tracker| {
428
+ tracker.clear();
429
+ }
430
+ }
431
+
432
+ /// Set the text content using SIMD-optimized line break detection
433
+ pub fn setText(self: *Self, text: []const u8) TextBufferError!void {
434
+ self.clear();
435
+ const mem_id = try self.mem_registry.register(text, false);
436
+ try self.setTextInternal(mem_id, text);
437
+ }
438
+
439
+ /// Set text from a pre-registered memory ID
440
+ pub fn setTextFromMemId(self: *Self, mem_id: u8) TextBufferError!void {
441
+ const text = self.mem_registry.get(mem_id) orelse return TextBufferError.InvalidMemId;
442
+ self.clear();
443
+ try self.setTextInternal(mem_id, text);
444
+ }
445
+
446
+ /// Append text to the end of the buffer without clearing
447
+ pub fn append(self: *Self, text: []const u8) TextBufferError!void {
448
+ if (text.len == 0) {
449
+ return;
450
+ }
451
+
452
+ const mem_id = try self.mem_registry.register(text, false);
453
+ try self.appendInternal(mem_id, text);
454
+ }
455
+
456
+ /// Append text from a pre-registered memory ID
457
+ pub fn appendFromMemId(self: *Self, mem_id: u8) TextBufferError!void {
458
+ const text = self.mem_registry.get(mem_id) orelse return TextBufferError.InvalidMemId;
459
+ try self.appendInternal(mem_id, text);
460
+ }
461
+
462
+ /// Internal append that doesn't register memory
463
+ fn appendInternal(self: *Self, mem_id: u8, text: []const u8) TextBufferError!void {
464
+ if (text.len == 0) {
465
+ return;
466
+ }
467
+
468
+ // The rope's boundary rewrite will handle normalization at join points
469
+ var result = try self.textToSegments(self.global_allocator, text, mem_id, 0, false);
470
+ defer result.segments.deinit(result.allocator);
471
+
472
+ const insert_pos = self._rope.count();
473
+ try self._rope.insert_slice(insert_pos, result.segments.items);
474
+
475
+ self.markAllViewsDirty();
476
+ }
477
+
478
+ /// Internal setText that doesn't call clear (for use by setStyledText)
479
+ fn setTextInternal(self: *Self, mem_id: u8, text: []const u8) TextBufferError!void {
480
+ if (text.len == 0) {
481
+ self.markAllViewsDirty();
482
+ return;
483
+ }
484
+
485
+ var result = try self.textToSegments(self.global_allocator, text, mem_id, 0, true);
486
+ defer result.segments.deinit(result.allocator);
487
+
488
+ try self._rope.setSegments(result.segments.items);
489
+
490
+ self.markAllViewsDirty();
491
+ }
492
+
493
+ /// Create a TextChunk from a memory buffer range
494
+ pub fn createChunk(
495
+ self: *const Self,
496
+ mem_id: u8,
497
+ byte_start: u32,
498
+ byte_end: u32,
499
+ ) TextChunk {
500
+ const mem_buf = self.mem_registry.get(mem_id).?;
501
+ const chunk_bytes = mem_buf[byte_start..byte_end];
502
+ const is_ascii = utf8.isAsciiOnly(chunk_bytes);
503
+
504
+ var flags: u8 = 0;
505
+ if (chunk_bytes.len > 0 and is_ascii) {
506
+ flags |= TextChunk.Flags.ASCII_ONLY;
507
+ }
508
+
509
+ const chunk_width: u16 = @intCast(@min(65535, utf8.calculateTextWidth(chunk_bytes, self.tab_width, is_ascii, self.width_method)));
510
+
511
+ return TextChunk{
512
+ .mem_id = mem_id,
513
+ .byte_start = byte_start,
514
+ .byte_end = byte_end,
515
+ .width = chunk_width,
516
+ .flags = flags,
517
+ };
518
+ }
519
+
520
+ /// Convert text to segments with line breaks
521
+ /// Returns segments array and total width
522
+ pub fn textToSegments(
523
+ self: *const Self,
524
+ allocator: Allocator,
525
+ text: []const u8,
526
+ mem_id: u8,
527
+ byte_offset: u32,
528
+ prepend_linestart: bool,
529
+ ) TextBufferError!struct { segments: std.ArrayListUnmanaged(Segment), total_width: u32, allocator: Allocator } {
530
+ var break_result = utf8.LineBreakResult.init(allocator);
531
+ defer break_result.deinit();
532
+ try utf8.findLineBreaks(text, &break_result);
533
+
534
+ var segments: std.ArrayListUnmanaged(Segment) = .{};
535
+ errdefer segments.deinit(allocator);
536
+
537
+ if (prepend_linestart) {
538
+ try segments.append(allocator, Segment{ .linestart = {} });
539
+ }
540
+
541
+ var local_start: u32 = 0;
542
+ var total_width: u32 = 0;
543
+
544
+ for (break_result.breaks.items) |line_break| {
545
+ const break_pos: u32 = @intCast(line_break.pos);
546
+ const local_end: u32 = switch (line_break.kind) {
547
+ .CRLF => break_pos - 1,
548
+ .CR, .LF => break_pos,
549
+ };
550
+
551
+ if (local_end > local_start) {
552
+ const chunk = self.createChunk(mem_id, byte_offset + local_start, byte_offset + local_end);
553
+ try segments.append(allocator, Segment{ .text = chunk });
554
+ total_width += chunk.width;
555
+ }
556
+
557
+ try segments.append(allocator, Segment{ .brk = {} });
558
+ try segments.append(allocator, Segment{ .linestart = {} });
559
+
560
+ local_start = break_pos + 1;
561
+ }
562
+
563
+ if (local_start < text.len) {
564
+ const chunk = self.createChunk(mem_id, byte_offset + local_start, byte_offset + @as(u32, @intCast(text.len)));
565
+ try segments.append(allocator, Segment{ .text = chunk });
566
+ total_width += chunk.width;
567
+ }
568
+
569
+ return .{ .segments = segments, .total_width = total_width, .allocator = allocator };
570
+ }
571
+
572
+ pub fn getLineCount(self: *const Self) u32 {
573
+ const count = self._rope.count();
574
+ if (count == 0) return 0; // Truly empty (after reset)
575
+ return iter_mod.getLineCount(&self._rope);
576
+ }
577
+
578
+ pub fn lineCount(self: *const Self) u32 {
579
+ return self.getLineCount();
580
+ }
581
+
582
+ /// Register a memory buffer
583
+ pub fn registerMemBuffer(self: *Self, data: []const u8, owned: bool) TextBufferError!u8 {
584
+ return try self.mem_registry.register(data, owned);
585
+ }
586
+
587
+ pub fn replaceMemBuffer(self: *Self, mem_id: u8, data: []const u8, owned: bool) TextBufferError!void {
588
+ try self.mem_registry.replace(mem_id, data, owned);
589
+ }
590
+
591
+ pub fn clearMemRegistry(self: *Self) void {
592
+ self.mem_registry.clear();
593
+ }
594
+
595
+ pub fn getMemBuffer(self: *const Self, mem_id: u8) ?[]const u8 {
596
+ return self.mem_registry.get(mem_id);
597
+ }
598
+
599
+ /// Add a line from a memory buffer (for compatibility with old API)
600
+ /// Note: This is not as efficient as setText for bulk operations
601
+ /// Adds text segment with a break separator before it (if not the first line)
602
+ pub fn addLine(
603
+ self: *Self,
604
+ mem_id: u8,
605
+ byte_start: u32,
606
+ byte_end: u32,
607
+ ) TextBufferError!void {
608
+ _ = self.mem_registry.get(mem_id) orelse return TextBufferError.InvalidMemId;
609
+
610
+ const chunk = self.createChunk(mem_id, byte_start, byte_end);
611
+
612
+ const had_content = self._rope.count() > 1;
613
+
614
+ if (had_content) {
615
+ try self._rope.append(Segment{ .brk = {} });
616
+ try self._rope.append(Segment{ .linestart = {} });
617
+ }
618
+
619
+ try self._rope.append(Segment{ .text = chunk });
620
+
621
+ self.markAllViewsDirty();
622
+ }
623
+
624
+ pub fn getArenaAllocatedBytes(self: *const Self) usize {
625
+ return self.arena.queryCapacity();
626
+ }
627
+
628
+ /// Extract all text as UTF-8 bytes into provided output buffer
629
+ pub fn getPlainTextIntoBuffer(self: *const Self, out_buffer: []u8) usize {
630
+ var out_index: usize = 0;
631
+
632
+ const line_count = self.getLineCount();
633
+
634
+ const Context = struct {
635
+ buffer: *const UnifiedTextBuffer,
636
+ out_buffer: []u8,
637
+ out_index: *usize,
638
+ line_count: u32,
639
+
640
+ fn segmentCallback(ctx_ptr: *anyopaque, line_idx: u32, chunk: *const TextChunk, chunk_idx_in_line: u32) void {
641
+ _ = line_idx;
642
+ _ = chunk_idx_in_line;
643
+ const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_ptr)));
644
+ const chunk_bytes = chunk.getBytes(&ctx.buffer.mem_registry);
645
+ const copy_len = @min(chunk_bytes.len, ctx.out_buffer.len - ctx.out_index.*);
646
+ if (copy_len > 0) {
647
+ @memcpy(ctx.out_buffer[ctx.out_index.* .. ctx.out_index.* + copy_len], chunk_bytes[0..copy_len]);
648
+ ctx.out_index.* += copy_len;
649
+ }
650
+ }
651
+
652
+ fn lineEndCallback(ctx_ptr: *anyopaque, line_info: LineInfo) void {
653
+ const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_ptr)));
654
+ // Add newline between lines (not after last line)
655
+ if (ctx.line_count > 0 and line_info.line_idx < ctx.line_count - 1 and ctx.out_index.* < ctx.out_buffer.len) {
656
+ ctx.out_buffer[ctx.out_index.*] = '\n';
657
+ ctx.out_index.* += 1;
658
+ }
659
+ }
660
+ };
661
+
662
+ var ctx = Context{
663
+ .buffer = self,
664
+ .out_buffer = out_buffer,
665
+ .out_index = &out_index,
666
+ .line_count = line_count,
667
+ };
668
+ self.walkLinesAndSegments(&ctx, Context.segmentCallback, Context.lineEndCallback);
669
+
670
+ return out_index;
671
+ }
672
+
673
+ pub fn startHighlightsTransaction(self: *Self) void {
674
+ self.highlight_batch_depth += 1;
675
+ }
676
+
677
+ pub fn endHighlightsTransaction(self: *Self) void {
678
+ if (self.highlight_batch_depth == 0) return;
679
+
680
+ self.highlight_batch_depth -= 1;
681
+
682
+ if (self.highlight_batch_depth == 0) {
683
+ var it = self.dirty_span_lines.keyIterator();
684
+ while (it.next()) |line_idx| {
685
+ self.rebuildLineSpans(line_idx.*) catch {};
686
+ }
687
+ self.dirty_span_lines.clearRetainingCapacity();
688
+ }
689
+ }
690
+
691
+ fn markLineSpansDirty(self: *Self, line_idx: usize) void {
692
+ self.dirty_span_lines.put(line_idx, {}) catch {};
693
+ }
694
+
695
+ // Highlight system
696
+ fn ensureLineHighlightStorage(self: *Self, line_idx: usize) TextBufferError!void {
697
+ while (self.line_highlights.items.len <= line_idx) {
698
+ try self.line_highlights.append(self.global_allocator, .{});
699
+ }
700
+ while (self.line_spans.items.len <= line_idx) {
701
+ try self.line_spans.append(self.global_allocator, .{});
702
+ }
703
+ }
704
+
705
+ pub fn addHighlight(
706
+ self: *Self,
707
+ line_idx: usize,
708
+ col_start: u32,
709
+ col_end: u32,
710
+ style_id: u32,
711
+ priority: u8,
712
+ hl_ref: u16,
713
+ ) TextBufferError!void {
714
+ const line_count = self.getLineCount();
715
+ if (line_idx >= line_count) {
716
+ return TextBufferError.InvalidIndex;
717
+ }
718
+
719
+ if (col_start >= col_end) {
720
+ return; // Empty range
721
+ }
722
+
723
+ try self.ensureLineHighlightStorage(line_idx);
724
+
725
+ const hl = Highlight{
726
+ .col_start = col_start,
727
+ .col_end = col_end,
728
+ .style_id = style_id,
729
+ .priority = priority,
730
+ .hl_ref = hl_ref,
731
+ };
732
+
733
+ try self.line_highlights.items[line_idx].append(self.global_allocator, hl);
734
+
735
+ if (self.highlight_batch_depth == 0) {
736
+ try self.rebuildLineSpans(line_idx);
737
+ } else {
738
+ self.markLineSpansDirty(line_idx);
739
+ }
740
+ }
741
+
742
+ pub fn getLineHighlights(self: *const Self, line_idx: usize) []const Highlight {
743
+ if (line_idx < self.line_highlights.items.len) {
744
+ return self.line_highlights.items[line_idx].items;
745
+ }
746
+ return &[_]Highlight{};
747
+ }
748
+
749
+ pub fn getLineSpans(self: *const Self, line_idx: usize) []const StyleSpan {
750
+ if (line_idx < self.line_spans.items.len) {
751
+ return self.line_spans.items[line_idx].items;
752
+ }
753
+ return &[_]StyleSpan{};
754
+ }
755
+
756
+ fn rebuildLineSpans(self: *Self, line_idx: usize) TextBufferError!void {
757
+ if (line_idx >= self.line_spans.items.len) {
758
+ return TextBufferError.InvalidIndex;
759
+ }
760
+
761
+ self.line_spans.items[line_idx].clearRetainingCapacity();
762
+
763
+ if (line_idx >= self.line_highlights.items.len or self.line_highlights.items[line_idx].items.len == 0) {
764
+ return; // No highlights
765
+ }
766
+
767
+ const highlights = self.line_highlights.items[line_idx].items;
768
+
769
+ // Collect all boundary columns
770
+ const Event = struct {
771
+ col: u32,
772
+ is_start: bool,
773
+ hl_idx: usize,
774
+ };
775
+
776
+ var events: std.ArrayListUnmanaged(Event) = .{};
777
+ defer events.deinit(self.global_allocator);
778
+
779
+ for (highlights, 0..) |hl, idx| {
780
+ try events.append(self.global_allocator, .{ .col = hl.col_start, .is_start = true, .hl_idx = idx });
781
+ try events.append(self.global_allocator, .{ .col = hl.col_end, .is_start = false, .hl_idx = idx });
782
+ }
783
+
784
+ // Sort by column, ends before starts at same position
785
+ const sortFn = struct {
786
+ fn lessThan(_: void, a: Event, b: Event) bool {
787
+ if (a.col != b.col) return a.col < b.col;
788
+ if (a.is_start != b.is_start) return !a.is_start; // ends before starts
789
+ // If both are same type at same column, use hl_idx for stable sort
790
+ return a.hl_idx < b.hl_idx;
791
+ }
792
+ }.lessThan;
793
+ std.mem.sort(Event, events.items, {}, sortFn);
794
+
795
+ // Build spans by tracking active highlights
796
+ var active = std.AutoHashMap(usize, void).init(self.global_allocator);
797
+ defer active.deinit();
798
+
799
+ var current_col: u32 = 0;
800
+
801
+ for (events.items) |event| {
802
+ // Find current highest priority style before processing event
803
+ var current_priority: i16 = -1;
804
+ var current_style: u32 = 0;
805
+ var it = active.keyIterator();
806
+ while (it.next()) |hl_idx| {
807
+ const hl = highlights[hl_idx.*];
808
+ if (hl.priority > current_priority) {
809
+ current_priority = @intCast(hl.priority);
810
+ current_style = hl.style_id;
811
+ }
812
+ }
813
+
814
+ // Emit span for the segment leading up to this event
815
+ if (event.col > current_col) {
816
+ try self.line_spans.items[line_idx].append(self.global_allocator, StyleSpan{
817
+ .col = current_col,
818
+ .style_id = current_style,
819
+ .next_col = event.col,
820
+ });
821
+ current_col = event.col;
822
+ }
823
+
824
+ // Process event
825
+ if (event.is_start) {
826
+ try active.put(event.hl_idx, {});
827
+ } else {
828
+ _ = active.remove(event.hl_idx);
829
+ }
830
+ }
831
+
832
+ // Emit final span after last event if there were any highlights
833
+ // This ensures the line returns to default styling after the last highlight ends
834
+ if (events.items.len > 0 and active.count() == 0) {
835
+ const line_width = self.lineWidthAt(@intCast(line_idx));
836
+ if (current_col < line_width) {
837
+ try self.line_spans.items[line_idx].append(self.global_allocator, StyleSpan{
838
+ .col = current_col,
839
+ .style_id = 0, // No style (default)
840
+ .next_col = line_width,
841
+ });
842
+ }
843
+ }
844
+ }
845
+
846
+ /// Add highlight by row/col coordinates
847
+ pub fn addHighlightByCoords(
848
+ self: *Self,
849
+ start_row: u32,
850
+ start_col: u32,
851
+ end_row: u32,
852
+ end_col: u32,
853
+ style_id: u32,
854
+ priority: u8,
855
+ hl_ref: u16,
856
+ ) TextBufferError!void {
857
+ const char_start = iter_mod.coordsToOffset(&self._rope, start_row, start_col) orelse return TextBufferError.InvalidIndex;
858
+ const char_end = iter_mod.coordsToOffset(&self._rope, end_row, end_col) orelse return TextBufferError.InvalidIndex;
859
+ return self.addHighlightByCharRange(char_start, char_end, style_id, priority, hl_ref);
860
+ }
861
+
862
+ /// Add highlight by character range
863
+ pub fn addHighlightByCharRange(
864
+ self: *Self,
865
+ char_start: u32,
866
+ char_end: u32,
867
+ style_id: u32,
868
+ priority: u8,
869
+ hl_ref: u16,
870
+ ) TextBufferError!void {
871
+ const line_count = self.getLineCount();
872
+ if (char_start >= char_end or line_count == 0) {
873
+ return;
874
+ }
875
+
876
+ // Walk lines to find which lines this highlight affects
877
+ const Context = struct {
878
+ buffer: *Self,
879
+ char_start: u32,
880
+ char_end: u32,
881
+ style_id: u32,
882
+ priority: u8,
883
+ hl_ref: u16,
884
+ start_line_idx: ?usize = null,
885
+
886
+ fn callback(ctx_ptr: *anyopaque, line_info: LineInfo) void {
887
+ const ctx = @as(*@This(), @ptrCast(@alignCast(ctx_ptr)));
888
+ const line_start_col_offset = line_info.col_offset;
889
+ const line_end_col_offset = line_info.col_offset + line_info.width_cols;
890
+
891
+ // Skip lines before the highlight
892
+ if (line_end_col_offset <= ctx.char_start) return;
893
+ // Stop after the highlight ends
894
+ if (line_start_col_offset >= ctx.char_end) return;
895
+
896
+ // This line overlaps with the highlight
897
+ const col_start = if (ctx.char_start > line_start_col_offset)
898
+ ctx.char_start - line_start_col_offset
899
+ else
900
+ 0;
901
+
902
+ const col_end = if (ctx.char_end < line_end_col_offset)
903
+ ctx.char_end - line_start_col_offset
904
+ else
905
+ line_info.width_cols;
906
+
907
+ ctx.buffer.addHighlight(
908
+ line_info.line_idx,
909
+ col_start,
910
+ col_end,
911
+ ctx.style_id,
912
+ ctx.priority,
913
+ ctx.hl_ref,
914
+ ) catch {};
915
+ }
916
+ };
917
+
918
+ var ctx = Context{
919
+ .buffer = self,
920
+ .char_start = char_start,
921
+ .char_end = char_end,
922
+ .style_id = style_id,
923
+ .priority = priority,
924
+ .hl_ref = hl_ref,
925
+ };
926
+ iter_mod.walkLines(&self._rope, &ctx, Context.callback, false);
927
+ }
928
+
929
+ /// Remove all highlights with a specific reference ID
930
+ pub fn removeHighlightsByRef(self: *Self, hl_ref: u16) void {
931
+ for (self.line_highlights.items, 0..) |*hl_list, line_idx| {
932
+ var i: usize = 0;
933
+ var changed = false;
934
+ while (i < hl_list.items.len) {
935
+ if (hl_list.items[i].hl_ref == hl_ref) {
936
+ _ = hl_list.orderedRemove(i);
937
+ changed = true;
938
+ continue;
939
+ }
940
+ i += 1;
941
+ }
942
+ if (changed) {
943
+ if (self.highlight_batch_depth == 0) {
944
+ self.rebuildLineSpans(line_idx) catch {};
945
+ } else {
946
+ self.markLineSpansDirty(line_idx);
947
+ }
948
+ }
949
+ }
950
+ }
951
+
952
+ /// Clear all highlights from a specific line
953
+ pub fn clearLineHighlights(self: *Self, line_idx: usize) void {
954
+ if (line_idx < self.line_highlights.items.len) {
955
+ self.line_highlights.items[line_idx].clearRetainingCapacity();
956
+ }
957
+ if (line_idx < self.line_spans.items.len) {
958
+ self.line_spans.items[line_idx].clearRetainingCapacity();
959
+ }
960
+ }
961
+
962
+ /// Clear all highlights
963
+ pub fn clearAllHighlights(self: *Self) void {
964
+ for (self.line_highlights.items) |*hl_list| {
965
+ hl_list.clearRetainingCapacity();
966
+ }
967
+ for (self.line_spans.items) |*span_list| {
968
+ span_list.clearRetainingCapacity();
969
+ }
970
+ }
971
+
972
+ /// Get highlights for a specific line
973
+ pub fn getLineHighlightsSlice(self: *const Self, line_idx: usize) []const Highlight {
974
+ if (line_idx < self.line_highlights.items.len) {
975
+ return self.line_highlights.items[line_idx].items;
976
+ }
977
+ return &[_]Highlight{};
978
+ }
979
+
980
+ /// Get total number of highlights across all lines
981
+ pub fn getHighlightCount(self: *const Self) u32 {
982
+ var count: u32 = 0;
983
+ for (self.line_highlights.items) |hl_list| {
984
+ count += @intCast(hl_list.items.len);
985
+ }
986
+ return count;
987
+ }
988
+
989
+ /// Set styled text from chunks with individual styling
990
+ /// Accepts StyledChunk array for FFI compatibility
991
+ /// TODO: This is for backward compatibility, there should be a better way to do this.
992
+ pub fn setStyledText(
993
+ self: *Self,
994
+ chunks: []const StyledChunk,
995
+ ) TextBufferError!void {
996
+ if (chunks.len == 0) {
997
+ self.clear();
998
+ self.clearAllHighlights();
999
+ return;
1000
+ }
1001
+
1002
+ // Calculate total text length
1003
+ var total_len: usize = 0;
1004
+ for (chunks) |chunk| {
1005
+ total_len += chunk.text_len;
1006
+ }
1007
+
1008
+ if (total_len == 0) {
1009
+ self.clear();
1010
+ self.clearAllHighlights();
1011
+ return;
1012
+ }
1013
+
1014
+ self.clear();
1015
+ self.clearAllHighlights();
1016
+
1017
+ _ = self.arena.reset(.retain_capacity);
1018
+
1019
+ self._rope = UnifiedRope.init(self.allocator) catch return TextBufferError.OutOfMemory;
1020
+
1021
+ if (total_len > self.styled_capacity) {
1022
+ if (self.styled_buffer) |old_buf| {
1023
+ self.global_allocator.free(old_buf);
1024
+ }
1025
+ const new_buf = self.global_allocator.alloc(u8, total_len) catch return TextBufferError.OutOfMemory;
1026
+ self.styled_buffer = new_buf;
1027
+ self.styled_capacity = total_len;
1028
+ }
1029
+
1030
+ const full_text = self.styled_buffer.?[0..total_len];
1031
+
1032
+ var offset: usize = 0;
1033
+ for (chunks) |chunk| {
1034
+ if (chunk.text_len > 0) {
1035
+ const chunk_text = chunk.text_ptr[0..chunk.text_len];
1036
+ @memcpy(full_text[offset .. offset + chunk.text_len], chunk_text);
1037
+ offset += chunk.text_len;
1038
+ }
1039
+ }
1040
+
1041
+ if (self.styled_text_mem_id) |mem_id| {
1042
+ try self.mem_registry.replace(mem_id, full_text, false);
1043
+ } else {
1044
+ const mem_id = try self.mem_registry.register(full_text, false);
1045
+ self.styled_text_mem_id = mem_id;
1046
+ }
1047
+
1048
+ try self.setTextInternal(self.styled_text_mem_id.?, full_text);
1049
+
1050
+ if (self.syntax_style) |style| {
1051
+ var seen_link_ids: std.AutoHashMapUnmanaged(u32, void) = .{};
1052
+ defer seen_link_ids.deinit(self.global_allocator);
1053
+
1054
+ self.startHighlightsTransaction();
1055
+ defer self.endHighlightsTransaction();
1056
+
1057
+ var char_pos: u32 = 0;
1058
+ for (chunks, 0..) |chunk, i| {
1059
+ const chunk_text = chunk.text_ptr[0..chunk.text_len];
1060
+ const chunk_len = self.measureText(chunk_text);
1061
+
1062
+ if (chunk_len > 0) {
1063
+ const fg = if (chunk.fg_ptr) |fgPtr| utils.f32PtrToRGBA(fgPtr) else null;
1064
+ const bg = if (chunk.bg_ptr) |bgPtr| utils.f32PtrToRGBA(bgPtr) else null;
1065
+
1066
+ var attributes = chunk.attributes;
1067
+ if (chunk.link_ptr) |link_ptr| {
1068
+ if (chunk.link_len > 0) {
1069
+ const tracker = self.getLinkTracker();
1070
+ const url = link_ptr[0..chunk.link_len];
1071
+ const link_id = tracker.pool.alloc(url) catch 0;
1072
+ if (link_id != 0) {
1073
+ const maybe_seen = seen_link_ids.getOrPut(self.global_allocator, link_id) catch null;
1074
+ const should_track = if (maybe_seen) |seen| !seen.found_existing else true;
1075
+ if (should_track) {
1076
+ tracker.addCellRef(link_id);
1077
+ }
1078
+ attributes = ansi.TextAttributes.setLinkId(attributes, link_id);
1079
+ }
1080
+ }
1081
+ }
1082
+
1083
+ var style_name_buf: [64]u8 = undefined;
1084
+ const style_name = std.fmt.bufPrint(&style_name_buf, "chunk{d}", .{i}) catch continue;
1085
+ const style_id = (@constCast(style)).registerStyle(style_name, fg, bg, attributes) catch continue;
1086
+
1087
+ self.addHighlightByCharRange(char_pos, char_pos + chunk_len, style_id, 1, 0) catch {};
1088
+ }
1089
+
1090
+ char_pos += chunk_len;
1091
+ }
1092
+ }
1093
+ }
1094
+
1095
+ /// Load text from a file path (relative to cwd)
1096
+ /// The file content is allocated in the arena and will be freed when the buffer is destroyed
1097
+ pub fn loadFile(self: *Self, path: []const u8) TextBufferError!void {
1098
+ const file = std.fs.cwd().openFile(path, .{}) catch |err| {
1099
+ return switch (err) {
1100
+ error.FileNotFound => TextBufferError.InvalidIndex,
1101
+ error.AccessDenied => TextBufferError.InvalidIndex,
1102
+ else => TextBufferError.OutOfMemory,
1103
+ };
1104
+ };
1105
+ defer file.close();
1106
+
1107
+ const file_size = file.getEndPos() catch return TextBufferError.OutOfMemory;
1108
+
1109
+ self.clear();
1110
+
1111
+ const content = self.allocator.alloc(u8, file_size) catch return TextBufferError.OutOfMemory;
1112
+ const bytes_read = file.readAll(content) catch return TextBufferError.OutOfMemory;
1113
+ const text = content[0..bytes_read];
1114
+ const mem_id = try self.mem_registry.register(text, false);
1115
+
1116
+ try self.setTextInternal(mem_id, text);
1117
+ }
1118
+
1119
+ pub fn getTabWidth(self: *const Self) u8 {
1120
+ return self.tabWidth();
1121
+ }
1122
+
1123
+ /// Set tab width, rounding up to nearest multiple of 2 (minimum 2).
1124
+ /// Marks all views dirty if the width actually changes, since tab width
1125
+ /// affects measured line widths and virtual line calculations.
1126
+ pub fn setTabWidth(self: *Self, width: u8) void {
1127
+ const clamped_width = @max(2, width);
1128
+ const new_width = if (clamped_width % 2 == 0) clamped_width else clamped_width + 1;
1129
+ if (self.tab_width == new_width) return;
1130
+ self.tab_width = new_width;
1131
+ self.markAllViewsDirty();
1132
+ }
1133
+
1134
+ /// Debug log the rope structure using rope.toText
1135
+ pub fn debugLogRope(self: *const Self) void {
1136
+ logger.debug("=== TextBuffer Rope Debug ===", .{});
1137
+ logger.debug("Line count: {}", .{self.getLineCount()});
1138
+ logger.debug("Char count: {}", .{self.getLength()});
1139
+ logger.debug("Byte size: {}", .{self.getByteSize()});
1140
+
1141
+ const rope_text = self._rope.toText(self.allocator) catch {
1142
+ logger.debug("Failed to generate rope text representation", .{});
1143
+ return;
1144
+ };
1145
+ logger.debug("Rope structure: {s}", .{rope_text});
1146
+ logger.debug("=== End Rope Debug ===", .{});
1147
+ }
1148
+
1149
+ /// Get text within a range of display-width offsets
1150
+ /// Automatically snaps to grapheme boundaries:
1151
+ /// Returns number of bytes written to out_buffer
1152
+ pub fn getTextRange(self: *const Self, start_offset: u32, end_offset: u32, out_buffer: []u8) usize {
1153
+ if (start_offset >= end_offset) return 0;
1154
+ if (out_buffer.len == 0) return 0;
1155
+
1156
+ const total_weight = self._rope.totalWeight();
1157
+ if (start_offset >= total_weight) return 0;
1158
+
1159
+ const clamped_end = @min(end_offset, total_weight);
1160
+
1161
+ return iter_mod.extractTextBetweenOffsets(
1162
+ &self._rope,
1163
+ &self.mem_registry,
1164
+ self.tab_width,
1165
+ start_offset,
1166
+ clamped_end,
1167
+ out_buffer,
1168
+ self.width_method,
1169
+ );
1170
+ }
1171
+
1172
+ /// Get text within a range specified by row/col coordinates
1173
+ /// Automatically snaps to grapheme boundaries:
1174
+ /// Returns number of bytes written to out_buffer
1175
+ pub fn getTextRangeByCoords(self: *Self, start_row: u32, start_col: u32, end_row: u32, end_col: u32, out_buffer: []u8) usize {
1176
+ const start_offset = iter_mod.coordsToOffset(&self._rope, start_row, start_col) orelse return 0;
1177
+ const end_offset = iter_mod.coordsToOffset(&self._rope, end_row, end_col) orelse return 0;
1178
+ return self.getTextRange(start_offset, end_offset, out_buffer);
1179
+ }
1180
+ };