@native-sdk/cli 0.7.0 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/build/app.zig +8 -0
  2. package/build.zig +114 -103
  3. package/native-sdk.d.ts +6 -3
  4. package/package.json +9 -9
  5. package/packages/core/package-lock.json +2 -2
  6. package/packages/core/package.json +1 -1
  7. package/packages/core/sdk/text.ts +25 -14
  8. package/packages/core/src/checker.ts +39 -0
  9. package/packages/core/src/emitter.ts +125 -1
  10. package/skill-data/native-ui/SKILL.md +11 -7
  11. package/src/platform/macos/appkit_host.m +76 -4
  12. package/src/platform/macos/cef_host.mm +24 -2
  13. package/src/platform/macos/root.zig +13 -0
  14. package/src/platform/types.zig +4 -0
  15. package/src/platform/windows/gpu_surface_renderer.cpp +2280 -0
  16. package/src/platform/windows/gpu_surface_renderer.h +86 -0
  17. package/src/platform/windows/root.zig +298 -20
  18. package/src/platform/windows/webview2_host.cpp +343 -33
  19. package/src/primitives/canvas/code.zig +501 -33
  20. package/src/primitives/canvas/code_tests.zig +1029 -6
  21. package/src/primitives/canvas/events.zig +111 -1
  22. package/src/primitives/canvas/frame.zig +45 -1
  23. package/src/primitives/canvas/gpu.zig +6 -0
  24. package/src/primitives/canvas/markdown_tests.zig +17 -18
  25. package/src/primitives/canvas/render_tests.zig +93 -0
  26. package/src/primitives/canvas/root.zig +6 -0
  27. package/src/primitives/canvas/serialization.zig +150 -21
  28. package/src/primitives/canvas/text.zig +6 -0
  29. package/src/primitives/canvas/themes/geist.zig +51 -5
  30. package/src/primitives/canvas/tokens.zig +33 -4
  31. package/src/primitives/canvas/ui.zig +160 -36
  32. package/src/primitives/canvas/ui_markup.zig +60 -9
  33. package/src/primitives/canvas/ui_markup_compiled.zig +41 -0
  34. package/src/primitives/canvas/ui_markup_compiled_tests.zig +12 -0
  35. package/src/primitives/canvas/ui_markup_contract.zig +17 -1
  36. package/src/primitives/canvas/ui_markup_view.zig +44 -0
  37. package/src/primitives/canvas/ui_markup_view_tests.zig +80 -6
  38. package/src/primitives/canvas/ui_schema.zig +18 -0
  39. package/src/primitives/canvas/ui_schema_tests.zig +10 -7
  40. package/src/primitives/canvas/ui_tests.zig +40 -0
  41. package/src/primitives/canvas/widget_builtin_tests.zig +513 -13
  42. package/src/primitives/canvas/widget_invalidation.zig +12 -1
  43. package/src/primitives/canvas/widget_layout.zig +260 -21
  44. package/src/primitives/canvas/widget_metrics.zig +47 -6
  45. package/src/primitives/canvas/widget_render.zig +849 -78
  46. package/src/primitives/canvas/widget_render_controls.zig +83 -25
  47. package/src/primitives/canvas/widget_render_style.zig +52 -8
  48. package/src/primitives/canvas/widget_runtime.zig +2 -0
  49. package/src/primitives/canvas/widget_text_input.zig +167 -8
  50. package/src/primitives/canvas/widget_tree.zig +6 -5
  51. package/src/primitives/canvas/widgets.zig +78 -2
  52. package/src/runtime/canvas_budget_edge_tests.zig +3 -2
  53. package/src/runtime/canvas_frame.zig +60 -7
  54. package/src/runtime/canvas_frame_invalidation_tests.zig +80 -0
  55. package/src/runtime/canvas_frame_packet_tests.zig +83 -9
  56. package/src/runtime/canvas_frame_patch_tests.zig +16 -0
  57. package/src/runtime/canvas_images.zig +8 -2
  58. package/src/runtime/canvas_limits.zig +13 -6
  59. package/src/runtime/canvas_widget_clipboard_tests.zig +8 -5
  60. package/src/runtime/canvas_widget_context_menu.zig +7 -2
  61. package/src/runtime/canvas_widget_event_tests.zig +18 -14
  62. package/src/runtime/canvas_widget_events.zig +51 -20
  63. package/src/runtime/canvas_widget_runtime.zig +108 -4
  64. package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
  65. package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
  66. package/src/runtime/canvas_widget_state.zig +27 -0
  67. package/src/runtime/canvas_widget_text_tests.zig +326 -0
  68. package/src/runtime/core.zig +15 -5
  69. package/src/runtime/effects.zig +6 -0
  70. package/src/runtime/effects_tests.zig +30 -0
  71. package/src/runtime/gpu_surface_events.zig +50 -19
  72. package/src/runtime/media_surface_tests.zig +1 -1
  73. package/src/runtime/runtime_event_tests.zig +74 -0
  74. package/src/runtime/ui_app.zig +10 -8
  75. package/src/runtime/ui_app_tests.zig +67 -0
  76. package/src/runtime/view.zig +60 -16
  77. package/src/runtime/view_canvas.zig +15 -0
  78. package/src/runtime/view_widget_scroll.zig +48 -18
  79. package/src/runtime/view_widget_text.zig +59 -16
  80. package/src/runtime/window_view_runtime.zig +10 -0
  81. package/src/tooling/templates.zig +9 -6
