@native-sdk/cli 0.7.0 → 0.7.2

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 (81) hide show
  1. package/build/app.zig +8 -0
  2. package/build.zig +114 -103
  3. package/native-sdk.d.ts +6 -3
  4. package/package.json +9 -9
  5. package/packages/core/package-lock.json +2 -2
  6. package/packages/core/package.json +1 -1
  7. package/packages/core/sdk/text.ts +25 -14
  8. package/packages/core/src/checker.ts +39 -0
  9. package/packages/core/src/emitter.ts +125 -1
  10. package/skill-data/native-ui/SKILL.md +11 -7
  11. package/src/platform/macos/appkit_host.m +76 -4
  12. package/src/platform/macos/cef_host.mm +24 -2
  13. package/src/platform/macos/root.zig +13 -0
  14. package/src/platform/types.zig +4 -0
  15. package/src/platform/windows/gpu_surface_renderer.cpp +2280 -0
  16. package/src/platform/windows/gpu_surface_renderer.h +86 -0
  17. package/src/platform/windows/root.zig +298 -20
  18. package/src/platform/windows/webview2_host.cpp +343 -33
  19. package/src/primitives/canvas/code.zig +501 -33
  20. package/src/primitives/canvas/code_tests.zig +1029 -6
  21. package/src/primitives/canvas/events.zig +111 -1
  22. package/src/primitives/canvas/frame.zig +45 -1
  23. package/src/primitives/canvas/gpu.zig +6 -0
  24. package/src/primitives/canvas/markdown_tests.zig +17 -18
  25. package/src/primitives/canvas/render_tests.zig +93 -0
  26. package/src/primitives/canvas/root.zig +6 -0
  27. package/src/primitives/canvas/serialization.zig +150 -21
  28. package/src/primitives/canvas/text.zig +6 -0
  29. package/src/primitives/canvas/themes/geist.zig +51 -5
  30. package/src/primitives/canvas/tokens.zig +33 -4
  31. package/src/primitives/canvas/ui.zig +160 -36
  32. package/src/primitives/canvas/ui_markup.zig +60 -9
  33. package/src/primitives/canvas/ui_markup_compiled.zig +41 -0
  34. package/src/primitives/canvas/ui_markup_compiled_tests.zig +12 -0
  35. package/src/primitives/canvas/ui_markup_contract.zig +17 -1
  36. package/src/primitives/canvas/ui_markup_view.zig +44 -0
  37. package/src/primitives/canvas/ui_markup_view_tests.zig +80 -6
  38. package/src/primitives/canvas/ui_schema.zig +18 -0
  39. package/src/primitives/canvas/ui_schema_tests.zig +10 -7
  40. package/src/primitives/canvas/ui_tests.zig +40 -0
  41. package/src/primitives/canvas/widget_builtin_tests.zig +513 -13
  42. package/src/primitives/canvas/widget_invalidation.zig +12 -1
  43. package/src/primitives/canvas/widget_layout.zig +260 -21
  44. package/src/primitives/canvas/widget_metrics.zig +47 -6
  45. package/src/primitives/canvas/widget_render.zig +849 -78
  46. package/src/primitives/canvas/widget_render_controls.zig +83 -25
  47. package/src/primitives/canvas/widget_render_style.zig +52 -8
  48. package/src/primitives/canvas/widget_runtime.zig +2 -0
  49. package/src/primitives/canvas/widget_text_input.zig +167 -8
  50. package/src/primitives/canvas/widget_tree.zig +6 -5
  51. package/src/primitives/canvas/widgets.zig +78 -2
  52. package/src/runtime/canvas_budget_edge_tests.zig +3 -2
  53. package/src/runtime/canvas_frame.zig +60 -7
  54. package/src/runtime/canvas_frame_invalidation_tests.zig +80 -0
  55. package/src/runtime/canvas_frame_packet_tests.zig +83 -9
  56. package/src/runtime/canvas_frame_patch_tests.zig +16 -0
  57. package/src/runtime/canvas_images.zig +8 -2
  58. package/src/runtime/canvas_limits.zig +13 -6
  59. package/src/runtime/canvas_widget_clipboard_tests.zig +8 -5
  60. package/src/runtime/canvas_widget_context_menu.zig +7 -2
  61. package/src/runtime/canvas_widget_event_tests.zig +18 -14
  62. package/src/runtime/canvas_widget_events.zig +51 -20
  63. package/src/runtime/canvas_widget_runtime.zig +108 -4
  64. package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
  65. package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
  66. package/src/runtime/canvas_widget_state.zig +27 -0
  67. package/src/runtime/canvas_widget_text_tests.zig +326 -0
  68. package/src/runtime/core.zig +15 -5
  69. package/src/runtime/effects.zig +6 -0
  70. package/src/runtime/effects_tests.zig +30 -0
  71. package/src/runtime/gpu_surface_events.zig +50 -19
  72. package/src/runtime/media_surface_tests.zig +1 -1
  73. package/src/runtime/runtime_event_tests.zig +74 -0
  74. package/src/runtime/ui_app.zig +10 -8
  75. package/src/runtime/ui_app_tests.zig +67 -0
  76. package/src/runtime/view.zig +60 -16
  77. package/src/runtime/view_canvas.zig +15 -0
  78. package/src/runtime/view_widget_scroll.zig +48 -18
  79. package/src/runtime/view_widget_text.zig +59 -16
  80. package/src/runtime/window_view_runtime.zig +10 -0
  81. package/src/tooling/templates.zig +9 -6
@@ -4,6 +4,7 @@ const geometry = @import("geometry");
4
4
  const canvas = @import("root.zig");
5
5
  const text_spans = @import("text_spans.zig");
6
6
  const ui_model = @import("ui.zig");
7
+ const widget_metrics = @import("widget_metrics.zig");
7
8
 
8
9
  const testing = std.testing;
9
10
  const Ui = ui_model.Ui(enum { noop });
@@ -15,6 +16,38 @@ fn spanWithFragment(spans: []const canvas.TextSpan, fragment: []const u8) ?canva
15
16
  return null;
16
17
  }
17
18
 
