@native-sdk/cli 0.6.3 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/package.json +9 -9
  2. package/packages/core/package-lock.json +2 -2
  3. package/packages/core/package.json +1 -1
  4. package/skill-data/native-ui/SKILL.md +17 -2
  5. package/src/primitives/canvas/code.zig +590 -0
  6. package/src/primitives/canvas/code_tests.zig +1062 -0
  7. package/src/primitives/canvas/layout_audit.zig +3 -1
  8. package/src/primitives/canvas/markdown.zig +13 -11
  9. package/src/primitives/canvas/markdown_tests.zig +167 -4
  10. package/src/primitives/canvas/root.zig +4 -2
  11. package/src/primitives/canvas/tests.zig +1 -0
  12. package/src/primitives/canvas/text_measure_cache.zig +4 -4
  13. package/src/primitives/canvas/text_span_tests.zig +254 -0
  14. package/src/primitives/canvas/text_spans.zig +556 -32
  15. package/src/primitives/canvas/themes/geist.zig +29 -0
  16. package/src/primitives/canvas/tokens.zig +72 -2
  17. package/src/primitives/canvas/ui.zig +304 -5
  18. package/src/primitives/canvas/ui_markup.zig +59 -2
  19. package/src/primitives/canvas/ui_markup_compiled.zig +76 -0
  20. package/src/primitives/canvas/ui_markup_compiled_tests.zig +33 -0
  21. package/src/primitives/canvas/ui_markup_contract.zig +34 -0
  22. package/src/primitives/canvas/ui_markup_view.zig +73 -0
  23. package/src/primitives/canvas/ui_markup_view_tests.zig +85 -0
  24. package/src/primitives/canvas/ui_schema.zig +17 -6
  25. package/src/primitives/canvas/ui_schema_tests.zig +7 -6
  26. package/src/primitives/canvas/widget_invalidation.zig +3 -0
  27. package/src/primitives/canvas/widget_layout.zig +57 -8
  28. package/src/primitives/canvas/widget_layout_tests.zig +35 -0
  29. package/src/primitives/canvas/widget_metrics.zig +30 -1
  30. package/src/primitives/canvas/widget_render.zig +454 -9
  31. package/src/primitives/canvas/widget_text_select.zig +2 -2
  32. package/src/primitives/canvas/widgets.zig +13 -0
  33. package/src/root.zig +1 -0
  34. package/src/runtime/canvas_widget_clipboard_tests.zig +130 -0
  35. package/src/runtime/canvas_widget_context_menu.zig +12 -2
  36. package/src/runtime/canvas_widget_events.zig +7 -1
  37. package/src/runtime/canvas_widget_runtime.zig +7 -2
  38. package/src/runtime/canvas_widget_text_tests.zig +57 -0
  39. package/src/runtime/view.zig +7 -3
  40. package/src/runtime/view_widget_text.zig +194 -4
