@native-sdk/cli 0.0.0 → 0.4.0

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 (367) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +50 -1
  3. package/app.zon +30 -0
  4. package/bin/native.js +156 -0
  5. package/build/app.zig +716 -0
  6. package/build.zig +2593 -0
  7. package/build.zig.zon +24 -0
  8. package/native-sdk.d.ts +615 -0
  9. package/package.json +44 -8
  10. package/skill-data/automation/SKILL.md +248 -0
  11. package/skill-data/core/SKILL.md +249 -0
  12. package/skill-data/core/references/app-model-runtime.md +141 -0
  13. package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
  14. package/skill-data/core/references/frontend-assets.md +124 -0
  15. package/skill-data/core/references/project-anatomy.md +108 -0
  16. package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
  17. package/skill-data/native-ui/SKILL.md +1080 -0
  18. package/skills/native-sdk/SKILL.md +33 -0
  19. package/src/app_runner/root.zig +740 -0
  20. package/src/assets/root.zig +20 -0
  21. package/src/automation/protocol.zig +285 -0
  22. package/src/automation/root.zig +13 -0
  23. package/src/automation/server.zig +506 -0
  24. package/src/automation/snapshot.zig +1202 -0
  25. package/src/automation/watcher.zig +165 -0
  26. package/src/bridge/root.zig +507 -0
  27. package/src/debug/root.zig +246 -0
  28. package/src/embed/app_exports.zig +11 -0
  29. package/src/embed/c_api.zig +793 -0
  30. package/src/embed/c_exports.zig +10 -0
  31. package/src/embed/chrome.zig +168 -0
  32. package/src/embed/conversions.zig +359 -0
  33. package/src/embed/host.zig +960 -0
  34. package/src/embed/root.zig +81 -0
  35. package/src/embed/tests.zig +2552 -0
  36. package/src/embed/types.zig +382 -0
  37. package/src/embed/ui_host.zig +289 -0
  38. package/src/extensions/root.zig +197 -0
  39. package/src/frontend/root.zig +48 -0
  40. package/src/js/root.zig +65 -0
  41. package/src/platform/android/NativeSdkActivity.java +1370 -0
  42. package/src/platform/android/android_host.c +696 -0
  43. package/src/platform/android/files.zig +9 -0
  44. package/src/platform/android/native_sdk_app.h +237 -0
  45. package/src/platform/ios/files.zig +8 -0
  46. package/src/platform/ios/native_sdk_app.h +289 -0
  47. package/src/platform/ios/uikit_host.m +2739 -0
  48. package/src/platform/linux/cef_host.cpp +599 -0
  49. package/src/platform/linux/gtk_host.c +5362 -0
  50. package/src/platform/linux/gtk_host.h +265 -0
  51. package/src/platform/linux/root.zig +1519 -0
  52. package/src/platform/macos/appkit_host.h +604 -0
  53. package/src/platform/macos/appkit_host.m +10392 -0
  54. package/src/platform/macos/automation_host.h +14 -0
  55. package/src/platform/macos/automation_host.m +12 -0
  56. package/src/platform/macos/cef_host.mm +3028 -0
  57. package/src/platform/macos/root.zig +2260 -0
  58. package/src/platform/null_platform.zig +2232 -0
  59. package/src/platform/null_platform_tests.zig +670 -0
  60. package/src/platform/policy_values.zig +14 -0
  61. package/src/platform/root.zig +181 -0
  62. package/src/platform/types.zig +2681 -0
  63. package/src/platform/windows/cef_host.cpp +3 -0
  64. package/src/platform/windows/root.zig +1521 -0
  65. package/src/platform/windows/webview2_host.cpp +6230 -0
  66. package/src/primitives/app_dirs/root.zig +440 -0
  67. package/src/primitives/app_manifest/root.zig +109 -0
  68. package/src/primitives/app_manifest/tests.zig +758 -0
  69. package/src/primitives/app_manifest/types.zig +587 -0
  70. package/src/primitives/app_manifest/validation.zig +828 -0
  71. package/src/primitives/assets/root.zig +520 -0
  72. package/src/primitives/canvas/a11y_audit.zig +390 -0
  73. package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
  74. package/src/primitives/canvas/app_icon.zig +1590 -0
  75. package/src/primitives/canvas/chart.zig +392 -0
  76. package/src/primitives/canvas/chart_tests.zig +751 -0
  77. package/src/primitives/canvas/commands.zig +446 -0
  78. package/src/primitives/canvas/drawing.zig +309 -0
  79. package/src/primitives/canvas/equality.zig +316 -0
  80. package/src/primitives/canvas/events.zig +696 -0
  81. package/src/primitives/canvas/font_coverage.zig +118 -0
  82. package/src/primitives/canvas/font_ttf.zig +510 -0
  83. package/src/primitives/canvas/font_ttf_tests.zig +280 -0
  84. package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
  85. package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
  86. package/src/primitives/canvas/frame.zig +679 -0
  87. package/src/primitives/canvas/frame_metrics.zig +462 -0
  88. package/src/primitives/canvas/gpu.zig +639 -0
  89. package/src/primitives/canvas/hash.zig +112 -0
  90. package/src/primitives/canvas/icons/alert.svg +1 -0
  91. package/src/primitives/canvas/icons/archive.svg +1 -0
  92. package/src/primitives/canvas/icons/arrow-down.svg +1 -0
  93. package/src/primitives/canvas/icons/arrow-right.svg +1 -0
  94. package/src/primitives/canvas/icons/arrow-up.svg +1 -0
  95. package/src/primitives/canvas/icons/check-circle.svg +1 -0
  96. package/src/primitives/canvas/icons/check.svg +1 -0
  97. package/src/primitives/canvas/icons/chevron-down.svg +1 -0
  98. package/src/primitives/canvas/icons/chevron-left.svg +1 -0
  99. package/src/primitives/canvas/icons/chevron-right.svg +1 -0
  100. package/src/primitives/canvas/icons/chevron-up.svg +1 -0
  101. package/src/primitives/canvas/icons/circle-dot.svg +1 -0
  102. package/src/primitives/canvas/icons/clock.svg +1 -0
  103. package/src/primitives/canvas/icons/copy.svg +1 -0
  104. package/src/primitives/canvas/icons/download.svg +1 -0
  105. package/src/primitives/canvas/icons/edit.svg +1 -0
  106. package/src/primitives/canvas/icons/ellipsis.svg +1 -0
  107. package/src/primitives/canvas/icons/external-link.svg +1 -0
  108. package/src/primitives/canvas/icons/eye.svg +1 -0
  109. package/src/primitives/canvas/icons/file-text.svg +1 -0
  110. package/src/primitives/canvas/icons/folder-open.svg +1 -0
  111. package/src/primitives/canvas/icons/folder.svg +1 -0
  112. package/src/primitives/canvas/icons/git-branch.svg +1 -0
  113. package/src/primitives/canvas/icons/git-merge.svg +1 -0
  114. package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
  115. package/src/primitives/canvas/icons/info.svg +1 -0
  116. package/src/primitives/canvas/icons/menu.svg +1 -0
  117. package/src/primitives/canvas/icons/missing.svg +1 -0
  118. package/src/primitives/canvas/icons/moon.svg +1 -0
  119. package/src/primitives/canvas/icons/music.svg +1 -0
  120. package/src/primitives/canvas/icons/panel-left.svg +1 -0
  121. package/src/primitives/canvas/icons/panel-right.svg +1 -0
  122. package/src/primitives/canvas/icons/pause.svg +1 -0
  123. package/src/primitives/canvas/icons/play.svg +1 -0
  124. package/src/primitives/canvas/icons/plus.svg +1 -0
  125. package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
  126. package/src/primitives/canvas/icons/repeat.svg +1 -0
  127. package/src/primitives/canvas/icons/save.svg +1 -0
  128. package/src/primitives/canvas/icons/search.svg +1 -0
  129. package/src/primitives/canvas/icons/send.svg +1 -0
  130. package/src/primitives/canvas/icons/settings.svg +1 -0
  131. package/src/primitives/canvas/icons/shuffle.svg +1 -0
  132. package/src/primitives/canvas/icons/skip-back.svg +1 -0
  133. package/src/primitives/canvas/icons/skip-forward.svg +1 -0
  134. package/src/primitives/canvas/icons/sun.svg +1 -0
  135. package/src/primitives/canvas/icons/terminal.svg +1 -0
  136. package/src/primitives/canvas/icons/trash.svg +1 -0
  137. package/src/primitives/canvas/icons/volume.svg +1 -0
  138. package/src/primitives/canvas/icons/wrench.svg +1 -0
  139. package/src/primitives/canvas/icons/x-circle.svg +1 -0
  140. package/src/primitives/canvas/icons/x.svg +1 -0
  141. package/src/primitives/canvas/icons.zig +249 -0
  142. package/src/primitives/canvas/layout_audit.zig +744 -0
  143. package/src/primitives/canvas/layout_audit_tests.zig +426 -0
  144. package/src/primitives/canvas/markdown.zig +1031 -0
  145. package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
  146. package/src/primitives/canvas/markdown_tests.zig +538 -0
  147. package/src/primitives/canvas/plan_key_index.zig +120 -0
  148. package/src/primitives/canvas/png.zig +355 -0
  149. package/src/primitives/canvas/reference.zig +1457 -0
  150. package/src/primitives/canvas/reference_blur.zig +104 -0
  151. package/src/primitives/canvas/reference_memo.zig +322 -0
  152. package/src/primitives/canvas/reference_paths.zig +184 -0
  153. package/src/primitives/canvas/reference_tests.zig +1650 -0
  154. package/src/primitives/canvas/render.zig +747 -0
  155. package/src/primitives/canvas/render_effects.zig +255 -0
  156. package/src/primitives/canvas/render_fingerprints.zig +256 -0
  157. package/src/primitives/canvas/render_generic_resources.zig +376 -0
  158. package/src/primitives/canvas/render_images.zig +247 -0
  159. package/src/primitives/canvas/render_layers.zig +260 -0
  160. package/src/primitives/canvas/render_paths.zig +347 -0
  161. package/src/primitives/canvas/render_resources.zig +51 -0
  162. package/src/primitives/canvas/render_tests.zig +3186 -0
  163. package/src/primitives/canvas/root.zig +744 -0
  164. package/src/primitives/canvas/serialization.zig +1528 -0
  165. package/src/primitives/canvas/svg_icon.zig +636 -0
  166. package/src/primitives/canvas/svg_icon_tests.zig +312 -0
  167. package/src/primitives/canvas/test_support.zig +422 -0
  168. package/src/primitives/canvas/testdata/markdown_document.md +83 -0
  169. package/src/primitives/canvas/tests.zig +30 -0
  170. package/src/primitives/canvas/text.zig +82 -0
  171. package/src/primitives/canvas/text_atlas.zig +408 -0
  172. package/src/primitives/canvas/text_batch_tests.zig +431 -0
  173. package/src/primitives/canvas/text_interaction.zig +615 -0
  174. package/src/primitives/canvas/text_layout.zig +1069 -0
  175. package/src/primitives/canvas/text_layout_cache.zig +281 -0
  176. package/src/primitives/canvas/text_layout_hash.zig +113 -0
  177. package/src/primitives/canvas/text_layout_types.zig +128 -0
  178. package/src/primitives/canvas/text_measure_cache.zig +279 -0
  179. package/src/primitives/canvas/text_metrics.zig +309 -0
  180. package/src/primitives/canvas/text_metrics_tests.zig +262 -0
  181. package/src/primitives/canvas/text_span_tests.zig +608 -0
  182. package/src/primitives/canvas/text_spans.zig +1010 -0
  183. package/src/primitives/canvas/text_tests.zig +1949 -0
  184. package/src/primitives/canvas/themes/geist.zig +470 -0
  185. package/src/primitives/canvas/tokens.zig +1522 -0
  186. package/src/primitives/canvas/ui.zig +2686 -0
  187. package/src/primitives/canvas/ui_markup.zig +3791 -0
  188. package/src/primitives/canvas/ui_markup_binary.zig +522 -0
  189. package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
  190. package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
  191. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
  192. package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
  193. package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
  194. package/src/primitives/canvas/ui_markup_edit.zig +416 -0
  195. package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
  196. package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
  197. package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
  198. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
  199. package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
  200. package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
  201. package/src/primitives/canvas/ui_markup_view.zig +2352 -0
  202. package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
  203. package/src/primitives/canvas/ui_provenance.zig +96 -0
  204. package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
  205. package/src/primitives/canvas/ui_schema.zig +825 -0
  206. package/src/primitives/canvas/ui_schema_tests.zig +128 -0
  207. package/src/primitives/canvas/ui_tests.zig +1432 -0
  208. package/src/primitives/canvas/vector.zig +919 -0
  209. package/src/primitives/canvas/vector_tests.zig +547 -0
  210. package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
  211. package/src/primitives/canvas/virtual_extents.zig +540 -0
  212. package/src/primitives/canvas/widget_access.zig +208 -0
  213. package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
  214. package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
  215. package/src/primitives/canvas/widget_invalidation.zig +748 -0
  216. package/src/primitives/canvas/widget_layout.zig +2055 -0
  217. package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
  218. package/src/primitives/canvas/widget_metrics.zig +228 -0
  219. package/src/primitives/canvas/widget_render.zig +2534 -0
  220. package/src/primitives/canvas/widget_render_controls.zig +1563 -0
  221. package/src/primitives/canvas/widget_render_scroll.zig +136 -0
  222. package/src/primitives/canvas/widget_render_style.zig +626 -0
  223. package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
  224. package/src/primitives/canvas/widget_routing.zig +475 -0
  225. package/src/primitives/canvas/widget_runtime.zig +307 -0
  226. package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
  227. package/src/primitives/canvas/widget_semantics.zig +480 -0
  228. package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
  229. package/src/primitives/canvas/widget_tests.zig +8 -0
  230. package/src/primitives/canvas/widget_text_input.zig +351 -0
  231. package/src/primitives/canvas/widget_text_select.zig +147 -0
  232. package/src/primitives/canvas/widget_tree.zig +232 -0
  233. package/src/primitives/canvas/widgets.zig +1319 -0
  234. package/src/primitives/diagnostics/root.zig +445 -0
  235. package/src/primitives/geometry/root.zig +889 -0
  236. package/src/primitives/json/root.zig +272 -0
  237. package/src/primitives/platform_info/root.zig +450 -0
  238. package/src/primitives/trace/root.zig +569 -0
  239. package/src/root.zig +165 -0
  240. package/src/runtime/api.zig +431 -0
  241. package/src/runtime/async_bridge.zig +49 -0
  242. package/src/runtime/automation_commands.zig +686 -0
  243. package/src/runtime/automation_liveness_tests.zig +140 -0
  244. package/src/runtime/automation_snapshot.zig +246 -0
  245. package/src/runtime/automation_widget_dispatch.zig +617 -0
  246. package/src/runtime/bridge_dispatch_tests.zig +202 -0
  247. package/src/runtime/bridge_payload.zig +194 -0
  248. package/src/runtime/bridge_permission_tests.zig +551 -0
  249. package/src/runtime/bridge_responses.zig +414 -0
  250. package/src/runtime/builtin_bridge.zig +554 -0
  251. package/src/runtime/canvas_budget_edge_tests.zig +415 -0
  252. package/src/runtime/canvas_font_tests.zig +410 -0
  253. package/src/runtime/canvas_fonts.zig +229 -0
  254. package/src/runtime/canvas_frame.zig +1734 -0
  255. package/src/runtime/canvas_frame_animation_tests.zig +609 -0
  256. package/src/runtime/canvas_frame_cache_tests.zig +333 -0
  257. package/src/runtime/canvas_frame_helpers.zig +414 -0
  258. package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
  259. package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
  260. package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
  261. package/src/runtime/canvas_frame_retained_tests.zig +753 -0
  262. package/src/runtime/canvas_frame_tests.zig +9 -0
  263. package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
  264. package/src/runtime/canvas_image_tests.zig +362 -0
  265. package/src/runtime/canvas_images.zig +224 -0
  266. package/src/runtime/canvas_limits.zig +317 -0
  267. package/src/runtime/canvas_screenshot_tests.zig +289 -0
  268. package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
  269. package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
  270. package/src/runtime/canvas_widget_chart_tests.zig +249 -0
  271. package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
  272. package/src/runtime/canvas_widget_context_menu.zig +318 -0
  273. package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
  274. package/src/runtime/canvas_widget_control_tests.zig +2164 -0
  275. package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
  276. package/src/runtime/canvas_widget_display.zig +677 -0
  277. package/src/runtime/canvas_widget_event_tests.zig +1779 -0
  278. package/src/runtime/canvas_widget_events.zig +1098 -0
  279. package/src/runtime/canvas_widget_floating_tests.zig +695 -0
  280. package/src/runtime/canvas_widget_layout_tests.zig +26 -0
  281. package/src/runtime/canvas_widget_runtime.zig +1335 -0
  282. package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
  283. package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
  284. package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
  285. package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
  286. package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
  287. package/src/runtime/canvas_widget_state.zig +798 -0
  288. package/src/runtime/canvas_widget_text_tests.zig +2104 -0
  289. package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
  290. package/src/runtime/clock.zig +231 -0
  291. package/src/runtime/core.zig +927 -0
  292. package/src/runtime/effects.zig +4197 -0
  293. package/src/runtime/effects_audio_tests.zig +690 -0
  294. package/src/runtime/effects_clipboard_tests.zig +317 -0
  295. package/src/runtime/effects_fetch_tests.zig +1031 -0
  296. package/src/runtime/effects_file_tests.zig +407 -0
  297. package/src/runtime/effects_tests.zig +1251 -0
  298. package/src/runtime/flow.zig +1175 -0
  299. package/src/runtime/frame_profile.zig +221 -0
  300. package/src/runtime/gpu_surface_events.zig +639 -0
  301. package/src/runtime/launch_timing.zig +59 -0
  302. package/src/runtime/markdown_app_tests.zig +224 -0
  303. package/src/runtime/native_command_event_tests.zig +431 -0
  304. package/src/runtime/platform_bridge_tests.zig +8 -0
  305. package/src/runtime/platform_config_tests.zig +392 -0
  306. package/src/runtime/root.zig +150 -0
  307. package/src/runtime/runtime_event_tests.zig +1079 -0
  308. package/src/runtime/session_journal.zig +1522 -0
  309. package/src/runtime/session_record.zig +332 -0
  310. package/src/runtime/session_replay.zig +274 -0
  311. package/src/runtime/session_state.zig +30 -0
  312. package/src/runtime/session_tests.zig +399 -0
  313. package/src/runtime/shell_layout.zig +472 -0
  314. package/src/runtime/shell_layout_tests.zig +1246 -0
  315. package/src/runtime/shell_view_tests.zig +604 -0
  316. package/src/runtime/state.zig +110 -0
  317. package/src/runtime/system_services.zig +392 -0
  318. package/src/runtime/test_support.zig +79 -0
  319. package/src/runtime/tests.zig +30 -0
  320. package/src/runtime/ui_app.zig +3290 -0
  321. package/src/runtime/ui_app_huge_model_tests.zig +351 -0
  322. package/src/runtime/ui_app_provenance.zig +370 -0
  323. package/src/runtime/ui_app_tests.zig +3254 -0
  324. package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
  325. package/src/runtime/ui_app_window_tests.zig +442 -0
  326. package/src/runtime/validation.zig +206 -0
  327. package/src/runtime/view.zig +908 -0
  328. package/src/runtime/view_canvas.zig +850 -0
  329. package/src/runtime/view_widget_control.zig +433 -0
  330. package/src/runtime/view_widget_scroll.zig +360 -0
  331. package/src/runtime/view_widget_text.zig +275 -0
  332. package/src/runtime/view_widget_tree.zig +843 -0
  333. package/src/runtime/webview_view_bridge_tests.zig +664 -0
  334. package/src/runtime/widget_bridge.zig +231 -0
  335. package/src/runtime/window_command_bridge_tests.zig +403 -0
  336. package/src/runtime/window_storage.zig +255 -0
  337. package/src/runtime/window_view_runtime.zig +727 -0
  338. package/src/runtime/window_views.zig +334 -0
  339. package/src/security/root.zig +118 -0
  340. package/src/tooling/android.zig +1172 -0
  341. package/src/tooling/assets.zig +128 -0
  342. package/src/tooling/buildgraph.zig +379 -0
  343. package/src/tooling/cef.zig +671 -0
  344. package/src/tooling/codesign.zig +172 -0
  345. package/src/tooling/components/identity_tests.zig +185 -0
  346. package/src/tooling/components/stepper.zig +113 -0
  347. package/src/tooling/components/timeline.native +27 -0
  348. package/src/tooling/components/timeline_item.zig +131 -0
  349. package/src/tooling/default_icon.icns +0 -0
  350. package/src/tooling/default_icon.png +0 -0
  351. package/src/tooling/dev.zig +172 -0
  352. package/src/tooling/doctor.zig +349 -0
  353. package/src/tooling/eject_components.zig +170 -0
  354. package/src/tooling/embedlib.zig +290 -0
  355. package/src/tooling/ios.zig +528 -0
  356. package/src/tooling/manifest.zig +1939 -0
  357. package/src/tooling/package.zig +2388 -0
  358. package/src/tooling/process_tree.zig +114 -0
  359. package/src/tooling/raw_manifest.zig +193 -0
  360. package/src/tooling/root.zig +22 -0
  361. package/src/tooling/templates.zig +3191 -0
  362. package/src/tooling/toolchain.zig +371 -0
  363. package/src/tooling/verbs.zig +204 -0
  364. package/src/tooling/web_engine.zig +135 -0
  365. package/src/tooling/xcodeproj.zig +462 -0
  366. package/src/window_state/root.zig +507 -0
  367. package/index.js +0 -2