19
+ test "diff line specs accept one-based values and compact ranges" {
20
+ var storage: [code_model.max_diff_lines]usize = undefined;
21
+ const lines = code_model.parseLineNumberSpec("2-4, 7, 3", &storage).?;
22
+ try testing.expectEqualSlices(usize, &.{ 2, 3, 4, 7 }, lines);
23
+ try testing.expectEqual(@as(usize, 0), code_model.parseLineNumberSpec("", &storage).?.len);
24
+
25
+ const invalid = [_][]const u8{ "0", "4-2", "1-129", "1,,2", "2-", "2-3-4", "nope" };
26
+ for (invalid) |spec| try testing.expect(code_model.parseLineNumberSpec(spec, &storage) == null);
27
+ }
28
+
29
+ test "widget diff metadata round-trips both 128-line masks" {
30
+ var widget = canvas.Widget{ .kind = .text, .code_line_number_digits = 3 };
31
+ const expected = canvas.CodeDiffLines{
32
+ .added = (@as(u128, 1) << 127) | (@as(u128, 1) << 64) | 1,
33
+ .removed = (@as(u128, 1) << 126) | (@as(u128, 1) << 63) | 2,
34
+ };
35
+ widget.setCodeDiffLines(expected);
36
+
37
+ try testing.expectEqual(@as(u8, 3), widget.codeLineNumberDigits());
38
+ try testing.expectEqual(expected.added, widget.codeDiffLines().?.added);
39
+ try testing.expectEqual(expected.removed, widget.codeDiffLines().?.removed);
40
+ }
41
+
42
+ fn colorAtOffset(spans: []const canvas.TextSpan, offset: usize) ?canvas.TextSpanColor {
43
+ var cursor: usize = 0;
44
+ for (spans) |span| {
45
+ if (offset < cursor + span.text.len) return span.color;
46
+ cursor += span.text.len;
47
+ }
48
+ return null;
49
+ }
50
+
18
51
  fn findByKind(widget: canvas.Widget, kind: canvas.WidgetKind) ?canvas.Widget {
19
52
  if (widget.kind == kind) return widget;
20
53
  for (widget.children) |child| {
@@ -59,6 +92,32 @@ fn displayListTextBytes(display_list: canvas.DisplayList) usize {
59
92
  return count;
60
93
  }
61
94
 
95
+ fn codeFontSensitiveMeasure(context: ?*anyopaque, font_id: canvas.FontId, size: f32, text: []const u8) f32 {
96
+ _ = context;
97
+ const advance: f32 = if (font_id == canvas.default_mono_font_id) 1 else 0.1;
98
+ return size * advance * @as(f32, @floatFromInt(text.len));
99
+ }
100
+
101
+ const code_font_sensitive_measure = canvas.TextMeasureProvider{ .measure_fn = codeFontSensitiveMeasure };
102
+
103
+ const CodeWidthMeasure = struct {
104
+ width_calls: usize = 0,
105
+ advance_calls: usize = 0,
106
+
107
+ fn width(context: ?*anyopaque, _: canvas.FontId, size: f32, text: []const u8) f32 {
108
+ const self: *CodeWidthMeasure = @ptrCast(@alignCast(context.?));
109
+ self.width_calls += 1;
110
+ return size * @as(f32, @floatFromInt(text.len));
111
+ }
112
+
113
+ fn advances(context: ?*anyopaque, _: canvas.FontId, size: f32, text: []const u8, out: []f32) bool {
114
+ const self: *CodeWidthMeasure = @ptrCast(@alignCast(context.?));
115
+ self.advance_calls += 1;
116
+ for (text, 0..) |byte, index| out[index] = if (byte == '\n') 0 else size;
117
+ return true;
118
+ }
119
+ };
120
+
62
121
  fn expectCompleteSpanLayouts(widget: canvas.Widget) !void {
63
122
  if (widget.kind == .text and widget.spans.len > 0) {
64
123
  var runs: [text_spans.max_text_span_runs_per_paragraph]text_spans.TextSpanRun = undefined;
@@ -85,6 +144,21 @@ fn allTextSpansHaveColor(widget: canvas.Widget, color: canvas.TextSpanColor) boo
85
144
  return true;
86
145
  }
87
146
 
147
+ fn expectCodeTabInsertion(source: []const u8, expected: []const u8) !void {
148
+ const edit = canvas.widgetCodeTabTextEditEvent(.{
149
+ .kind = .textarea,
150
+ .code_editor = true,
151
+ .text = source,
152
+ }, .{
153
+ .phase = .key_down,
154
+ .key = "tab",
155
+ }).?;
156
+ switch (edit) {
157
+ .insert_text => |text| try testing.expectEqualStrings(expected, text),
158
+ else => return error.TestExpectedEqual,
159
+ }
160
+ }
161
+
88
162
  test "HTML and JSX highlighting distinguishes tags attributes expressions and strings" {
89
163
  const source =
90
164
  \\<Accordion defaultValue={["item-1"]}>
@@ -94,7 +168,12 @@ test "HTML and JSX highlighting distinguishes tags attributes expressions and st
94
168
  var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
95
169
  const spans = code_model.highlight(source, code_model.languageFromName("tsx"), &storage);
96
170
 
97
- try testing.expectEqual(code_model.Language.html, code_model.languageFromName("jsx"));
171
+ try testing.expectEqual(code_model.Language.jsx, code_model.languageFromName("jsx"));
172
+ try testing.expectEqual(code_model.Language.tsx, code_model.languageFromName("tsx"));
173
+ try testing.expectEqual(code_model.Language.javascript, code_model.languageFromName("mjs"));
174
+ try testing.expectEqual(code_model.Language.yaml, code_model.languageFromName("yaml"));
175
+ try testing.expectEqual(code_model.Language.yaml, code_model.languageFromName("yml"));
176
+ try testing.expectEqual(code_model.Language.markdown, code_model.languageFromName("md"));
98
177
  try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "Accordion").?.color.?);
99
178
  try testing.expectEqual(canvas.TextSpanColor.syntax_function, spanWithFragment(spans, "defaultValue").?.color.?);
100
179
  try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "\"item-1\"").?.color.?);
@@ -102,6 +181,54 @@ test "HTML and JSX highlighting distinguishes tags attributes expressions and st
102
181
  try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(spans, "Accessible?").?.color.?);
103
182
  }
104
183
 
