@native-sdk/cli 0.7.0 → 0.7.1

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 (52) hide show
  1. package/build.zig +65 -91
  2. package/package.json +9 -9
  3. package/packages/core/package-lock.json +2 -2
  4. package/packages/core/package.json +1 -1
  5. package/packages/core/sdk/text.ts +25 -14
  6. package/packages/core/src/checker.ts +39 -0
  7. package/packages/core/src/emitter.ts +125 -1
  8. package/skill-data/native-ui/SKILL.md +9 -7
  9. package/src/platform/macos/appkit_host.m +25 -1
  10. package/src/platform/macos/cef_host.mm +24 -2
  11. package/src/platform/macos/root.zig +13 -0
  12. package/src/primitives/canvas/code.zig +451 -33
  13. package/src/primitives/canvas/code_tests.zig +855 -5
  14. package/src/primitives/canvas/events.zig +111 -1
  15. package/src/primitives/canvas/markdown_tests.zig +17 -18
  16. package/src/primitives/canvas/root.zig +4 -0
  17. package/src/primitives/canvas/text.zig +6 -0
  18. package/src/primitives/canvas/ui.zig +108 -29
  19. package/src/primitives/canvas/ui_markup.zig +23 -9
  20. package/src/primitives/canvas/ui_markup_compiled.zig +13 -0
  21. package/src/primitives/canvas/ui_markup_compiled_tests.zig +12 -0
  22. package/src/primitives/canvas/ui_markup_contract.zig +8 -1
  23. package/src/primitives/canvas/ui_markup_view.zig +16 -0
  24. package/src/primitives/canvas/ui_markup_view_tests.zig +71 -5
  25. package/src/primitives/canvas/ui_schema.zig +13 -0
  26. package/src/primitives/canvas/ui_schema_tests.zig +9 -7
  27. package/src/primitives/canvas/ui_tests.zig +26 -0
  28. package/src/primitives/canvas/widget_invalidation.zig +1 -0
  29. package/src/primitives/canvas/widget_metrics.zig +10 -5
  30. package/src/primitives/canvas/widget_render.zig +660 -39
  31. package/src/primitives/canvas/widget_render_controls.zig +4 -4
  32. package/src/primitives/canvas/widget_runtime.zig +2 -0
  33. package/src/primitives/canvas/widget_text_input.zig +165 -8
  34. package/src/primitives/canvas/widget_tree.zig +6 -5
  35. package/src/primitives/canvas/widgets.zig +25 -0
  36. package/src/runtime/canvas_budget_edge_tests.zig +3 -2
  37. package/src/runtime/canvas_limits.zig +13 -6
  38. package/src/runtime/canvas_widget_clipboard_tests.zig +7 -4
  39. package/src/runtime/canvas_widget_context_menu.zig +7 -2
  40. package/src/runtime/canvas_widget_event_tests.zig +18 -14
  41. package/src/runtime/canvas_widget_events.zig +51 -20
  42. package/src/runtime/canvas_widget_runtime.zig +105 -4
  43. package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
  44. package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
  45. package/src/runtime/canvas_widget_state.zig +27 -0
  46. package/src/runtime/canvas_widget_text_tests.zig +326 -0
  47. package/src/runtime/core.zig +15 -5
  48. package/src/runtime/gpu_surface_events.zig +12 -12
  49. package/src/runtime/view.zig +54 -16
  50. package/src/runtime/view_widget_scroll.zig +48 -18
  51. package/src/runtime/view_widget_text.zig +54 -13
  52. package/src/runtime/window_view_runtime.zig +9 -0
@@ -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,15 @@ fn spanWithFragment(spans: []const canvas.TextSpan, fragment: []const u8) ?canva
15
16
  return null;
16
17
  }
17
18
 
19
+ fn colorAtOffset(spans: []const canvas.TextSpan, offset: usize) ?canvas.TextSpanColor {
20
+ var cursor: usize = 0;
21
+ for (spans) |span| {
22
+ if (offset < cursor + span.text.len) return span.color;
23
+ cursor += span.text.len;
24
+ }
25
+ return null;
26
+ }
27
+
18
28
  fn findByKind(widget: canvas.Widget, kind: canvas.WidgetKind) ?canvas.Widget {
19
29
  if (widget.kind == kind) return widget;
20
30
  for (widget.children) |child| {
@@ -59,6 +69,32 @@ fn displayListTextBytes(display_list: canvas.DisplayList) usize {
59
69
  return count;
60
70
  }
61
71
 
72
+ fn codeFontSensitiveMeasure(context: ?*anyopaque, font_id: canvas.FontId, size: f32, text: []const u8) f32 {
73
+ _ = context;
74
+ const advance: f32 = if (font_id == canvas.default_mono_font_id) 1 else 0.1;
75
+ return size * advance * @as(f32, @floatFromInt(text.len));
76
+ }
77
+
78
+ const code_font_sensitive_measure = canvas.TextMeasureProvider{ .measure_fn = codeFontSensitiveMeasure };
79
+
80
+ const CodeWidthMeasure = struct {
81
+ width_calls: usize = 0,
82
+ advance_calls: usize = 0,
83
+
84
+ fn width(context: ?*anyopaque, _: canvas.FontId, size: f32, text: []const u8) f32 {
85
+ const self: *CodeWidthMeasure = @ptrCast(@alignCast(context.?));
86
+ self.width_calls += 1;
87
+ return size * @as(f32, @floatFromInt(text.len));
88
+ }
89
+
90
+ fn advances(context: ?*anyopaque, _: canvas.FontId, size: f32, text: []const u8, out: []f32) bool {
91
+ const self: *CodeWidthMeasure = @ptrCast(@alignCast(context.?));
92
+ self.advance_calls += 1;
93
+ for (text, 0..) |byte, index| out[index] = if (byte == '\n') 0 else size;
94
+ return true;
95
+ }
96
+ };
97
+
62
98
  fn expectCompleteSpanLayouts(widget: canvas.Widget) !void {
63
99
  if (widget.kind == .text and widget.spans.len > 0) {
64
100
  var runs: [text_spans.max_text_span_runs_per_paragraph]text_spans.TextSpanRun = undefined;
@@ -85,6 +121,21 @@ fn allTextSpansHaveColor(widget: canvas.Widget, color: canvas.TextSpanColor) boo
85
121
  return true;
86
122
  }
87
123
 
124
+ fn expectCodeTabInsertion(source: []const u8, expected: []const u8) !void {
125
+ const edit = canvas.widgetCodeTabTextEditEvent(.{
126
+ .kind = .textarea,
127
+ .code_editor = true,
128
+ .text = source,
129
+ }, .{
130
+ .phase = .key_down,
131
+ .key = "tab",
132
+ }).?;
133
+ switch (edit) {
134
+ .insert_text => |text| try testing.expectEqualStrings(expected, text),
135
+ else => return error.TestExpectedEqual,
136
+ }
137
+ }
138
+
88
139
  test "HTML and JSX highlighting distinguishes tags attributes expressions and strings" {
89
140
  const source =
90
141
  \\<Accordion defaultValue={["item-1"]}>
@@ -94,7 +145,12 @@ test "HTML and JSX highlighting distinguishes tags attributes expressions and st
94
145
  var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
95
146
  const spans = code_model.highlight(source, code_model.languageFromName("tsx"), &storage);
96
147
 
97
- try testing.expectEqual(code_model.Language.html, code_model.languageFromName("jsx"));
148
+ try testing.expectEqual(code_model.Language.jsx, code_model.languageFromName("jsx"));
149
+ try testing.expectEqual(code_model.Language.tsx, code_model.languageFromName("tsx"));
150
+ try testing.expectEqual(code_model.Language.javascript, code_model.languageFromName("mjs"));
151
+ try testing.expectEqual(code_model.Language.yaml, code_model.languageFromName("yaml"));
152
+ try testing.expectEqual(code_model.Language.yaml, code_model.languageFromName("yml"));
153
+ try testing.expectEqual(code_model.Language.markdown, code_model.languageFromName("md"));
98
154
  try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "Accordion").?.color.?);
99
155
  try testing.expectEqual(canvas.TextSpanColor.syntax_function, spanWithFragment(spans, "defaultValue").?.color.?);
100
156
  try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "\"item-1\"").?.color.?);