@@ -0,0 +1,615 @@
1
+ const std = @import("std");
2
+ const geometry = @import("geometry");
3
+
4
+ const Error = error{
5
+ TextEditBufferTooSmall,
6
+ };
7
+
8
+ pub const TextRange = struct {
9
+ start: usize = 0,
10
+ end: usize = 0,
11
+
12
+ pub fn init(start: usize, end: usize) TextRange {
13
+ return .{ .start = start, .end = end };
14
+ }
15
+
16
+ pub fn normalized(self: TextRange, text_len: usize) TextRange {
17
+ const start = @min(self.start, text_len);
18
+ const end = @min(self.end, text_len);
19
+ return if (start <= end)
20
+ .{ .start = start, .end = end }
21
+ else
22
+ .{ .start = end, .end = start };
23
+ }
24
+
25
+ pub fn byteLen(self: TextRange, text_len: usize) usize {
26
+ const range = self.normalized(text_len);
27
+ return range.end - range.start;
28
+ }
29
+
30
+ pub fn isCollapsed(self: TextRange, text_len: usize) bool {
31
+ const range = self.normalized(text_len);
32
+ return range.start == range.end;
33
+ }
34
+ };
35
+
36
+ pub const TextSelectionRect = struct {
37
+ range: TextRange = .{},
38
+ rect: geometry.RectF = .{},
39
+ };
40
+
41
+ pub const TextSelection = struct {
42
+ anchor: usize = 0,
43
+ focus: usize = 0,
44
+
45
+ pub fn collapsed(offset: usize) TextSelection {
46
+ return .{ .anchor = offset, .focus = offset };
47
+ }
48
+
49
+ pub fn range(self: TextSelection, text_len: usize) TextRange {
50
+ return TextRange.init(self.anchor, self.focus).normalized(text_len);
51
+ }
52
+
53
+ pub fn isCollapsed(self: TextSelection, text_len: usize) bool {
54
+ return self.range(text_len).isCollapsed(text_len);
55
+ }
56
+ };
57
+
58
+ pub const TextCaretDirection = enum {
59
+ previous,
60
+ next,
61
+ previous_word,
62
+ next_word,
63
+ start,
64
+ end,
65
+ };
66
+
67
+ pub const TextCaretMove = struct {
68
+ direction: TextCaretDirection,
69
+ extend: bool = false,
70
+ };
71
+
72
+ pub const TextCompositionUpdate = struct {
73
+ text: []const u8 = "",
74
+ cursor: ?usize = null,
75
+ };
76
+
77
+ pub const TextInputEvent = union(enum) {
78
+ insert_text: []const u8,
79
+ delete_backward,
80
+ delete_forward,
81
+ delete_word_backward,
82
+ delete_word_forward,
83
+ clear,
84
+ move_caret: TextCaretMove,
85
+ set_selection: TextSelection,
86
+ set_composition: TextCompositionUpdate,
87
+ commit_composition,
88
+ cancel_composition,
89
+ };
90
+
91
+ pub const TextEditState = struct {
92
+ text: []const u8 = "",
93
+ selection: TextSelection = .{},
94
+ composition: ?TextRange = null,
95
+
96
+ pub fn init(text: []const u8) TextEditState {
97
+ return .{
98
+ .text = text,
99
+ .selection = TextSelection.collapsed(text.len),
100
+ };
101
+ }
102
+
103
+ pub fn apply(self: TextEditState, event: TextInputEvent, output: []u8) Error!TextEditState {
104
+ return applyTextInputEvent(self, event, output);
105
+ }
106
+ };
107
+
108
+ pub fn applyTextInputEvent(state: TextEditState, event: TextInputEvent, output: []u8) Error!TextEditState {
109
+ const normalized = normalizeTextEditState(state);
110
+ return switch (event) {
111
+ .insert_text => |text| replaceTextEditRange(normalized, activeTextReplaceRange(normalized), text, output, null, text.len),
112
+ .delete_backward => deleteBackwardTextEdit(normalized, output),
113
+ .delete_forward => deleteForwardTextEdit(normalized, output),
114
+ .delete_word_backward => deleteWordBackwardTextEdit(normalized, output),
115
+ .delete_word_forward => deleteWordForwardTextEdit(normalized, output),
116
+ .clear => .{
117
+ .text = "",
118
+ .selection = TextSelection.collapsed(0),
119
+ .composition = null,
120
+ },
121
+ .move_caret => |move| moveTextCaret(normalized, move),
122
+ .set_selection => |selection| .{
123
+ .text = normalized.text,
124
+ .selection = snapTextSelection(normalized.text, selection),
125
+ .composition = null,
126
+ },
127
+ .set_composition => |composition| setTextComposition(normalized, composition, output),
128
+ .commit_composition => .{
129
+ .text = normalized.text,
130
+ .selection = normalized.selection,
131
+ .composition = null,
132
+ },
133
+ .cancel_composition => cancelTextComposition(normalized, output),
134
+ };
135
+ }
136
+
137
+ const TextReplaceResult = struct {
138
+ text: []const u8,
139
+ inserted_range: TextRange,
140
+ };
141
+
142
+ fn normalizeTextEditState(state: TextEditState) TextEditState {
143
+ return .{
144
+ .text = state.text,
145
+ .selection = snapTextSelection(state.text, state.selection),
146
+ .composition = if (state.composition) |range| snapTextRange(state.text, range) else null,
147
+ };
148
+ }
149
+
150
+ fn activeTextReplaceRange(state: TextEditState) TextRange {
151
+ if (state.composition) |range| return snapTextRange(state.text, range);
152
+ return state.selection.range(state.text.len);
153
+ }
154
+
155
+ fn replaceTextEditRange(
156
+ state: TextEditState,
157
+ range: TextRange,
158
+ replacement: []const u8,
159
+ output: []u8,
160
+ composition: ?TextRange,
161
+ cursor_offset: usize,
162
+ ) Error!TextEditState {
163
+ const result = try replaceTextRange(state.text, range, replacement, output);
164
+ const cursor = snapTextOffset(result.text, result.inserted_range.start + @min(cursor_offset, replacement.len));
165
+ return .{
166
+ .text = result.text,
167
+ .selection = TextSelection.collapsed(cursor),
168
+ .composition = composition,
169
+ };
170
+ }
171
+
172
+ fn setTextComposition(state: TextEditState, composition: TextCompositionUpdate, output: []u8) Error!TextEditState {
173
+ const range = activeTextReplaceRange(state);
174
+ const cursor = snapTextOffset(composition.text, composition.cursor orelse composition.text.len);
175
+ const result = try replaceTextRange(state.text, range, composition.text, output);
176
+ const absolute_cursor = snapTextOffset(result.text, result.inserted_range.start + cursor);
177
+ return .{
178
+ .text = result.text,
179
+ .selection = TextSelection.collapsed(absolute_cursor),
180
+ .composition = result.inserted_range,
181
+ };
182
+ }
183
+
184
+ fn cancelTextComposition(state: TextEditState, output: []u8) Error!TextEditState {
185
+ const composition = state.composition orelse return state;
186
+ const range = snapTextRange(state.text, composition);
187
+ const result = try replaceTextRange(state.text, range, "", output);
188
+ return .{
189
+ .text = result.text,
190
+ .selection = TextSelection.collapsed(result.inserted_range.start),
191
+ .composition = null,
192
+ };
193
+ }
194
+
195
+ fn deleteBackwardTextEdit(state: TextEditState, output: []u8) Error!TextEditState {
196
+ const range = activeTextReplaceRange(state);
197
+ if (!range.isCollapsed(state.text.len)) return replaceTextEditRange(state, range, "", output, null, 0);
198
+
199
+ const caret = snapTextOffset(state.text, state.selection.focus);
200
+ if (caret == 0) return .{ .text = state.text, .selection = TextSelection.collapsed(0), .composition = null };
201
+ return replaceTextEditRange(state, TextRange.init(previousTextOffset(state.text, caret), caret), "", output, null, 0);
202
+ }
203
+
204
+ fn deleteForwardTextEdit(state: TextEditState, output: []u8) Error!TextEditState {
205
+ const range = activeTextReplaceRange(state);
206
+ if (!range.isCollapsed(state.text.len)) return replaceTextEditRange(state, range, "", output, null, 0);
207
+
208
+ const caret = snapTextOffset(state.text, state.selection.focus);
209
+ if (caret >= state.text.len) return .{ .text = state.text, .selection = TextSelection.collapsed(state.text.len), .composition = null };
210
+ return replaceTextEditRange(state, TextRange.init(caret, nextTextOffset(state.text, caret)), "", output, null, 0);
211
+ }
212
+
213
+ fn deleteWordBackwardTextEdit(state: TextEditState, output: []u8) Error!TextEditState {
214
+ const range = activeTextReplaceRange(state);
215
+ if (!range.isCollapsed(state.text.len)) return replaceTextEditRange(state, range, "", output, null, 0);
216
+
217
+ const caret = snapTextOffset(state.text, state.selection.focus);
218
+ if (caret == 0) return .{ .text = state.text, .selection = TextSelection.collapsed(0), .composition = null };
219
+ return replaceTextEditRange(state, TextRange.init(previousTextWordOffset(state.text, caret), caret), "", output, null, 0);
220
+ }
221
+
222
+ fn deleteWordForwardTextEdit(state: TextEditState, output: []u8) Error!TextEditState {
223
+ const range = activeTextReplaceRange(state);
224
+ if (!range.isCollapsed(state.text.len)) return replaceTextEditRange(state, range, "", output, null, 0);
225
+
226
+ const caret = snapTextOffset(state.text, state.selection.focus);
227
+ if (caret >= state.text.len) return .{ .text = state.text, .selection = TextSelection.collapsed(state.text.len), .composition = null };
228
+ return replaceTextEditRange(state, TextRange.init(caret, nextTextWordOffset(state.text, caret)), "", output, null, 0);
229
+ }
230
+
231
+ fn moveTextCaret(state: TextEditState, move: TextCaretMove) TextEditState {
232
+ const range = state.selection.range(state.text.len);
233
+ const focus = snapTextOffset(state.text, state.selection.focus);
234
+ const target = switch (move.direction) {
235
+ .previous => if (!move.extend and !range.isCollapsed(state.text.len)) range.start else previousTextOffset(state.text, focus),
236
+ .next => if (!move.extend and !range.isCollapsed(state.text.len)) range.end else nextTextOffset(state.text, focus),
237
+ .previous_word => if (!move.extend and !range.isCollapsed(state.text.len)) range.start else previousTextWordOffset(state.text, focus),
238
+ .next_word => if (!move.extend and !range.isCollapsed(state.text.len)) range.end else nextTextWordOffset(state.text, focus),
239
+ .start => 0,
240
+ .end => state.text.len,
241
+ };
242
+ const selection = if (move.extend)
243
+ TextSelection{ .anchor = state.selection.anchor, .focus = target }
244
+ else
245
+ TextSelection.collapsed(target);
246
+ return .{
247
+ .text = state.text,
248
+ .selection = snapTextSelection(state.text, selection),
249
+ .composition = null,
250
+ };
251
+ }
252
+
253
+ fn replaceTextRange(source: []const u8, range: TextRange, replacement: []const u8, output: []u8) Error!TextReplaceResult {
254
+ const snapped = snapTextRange(source, range);
255
+ const prefix_len = snapped.start;
256
+ const suffix = source[snapped.end..];
257
+ const suffix_start = prefix_len + replacement.len;
258
+ const next_len = prefix_len + replacement.len + suffix.len;
259
+ if (next_len > output.len) return error.TextEditBufferTooSmall;
260
+
261
+ if (suffix_start > snapped.end) {
262
+ std.mem.copyBackwards(u8, output[suffix_start..next_len], suffix);
263
+ std.mem.copyForwards(u8, output[0..prefix_len], source[0..prefix_len]);
264
+ std.mem.copyForwards(u8, output[prefix_len..suffix_start], replacement);
265
+ } else {
266
+ std.mem.copyForwards(u8, output[0..prefix_len], source[0..prefix_len]);
267
+ std.mem.copyForwards(u8, output[prefix_len..suffix_start], replacement);
268
+ std.mem.copyForwards(u8, output[suffix_start..next_len], suffix);
269
+ }
270
+ return .{
271
+ .text = output[0..next_len],
272
+ .inserted_range = TextRange.init(prefix_len, suffix_start),
273
+ };
274
+ }
275
+
276
+ pub fn snapTextSelection(text: []const u8, selection: TextSelection) TextSelection {
277
+ return .{
278
+ .anchor = snapTextOffset(text, selection.anchor),
279
+ .focus = snapTextOffset(text, selection.focus),
280
+ };
281
+ }
282
+
283
+ pub fn snapTextRange(text: []const u8, range: TextRange) TextRange {
284
+ const normalized = range.normalized(text.len);
285
+ return TextRange.init(
286
+ snapTextOffset(text, normalized.start),
287
+ snapTextOffset(text, normalized.end),
288
+ ).normalized(text.len);
289
+ }
290
+
291
+ pub fn previousTextOffset(text: []const u8, offset: usize) usize {
292
+ var cursor = snapTextOffset(text, offset);
293
+ if (cursor == 0) return 0;
294
+ cursor -= 1;
295
+ while (cursor > 0 and isUtf8ContinuationByte(text[cursor])) {
296
+ cursor -= 1;
297
+ }
298
+ return cursor;
299
+ }
300
+
301
+ pub fn nextTextOffset(text: []const u8, offset: usize) usize {
302
+ const cursor = snapTextOffset(text, offset);
303
+ if (cursor >= text.len) return text.len;
304
+ const next = @min(text.len, cursor + utf8SequenceLength(text[cursor]));
305
+ // Invalid UTF-8 must never stall or reverse the walk: an orphan
306
+ // continuation byte at `offset` snaps back to the previous lead,
307
+ // whose sequence length can land at or before `offset` — and every
308
+ // scalar loop over this function (glyph-atlas planning, line wrap,
309
+ // caret movement) would spin forever on one stray 0x80 byte. Such
310
+ // bytes advance one byte instead: the fallback-scalar rule.
311
+ if (next <= offset) return @min(text.len, offset + 1);
312
+ return next;
313
+ }
314
+
315
+ pub fn previousTextWordOffset(text: []const u8, offset: usize) usize {
316
+ var cursor = snapTextOffset(text, offset);
317
+ while (cursor > 0) {
318
+ const previous = previousTextOffset(text, cursor);
319
+ if (textOffsetStartsWord(text, previous)) break;
320
+ cursor = previous;
321
+ }
322
+ while (cursor > 0) {
323
+ const previous = previousTextOffset(text, cursor);
324
+ if (!textOffsetStartsWord(text, previous)) break;
325
+ cursor = previous;
326
+ }
327
+ return cursor;
328
+ }
329
+
330
+ pub fn nextTextWordOffset(text: []const u8, offset: usize) usize {
331
+ var cursor = snapTextOffset(text, offset);
332
+ while (cursor < text.len and !textOffsetStartsWord(text, cursor)) {
333
+ cursor = nextTextOffset(text, cursor);
334
+ }
335
+ while (cursor < text.len and textOffsetStartsWord(text, cursor)) {
336
+ cursor = nextTextOffset(text, cursor);
337
+ }
338
+ return cursor;
339
+ }
340
+
341
+ pub fn snapTextOffset(text: []const u8, offset: usize) usize {
342
+ var cursor = @min(offset, text.len);
343
+ while (cursor > 0 and cursor < text.len and isUtf8ContinuationByte(text[cursor])) {
344
+ cursor -= 1;
345
+ }
346
+ return cursor;
347
+ }
348
+
349
+ pub fn utf8SequenceLength(lead: u8) usize {
350
+ if ((lead & 0x80) == 0) return 1;
351
+ if ((lead & 0xe0) == 0xc0) return 2;
352
+ if ((lead & 0xf0) == 0xe0) return 3;
353
+ if ((lead & 0xf8) == 0xf0) return 4;
354
+ return 1;
355
+ }
356
+
357
+ pub fn isUtf8ContinuationByte(byte: u8) bool {
358
+ return (byte & 0xc0) == 0x80;
359
+ }
360
+
361
+ /// The three character classes the double-click gesture selects runs
362
+ /// of. `word` is deliberately the SAME class the caret's word-jump
363
+ /// (`previousTextWordOffset`/`nextTextWordOffset`) walks — ASCII
364
+ /// alphanumerics, `_`, and every non-ASCII codepoint — so a
365
+ /// double-click and an alt+arrow agree on where a word ends. The
366
+ /// non-word remainder splits in two: whitespace runs and punctuation
367
+ /// clusters select separately, matching platform text fields (a
368
+ /// double-click between words selects the gap, not the neighbors).
369
+ const TextRunClass = enum { word, space, other };
370
+
371
+ /// Class of the codepoint whose UTF-8 sequence contains `offset`
372
+ /// (continuation bytes snap back to their lead). Null past the end of
373
+ /// the text. Multibyte sequences classify by their lead byte alone —
374
+ /// all their bytes have the high bit set, so a run never splits a
375
+ /// codepoint.
376
+ fn textRunClassAt(text: []const u8, offset: usize) ?TextRunClass {
377
+ const cursor = snapTextOffset(text, offset);
378
+ if (cursor >= text.len) return null;
379
+ const lead = text[cursor];
380
+ if ((lead & 0x80) != 0) return .word;
381
+ if (std.ascii.isAlphanumeric(lead) or lead == '_') return .word;
382
+ if (std.ascii.isWhitespace(lead)) return .space;
383
+ return .other;
384
+ }
385
+
386
+ /// The double-click selection: the run of same-class codepoints under
387
+ /// `offset` (word characters, whitespace, or punctuation — see
388
+ /// `TextRunClass`). A click at or past the end of the text selects the
389
+ /// trailing run, the platform text-field convention. Anchor is the run
390
+ /// start and focus the run end, so a subsequent shift-arrow extends
391
+ /// forward from the word.
392
+ pub fn textWordSelectionAtOffset(text: []const u8, offset: usize) TextSelection {
393
+ if (text.len == 0) return TextSelection.collapsed(0);
394
+ var cursor = snapTextOffset(text, offset);
395
+ if (cursor >= text.len) cursor = previousTextOffset(text, text.len);
396
+ const class = textRunClassAt(text, cursor) orelse return TextSelection.collapsed(text.len);
397
+ var start = cursor;
398
+ while (start > 0) {
399
+ const previous = previousTextOffset(text, start);
400
+ const previous_class = textRunClassAt(text, previous) orelse break;
401
+ if (previous_class != class) break;
402
+ start = previous;
403
+ }
404
+ var end = nextTextOffset(text, cursor);
405
+ while (end < text.len) {
406
+ const next_class = textRunClassAt(text, end) orelse break;
407
+ if (next_class != class) break;
408
+ end = nextTextOffset(text, end);
409
+ }
410
+ return .{ .anchor = start, .focus = end };
411
+ }
412
+
413
+ /// The triple-click selection in a multi-line editor: the hard-newline
414
+ /// delimited line containing `offset`. The trailing newline stays
415
+ /// OUTSIDE the selection (pinned: deleting a triple-click selection
416
+ /// empties the line but keeps the line break, and copy takes the line's
417
+ /// text without a stray terminator). Scanning raw bytes for `\n` is
418
+ /// UTF-8 safe — 0x0A never appears inside a multibyte sequence.
419
+ pub fn textLineSelectionAtOffset(text: []const u8, offset: usize) TextSelection {
420
+ const cursor = @min(offset, text.len);
421
+ var start = cursor;
422
+ while (start > 0 and text[start - 1] != '\n') start -= 1;
423
+ var end = cursor;
424
+ while (end < text.len and text[end] != '\n') end += 1;
425
+ return .{ .anchor = start, .focus = end };
426
+ }
427
+
428
+ fn textOffsetStartsWord(text: []const u8, offset: usize) bool {
429
+ const class = textRunClassAt(text, offset) orelse return false;
430
+ return class == .word;
431
+ }
432
+
433
+ /// Fixed-capacity editor state for elm-style text fields: the model applies
434
+ /// every `TextInputEvent` and stays an exact mirror of the runtime's editor
435
+ /// (text, selection, and composition), so rebuild reconciliation preserves
436
+ /// runtime caret state while the texts match and model-side changes (like
437
+ /// clearing on submit) win.
438
+ pub fn TextBuffer(comptime capacity: usize) type {
439
+ return struct {
440
+ const Self = @This();
441
+
442
+ storage: [capacity]u8 = undefined,
443
+ len: usize = 0,
444
+ selection: TextSelection = .{},
445
+ composition: ?TextRange = null,
446
+ /// True when the most recent `apply` had to clamp (insertions) or
447
+ /// reject (other edits) the event to stay within capacity. Loud
448
+ /// seam for paste: check after applying a clipboard insert.
449
+ truncated: bool = false,
450
+
451
+ pub fn init(initial: []const u8) Self {
452
+ var self = Self{};
453
+ self.set(initial);
454
+ return self;
455
+ }
456
+
457
+ pub fn text(self: *const Self) []const u8 {
458
+ return self.storage[0..self.len];
459
+ }
460
+
461
+ pub fn isEmpty(self: *const Self) bool {
462
+ return std.mem.trim(u8, self.text(), " \t").len == 0;
463
+ }
464
+
465
+ /// Apply one edit event. Insertions that would exceed capacity are
466
+ /// clamped to the bytes that fit (at a UTF-8 boundary); any other
467
+ /// over-capacity edit is rejected. Both set `truncated`.
468
+ pub fn apply(self: *Self, event: TextInputEvent) void {
469
+ var scratch: [capacity]u8 = undefined;
470
+ const state = TextEditState{
471
+ .text = self.text(),
472
+ .selection = self.selection,
473
+ .composition = self.composition,
474
+ };
475
+ const next = applyTextInputEvent(state, event, &scratch) catch {
476
+ self.truncated = true;
477
+ const clamped = clampedInsertEvent(state, event, capacity) orelse return;
478
+ const next_clamped = applyTextInputEvent(state, clamped, &scratch) catch return;
479
+ self.commit(next_clamped);
480
+ return;
481
+ };
482
+ self.truncated = false;
483
+ self.commit(next);
484
+ }
485
+
486
+ fn commit(self: *Self, next: TextEditState) void {
487
+ const next_len = @min(next.text.len, capacity);
488
+ std.mem.copyForwards(u8, self.storage[0..next_len], next.text[0..next_len]);
489
+ self.len = next_len;
490
+ self.selection = next.selection;
491
+ self.composition = next.composition;
492
+ }
493
+
494
+ pub fn set(self: *Self, new_text: []const u8) void {
495
+ const next_len = @min(new_text.len, capacity);
496
+ std.mem.copyForwards(u8, self.storage[0..next_len], new_text[0..next_len]);
497
+ self.len = next_len;
498
+ self.selection = TextSelection.collapsed(next_len);
499
+ self.composition = null;
500
+ }
501
+
502
+ pub fn clear(self: *Self) void {
503
+ self.len = 0;
504
+ self.selection = .{};
505
+ self.composition = null;
506
+ }
507
+ };
508
+ }
509
+
510
+ /// For an over-capacity `.insert_text`, the same event with its payload
511
+ /// clamped (at a UTF-8 boundary) to the bytes that fit alongside the text
512
+ /// the edit keeps. Null when the event is not an insertion or when
513
+ /// nothing fits.
514
+ fn clampedInsertEvent(state: TextEditState, event: TextInputEvent, capacity: usize) ?TextInputEvent {
515
+ const insertion = switch (event) {
516
+ .insert_text => |text| text,
517
+ else => return null,
518
+ };
519
+ const normalized = normalizeTextEditState(state);
520
+ const replaced = activeTextReplaceRange(normalized).byteLen(normalized.text.len);
521
+ const kept = normalized.text.len - replaced;
522
+ if (kept >= capacity) return null;
523
+ const available = capacity - kept;
524
+ if (available >= insertion.len) return null;
525
+ const clamped_len = snapTextOffset(insertion, available);
526
+ if (clamped_len == 0) return null;
527
+ return .{ .insert_text = insertion[0..clamped_len] };
528
+ }
529
+
530
+ test "textWordSelectionAtOffset selects word, whitespace, and punctuation runs" {
531
+ const text = "hello world, ok";
532
+ // Middle of a word selects the whole word.
533
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 0, .focus = 5 }, textWordSelectionAtOffset(text, 2));
534
+ // The word's first byte belongs to the word too.
535
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 0, .focus = 5 }, textWordSelectionAtOffset(text, 0));
536
+ // A click on whitespace selects the whitespace run, not a neighbor.
537
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 5, .focus = 7 }, textWordSelectionAtOffset(text, 5));
538
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 5, .focus = 7 }, textWordSelectionAtOffset(text, 6));
539
+ // Punctuation selects the punctuation cluster.
540
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 12, .focus = 13 }, textWordSelectionAtOffset(text, 12));
541
+ // At (or past) the end of the text the trailing run selects.
542
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 14, .focus = 16 }, textWordSelectionAtOffset(text, text.len));
543
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 14, .focus = 16 }, textWordSelectionAtOffset(text, 99));
544
+ // Empty text collapses at 0.
545
+ try std.testing.expectEqualDeep(TextSelection.collapsed(0), textWordSelectionAtOffset("", 0));
546
+ }
547
+
548
+ test "textWordSelectionAtOffset never splits multibyte codepoints" {
549
+ // "héllo wörld" — é and ö are 2-byte codepoints; non-ASCII is word
550
+ // class, so accented words select whole.
551
+ const text = "h\xc3\xa9llo w\xc3\xb6rld";
552
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 0, .focus = 6 }, textWordSelectionAtOffset(text, 0));
553
+ // Offset landing on the continuation byte snaps to its codepoint.
554
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 0, .focus = 6 }, textWordSelectionAtOffset(text, 2));
555
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 7, .focus = 13 }, textWordSelectionAtOffset(text, 9));
556
+ // Underscores join words, the caret word-jump's rule.
557
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 0, .focus = 9 }, textWordSelectionAtOffset("snake_car go", 3));
558
+ }
559
+
560
+ test "textLineSelectionAtOffset selects the newline-delimited line without its break" {
561
+ const text = "first line\nsecond\n\nfourth";
562
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 0, .focus = 10 }, textLineSelectionAtOffset(text, 4));
563
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 11, .focus = 17 }, textLineSelectionAtOffset(text, 13));
564
+ // A click on an empty line collapses on it (nothing to select).
565
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 18, .focus = 18 }, textLineSelectionAtOffset(text, 18));
566
+ // The last line has no trailing newline; it still selects fully.
567
+ try std.testing.expectEqualDeep(TextSelection{ .anchor = 19, .focus = 25 }, textLineSelectionAtOffset(text, 22));
568
+ }
569
+
570
+ test "TextBuffer mirrors edits, truncates at capacity, and clears" {
571
+ var buffer = TextBuffer(8){};
572
+ buffer.apply(.{ .insert_text = "hi" });
573
+ buffer.apply(.{ .insert_text = " there" });
574
+ try @import("std").testing.expectEqualStrings("hi there", buffer.text());
575
+ try @import("std").testing.expect(!buffer.truncated);
576
+
577
+ // Over-capacity edits truncate rather than fail, and say so.
578
+ buffer.apply(.{ .insert_text = "!" });
579
+ try @import("std").testing.expectEqual(@as(usize, 8), buffer.text().len);
580
+ try @import("std").testing.expect(buffer.truncated);
581
+
582
+ buffer.apply(.delete_backward);
583
+ try @import("std").testing.expectEqualStrings("hi ther", buffer.text());
584
+ try @import("std").testing.expect(!buffer.truncated);
585
+
586
+ buffer.clear();
587
+ try @import("std").testing.expectEqualStrings("", buffer.text());
588
+ try @import("std").testing.expect(buffer.isEmpty());
589
+ }
590
+
591
+ test "TextBuffer clamps over-capacity insertions at a UTF-8 boundary" {
592
+ const std_testing = @import("std").testing;
593
+ var buffer = TextBuffer(8){};
594
+ buffer.apply(.{ .insert_text = "hi" });
595
+ // 8-byte insertion into 6 available bytes clamps to what fits.
596
+ buffer.apply(.{ .insert_text = " there!!" });
597
+ try std_testing.expectEqualStrings("hi there", buffer.text());
598
+ try std_testing.expect(buffer.truncated);
599
+ try std_testing.expectEqualDeep(TextSelection.collapsed(8), buffer.selection);
600
+
601
+ // Multi-byte codepoints never split: "é" is 2 bytes and only 1 fits.
602
+ var accents = TextBuffer(3){};
603
+ accents.apply(.{ .insert_text = "ab" });
604
+ accents.apply(.{ .insert_text = "\xc3\xa9\xc3\xa9" });
605
+ try std_testing.expectEqualStrings("ab", accents.text());
606
+ try std_testing.expect(accents.truncated);
607
+
608
+ // A selection being replaced frees its bytes for the insertion.
609
+ var replace = TextBuffer(8){};
610
+ replace.apply(.{ .insert_text = "abcdefgh" });
611
+ replace.apply(.{ .set_selection = .{ .anchor = 0, .focus = 8 } });
612
+ replace.apply(.{ .insert_text = "0123456789" });
613
+ try std_testing.expectEqualStrings("01234567", replace.text());
614
+ try std_testing.expect(replace.truncated);
615
+ }