@@ -153,6 +153,14 @@ fn light() ColorTokens {
153
153
  .text = Color.rgb8(23, 23, 23),
154
154
  // Secondary ink: gray-900 #4d4d4d.
155
155
  .text_muted = Color.rgb8(77, 77, 77),
156
+ // Geist Code Block syntax inks (the published 900 steps).
157
+ .syntax_plain = Color.rgb8(23, 23, 23),
158
+ .syntax_comment = Color.rgb8(77, 77, 77),
159
+ .syntax_keyword = Color.rgb8(189, 40, 100),
160
+ .syntax_literal = Color.rgb8(41, 122, 58),
161
+ .syntax_function = Color.rgb8(120, 32, 188),
162
+ .syntax_property = Color.rgb8(203, 42, 47),
163
+ .syntax_constant = Color.rgb8(0, 104, 214),
156
164
  // Hairline: black at 8% — the translucent border register.
157
165
  .border = Color.rgba8(0, 0, 0, 20),
158
166
  // The monochrome primary FILL: pure black #000000 filled
@@ -205,6 +213,13 @@ fn dark() ColorTokens {
205
213
  .text = Color.rgb8(237, 237, 237),
206
214
  // Secondary ink: gray-900 #a0a0a0.
207
215
  .text_muted = Color.rgb8(160, 160, 160),
216
+ .syntax_plain = Color.rgb8(237, 237, 237),
217
+ .syntax_comment = Color.rgb8(161, 161, 161),
218
+ .syntax_keyword = Color.rgb8(247, 95, 143),
219
+ .syntax_literal = Color.rgb8(98, 192, 115),
220
+ .syntax_function = Color.rgb8(191, 122, 240),
221
+ .syntax_property = Color.rgb8(255, 97, 102),
222
+ .syntax_constant = Color.rgb8(82, 168, 255),
208
223
  // Hairline: white at 14% — hairlines brighten what they overlap.
209
224
  .border = Color.rgba8(255, 255, 255, 36),
210
225
  // Porcelain primary with black knockout text.
@@ -247,6 +262,13 @@ fn highContrastLight() ColorTokens {
247
262
  .text = Color.rgb8(0, 0, 0),
248
263
  // Secondary ink darkens to gray-1000.
249
264
  .text_muted = Color.rgb8(23, 23, 23),
265
+ .syntax_plain = Color.rgb8(23, 23, 23),
266
+ .syntax_comment = Color.rgb8(77, 77, 77),
267
+ .syntax_keyword = Color.rgb8(189, 40, 100),
268
+ .syntax_literal = Color.rgb8(41, 122, 58),
269
+ .syntax_function = Color.rgb8(120, 32, 188),
270
+ .syntax_property = Color.rgb8(203, 42, 47),
271
+ .syntax_constant = Color.rgb8(0, 104, 214),
250
272
  .border = Color.rgba8(0, 0, 0, 180),
251
273
  .accent = Color.rgb8(0, 0, 0),
252
274
  .accent_text = Color.rgb8(255, 255, 255),
@@ -281,6 +303,13 @@ fn highContrastDark() ColorTokens {
281
303
  .surface_pressed = Color.rgb8(41, 41, 41),
282
304
  .text = Color.rgb8(255, 255, 255),
283
305
  .text_muted = Color.rgb8(237, 237, 237),
306
+ .syntax_plain = Color.rgb8(237, 237, 237),
307
+ .syntax_comment = Color.rgb8(161, 161, 161),
308
+ .syntax_keyword = Color.rgb8(247, 95, 143),
309
+ .syntax_literal = Color.rgb8(98, 192, 115),
310
+ .syntax_function = Color.rgb8(191, 122, 240),
311
+ .syntax_property = Color.rgb8(255, 97, 102),
312
+ .syntax_constant = Color.rgb8(82, 168, 255),
284
313
  .border = Color.rgba8(255, 255, 255, 190),
285
314
  .accent = Color.rgb8(255, 255, 255),
286
315
  .accent_text = Color.rgb8(0, 0, 0),
@@ -132,6 +132,20 @@ pub const ColorTokens = struct {
132
132
  text: Color = Color.rgb8(10, 10, 10),
133
133
  /// Muted foreground; oklch(0.556 0 0) = #737373.
134
134
  text_muted: Color = Color.rgb8(115, 115, 115),
135
+ /// Source-code ink follows the Geist Code Block palette in every
136
+ /// built-in pack. Dedicated roles keep syntax highlighting independent
137
+ /// from the app's semantic success/warning/info colors. Transparent is
138
+ /// the compatibility sentinel for an older custom palette that omitted
139
+ /// these newer fields: syntax resolution inherits `text` (or
140
+ /// `text_muted` for comments), so a dark palette never silently picks up
141
+ /// the light register's near-black ink.
142
+ syntax_plain: Color = Color.rgba8(0, 0, 0, 0),
143
+ syntax_comment: Color = Color.rgba8(0, 0, 0, 0),
144
+ syntax_keyword: Color = Color.rgba8(0, 0, 0, 0),
145
+ syntax_literal: Color = Color.rgba8(0, 0, 0, 0),
146
+ syntax_function: Color = Color.rgba8(0, 0, 0, 0),
147
+ syntax_property: Color = Color.rgba8(0, 0, 0, 0),
148
+ syntax_constant: Color = Color.rgba8(0, 0, 0, 0),
135
149
  /// Border/input hairline; oklch(0.922 0 0) = #e5e5e5.
136
150
  border: Color = Color.rgb8(229, 229, 229),
137
151
  /// Primary; oklch(0.205 0 0) = #171717 — the monochrome near-black
@@ -183,7 +197,15 @@ pub const ColorTokens = struct {
183
197
  }
184
198
 
185
199
  pub fn light() ColorTokens {
186
- return .{};
200
+ return .{
201
+ .syntax_plain = Color.rgb8(23, 23, 23),
202
+ .syntax_comment = Color.rgb8(77, 77, 77),
203
+ .syntax_keyword = Color.rgb8(189, 40, 100),
204
+ .syntax_literal = Color.rgb8(41, 122, 58),
205
+ .syntax_function = Color.rgb8(120, 32, 188),
206
+ .syntax_property = Color.rgb8(203, 42, 47),
207
+ .syntax_constant = Color.rgb8(0, 104, 214),
208
+ };
187
209
  }
188
210
 
189
211
  pub fn dark() ColorTokens {
@@ -203,6 +225,13 @@ pub const ColorTokens = struct {
203
225
  .text = Color.rgb8(250, 250, 250),
204
226
  // Muted foreground; oklch(0.708 0 0) = #a1a1a1.
205
227
  .text_muted = Color.rgb8(161, 161, 161),
228
+ .syntax_plain = Color.rgb8(237, 237, 237),
229
+ .syntax_comment = Color.rgb8(161, 161, 161),
230
+ .syntax_keyword = Color.rgb8(247, 95, 143),
231
+ .syntax_literal = Color.rgb8(98, 192, 115),
232
+ .syntax_function = Color.rgb8(191, 122, 240),
233
+ .syntax_property = Color.rgb8(255, 97, 102),
234
+ .syntax_constant = Color.rgb8(82, 168, 255),
206
235
  // Dark borders are translucent white (10%), not a gray fill:
207
236
  // hairlines brighten what they overlap instead of muddying it.
208
237
  .border = Color.rgba8(255, 255, 255, 26),
@@ -241,6 +270,13 @@ pub const ColorTokens = struct {
241
270
  .surface_pressed = Color.rgb8(229, 229, 229),
242
271
  .text = Color.rgb8(0, 0, 0),
243
272
  .text_muted = Color.rgb8(64, 64, 64),
273
+ .syntax_plain = Color.rgb8(23, 23, 23),
274
+ .syntax_comment = Color.rgb8(77, 77, 77),
275
+ .syntax_keyword = Color.rgb8(189, 40, 100),
276
+ .syntax_literal = Color.rgb8(41, 122, 58),
277
+ .syntax_function = Color.rgb8(120, 32, 188),
278
+ .syntax_property = Color.rgb8(203, 42, 47),
279
+ .syntax_constant = Color.rgb8(0, 104, 214),
244
280
  .border = Color.rgba8(0, 0, 0, 180),
245
281
  // The monochrome primary at its contrast extreme: pure
246
282
  // black filled controls, 21:1 against the white accent
@@ -276,6 +312,13 @@ pub const ColorTokens = struct {
276
312
  .surface_pressed = Color.rgb8(38, 38, 38),
277
313
  .text = Color.rgb8(255, 255, 255),
278
314
  .text_muted = Color.rgb8(229, 229, 229),
315
+ .syntax_plain = Color.rgb8(237, 237, 237),
316
+ .syntax_comment = Color.rgb8(161, 161, 161),
317
+ .syntax_keyword = Color.rgb8(247, 95, 143),
318
+ .syntax_literal = Color.rgb8(98, 192, 115),
319
+ .syntax_function = Color.rgb8(191, 122, 240),
320
+ .syntax_property = Color.rgb8(255, 97, 102),
321
+ .syntax_constant = Color.rgb8(82, 168, 255),
279
322
  .border = Color.rgba8(255, 255, 255, 190),
280
323
  // The monochrome primary at its contrast extreme: pure
281
324
  // white filled controls, 21:1 against the black accent
@@ -303,6 +346,26 @@ pub const ColorTokens = struct {
303
346
  }
304
347
  };
305
348
 
349
+ /// Resolve one named color token, including the compatibility inheritance
350
+ /// for syntax roles omitted by older custom palettes.
351
+ pub fn colorTokenValue(colors: ColorTokens, ref: std.meta.FieldEnum(ColorTokens)) Color {
352
+ const value = switch (ref) {
353
+ inline else => |tag| @field(colors, @tagName(tag)),
354
+ };
355
+ if (value.a != 0) return value;
356
+ return switch (ref) {
357
+ .syntax_comment => colors.text_muted,
358
+ .syntax_plain,
359
+ .syntax_keyword,
360
+ .syntax_literal,
361
+ .syntax_function,
362
+ .syntax_property,
363
+ .syntax_constant,
364
+ => colors.text,
365
+ else => value,
366
+ };
367
+ }
368
+
306
369
  pub const FontFamily = enum {
307
370
  geist,
308
371
  geist_mono,
@@ -1205,6 +1268,13 @@ pub const ColorTokenOverrides = struct {
1205
1268
  surface_pressed: ?Color = null,
1206
1269
  text: ?Color = null,
1207
1270
  text_muted: ?Color = null,
1271
+ syntax_plain: ?Color = null,
1272
+ syntax_comment: ?Color = null,
1273
+ syntax_keyword: ?Color = null,
1274
+ syntax_literal: ?Color = null,
1275
+ syntax_function: ?Color = null,
1276
+ syntax_property: ?Color = null,
1277
+ syntax_constant: ?Color = null,
1208
1278
  border: ?Color = null,
1209
1279
  accent: ?Color = null,
1210
1280
  accent_text: ?Color = null,
@@ -1767,7 +1837,7 @@ pub const DesignTokenOverrides = struct {
1767
1837
  };
1768
1838
 
1769
1839
  pub const DesignTokens = struct {
1770
- colors: ColorTokens = .{},
1840
+ colors: ColorTokens = ColorTokens.light(),
1771
1841
  typography: TypographyTokens = .{},
1772
1842
  spacing: SpacingTokens = .{},
1773
1843
  radius: RadiusTokens = .{},
@@ -20,6 +20,7 @@
20
20
 
21
21
  const std = @import("std");
22
22
  const builtin = @import("builtin");
23
+ const code_model = @import("code.zig");
23
24
  const font_coverage = @import("font_coverage.zig");
24
25
  const geometry = @import("geometry");
25
26
  const canvas = @import("root.zig");
@@ -383,9 +384,7 @@ pub const StyleTokenRefs = struct {
383
384
  };
384
385
 
385
386
  fn colorTokenValue(colors: canvas.ColorTokens, ref: ColorTokenName) canvas.Color {
386
- return switch (ref) {
387
- inline else => |tag| @field(colors, @tagName(tag)),
388
- };
387
+ return canvas.colorTokenValue(colors, ref);
389
388
  }
390
389
 
391
390
  fn radiusTokenValue(radius: canvas.RadiusTokens, ref: RadiusTokenName) f32 {
@@ -937,6 +936,11 @@ pub fn Ui(comptime Msg: type) type {
937
936
  on_terminal: ?TerminalMsgFn = null,
938
937
  context_menu: []const ContextMenuItem = &.{},
939
938
  nodes: []const Node = &.{},
939
+ /// Internal source fingerprint for adjacent code paragraphs
940
+ /// that form one selectable document. `finalizeNode` combines
941
+ /// it with their parent structural id into a source-sensitive
942
+ /// group identity.
943
+ static_text_group_fingerprint: u64 = 0,
940
944
  /// Markup authoring provenance, stamped by the markup engines
941
945
  /// when the builder carries a `provenance_sink`; null for
942
946
  /// builder-authored (Zig) nodes, which is itself the honest
@@ -2289,6 +2293,292 @@ pub fn Ui(comptime Msg: type) type {
2289
2293
  return self.el(.stack, .{ .grow = grow }, .{});
2290
2294
  }
2291
2295
 
2296
+ pub const CodeOptions = struct {
2297
+ key: ?UiKey = null,
2298
+ global_key: ?UiKey = null,
2299
+ language: code_model.Language = .plain,
2300
+ /// Prefix each logical source line with a muted, monospace
2301
+ /// number. Off by default.
2302
+ line_numbers: bool = false,
2303
+ /// Word-wrap long source lines. `false` keeps logical lines
2304
+ /// intact inside one horizontal scroll region.
2305
+ wrap: bool = true,
2306
+ width: f32 = 0,
2307
+ /// Definite surface height. Overflow scrolls vertically;
2308
+ /// no-wrap surfaces scroll on both axes.
2309
+ height: f32 = 0,
2310
+ min_width: f32 = 0,
2311
+ grow: f32 = 0,
2312
+ semantics: canvas.WidgetSemantics = .{},
2313
+ };
2314
+
2315
+ /// A themed source-code surface with bounded syntax highlighting.
2316
+ /// Markdown fences lower through this same component.
2317
+ pub fn code(self: *Self, options: CodeOptions, source: []const u8) Node {
2318
+ const line_count = codeLineCount(source);
2319
+ const numbered = options.line_numbers and line_count <= max_code_lines;
2320
+ const content = if (numbered)
2321
+ self.numberedCodeParagraph(source, options.language, options.wrap, line_count)
2322
+ else
2323
+ self.codeParagraphChunks(source, options.language, options.wrap);
2324
+ const body = if (options.wrap)
2325
+ if (options.height > 0) blk: {
2326
+ // Generic scroll children stay viewport-sized by
2327
+ // contract. Put code in an internal flow track whose
2328
+ // non-growing paragraph retains its full wrapped
2329
+ // height, giving the vertical scroll walker an honest
2330
+ // descendant extent without remeasuring arbitrary
2331
+ // public scroll content.
2332
+ var tracked_content = content;
2333
+ tracked_content.widget.layout.grow = 0;
2334
+ const track = self.column(.{}, .{tracked_content});
2335
+ break :blk self.scroll(.{ .axis = .vertical, .grow = 1 }, .{track});
2336
+ } else content
2337
+ else blk: {
2338
+ // The engine scrollbar overlays the viewport's bottom
2339
+ // edge. Reserve one quiet band inside the scrollable
2340
+ // track so the final code baseline never sits under it.
2341
+ const track = self.column(.{}, .{
2342
+ content,
2343
+ self.el(.stack, .{ .height = 8 }, .{}),
2344
+ });
2345
+ break :blk self.scroll(.{
2346
+ .axis = if (options.height > 0) .both else .horizontal,
2347
+ .grow = 1,
2348
+ }, .{track});
2349
+ };
2350
+ var surface = self.el(.panel, .{
2351
+ .key = options.key,
2352
+ .global_key = options.global_key,
2353
+ .width = options.width,
2354
+ .height = options.height,
2355
+ .min_width = options.min_width,
2356
+ .grow = options.grow,
2357
+ .padding = 12,
2358
+ .style_tokens = .{ .background = .surface_subtle },
2359
+ .semantics = options.semantics,
2360
+ }, .{body});
2361
+ surface.widget.layout.clip_content = true;
2362
+ return surface;
2363
+ }
2364
+
2365
+ /// Keep numbered source in one selectable paragraph. The renderer
2366
+ /// owns its muted gutter, so marker digits never enter retained
2367
+ /// text or clipboard bytes; it derives each marker baseline from
2368
+ /// this paragraph's real wrapped layout.
2369
+ fn numberedCodeParagraph(
2370
+ self: *Self,
2371
+ source: []const u8,
2372
+ language: code_model.Language,
2373
+ wrap: bool,
2374
+ line_count: usize,
2375
+ ) Node {
2376
+ var state: code_model.HighlightState = .{};
2377
+ var source_node = self.codeParagraphWithState(source, language, wrap, 1, &state, null);
2378
+ source_node.widget.code_line_number_digits = @intCast(decimalDigits(line_count));
2379
+ return source_node;
2380
+ }
2381
+
2382
+ /// Keep ordinary code in one text widget so static selection and copy
2383
+ /// span logical lines. Only split when the paragraph layout's bounded
2384
+ /// line capacity requires it; every chunk remains independently
2385
+ /// visible instead of silently truncating a large source block.
2386
+ fn codeParagraphChunks(self: *Self, source: []const u8, language: code_model.Language, wrap: bool) Node {
2387
+ var state: code_model.HighlightState = .{};
2388
+ var budget: CodeSpanBudget = .{
2389
+ .remaining_chunks = codeParagraphChunkCount(source, wrap),
2390
+ };
2391
+ return self.codeParagraphChunksWithStateBudgeted(
2392
+ source,
2393
+ language,
2394
+ wrap,
2395
+ if (wrap) 1 else 0,
2396
+ &state,
2397
+ &budget,
2398
+ );
2399
+ }
2400
+
2401
+ fn codeParagraphChunksWithState(
2402
+ self: *Self,
2403
+ source: []const u8,
2404
+ language: code_model.Language,
2405
+ wrap: bool,
2406
+ grow: f32,
2407
+ state: *code_model.HighlightState,
2408
+ ) Node {
2409
+ return self.codeParagraphChunksWithStateBudgeted(
2410
+ source,
2411
+ language,
2412
+ wrap,
2413
+ grow,
2414
+ state,
2415
+ null,
2416
+ );
2417
+ }
2418
+
2419
+ fn codeParagraphChunksWithStateBudgeted(
2420
+ self: *Self,
2421
+ source: []const u8,
2422
+ language: code_model.Language,
2423
+ wrap: bool,
2424
+ grow: f32,
2425
+ state: *code_model.HighlightState,
2426
+ budget: ?*CodeSpanBudget,
2427
+ ) Node {
2428
+ const chunk_count = codeParagraphChunkCount(source, wrap);
2429
+ if (chunk_count == 1) {
2430
+ return self.codeParagraphWithState(source, language, wrap, grow, state, budget);
2431
+ }
2432
+ const chunks = self.arena.alloc(Node, chunk_count) catch {
2433
+ self.failed = true;
2434
+ return self.column(.{}, .{});
2435
+ };
2436
+ const group_fingerprint = std.hash.Wyhash.hash(0, source) | 1;
2437
+ var chunk_index: usize = 0;
2438
+ var chunk_start: usize = 0;
2439
+ while (chunk_start < source.len) {
2440
+ const chunk_end = codeParagraphChunkEnd(source, chunk_start, wrap);
2441
+ chunks[chunk_index] = self.codeParagraphWithState(
2442
+ source[chunk_start..chunk_end],
2443
+ language,
2444
+ wrap,
2445
+ 0,
2446
+ state,
2447
+ budget,
2448
+ );
2449
+ chunks[chunk_index].static_text_group_fingerprint = group_fingerprint;
2450
+ chunks[chunk_index].widget.static_text_group_offset = chunk_start;
2451
+ chunk_index += 1;
2452
+ chunk_start = chunk_end;
2453
+ }
2454
+ return self.column(.{ .grow = grow }, .{chunks[0..chunk_index]});
2455
+ }
2456
+
2457
+ fn codeParagraphWithState(
2458
+ self: *Self,
2459
+ source: []const u8,
2460
+ language: code_model.Language,
2461
+ wrap: bool,
2462
+ grow: f32,
2463
+ state: *code_model.HighlightState,
2464
+ budget: ?*CodeSpanBudget,
2465
+ ) Node {
2466
+ var storage: [canvas.text_spans.max_text_spans_per_paragraph]canvas.TextSpan = undefined;
2467
+ var highlighted = if (source.len == 0) blk: {
2468
+ storage[0] = .{ .text = source, .monospace = true, .color = .syntax_plain };
2469
+ break :blk storage[0..1];
2470
+ } else code_model.highlightWithState(source, language, &storage, state);
2471
+ if (budget) |span_budget| {
2472
+ std.debug.assert(span_budget.remaining_chunks > 0);
2473
+ span_budget.remaining_chunks -= 1;
2474
+ const highlighted_total = span_budget.used +
2475
+ highlighted.len +
2476
+ span_budget.remaining_chunks;
2477
+ if (highlighted_total <= max_code_spans_per_surface) {
2478
+ span_budget.used += highlighted.len;
2479
+ } else {
2480
+ storage[0] = .{
2481
+ .text = source,
2482
+ .monospace = true,
2483
+ .color = .syntax_plain,
2484
+ };
2485
+ highlighted = storage[0..1];
2486
+ span_budget.used += 1;
2487
+ }
2488
+ }
2489
+ return self.paragraph(.{ .wrap = wrap, .grow = grow }, highlighted);
2490
+ }
2491
+
2492
+ /// Upper bound for formatting renderer-owned logical-line markers.
2493
+ /// Sources above it keep every source byte and omit the gutter.
2494
+ pub const max_code_lines: usize = 128;
2495
+ pub const max_code_spans_per_surface: usize = 512;
2496
+
2497
+ const CodeSpanBudget = struct {
2498
+ used: usize = 0,
2499
+ remaining_chunks: usize,
2500
+ };
2501
+
2502
+ fn codeParagraphChunkCount(source: []const u8, wrap: bool) usize {
2503
+ if (source.len == 0) return 1;
2504
+ var count: usize = 0;
2505
+ var start: usize = 0;
2506
+ while (start < source.len) {
2507
+ start = codeParagraphChunkEnd(source, start, wrap);
2508
+ count += 1;
2509
+ }
2510
+ return count;
2511
+ }
2512
+
2513
+ /// Pack complete logical lines while their UTF-8 scalar count fits
2514
+ /// the span layout's worst case (one glyph per visual line). A
2515
+ /// single over-capacity logical line stays whole: the viewport
2516
+ /// painter pages its visual runs without inserting a source break.
2517
+ fn codeParagraphChunkEnd(source: []const u8, start: usize, wrap: bool) usize {
2518
+ if (!wrap) {
2519
+ var cursor = start;
2520
+ var lines: usize = 0;
2521
+ while (cursor < source.len) : (cursor += 1) {
2522
+ if (source[cursor] != '\n') continue;
2523
+ lines += 1;
2524
+ if (lines >= max_code_logical_lines_per_paragraph and cursor + 1 < source.len) {
2525
+ return cursor + 1;
2526
+ }
2527
+ }
2528
+ return source.len;
2529
+ }
2530
+
2531
+ var cursor = start;
2532
+ var units: usize = 0;
2533
+ var lines: usize = 0;
2534
+ while (cursor < source.len) {
2535
+ const newline = std.mem.indexOfScalarPos(u8, source, cursor, '\n');
2536
+ const line_end = newline orelse source.len;
2537
+ const after_line = if (newline != null) line_end + 1 else line_end;
2538
+ // A trailing newline ends the final painted line without
2539
+ // adding another one. An empty source line still occupies
2540
+ // one visual line.
2541
+ const line_units = @max(1, codeScalarCount(source[cursor..line_end]));
2542
+ if (units + line_units > canvas.text_spans.max_text_span_lines_per_paragraph and cursor > start) {
2543
+ return cursor;
2544
+ }
2545
+ units += line_units;
2546
+ lines += 1;
2547
+ cursor = after_line;
2548
+ if (lines >= max_code_logical_lines_per_paragraph and cursor < source.len) return cursor;
2549
+ }
2550
+ return source.len;
2551
+ }
2552
+
2553
+ fn codeScalarCount(source: []const u8) usize {
2554
+ var count: usize = 0;
2555
+ var cursor: usize = 0;
2556
+ while (cursor < source.len) : (count += 1) {
2557
+ const sequence_len = std.unicode.utf8ByteSequenceLength(source[cursor]) catch 1;
2558
+ cursor += @min(sequence_len, source.len - cursor);
2559
+ }
2560
+ return count;
2561
+ }
2562
+
2563
+ // Keep the maximal 64 KiB newline-only source within both the
2564
+ // component's 512-span share and the runtime's 1024-node view cap:
2565
+ // 128 logical lines per paragraph yields at most 512 chunks.
2566
+ const max_code_logical_lines_per_paragraph: usize =
2567
+ canvas.text_spans.max_text_span_lines_per_paragraph;
2568
+
2569
+ fn codeLineCount(source: []const u8) usize {
2570
+ if (source.len == 0) return 1;
2571
+ return std.mem.count(u8, source, "\n") +
2572
+ @intFromBool(source[source.len - 1] != '\n');
2573
+ }
2574
+
2575
+ fn decimalDigits(value: usize) usize {
2576
+ var remaining = value;
2577
+ var digits: usize = 1;
2578
+ while (remaining >= 10) : (remaining /= 10) digits += 1;
2579
+ return digits;
2580
+ }
2581
+
2292
2582
  pub const ChartOptions = struct {
2293
2583
  key: ?UiKey = null,
2294
2584
  global_key: ?UiKey = null,
@@ -2889,14 +3179,23 @@ pub fn Ui(comptime Msg: type) type {
2889
3179
  // way, matching the single-line measurement both layout paths
2890
3180
  // already perform; overflow policy (`overflow`, trailing
2891
3181
  // ellipsis by default) decides what happens past the frame.
2892
- // Span paragraphs keep wrapping.
2893
- if (node.wrap == false and widget.kind == .text and widget.spans.len == 0) {
3182
+ // Span paragraphs honor the same explicit no-wrap policy.
3183
+ // `Ui.code` uses this to keep highlighted logical lines
3184
+ // intact inside its horizontal scroll region.
3185
+ if (node.wrap == false and widget.kind == .text) {
2894
3186
  widget.text_no_wrap = true;
2895
3187
  }
2896
3188
  widget.id = if (node.global_key) |global_key|
2897
3189
  structuralId(global_id_seed, widget.kind, global_key)
2898
3190
  else
2899
3191
  structuralId(parent_id, widget.kind, key);
3192
+ if (node.static_text_group_fingerprint != 0) {
3193
+ widget.static_text_group_id = structuralId(
3194
+ parent_id,
3195
+ .stack,
3196
+ .{ .int = node.static_text_group_fingerprint },
3197
+ );
3198
+ }
2900
3199
  // Provenance record (write-back's read half): this is the one
2901
3200
  // point where the markup-stamped source and the just-assigned
2902
3201
  // structural id are both in hand. Explicit keys (loop item
@@ -1847,7 +1847,7 @@ pub const known_color_token_names = schema.color_token_names;
1847
1847
  pub const known_radius_token_names = schema.radius_token_names;
1848
1848
 
1849
1849
  pub const style_token_literal_message = "style token attributes take a literal token name - dynamic styling stays in Zig";
1850
- pub const unknown_color_token_message = "unknown color token: color style attributes take a canvas ColorTokens field name (background, surface, surface_subtle, surface_pressed, text, text_muted, border, accent, accent_text, destructive, destructive_text, success, success_text, warning, warning_text, info, info_text, focus_ring, shadow, scrim, disabled)";
1850
+ pub const unknown_color_token_message = "unknown color token: color style attributes take a canvas ColorTokens field name (background, surface, surface_subtle, surface_pressed, text, text_muted, syntax_plain, syntax_comment, syntax_keyword, syntax_literal, syntax_function, syntax_property, syntax_constant, border, accent, accent_text, destructive, destructive_text, success, success_text, warning, warning_text, info, info_text, focus_ring, shadow, scrim, disabled)";
1851
1851
  pub const unknown_radius_token_message = "unknown radius token: radius takes a canvas RadiusTokens field name (sm, md, lg, xl)";
1852
1852
 
1853
1853
  pub const for_children_message = "for takes one or more element children (elements, use, if/else, or a nested for) - text content is only allowed inside text-bearing elements";
@@ -1865,6 +1865,10 @@ pub const markdown_issue_link_base_message = "issue-link-base takes a literal UR
1865
1865
  pub const markdown_on_link_message = "on-link takes a bare Msg tag whose payload is the pressed link URL (a []const u8 variant, like open_url: []const u8)";
1866
1866
  pub const markdown_on_details_message = "on-details takes a bare Msg tag whose payload is the details block index (a usize variant, like toggle_details: usize)";
1867
1867
  pub const markdown_details_expanded_message = "details-expanded takes one {binding} naming a []const bool iterable (a model field, pub decl, or fn - the same sources for each accepts)";
1868
+ pub const code_source_message = "code requires a source attribute with one {binding} naming the source text (a []const u8 field or fn - arena fns work)";
1869
+ pub const code_children_message = "code takes no children or text content - the source binding provides the code";
1870
+ pub const code_attr_message = "unknown attribute for code - it takes source, language, line-numbers, wrap, width, height, min-width, grow, key, global-key, and label";
1871
+ pub const code_language_message = "language takes a literal lexer name: plain, zig, javascript/js, typescript/ts, jsx/tsx, json, shell/sh/bash/zsh, python/py, rust/rs, c/cpp/c++/csharp/java/kotlin/swift, go, html/xml/svg, css/scss/less, or sql";
1868
1872
  pub const stepper_active_message = "stepper requires an active attribute (a number or one {binding}) naming the active step index";
1869
1873
  pub const stepper_attr_message = "unknown attribute for stepper - it takes active, key, global-key, and label";
1870
1874
  pub const stepper_children_message = "stepper takes only step children (each step is a text leaf: <step>Work</step>)";
@@ -2215,6 +2219,58 @@ fn validateMarkdown(node: MarkupNode) ?MarkupErrorInfo {
2215
2219
  return null;
2216
2220
  }
2217
2221
 
2222
+ fn codeLanguageName(name: []const u8) bool {
2223
+ const names = "plain text zig js javascript jsx ts typescript tsx json jsonc sh bash zsh shell py python rs rust c h cc cpp c++ cs csharp java kotlin swift go golang html xml svg css scss less sql";
2224
+ var known = std.mem.tokenizeScalar(u8, names, ' ');
2225
+ while (known.next()) |candidate| {
2226
+ if (std.ascii.eqlIgnoreCase(name, candidate)) return true;
2227
+ }
2228
+ return false;
2229
+ }
2230
+
2231
+ /// `<code>` is a source-bound leaf lowered through `Ui.code`: syntax
2232
+ /// language is static markup, while flags and layout values can bind.
2233
+ fn validateCode(node: MarkupNode) ?MarkupErrorInfo {
2234
+ for (node.children) |child| return errorAt(child, code_children_message);
2235
+ var has_source = false;
2236
+ for (node.attrs) |attribute| {
2237
+ if (std.mem.eql(u8, attribute.name, "source")) {
2238
+ has_source = true;
2239
+ const expression = parseAttrExpression(attribute.value);
2240
+ if (expression == null or expression.? != .binding) return attrError(node, attribute, code_source_message);
2241
+ continue;
2242
+ }
2243
+ if (std.mem.eql(u8, attribute.name, "language")) {
2244
+ const expression = parseAttrExpression(attribute.value);
2245
+ if (expression == null or expression.? != .literal or !codeLanguageName(expression.?.literal)) {
2246
+ return attrError(node, attribute, code_language_message);
2247
+ }
2248
+ continue;
2249
+ }
2250
+ if (std.mem.eql(u8, attribute.name, "line-numbers") or std.mem.eql(u8, attribute.name, "wrap")) {
2251
+ if (attribute.value.len == 0) continue;
2252
+ if (attrExpressionError(attribute.value, invalid_expression_message)) |message| {
2253
+ return attrError(node, attribute, message);
2254
+ }
2255
+ continue;
2256
+ }
2257
+ const known = std.mem.eql(u8, attribute.name, "width") or
2258
+ std.mem.eql(u8, attribute.name, "height") or
2259
+ std.mem.eql(u8, attribute.name, "min-width") or
2260
+ std.mem.eql(u8, attribute.name, "grow") or
2261
+ std.mem.eql(u8, attribute.name, "label") or
2262
+ std.mem.eql(u8, attribute.name, "key") or
2263
+ std.mem.eql(u8, attribute.name, "global-key");
2264
+ if (!known) return attrError(node, attribute, code_attr_message);
2265
+ if (attrExpressionError(attribute.value, invalid_expression_message)) |message| {
2266
+ return attrError(node, attribute, message);
2267
+ }
2268
+ if (attrCoverageError(node, attribute)) |info| return info;
2269
+ }
2270
+ if (!has_source) return errorAt(node, code_source_message);
2271
+ return null;
2272
+ }
2273
+
2218
2274
  /// `<stepper active="{index}">` takes only `<step>` text-leaf children:
2219
2275
  /// each step's state (completed/active/pending) derives from its position
2220
2276
  /// against the active index, so steps carry no attributes of their own.
@@ -2806,7 +2862,7 @@ fn validateVideo(node: MarkupNode) ?MarkupErrorInfo {
2806
2862
  /// The rule hooks the composite registry entries name. A registry entry
2807
2863
  /// whose hook this table does not implement is a compile error (below),
2808
2864
  /// so attachment and implementation can never drift.
2809
- const rule_hook_names = [_][]const u8{ "markdown", "stepper", "step", "timeline", "timeline-item", "chart", "series", "context-menu", "input-group", "input-group-actions", "span", "reactions", "video" };
2865
+ const rule_hook_names = [_][]const u8{ "markdown", "code", "stepper", "step", "timeline", "timeline-item", "chart", "series", "context-menu", "input-group", "input-group-actions", "span", "reactions", "video" };
2810
2866
 
2811
2867
  comptime {
2812
2868
  for (schema.elements) |entry| {
@@ -2826,6 +2882,7 @@ comptime {
2826
2882
  /// a worse inner language than plain Zig.
2827
2883
  fn validateRuleHook(hook: []const u8, document: MarkupDocument, node: MarkupNode, parent_element: ?[]const u8, template_limit: usize, slot_rule: SlotRule) ?MarkupErrorInfo {
2828
2884
  if (std.mem.eql(u8, hook, "markdown")) return validateMarkdown(node);
2885
+ if (std.mem.eql(u8, hook, "code")) return validateCode(node);
2829
2886
  if (std.mem.eql(u8, hook, "stepper")) return validateStepper(node);
2830
2887
  if (std.mem.eql(u8, hook, "step")) {
2831
2888
  // Steps inside a stepper are consumed by validateStepper; one