@@ -102,6 +158,54 @@ test "HTML and JSX highlighting distinguishes tags attributes expressions and st
102
158
  try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(spans, "Accessible?").?.color.?);
103
159
  }
104
160
 
161
+ test "YAML highlighting distinguishes mapping keys scalars symbols and comments" {
162
+ const source =
163
+ \\---
164
+ \\name: native
165
+ \\runs-on: macos-15
166
+ \\enabled: true
167
+ \\empty: ~
168
+ \\defaults: &defaults
169
+ \\command: "zig build test"
170
+ \\use: *defaults
171
+ \\url: https://example.test/#section # trailing comment
172
+ ;
173
+ var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
174
+ const spans = code_model.highlight(source, .yaml, &storage);
175
+
176
+ try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(spans, "---").?.color.?);
177
+ try testing.expectEqual(canvas.TextSpanColor.syntax_property, spanWithFragment(spans, "name").?.color.?);
178
+ try testing.expectEqual(canvas.TextSpanColor.syntax_property, spanWithFragment(spans, "runs-on").?.color.?);
179
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "true").?.color.?);
180
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "~").?.color.?);
181
+ try testing.expectEqual(canvas.TextSpanColor.syntax_constant, spanWithFragment(spans, "&defaults").?.color.?);
182
+ try testing.expectEqual(canvas.TextSpanColor.syntax_constant, spanWithFragment(spans, "*defaults").?.color.?);
183
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "\"zig build test\"").?.color.?);
184
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(spans, "#section").?.color.?);
185
+ try testing.expectEqual(canvas.TextSpanColor.syntax_comment, spanWithFragment(spans, "# trailing comment").?.color.?);
186
+ }
187
+
188
+ test "TSX highlights top-level TypeScript and embedded JSX in one grammar" {
189
+ const source =
190
+ \\import type { Metadata } from "next";
191
+ \\export default function RootLayout() { return <html lang="en" />; }
192
+ ;
193
+ var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
194
+ const spans = code_model.highlight(source, .tsx, &storage);
195
+
196
+ inline for (.{ "import", "type", "from", "export", "default", "function", "return" }) |keyword| {
197
+ try testing.expectEqual(
198
+ canvas.TextSpanColor.syntax_keyword,
199
+ spanWithFragment(spans, keyword).?.color.?,
200
+ );
201
+ }
202
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "\"next\"").?.color.?);
203
+ try testing.expectEqual(canvas.TextSpanColor.syntax_function, spanWithFragment(spans, "RootLayout").?.color.?);
204
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "html").?.color.?);
205
+ try testing.expectEqual(canvas.TextSpanColor.syntax_function, spanWithFragment(spans, "lang").?.color.?);
206
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "\"en\"").?.color.?);
207
+ }
208
+
105
209
  test "JSX relational less-than preserves expression state and nested tags" {
106
210
  const source = "<span>{count<limit ? <Low/> : <High/>}</span>";
107
211
  var state: code_model.HighlightState = .{};
@@ -124,6 +228,83 @@ test "JSX relational less-than preserves expression state and nested tags" {
124
228
  try testing.expect(!chunked_state.html_in_tag);
125
229
  }
126
230
 
231
+ test "TSX relational less-than at top level does not open a tag" {
232
+ const source = "const ok=count<limit; const later=true;";
233
+ var state: code_model.HighlightState = .{};
234
+ var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
235
+ const spans = code_model.highlightWithState(source, .tsx, &storage, &state);
236
+
237
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(spans, "limit").?.color.?);
238
+ try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(spans, "const").?.color.?);
239
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "true").?.color.?);
240
+ try testing.expect(!state.html_in_tag);
241
+
242
+ var chunked_state: code_model.HighlightState = .{};
243
+ var first_storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
244
+ _ = code_model.highlightWithState("const ok=count", .tsx, &first_storage, &chunked_state);
245
+ var second_storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
246
+ const second = code_model.highlightWithState("<limit; const later=true;", .tsx, &second_storage, &chunked_state);
247
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(second, "limit").?.color.?);
248
+ try testing.expect(!chunked_state.html_in_tag);
249
+ }
250
+
251
+ test "JSX closing tags remain structural after plain text" {
252
+ const source = "const view = <div>Hello<span>world</span></div>; const later = true;";
253
+ var state: code_model.HighlightState = .{};
254
+ var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
255
+ const spans = code_model.highlightWithState(source, .tsx, &storage, &state);
256
+ const closing_name = std.mem.lastIndexOf(u8, source, "div").?;
257
+ const nested_opening_name = std.mem.indexOf(u8, source, "span").?;
258
+ const nested_closing_name = std.mem.lastIndexOf(u8, source, "span").?;
259
+ const later_name = std.mem.indexOf(u8, source, "later").?;
260
+
261
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, colorAtOffset(spans, closing_name).?);
262
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, colorAtOffset(spans, nested_opening_name).?);
263
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, colorAtOffset(spans, nested_closing_name).?);
264
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, colorAtOffset(spans, later_name).?);
265
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "true").?.color.?);
266
+ try testing.expectEqual(@as(usize, 0), state.html_element_len);
267
+ try testing.expect(!state.html_in_tag);
268
+ }
269
+
270
+ test "JSX regex comparisons inside elements do not become closing tags" {
271
+ const source = "const view = <div>{value</x/.test(text) ? 'yes' : 'no'}</div>;";
272
+ var state: code_model.HighlightState = .{};
273
+ var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
274
+ const spans = code_model.highlightWithState(source, .tsx, &storage, &state);
275
+ const regex_name = std.mem.indexOf(u8, source, "x/").?;
276
+ const closing_name = std.mem.lastIndexOf(u8, source, "div").?;
277
+
278
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, colorAtOffset(spans, regex_name).?);
279
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, colorAtOffset(spans, closing_name).?);
280
+ try testing.expectEqual(@as(usize, 0), state.html_element_len);
281
+ try testing.expectEqual(@as(usize, 0), state.html_expression_depth);
282
+ try testing.expect(!state.html_in_tag);
283
+ }
284
+
285
+ test "Markdown highlighting distinguishes structure links code and comments" {
286
+ const source =
287
+ \\# Heading
288
+ \\- [Native](https://native.dev) uses `code` and **emphasis**.
289
+ \\<!-- note -->
290
+ \\```zig
291
+ \\const value = true;
292
+ \\```
293
+ ;
294
+ var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
295
+ const spans = code_model.highlight(source, .markdown, &storage);
296
+
297
+ try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(spans, "#").?.color.?);
298
+ try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(spans, "-").?.color.?);
299
+ try testing.expectEqual(canvas.TextSpanColor.syntax_property, spanWithFragment(spans, "[").?.color.?);
300
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "https://native.dev").?.color.?);
301
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "`code`").?.color.?);
302
+ try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(spans, "**").?.color.?);
303
+ try testing.expectEqual(canvas.TextSpanColor.syntax_comment, spanWithFragment(spans, "<!-- note -->").?.color.?);
304
+ try testing.expectEqual(canvas.TextSpanColor.syntax_constant, spanWithFragment(spans, "zig").?.color.?);
305
+ try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "const value").?.color.?);
306
+ }
307
+
127
308
  test "nested JSX attribute tags restore the enclosing opening tag" {
128
309
  const source = "<Comp render={<Inner />} disabled=\"yes\">";
129
310
  var state: code_model.HighlightState = .{};
@@ -273,15 +454,26 @@ test "token-dense fallback still updates lexer state" {
273
454
  try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(second, "const").?.color.?);
274
455
  }