184
+ test "YAML highlighting distinguishes mapping keys scalars symbols and comments" {
185
+ const source =
186
+ \\---
187
+ \\name: native
188
+ \\runs-on: macos-15
189
+ \\enabled: true
190
+ \\empty: ~
191
+ \\defaults: &defaults
192
+ \\command: "zig build test"
193
+ \\use: *defaults
194
+ \\url: https://example.test/#section # trailing comment
195
+ ;
196
+ var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
197
+ const spans = code_model.highlight(source, .yaml, &storage);
198
+
199
+ try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(spans, "---").?.color.?);
200
+ try testing.expectEqual(canvas.TextSpanColor.syntax_property, spanWithFragment(spans, "name").?.color.?);
201
+ try testing.expectEqual(canvas.TextSpanColor.syntax_property, spanWithFragment(spans, "runs-on").?.color.?);
202
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "true").?.color.?);
203
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "~").?.color.?);
204
+ try testing.expectEqual(canvas.TextSpanColor.syntax_constant, spanWithFragment(spans, "&defaults").?.color.?);
205
+ try testing.expectEqual(canvas.TextSpanColor.syntax_constant, spanWithFragment(spans, "*defaults").?.color.?);
206
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "\"zig build test\"").?.color.?);
207
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(spans, "#section").?.color.?);
208
+ try testing.expectEqual(canvas.TextSpanColor.syntax_comment, spanWithFragment(spans, "# trailing comment").?.color.?);
209
+ }
210
+
211
+ test "TSX highlights top-level TypeScript and embedded JSX in one grammar" {
212
+ const source =
213
+ \\import type { Metadata } from "next";
214
+ \\export default function RootLayout() { return <html lang="en" />; }
215
+ ;
216
+ var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
217
+ const spans = code_model.highlight(source, .tsx, &storage);
218
+
219
+ inline for (.{ "import", "type", "from", "export", "default", "function", "return" }) |keyword| {
220
+ try testing.expectEqual(
221
+ canvas.TextSpanColor.syntax_keyword,
222
+ spanWithFragment(spans, keyword).?.color.?,
223
+ );
224
+ }
225
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "\"next\"").?.color.?);
226
+ try testing.expectEqual(canvas.TextSpanColor.syntax_function, spanWithFragment(spans, "RootLayout").?.color.?);
227
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "html").?.color.?);
228
+ try testing.expectEqual(canvas.TextSpanColor.syntax_function, spanWithFragment(spans, "lang").?.color.?);
229
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "\"en\"").?.color.?);
230
+ }
231
+
105
232
  test "JSX relational less-than preserves expression state and nested tags" {
106
233
  const source = "<span>{count<limit ? <Low/> : <High/>}</span>";
107
234
  var state: code_model.HighlightState = .{};
@@ -124,6 +251,83 @@ test "JSX relational less-than preserves expression state and nested tags" {
124
251
  try testing.expect(!chunked_state.html_in_tag);
125
252
  }
126
253
 
254
+ test "TSX relational less-than at top level does not open a tag" {
255
+ const source = "const ok=count<limit; const later=true;";
256
+ var state: code_model.HighlightState = .{};
257
+ var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
258
+ const spans = code_model.highlightWithState(source, .tsx, &storage, &state);
259
+
260
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(spans, "limit").?.color.?);
261
+ try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(spans, "const").?.color.?);
262
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "true").?.color.?);
263
+ try testing.expect(!state.html_in_tag);
264
+
265
+ var chunked_state: code_model.HighlightState = .{};
266
+ var first_storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
267
+ _ = code_model.highlightWithState("const ok=count", .tsx, &first_storage, &chunked_state);
268
+ var second_storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
269
+ const second = code_model.highlightWithState("<limit; const later=true;", .tsx, &second_storage, &chunked_state);
270
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(second, "limit").?.color.?);
271
+ try testing.expect(!chunked_state.html_in_tag);
272
+ }
273
+
274
+ test "JSX closing tags remain structural after plain text" {
275
+ const source = "const view = <div>Hello<span>world</span></div>; const later = true;";
276
+ var state: code_model.HighlightState = .{};
277
+ var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
278
+ const spans = code_model.highlightWithState(source, .tsx, &storage, &state);
279
+ const closing_name = std.mem.lastIndexOf(u8, source, "div").?;
280
+ const nested_opening_name = std.mem.indexOf(u8, source, "span").?;
281
+ const nested_closing_name = std.mem.lastIndexOf(u8, source, "span").?;
282
+ const later_name = std.mem.indexOf(u8, source, "later").?;
283
+
284
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, colorAtOffset(spans, closing_name).?);
285
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, colorAtOffset(spans, nested_opening_name).?);
286
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, colorAtOffset(spans, nested_closing_name).?);
287
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, colorAtOffset(spans, later_name).?);
288
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "true").?.color.?);
289
+ try testing.expectEqual(@as(usize, 0), state.html_element_len);
290
+ try testing.expect(!state.html_in_tag);
291
+ }
292
+
293
+ test "JSX regex comparisons inside elements do not become closing tags" {
294
+ const source = "const view = <div>{value</x/.test(text) ? 'yes' : 'no'}</div>;";
295
+ var state: code_model.HighlightState = .{};
296
+ var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
297
+ const spans = code_model.highlightWithState(source, .tsx, &storage, &state);
298
+ const regex_name = std.mem.indexOf(u8, source, "x/").?;
299
+ const closing_name = std.mem.lastIndexOf(u8, source, "div").?;
300
+
301
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, colorAtOffset(spans, regex_name).?);
302
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, colorAtOffset(spans, closing_name).?);
303
+ try testing.expectEqual(@as(usize, 0), state.html_element_len);
304
+ try testing.expectEqual(@as(usize, 0), state.html_expression_depth);
305
+ try testing.expect(!state.html_in_tag);
306
+ }
307
+
308
+ test "Markdown highlighting distinguishes structure links code and comments" {
309
+ const source =
310
+ \\# Heading
311
+ \\- [Native](https://native.dev) uses `code` and **emphasis**.
312
+ \\<!-- note -->
313
+ \\```zig
314
+ \\const value = true;
315
+ \\```
316
+ ;
317
+ var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
318
+ const spans = code_model.highlight(source, .markdown, &storage);
319
+
320
+ try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(spans, "#").?.color.?);
321
+ try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(spans, "-").?.color.?);
322
+ try testing.expectEqual(canvas.TextSpanColor.syntax_property, spanWithFragment(spans, "[").?.color.?);
323
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "https://native.dev").?.color.?);
324
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "`code`").?.color.?);
325
+ try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(spans, "**").?.color.?);
326
+ try testing.expectEqual(canvas.TextSpanColor.syntax_comment, spanWithFragment(spans, "<!-- note -->").?.color.?);
327
+ try testing.expectEqual(canvas.TextSpanColor.syntax_constant, spanWithFragment(spans, "zig").?.color.?);
328
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "const value").?.color.?);
329
+ }
330
+
127
331
  test "nested JSX attribute tags restore the enclosing opening tag" {
128
332
  const source = "<Comp render={<Inner />} disabled=\"yes\">";
129
333
  var state: code_model.HighlightState = .{};
@@ -273,15 +477,26 @@ test "token-dense fallback still updates lexer state" {
273
477
  try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(second, "const").?.color.?);
274
478
  }
275
479
 
276
- test "callables and object properties use Geist syntax roles" {
277
- const source = "function render() { return { color: true }; }";
480
+ test "callables and object keys use variable syntax roles" {
481
+ const source = "function render() { const variable = { color: true }; return variable; }";
278
482
  var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
279
483
  const spans = code_model.highlight(source, .javascript, &storage);
280
484
 
281
485
  try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(spans, "function").?.color.?);
282
486
  try testing.expectEqual(canvas.TextSpanColor.syntax_function, spanWithFragment(spans, "render").?.color.?);
283
- try testing.expectEqual(canvas.TextSpanColor.syntax_property, spanWithFragment(spans, "color").?.color.?);
487
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(spans, "variable").?.color.?);
488
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(spans, "color").?.color.?);
284
489
  try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "true").?.color.?);
490
+
491
+ const typed_source = "const options: { enabled: boolean } = { enabled: true };";
492
+ var typed_storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
493
+ const typed_spans = code_model.highlight(typed_source, .typescript, &typed_storage);
494
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(typed_spans, "options").?.color.?);
495
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(typed_spans, "enabled").?.color.?);
496
+
497
+ var css_storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
498
+ const css_spans = code_model.highlight("body { color: red; }", .css, &css_storage);
499
+ try testing.expectEqual(canvas.TextSpanColor.syntax_property, spanWithFragment(css_spans, "color").?.color.?);
285
500
  }
286
501
 
