@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
@@ -18,6 +18,7 @@ pub const Language = enum {
18
18
  javascript,
19
19
  typescript,
20
20
  json,
21
+ yaml,
21
22
  shell,
22
23
  python,
23
24
  rust,
@@ -26,6 +27,9 @@ pub const Language = enum {
26
27
  html,
27
28
  css,
28
29
  sql,
30
+ jsx,
31
+ tsx,
32
+ markdown,
29
33
  };
30
34
 
31
35
  /// Lexer state carried between bounded source chunks by `Ui.code`.
@@ -45,7 +49,14 @@ pub const HighlightState = struct {
45
49
  /// of ending it.
46
50
  html_tag_context_bases: [max_html_tag_contexts]usize = [_]usize{0} ** max_html_tag_contexts,
47
51
  html_tag_context_expect_names: [max_html_tag_contexts]bool = [_]bool{false} ** max_html_tag_contexts,
52
+ html_tag_context_opened_elements: [max_html_tag_contexts]bool = [_]bool{false} ** max_html_tag_contexts,
48
53
  html_tag_context_len: usize = 0,
54
+ /// Expression depth at which each currently open element began.
55
+ /// A closing tag is structural at that same depth even when ordinary
56
+ /// JSX text immediately before it ends in an identifier.
57
+ html_element_expression_bases: [max_html_tag_contexts]usize = [_]usize{0} ** max_html_tag_contexts,
58
+ html_element_len: usize = 0,
59
+ html_tag_opened_element: bool = false,
49
60
  /// Last non-whitespace source byte from the preceding presentation
50
61
  /// chunk. JSX comparison/tag disambiguation needs its left context even
51
62
  /// when a bounded paragraph happens to split immediately before `<`.
@@ -55,6 +66,10 @@ pub const HighlightState = struct {
55
66
  line_comment: bool = false,
56
67
  preprocessor_line: bool = false,
57
68
  string_quote: ?u8 = null,
69
+ markdown_line_start: bool = true,
70
+ markdown_fence_byte: u8 = 0,
71
+ markdown_fence_len: u8 = 0,
72
+ markdown_inline_code_len: u8 = 0,
58
73
  };
59
74
 
60
75
  fn pushHtmlTagContext(state: *HighlightState) void {
@@ -62,6 +77,7 @@ fn pushHtmlTagContext(state: *HighlightState) void {
62
77
  const index = state.html_tag_context_len;
63
78
  state.html_tag_context_bases[index] = state.html_tag_expression_base;
64
79
  state.html_tag_context_expect_names[index] = state.html_expect_tag_name;
80
+ state.html_tag_context_opened_elements[index] = state.html_tag_opened_element;
65
81
  state.html_tag_context_len += 1;
66
82
  }
67
83
 
@@ -72,6 +88,7 @@ fn restoreHtmlTagContext(state: *HighlightState) bool {
72
88
  state.html_in_tag = true;
73
89
  state.html_tag_expression_base = state.html_tag_context_bases[index];
74
90
  state.html_expect_tag_name = state.html_tag_context_expect_names[index];
91
+ state.html_tag_opened_element = state.html_tag_context_opened_elements[index];
75
92
  return true;
76
93
  }
77
94
 
@@ -80,10 +97,14 @@ fn restoreHtmlTagContext(state: *HighlightState) bool {
80
97
  pub fn languageFromName(name_raw: []const u8) Language {
81
98
  const name = std.mem.trim(u8, name_raw, " \t\r\n");
82
99
  if (std.ascii.eqlIgnoreCase(name, "zig")) return .zig;
83
- if (std.ascii.eqlIgnoreCase(name, "jsx") or std.ascii.eqlIgnoreCase(name, "tsx")) return .html;
84
- if (std.ascii.eqlIgnoreCase(name, "js") or std.ascii.eqlIgnoreCase(name, "javascript")) return .javascript;
100
+ if (std.ascii.eqlIgnoreCase(name, "jsx")) return .jsx;
101
+ if (std.ascii.eqlIgnoreCase(name, "tsx")) return .tsx;
102
+ if (std.ascii.eqlIgnoreCase(name, "js") or
103
+ std.ascii.eqlIgnoreCase(name, "mjs") or
104
+ std.ascii.eqlIgnoreCase(name, "javascript")) return .javascript;
85
105
  if (std.ascii.eqlIgnoreCase(name, "ts") or std.ascii.eqlIgnoreCase(name, "typescript")) return .typescript;
86
106
  if (std.ascii.eqlIgnoreCase(name, "json") or std.ascii.eqlIgnoreCase(name, "jsonc")) return .json;
107
+ if (std.ascii.eqlIgnoreCase(name, "yaml") or std.ascii.eqlIgnoreCase(name, "yml")) return .yaml;
87
108
  if (std.ascii.eqlIgnoreCase(name, "sh") or std.ascii.eqlIgnoreCase(name, "bash") or std.ascii.eqlIgnoreCase(name, "zsh") or std.ascii.eqlIgnoreCase(name, "shell")) return .shell;
88
109
  if (std.ascii.eqlIgnoreCase(name, "py") or std.ascii.eqlIgnoreCase(name, "python")) return .python;
89
110
  if (std.ascii.eqlIgnoreCase(name, "rs") or std.ascii.eqlIgnoreCase(name, "rust")) return .rust;
@@ -99,6 +120,7 @@ pub fn languageFromName(name_raw: []const u8) Language {
99
120
  if (std.ascii.eqlIgnoreCase(name, "html") or std.ascii.eqlIgnoreCase(name, "xml") or std.ascii.eqlIgnoreCase(name, "svg")) return .html;
100
121
  if (std.ascii.eqlIgnoreCase(name, "css") or std.ascii.eqlIgnoreCase(name, "scss") or std.ascii.eqlIgnoreCase(name, "less")) return .css;
101
122
  if (std.ascii.eqlIgnoreCase(name, "sql")) return .sql;
123
+ if (std.ascii.eqlIgnoreCase(name, "md") or std.ascii.eqlIgnoreCase(name, "markdown")) return .markdown;
102
124
  return .plain;
103
125
  }
104
126
 
@@ -123,6 +145,56 @@ pub fn languageFromFence(opening: []const u8) Language {
123
145
  return languageFromName(info[0..end]);
124
146
  }
125
147
 
148
+ /// Diff annotations share the read-only code paragraph's bounded logical
149
+ /// line model. A fixed ceiling keeps line metadata inline on the widget and
150
+ /// makes malformed markup fail before it can silently decorate the wrong
151
+ /// source row.
152
+ pub const max_diff_lines: usize = 128;
153
+
154
+ /// Parse a one-based comma/range list such as `2-4, 7`. Results are unique
155
+ /// and retain author order. Empty text means no annotated lines; zero,
156
+ /// descending ranges, values above `max_diff_lines`, and malformed pieces
157
+ /// are rejected.
158
+ pub fn parseLineNumberSpec(spec_raw: []const u8, storage: *[max_diff_lines]usize) ?[]const usize {
159
+ const spec = std.mem.trim(u8, spec_raw, " \t\r\n");
160
+ if (spec.len == 0) return storage[0..0];
161
+
162
+ var len: usize = 0;
163
+ var pieces = std.mem.splitScalar(u8, spec, ',');
164
+ while (pieces.next()) |piece_raw| {
165
+ const piece = std.mem.trim(u8, piece_raw, " \t\r\n");
166
+ if (piece.len == 0) return null;
167
+
168
+ const dash = std.mem.indexOfScalar(u8, piece, '-');
169
+ const first_text = std.mem.trim(u8, if (dash) |index| piece[0..index] else piece, " \t");
170
+ const last_text = if (dash) |index| std.mem.trim(u8, piece[index + 1 ..], " \t") else first_text;
171
+ if (first_text.len == 0 or last_text.len == 0) return null;
172
+ if (dash) |index| {
173
+ if (std.mem.indexOfScalar(u8, piece[index + 1 ..], '-') != null) return null;
174
+ }
175
+
176
+ const first = std.fmt.parseInt(usize, first_text, 10) catch return null;
177
+ const last = std.fmt.parseInt(usize, last_text, 10) catch return null;
178
+ if (first == 0 or last < first or last > max_diff_lines) return null;
179
+
180
+ var line = first;
181
+ while (line <= last) : (line += 1) {
182
+ var duplicate = false;
183
+ for (storage[0..len]) |existing| {
184
+ if (existing == line) {
185
+ duplicate = true;
186
+ break;
187
+ }
188
+ }
189
+ if (duplicate) continue;
190
+ if (len == storage.len) return null;
191
+ storage[len] = line;
192
+ len += 1;
193
+ }
194
+ }
195
+ return storage[0..len];
196
+ }
197
+
126
198
  fn wordInList(word: []const u8, list: []const u8, ignore_case: bool) bool {
127
199
  var words = std.mem.tokenizeScalar(u8, list, ' ');
128
200
  while (words.next()) |candidate| {
@@ -166,14 +238,16 @@ fn wordColor(language: Language, word: []const u8) ?text_spans.TextSpanColor {
166
238
  if (word.len > 0 and word[0] == '@') return .syntax_function;
167
239
  if (language == .zig) return zig_words.get(word);
168
240
  if (language == .python and wordInList(word, "True False None", false)) return .syntax_literal;
241
+ if (language == .yaml and wordInList(word, "true false null yes no on off", true)) return .syntax_literal;
169
242
  if (wordInList(word, "true false null nil none undefined this self super", language == .sql)) return .syntax_literal;
170
243
 
171
244
  const keywords = switch (language) {
172
245
  .plain => return null,
173
246
  .zig => unreachable,
174
- .javascript => "async await break case catch class const continue debugger default delete do else export extends finally for from function get if import in instanceof let new of return set static switch throw try typeof var void while with yield",
175
- .typescript => "abstract any as asserts async await bigint boolean break case catch class const constructor continue declare default delete do else enum export extends finally for from function get if implements import in infer interface instanceof is keyof let module namespace never new number object of override private protected public readonly require return satisfies set static string super switch symbol this throw try type typeof undefined unique unknown var void while with yield",
247
+ .javascript, .jsx => "async await break case catch class const continue debugger default delete do else export extends finally for from function get if import in instanceof let new of return set static switch throw try typeof var void while with yield",
248
+ .typescript, .tsx => "abstract any as asserts async await bigint boolean break case catch class const constructor continue declare default delete do else enum export extends finally for from function get if implements import in infer interface instanceof is keyof let module namespace never new number object of override private protected public readonly require return satisfies set static string super switch symbol this throw try type typeof undefined unique unknown var void while with yield",
176
249
  .json => "",
250
+ .yaml => "",
177
251
  .shell => "case coproc do done elif else esac fi for function if in select then time until while",
178
252
  .python => "and as assert async await break case class continue def del elif else except finally for from global if import in is lambda match nonlocal not or pass raise return try while with yield",
179
253
  .rust => "as async await break const continue crate dyn else enum extern fn for if impl in let loop match mod move mut pub ref return self Self static struct super trait type union unsafe use where while",
@@ -182,6 +256,7 @@ fn wordColor(language: Language, word: []const u8) ?text_spans.TextSpanColor {
182
256
  .html => "",
183
257
  .css => "and important inherit initial none not only or revert unset",
184
258
  .sql => "add all alter and any as asc begin between by case check column commit constraint create cross database default delete desc distinct drop else end exists foreign from full grant group having in index inner insert intersect into is join key left like limit not null on or order outer primary references right rollback row select set table then union unique update values view when where with",
259
+ .markdown => "",
185
260
  };
186
261
  if (wordInList(word, keywords, language == .sql)) return .syntax_keyword;
187
262
 
@@ -189,7 +264,7 @@ fn wordColor(language: Language, word: []const u8) ?text_spans.TextSpanColor {
189
264
  .rust => "bool char str String Vec Option Result Box i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize f32 f64",
190
265
  .c_like => "bool boolean byte char decimal double float int long object sbyte short string uint ulong ushort void",
191
266
  .go => "any bool byte comparable complex64 complex128 error float32 float64 int int8 int16 int32 int64 rune string uint uint8 uint16 uint32 uint64 uintptr",
192
- .javascript, .typescript => "Array BigInt Boolean Date Error Map Number Object Promise RegExp Set String Symbol",
267
+ .javascript, .typescript, .jsx, .tsx => "Array BigInt Boolean Date Error Map Number Object Promise RegExp Set String Symbol",
193
268
  .python => "bool bytes dict float int list object set str tuple",
194
269
  else => "",
195
270
  };
@@ -202,10 +277,10 @@ fn identifierStructuralColor(language: Language, source: []const u8, end: usize)
202
277
  while (cursor < source.len and (source[cursor] == ' ' or source[cursor] == '\t')) cursor += 1;
203
278
  if (cursor >= source.len or source[cursor] == '\n') return null;
204
279
  if (source[cursor] == '(' and language != .plain and language != .json) return .syntax_function;
205
- if (source[cursor] == ':' and switch (language) {
206
- .javascript, .typescript, .css => true,
207
- else => false,
208
- }) return .syntax_property;
280
+ // JavaScript/TypeScript object keys and typed bindings use the same
281
+ // identifier ink as variables. CSS declarations retain the dedicated
282
+ // property role.
283
+ if (source[cursor] == ':' and language == .css) return .syntax_property;
209
284
  if (source[cursor] == '{' and language == .css) return .syntax_literal;
210
285
  return null;
211
286
  }
@@ -218,6 +293,92 @@ fn identifierContinue(byte: u8) bool {
218
293
  return std.ascii.isAlphanumeric(byte) or byte == '_' or byte == '@' or byte == '$';
219
294
  }
220
295
 
296
+ fn yamlMappingKeyEnd(source: []const u8, start: usize) ?usize {
297
+ if (start >= source.len) return null;
298
+ // Let the sequence/explicit-key indicator stand on its own; the key
299
+ // beginning after its whitespace is considered from the next token.
300
+ if ((source[start] == '-' or source[start] == '?') and
301
+ start + 1 < source.len and
302
+ (source[start + 1] == ' ' or source[start + 1] == '\t'))
303
+ {
304
+ return null;
305
+ }
306
+
307
+ var before = start;
308
+ while (before > 0 and (source[before - 1] == ' ' or source[before - 1] == '\t')) before -= 1;
309
+ if (before > 0) {
310
+ switch (source[before - 1]) {
311
+ '\n', '\r', '{', '[', ',', '-', '?' => {},
312
+ else => return null,
313
+ }
314
+ }
315
+
316
+ var quote: ?u8 = null;
317
+ var cursor = start;
318
+ while (cursor < source.len) {
319
+ const byte = source[cursor];
320
+ if (quote) |active_quote| {
321
+ if (active_quote == '"' and byte == '\\' and cursor + 1 < source.len) {
322
+ cursor += 2;
323
+ continue;
324
+ }
325
+ if (byte == active_quote) {
326
+ // YAML single-quoted strings escape a quote by doubling it.
327
+ if (active_quote == '\'' and cursor + 1 < source.len and source[cursor + 1] == '\'') {
328
+ cursor += 2;
329
+ continue;
330
+ }
331
+ quote = null;
332
+ }
333
+ cursor += 1;
334
+ continue;
335
+ }
336
+
337
+ if (byte == '"' or byte == '\'') {
338
+ quote = byte;
339
+ cursor += 1;
340
+ continue;
341
+ }
342
+ if (byte == ':') {
343
+ const separates_value = cursor + 1 == source.len or switch (source[cursor + 1]) {
344
+ ' ', '\t', '\r', '\n', ',', '}', ']' => true,
345
+ else => false,
346
+ };
347
+ if (!separates_value) {
348
+ cursor += 1;
349
+ continue;
350
+ }
351
+ var end = cursor;
352
+ while (end > start and (source[end - 1] == ' ' or source[end - 1] == '\t')) end -= 1;
353
+ return if (end > start) end else null;
354
+ }
355
+ if (byte == '\n' or byte == '\r' or byte == ',' or byte == '}' or byte == ']') return null;
356
+ cursor += 1;
357
+ }
358
+ return null;
359
+ }
360
+
361
+ fn yamlSymbolEnd(source: []const u8, start: usize) usize {
362
+ var cursor = start + 1;
363
+ while (cursor < source.len) : (cursor += 1) {
364
+ switch (source[cursor]) {
365
+ ' ', '\t', '\r', '\n', ',', '[', ']', '{', '}' => break,
366
+ else => {},
367
+ }
368
+ }
369
+ return cursor;
370
+ }
371
+
372
+ fn yamlDocumentMarkerLength(rest: []const u8) usize {
373
+ if (rest.len < 3) return 0;
374
+ if (!std.mem.startsWith(u8, rest, "---") and !std.mem.startsWith(u8, rest, "...")) return 0;
375
+ if (rest.len == 3) return 3;
376
+ return switch (rest[3]) {
377
+ ' ', '\t', '\r', '\n', '#' => 3,
378
+ else => 0,
379
+ };
380
+ }
381
+
221
382
  fn htmlTagOpenerByte(byte: u8) bool {
222
383
  return identifierStart(byte) or byte == '/' or byte == '!' or byte == '?' or byte == '>';
223
384
  }
@@ -229,19 +390,41 @@ fn htmlPreviousAllowsTag(byte: u8) bool {
229
390
  };
230
391
  }
231
392
 
393
+ fn isHtmlFamily(language: Language) bool {
394
+ return language == .html or language == .jsx or language == .tsx;
395
+ }
396
+
397
+ fn embeddedScriptLanguage(language: Language) Language {
398
+ return switch (language) {
399
+ .jsx => .javascript,
400
+ .html, .tsx => .typescript,
401
+ else => language,
402
+ };
403
+ }
404
+
232
405
  /// A `<` in HTML-family source is structural only when it can begin a tag.
233
406
  /// Inside a JSX expression, the preceding token must also leave room for an
234
407
  /// expression operand; `count < limit` is relational, while
235
408
  /// `ok && <Badge />` starts nested JSX.
236
- fn htmlLessThanStartsTag(source: []const u8, index: usize, state: HighlightState) bool {
409
+ fn htmlLessThanStartsTag(source: []const u8, index: usize, language: Language, state: HighlightState) bool {
237
410
  if (index + 1 >= source.len or !htmlTagOpenerByte(source[index + 1])) return false;
238
- if (state.html_expression_depth == 0) return true;
411
+ if (language == .html and state.html_expression_depth == 0) return true;
412
+ if (!state.html_in_tag and state.html_element_len > 0 and
413
+ state.html_expression_depth == state.html_element_expression_bases[state.html_element_len - 1])
414
+ {
415
+ return true;
416
+ }
239
417
 
240
418
  var cursor = index;
241
419
  while (cursor > 0) {
242
420
  cursor -= 1;
243
421
  const byte = source[cursor];
244
422
  if (byte == ' ' or byte == '\t' or byte == '\r' or byte == '\n') continue;
423
+ if (identifierContinue(byte)) {
424
+ const end = cursor + 1;
425
+ while (cursor > 0 and identifierContinue(source[cursor - 1])) cursor -= 1;
426
+ return wordInList(source[cursor..end], "return throw yield", false);
427
+ }
245
428
  return htmlPreviousAllowsTag(byte);
246
429
  }
247
430
  return htmlPreviousAllowsTag(state.html_previous_significant);
@@ -260,13 +443,13 @@ fn updateHtmlPreviousSignificant(state: *HighlightState, source: []const u8) voi
260
443
 
261
444
  fn stringQuote(language: Language, byte: u8) bool {
262
445
  return switch (language) {
263
- .plain, .html => false,
446
+ .plain, .html, .markdown => false,
264
447
  .json => byte == '"',
265
448
  // A Rust apostrophe begins a character only when a closing quote
266
449
  // follows one scalar or escape; otherwise it introduces a lifetime
267
450
  // (`'a`, `'static`) and must not open multiline string state.
268
451
  .rust => byte == '"',
269
- .shell, .javascript, .typescript, .go => byte == '"' or byte == '\'' or byte == '`',
452
+ .shell, .javascript, .typescript, .jsx, .tsx, .go => byte == '"' or byte == '\'' or byte == '`',
270
453
  else => byte == '"' or byte == '\'',
271
454
  };
272
455
  }
@@ -318,19 +501,27 @@ fn backslashEscapesQuote(language: Language, state: HighlightState, quote: u8) b
318
501
  // Plain HTML attributes do not use JavaScript escapes for either
319
502
  // quote style, but strings inside JSX expressions do.
320
503
  .html => state.html_expression_depth > 0,
504
+ // JSX attribute text follows HTML quote rules; JavaScript strings
505
+ // at top level or inside an attribute expression use escapes.
506
+ .jsx, .tsx => !state.html_in_tag or state.html_expression_depth > state.html_tag_expression_base,
321
507
  // Shell single quotes are literal. SQL quotes are escaped by
322
508
  // doubling them, never with a backslash.
323
509
  .shell => quote != '\'',
510
+ .yaml => quote == '"',
324
511
  .sql => false,
325
512
  else => true,
326
513
  };
327
514
  }
328
515
 
329
- fn lineCommentPrefix(language: Language, rest: []const u8) usize {
516
+ fn lineCommentPrefix(language: Language, source: []const u8, index: usize) usize {
517
+ const rest = source[index..];
330
518
  if (rest.len == 0) return 0;
331
519
  return switch (language) {
332
- .zig, .javascript, .typescript, .rust, .c_like, .go => if (std.mem.startsWith(u8, rest, "//")) 2 else 0,
520
+ .zig, .javascript, .typescript, .jsx, .tsx, .rust, .c_like, .go => if (std.mem.startsWith(u8, rest, "//")) 2 else 0,
333
521
  .shell, .python => if (rest[0] == '#') 1 else 0,
522
+ .yaml => if (rest[0] == '#' and
523
+ (index == 0 or source[index - 1] == ' ' or source[index - 1] == '\t' or
524
+ source[index - 1] == '\r' or source[index - 1] == '\n')) 1 else 0,
334
525
  .sql => if (std.mem.startsWith(u8, rest, "--")) 2 else 0,
335
526
  else => 0,
336
527
  };
@@ -338,7 +529,7 @@ fn lineCommentPrefix(language: Language, rest: []const u8) usize {
338
529
 
339
530
  fn hasBlockComments(language: Language) bool {
340
531
  return switch (language) {
341
- .zig, .javascript, .typescript, .rust, .c_like, .go, .css, .sql => true,
532
+ .zig, .javascript, .typescript, .jsx, .tsx, .rust, .c_like, .go, .css, .sql => true,
342
533
  else => false,
343
534
  };
344
535
  }
@@ -371,6 +562,243 @@ fn appendSpan(
371
562
  return true;
372
563
  }
373
564
 
565
+ fn appendMarkdownSpan(
566
+ storage: *[text_spans.max_text_spans_per_paragraph]TextSpan,
567
+ len: *usize,
568
+ source: []const u8,
569
+ start: usize,
570
+ end: usize,
571
+ color: text_spans.TextSpanColor,
572
+ styling_full: *bool,
573
+ ) void {
574
+ if (styling_full.*) return;
575
+ if (!appendSpan(storage, len, source, start, end, color)) styling_full.* = true;
576
+ }
577
+
578
+ fn markdownDelimiterRun(source: []const u8, start: usize, byte: u8) usize {
579
+ var end = start;
580
+ while (end < source.len and source[end] == byte) end += 1;
581
+ return end - start;
582
+ }
583
+
584
+ fn markdownFenceLine(
585
+ source: []const u8,
586
+ start: usize,
587
+ state: HighlightState,
588
+ ) ?struct { marker_start: usize, marker_end: usize, line_end: usize, closes: bool } {
589
+ var marker_start = start;
590
+ var indent: usize = 0;
591
+ while (marker_start < source.len and indent < 3 and source[marker_start] == ' ') : (indent += 1) marker_start += 1;
592
+ if (marker_start >= source.len) return null;
593
+ const byte = source[marker_start];
594
+ if (byte != '`' and byte != '~') return null;
595
+ const run = markdownDelimiterRun(source, marker_start, byte);
596
+ if (run < 3) return null;
597
+
598
+ const line_break = std.mem.indexOfScalarPos(u8, source, marker_start + run, '\n');
599
+ const line_end = if (line_break) |newline| newline + 1 else source.len;
600
+ if (state.markdown_fence_byte == 0) {
601
+ return .{ .marker_start = marker_start, .marker_end = marker_start + run, .line_end = line_end, .closes = false };
602
+ }
603
+ if (byte != state.markdown_fence_byte or run < state.markdown_fence_len) return null;
604
+ const suffix_end = if (line_break) |newline| newline else source.len;
605
+ if (std.mem.trim(u8, source[marker_start + run .. suffix_end], " \t\r").len != 0) return null;
606
+ return .{ .marker_start = marker_start, .marker_end = marker_start + run, .line_end = line_end, .closes = true };
607
+ }
608
+
609
+ fn markdownListMarkerEnd(source: []const u8, start: usize) ?usize {
610
+ if (start >= source.len) return null;
611
+ if ((source[start] == '-' or source[start] == '+' or source[start] == '*') and
612
+ start + 1 < source.len and (source[start + 1] == ' ' or source[start + 1] == '\t'))
613
+ {
614
+ return start + 1;
615
+ }
616
+ if (!std.ascii.isDigit(source[start])) return null;
617
+ var end = start + 1;
618
+ while (end < source.len and std.ascii.isDigit(source[end])) end += 1;
619
+ if (end >= source.len or (source[end] != '.' and source[end] != ')')) return null;
620
+ if (end + 1 >= source.len or (source[end + 1] != ' ' and source[end + 1] != '\t')) return null;
621
+ return end + 1;
622
+ }
623
+
624
+ fn markdownInlineCodeEnd(source: []const u8, start: usize, delimiter_len: usize) ?usize {
625
+ var cursor = start;
626
+ while (cursor < source.len) {
627
+ if (source[cursor] == '`') {
628
+ const run = markdownDelimiterRun(source, cursor, '`');
629
+ if (run == delimiter_len) return cursor + run;
630
+ cursor += run;
631
+ continue;
632
+ }
633
+ cursor += 1;
634
+ }
635
+ return null;
636
+ }
637
+
638
+ fn highlightMarkdownWithState(
639
+ source: []const u8,
640
+ storage: *[text_spans.max_text_spans_per_paragraph]TextSpan,
641
+ state: *HighlightState,
642
+ ) []const TextSpan {
643
+ var len: usize = 0;
644
+ var styling_full = false;
645
+ var index: usize = 0;
646
+ while (index < source.len) {
647
+ if (state.markdown_line_start) {
648
+ if (markdownFenceLine(source, index, state.*)) |fence| {
649
+ appendMarkdownSpan(storage, &len, source, index, fence.marker_start, .syntax_plain, &styling_full);
650
+ appendMarkdownSpan(storage, &len, source, fence.marker_start, fence.marker_end, .syntax_keyword, &styling_full);
651
+ appendMarkdownSpan(storage, &len, source, fence.marker_end, fence.line_end, if (fence.closes) .syntax_plain else .syntax_constant, &styling_full);
652
+ if (fence.closes) {
653
+ state.markdown_fence_byte = 0;
654
+ state.markdown_fence_len = 0;
655
+ } else {
656
+ state.markdown_fence_byte = source[fence.marker_start];
657
+ state.markdown_fence_len = @intCast(@min(fence.marker_end - fence.marker_start, std.math.maxInt(u8)));
658
+ }
659
+ state.markdown_line_start = fence.line_end > fence.marker_end and source[fence.line_end - 1] == '\n';
660
+ index = fence.line_end;
661
+ continue;
662
+ }
663
+ if (state.markdown_fence_byte != 0) {
664
+ const newline = std.mem.indexOfScalarPos(u8, source, index, '\n');
665
+ const end = if (newline) |line_break| line_break + 1 else source.len;
666
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_literal, &styling_full);
667
+ state.markdown_line_start = newline != null;
668
+ index = end;
669
+ continue;
670
+ }
671
+
672
+ var content = index;
673
+ var indent: usize = 0;
674
+ while (content < source.len and indent < 3 and source[content] == ' ') : (indent += 1) content += 1;
675
+ appendMarkdownSpan(storage, &len, source, index, content, .syntax_plain, &styling_full);
676
+ if (content < source.len and source[content] == '#') {
677
+ const run = markdownDelimiterRun(source, content, '#');
678
+ if (run <= 6 and content + run < source.len and (source[content + run] == ' ' or source[content + run] == '\t')) {
679
+ appendMarkdownSpan(storage, &len, source, content, content + run, .syntax_keyword, &styling_full);
680
+ index = content + run;
681
+ state.markdown_line_start = false;
682
+ continue;
683
+ }
684
+ }
685
+ if (content < source.len and source[content] == '>') {
686
+ appendMarkdownSpan(storage, &len, source, content, content + 1, .syntax_keyword, &styling_full);
687
+ index = content + 1;
688
+ state.markdown_line_start = false;
689
+ continue;
690
+ }
691
+ if (markdownListMarkerEnd(source, content)) |marker_end| {
692
+ appendMarkdownSpan(storage, &len, source, content, marker_end, .syntax_keyword, &styling_full);
693
+ index = marker_end;
694
+ state.markdown_line_start = false;
695
+ continue;
696
+ }
697
+ index = content;
698
+ state.markdown_line_start = false;
699
+ if (index >= source.len) break;
700
+ }
701
+
702
+ if (source[index] == '\n') {
703
+ appendMarkdownSpan(storage, &len, source, index, index + 1, .syntax_plain, &styling_full);
704
+ index += 1;
705
+ state.markdown_line_start = true;
706
+ continue;
707
+ }
708
+ if (state.html_comment) {
709
+ const closing = std.mem.indexOfPos(u8, source, index, "-->");
710
+ const end = if (closing) |close| close + 3 else source.len;
711
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_comment, &styling_full);
712
+ state.html_comment = closing == null;
713
+ index = end;
714
+ continue;
715
+ }
716
+ if (std.mem.startsWith(u8, source[index..], "<!--")) {
717
+ const closing = std.mem.indexOfPos(u8, source, index + 4, "-->");
718
+ const end = if (closing) |close| close + 3 else source.len;
719
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_comment, &styling_full);
720
+ state.html_comment = closing == null;
721
+ index = end;
722
+ continue;
723
+ }
724
+ if (state.markdown_inline_code_len != 0) {
725
+ const delimiter_len = state.markdown_inline_code_len;
726
+ if (markdownInlineCodeEnd(source, index, delimiter_len)) |end| {
727
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_literal, &styling_full);
728
+ state.markdown_inline_code_len = 0;
729
+ index = end;
730
+ } else {
731
+ appendMarkdownSpan(storage, &len, source, index, source.len, .syntax_literal, &styling_full);
732
+ index = source.len;
733
+ }
734
+ continue;
735
+ }
736
+ if (source[index] == '`') {
737
+ const delimiter_len = markdownDelimiterRun(source, index, '`');
738
+ const content_start = index + delimiter_len;
739
+ if (markdownInlineCodeEnd(source, content_start, delimiter_len)) |end| {
740
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_literal, &styling_full);
741
+ state.markdown_inline_code_len = 0;
742
+ index = end;
743
+ } else {
744
+ appendMarkdownSpan(storage, &len, source, index, source.len, .syntax_literal, &styling_full);
745
+ state.markdown_inline_code_len = @intCast(@min(delimiter_len, std.math.maxInt(u8)));
746
+ index = source.len;
747
+ }
748
+ continue;
749
+ }
750
+ if (source[index] == '\\' and index + 1 < source.len) {
751
+ appendMarkdownSpan(storage, &len, source, index, index + 2, .syntax_constant, &styling_full);
752
+ index += 2;
753
+ continue;
754
+ }
755
+ if (std.mem.startsWith(u8, source[index..], "![")) {
756
+ appendMarkdownSpan(storage, &len, source, index, index + 2, .syntax_keyword, &styling_full);
757
+ index += 2;
758
+ continue;
759
+ }
760
+ if (source[index] == '[' or source[index] == ']') {
761
+ appendMarkdownSpan(storage, &len, source, index, index + 1, .syntax_property, &styling_full);
762
+ index += 1;
763
+ continue;
764
+ }
765
+ if (source[index] == '(' and index > 0 and source[index - 1] == ']') {
766
+ const close = std.mem.indexOfScalarPos(u8, source, index + 1, ')');
767
+ const end = if (close) |value| value + 1 else source.len;
768
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_literal, &styling_full);
769
+ index = end;
770
+ continue;
771
+ }
772
+ if (source[index] == '<' and
773
+ (std.mem.startsWith(u8, source[index..], "<http://") or std.mem.startsWith(u8, source[index..], "<https://")))
774
+ {
775
+ const close = std.mem.indexOfScalarPos(u8, source, index + 1, '>');
776
+ const end = if (close) |value| value + 1 else source.len;
777
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_literal, &styling_full);
778
+ index = end;
779
+ continue;
780
+ }
781
+ if (source[index] == '*' or source[index] == '_' or source[index] == '~') {
782
+ const run = @min(markdownDelimiterRun(source, index, source[index]), 2);
783
+ appendMarkdownSpan(storage, &len, source, index, index + run, .syntax_keyword, &styling_full);
784
+ index += run;
785
+ continue;
786
+ }
787
+
788
+ const start = index;
789
+ while (index < source.len and
790
+ source[index] != '\n' and source[index] != '`' and source[index] != '\\' and
791
+ source[index] != '[' and source[index] != ']' and source[index] != '(' and
792
+ source[index] != '<' and source[index] != '*' and source[index] != '_' and source[index] != '~')
793
+ {
794
+ index += 1;
795
+ }
796
+ if (index == start) index += 1;
797
+ appendMarkdownSpan(storage, &len, source, start, index, .syntax_plain, &styling_full);
798
+ }
799
+ return storage[0..len];
800
+ }
801
+
374
802
  /// Tokenize `source` into theme-colored monospace spans.
375
803
  pub fn highlight(
376
804
  source: []const u8,
@@ -395,6 +823,7 @@ pub fn highlightWithState(
395
823
  storage[0] = .{ .text = source, .monospace = true, .color = .syntax_plain };
396
824
  return storage[0..1];
397
825
  }
826
+ if (language == .markdown) return highlightMarkdownWithState(source, storage, state);
398
827
 
399
828
  var len: usize = 0;
400
829
  var styling_full = false;
@@ -471,7 +900,7 @@ pub fn highlightWithState(
471
900
  state.block_comment = true;
472
901
  }
473
902
  color = .syntax_comment;
474
- } else if (lineCommentPrefix(language, rest) != 0) {
903
+ } else if (lineCommentPrefix(language, source, index) != 0) {
475
904
  while (index < source.len and source[index] != '\n') index += 1;
476
905
  state.line_comment = index == source.len;
477
906
  color = .syntax_comment;
@@ -479,41 +908,74 @@ pub fn highlightWithState(
479
908
  while (index < source.len and source[index] != '\n') index += 1;
480
909
  state.preprocessor_line = index == source.len;
481
910
  color = .syntax_constant;
482
- } else if (language == .html and
911
+ } else if (isHtmlFamily(language) and
483
912
  rest[0] == '<' and
484
- htmlLessThanStartsTag(source, index, state.*))
913
+ htmlLessThanStartsTag(source, index, language, state.*))
485
914
  {
486
915
  index += 1;
487
- if (index < source.len and source[index] == '/') index += 1;
916
+ const opener = if (index < source.len) source[index] else 0;
917
+ const closing = opener == '/';
918
+ if (closing) index += 1;
488
919
  pushHtmlTagContext(state);
489
920
  state.html_in_tag = true;
490
921
  state.html_expect_tag_name = true;
491
922
  state.html_tag_expression_base = state.html_expression_depth;
923
+ state.html_tag_opened_element = false;
924
+ if (closing) {
925
+ if (state.html_element_len > 0) state.html_element_len -= 1;
926
+ } else if ((identifierStart(opener) or opener == '>') and
927
+ state.html_element_len < state.html_element_expression_bases.len)
928
+ {
929
+ state.html_element_expression_bases[state.html_element_len] = state.html_expression_depth;
930
+ state.html_element_len += 1;
931
+ state.html_tag_opened_element = true;
932
+ }
492
933
  color = .syntax_plain;
493
- } else if (language == .html and
934
+ } else if (isHtmlFamily(language) and
494
935
  state.html_in_tag and
495
936
  state.html_expression_depth == state.html_tag_expression_base and
496
937
  rest[0] == '>')
497
938
  {
939
+ if (state.html_tag_opened_element and index > 0 and source[index - 1] == '/' and state.html_element_len > 0) {
940
+ state.html_element_len -= 1;
941
+ }
498
942
  index += 1;
499
943
  if (!restoreHtmlTagContext(state)) {
500
944
  state.html_in_tag = false;
501
945
  state.html_expect_tag_name = false;
946
+ state.html_tag_opened_element = false;
502
947
  }
503
948
  color = .syntax_plain;
504
- } else if (language == .html and rest[0] == '{') {
949
+ } else if (isHtmlFamily(language) and rest[0] == '{') {
505
950
  index += 1;
506
951
  state.html_expression_depth += 1;
507
952
  color = .syntax_plain;
508
- } else if (language == .html and state.html_expression_depth > 0 and rest[0] == '}') {
953
+ } else if (isHtmlFamily(language) and state.html_expression_depth > 0 and rest[0] == '}') {
509
954
  index += 1;
510
955
  state.html_expression_depth -= 1;
511
956
  color = .syntax_plain;
512
957
  } else if (if (language == .rust) rustCharLiteralLength(rest) else null) |literal_len| {
513
958
  index += literal_len;
514
959
  color = .syntax_literal;
960
+ } else if (if (language == .yaml) yamlMappingKeyEnd(source, index) else null) |key_end| {
961
+ index = key_end;
962
+ color = .syntax_property;
963
+ } else if (language == .yaml and yamlDocumentMarkerLength(rest) != 0) {
964
+ index += yamlDocumentMarkerLength(rest);
965
+ color = .syntax_keyword;
966
+ } else if (language == .yaml and
967
+ (rest[0] == '&' or rest[0] == '*' or rest[0] == '!' or rest[0] == '%'))
968
+ {
969
+ index = yamlSymbolEnd(source, index);
970
+ color = .syntax_constant;
971
+ } else if (language == .yaml and rest[0] == '~') {
972
+ index += 1;
973
+ color = .syntax_literal;
974
+ } else if (language == .yaml and (rest[0] == '|' or rest[0] == '>')) {
975
+ index += 1;
976
+ color = .syntax_keyword;
515
977
  } else if (stringQuote(language, rest[0]) or
516
- (language == .html and
978
+ (isHtmlFamily(language) and
517
979
  (state.html_in_tag or state.html_expression_depth > 0) and
518
980
  (rest[0] == '"' or rest[0] == '\'' or rest[0] == '`')))
519
981
  {
@@ -549,24 +1011,30 @@ pub fn highlightWithState(
549
1011
  index += 1;
550
1012
  while (index < source.len and
551
1013
  (identifierContinue(source[index]) or
552
- ((language == .html or language == .css) and source[index] == '-')))
1014
+ ((language == .css or
1015
+ (isHtmlFamily(language) and state.html_in_tag)) and
1016
+ source[index] == '-')))
553
1017
  {
554
1018
  index += 1;
555
1019
  }
556
- if (language == .html and state.html_in_tag) {
1020
+ if (isHtmlFamily(language) and state.html_in_tag) {
557
1021
  if (state.html_expect_tag_name) {
558
1022
  color = .syntax_literal;
559
1023
  state.html_expect_tag_name = false;
560
- } else if (state.html_expression_depth == 0) {
1024
+ } else if (state.html_expression_depth == state.html_tag_expression_base) {
561
1025
  color = .syntax_function;
562
1026
  } else {
563
- color = wordColor(.typescript, source[start..index]) orelse
564
- identifierStructuralColor(.typescript, source, index) orelse
1027
+ const script_language = embeddedScriptLanguage(language);
1028
+ color = wordColor(script_language, source[start..index]) orelse
1029
+ identifierStructuralColor(script_language, source, index) orelse
565
1030
  .syntax_plain;
566
1031
  }
567
- } else if (language == .html and state.html_expression_depth > 0) {
568
- color = wordColor(.typescript, source[start..index]) orelse
569
- identifierStructuralColor(.typescript, source, index) orelse
1032
+ } else if (isHtmlFamily(language) and
1033
+ (state.html_expression_depth > 0 or language == .jsx or language == .tsx))
1034
+ {
1035
+ const script_language = embeddedScriptLanguage(language);
1036
+ color = wordColor(script_language, source[start..index]) orelse
1037
+ identifierStructuralColor(script_language, source, index) orelse
570
1038
  .syntax_plain;
571
1039
  } else {
572
1040
  color = wordColor(language, source[start..index]) orelse
@@ -577,7 +1045,7 @@ pub fn highlightWithState(
577
1045
  index += 1;
578
1046
  }
579
1047
 
580
- if (language == .html) updateHtmlPreviousSignificant(state, source[start..index]);
1048
+ if (isHtmlFamily(language)) updateHtmlPreviousSignificant(state, source[start..index]);
581
1049
 
582
1050
  // The last span already covers the entire plain-syntax remainder once
583
1051
  // capacity fills, but keep scanning it so state handed to the next