275
456
 
276
- test "callables and object properties use Geist syntax roles" {
277
- const source = "function render() { return { color: true }; }";
457
+ test "callables and object keys use variable syntax roles" {
458
+ const source = "function render() { const variable = { color: true }; return variable; }";
278
459
  var storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
279
460
  const spans = code_model.highlight(source, .javascript, &storage);
280
461
 
281
462
  try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(spans, "function").?.color.?);
282
463
  try testing.expectEqual(canvas.TextSpanColor.syntax_function, spanWithFragment(spans, "render").?.color.?);
283
- try testing.expectEqual(canvas.TextSpanColor.syntax_property, spanWithFragment(spans, "color").?.color.?);
464
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(spans, "variable").?.color.?);
465
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(spans, "color").?.color.?);
284
466
  try testing.expectEqual(canvas.TextSpanColor.syntax_literal, spanWithFragment(spans, "true").?.color.?);
467
+
468
+ const typed_source = "const options: { enabled: boolean } = { enabled: true };";
469
+ var typed_storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
470
+ const typed_spans = code_model.highlight(typed_source, .typescript, &typed_storage);
471
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(typed_spans, "options").?.color.?);
472
+ try testing.expectEqual(canvas.TextSpanColor.syntax_plain, spanWithFragment(typed_spans, "enabled").?.color.?);
473
+
474
+ var css_storage: [text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
475
+ const css_spans = code_model.highlight("body { color: red; }", .css, &css_storage);
476
+ try testing.expectEqual(canvas.TextSpanColor.syntax_property, spanWithFragment(css_spans, "color").?.color.?);
285
477
  }
286
478
 
