@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 canvas = @import("root.zig");
4
4
  const drawing_model = @import("drawing.zig");
5
5
  const text_model = @import("text.zig");
6
6
  const text_spans_model = @import("text_spans.zig");
7
+ const code_model = @import("code.zig");
7
8
  const token_model = @import("tokens.zig");
8
9
  const widget_model = @import("widgets.zig");
9
10
  const event_model = @import("events.zig");
@@ -320,7 +321,7 @@ fn emitWidgetDepthContent(builder: *Builder, widget: Widget, tokens: DesignToken
320
321
  visible_bounds.normalized(),
321
322
  );
322
323
  if (!clipped.isEmpty()) {
323
- try emitVisibleCodeTextSpansWidget(builder, paint_widget, tokens, clipped);
324
+ try emitVisibleCodeTextSpansWidget(builder, paint_widget, tokens, clipped, .{});
324
325
  }
325
326
  }
326
327
  } else {
@@ -336,7 +337,11 @@ fn emitWidgetDepthContent(builder: *Builder, widget: Widget, tokens: DesignToken
336
337
  .button, .toggle_button, .toggle => try widget_render_controls.emitButtonWidget(builder, paint_widget, tokens),
337
338
  .icon_button => try widget_render_controls.emitIconButtonWidget(builder, paint_widget, tokens),
338
339
  .select => try widget_render_controls.emitSelectWidget(builder, paint_widget, tokens),
339
- .input, .text_field, .textarea => try widget_render_controls.emitTextFieldWidget(builder, paint_widget, tokens),
340
+ .input, .text_field => try widget_render_controls.emitTextFieldWidget(builder, paint_widget, tokens),
341
+ .textarea => if (paint_widget.code_editor)
342
+ try emitCodeEditorWidget(builder, paint_widget, tokens)
343
+ else
344
+ try widget_render_controls.emitTextFieldWidget(builder, paint_widget, tokens),
340
345
  .search_field, .combobox => try widget_render_controls.emitSearchFieldWidget(builder, paint_widget, tokens),
341
346
  .tooltip => try widget_render_controls.emitTooltipWidget(builder, paint_widget, tokens),
342
347
  .menu_item => try widget_render_controls.emitMenuItemWidget(builder, paint_widget, tokens),
@@ -678,7 +683,7 @@ fn emitWidgetLayoutNodeContent(
678
683
  .text => {
679
684
  if (isSyntaxCodeParagraph(paint_widget)) {
680
685
  if (widgetLayoutNodeVisibleBounds(layout, node_index, paint_widget.frame)) |visible_bounds| {
681
- try emitVisibleCodeTextSpansWidget(builder, paint_widget, tokens, visible_bounds);
686
+ try emitVisibleCodeTextSpansWidget(builder, paint_widget, tokens, visible_bounds, .{});
682
687
  }
683
688
  } else {
684
689
  try emitTextWidget(builder, paint_widget, tokens);
@@ -693,7 +698,11 @@ fn emitWidgetLayoutNodeContent(
693
698
  .button, .toggle_button, .toggle => try widget_render_controls.emitButtonWidget(builder, paint_widget, tokens),
694
699
  .icon_button => try widget_render_controls.emitIconButtonWidget(builder, paint_widget, tokens),
695
700
  .select => try widget_render_controls.emitSelectWidget(builder, paint_widget, tokens),
696
- .input, .text_field, .textarea => try widget_render_controls.emitTextFieldWidget(builder, paint_widget, tokens),
701
+ .input, .text_field => try widget_render_controls.emitTextFieldWidget(builder, paint_widget, tokens),
702
+ .textarea => if (paint_widget.code_editor)
703
+ try emitCodeEditorWidget(builder, paint_widget, tokens)
704
+ else
705
+ try widget_render_controls.emitTextFieldWidget(builder, paint_widget, tokens),
697
706
  .search_field, .combobox => try widget_render_controls.emitSearchFieldWidget(builder, paint_widget, tokens),
698
707
  .tooltip => try widget_render_controls.emitTooltipWidget(builder, paint_widget, tokens),
699
708
  .menu_item => try widget_render_controls.emitMenuItemWidget(builder, paint_widget, tokens),
@@ -1103,6 +1112,151 @@ fn emitTextWidget(builder: *Builder, widget: Widget, tokens: DesignTokens) Error
1103
1112
  if (clip_overflow) try builder.popClip();
1104
1113
  }
1105
1114
 
1115
+ /// Editable code is a textarea behaviorally and a bare code surface
1116
+ /// visually. Selection/caret geometry comes from the shared text-input
1117
+ /// seam while glyphs come from the same syntax-span emitter as read-only
1118
+ /// code; there is intentionally no textarea chrome or component inset.
1119
+ fn emitCodeEditorWidget(builder: *Builder, widget: Widget, tokens: DesignTokens) Error!void {
1120
+ const text_size = widgetTextInputSize(widget, tokens);
1121
+ const text_inset = widgetTextInputInset(widget, tokens);
1122
+ const layout_options = widgetTextInputLayoutOptions(widget, tokens, text_size, text_inset);
1123
+ const origin = widgetTextInputOrigin(widget, tokens, text_size, text_inset, layout_options);
1124
+ const draw_text = widget_text_input.widgetTextInputDrawText(
1125
+ widget,
1126
+ tokens,
1127
+ text_size,
1128
+ origin,
1129
+ widgetForegroundColor(widget, tokens, tokens.colors.text),
1130
+ layout_options,
1131
+ );
1132
+ const selection_range = widget_access.widgetTextSelectionRange(widget);
1133
+ const composition_range = widget_access.widgetTextCompositionRange(widget);
1134
+ const active_row = codeEditorActiveRowRect(widget, draw_text, layout_options, selection_range);
1135
+
1136
+ try builder.pushClip(.{ .id = widgetPartId(widget.id, 16), .rect = widget.frame });
1137
+ if (active_row) |row| {
1138
+ try builder.fillRect(.{
1139
+ .id = codeEditorActiveRowCommandId(widget.id, 0),
1140
+ .rect = pixelSnapGeometryRect(tokens, row),
1141
+ .fill = colorFill(tokens.colors.surface_subtle),
1142
+ });
1143
+ }
1144
+ if (selection_range) |range| {
1145
+ if (!range.isCollapsed(widget.text.len)) {
1146
+ try widget_render_controls.emitWidgetTextSelectionRects(
1147
+ builder,
1148
+ widget,
1149
+ draw_text,
1150
+ layout_options,
1151
+ range,
1152
+ 3,
1153
+ 13,
1154
+ 4,
1155
+ tokens,
1156
+ );
1157
+ }
1158
+ }
1159
+ try emitVisibleCodeTextSpansWidget(builder, widget, tokens, widget.frame, .{});
1160
+ if (selection_range) |range| {
1161
+ if (!range.isCollapsed(widget.text.len)) {
1162
+ try emitCodeEditorSelectedGlyphs(
1163
+ builder,
1164
+ widget,
1165
+ draw_text,
1166
+ layout_options,
1167
+ range,
1168
+ tokens,
1169
+ );
1170
+ }
1171
+ }
1172
+ if (composition_range) |range| {
1173
+ if (!range.isCollapsed(widget.text.len)) {
1174
+ try widget_render_controls.emitWidgetTextCompositionLines(
1175
+ builder,
1176
+ widget,
1177
+ draw_text,
1178
+ layout_options,
1179
+ range,
1180
+ 5,
1181
+ 10,
1182
+ 4,
1183
+ tokens,
1184
+ );
1185
+ }
1186
+ }
1187
+ if (widget.state.focused) {
1188
+ if (selection_range) |range| {
1189
+ if (range.isCollapsed(widget.text.len)) {
1190
+ try widget_render_controls.emitWidgetTextCaret(
1191
+ builder,
1192
+ widget,
1193
+ draw_text,
1194
+ layout_options,
1195
+ range.start,
1196
+ 6,
1197
+ tokens,
1198
+ );
1199
+ }
1200
+ }
1201
+ }
1202
+ // Wrapped code emits its gutter beside the wrapped span rows. The
1203
+ // viewport-owned gutter below is the no-wrap path: emitting both would
1204
+ // duplicate the retained line-number command IDs and place a second set
1205
+ // of markers at logical-line rather than visual-row positions.
1206
+ if (widget.text_no_wrap) {
1207
+ try emitVisibleEditableCodeLineNumberGutter(builder, widget, tokens, widget.frame, active_row);
1208
+ }
1209
+ try builder.popClip();
1210
+ }
1211
+
1212
+ /// Repaint selected code from the same viewport-bounded syntax runs as the
1213
+ /// base pass. Ordinary text inputs can safely repeat their one small text
1214
+ /// command under each selection clip; an editor may retain hundreds of KiB,
1215
+ /// so repeating its full `DrawText` would overflow the 32 KiB display-list
1216
+ /// text store even when only a few visible glyphs are selected.
1217
+ fn emitCodeEditorSelectedGlyphs(
1218
+ builder: *Builder,
1219
+ widget: Widget,
1220
+ text: text_model.DrawText,
1221
+ options: TextLayoutOptions,
1222
+ range: text_model.TextRange,
1223
+ tokens: DesignTokens,
1224
+ ) Error!void {
1225
+ var rect_buffer: [4]text_model.TextSelectionRect = undefined;
1226
+ const rects = text_model.layoutTextSelectionRects(text, options, range, &rect_buffer);
1227
+ for (rects, 0..) |selection, ordinal| {
1228
+ const visible = geometry.RectF.intersection(selection.rect, widget.frame);
1229
+ if (visible.isEmpty()) continue;
1230
+ try builder.pushClip(.{
1231
+ .id = textSpanCommandId(0x5eed_59a2_0000_0017, widget.id, ordinal),
1232
+ .rect = pixelSnapGeometryRect(tokens, selection.rect),
1233
+ });
1234
+ try emitVisibleCodeTextSpansWidget(builder, widget, tokens, visible, .{
1235
+ .color = textSelectionTextColor(widget, tokens),
1236
+ .selection_ordinal = ordinal,
1237
+ });
1238
+ try builder.popClip();
1239
+ }
1240
+ }
1241
+
1242
+ /// The visual row containing the rendered caret. It derives from the
1243
+ /// same scroll/wrap-aware text command as the caret itself, then spans
1244
+ /// the editor viewport instead of the caret's one-point width.
1245
+ fn codeEditorActiveRowRect(
1246
+ widget: Widget,
1247
+ draw_text: text_model.DrawText,
1248
+ layout_options: TextLayoutOptions,
1249
+ selection_range: ?text_model.TextRange,
1250
+ ) ?geometry.RectF {
1251
+ if (!widget.state.focused) return null;
1252
+ const range = selection_range orelse return null;
1253
+ if (!range.isCollapsed(widget.text.len)) return null;
1254
+ const caret = text_model.layoutTextCaretRect(draw_text, layout_options, range.start) orelse return null;
1255
+ const row = geometry.RectF.init(widget.frame.x, caret.y, widget.frame.width, caret.height);
1256
+ const clipped = geometry.RectF.intersection(row, widget.frame);
1257
+ return if (clipped.isEmpty()) null else clipped;
1258
+ }
1259
+
1106
1260
  /// Wrap budget for text painted inside a pixel-snapped frame — the
1107
1261
  /// shared quantum hand-back (`widget_metrics.textWrapMaxWidth`), aliased
1108
1262
  /// for the emit sites here.
@@ -1155,7 +1309,7 @@ fn emitTextSpansWidget(builder: *Builder, widget: Widget, tokens: DesignTokens)
1155
1309
  &runs,
1156
1310
  );
1157
1311
 
1158
- try emitCodeLineNumberGutter(builder, widget, tokens, content, widget.frame, layout_options, null);
1312
+ try emitCodeLineNumberGutter(builder, widget, widget.spans, tokens, content, widget.frame, layout_options, null);
1159
1313
  // Span background highlights (intra-line diff emphasis): one
1160
1314
  // full-line-height rect per run, the same geometry selection rects
1161
1315
  // use, painted before selection and glyphs. Edge-snapped rects of
@@ -1339,19 +1493,32 @@ fn codeTextPrefix(text: []const u8, max_len: usize) []const u8 {
1339
1493
  /// paragraph widgets for layout, selection, copy, and scroll extents, while
1340
1494
  /// the display list contains only line runs that intersect the current
1341
1495
  /// window/scroll clip. Long no-wrap runs are sliced horizontally as well.
1496
+ const CodeTextPaint = struct {
1497
+ color: ?Color = null,
1498
+ selection_ordinal: ?usize = null,
1499
+ };
1500
+
1342
1501
  fn emitVisibleCodeTextSpansWidget(
1343
1502
  builder: *Builder,
1344
1503
  widget: Widget,
1345
1504
  tokens: DesignTokens,
1346
1505
  visible_bounds: geometry.RectF,
1506
+ paint: CodeTextPaint,
1347
1507
  ) Error!void {
1508
+ if (widget.code_editor and widget.text_no_wrap) {
1509
+ return emitVisibleEditableCodeLines(builder, widget, tokens, visible_bounds, paint);
1510
+ }
1511
+ if (widget.code_editor) {
1512
+ return emitVisibleWrappedEditableCodeLines(builder, widget, tokens, visible_bounds, paint);
1513
+ }
1514
+ const spans = widget.spans;
1348
1515
  const content = widget_metrics.widgetTextSpanContentFrame(widget, tokens);
1349
1516
  const layout_options = widget_metrics.widgetTextSpanLayoutOptions(
1350
1517
  widget,
1351
1518
  tokens,
1352
1519
  textWrapMaxWidth(tokens, content.width),
1353
1520
  );
1354
- const line_height = text_spans_model.textSpanLineHeight(widget.spans, layout_options);
1521
+ const line_height = text_spans_model.textSpanLineHeight(spans, layout_options);
1355
1522
  const visible_line: usize = if (line_height > 0 and
1356
1523
  std.math.isFinite(line_height) and
1357
1524
  std.math.isFinite(visible_bounds.y - content.y) and
@@ -1370,13 +1537,15 @@ fn emitVisibleCodeTextSpansWidget(
1370
1537
  var budget = CodeEmissionBudget.init(builder);
1371
1538
  if (!budget.hasCommand(builder)) return;
1372
1539
 
1373
- try emitCodeLineNumberGutter(builder, widget, tokens, content, visible_bounds, layout_options, &budget);
1374
- try emitStaticTextSelectionBounded(builder, widget, tokens, budget.command_ceiling);
1540
+ if (paint.selection_ordinal == null) {
1541
+ try emitCodeLineNumberGutter(builder, widget, spans, tokens, content, visible_bounds, layout_options, &budget);
1542
+ try emitStaticTextSelectionBounded(builder, widget, tokens, budget.command_ceiling);
1543
+ }
1375
1544
  if (!budget.hasCommand(builder) or budget.remainingText() == 0) return;
1376
1545
  var page_first_line = visible_line -| 1;
1377
1546
  while (true) {
1378
1547
  const layout = text_spans_model.layoutTextSpansFromLine(
1379
- widget.spans,
1548
+ spans,
1380
1549
  layout_options,
1381
1550
  page_first_line,
1382
1551
  &runs,
@@ -1392,7 +1561,7 @@ fn emitVisibleCodeTextSpansWidget(
1392
1561
  );
1393
1562
  if (!run_bounds.intersects(visible_bounds)) continue;
1394
1563
 
1395
- const span = widget.spans[run.span_index];
1564
+ const span = spans[run.span_index];
1396
1565
  const absolute_x = content.x + run.x;
1397
1566
  const visible = text_spans_model.textSpanRunVisibleSlice(
1398
1567
  span,
@@ -1404,10 +1573,10 @@ fn emitVisibleCodeTextSpansWidget(
1404
1573
  if (!budget.hasCommand(builder)) return;
1405
1574
  const admitted_text = codeTextPrefix(visible.text, budget.remainingText());
1406
1575
  if (admitted_text.len == 0) return;
1407
- const color = text_spans_model.textSpanColorValue(tokens.colors, span.color.?);
1576
+ const color = paint.color orelse text_spans_model.textSpanColorValue(tokens.colors, span.color.?);
1408
1577
  const origin = pixelSnapTextPoint(tokens, geometry.PointF.init(absolute_x + visible.x, content.y + run.baseline));
1409
1578
  try builder.drawText(.{
1410
- .id = codeTextSpanRunCommandId(widget, run),
1579
+ .id = codeTextPaintCommandId(codeTextSpanRunCommandId(widget, spans, run), paint),
1411
1580
  .font_id = run.font_id,
1412
1581
  .size = run.size,
1413
1582
  .origin = origin,
@@ -1435,6 +1604,408 @@ fn emitVisibleCodeTextSpansWidget(
1435
1604
  }
1436
1605
  }
1437
1606
 
1607
+ /// Wrapped editors cannot retain one syntax span for every token in a large
1608
+ /// document. Tokenize one logical line at a time while advancing lexer state
1609
+ /// through the prefix, then paint only lines intersecting the viewport. Each
1610
+ /// line gets the full bounded span budget without dropping later syntax.
1611
+ fn emitVisibleWrappedEditableCodeLines(
1612
+ builder: *Builder,
1613
+ widget: Widget,
1614
+ tokens: DesignTokens,
1615
+ visible_bounds: geometry.RectF,
1616
+ paint: CodeTextPaint,
1617
+ ) Error!void {
1618
+ var content = widget_metrics.widgetTextSpanContentFrame(widget, tokens);
1619
+ content.x -= widget.value_x;
1620
+ content.y -= widget.value;
1621
+ const layout_options = widget_metrics.widgetTextSpanLayoutOptions(
1622
+ widget,
1623
+ tokens,
1624
+ // Interactive glyphs must wrap on the exact same width as caret,
1625
+ // selection, composition, and hit-test geometry. The pixel-snap
1626
+ // hand-back is safe only for non-interactive paragraph painting.
1627
+ content.width,
1628
+ );
1629
+ const line_height = text_spans_model.textSpanLineHeight(widget.spans, layout_options);
1630
+ if (line_height <= 0 or !std.math.isFinite(line_height)) return;
1631
+
1632
+ var budget = CodeEmissionBudget.init(builder);
1633
+ if (!budget.hasCommand(builder)) return;
1634
+ if (paint.selection_ordinal == null) {
1635
+ var gutter_content = content;
1636
+ gutter_content.x += widget.value_x;
1637
+ try emitCodeLineNumberGutter(
1638
+ builder,
1639
+ widget,
1640
+ widget.spans,
1641
+ tokens,
1642
+ gutter_content,
1643
+ visible_bounds,
1644
+ layout_options,
1645
+ &budget,
1646
+ );
1647
+ }
1648
+ if (!budget.hasCommand(builder) or budget.remainingText() == 0) return;
1649
+
1650
+ var state: code_model.HighlightState = .{};
1651
+ var span_storage: [text_spans_model.max_text_spans_per_paragraph]text_spans_model.TextSpan = undefined;
1652
+ var line_runs: [text_spans_model.max_text_span_runs_per_paragraph]text_spans_model.TextSpanRun = undefined;
1653
+ var line_start: usize = 0;
1654
+ var logical_line: usize = 0;
1655
+ var visual_line: usize = 0;
1656
+
1657
+ while (line_start <= widget.text.len) : (logical_line += 1) {
1658
+ const newline = std.mem.indexOfScalarPos(u8, widget.text, line_start, '\n');
1659
+ const line_end = if (newline) |index| index + 1 else widget.text.len;
1660
+ const line_source = widget.text[line_start..line_end];
1661
+ const highlighted = if (line_source.len == 0) blk: {
1662
+ span_storage[0] = .{
1663
+ .text = line_source,
1664
+ .monospace = true,
1665
+ .color = .syntax_plain,
1666
+ };
1667
+ break :blk span_storage[0..1];
1668
+ } else code_model.highlightWithState(
1669
+ line_source,
1670
+ widget.code_language,
1671
+ &span_storage,
1672
+ &state,
1673
+ );
1674
+ const line_layout = text_spans_model.layoutTextSpans(
1675
+ highlighted,
1676
+ layout_options,
1677
+ &line_runs,
1678
+ );
1679
+ const visual_count = @max(1, line_layout.line_count);
1680
+ const line_top = content.y + @as(f32, @floatFromInt(visual_line)) * line_height;
1681
+ const line_bottom = line_top + @as(f32, @floatFromInt(visual_count)) * line_height;
1682
+ if (line_top > visible_bounds.maxY()) return;
1683
+
1684
+ if (line_bottom >= visible_bounds.y) {
1685
+ const first_visible_line: usize = if (visible_bounds.y > line_top and
1686
+ std.math.isFinite(visible_bounds.y - line_top))
1687
+ @intFromFloat(@floor((visible_bounds.y - line_top) / line_height))
1688
+ else
1689
+ 0;
1690
+ const last_visible_line: usize = if (visible_bounds.maxY() > line_top and
1691
+ std.math.isFinite(visible_bounds.maxY() - line_top))
1692
+ @intFromFloat(@floor((visible_bounds.maxY() - line_top) / line_height))
1693
+ else
1694
+ first_visible_line;
1695
+ var page_first_line = first_visible_line -| 1;
1696
+ while (true) {
1697
+ const page_layout = if (page_first_line == 0)
1698
+ line_layout
1699
+ else
1700
+ text_spans_model.layoutTextSpansFromLine(
1701
+ highlighted,
1702
+ layout_options,
1703
+ page_first_line,
1704
+ &line_runs,
1705
+ );
1706
+ for (page_layout.runs) |run| {
1707
+ if (run.text.len == 0) continue;
1708
+ const local_bounds = text_spans_model.textSpanRunBounds(page_layout, run);
1709
+ const run_bounds = geometry.RectF.init(
1710
+ content.x + local_bounds.x,
1711
+ line_top + local_bounds.y,
1712
+ local_bounds.width,
1713
+ local_bounds.height,
1714
+ );
1715
+ if (!run_bounds.intersects(visible_bounds)) continue;
1716
+ const span = highlighted[run.span_index];
1717
+ const absolute_x = content.x + run.x;
1718
+ const visible = text_spans_model.textSpanRunVisibleSlice(
1719
+ span,
1720
+ run,
1721
+ layout_options,
1722
+ visible_bounds.x - absolute_x,
1723
+ visible_bounds.maxX() - absolute_x,
1724
+ ) orelse continue;
1725
+ if (!budget.hasCommand(builder)) return;
1726
+ const admitted_text = codeTextPrefix(visible.text, budget.remainingText());
1727
+ if (admitted_text.len == 0) return;
1728
+ const color_ref = span.color orelse .syntax_plain;
1729
+ try builder.drawText(.{
1730
+ .id = codeTextPaintCommandId(editableCodeRunCommandId(widget, logical_line, run), paint),
1731
+ .font_id = run.font_id,
1732
+ .size = run.size,
1733
+ .origin = pixelSnapTextPoint(tokens, geometry.PointF.init(
1734
+ absolute_x + visible.x,
1735
+ line_top + run.baseline,
1736
+ )),
1737
+ .color = paint.color orelse text_spans_model.textSpanColorValue(tokens.colors, color_ref),
1738
+ .text = admitted_text,
1739
+ .text_layout = .{
1740
+ .max_width = 0,
1741
+ .line_height = line_height,
1742
+ .wrap = .none,
1743
+ .alignment = .start,
1744
+ .measure = tokens.text_measure,
1745
+ },
1746
+ });
1747
+ budget.chargeText(admitted_text.len);
1748
+ }
1749
+
1750
+ const next_first_line = page_first_line +| text_spans_model.max_text_span_lines_per_paragraph;
1751
+ if (next_first_line <= page_first_line or
1752
+ next_first_line > last_visible_line or
1753
+ next_first_line >= line_layout.line_count)
1754
+ {
1755
+ break;
1756
+ }
1757
+ page_first_line = next_first_line;
1758
+ }
1759
+ }
1760
+
1761
+ visual_line += visual_count;
1762
+ if (newline == null) break;
1763
+ line_start = line_end;
1764
+ }
1765
+ }
1766
+
1767
+ /// Large no-wrap editors retain one plain source span, then tokenize just
1768
+ /// the logical lines intersecting the viewport. Lexer state is advanced
1769
+ /// across the offscreen prefix so multiline comments, strings, and JSX
1770
+ /// constructs remain correct without retaining an unbounded run list.
1771
+ fn emitVisibleEditableCodeLines(
1772
+ builder: *Builder,
1773
+ widget: Widget,
1774
+ tokens: DesignTokens,
1775
+ visible_bounds: geometry.RectF,
1776
+ paint: CodeTextPaint,
1777
+ ) Error!void {
1778
+ var content = widget_metrics.widgetTextSpanContentFrame(widget, tokens);
1779
+ content.x -= widget.value_x;
1780
+ content.y -= widget.value;
1781
+ const layout_options = widget_metrics.widgetTextSpanLayoutOptions(
1782
+ widget,
1783
+ tokens,
1784
+ 0,
1785
+ );
1786
+ const line_height = text_spans_model.textSpanLineHeight(widget.spans, layout_options);
1787
+ if (line_height <= 0 or !std.math.isFinite(line_height)) return;
1788
+
1789
+ const first_visible: usize = if (visible_bounds.y > content.y and
1790
+ std.math.isFinite(visible_bounds.y - content.y))
1791
+ @intFromFloat(@floor((visible_bounds.y - content.y) / line_height))
1792
+ else
1793
+ 0;
1794
+ const last_visible: usize = if (visible_bounds.maxY() > content.y and
1795
+ std.math.isFinite(visible_bounds.maxY() - content.y))
1796
+ @intFromFloat(@floor((visible_bounds.maxY() - content.y) / line_height))
1797
+ else
1798
+ first_visible;
1799
+ const first_line = first_visible -| 1;
1800
+ const last_line = last_visible +| 1;
1801
+
1802
+ var line_start: usize = 0;
1803
+ var logical_line: usize = 0;
1804
+ while (logical_line < first_line and line_start < widget.text.len) : (logical_line += 1) {
1805
+ const newline = std.mem.indexOfScalarPos(u8, widget.text, line_start, '\n') orelse {
1806
+ line_start = widget.text.len;
1807
+ break;
1808
+ };
1809
+ line_start = newline + 1;
1810
+ }
1811
+
1812
+ var state: code_model.HighlightState = .{};
1813
+ var span_storage: [text_spans_model.max_text_spans_per_paragraph]text_spans_model.TextSpan = undefined;
1814
+ if (line_start > 0) {
1815
+ _ = code_model.highlightWithState(
1816
+ widget.text[0..line_start],
1817
+ widget.code_language,
1818
+ &span_storage,
1819
+ &state,
1820
+ );
1821
+ }
1822
+
1823
+ var budget = CodeEmissionBudget.init(builder);
1824
+ if (!budget.hasCommand(builder)) return;
1825
+ var line_runs: [text_spans_model.max_text_span_runs_per_paragraph]text_spans_model.TextSpanRun = undefined;
1826
+
1827
+ while (line_start <= widget.text.len and logical_line <= last_line) : (logical_line += 1) {
1828
+ const newline = std.mem.indexOfScalarPos(u8, widget.text, line_start, '\n');
1829
+ const line_end = if (newline) |index| index + 1 else widget.text.len;
1830
+ const line_source = widget.text[line_start..line_end];
1831
+ const highlighted = if (line_source.len == 0) blk: {
1832
+ span_storage[0] = .{
1833
+ .text = line_source,
1834
+ .monospace = true,
1835
+ .color = .syntax_plain,
1836
+ };
1837
+ break :blk span_storage[0..1];
1838
+ } else code_model.highlightWithState(
1839
+ line_source,
1840
+ widget.code_language,
1841
+ &span_storage,
1842
+ &state,
1843
+ );
1844
+ const line_layout = text_spans_model.layoutTextSpans(
1845
+ highlighted,
1846
+ layout_options,
1847
+ &line_runs,
1848
+ );
1849
+ const line_top = content.y + @as(f32, @floatFromInt(logical_line)) * line_height;
1850
+
1851
+ for (line_layout.runs) |run| {
1852
+ if (run.text.len == 0) continue;
1853
+ const local_bounds = text_spans_model.textSpanRunBounds(line_layout, run);
1854
+ const run_bounds = geometry.RectF.init(
1855
+ content.x + local_bounds.x,
1856
+ line_top + local_bounds.y,
1857
+ local_bounds.width,
1858
+ local_bounds.height,
1859
+ );
1860
+ if (!run_bounds.intersects(visible_bounds)) continue;
1861
+ const span = highlighted[run.span_index];
1862
+ const absolute_x = content.x + run.x;
1863
+ const visible = text_spans_model.textSpanRunVisibleSlice(
1864
+ span,
1865
+ run,
1866
+ layout_options,
1867
+ visible_bounds.x - absolute_x,
1868
+ visible_bounds.maxX() - absolute_x,
1869
+ ) orelse continue;
1870
+ if (!budget.hasCommand(builder)) return;
1871
+ const admitted_text = codeTextPrefix(visible.text, budget.remainingText());
1872
+ if (admitted_text.len == 0) return;
1873
+ const color_ref = span.color orelse .syntax_plain;
1874
+ try builder.drawText(.{
1875
+ .id = codeTextPaintCommandId(editableCodeRunCommandId(widget, logical_line, run), paint),
1876
+ .font_id = run.font_id,
1877
+ .size = run.size,
1878
+ .origin = pixelSnapTextPoint(tokens, geometry.PointF.init(
1879
+ absolute_x + visible.x,
1880
+ line_top + run.baseline,
1881
+ )),
1882
+ .color = paint.color orelse text_spans_model.textSpanColorValue(tokens.colors, color_ref),
1883
+ .text = admitted_text,
1884
+ .text_layout = .{
1885
+ .max_width = 0,
1886
+ .line_height = line_height,
1887
+ .wrap = .none,
1888
+ .alignment = .start,
1889
+ .measure = tokens.text_measure,
1890
+ },
1891
+ });
1892
+ budget.chargeText(admitted_text.len);
1893
+ }
1894
+
1895
+ if (newline == null) break;
1896
+ line_start = line_end;
1897
+ }
1898
+ }
1899
+
1900
+ /// The editable no-wrap code path scrolls source glyphs beneath a pinned
1901
+ /// line-number gutter. Paint that gutter last so source, selection, IME
1902
+ /// decoration, and the caret all disappear cleanly behind its opaque
1903
+ /// surface instead of clashing with the anchored markers.
1904
+ fn emitVisibleEditableCodeLineNumberGutter(
1905
+ builder: *Builder,
1906
+ widget: Widget,
1907
+ tokens: DesignTokens,
1908
+ visible_bounds: geometry.RectF,
1909
+ active_row: ?geometry.RectF,
1910
+ ) Error!void {
1911
+ if (widget.code_line_number_digits == 0) return;
1912
+
1913
+ var content = widget_metrics.widgetTextSpanContentFrame(widget, tokens);
1914
+ content.y -= widget.value;
1915
+ const layout_options = widget_metrics.widgetTextSpanLayoutOptions(widget, tokens, 0);
1916
+ const line_height = text_spans_model.textSpanLineHeight(widget.spans, layout_options);
1917
+ if (line_height <= 0 or !std.math.isFinite(line_height)) return;
1918
+
1919
+ const padded = widget.frame.inset(widget.layout.padding);
1920
+ const marker_width = @max(0, content.x - 12 - padded.x);
1921
+ const gutter_bounds = geometry.RectF.intersection(
1922
+ geometry.RectF.init(
1923
+ widget.frame.x,
1924
+ widget.frame.y,
1925
+ @max(0, content.x - widget.frame.x),
1926
+ widget.frame.height,
1927
+ ),
1928
+ visible_bounds,
1929
+ );
1930
+ if (!gutter_bounds.isEmpty()) {
1931
+ try builder.fillRect(.{
1932
+ .id = codeLineNumberGutterCommandId(widget.id),
1933
+ .rect = pixelSnapGeometryRect(tokens, gutter_bounds),
1934
+ .fill = colorFill(widgetBackgroundColor(widget, tokens.colors.background)),
1935
+ });
1936
+ }
1937
+ if (active_row) |row| {
1938
+ const active_gutter = geometry.RectF.intersection(gutter_bounds, row);
1939
+ if (!active_gutter.isEmpty()) {
1940
+ try builder.fillRect(.{
1941
+ .id = codeEditorActiveRowCommandId(widget.id, 1),
1942
+ .rect = pixelSnapGeometryRect(tokens, active_gutter),
1943
+ .fill = colorFill(tokens.colors.surface_subtle),
1944
+ });
1945
+ }
1946
+ }
1947
+
1948
+ const first_visible: usize = if (visible_bounds.y > content.y and
1949
+ std.math.isFinite(visible_bounds.y - content.y))
1950
+ @intFromFloat(@floor((visible_bounds.y - content.y) / line_height))
1951
+ else
1952
+ 0;
1953
+ const last_visible: usize = if (visible_bounds.maxY() > content.y and
1954
+ std.math.isFinite(visible_bounds.maxY() - content.y))
1955
+ @intFromFloat(@floor((visible_bounds.maxY() - content.y) / line_height))
1956
+ else
1957
+ first_visible;
1958
+ const first_line = first_visible -| 1;
1959
+ const last_line = last_visible +| 1;
1960
+
1961
+ var line_start: usize = 0;
1962
+ var logical_line: usize = 0;
1963
+ while (logical_line < first_line and line_start < widget.text.len) : (logical_line += 1) {
1964
+ const newline = std.mem.indexOfScalarPos(u8, widget.text, line_start, '\n') orelse {
1965
+ line_start = widget.text.len;
1966
+ break;
1967
+ };
1968
+ line_start = newline + 1;
1969
+ }
1970
+
1971
+ while (line_start <= widget.text.len and logical_line <= last_line) : (logical_line += 1) {
1972
+ const line_top = content.y + @as(f32, @floatFromInt(logical_line)) * line_height;
1973
+ const marker_bounds = geometry.RectF.init(padded.x, line_top, marker_width, line_height);
1974
+ if (marker_bounds.intersects(visible_bounds)) {
1975
+ var marker_buffer: [20]u8 = undefined;
1976
+ const marker_formatted = std.fmt.bufPrint(
1977
+ &marker_buffer,
1978
+ "{d}",
1979
+ .{logical_line + 1},
1980
+ ) catch "";
1981
+ const marker_text = builder.allocTextBytes(marker_formatted) catch "";
1982
+ if (marker_text.len > 0) {
1983
+ try builder.drawText(.{
1984
+ .id = codeLineNumberCommandId(widget.id, logical_line + 1),
1985
+ .font_id = tokens.typography.mono_font_id,
1986
+ .size = layout_options.size,
1987
+ .origin = pixelSnapTextPoint(tokens, geometry.PointF.init(
1988
+ padded.x,
1989
+ line_top + layout_options.size,
1990
+ )),
1991
+ .color = tokens.colors.text_muted,
1992
+ .text = marker_text,
1993
+ .text_layout = .{
1994
+ .max_width = marker_width,
1995
+ .line_height = line_height,
1996
+ .wrap = .none,
1997
+ .alignment = .end,
1998
+ .measure = tokens.text_measure,
1999
+ },
2000
+ });
2001
+ }
2002
+ }
2003
+
2004
+ const newline = std.mem.indexOfScalarPos(u8, widget.text, line_start, '\n') orelse break;
2005
+ line_start = newline + 1;
2006
+ }
2007
+ }
2008
+
1438
2009
  /// Muted logical-line markers for one coherent code paragraph. Each
1439
2010
  /// logical line is measured independently with the same monospace layout
1440
2011
  /// options; summing those wrapped extents places the next marker on the
@@ -1442,6 +2013,7 @@ fn emitVisibleCodeTextSpansWidget(
1442
2013
  fn emitCodeLineNumberGutter(
1443
2014
  builder: *Builder,
1444
2015
  widget: Widget,
2016
+ spans: []const text_spans_model.TextSpan,
1445
2017
  tokens: DesignTokens,
1446
2018
  content: geometry.RectF,
1447
2019
  visible_bounds: geometry.RectF,
@@ -1449,12 +2021,11 @@ fn emitCodeLineNumberGutter(
1449
2021
  budget: ?*CodeEmissionBudget,
1450
2022
  ) Error!void {
1451
2023
  if (widget.code_line_number_digits == 0) return;
1452
- const line_height = text_spans_model.textSpanLineHeight(widget.spans, layout_options);
2024
+ const line_height = text_spans_model.textSpanLineHeight(spans, layout_options);
1453
2025
  if (line_height <= 0 or !std.math.isFinite(line_height)) return;
1454
2026
 
1455
2027
  const padded = widget.frame.inset(widget.layout.padding);
1456
2028
  const marker_width = @max(0, content.x - 12 - padded.x);
1457
- const digits = @min(@as(usize, widget.code_line_number_digits), 20);
1458
2029
  var line_runs: [text_spans_model.max_text_span_runs_per_paragraph]text_spans_model.TextSpanRun = undefined;
1459
2030
  var logical_line: usize = 1;
1460
2031
  var visual_line: usize = 0;
@@ -1462,7 +2033,7 @@ fn emitCodeLineNumberGutter(
1462
2033
  while (line_start <= widget.text.len) : (logical_line += 1) {
1463
2034
  // A terminal newline closes the preceding painted line; the span
1464
2035
  // breaker intentionally does not reserve another empty visual line.
1465
- if (line_start == widget.text.len and widget.text.len > 0) break;
2036
+ if (line_start == widget.text.len and widget.text.len > 0 and !widget.code_editor) break;
1466
2037
  const newline = std.mem.indexOfScalarPos(u8, widget.text, line_start, '\n');
1467
2038
  const line_end = newline orelse widget.text.len;
1468
2039
  const baseline = content.y + layout_options.size +
@@ -1474,7 +2045,13 @@ fn emitCodeLineNumberGutter(
1474
2045
  line_height,
1475
2046
  );
1476
2047
  if (marker_bounds.intersects(visible_bounds)) {
1477
- const marker_text = codeLineNumberText(logical_line, digits);
2048
+ var marker_buffer: [20]u8 = undefined;
2049
+ const marker_formatted = std.fmt.bufPrint(
2050
+ &marker_buffer,
2051
+ "{d}",
2052
+ .{logical_line},
2053
+ ) catch "";
2054
+ const marker_text = builder.allocTextBytes(marker_formatted) catch "";
1478
2055
  if (budget) |admission| {
1479
2056
  if (!admission.hasCommand(builder)) return;
1480
2057
  if (marker_text.len > admission.remainingText()) return;
@@ -1518,34 +2095,70 @@ fn emitCodeLineNumberGutter(
1518
2095
  }
1519
2096
  }
1520
2097
 
1521
- const code_line_number_texts = blk: {
1522
- var values: [128][3]u8 = @splat(@splat(' '));
1523
- for (&values, 1..) |*text, line_number| {
1524
- var value = line_number;
1525
- var cursor: usize = text.len;
1526
- while (cursor > 0 and value > 0) {
1527
- cursor -= 1;
1528
- text[cursor] = '0' + @as(u8, @intCast(value % 10));
1529
- value /= 10;
1530
- }
2098
+ fn codeTextSpanRunCommandId(widget: Widget, spans: []const text_spans_model.TextSpan, run: text_spans_model.TextSpanRun) ObjectId {
2099
+ if (text_spans_model.textSpanRunParagraphRange(widget.text, run)) |range| {
2100
+ var hasher = std.hash.Wyhash.init(0x5eed_59a2_0000_0011);
2101
+ hasher.update(std.mem.asBytes(&widget.id));
2102
+ hasher.update(std.mem.asBytes(&run.line_index));
2103
+ hasher.update(std.mem.asBytes(&range.start));
2104
+ const value = hasher.final();
2105
+ return if (value == 0) 1 else value;
1531
2106
  }
1532
- break :blk values;
1533
- };
1534
2107
 
1535
- fn codeLineNumberText(logical_line: usize, digits: usize) []const u8 {
1536
- if (logical_line == 0 or logical_line > code_line_number_texts.len) return "";
1537
- const text = &code_line_number_texts[logical_line - 1];
1538
- const len = @min(digits, text.len);
1539
- return text[text.len - len ..];
2108
+ // An editable widget's runtime-owned text allocation can briefly be
2109
+ // distinct from the source allocation its freshly highlighted spans
2110
+ // reference. Paragraph-relative pointer arithmetic is unavailable in
2111
+ // that state, but every layout run still belongs to one span. Key the
2112
+ // fallback by that span and the run's offset within it; collapsing all
2113
+ // detached runs to byte zero gives every token on a line the same id
2114
+ // and makes the retained display-list diff reject the frame.
2115
+ var hasher = std.hash.Wyhash.init(0x5eed_59a2_0000_0013);
2116
+ hasher.update(std.mem.asBytes(&widget.id));
2117
+ hasher.update(std.mem.asBytes(&run.line_index));
2118
+ hasher.update(std.mem.asBytes(&run.span_index));
2119
+ if (run.span_index < spans.len) {
2120
+ const span = spans[run.span_index];
2121
+ const span_start = @intFromPtr(span.text.ptr);
2122
+ const run_start = @intFromPtr(run.text.ptr);
2123
+ if (run_start >= span_start and run_start - span_start + run.text.len <= span.text.len) {
2124
+ const span_offset = run_start - span_start;
2125
+ hasher.update(std.mem.asBytes(&span_offset));
2126
+ const value = hasher.final();
2127
+ return if (value == 0) 1 else value;
2128
+ }
2129
+ }
2130
+ hasher.update(std.mem.asBytes(&run.x));
2131
+ hasher.update(std.mem.asBytes(&run.text.len));
2132
+ hasher.update(run.text);
2133
+ const value = hasher.final();
2134
+ return if (value == 0) 1 else value;
1540
2135
  }
1541
2136
 
1542
- fn codeTextSpanRunCommandId(widget: Widget, run: text_spans_model.TextSpanRun) ObjectId {
1543
- const range = text_spans_model.textSpanRunParagraphRange(widget.text, run) orelse
1544
- text_model.TextRange.init(0, run.text.len);
1545
- var hasher = std.hash.Wyhash.init(0x5eed_59a2_0000_0011);
2137
+ fn editableCodeRunCommandId(
2138
+ widget: Widget,
2139
+ logical_line: usize,
2140
+ run: text_spans_model.TextSpanRun,
2141
+ ) ObjectId {
2142
+ var hasher = std.hash.Wyhash.init(0x5eed_59a2_0000_0014);
1546
2143
  hasher.update(std.mem.asBytes(&widget.id));
1547
- hasher.update(std.mem.asBytes(&run.line_index));
1548
- hasher.update(std.mem.asBytes(&range.start));
2144
+ hasher.update(std.mem.asBytes(&logical_line));
2145
+ const text_start = @intFromPtr(widget.text.ptr);
2146
+ const run_start = @intFromPtr(run.text.ptr);
2147
+ if (run_start >= text_start and run_start - text_start <= widget.text.len) {
2148
+ const offset = run_start - text_start;
2149
+ hasher.update(std.mem.asBytes(&offset));
2150
+ } else {
2151
+ hasher.update(run.text);
2152
+ }
2153
+ const value = hasher.final();
2154
+ return if (value == 0) 1 else value;
2155
+ }
2156
+
2157
+ fn codeTextPaintCommandId(base_id: ObjectId, paint: CodeTextPaint) ObjectId {
2158
+ const ordinal = paint.selection_ordinal orelse return base_id;
2159
+ var hasher = std.hash.Wyhash.init(0x5eed_59a2_0000_0018);
2160
+ hasher.update(std.mem.asBytes(&base_id));
2161
+ hasher.update(std.mem.asBytes(&ordinal));
1549
2162
  const value = hasher.final();
1550
2163
  return if (value == 0) 1 else value;
1551
2164
  }
@@ -1554,6 +2167,14 @@ fn codeLineNumberCommandId(widget_id: ObjectId, logical_line: usize) ObjectId {
1554
2167
  return textSpanCommandId(0x5eed_59a2_0000_0012, widget_id, logical_line);
1555
2168
  }
1556
2169
 
2170
+ fn codeLineNumberGutterCommandId(widget_id: ObjectId) ObjectId {
2171
+ return textSpanCommandId(0x5eed_59a2_0000_0015, widget_id, 0);
2172
+ }
2173
+
2174
+ fn codeEditorActiveRowCommandId(widget_id: ObjectId, ordinal: usize) ObjectId {
2175
+ return textSpanCommandId(0x5eed_59a2_0000_0016, widget_id, ordinal);
2176
+ }
2177
+
1557
2178
  pub fn textSpanRunCommandId(widget_id: ObjectId, ordinal: usize) ObjectId {
1558
2179
  return textSpanCommandId(0x5eed_59a2_0000_0001, widget_id, ordinal);
1559
2180
  }