@@ -4,6 +4,7 @@ const 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
+ try emitVisibleCodeTextSpansWidget(builder, widget, tokens, widget.frame, .{});
1145
+ if (selection_range) |range| {
1146
+ if (!range.isCollapsed(widget.text.len)) {
1147
+ try widget_render_controls.emitWidgetTextSelectionRects(
1148
+ builder,
1149
+ widget,
1150
+ draw_text,
1151
+ layout_options,
1152
+ range,
1153
+ 3,
1154
+ 13,
1155
+ 4,
1156
+ tokens,
1157
+ );
1158
+ }
1159
+ }
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 emitVisibleEditableCodeGutter(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 emitCodeLineDecorations(builder, widget, widget.spans, tokens, content, widget.frame, layout_options, null, true);
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 emitCodeLineDecorations(builder, widget, spans, tokens, content, visible_bounds, layout_options, &budget, true);
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,72 +1604,535 @@ fn emitVisibleCodeTextSpansWidget(
1435
1604
  }
1436
1605
  }
1437
1606
 
1438
- /// Muted logical-line markers for one coherent code paragraph. Each
1439
- /// logical line is measured independently with the same monospace layout
1440
- /// options; summing those wrapped extents places the next marker on the
1441
- /// exact first visual line occupied by its source.
1442
- fn emitCodeLineNumberGutter(
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(
1443
1612
  builder: *Builder,
1444
1613
  widget: Widget,
1445
1614
  tokens: DesignTokens,
1446
- content: geometry.RectF,
1447
1615
  visible_bounds: geometry.RectF,
1448
- layout_options: text_spans_model.TextSpanLayoutOptions,
1449
- budget: ?*CodeEmissionBudget,
1616
+ paint: CodeTextPaint,
1450
1617
  ) Error!void {
1451
- if (widget.code_line_number_digits == 0) return;
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
+ );
1452
1629
  const line_height = text_spans_model.textSpanLineHeight(widget.spans, layout_options);
1453
1630
  if (line_height <= 0 or !std.math.isFinite(line_height)) return;
1454
1631
 
1455
- const padded = widget.frame.inset(widget.layout.padding);
1456
- const marker_width = @max(0, content.x - 12 - padded.x);
1457
- const digits = @min(@as(usize, widget.code_line_number_digits), 20);
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 emitCodeLineDecorations(
1638
+ builder,
1639
+ widget,
1640
+ widget.spans,
1641
+ tokens,
1642
+ gutter_content,
1643
+ visible_bounds,
1644
+ layout_options,
1645
+ &budget,
1646
+ true,
1647
+ );
1648
+ }
1649
+ if (!budget.hasCommand(builder) or budget.remainingText() == 0) return;
1650
+
1651
+ var state: code_model.HighlightState = .{};
1652
+ var span_storage: [text_spans_model.max_text_spans_per_paragraph]text_spans_model.TextSpan = undefined;
1458
1653
  var line_runs: [text_spans_model.max_text_span_runs_per_paragraph]text_spans_model.TextSpanRun = undefined;
1459
- var logical_line: usize = 1;
1460
- var visual_line: usize = 0;
1461
1654
  var line_start: usize = 0;