287
479
  test "both built-in packs use the Geist Code Block syntax palette" {
@@ -328,7 +520,11 @@ test "code wraps by default and no-wrap composes one horizontal scroller" {
328
520
  defer wrapped_arena.deinit();
329
521
  var wrapped_ui = Ui.init(wrapped_arena.allocator());
330
522
  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);
523
+ try testing.expectEqual(canvas.WidgetKind.column, wrapped.root.kind);
524
+ try testing.expectEqual(geometry.InsetsF{}, wrapped.root.layout.padding);
525
+ try testing.expect(wrapped.root.style.background == null);
526
+ try testing.expect(wrapped.root.style.border == null);
527
+ try testing.expect(wrapped.root.style.radius == null);
332
528
  try testing.expect(findByKind(wrapped.root, .scroll_view) == null);
333
529
  const wrapped_text = findByKind(wrapped.root, .text).?;
334
530
  try testing.expect(!wrapped_text.text_no_wrap);
@@ -357,6 +553,71 @@ test "code wraps by default and no-wrap composes one horizontal scroller" {
357
553
  try testing.expect(text_width > scroll_width);
358
554
  }
359
555
 
556
+ test "editable code layout paints syntax-highlighted token runs" {
557
+ const source = "const answer: u32 = 42;";
558
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
559
+ defer arena.deinit();
560
+ var ui = Ui.init(arena.allocator());
561
+ const view = try ui.finalize(ui.code(.{
562
+ .language = .zig,
563
+ .editable = true,
564
+ .width = 320,
565
+ .height = 80,
566
+ }, source));
567
+
568
+ var nodes: [8]canvas.WidgetLayoutNode = undefined;
569
+ const layout = try canvas.layoutWidgetTree(
570
+ view.root,
571
+ geometry.RectF.init(0, 0, 320, 80),
572
+ &nodes,
573
+ );
574
+ const tokens = canvas.DesignTokens{};
575
+ var commands: [64]canvas.CanvasCommand = undefined;
576
+ var builder = canvas.Builder.init(&commands);
577
+ try canvas.emitWidgetLayout(&builder, layout, tokens);
578
+
579
+ var saw_keyword = false;
580
+ var saw_type = false;
581
+ var saw_literal = false;
582
+ for (builder.displayList().commands) |command| {
583
+ switch (command) {
584
+ .draw_text => |draw| {
585
+ if (std.mem.eql(u8, draw.text, "const")) {
586
+ try testing.expectEqual(tokens.colors.syntax_keyword, draw.color);
587
+ saw_keyword = true;
588
+ } else if (std.mem.eql(u8, draw.text, "u32")) {
589
+ try testing.expectEqual(tokens.colors.syntax_literal, draw.color);
590
+ saw_type = true;
591
+ } else if (std.mem.eql(u8, draw.text, "42")) {
592
+ try testing.expectEqual(tokens.colors.syntax_literal, draw.color);
593
+ saw_literal = true;
594
+ }
595
+ },
596
+ else => {},
597
+ }
598
+ }
599
+ try testing.expect(saw_keyword);
600
+ try testing.expect(saw_type);
601
+ try testing.expect(saw_literal);
602
+
603
+ var selected_editor = view.root;
604
+ // Runtime-owned text storage can be distinct from the highlighted
605
+ // spans' source storage between an edit and the controlled rebuild.
606
+ selected_editor.text = try arena.allocator().dupe(u8, source);
607
+ selected_editor.text_selection = .{ .anchor = 2, .focus = 12 };
608
+ selected_editor.state.focused = false;
609
+ const selected_layout = try canvas.layoutWidgetTree(
610
+ selected_editor,
611
+ geometry.RectF.init(0, 0, 320, 80),
612
+ &nodes,
613
+ );
614
+ var selected_commands: [64]canvas.CanvasCommand = undefined;
615
+ var selected_builder = canvas.Builder.init(&selected_commands);
616
+ try canvas.emitWidgetLayout(&selected_builder, selected_layout, tokens);
617
+ var changes: [64]canvas.DiffChange = undefined;
618
+ _ = try canvas.DisplayList.diff(.{}, selected_builder.displayList(), &changes);
619
+ }
620
+
360
621
  test "unwrapped multiline code hugs the height of every logical line" {
361
622
  var arena = std.heap.ArenaAllocator.init(testing.allocator);
362
623
  defer arena.deinit();
@@ -520,6 +781,515 @@ test "line numbers are opt-in and stay paired with logical source lines" {
520
781
  try testing.expectEqual(canvas.TextSpanColor.syntax_keyword, spanWithFragment(comment_text.spans, "const").?.color.?);
521
782
  }
522
783
 
784
+ test "line number gutter reserves at least three marker columns" {
785
+ const tokens = canvas.DesignTokens{};
786
+ var numbered = canvas.Widget{
787
+ .kind = .textarea,
788
+ .code_editor = true,
789
+ .code_line_number_digits = 1,
790
+ };
791
+ const one_digit_width = widget_metrics.widgetCodeLineNumberGutterWidth(numbered, tokens);
792
+ numbered.code_line_number_digits = 3;
793
+ const three_digit_width = widget_metrics.widgetCodeLineNumberGutterWidth(numbered, tokens);
794
+ numbered.code_line_number_digits = 4;
795
+ const four_digit_width = widget_metrics.widgetCodeLineNumberGutterWidth(numbered, tokens);
796
+
797
+ try testing.expectEqual(three_digit_width, one_digit_width);
798
+ try testing.expect(four_digit_width > three_digit_width);
799
+ }
800
+
801
+ test "editable line-number gutter masks horizontally scrolled source" {
802
+ const source = "const deliberately_long_identifier = another_deliberately_long_identifier;\n";
803
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
804
+ defer arena.deinit();
805
+ var ui = Ui.init(arena.allocator());
806
+ const view = try ui.finalize(ui.code(.{
807
+ .language = .zig,
808
+ .editable = true,
809
+ .line_numbers = true,
810
+ .wrap = false,
811
+ .height = 80,
812
+ }, source));
813
+ var editor = findByText(view.root, source).?;
814
+ editor.frame = geometry.RectF.init(0, 0, 220, 80);
815
+ editor.value_x = 48;
816
+
817
+ const tokens = canvas.DesignTokens{};
818
+ var commands: [128]canvas.CanvasCommand = undefined;
819
+ var builder = canvas.Builder.init(&commands);
820
+ try canvas.emitWidgetTree(&builder, editor, tokens);
821
+
822
+ var source_index: ?usize = null;
823
+ var gutter_index: ?usize = null;
824
+ var marker_index: ?usize = null;
825
+ var gutter_rect: ?geometry.RectF = null;
826
+ for (builder.displayList().commands, 0..) |command, index| {
827
+ switch (command) {
828
+ .fill_rect => |fill| {
829
+ if (std.meta.eql(fill.fill.color, tokens.colors.background)) {
830
+ gutter_index = index;
831
+ gutter_rect = fill.rect;
832
+ }
833
+ },
834
+ .draw_text => |draw| {
835
+ if (std.mem.eql(u8, draw.text, "1") or std.mem.eql(u8, draw.text, "2")) {
836
+ marker_index = index;
837
+ } else {
838
+ source_index = index;
839
+ }
840
+ },
841
+ else => {},
842
+ }
843
+ }
844
+
845
+ try testing.expect(source_index != null);
846
+ try testing.expect(gutter_index != null);
847
+ try testing.expect(marker_index != null);
848
+ try testing.expect(source_index.? < gutter_index.?);
849
+ try testing.expect(gutter_index.? < marker_index.?);
850
+ try testing.expectEqual(editor.frame.x, gutter_rect.?.x);
851
+ try testing.expectEqual(editor.frame.y, gutter_rect.?.y);
852
+ try testing.expect(gutter_rect.?.width > 0);
853
+ try testing.expectEqual(editor.frame.height, gutter_rect.?.height);
854
+ }
855
+
856
+ test "wrapped numbered editable code emits one retained gutter" {
857
+ const source = "alpha beta gamma delta epsilon zeta eta theta";
858
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
859
+ defer arena.deinit();
860
+ var ui = Ui.init(arena.allocator());
861
+ const view = try ui.finalize(ui.code(.{
862
+ .language = .plain,
863
+ .editable = true,
864
+ .line_numbers = true,
865
+ .wrap = true,
866
+ }, source));
867
+ var editor = findByText(view.root, source).?;
868
+ editor.frame = geometry.RectF.init(0, 0, 120, 80);
869
+
870
+ var commands: [256]canvas.CanvasCommand = undefined;
871
+ var builder = canvas.Builder.init(&commands);
872
+ try canvas.emitWidgetTree(&builder, editor, .{});
873
+
874
+ var line_one_count: usize = 0;
875
+ for (builder.displayList().commands) |command| {
876
+ if (command == .draw_text and std.mem.eql(u8, command.draw_text.text, "1")) {
877
+ line_one_count += 1;
878
+ }
879
+ }
880
+ try testing.expectEqual(@as(usize, 1), line_one_count);
881
+
882
+ var changes: [256]canvas.DiffChange = undefined;
883
+ _ = try canvas.DisplayList.diff(.{}, builder.displayList(), &changes);
884
+ }
885
+
886
+ test "wrapped editable code re-highlights runtime-owned text" {
887
+ const source = "const stale = true;";
888
+ const edited = "fn fresh() void {}";
889
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
890
+ defer arena.deinit();
891
+ var ui = Ui.init(arena.allocator());
892
+ const view = try ui.finalize(ui.code(.{
893
+ .language = .zig,
894
+ .editable = true,
895
+ .wrap = true,
896
+ }, source));
897
+ var editor = findByText(view.root, source).?;
898
+ editor.text = edited;
899
+ editor.frame = geometry.RectF.init(0, 0, 300, 80);
900
+
901
+ var commands: [256]canvas.CanvasCommand = undefined;
902
+ var builder = canvas.Builder.init(&commands);
903
+ try canvas.emitWidgetTree(&builder, editor, .{});
904
+
905
+ var rendered: std.ArrayListUnmanaged(u8) = .empty;
906
+ defer rendered.deinit(testing.allocator);
907
+ for (builder.displayList().commands) |command| {
908
+ if (command == .draw_text) try rendered.appendSlice(testing.allocator, command.draw_text.text);
909
+ }
910
+ try testing.expectEqualStrings(edited, rendered.items);
911
+ }
912
+
913
+ test "wrapped editable code preserves syntax after the document exhausts the span budget" {
914
+ var source: std.ArrayListUnmanaged(u8) = .empty;
915
+ defer source.deinit(testing.allocator);
916
+ for (0..40) |index| {
917
+ var line_buffer: [64]u8 = undefined;
918
+ const line = try std.fmt.bufPrint(&line_buffer, "const value_{d} = \"ordinary\";\n", .{index});
919
+ try source.appendSlice(testing.allocator, line);
920
+ }
921
+ try source.appendSlice(testing.allocator, "const tail = \"TAIL_SENTINEL\";");
922
+
923
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
924
+ defer arena.deinit();
925
+ var ui = Ui.init(arena.allocator());
926
+ const view = try ui.finalize(ui.code(.{
927
+ .language = .zig,
928
+ .editable = true,
929
+ .wrap = true,
930
+ }, source.items));
931
+ var editor = findByText(view.root, source.items).?;
932
+ editor.frame = geometry.RectF.init(0, 0, 800, 1_000);
933
+
934
+ const tokens = canvas.DesignTokens{};
935
+ var commands: [512]canvas.CanvasCommand = undefined;
936
+ var builder = canvas.Builder.init(&commands);
937
+ try canvas.emitWidgetTree(&builder, editor, tokens);
938
+
939
+ var tail_color: ?canvas.Color = null;
940
+ for (builder.displayList().commands) |command| {
941
+ if (command != .draw_text) continue;
942
+ if (std.mem.indexOf(u8, command.draw_text.text, "TAIL_SENTINEL") != null) {
943
+ tail_color = command.draw_text.color;
944
+ break;
945
+ }
946
+ }
947
+ try testing.expectEqual(tokens.colors.syntax_literal, tail_color.?);
948
+ }
949
+
950
+ test "wrapped editable code paints the visible tail of an over-capacity logical line" {
951
+ const source = ("x" ** (text_spans.max_text_span_lines_per_paragraph * 2 + 8)) ++ "Z";
952
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
953
+ defer arena.deinit();
954
+ var ui = Ui.init(arena.allocator());
955
+ const view = try ui.finalize(ui.code(.{
956
+ .language = .plain,
957
+ .editable = true,
958
+ .wrap = true,
959
+ }, source));
960
+ var editor = findByText(view.root, source).?;
961
+
962
+ var measured = CodeWidthMeasure{};
963
+ const provider = canvas.TextMeasureProvider{
964
+ .context = &measured,
965
+ .measure_fn = CodeWidthMeasure.width,
966
+ .measure_advances_fn = CodeWidthMeasure.advances,
967
+ };
968
+ const tokens = canvas.DesignTokens{ .text_measure = &provider };
969
+ const line_height = tokens.typography.body_size * 1.25;
970
+ editor.frame = geometry.RectF.init(
971
+ 0,
972
+ 0,
973
+ tokens.typography.body_size * 2,
974
+ line_height * 2,
975
+ );
976
+ editor.value = canvas.textInputMaxScrollOffsetForWidget(editor, tokens);
977
+ try testing.expect(editor.value > line_height * text_spans.max_text_span_lines_per_paragraph);
978
+
979
+ var commands: [64]canvas.CanvasCommand = undefined;
980
+ var builder = canvas.Builder.init(&commands);
981
+ try canvas.emitWidgetTree(&builder, editor, tokens);
982
+
983
+ var saw_tail = false;
984
+ for (builder.displayList().commands) |command| {
985
+ if (command == .draw_text and std.mem.indexOfScalar(u8, command.draw_text.text, 'Z') != null) {
986
+ saw_tail = true;
987
+ }
988
+ }
989
+ try testing.expect(saw_tail);
990
+ }
991
+
992
+ test "editable code numbers the empty caret row after a terminal newline" {
993
+ const source = "alpha\n";
994
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
995
+ defer arena.deinit();
996
+ var ui = Ui.init(arena.allocator());
997
+ const view = try ui.finalize(ui.code(.{
998
+ .language = .plain,
999
+ .editable = true,
1000
+ .line_numbers = true,
1001
+ .wrap = false,
1002
+ }, source));
1003
+ var editor = findByText(view.root, source).?;
1004
+ editor.frame = geometry.RectF.init(0, 0, 300, 80);
1005
+ try testing.expectEqual(@as(u8, 1), editor.code_line_number_digits);
1006
+
1007
+ var commands: [256]canvas.CanvasCommand = undefined;
1008
+ var builder = canvas.Builder.init(&commands);
1009
+ try canvas.emitWidgetTree(&builder, editor, .{});
1010
+
1011
+ var saw_second_line = false;
1012
+ for (builder.displayList().commands) |command| {
1013
+ if (command == .draw_text and std.mem.eql(u8, command.draw_text.text, "2")) {
1014
+ saw_second_line = true;
1015
+ break;
1016
+ }
1017
+ }
1018
+ try testing.expect(saw_second_line);
1019
+ }
1020
+
1021
+ test "wrapped editable code measures vertical extent with its monospace font" {
1022
+ const source = "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii";
1023
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1024
+ defer arena.deinit();
1025
+ var ui = Ui.init(arena.allocator());
1026
+ const view = try ui.finalize(ui.code(.{
1027
+ .language = .plain,
1028
+ .editable = true,
1029
+ .line_numbers = true,
1030
+ .wrap = true,
1031
+ }, source));
1032
+ var editor = findByText(view.root, source).?;
1033
+ editor.frame = geometry.RectF.init(0, 0, 140, 20);
1034
+ const tokens = canvas.DesignTokens{ .text_measure = &code_font_sensitive_measure };
1035
+
1036
+ try testing.expect(canvas.textInputContentExtentForWidget(editor, tokens) > editor.frame.height);
1037
+ try testing.expect(canvas.textInputMaxScrollOffsetForWidget(editor, tokens) > 0);
1038
+ }
1039
+
1040
+ test "wrapped editable code paints the caret row from the interaction width" {
1041
+ const source = "ab";
1042
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1043
+ defer arena.deinit();
1044
+ var ui = Ui.init(arena.allocator());
1045
+ const view = try ui.finalize(ui.code(.{
1046
+ .language = .plain,
1047
+ .editable = true,
1048
+ .wrap = true,
1049
+ }, source));
1050
+ var editor = findByText(view.root, source).?;
1051
+
1052
+ var measured = CodeWidthMeasure{};
1053
+ const provider = canvas.TextMeasureProvider{
1054
+ .context = &measured,
1055
+ .measure_fn = CodeWidthMeasure.width,
1056
+ .measure_advances_fn = CodeWidthMeasure.advances,
1057
+ };
1058
+ const tokens = canvas.DesignTokens{
1059
+ .pixel_snap = .{ .geometry = true, .scale = 1 },
1060
+ .text_measure = &provider,
1061
+ };
1062
+ const text_size = tokens.typography.body_size;
1063
+ // The exact interaction width wraps the second glyph, while the static
1064
+ // paragraph pixel hand-back would make both glyphs fit.
1065
+ editor.frame = geometry.RectF.init(0, 0, text_size * 2 - 0.6, 80);
1066
+ editor.state.focused = true;
1067
+ editor.text_selection = canvas.TextSelection.collapsed(source.len);
1068
+
1069
+ var commands: [128]canvas.CanvasCommand = undefined;
1070
+ var builder = canvas.Builder.init(&commands);
1071
+ try canvas.emitWidgetTree(&builder, editor, tokens);
1072
+
1073
+ var final_glyph_baseline: ?f32 = null;
1074
+ for (builder.displayList().commands) |command| {
1075
+ if (command == .draw_text and std.mem.indexOfScalar(u8, command.draw_text.text, 'b') != null) {
1076
+ final_glyph_baseline = command.draw_text.origin.y;
1077
+ }
1078
+ }
1079
+ const caret = canvas.textGeometryForWidget(editor, tokens).caret_bounds.?;
1080
+ try testing.expect(final_glyph_baseline != null);
1081
+ try testing.expectApproxEqAbs(caret.y + text_size, final_glyph_baseline.?, 0.001);
1082
+ }
1083
+
1084
+ test "numbered editable code does not invent trailing horizontal overflow" {
1085
+ const source = "const concise = true;";
1086
+ const tokens = canvas.DesignTokens{};
1087
+ var editor = canvas.Widget{
1088
+ .kind = .textarea,
1089
+ .code_editor = true,
1090
+ .code_line_number_digits = 2,
1091
+ .text_no_wrap = true,
1092
+ .text = source,
1093
+ .frame = geometry.RectF.init(10, 12, 500, 80),
1094
+ };
1095
+
1096
+ const broad_viewport = canvas.textInputViewportForWidget(editor, tokens).?;
1097
+ const gutter = broad_viewport.x - editor.frame.x;
1098
+ const text_width = canvas.measureTextWidthForFont(
1099
+ tokens.text_measure,
1100
+ tokens.typography.mono_font_id,
1101
+ source,
1102
+ tokens.typography.body_size,
1103
+ );
1104
+ // Exactly enough room for the gutter, source, and one-point caret.
1105
+ editor.frame.width = gutter + text_width + 1;
1106
+
1107
+ const fitted_viewport = canvas.textInputViewportForWidget(editor, tokens).?;
1108
+ try testing.expectApproxEqAbs(editor.frame.maxX(), fitted_viewport.maxX(), 0.001);
1109
+ try testing.expectEqual(@as(f32, 0), canvas.textInputMaxHorizontalScrollOffsetForWidget(editor, tokens));
1110
+ try testing.expectEqual(fitted_viewport.width, canvas.textInputContentWidthForWidget(editor, tokens));
1111
+ }
1112
+
1113
+ test "editable code caches one batched longest-line measurement across geometry reads" {
1114
+ var source: std.ArrayListUnmanaged(u8) = .empty;
1115
+ defer source.deinit(testing.allocator);
1116
+ for (0..1000) |index| {
1117
+ var line_buffer: [48]u8 = undefined;
1118
+ const line = try std.fmt.bufPrint(&line_buffer, "const value_{d} = true;\n", .{index});
1119
+ try source.appendSlice(testing.allocator, line);
1120
+ }
1121
+
1122
+ var measured = CodeWidthMeasure{};
1123
+ const provider = canvas.TextMeasureProvider{
1124
+ .context = &measured,
1125
+ .measure_fn = CodeWidthMeasure.width,
1126
+ .measure_advances_fn = CodeWidthMeasure.advances,
1127
+ };
1128
+ const tokens = canvas.DesignTokens{ .text_measure = &provider };
1129
+ var editor = canvas.Widget{
1130
+ .kind = .textarea,
1131
+ .code_editor = true,
1132
+ .text_no_wrap = true,
1133
+ .text = source.items,
1134
+ .frame = geometry.RectF.init(0, 0, 240, 80),
1135
+ };
1136
+
1137
+ canvas.cacheTextInputContentWidthForWidget(&editor, tokens);
1138
+ canvas.cacheTextInputContentWidthForWidget(&editor, tokens);
1139
+ for (0..4) |_| {
1140
+ _ = canvas.textInputMaxHorizontalScrollOffsetForWidget(editor, tokens);
1141
+ _ = canvas.textInputContentWidthForWidget(editor, tokens);
1142
+ }
1143
+
1144
+ try testing.expectEqual(@as(usize, 1), measured.advance_calls);
1145
+ try testing.expectEqual(@as(usize, 0), measured.width_calls);
1146
+ }
1147
+
1148
+ test "editable code highlights the caret row but not a text selection" {
1149
+ const source = "const first = 1;\nconst second = 2;\n";
1150
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1151
+ defer arena.deinit();
1152
+ var ui = Ui.init(arena.allocator());
1153
+ const view = try ui.finalize(ui.code(.{
1154
+ .language = .zig,
1155
+ .editable = true,
1156
+ .line_numbers = true,
1157
+ .wrap = false,
1158
+ .height = 80,
1159
+ }, source));
1160
+ var editor = findByText(view.root, source).?;
1161
+ editor.frame = geometry.RectF.init(0, 0, 220, 80);
1162
+ editor.state.focused = true;
1163
+ editor.text_selection = canvas.TextSelection.collapsed(std.mem.indexOf(u8, source, "second").?);
1164
+
1165
+ var tokens = canvas.DesignTokens{};
1166
+ tokens.colors.surface_subtle = canvas.Color.rgb8(19, 23, 29);
1167
+ var commands: [128]canvas.CanvasCommand = undefined;
1168
+ var builder = canvas.Builder.init(&commands);
1169
+ try canvas.emitWidgetTree(&builder, editor, tokens);
1170
+
1171
+ const caret = canvas.textGeometryForWidget(editor, tokens).caret_bounds.?;
1172
+ var full_row_index: ?usize = null;
1173
+ var source_index: ?usize = null;
1174
+ var gutter_index: ?usize = null;
1175
+ var gutter_row_index: ?usize = null;
1176
+ var marker_index: ?usize = null;
1177
+ var full_row: ?geometry.RectF = null;
1178
+ var gutter_row: ?geometry.RectF = null;
1179
+ for (builder.displayList().commands, 0..) |command, index| {
1180
+ switch (command) {
1181
+ .fill_rect => |fill| {
1182
+ if (std.meta.eql(fill.fill.color, tokens.colors.surface_subtle)) {
1183
+ if (fill.rect.width == editor.frame.width) {
1184
+ full_row_index = index;
1185
+ full_row = fill.rect;
1186
+ } else {
1187
+ gutter_row_index = index;
1188
+ gutter_row = fill.rect;
1189
+ }
1190
+ } else if (std.meta.eql(fill.fill.color, tokens.colors.background)) {
1191
+ gutter_index = index;
1192
+ }
1193
+ },
1194
+ .draw_text => |draw| {
1195
+ if (std.mem.eql(u8, draw.text, "2")) {
1196
+ marker_index = index;
1197
+ } else if (!std.mem.eql(u8, draw.text, "1") and !std.mem.eql(u8, draw.text, "3")) {
1198
+ source_index = index;
1199
+ }
1200
+ },
1201
+ else => {},
1202
+ }
1203
+ }
1204
+
1205
+ try testing.expect(full_row_index != null);
1206
+ try testing.expect(source_index != null);
1207
+ try testing.expect(gutter_index != null);
1208
+ try testing.expect(gutter_row_index != null);
1209
+ try testing.expect(marker_index != null);
1210
+ try testing.expect(full_row_index.? < source_index.?);
1211
+ try testing.expect(source_index.? < gutter_index.?);
1212
+ try testing.expect(gutter_index.? < gutter_row_index.?);
1213
+ try testing.expect(gutter_row_index.? < marker_index.?);
1214
+ try testing.expectEqual(editor.frame.x, full_row.?.x);
1215
+ try testing.expectEqual(editor.frame.width, full_row.?.width);
1216
+ try testing.expectEqual(caret.y, full_row.?.y);
1217
+ try testing.expectEqual(caret.height, full_row.?.height);
1218
+ try testing.expectEqual(full_row.?.y, gutter_row.?.y);
1219
+ try testing.expectEqual(full_row.?.height, gutter_row.?.height);
1220
+ try testing.expect(gutter_row.?.width < full_row.?.width);
1221
+
1222
+ editor.text_selection = .{ .anchor = 0, .focus = 5 };
1223
+ var selected_commands: [128]canvas.CanvasCommand = undefined;
1224
+ var selected_builder = canvas.Builder.init(&selected_commands);
1225
+ try canvas.emitWidgetTree(&selected_builder, editor, tokens);
1226
+ for (selected_builder.displayList().commands) |command| {
1227
+ if (command == .fill_rect) {
1228
+ try testing.expect(!std.meta.eql(command.fill_rect.fill.color, tokens.colors.surface_subtle));
1229
+ }
1230
+ }
1231
+
1232
+ editor.text_selection = canvas.TextSelection.collapsed(0);
1233
+ editor.state.focused = false;
1234
+ var blurred_commands: [128]canvas.CanvasCommand = undefined;
1235
+ var blurred_builder = canvas.Builder.init(&blurred_commands);
1236
+ try canvas.emitWidgetTree(&blurred_builder, editor, tokens);
1237
+ for (blurred_builder.displayList().commands) |command| {
1238
+ if (command == .fill_rect) {
1239
+ try testing.expect(!std.meta.eql(command.fill_rect.fill.color, tokens.colors.surface_subtle));
1240
+ }
1241
+ }
1242
+ }
1243
+
1244
+ test "editable code Tab infers the file indentation and falls back to two spaces" {
1245
+ try expectCodeTabInsertion("const answer = 42;\n", " ");
1246
+ try expectCodeTabInsertion(
1247
+ \\if (ready) {
1248
+ \\ while (running) {
1249
+ \\ tick();
1250
+ \\ }
1251
+ \\}
1252
+ , " ");
1253
+ try expectCodeTabInsertion(
1254
+ \\if (ready) {
1255
+ \\ while (running) {
1256
+ \\ tick();
1257
+ \\ }
1258
+ \\}
1259
+ , " ");
1260
+ try expectCodeTabInsertion(
1261
+ "if (ready) {\n\twhile (running) {\n\t\ttick();\n\t}\n}\n",
1262
+ "\t",
1263
+ );
1264
+
1265
+ const plain_textarea = canvas.Widget{
1266
+ .kind = .textarea,
1267
+ .text = "plain",
1268
+ };
1269
+ try testing.expect(canvas.widgetCodeTabTextEditEvent(plain_textarea, .{
1270
+ .phase = .key_down,
1271
+ .key = "tab",
1272
+ }) == null);
1273
+ try testing.expect(canvas.widgetCodeTabTextEditEvent(.{
1274
+ .kind = .textarea,
1275
+ .code_editor = true,
1276
+ .text = "code",
1277
+ }, .{
1278
+ .phase = .key_down,
1279
+ .key = "tab",
1280
+ .modifiers = .{ .shift = true },
1281
+ }) == null);
1282
+ try testing.expect(canvas.widgetCodeTabTextEditEvent(.{
1283
+ .kind = .textarea,
1284
+ .code_editor = true,
1285
+ .text = "code",
1286
+ }, .{
1287
+ .phase = .key_down,
1288
+ .key = "tab",
1289
+ .focus_moved = true,
1290
+ }) == null);
1291
+ }
1292
+
523
1293
  test "empty code retains the exact source text" {
524
1294
  var arena = std.heap.ArenaAllocator.init(testing.allocator);
525
1295
  defer arena.deinit();
@@ -654,6 +1424,32 @@ test "large code retains all source while emitting only visible text" {
654
1424
  }
655
1425
  }
656
1426
 
1427
+ test "large editable code selection repaints only visible glyphs" {
1428
+ const source = try testing.allocator.alloc(u8, 65_536);
1429
+ defer testing.allocator.free(source);
1430
+ @memset(source, 'x');
1431
+
1432
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1433
+ defer arena.deinit();
1434
+ var ui = Ui.init(arena.allocator());
1435
+ const view = try ui.finalize(ui.code(.{
1436
+ .editable = true,
1437
+ .wrap = false,
1438
+ .height = 80,
1439
+ }, source));
1440
+ var editor = findByText(view.root, source).?;
1441
+ editor.frame = geometry.RectF.init(0, 0, 320, 80);
1442
+ editor.text_selection = .{ .anchor = 0, .focus = 5 };
1443
+
1444
+ var commands: [256]canvas.CanvasCommand = undefined;
1445
+ var builder = canvas.Builder.init(&commands);
1446
+ try canvas.emitWidgetTree(&builder, editor, .{});
1447
+
1448
+ const text_bytes = displayListTextBytes(builder.displayList());
1449
+ try testing.expect(text_bytes <= canvas.max_display_list_text_bytes);
1450
+ try testing.expect(text_bytes < source.len);
1451
+ }
1452
+
657
1453
  test "direct tree code emission honors scroll viewports and later layout pages" {
658
1454
  const horizontal_source = "x" ** 65_536;
659
1455
  const horizontal_spans = [_]canvas.TextSpan{.{
@@ -721,6 +1517,60 @@ test "direct tree code emission honors scroll viewports and later layout pages"
721
1517
  try testing.expect(visible_text_commands > 0);
722
1518
  }
723
1519
 
1520
+ test "editable code highlights the visible tail of a ten-thousand-line source" {
1521
+ var source: std.ArrayListUnmanaged(u8) = .empty;
1522
+ defer source.deinit(testing.allocator);
1523
+ for (0..9_997) |_| {
1524
+ try source.appendSlice(testing.allocator, ":root { --color: #fff; }\n");
1525
+ }
1526
+ try source.appendSlice(testing.allocator, "body {\n color: red;\n}\n");
1527
+
1528
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
1529
+ defer arena.deinit();
1530
+ var ui = Ui.init(arena.allocator());
1531
+ const view = try ui.finalize(ui.code(.{
1532
+ .language = .css,
1533
+ .editable = true,
1534
+ .line_numbers = true,
1535
+ .wrap = false,
1536
+ .height = 100,
1537
+ }, source.items));
1538
+ var editor = findByText(view.root, source.items).?;
1539
+ try testing.expect(editor.code_editor);
1540
+ try testing.expectEqual(code_model.Language.css, editor.code_language);
1541
+ try testing.expectEqual(@as(usize, 1), editor.spans.len);
1542
+ try testing.expectEqual(@as(u8, 5), editor.code_line_number_digits);
1543
+
1544
+ const tokens = canvas.DesignTokens{};
1545
+ const line_height = tokens.typography.body_size * 1.25;
1546
+ editor.frame = geometry.RectF.init(0, 0, 500, 100);
1547
+ editor.value = 9_997 * line_height;
1548
+
1549
+ var commands: [256]canvas.CanvasCommand = undefined;
1550
+ var builder = canvas.Builder.init(&commands);
1551
+ try canvas.emitWidgetTree(&builder, editor, tokens);
1552
+
1553
+ var saw_body = false;
1554
+ var saw_property = false;
1555
+ var saw_late_line_number = false;
1556
+ for (builder.displayList().commands) |command| {
1557
+ if (command != .draw_text) continue;
1558
+ const draw = command.draw_text;
1559
+ if (std.mem.eql(u8, draw.text, "body")) {
1560
+ saw_body = true;
1561
+ try testing.expectEqual(tokens.colors.syntax_literal, draw.color);
1562
+ }
1563
+ if (std.mem.eql(u8, draw.text, "color")) {
1564
+ saw_property = true;
1565
+ try testing.expectEqual(tokens.colors.syntax_property, draw.color);
1566
+ }
1567
+ if (std.mem.eql(u8, draw.text, "9998")) saw_late_line_number = true;
1568
+ }
1569
+ try testing.expect(saw_body);
1570
+ try testing.expect(saw_property);
1571
+ try testing.expect(saw_late_line_number);
1572
+ }
1573
+
724
1574
  test "maximal one-line numbered code adds no retained gutter bytes" {
725
1575
  const source = try testing.allocator.alloc(u8, 65_536);
726
1576
  defer testing.allocator.free(source);