287
502
  test "both built-in packs use the Geist Code Block syntax palette" {
@@ -328,7 +543,11 @@ test "code wraps by default and no-wrap composes one horizontal scroller" {
328
543
  defer wrapped_arena.deinit();
329
544
  var wrapped_ui = Ui.init(wrapped_arena.allocator());
330
545
  const wrapped = try wrapped_ui.finalize(wrapped_ui.code(.{ .language = .zig }, "const answer: u32 = 42;"));
331
- try testing.expectEqual(canvas.WidgetKind.panel, wrapped.root.kind);
546
+ try testing.expectEqual(canvas.WidgetKind.column, wrapped.root.kind);
547
+ try testing.expectEqual(geometry.InsetsF{}, wrapped.root.layout.padding);
548
+ try testing.expect(wrapped.root.style.background == null);
549
+ try testing.expect(wrapped.root.style.border == null);
550
+ try testing.expect(wrapped.root.style.radius == null);
332
551
  try testing.expect(findByKind(wrapped.root, .scroll_view) == null);
333
552
  const wrapped_text = findByKind(wrapped.root, .text).?;
334
553
  try testing.expect(!wrapped_text.text_no_wrap);
@@ -357,6 +576,71 @@ test "code wraps by default and no-wrap composes one horizontal scroller" {
357
576
  try testing.expect(text_width > scroll_width);
358
577
  }
359
578
 
579
+ test "editable code layout paints syntax-highlighted token runs" {
580
+ const source = "const answer: u32 = 42;";
581
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
582
+ defer arena.deinit();
583
+ var ui = Ui.init(arena.allocator());
584
+ const view = try ui.finalize(ui.code(.{
585
+ .language = .zig,
586
+ .editable = true,
587
+ .width = 320,
588
+ .height = 80,
589
+ }, source));
590
+
591
+ var nodes: [8]canvas.WidgetLayoutNode = undefined;
592
+ const layout = try canvas.layoutWidgetTree(
593
+ view.root,
594
+ geometry.RectF.init(0, 0, 320, 80),
595
+ &nodes,
596
+ );
597
+ const tokens = canvas.DesignTokens{};
598
+ var commands: [64]canvas.CanvasCommand = undefined;
599
+ var builder = canvas.Builder.init(&commands);
600
+ try canvas.emitWidgetLayout(&builder, layout, tokens);
601
+
602
+ var saw_keyword = false;
603
+ var saw_type = false;
604
+ var saw_literal = false;
605
+ for (builder.displayList().commands) |command| {
606
+ switch (command) {
607
+ .draw_text => |draw| {
608
+ if (std.mem.eql(u8, draw.text, "const")) {
609
+ try testing.expectEqual(tokens.colors.syntax_keyword, draw.color);
610
+ saw_keyword = true;
611
+ } else if (std.mem.eql(u8, draw.text, "u32")) {
612
+ try testing.expectEqual(tokens.colors.syntax_literal, draw.color);
613
+ saw_type = true;
614
+ } else if (std.mem.eql(u8, draw.text, "42")) {
615
+ try testing.expectEqual(tokens.colors.syntax_literal, draw.color);
616
+ saw_literal = true;
617
+ }
618
+ },
619
+ else => {},
620
+ }
621
+ }
622
+ try testing.expect(saw_keyword);
623
+ try testing.expect(saw_type);
624
+ try testing.expect(saw_literal);
625
+
626
+ var selected_editor = view.root;
627
+ // Runtime-owned text storage can be distinct from the highlighted
628
+ // spans' source storage between an edit and the controlled rebuild.
629
+ selected_editor.text = try arena.allocator().dupe(u8, source);
630
+ selected_editor.text_selection = .{ .anchor = 2, .focus = 12 };
631
+ selected_editor.state.focused = false;
632
+ const selected_layout = try canvas.layoutWidgetTree(
633
+ selected_editor,
634
+ geometry.RectF.init(0, 0, 320, 80),
635
+ &nodes,
636
+ );
637
+ var selected_commands: [64]canvas.CanvasCommand = undefined;
638
+ var selected_builder = canvas.Builder.init(&selected_commands);
639
+ try canvas.emitWidgetLayout(&selected_builder, selected_layout, tokens);
640
+ var changes: [64]canvas.DiffChange = undefined;
641
+ _ = try canvas.DisplayList.diff(.{}, selected_builder.displayList(), &changes);
642
+ }
643
+
360
644
  test "unwrapped multiline code hugs the height of every logical line" {
361
645
  var arena = std.heap.ArenaAllocator.init(testing.allocator);
362
646
  defer arena.deinit();
@@ -520,6 +804,665 @@ test "line numbers are opt-in and stay paired with logical source lines" {
520
804
  try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(comment_text.spans, "const").?.color.?);
521
805
  }
522
806
 
807
+ test "diff lines use Geist washes and renderer-owned markers without changing source" {
808
+ const source =
809
+ \\module.exports = {
810
+ \\ experimental: {
811
+ \\ appDir: true,
812
+ \\ },
813
+ \\ appDir: true,
814
+ \\}
815
+ ;
816
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
817
+ defer arena.deinit();
818
+ var ui = Ui.init(arena.allocator());
819
+ const view = try ui.finalize(ui.code(.{
820
+ .language = .javascript,
821
+ .line_numbers = true,
822
+ .added_lines = &.{5},
823
+ .removed_lines = &.{ 2, 3, 4 },
824
+ .wrap = false,
825
+ .width = 360,
826
+ }, source));
827
+ const paragraph = findByText(view.root, source).?;
828
+ try testing.expectEqualStrings(source, paragraph.text);
829
+ const diff_lines = paragraph.codeDiffLines().?;
830
+ try testing.expectEqual(@as(u128, 1) << 4, diff_lines.added);
831
+ try testing.expectEqual((@as(u128, 1) << 1) | (@as(u128, 1) << 2) | (@as(u128, 1) << 3), diff_lines.removed);
832
+
833
+ var nodes: [16]canvas.WidgetLayoutNode = undefined;
834
+ const layout = try canvas.layoutWidgetTree(view.root, geometry.RectF.init(0, 0, 360, 140), &nodes);
835
+ for ([_]canvas.ThemePack{ .house, .geist }) |pack| {
836
+ for ([_]canvas.ColorScheme{ .light, .dark }) |scheme| {
837
+ const tokens = canvas.DesignTokens.theme(.{ .pack = pack, .color_scheme = scheme });
838
+ const added_background = if (scheme == .dark)
839
+ canvas.Color.rgb8(18, 54, 27)
840
+ else
841
+ canvas.Color.rgb8(218, 246, 218);
842
+ const removed_background = if (scheme == .dark)
843
+ canvas.Color.rgb8(86, 26, 30)
844
+ else
845
+ canvas.Color.rgb8(255, 230, 230);
846
+ const added_foreground = if (scheme == .dark)
847
+ canvas.Color.rgb8(98, 192, 115)
848
+ else
849
+ canvas.Color.rgb8(41, 122, 58);
850
+ const removed_foreground = if (scheme == .dark)
851
+ canvas.Color.rgb8(255, 97, 102)
852
+ else
853
+ canvas.Color.rgb8(203, 42, 47);
854
+ var commands: [256]canvas.CanvasCommand = undefined;
855
+ var builder = canvas.Builder.init(&commands);
856
+ try canvas.emitWidgetLayout(&builder, layout, tokens);
857
+
858
+ var added_washes: usize = 0;
859
+ var removed_washes: usize = 0;
860
+ var added_markers: usize = 0;
861
+ var removed_markers: usize = 0;
862
+ for (builder.displayList().commands) |command| {
863
+ switch (command) {
864
+ .fill_rect => |fill| {
865
+ if (std.meta.eql(fill.fill.color, added_background)) added_washes += 1;
866
+ if (std.meta.eql(fill.fill.color, removed_background)) removed_washes += 1;
867
+ },
868
+ .draw_text => |draw| {
869
+ if (std.mem.eql(u8, draw.text, "+") and std.meta.eql(draw.color, added_foreground)) added_markers += 1;
870
+ if (std.mem.eql(u8, draw.text, "-") and std.meta.eql(draw.color, removed_foreground)) removed_markers += 1;
871
+ },
872
+ else => {},
873
+ }
874
+ }
875
+ try testing.expectEqual(@as(usize, 1), added_washes);
876
+ try testing.expectEqual(@as(usize, 3), removed_washes);
877
+ try testing.expectEqual(@as(usize, 1), added_markers);
878
+ try testing.expectEqual(@as(usize, 3), removed_markers);
879
+ }
880
+ }
881
+ }
882
+
883
+ test "diff markers reserve a gutter when line numbers are hidden" {
884
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
885
+ defer arena.deinit();
886
+ var ui = Ui.init(arena.allocator());
887
+ const view = try ui.finalize(ui.code(.{ .added_lines = &.{1} }, "added"));
888
+ const paragraph = findByText(view.root, "added").?;
889
+ try testing.expectEqual(@as(u8, 0), paragraph.codeLineNumberDigits());
890
+ try testing.expect(widget_metrics.widgetCodeLineNumberGutterWidth(paragraph, .{}) > 0);
891
+ }
892
+
893
+ test "unwrapped editable diffs paint markers without line numbers" {
894
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
895
+ defer arena.deinit();
896
+ var ui = Ui.init(arena.allocator());
897
+ const view = try ui.finalize(ui.code(.{
898
+ .editable = true,
899
+ .wrap = false,
900
+ .added_lines = &.{1},
901
+ }, "added"));
902
+ var editor = findByText(view.root, "added").?;
903
+ editor.frame = geometry.RectF.init(0, 0, 160, 48);
904
+
905
+ var commands: [64]canvas.CanvasCommand = undefined;
906
+ var builder = canvas.Builder.init(&commands);
907
+ try canvas.emitWidgetTree(&builder, editor, .{});
908
+
909
+ var added_markers: usize = 0;
910
+ for (builder.displayList().commands) |command| {
911
+ if (command == .draw_text and std.mem.eql(u8, command.draw_text.text, "+")) {
912
+ added_markers += 1;
913
+ }
914
+ }
915
+ try testing.expectEqual(@as(usize, 1), added_markers);
916
+ }
917
+
918
+ test "editable diff washes paint behind text selections" {
919
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
920
+ defer arena.deinit();
921
+ var ui = Ui.init(arena.allocator());
922
+ const view = try ui.finalize(ui.code(.{
923
+ .editable = true,
924
+ .wrap = false,
925
+ .added_lines = &.{1},
926
+ }, "added"));
927
+ var editor = findByText(view.root, "added").?;
928
+ editor.frame = geometry.RectF.init(0, 0, 160, 48);
929
+ editor.text_selection = .{ .anchor = 0, .focus = editor.text.len };
930
+
931
+ var tokens = canvas.DesignTokens{};
932
+ const selection_fill = canvas.Color.rgb8(7, 11, 13);
933
+ const added_wash = canvas.Color.rgb8(218, 246, 218);
934
+ tokens.colors.accent = selection_fill;
935
+ tokens.colors.background = canvas.Color.rgb8(255, 255, 255);
936
+
937
+ var commands: [128]canvas.CanvasCommand = undefined;
938
+ var builder = canvas.Builder.init(&commands);
939
+ try canvas.emitWidgetTree(&builder, editor, tokens);
940
+
941
+ var wash_index: ?usize = null;
942
+ var selection_index: ?usize = null;
943
+ for (builder.displayList().commands, 0..) |command, index| {
944
+ if (command != .fill_rect) continue;
945
+ if (std.meta.eql(command.fill_rect.fill.color, added_wash) and
946
+ command.fill_rect.rect.width == editor.frame.width)
947
+ {
948
+ wash_index = index;
949
+ }
950
+ if (std.meta.eql(command.fill_rect.fill.color, selection_fill)) selection_index = index;
951
+ }
952
+ try testing.expect(wash_index != null);
953
+ try testing.expect(selection_index != null);
954
+ try testing.expect(wash_index.? < selection_index.?);
955
+ }
956
+
957
+ test "line number gutter reserves at least three marker columns" {
958
+ const tokens = canvas.DesignTokens{};
959
+ var numbered = canvas.Widget{
960
+ .kind = .textarea,
961
+ .code_editor = true,
962
+ .code_line_number_digits = 1,
963
+ };
964
+ const one_digit_width = widget_metrics.widgetCodeLineNumberGutterWidth(numbered, tokens);
965
+ numbered.code_line_number_digits = 3;
966
+ const three_digit_width = widget_metrics.widgetCodeLineNumberGutterWidth(numbered, tokens);
967
+ numbered.code_line_number_digits = 4;
968
+ const four_digit_width = widget_metrics.widgetCodeLineNumberGutterWidth(numbered, tokens);
969
+
970
+ try testing.expectEqual(three_digit_width, one_digit_width);
971
+ try testing.expect(four_digit_width > three_digit_width);
972
+ }
973
+
974
+ test "editable line-number gutter masks horizontally scrolled source" {
975
+ const source = "const deliberately_long_identifier = another_deliberately_long_identifier;\n";
976
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
977
+ defer arena.deinit();
978
+ var ui = Ui.init(arena.allocator());
979
+ const view = try ui.finalize(ui.code(.{
980
+ .language = .zig,
981
+ .editable = true,
982
+ .line_numbers = true,
983
+ .wrap = false,
984
+ .height = 80,
985
+ }, source));
986
+ var editor = findByText(view.root, source).?;
987
+ editor.frame = geometry.RectF.init(0, 0, 220, 80);
988
+ editor.value_x = 48;
989
+
990
+ const tokens = canvas.DesignTokens{};
991
+ var commands: [128]canvas.CanvasCommand = undefined;
992
+ var builder = canvas.Builder.init(&commands);
993
+ try canvas.emitWidgetTree(&builder, editor, tokens);
994
+
995
+ var source_index: ?usize = null;
996
+ var gutter_index: ?usize = null;
997
+ var marker_index: ?usize = null;
998
+ var gutter_rect: ?geometry.RectF = null;
999
+ for (builder.displayList().commands, 0..) |command, index| {
1000
+ switch (command) {
1001
+ .fill_rect => |fill| {
1002
+ if (std.meta.eql(fill.fill.color, tokens.colors.background)) {
1003
+ gutter_index = index;
1004
+ gutter_rect = fill.rect;
1005
+ }
1006
+ },
1007
+ .draw_text => |draw| {
1008
+ if (std.mem.eql(u8, draw.text, "1") or std.mem.eql(u8, draw.text, "2")) {
1009
+ marker_index = index;
1010
+ } else {
1011
+ source_index = index;
1012
+ }
1013
+ },
1014
+ else => {},
1015
+ }
1016
+ }
1017
+
1018
+ try testing.expect(source_index != null);
1019
+ try testing.expect(gutter_index != null);
1020
+ try testing.expect(marker_index != null);
1021
+ try testing.expect(source_index.? < gutter_index.?);
1022
+ try testing.expect(gutter_index.? < marker_index.?);
1023
+ try testing.expectEqual(editor.frame.x, gutter_rect.?.x);
1024
+ try testing.expectEqual(editor.frame.y, gutter_rect.?.y);
1025
+ try testing.expect(gutter_rect.?.width > 0);
1026
+ try testing.expectEqual(editor.frame.height, gutter_rect.?.height);
1027
+ }
1028
+
1029
+ test "wrapped numbered editable code emits one retained gutter" {
1030
+ const source = "alpha beta gamma delta epsilon zeta eta theta";
1031
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1032
+ defer arena.deinit();
1033
+ var ui = Ui.init(arena.allocator());
1034
+ const view = try ui.finalize(ui.code(.{
1035
+ .language = .plain,
1036
+ .editable = true,
1037
+ .line_numbers = true,
1038
+ .wrap = true,
1039
+ }, source));
1040
+ var editor = findByText(view.root, source).?;
1041
+ editor.frame = geometry.RectF.init(0, 0, 120, 80);
1042
+
1043
+ var commands: [256]canvas.CanvasCommand = undefined;
1044
+ var builder = canvas.Builder.init(&commands);
1045
+ try canvas.emitWidgetTree(&builder, editor, .{});
1046
+
1047
+ var line_one_count: usize = 0;
1048
+ for (builder.displayList().commands) |command| {
1049
+ if (command == .draw_text and std.mem.eql(u8, command.draw_text.text, "1")) {
1050
+ line_one_count += 1;
1051
+ }
1052
+ }
1053
+ try testing.expectEqual(@as(usize, 1), line_one_count);
1054
+
1055
+ var changes: [256]canvas.DiffChange = undefined;
1056
+ _ = try canvas.DisplayList.diff(.{}, builder.displayList(), &changes);
1057
+ }
1058
+
1059
+ test "wrapped editable code re-highlights runtime-owned text" {
1060
+ const source = "const stale = true;";
1061
+ const edited = "fn fresh() void {}";
1062
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1063
+ defer arena.deinit();
1064
+ var ui = Ui.init(arena.allocator());
1065
+ const view = try ui.finalize(ui.code(.{
1066
+ .language = .zig,
1067
+ .editable = true,
1068
+ .wrap = true,
1069
+ }, source));
1070
+ var editor = findByText(view.root, source).?;
1071
+ editor.text = edited;
1072
+ editor.frame = geometry.RectF.init(0, 0, 300, 80);
1073
+
1074
+ var commands: [256]canvas.CanvasCommand = undefined;
1075
+ var builder = canvas.Builder.init(&commands);
1076
+ try canvas.emitWidgetTree(&builder, editor, .{});
1077
+
1078
+ var rendered: std.ArrayListUnmanaged(u8) = .empty;
1079
+ defer rendered.deinit(testing.allocator);
1080
+ for (builder.displayList().commands) |command| {
1081
+ if (command == .draw_text) try rendered.appendSlice(testing.allocator, command.draw_text.text);
1082
+ }
1083
+ try testing.expectEqualStrings(edited, rendered.items);
1084
+ }
1085
+
1086
+ test "wrapped editable code preserves syntax after the document exhausts the span budget" {
1087
+ var source: std.ArrayListUnmanaged(u8) = .empty;
1088
+ defer source.deinit(testing.allocator);
1089
+ for (0..40) |index| {
1090
+ var line_buffer: [64]u8 = undefined;
1091
+ const line = try std.fmt.bufPrint(&line_buffer, "const value_{d} = \"ordinary\";\n", .{index});
1092
+ try source.appendSlice(testing.allocator, line);
1093
+ }
1094
+ try source.appendSlice(testing.allocator, "const tail = \"TAIL_SENTINEL\";");
1095
+
1096
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1097
+ defer arena.deinit();
1098
+ var ui = Ui.init(arena.allocator());
1099
+ const view = try ui.finalize(ui.code(.{
1100
+ .language = .zig,
1101
+ .editable = true,
1102
+ .wrap = true,
1103
+ }, source.items));
1104
+ var editor = findByText(view.root, source.items).?;
1105
+ editor.frame = geometry.RectF.init(0, 0, 800, 1_000);
1106
+
1107
+ const tokens = canvas.DesignTokens{};
1108
+ var commands: [512]canvas.CanvasCommand = undefined;
1109
+ var builder = canvas.Builder.init(&commands);
1110
+ try canvas.emitWidgetTree(&builder, editor, tokens);
1111
+
1112
+ var tail_color: ?canvas.Color = null;
1113
+ for (builder.displayList().commands) |command| {
1114
+ if (command != .draw_text) continue;
1115
+ if (std.mem.indexOf(u8, command.draw_text.text, "TAIL_SENTINEL") != null) {
1116
+ tail_color = command.draw_text.color;
1117
+ break;
1118
+ }
1119
+ }
1120
+ try testing.expectEqual(tokens.colors.syntax_literal, tail_color.?);
1121
+ }
1122
+
1123
+ test "wrapped editable code paints the visible tail of an over-capacity logical line" {
1124
+ const source = ("x" ** (text_spans.max_text_span_lines_per_paragraph * 2 + 8)) ++ "Z";
1125
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1126
+ defer arena.deinit();
1127
+ var ui = Ui.init(arena.allocator());
1128
+ const view = try ui.finalize(ui.code(.{
1129
+ .language = .plain,
1130
+ .editable = true,
1131
+ .wrap = true,
1132
+ }, source));
1133
+ var editor = findByText(view.root, source).?;
1134
+
1135
+ var measured = CodeWidthMeasure{};
1136
+ const provider = canvas.TextMeasureProvider{
1137
+ .context = &measured,
1138
+ .measure_fn = CodeWidthMeasure.width,
1139
+ .measure_advances_fn = CodeWidthMeasure.advances,
1140
+ };
1141
+ const tokens = canvas.DesignTokens{ .text_measure = &provider };
1142
+ const line_height = tokens.typography.body_size * 1.25;
1143
+ editor.frame = geometry.RectF.init(
1144
+ 0,
1145
+ 0,
1146
+ tokens.typography.body_size * 2,
1147
+ line_height * 2,
1148
+ );
1149
+ editor.value = canvas.textInputMaxScrollOffsetForWidget(editor, tokens);
1150
+ try testing.expect(editor.value > line_height * text_spans.max_text_span_lines_per_paragraph);
1151
+
1152
+ var commands: [64]canvas.CanvasCommand = undefined;
1153
+ var builder = canvas.Builder.init(&commands);
1154
+ try canvas.emitWidgetTree(&builder, editor, tokens);
1155
+
1156
+ var saw_tail = false;
1157
+ for (builder.displayList().commands) |command| {
1158
+ if (command == .draw_text and std.mem.indexOfScalar(u8, command.draw_text.text, 'Z') != null) {
1159
+ saw_tail = true;
1160
+ }
1161
+ }
1162
+ try testing.expect(saw_tail);
1163
+ }
1164
+
1165
+ test "editable code numbers the empty caret row after a terminal newline" {
1166
+ const source = "alpha\n";
1167
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1168
+ defer arena.deinit();
1169
+ var ui = Ui.init(arena.allocator());
1170
+ const view = try ui.finalize(ui.code(.{
1171
+ .language = .plain,
1172
+ .editable = true,
1173
+ .line_numbers = true,
1174
+ .wrap = false,
1175
+ }, source));
1176
+ var editor = findByText(view.root, source).?;
1177
+ editor.frame = geometry.RectF.init(0, 0, 300, 80);
1178
+ try testing.expectEqual(@as(u8, 1), editor.code_line_number_digits);
1179
+
1180
+ var commands: [256]canvas.CanvasCommand = undefined;
1181
+ var builder = canvas.Builder.init(&commands);
1182
+ try canvas.emitWidgetTree(&builder, editor, .{});
1183
+
1184
+ var saw_second_line = false;
1185
+ for (builder.displayList().commands) |command| {
1186
+ if (command == .draw_text and std.mem.eql(u8, command.draw_text.text, "2")) {
1187
+ saw_second_line = true;
1188
+ break;
1189
+ }
1190
+ }
1191
+ try testing.expect(saw_second_line);
1192
+ }
1193
+
1194
+ test "wrapped editable code measures vertical extent with its monospace font" {
1195
+ const source = "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii";
1196
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1197
+ defer arena.deinit();
1198
+ var ui = Ui.init(arena.allocator());
1199
+ const view = try ui.finalize(ui.code(.{
1200
+ .language = .plain,
1201
+ .editable = true,
1202
+ .line_numbers = true,
1203
+ .wrap = true,
1204
+ }, source));
1205
+ var editor = findByText(view.root, source).?;
1206
+ editor.frame = geometry.RectF.init(0, 0, 140, 20);
1207
+ const tokens = canvas.DesignTokens{ .text_measure = &code_font_sensitive_measure };
1208
+
1209
+ try testing.expect(canvas.textInputContentExtentForWidget(editor, tokens) > editor.frame.height);
1210
+ try testing.expect(canvas.textInputMaxScrollOffsetForWidget(editor, tokens) > 0);
1211
+ }
1212
+
1213
+ test "wrapped editable code paints the caret row from the interaction width" {
1214
+ const source = "ab";
1215
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1216
+ defer arena.deinit();
1217
+ var ui = Ui.init(arena.allocator());
1218
+ const view = try ui.finalize(ui.code(.{
1219
+ .language = .plain,
1220
+ .editable = true,
1221
+ .wrap = true,
1222
+ }, source));
1223
+ var editor = findByText(view.root, source).?;
1224
+
1225
+ var measured = CodeWidthMeasure{};
1226
+ const provider = canvas.TextMeasureProvider{
1227
+ .context = &measured,
1228
+ .measure_fn = CodeWidthMeasure.width,
1229
+ .measure_advances_fn = CodeWidthMeasure.advances,
1230
+ };
1231
+ const tokens = canvas.DesignTokens{
1232
+ .pixel_snap = .{ .geometry = true, .scale = 1 },
1233
+ .text_measure = &provider,
1234
+ };
1235
+ const text_size = tokens.typography.body_size;
1236
+ // The exact interaction width wraps the second glyph, while the static
1237
+ // paragraph pixel hand-back would make both glyphs fit.
1238
+ editor.frame = geometry.RectF.init(0, 0, text_size * 2 - 0.6, 80);
1239
+ editor.state.focused = true;
1240
+ editor.text_selection = canvas.TextSelection.collapsed(source.len);
1241
+
1242
+ var commands: [128]canvas.CanvasCommand = undefined;
1243
+ var builder = canvas.Builder.init(&commands);
1244
+ try canvas.emitWidgetTree(&builder, editor, tokens);
1245
+
1246
+ var final_glyph_baseline: ?f32 = null;
1247
+ for (builder.displayList().commands) |command| {
1248
+ if (command == .draw_text and std.mem.indexOfScalar(u8, command.draw_text.text, 'b') != null) {
1249
+ final_glyph_baseline = command.draw_text.origin.y;
1250
+ }
1251
+ }
1252
+ const caret = canvas.textGeometryForWidget(editor, tokens).caret_bounds.?;
1253
+ try testing.expect(final_glyph_baseline != null);
1254
+ try testing.expectApproxEqAbs(caret.y + text_size, final_glyph_baseline.?, 0.001);
1255
+ }
1256
+
1257
+ test "numbered editable code does not invent trailing horizontal overflow" {
1258
+ const source = "const concise = true;";
1259
+ const tokens = canvas.DesignTokens{};
1260
+ var editor = canvas.Widget{
1261
+ .kind = .textarea,
1262
+ .code_editor = true,
1263
+ .code_line_number_digits = 2,
1264
+ .text_no_wrap = true,
1265
+ .text = source,
1266
+ .frame = geometry.RectF.init(10, 12, 500, 80),
1267
+ };
1268
+
1269
+ const broad_viewport = canvas.textInputViewportForWidget(editor, tokens).?;
1270
+ const gutter = broad_viewport.x - editor.frame.x;
1271
+ const text_width = canvas.measureTextWidthForFont(
1272
+ tokens.text_measure,
1273
+ tokens.typography.mono_font_id,
1274
+ source,
1275
+ tokens.typography.body_size,
1276
+ );
1277
+ // Exactly enough room for the gutter, source, and one-point caret.
1278
+ editor.frame.width = gutter + text_width + 1;
1279
+
1280
+ const fitted_viewport = canvas.textInputViewportForWidget(editor, tokens).?;
1281
+ try testing.expectApproxEqAbs(editor.frame.maxX(), fitted_viewport.maxX(), 0.001);
1282
+ try testing.expectEqual(@as(f32, 0), canvas.textInputMaxHorizontalScrollOffsetForWidget(editor, tokens));
1283
+ try testing.expectEqual(fitted_viewport.width, canvas.textInputContentWidthForWidget(editor, tokens));
1284
+ }
1285
+
1286
+ test "editable code caches one batched longest-line measurement across geometry reads" {
1287
+ var source: std.ArrayListUnmanaged(u8) = .empty;
1288
+ defer source.deinit(testing.allocator);
1289
+ for (0..1000) |index| {
1290
+ var line_buffer: [48]u8 = undefined;
1291
+ const line = try std.fmt.bufPrint(&line_buffer, "const value_{d} = true;\n", .{index});
1292
+ try source.appendSlice(testing.allocator, line);
1293
+ }
1294
+
1295
+ var measured = CodeWidthMeasure{};
1296
+ const provider = canvas.TextMeasureProvider{
1297
+ .context = &measured,
1298
+ .measure_fn = CodeWidthMeasure.width,
1299
+ .measure_advances_fn = CodeWidthMeasure.advances,
1300
+ };
1301
+ const tokens = canvas.DesignTokens{ .text_measure = &provider };
1302
+ var editor = canvas.Widget{
1303
+ .kind = .textarea,
1304
+ .code_editor = true,
1305
+ .text_no_wrap = true,
1306
+ .text = source.items,
1307
+ .frame = geometry.RectF.init(0, 0, 240, 80),
1308
+ };
1309
+
1310
+ canvas.cacheTextInputContentWidthForWidget(&editor, tokens);
1311
+ canvas.cacheTextInputContentWidthForWidget(&editor, tokens);
1312
+ for (0..4) |_| {
1313
+ _ = canvas.textInputMaxHorizontalScrollOffsetForWidget(editor, tokens);
1314
+ _ = canvas.textInputContentWidthForWidget(editor, tokens);
1315
+ }
1316
+
1317
+ try testing.expectEqual(@as(usize, 1), measured.advance_calls);
1318
+ try testing.expectEqual(@as(usize, 0), measured.width_calls);
1319
+ }
1320
+
1321
+ test "editable code highlights the caret row but not a text selection" {
1322
+ const source = "const first = 1;\nconst second = 2;\n";
1323
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1324
+ defer arena.deinit();
1325
+ var ui = Ui.init(arena.allocator());
1326
+ const view = try ui.finalize(ui.code(.{
1327
+ .language = .zig,
1328
+ .editable = true,
1329
+ .line_numbers = true,
1330
+ .wrap = false,
1331
+ .height = 80,
1332
+ }, source));
1333
+ var editor = findByText(view.root, source).?;
1334
+ editor.frame = geometry.RectF.init(0, 0, 220, 80);
1335
+ editor.state.focused = true;
1336
+ editor.text_selection = canvas.TextSelection.collapsed(std.mem.indexOf(u8, source, "second").?);
1337
+
1338
+ var tokens = canvas.DesignTokens{};
1339
+ tokens.colors.surface_subtle = canvas.Color.rgb8(19, 23, 29);
1340
+ var commands: [128]canvas.CanvasCommand = undefined;
1341
+ var builder = canvas.Builder.init(&commands);
1342
+ try canvas.emitWidgetTree(&builder, editor, tokens);
1343
+
1344
+ const caret = canvas.textGeometryForWidget(editor, tokens).caret_bounds.?;
1345
+ var full_row_index: ?usize = null;
1346
+ var source_index: ?usize = null;
1347
+ var gutter_index: ?usize = null;
1348
+ var gutter_row_index: ?usize = null;
1349
+ var marker_index: ?usize = null;
1350
+ var full_row: ?geometry.RectF = null;
1351
+ var gutter_row: ?geometry.RectF = null;
1352
+ for (builder.displayList().commands, 0..) |command, index| {
1353
+ switch (command) {
1354
+ .fill_rect => |fill| {
1355
+ if (std.meta.eql(fill.fill.color, tokens.colors.surface_subtle)) {
1356
+ if (fill.rect.width == editor.frame.width) {
1357
+ full_row_index = index;
1358
+ full_row = fill.rect;
1359
+ } else {
1360
+ gutter_row_index = index;
1361
+ gutter_row = fill.rect;
1362
+ }
1363
+ } else if (std.meta.eql(fill.fill.color, tokens.colors.background)) {
1364
+ gutter_index = index;
1365
+ }
1366
+ },
1367
+ .draw_text => |draw| {
1368
+ if (std.mem.eql(u8, draw.text, "2")) {
1369
+ marker_index = index;
1370
+ } else if (!std.mem.eql(u8, draw.text, "1") and !std.mem.eql(u8, draw.text, "3")) {
1371
+ source_index = index;
1372
+ }
1373
+ },
1374
+ else => {},
1375
+ }
1376
+ }
1377
+
1378
+ try testing.expect(full_row_index != null);
1379
+ try testing.expect(source_index != null);
1380
+ try testing.expect(gutter_index != null);
1381
+ try testing.expect(gutter_row_index != null);
1382
+ try testing.expect(marker_index != null);
1383
+ try testing.expect(full_row_index.? < source_index.?);
1384
+ try testing.expect(source_index.? < gutter_index.?);
1385
+ try testing.expect(gutter_index.? < gutter_row_index.?);
1386
+ try testing.expect(gutter_row_index.? < marker_index.?);
1387
+ try testing.expectEqual(editor.frame.x, full_row.?.x);
1388
+ try testing.expectEqual(editor.frame.width, full_row.?.width);
1389
+ try testing.expectEqual(caret.y, full_row.?.y);
1390
+ try testing.expectEqual(caret.height, full_row.?.height);
1391
+ try testing.expectEqual(full_row.?.y, gutter_row.?.y);
1392
+ try testing.expectEqual(full_row.?.height, gutter_row.?.height);
1393
+ try testing.expect(gutter_row.?.width < full_row.?.width);
1394
+
1395
+ editor.text_selection = .{ .anchor = 0, .focus = 5 };
1396
+ var selected_commands: [128]canvas.CanvasCommand = undefined;
1397
+ var selected_builder = canvas.Builder.init(&selected_commands);
1398
+ try canvas.emitWidgetTree(&selected_builder, editor, tokens);
1399
+ for (selected_builder.displayList().commands) |command| {
1400
+ if (command == .fill_rect) {
1401
+ try testing.expect(!std.meta.eql(command.fill_rect.fill.color, tokens.colors.surface_subtle));
1402
+ }
1403
+ }
1404
+
1405
+ editor.text_selection = canvas.TextSelection.collapsed(0);
1406
+ editor.state.focused = false;
1407
+ var blurred_commands: [128]canvas.CanvasCommand = undefined;
1408
+ var blurred_builder = canvas.Builder.init(&blurred_commands);
1409
+ try canvas.emitWidgetTree(&blurred_builder, editor, tokens);
1410
+ for (blurred_builder.displayList().commands) |command| {
1411
+ if (command == .fill_rect) {
1412
+ try testing.expect(!std.meta.eql(command.fill_rect.fill.color, tokens.colors.surface_subtle));
1413
+ }
1414
+ }
1415
+ }
1416
+
1417
+ test "editable code Tab infers the file indentation and falls back to two spaces" {
1418
+ try expectCodeTabInsertion("const answer = 42;\n", " ");
1419
+ try expectCodeTabInsertion(
1420
+ \\if (ready) {
1421
+ \\ while (running) {
1422
+ \\ tick();
1423
+ \\ }
1424
+ \\}
1425
+ , " ");
1426
+ try expectCodeTabInsertion(
1427
+ \\if (ready) {
1428
+ \\ while (running) {
1429
+ \\ tick();
1430
+ \\ }
1431
+ \\}
1432
+ , " ");
1433
+ try expectCodeTabInsertion(
1434
+ "if (ready) {\n\twhile (running) {\n\t\ttick();\n\t}\n}\n",
1435
+ "\t",
1436
+ );
1437
+
1438
+ const plain_textarea = canvas.Widget{
1439
+ .kind = .textarea,
1440
+ .text = "plain",
1441
+ };
1442
+ try testing.expect(canvas.widgetCodeTabTextEditEvent(plain_textarea, .{
1443
+ .phase = .key_down,
1444
+ .key = "tab",
1445
+ }) == null);
1446
+ try testing.expect(canvas.widgetCodeTabTextEditEvent(.{
1447
+ .kind = .textarea,
1448
+ .code_editor = true,
1449
+ .text = "code",
1450
+ }, .{
1451
+ .phase = .key_down,
1452
+ .key = "tab",
1453
+ .modifiers = .{ .shift = true },
1454
+ }) == null);
1455
+ try testing.expect(canvas.widgetCodeTabTextEditEvent(.{
1456
+ .kind = .textarea,
1457
+ .code_editor = true,
1458
+ .text = "code",
1459
+ }, .{
1460
+ .phase = .key_down,
1461
+ .key = "tab",
1462
+ .focus_moved = true,
1463
+ }) == null);
1464
+ }
1465
+
523
1466
  test "empty code retains the exact source text" {
524
1467
  var arena = std.heap.ArenaAllocator.init(testing.allocator);
525
1468
  defer arena.deinit();
@@ -574,7 +1517,7 @@ test "large code blocks split at the paragraph line capacity without hiding thei
574
1517
  var group_id: ?canvas.ObjectId = null;
575
1518
  for (chunk_column.children) |chunk| {
576
1519
  try testing.expect(chunk.static_text_group_id != 0);
577
- try testing.expectEqual(expected_offset, chunk.static_text_group_offset);
1520
+ try testing.expectEqual(@as(u64, @intCast(expected_offset)), chunk.static_text_group_offset);
578
1521
  if (group_id) |expected| {
579
1522
  try testing.expectEqual(expected, chunk.static_text_group_id);
580
1523
  } else {
@@ -654,6 +1597,32 @@ test "large code retains all source while emitting only visible text" {
654
1597
  }
655
1598
  }
656
1599
 
1600
+ test "large editable code selection repaints only visible glyphs" {
1601
+ const source = try testing.allocator.alloc(u8, 65_536);
1602
+ defer testing.allocator.free(source);
1603
+ @memset(source, 'x');
1604
+
1605
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1606
+ defer arena.deinit();
1607
+ var ui = Ui.init(arena.allocator());
1608
+ const view = try ui.finalize(ui.code(.{
1609
+ .editable = true,
1610
+ .wrap = false,
1611
+ .height = 80,
1612
+ }, source));
1613
+ var editor = findByText(view.root, source).?;
1614
+ editor.frame = geometry.RectF.init(0, 0, 320, 80);
1615
+ editor.text_selection = .{ .anchor = 0, .focus = 5 };
1616
+
1617
+ var commands: [256]canvas.CanvasCommand = undefined;
1618
+ var builder = canvas.Builder.init(&commands);
1619
+ try canvas.emitWidgetTree(&builder, editor, .{});
1620
+
1621
+ const text_bytes = displayListTextBytes(builder.displayList());
1622
+ try testing.expect(text_bytes <= canvas.max_display_list_text_bytes);
1623
+ try testing.expect(text_bytes < source.len);
1624
+ }
1625
+
657
1626
  test "direct tree code emission honors scroll viewports and later layout pages" {
658
1627
  const horizontal_source = "x" ** 65_536;
659
1628
  const horizontal_spans = [_]canvas.TextSpan{.{
@@ -721,6 +1690,60 @@ test "direct tree code emission honors scroll viewports and later layout pages"
721
1690
  try testing.expect(visible_text_commands > 0);
722
1691
  }
723
1692
 
1693
+ test "editable code highlights the visible tail of a ten-thousand-line source" {
1694
+ var source: std.ArrayListUnmanaged(u8) = .empty;
1695
+ defer source.deinit(testing.allocator);
1696
+ for (0..9_997) |_| {
1697
+ try source.appendSlice(testing.allocator, ":root { --color: #fff; }\n");
1698
+ }
1699
+ try source.appendSlice(testing.allocator, "body {\n color: red;\n}\n");
1700
+
1701
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1702
+ defer arena.deinit();
1703
+ var ui = Ui.init(arena.allocator());
1704
+ const view = try ui.finalize(ui.code(.{
1705
+ .language = .css,
1706
+ .editable = true,
1707
+ .line_numbers = true,
1708
+ .wrap = false,
1709
+ .height = 100,
1710
+ }, source.items));
1711
+ var editor = findByText(view.root, source.items).?;
1712
+ try testing.expect(editor.code_editor);
1713
+ try testing.expectEqual(code_model.Language.css, editor.code_language);
1714
+ try testing.expectEqual(@as(usize, 1), editor.spans.len);
1715
+ try testing.expectEqual(@as(u8, 5), editor.code_line_number_digits);
1716
+
1717
+ const tokens = canvas.DesignTokens{};
1718
+ const line_height = tokens.typography.body_size * 1.25;
1719
+ editor.frame = geometry.RectF.init(0, 0, 500, 100);
1720
+ editor.value = 9_997 * line_height;
1721
+
1722
+ var commands: [256]canvas.CanvasCommand = undefined;
1723
+ var builder = canvas.Builder.init(&commands);
1724
+ try canvas.emitWidgetTree(&builder, editor, tokens);
1725
+
1726
+ var saw_body = false;
1727
+ var saw_property = false;
1728
+ var saw_late_line_number = false;
1729
+ for (builder.displayList().commands) |command| {
1730
+ if (command != .draw_text) continue;
1731
+ const draw = command.draw_text;
1732
+ if (std.mem.eql(u8, draw.text, "body")) {
1733
+ saw_body = true;
1734
+ try testing.expectEqual(tokens.colors.syntax_literal, draw.color);
1735
+ }
1736
+ if (std.mem.eql(u8, draw.text, "color")) {
1737
+ saw_property = true;
1738
+ try testing.expectEqual(tokens.colors.syntax_property, draw.color);
1739
+ }
1740
+ if (std.mem.eql(u8, draw.text, "9998")) saw_late_line_number = true;
1741
+ }
1742
+ try testing.expect(saw_body);
1743
+ try testing.expect(saw_property);
1744
+ try testing.expect(saw_late_line_number);
1745
+ }
1746
+
724
1747
  test "maximal one-line numbered code adds no retained gutter bytes" {
725
1748
  const source = try testing.allocator.alloc(u8, 65_536);
726
1749
  defer testing.allocator.free(source);