1655
+ var logical_line: usize = 0;
1656
+ var visual_line: usize = 0;
1657
+
1462
1658
  while (line_start <= widget.text.len) : (logical_line += 1) {
1463
- // A terminal newline closes the preceding painted line; the span
1464
- // breaker intentionally does not reserve another empty visual line.
1465
- if (line_start == widget.text.len and widget.text.len > 0) break;
1466
1659
  const newline = std.mem.indexOfScalarPos(u8, widget.text, line_start, '\n');
1467
- const line_end = newline orelse widget.text.len;
1468
- const baseline = content.y + layout_options.size +
1469
- @as(f32, @floatFromInt(visual_line)) * line_height;
1470
- const marker_bounds = geometry.RectF.init(
1471
- padded.x,
1472
- baseline - layout_options.size,
1473
- marker_width,
1474
- line_height,
1660
+ const line_end = if (newline) |index| index + 1 else widget.text.len;
1661
+ const line_source = widget.text[line_start..line_end];
1662
+ const highlighted = if (line_source.len == 0) blk: {
1663
+ span_storage[0] = .{
1664
+ .text = line_source,
1665
+ .monospace = true,
1666
+ .color = .syntax_plain,
1667
+ };
1668
+ break :blk span_storage[0..1];
1669
+ } else code_model.highlightWithState(
1670
+ line_source,
1671
+ widget.code_language,
1672
+ &span_storage,
1673
+ &state,
1475
1674
  );
1476
- if (marker_bounds.intersects(visible_bounds)) {
1477
- const marker_text = codeLineNumberText(logical_line, digits);
1478
- if (budget) |admission| {
1479
- if (!admission.hasCommand(builder)) return;
1480
- if (marker_text.len > admission.remainingText()) return;
1675
+ const line_layout = text_spans_model.layoutTextSpans(
1676
+ highlighted,
1677
+ layout_options,
1678
+ &line_runs,
1679
+ );
1680
+ const visual_count = @max(1, line_layout.line_count);
1681
+ const line_top = content.y + @as(f32, @floatFromInt(visual_line)) * line_height;
1682
+ const line_bottom = line_top + @as(f32, @floatFromInt(visual_count)) * line_height;
1683
+ if (line_top > visible_bounds.maxY()) return;
1684
+
1685
+ if (line_bottom >= visible_bounds.y) {
1686
+ const first_visible_line: usize = if (visible_bounds.y > line_top and
1687
+ std.math.isFinite(visible_bounds.y - line_top))
1688
+ @intFromFloat(@floor((visible_bounds.y - line_top) / line_height))
1689
+ else
1690
+ 0;
1691
+ const last_visible_line: usize = if (visible_bounds.maxY() > line_top and
1692
+ std.math.isFinite(visible_bounds.maxY() - line_top))
1693
+ @intFromFloat(@floor((visible_bounds.maxY() - line_top) / line_height))
1694
+ else
1695
+ first_visible_line;
1696
+ var page_first_line = first_visible_line -| 1;
1697
+ while (true) {
1698
+ const page_layout = if (page_first_line == 0)
1699
+ line_layout
1700
+ else
1701
+ text_spans_model.layoutTextSpansFromLine(
1702
+ highlighted,
1703
+ layout_options,
1704
+ page_first_line,
1705
+ &line_runs,
1706
+ );
1707
+ for (page_layout.runs) |run| {
1708
+ if (run.text.len == 0) continue;
1709
+ const local_bounds = text_spans_model.textSpanRunBounds(page_layout, run);
1710
+ const run_bounds = geometry.RectF.init(
1711
+ content.x + local_bounds.x,
1712
+ line_top + local_bounds.y,
1713
+ local_bounds.width,
1714
+ local_bounds.height,
1715
+ );
1716
+ if (!run_bounds.intersects(visible_bounds)) continue;
1717
+ const span = highlighted[run.span_index];
1718
+ const absolute_x = content.x + run.x;
1719
+ const visible = text_spans_model.textSpanRunVisibleSlice(
1720
+ span,
1721
+ run,
1722
+ layout_options,
1723
+ visible_bounds.x - absolute_x,
1724
+ visible_bounds.maxX() - absolute_x,
1725
+ ) orelse continue;
1726
+ if (!budget.hasCommand(builder)) return;
1727
+ const admitted_text = codeTextPrefix(visible.text, budget.remainingText());
1728
+ if (admitted_text.len == 0) return;
1729
+ const color_ref = span.color orelse .syntax_plain;
1730
+ try builder.drawText(.{
1731
+ .id = codeTextPaintCommandId(editableCodeRunCommandId(widget, logical_line, run), paint),
1732
+ .font_id = run.font_id,
1733
+ .size = run.size,
1734
+ .origin = pixelSnapTextPoint(tokens, geometry.PointF.init(
1735
+ absolute_x + visible.x,
1736
+ line_top + run.baseline,
1737
+ )),
1738
+ .color = paint.color orelse text_spans_model.textSpanColorValue(tokens.colors, color_ref),
1739
+ .text = admitted_text,
1740
+ .text_layout = .{
1741
+ .max_width = 0,
1742
+ .line_height = line_height,
1743
+ .wrap = .none,
1744
+ .alignment = .start,
1745
+ .measure = tokens.text_measure,
1746
+ },
1747
+ });
1748
+ budget.chargeText(admitted_text.len);
1749
+ }
1750
+
1751
+ const next_first_line = page_first_line +| text_spans_model.max_text_span_lines_per_paragraph;
1752
+ if (next_first_line <= page_first_line or
1753
+ next_first_line > last_visible_line or
1754
+ next_first_line >= line_layout.line_count)
1755
+ {
1756
+ break;
1757
+ }
1758
+ page_first_line = next_first_line;
1481
1759
  }
1760
+ }
1761
+
1762
+ visual_line += visual_count;
1763
+ if (newline == null) break;
1764
+ line_start = line_end;
1765
+ }
1766
+ }
1767
+
1768
+ /// Large no-wrap editors retain one plain source span, then tokenize just
1769
+ /// the logical lines intersecting the viewport. Lexer state is advanced
1770
+ /// across the offscreen prefix so multiline comments, strings, and JSX
1771
+ /// constructs remain correct without retaining an unbounded run list.
1772
+ fn emitVisibleEditableCodeLines(
1773
+ builder: *Builder,
1774
+ widget: Widget,
1775
+ tokens: DesignTokens,
1776
+ visible_bounds: geometry.RectF,
1777
+ paint: CodeTextPaint,
1778
+ ) Error!void {
1779
+ var content = widget_metrics.widgetTextSpanContentFrame(widget, tokens);
1780
+ content.x -= widget.value_x;
1781
+ content.y -= widget.value;
1782
+ const layout_options = widget_metrics.widgetTextSpanLayoutOptions(
1783
+ widget,
1784
+ tokens,
1785
+ 0,
1786
+ );
1787
+ const line_height = text_spans_model.textSpanLineHeight(widget.spans, layout_options);
1788
+ if (line_height <= 0 or !std.math.isFinite(line_height)) return;
1789
+
1790
+ const first_visible: usize = if (visible_bounds.y > content.y and
1791
+ std.math.isFinite(visible_bounds.y - content.y))
1792
+ @intFromFloat(@floor((visible_bounds.y - content.y) / line_height))
1793
+ else
1794
+ 0;
1795
+ const last_visible: usize = if (visible_bounds.maxY() > content.y and
1796
+ std.math.isFinite(visible_bounds.maxY() - content.y))
1797
+ @intFromFloat(@floor((visible_bounds.maxY() - content.y) / line_height))
1798
+ else
1799
+ first_visible;
1800
+ const first_line = first_visible -| 1;
1801
+ const last_line = last_visible +| 1;
1802
+
1803
+ var line_start: usize = 0;
1804
+ var logical_line: usize = 0;
1805
+ while (logical_line < first_line and line_start < widget.text.len) : (logical_line += 1) {
1806
+ const newline = std.mem.indexOfScalarPos(u8, widget.text, line_start, '\n') orelse {
1807
+ line_start = widget.text.len;
1808
+ break;
1809
+ };
1810
+ line_start = newline + 1;
1811
+ }
1812
+
1813
+ var state: code_model.HighlightState = .{};
1814
+ var span_storage: [text_spans_model.max_text_spans_per_paragraph]text_spans_model.TextSpan = undefined;
1815
+ if (line_start > 0) {
1816
+ _ = code_model.highlightWithState(
1817
+ widget.text[0..line_start],
1818
+ widget.code_language,
1819
+ &span_storage,
1820
+ &state,
1821
+ );
1822
+ }
1823
+
1824
+ var budget = CodeEmissionBudget.init(builder);
1825
+ if (!budget.hasCommand(builder)) return;
1826
+ if (paint.selection_ordinal == null) {
1827
+ try emitCodeLineDecorations(
1828
+ builder,
1829
+ widget,
1830
+ widget.spans,
1831
+ tokens,
1832
+ content,
1833
+ visible_bounds,
1834
+ layout_options,
1835
+ &budget,
1836
+ false,
1837
+ );
1838
+ }
1839
+ var line_runs: [text_spans_model.max_text_span_runs_per_paragraph]text_spans_model.TextSpanRun = undefined;
1840
+
1841
+ while (line_start <= widget.text.len and logical_line <= last_line) : (logical_line += 1) {
1842
+ const newline = std.mem.indexOfScalarPos(u8, widget.text, line_start, '\n');
1843
+ const line_end = if (newline) |index| index + 1 else widget.text.len;
1844
+ const line_source = widget.text[line_start..line_end];
1845
+ const highlighted = if (line_source.len == 0) blk: {
1846
+ span_storage[0] = .{
1847
+ .text = line_source,
1848
+ .monospace = true,
1849
+ .color = .syntax_plain,
1850
+ };
1851
+ break :blk span_storage[0..1];
1852
+ } else code_model.highlightWithState(
1853
+ line_source,
1854
+ widget.code_language,
1855
+ &span_storage,
1856
+ &state,
1857
+ );
1858
+ const line_layout = text_spans_model.layoutTextSpans(
1859
+ highlighted,
1860
+ layout_options,
1861
+ &line_runs,
1862
+ );
1863
+ const line_top = content.y + @as(f32, @floatFromInt(logical_line)) * line_height;
1864
+
1865
+ for (line_layout.runs) |run| {
1866
+ if (run.text.len == 0) continue;
1867
+ const local_bounds = text_spans_model.textSpanRunBounds(line_layout, run);
1868
+ const run_bounds = geometry.RectF.init(
1869
+ content.x + local_bounds.x,
1870
+ line_top + local_bounds.y,
1871
+ local_bounds.width,
1872
+ local_bounds.height,
1873
+ );
1874
+ if (!run_bounds.intersects(visible_bounds)) continue;
1875
+ const span = highlighted[run.span_index];
1876
+ const absolute_x = content.x + run.x;
1877
+ const visible = text_spans_model.textSpanRunVisibleSlice(
1878
+ span,
1879
+ run,
1880
+ layout_options,
1881
+ visible_bounds.x - absolute_x,
1882
+ visible_bounds.maxX() - absolute_x,
1883
+ ) orelse continue;
1884
+ if (!budget.hasCommand(builder)) return;
1885
+ const admitted_text = codeTextPrefix(visible.text, budget.remainingText());
1886
+ if (admitted_text.len == 0) return;
1887
+ const color_ref = span.color orelse .syntax_plain;
1482
1888
  try builder.drawText(.{
1483
- .id = codeLineNumberCommandId(widget.id, logical_line),
1484
- .font_id = tokens.typography.mono_font_id,
1485
- .size = layout_options.size,
1486
- .origin = pixelSnapTextPoint(tokens, geometry.PointF.init(padded.x, baseline)),
1487
- .color = tokens.colors.text_muted,
1488
- .text = marker_text,
1889
+ .id = codeTextPaintCommandId(editableCodeRunCommandId(widget, logical_line, run), paint),
1890
+ .font_id = run.font_id,
1891
+ .size = run.size,
1892
+ .origin = pixelSnapTextPoint(tokens, geometry.PointF.init(
1893
+ absolute_x + visible.x,
1894
+ line_top + run.baseline,
1895
+ )),
1896
+ .color = paint.color orelse text_spans_model.textSpanColorValue(tokens.colors, color_ref),
1897
+ .text = admitted_text,
1489
1898
  .text_layout = .{
1490
- .max_width = marker_width,
1899
+ .max_width = 0,
1491
1900
  .line_height = line_height,
1492
1901
  .wrap = .none,
1493
- .alignment = .end,
1902
+ .alignment = .start,
1494
1903
  .measure = tokens.text_measure,
1495
1904
  },
1496
1905
  });
1497
- if (budget) |admission| admission.chargeText(marker_text.len);
1906
+ budget.chargeText(admitted_text.len);
1907
+ }
1908
+
1909
+ if (newline == null) break;
1910
+ line_start = line_end;
1911
+ }
1912
+ }
1913
+
1914
+ /// The editable no-wrap code path scrolls source glyphs beneath a pinned
1915
+ /// number/diff-marker gutter. Paint that gutter last so source, selection,
1916
+ /// IME decoration, and the caret all disappear cleanly behind its opaque
1917
+ /// surface instead of clashing with the anchored decorations.
1918
+ fn emitVisibleEditableCodeGutter(
1919
+ builder: *Builder,
1920
+ widget: Widget,
1921
+ tokens: DesignTokens,
1922
+ visible_bounds: geometry.RectF,
1923
+ active_row: ?geometry.RectF,
1924
+ ) Error!void {
1925
+ const has_diff = widget.hasCodeDiff();
1926
+ const line_number_digits = widget.codeLineNumberDigits();
1927
+ if (line_number_digits == 0 and !has_diff) return;
1928
+
1929
+ var content = widget_metrics.widgetTextSpanContentFrame(widget, tokens);
1930
+ content.y -= widget.value;
1931
+ const layout_options = widget_metrics.widgetTextSpanLayoutOptions(widget, tokens, 0);
1932
+ const line_height = text_spans_model.textSpanLineHeight(widget.spans, layout_options);
1933
+ if (line_height <= 0 or !std.math.isFinite(line_height)) return;
1934
+
1935
+ const padded = widget.frame.inset(widget.layout.padding);
1936
+ const marker_width = @max(0, content.x - 12 - padded.x);
1937
+ const gutter_bounds = geometry.RectF.intersection(
1938
+ geometry.RectF.init(
1939
+ widget.frame.x,
1940
+ widget.frame.y,
1941
+ @max(0, content.x - widget.frame.x),
1942
+ widget.frame.height,
1943
+ ),
1944
+ visible_bounds,
1945
+ );
1946
+ if (!gutter_bounds.isEmpty()) {
1947
+ try builder.fillRect(.{
1948
+ .id = codeLineNumberGutterCommandId(widget.id),
1949
+ .rect = pixelSnapGeometryRect(tokens, gutter_bounds),
1950
+ .fill = colorFill(widgetBackgroundColor(widget, tokens.colors.background)),
1951
+ });
1952
+ }
1953
+ if (active_row) |row| {
1954
+ const active_gutter = geometry.RectF.intersection(gutter_bounds, row);
1955
+ if (!active_gutter.isEmpty()) {
1956
+ try builder.fillRect(.{
1957
+ .id = codeEditorActiveRowCommandId(widget.id, 1),
1958
+ .rect = pixelSnapGeometryRect(tokens, active_gutter),
1959
+ .fill = colorFill(tokens.colors.surface_subtle),
1960
+ });
1961
+ }
1962
+ }
1963
+
1964
+ const first_visible: usize = if (visible_bounds.y > content.y and
1965
+ std.math.isFinite(visible_bounds.y - content.y))
1966
+ @intFromFloat(@floor((visible_bounds.y - content.y) / line_height))
1967
+ else
1968
+ 0;
1969
+ const last_visible: usize = if (visible_bounds.maxY() > content.y and
1970
+ std.math.isFinite(visible_bounds.maxY() - content.y))
1971
+ @intFromFloat(@floor((visible_bounds.maxY() - content.y) / line_height))
1972
+ else
1973
+ first_visible;
1974
+ const first_line = first_visible -| 1;
1975
+ const last_line = last_visible +| 1;
1976
+
1977
+ var line_start: usize = 0;
1978
+ var logical_line: usize = 0;
1979
+ while (logical_line < first_line and line_start < widget.text.len) : (logical_line += 1) {
1980
+ const newline = std.mem.indexOfScalarPos(u8, widget.text, line_start, '\n') orelse {
1981
+ line_start = widget.text.len;
1982
+ break;
1983
+ };
1984
+ line_start = newline + 1;
1985
+ }
1986
+
1987
+ while (line_start <= widget.text.len and logical_line <= last_line) : (logical_line += 1) {
1988
+ const line_top = content.y + @as(f32, @floatFromInt(logical_line)) * line_height;
1989
+ const marker_bounds = geometry.RectF.init(padded.x, line_top, marker_width, line_height);
1990
+ if (marker_bounds.intersects(visible_bounds)) {
1991
+ const one_based_line = logical_line + 1;
1992
+ if (codeDiffKind(widget, one_based_line)) |kind| {
1993
+ const diff_gutter = geometry.RectF.intersection(
1994
+ gutter_bounds,
1995
+ geometry.RectF.init(gutter_bounds.x, line_top, gutter_bounds.width, line_height),
1996
+ );
1997
+ if (!diff_gutter.isEmpty()) {
1998
+ try builder.fillRect(.{
1999
+ .id = codeDiffGutterBackgroundCommandId(widget.id, one_based_line),
2000
+ .rect = pixelSnapGeometryRect(tokens, diff_gutter),
2001
+ .fill = colorFill(codeDiffBackground(tokens, kind)),
2002
+ });
2003
+ }
2004
+ const marker = switch (kind) {
2005
+ .added => "+",
2006
+ .removed => "-",
2007
+ };
2008
+ try builder.drawText(.{
2009
+ .id = codeDiffMarkerCommandId(widget.id, one_based_line),
2010
+ .font_id = tokens.typography.mono_font_id,
2011
+ .size = layout_options.size,
2012
+ .origin = pixelSnapTextPoint(tokens, geometry.PointF.init(
2013
+ padded.x,
2014
+ line_top + layout_options.size,
2015
+ )),
2016
+ .color = codeDiffForeground(tokens, kind),
2017
+ .text = marker,
2018
+ .text_layout = .{
2019
+ .max_width = marker_width,
2020
+ .line_height = line_height,
2021
+ .wrap = .none,
2022
+ .alignment = .start,
2023
+ .measure = tokens.text_measure,
2024
+ },
2025
+ });
2026
+ }
2027
+ if (line_number_digits != 0) {
2028
+ var marker_buffer: [20]u8 = undefined;
2029
+ const marker_formatted = std.fmt.bufPrint(
2030
+ &marker_buffer,
2031
+ "{d}",
2032
+ .{one_based_line},
2033
+ ) catch "";
2034
+ const marker_text = builder.allocTextBytes(marker_formatted) catch "";
2035
+ if (marker_text.len > 0) {
2036
+ try builder.drawText(.{
2037
+ .id = codeLineNumberCommandId(widget.id, one_based_line),
2038
+ .font_id = tokens.typography.mono_font_id,
2039
+ .size = layout_options.size,
2040
+ .origin = pixelSnapTextPoint(tokens, geometry.PointF.init(
2041
+ padded.x,
2042
+ line_top + layout_options.size,
2043
+ )),
2044
+ .color = tokens.colors.text_muted,
2045
+ .text = marker_text,
2046
+ .text_layout = .{
2047
+ .max_width = marker_width,
2048
+ .line_height = line_height,
2049
+ .wrap = .none,
2050
+ .alignment = .end,
2051
+ .measure = tokens.text_measure,
2052
+ },
2053
+ });
2054
+ }
2055
+ }
2056
+ }
2057
+
2058
+ const newline = std.mem.indexOfScalarPos(u8, widget.text, line_start, '\n') orelse break;
2059
+ line_start = newline + 1;
2060
+ }
2061
+ }
2062
+
2063
+ const CodeDiffKind = enum { added, removed };
2064
+
2065
+ fn codeDiffKind(widget: Widget, logical_line: usize) ?CodeDiffKind {
2066
+ if (logical_line == 0 or logical_line > code_model.max_diff_lines) return null;
2067
+ const lines = widget.codeDiffLines() orelse return null;
2068
+ const shift: u7 = @intCast(logical_line - 1);
2069
+ const bit = @as(u128, 1) << shift;
2070
+ if (lines.added & bit != 0) return .added;
2071
+ if (lines.removed & bit != 0) return .removed;
2072
+ return null;
2073
+ }
2074
+
2075
+ fn codeDiffBackground(tokens: DesignTokens, kind: CodeDiffKind) Color {
2076
+ // Geist publishes these as component states rather than general-purpose
2077
+ // surface roles. Both built-in packs share the exact pair; background
2078
+ // luminance keeps custom light/dark palettes on the matching register.
2079
+ const background = tokens.colors.background;
2080
+ const dark = background.r * 0.2126 + background.g * 0.7152 + background.b * 0.0722 < 0.5;
2081
+ return if (dark)
2082
+ switch (kind) {
2083
+ .added => Color.rgb8(18, 54, 27),
2084
+ .removed => Color.rgb8(86, 26, 30),
1498
2085
  }
2086
+ else switch (kind) {
2087
+ .added => Color.rgb8(218, 246, 218),
2088
+ .removed => Color.rgb8(255, 230, 230),
2089
+ };
2090
+ }
2091
+
2092
+ fn codeDiffForeground(tokens: DesignTokens, kind: CodeDiffKind) Color {
2093
+ return canvas.colorTokenValue(tokens.colors, switch (kind) {
2094
+ .added => .syntax_literal,
2095
+ .removed => .syntax_property,
2096
+ });
2097
+ }
2098
+
2099
+ /// Geist-style diff washes/markers plus muted logical-line numbers for one
2100
+ /// coherent code paragraph. Each logical line is measured independently
2101
+ /// with the same monospace layout options; summing wrapped extents keeps the
2102
+ /// next marker paired with its source. `draw_gutter=false` is the first pass
2103
+ /// of a horizontally scrolling editor: it paints the wash before source,
2104
+ /// while the pinned gutter redraws its marker and number last.
2105
+ fn emitCodeLineDecorations(
2106
+ builder: *Builder,
2107
+ widget: Widget,
2108
+ spans: []const text_spans_model.TextSpan,
2109
+ tokens: DesignTokens,
2110
+ content: geometry.RectF,
2111
+ visible_bounds: geometry.RectF,
2112
+ layout_options: text_spans_model.TextSpanLayoutOptions,
2113
+ budget: ?*CodeEmissionBudget,
2114
+ draw_gutter: bool,
2115
+ ) Error!void {
2116
+ const has_diff = widget.hasCodeDiff();
2117
+ const line_number_digits = widget.codeLineNumberDigits();
2118
+ if (!has_diff and (line_number_digits == 0 or !draw_gutter)) return;
2119
+ const line_height = text_spans_model.textSpanLineHeight(spans, layout_options);
2120
+ if (line_height <= 0 or !std.math.isFinite(line_height)) return;
1499
2121
 
2122
+ const padded = widget.frame.inset(widget.layout.padding);
2123
+ const marker_width = @max(0, content.x - 12 - padded.x);
2124
+ var line_runs: [text_spans_model.max_text_span_runs_per_paragraph]text_spans_model.TextSpanRun = undefined;
2125
+ var logical_line: usize = 1;
2126
+ var visual_line: usize = 0;
2127
+ var line_start: usize = 0;
2128
+ while (line_start <= widget.text.len) : (logical_line += 1) {
2129
+ // A terminal newline closes the preceding painted line; the span
2130
+ // breaker intentionally does not reserve another empty visual line.
2131
+ if (line_start == widget.text.len and widget.text.len > 0 and !widget.code_editor) break;
2132
+ const newline = std.mem.indexOfScalarPos(u8, widget.text, line_start, '\n');
2133
+ const line_end = newline orelse widget.text.len;
1500
2134
  const line = widget.text[line_start..line_end];
1501
- if (line.len == 0) {
1502
- visual_line += 1;
1503
- } else {
2135
+ const visual_count = if (line.len == 0) 1 else blk: {
1504
2136
  const line_spans = [_]text_spans_model.TextSpan{.{
1505
2137
  .text = line,
1506
2138
  .monospace = true,
@@ -1511,41 +2143,160 @@ fn emitCodeLineNumberGutter(
1511
2143
  layout_options,
1512
2144
  &line_runs,
1513
2145
  );
1514
- visual_line += @max(1, line_layout.line_count);
2146
+ break :blk @max(1, line_layout.line_count);
2147
+ };
2148
+ const baseline = content.y + layout_options.size +
2149
+ @as(f32, @floatFromInt(visual_line)) * line_height;
2150
+ const row_bounds = geometry.RectF.init(
2151
+ widget.frame.x,
2152
+ baseline - layout_options.size,
2153
+ widget.frame.width,
2154
+ @as(f32, @floatFromInt(visual_count)) * line_height,
2155
+ );
2156
+ const diff_kind = codeDiffKind(widget, logical_line);
2157
+ if (diff_kind) |kind| {
2158
+ const visible_row = geometry.RectF.intersection(row_bounds, visible_bounds);
2159
+ if (!visible_row.isEmpty()) {
2160
+ if (budget) |admission| if (!admission.hasCommand(builder)) return;
2161
+ try builder.fillRect(.{
2162
+ .id = codeDiffBackgroundCommandId(widget.id, logical_line),
2163
+ .rect = pixelSnapGeometryRect(tokens, visible_row),
2164
+ .fill = colorFill(codeDiffBackground(tokens, kind)),
2165
+ });
2166
+ }
1515
2167
  }
2168
+ const marker_bounds = geometry.RectF.init(
2169
+ padded.x,
2170
+ baseline - layout_options.size,
2171
+ marker_width,
2172
+ line_height,
2173
+ );
2174
+ if (draw_gutter and marker_bounds.intersects(visible_bounds)) {
2175
+ if (diff_kind) |kind| {
2176
+ if (budget) |admission| {
2177
+ if (!admission.hasCommand(builder) or admission.remainingText() == 0) return;
2178
+ }
2179
+ const marker = switch (kind) {
2180
+ .added => "+",
2181
+ .removed => "-",
2182
+ };
2183
+ try builder.drawText(.{
2184
+ .id = codeDiffMarkerCommandId(widget.id, logical_line),
2185
+ .font_id = tokens.typography.mono_font_id,
2186
+ .size = layout_options.size,
2187
+ .origin = pixelSnapTextPoint(tokens, geometry.PointF.init(padded.x, baseline)),
2188
+ .color = codeDiffForeground(tokens, kind),
2189
+ .text = marker,
2190
+ .text_layout = .{
2191
+ .max_width = marker_width,
2192
+ .line_height = line_height,
2193
+ .wrap = .none,
2194
+ .alignment = .start,
2195
+ .measure = tokens.text_measure,
2196
+ },
2197
+ });
2198
+ if (budget) |admission| admission.chargeText(marker.len);
2199
+ }
2200
+ if (line_number_digits != 0) {
2201
+ var marker_buffer: [20]u8 = undefined;
2202
+ const marker_formatted = std.fmt.bufPrint(
2203
+ &marker_buffer,
2204
+ "{d}",
2205
+ .{logical_line},
2206
+ ) catch "";
2207
+ const marker_text = builder.allocTextBytes(marker_formatted) catch "";
2208
+ if (budget) |admission| {
2209
+ if (!admission.hasCommand(builder)) return;
2210
+ if (marker_text.len > admission.remainingText()) return;
2211
+ }
2212
+ try builder.drawText(.{
2213
+ .id = codeLineNumberCommandId(widget.id, logical_line),
2214
+ .font_id = tokens.typography.mono_font_id,
2215
+ .size = layout_options.size,
2216
+ .origin = pixelSnapTextPoint(tokens, geometry.PointF.init(padded.x, baseline)),
2217
+ .color = tokens.colors.text_muted,
2218
+ .text = marker_text,
2219
+ .text_layout = .{
2220
+ .max_width = marker_width,
2221
+ .line_height = line_height,
2222
+ .wrap = .none,
2223
+ .alignment = .end,
2224
+ .measure = tokens.text_measure,
2225
+ },
2226
+ });
2227
+ if (budget) |admission| admission.chargeText(marker_text.len);
2228
+ }
2229
+ }
2230
+ visual_line += visual_count;
1516
2231
  if (newline == null) break;
1517
2232
  line_start = line_end + 1;
1518
2233
  }
1519
2234
  }
1520
2235
 
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
- }
2236
+ fn codeTextSpanRunCommandId(widget: Widget, spans: []const text_spans_model.TextSpan, run: text_spans_model.TextSpanRun) ObjectId {
2237
+ if (text_spans_model.textSpanRunParagraphRange(widget.text, run)) |range| {
2238
+ var hasher = std.hash.Wyhash.init(0x5eed_59a2_0000_0011);
2239
+ hasher.update(std.mem.asBytes(&widget.id));
2240
+ hasher.update(std.mem.asBytes(&run.line_index));
2241
+ hasher.update(std.mem.asBytes(&range.start));
2242
+ const value = hasher.final();
2243
+ return if (value == 0) 1 else value;
1531
2244
  }
1532
- break :blk values;
1533
- };
1534
2245
 
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 ..];
2246
+ // An editable widget's runtime-owned text allocation can briefly be
2247
+ // distinct from the source allocation its freshly highlighted spans
2248
+ // reference. Paragraph-relative pointer arithmetic is unavailable in
2249
+ // that state, but every layout run still belongs to one span. Key the
2250
+ // fallback by that span and the run's offset within it; collapsing all
2251
+ // detached runs to byte zero gives every token on a line the same id
2252
+ // and makes the retained display-list diff reject the frame.
2253
+ var hasher = std.hash.Wyhash.init(0x5eed_59a2_0000_0013);
2254
+ hasher.update(std.mem.asBytes(&widget.id));
2255
+ hasher.update(std.mem.asBytes(&run.line_index));
2256
+ hasher.update(std.mem.asBytes(&run.span_index));
2257
+ if (run.span_index < spans.len) {
2258
+ const span = spans[run.span_index];
2259
+ const span_start = @intFromPtr(span.text.ptr);
2260
+ const run_start = @intFromPtr(run.text.ptr);
2261
+ if (run_start >= span_start and run_start - span_start + run.text.len <= span.text.len) {
2262
+ const span_offset = run_start - span_start;
2263
+ hasher.update(std.mem.asBytes(&span_offset));
2264
+ const value = hasher.final();
2265
+ return if (value == 0) 1 else value;
2266
+ }
2267
+ }
2268
+ hasher.update(std.mem.asBytes(&run.x));
2269
+ hasher.update(std.mem.asBytes(&run.text.len));
2270
+ hasher.update(run.text);
2271
+ const value = hasher.final();
2272
+ return if (value == 0) 1 else value;
1540
2273
  }
1541
2274
 
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);
2275
+ fn editableCodeRunCommandId(
2276
+ widget: Widget,
2277
+ logical_line: usize,
2278
+ run: text_spans_model.TextSpanRun,
2279
+ ) ObjectId {
2280
+ var hasher = std.hash.Wyhash.init(0x5eed_59a2_0000_0014);
1546
2281
  hasher.update(std.mem.asBytes(&widget.id));
1547
- hasher.update(std.mem.asBytes(&run.line_index));
1548
- hasher.update(std.mem.asBytes(&range.start));
2282
+ hasher.update(std.mem.asBytes(&logical_line));
2283
+ const text_start = @intFromPtr(widget.text.ptr);
2284
+ const run_start = @intFromPtr(run.text.ptr);
2285
+ if (run_start >= text_start and run_start - text_start <= widget.text.len) {
2286
+ const offset = run_start - text_start;
2287
+ hasher.update(std.mem.asBytes(&offset));
2288
+ } else {
2289
+ hasher.update(run.text);
2290
+ }
2291
+ const value = hasher.final();
2292
+ return if (value == 0) 1 else value;
2293
+ }
2294
+
2295
+ fn codeTextPaintCommandId(base_id: ObjectId, paint: CodeTextPaint) ObjectId {
2296
+ const ordinal = paint.selection_ordinal orelse return base_id;
2297
+ var hasher = std.hash.Wyhash.init(0x5eed_59a2_0000_0018);
2298
+ hasher.update(std.mem.asBytes(&base_id));
2299
+ hasher.update(std.mem.asBytes(&ordinal));
1549
2300
  const value = hasher.final();
1550
2301
  return if (value == 0) 1 else value;
1551
2302
  }
@@ -1554,6 +2305,26 @@ fn codeLineNumberCommandId(widget_id: ObjectId, logical_line: usize) ObjectId {
1554
2305
  return textSpanCommandId(0x5eed_59a2_0000_0012, widget_id, logical_line);
1555
2306
  }
1556
2307
 
2308
+ fn codeLineNumberGutterCommandId(widget_id: ObjectId) ObjectId {
2309
+ return textSpanCommandId(0x5eed_59a2_0000_0015, widget_id, 0);
2310
+ }
2311
+
2312
+ fn codeDiffBackgroundCommandId(widget_id: ObjectId, logical_line: usize) ObjectId {
2313
+ return textSpanCommandId(0x5eed_59a2_0000_0019, widget_id, logical_line);
2314
+ }
2315
+
2316
+ fn codeDiffGutterBackgroundCommandId(widget_id: ObjectId, logical_line: usize) ObjectId {
2317
+ return textSpanCommandId(0x5eed_59a2_0000_001b, widget_id, logical_line);
2318
+ }
2319
+
2320
+ fn codeDiffMarkerCommandId(widget_id: ObjectId, logical_line: usize) ObjectId {
2321
+ return textSpanCommandId(0x5eed_59a2_0000_001a, widget_id, logical_line);
2322
+ }
2323
+
2324
+ fn codeEditorActiveRowCommandId(widget_id: ObjectId, ordinal: usize) ObjectId {
2325
+ return textSpanCommandId(0x5eed_59a2_0000_0016, widget_id, ordinal);
2326
+ }
2327
+
1557
2328
  pub fn textSpanRunCommandId(widget_id: ObjectId, ordinal: usize) ObjectId {
1558
2329
  return textSpanCommandId(0x5eed_59a2_0000_0001, widget_id, ordinal);
1559
2330
  }