@native-sdk/cli 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/build.zig +65 -91
  2. package/package.json +9 -9
  3. package/packages/core/package-lock.json +2 -2
  4. package/packages/core/package.json +1 -1
  5. package/packages/core/sdk/text.ts +25 -14
  6. package/packages/core/src/checker.ts +39 -0
  7. package/packages/core/src/emitter.ts +125 -1
  8. package/skill-data/native-ui/SKILL.md +9 -7
  9. package/src/platform/macos/appkit_host.m +25 -1
  10. package/src/platform/macos/cef_host.mm +24 -2
  11. package/src/platform/macos/root.zig +13 -0
  12. package/src/primitives/canvas/code.zig +451 -33
  13. package/src/primitives/canvas/code_tests.zig +855 -5
  14. package/src/primitives/canvas/events.zig +111 -1
  15. package/src/primitives/canvas/markdown_tests.zig +17 -18
  16. package/src/primitives/canvas/root.zig +4 -0
  17. package/src/primitives/canvas/text.zig +6 -0
  18. package/src/primitives/canvas/ui.zig +108 -29
  19. package/src/primitives/canvas/ui_markup.zig +23 -9
  20. package/src/primitives/canvas/ui_markup_compiled.zig +13 -0
  21. package/src/primitives/canvas/ui_markup_compiled_tests.zig +12 -0
  22. package/src/primitives/canvas/ui_markup_contract.zig +8 -1
  23. package/src/primitives/canvas/ui_markup_view.zig +16 -0
  24. package/src/primitives/canvas/ui_markup_view_tests.zig +71 -5
  25. package/src/primitives/canvas/ui_schema.zig +13 -0
  26. package/src/primitives/canvas/ui_schema_tests.zig +9 -7
  27. package/src/primitives/canvas/ui_tests.zig +26 -0
  28. package/src/primitives/canvas/widget_invalidation.zig +1 -0
  29. package/src/primitives/canvas/widget_metrics.zig +10 -5
  30. package/src/primitives/canvas/widget_render.zig +660 -39
  31. package/src/primitives/canvas/widget_render_controls.zig +4 -4
  32. package/src/primitives/canvas/widget_runtime.zig +2 -0
  33. package/src/primitives/canvas/widget_text_input.zig +165 -8
  34. package/src/primitives/canvas/widget_tree.zig +6 -5
  35. package/src/primitives/canvas/widgets.zig +25 -0
  36. package/src/runtime/canvas_budget_edge_tests.zig +3 -2
  37. package/src/runtime/canvas_limits.zig +13 -6
  38. package/src/runtime/canvas_widget_clipboard_tests.zig +7 -4
  39. package/src/runtime/canvas_widget_context_menu.zig +7 -2
  40. package/src/runtime/canvas_widget_event_tests.zig +18 -14
  41. package/src/runtime/canvas_widget_events.zig +51 -20
  42. package/src/runtime/canvas_widget_runtime.zig +105 -4
  43. package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
  44. package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
  45. package/src/runtime/canvas_widget_state.zig +27 -0
  46. package/src/runtime/canvas_widget_text_tests.zig +326 -0
  47. package/src/runtime/core.zig +15 -5
  48. package/src/runtime/gpu_surface_events.zig +12 -12
  49. package/src/runtime/view.zig +54 -16
  50. package/src/runtime/view_widget_scroll.zig +48 -18
  51. package/src/runtime/view_widget_text.zig +54 -13
  52. package/src/runtime/window_view_runtime.zig +9 -0
@@ -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
 
@@ -166,14 +188,16 @@ fn wordColor(language: Language, word: []const u8) ?text_spans.TextSpanColor {
166
188
  if (word.len > 0 and word[0] == '@') return .syntax_function;
167
189
  if (language == .zig) return zig_words.get(word);
168
190
  if (language == .python and wordInList(word, "True False None", false)) return .syntax_literal;
191
+ if (language == .yaml and wordInList(word, "true false null yes no on off", true)) return .syntax_literal;
169
192
  if (wordInList(word, "true false null nil none undefined this self super", language == .sql)) return .syntax_literal;
170
193
 
171
194
  const keywords = switch (language) {
172
195
  .plain => return null,
173
196
  .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",
197
+ .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",
198
+ .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
199
  .json => "",
200
+ .yaml => "",
177
201
  .shell => "case coproc do done elif else esac fi for function if in select then time until while",
178
202
  .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
203
  .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 +206,7 @@ fn wordColor(language: Language, word: []const u8) ?text_spans.TextSpanColor {
182
206
  .html => "",
183
207
  .css => "and important inherit initial none not only or revert unset",
184
208
  .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",
209
+ .markdown => "",
185
210
  };
186
211
  if (wordInList(word, keywords, language == .sql)) return .syntax_keyword;
187
212
 
@@ -189,7 +214,7 @@ fn wordColor(language: Language, word: []const u8) ?text_spans.TextSpanColor {
189
214
  .rust => "bool char str String Vec Option Result Box i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize f32 f64",
190
215
  .c_like => "bool boolean byte char decimal double float int long object sbyte short string uint ulong ushort void",
191
216
  .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",
217
+ .javascript, .typescript, .jsx, .tsx => "Array BigInt Boolean Date Error Map Number Object Promise RegExp Set String Symbol",
193
218
  .python => "bool bytes dict float int list object set str tuple",
194
219
  else => "",
195
220
  };
@@ -202,10 +227,10 @@ fn identifierStructuralColor(language: Language, source: []const u8, end: usize)
202
227
  while (cursor < source.len and (source[cursor] == ' ' or source[cursor] == '\t')) cursor += 1;
203
228
  if (cursor >= source.len or source[cursor] == '\n') return null;
204
229
  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;
230
+ // JavaScript/TypeScript object keys and typed bindings use the same
231
+ // identifier ink as variables. CSS declarations retain the dedicated
232
+ // property role.
233
+ if (source[cursor] == ':' and language == .css) return .syntax_property;
209
234
  if (source[cursor] == '{' and language == .css) return .syntax_literal;
210
235
  return null;
211
236
  }
@@ -218,6 +243,92 @@ fn identifierContinue(byte: u8) bool {
218
243
  return std.ascii.isAlphanumeric(byte) or byte == '_' or byte == '@' or byte == '$';
219
244
  }
220
245
 
246
+ fn yamlMappingKeyEnd(source: []const u8, start: usize) ?usize {
247
+ if (start >= source.len) return null;
248
+ // Let the sequence/explicit-key indicator stand on its own; the key
249
+ // beginning after its whitespace is considered from the next token.
250
+ if ((source[start] == '-' or source[start] == '?') and
251
+ start + 1 < source.len and
252
+ (source[start + 1] == ' ' or source[start + 1] == '\t'))
253
+ {
254
+ return null;
255
+ }
256
+
257
+ var before = start;
258
+ while (before > 0 and (source[before - 1] == ' ' or source[before - 1] == '\t')) before -= 1;
259
+ if (before > 0) {
260
+ switch (source[before - 1]) {
261
+ '\n', '\r', '{', '[', ',', '-', '?' => {},
262
+ else => return null,
263
+ }
264
+ }
265
+
266
+ var quote: ?u8 = null;
267
+ var cursor = start;
268
+ while (cursor < source.len) {
269
+ const byte = source[cursor];
270
+ if (quote) |active_quote| {
271
+ if (active_quote == '"' and byte == '\\' and cursor + 1 < source.len) {
272
+ cursor += 2;
273
+ continue;
274
+ }
275
+ if (byte == active_quote) {
276
+ // YAML single-quoted strings escape a quote by doubling it.
277
+ if (active_quote == '\'' and cursor + 1 < source.len and source[cursor + 1] == '\'') {
278
+ cursor += 2;
279
+ continue;
280
+ }
281
+ quote = null;
282
+ }
283
+ cursor += 1;
284
+ continue;
285
+ }
286
+
287
+ if (byte == '"' or byte == '\'') {
288
+ quote = byte;
289
+ cursor += 1;
290
+ continue;
291
+ }
292
+ if (byte == ':') {
293
+ const separates_value = cursor + 1 == source.len or switch (source[cursor + 1]) {
294
+ ' ', '\t', '\r', '\n', ',', '}', ']' => true,
295
+ else => false,
296
+ };
297
+ if (!separates_value) {
298
+ cursor += 1;
299
+ continue;
300
+ }
301
+ var end = cursor;
302
+ while (end > start and (source[end - 1] == ' ' or source[end - 1] == '\t')) end -= 1;
303
+ return if (end > start) end else null;
304
+ }
305
+ if (byte == '\n' or byte == '\r' or byte == ',' or byte == '}' or byte == ']') return null;
306
+ cursor += 1;
307
+ }
308
+ return null;
309
+ }
310
+
311
+ fn yamlSymbolEnd(source: []const u8, start: usize) usize {
312
+ var cursor = start + 1;
313
+ while (cursor < source.len) : (cursor += 1) {
314
+ switch (source[cursor]) {
315
+ ' ', '\t', '\r', '\n', ',', '[', ']', '{', '}' => break,
316
+ else => {},
317
+ }
318
+ }
319
+ return cursor;
320
+ }
321
+
322
+ fn yamlDocumentMarkerLength(rest: []const u8) usize {
323
+ if (rest.len < 3) return 0;
324
+ if (!std.mem.startsWith(u8, rest, "---") and !std.mem.startsWith(u8, rest, "...")) return 0;
325
+ if (rest.len == 3) return 3;
326
+ return switch (rest[3]) {
327
+ ' ', '\t', '\r', '\n', '#' => 3,
328
+ else => 0,
329
+ };
330
+ }
331
+
221
332
  fn htmlTagOpenerByte(byte: u8) bool {
222
333
  return identifierStart(byte) or byte == '/' or byte == '!' or byte == '?' or byte == '>';
223
334
  }
@@ -229,19 +340,41 @@ fn htmlPreviousAllowsTag(byte: u8) bool {
229
340
  };
230
341
  }
231
342
 
343
+ fn isHtmlFamily(language: Language) bool {
344
+ return language == .html or language == .jsx or language == .tsx;
345
+ }
346
+
347
+ fn embeddedScriptLanguage(language: Language) Language {
348
+ return switch (language) {
349
+ .jsx => .javascript,
350
+ .html, .tsx => .typescript,
351
+ else => language,
352
+ };
353
+ }
354
+
232
355
  /// A `<` in HTML-family source is structural only when it can begin a tag.
233
356
  /// Inside a JSX expression, the preceding token must also leave room for an
234
357
  /// expression operand; `count < limit` is relational, while
235
358
  /// `ok && <Badge />` starts nested JSX.
236
- fn htmlLessThanStartsTag(source: []const u8, index: usize, state: HighlightState) bool {
359
+ fn htmlLessThanStartsTag(source: []const u8, index: usize, language: Language, state: HighlightState) bool {
237
360
  if (index + 1 >= source.len or !htmlTagOpenerByte(source[index + 1])) return false;
238
- if (state.html_expression_depth == 0) return true;
361
+ if (language == .html and state.html_expression_depth == 0) return true;
362
+ if (!state.html_in_tag and state.html_element_len > 0 and
363
+ state.html_expression_depth == state.html_element_expression_bases[state.html_element_len - 1])
364
+ {
365
+ return true;
366
+ }
239
367
 
240
368
  var cursor = index;
241
369
  while (cursor > 0) {
242
370
  cursor -= 1;
243
371
  const byte = source[cursor];
244
372
  if (byte == ' ' or byte == '\t' or byte == '\r' or byte == '\n') continue;
373
+ if (identifierContinue(byte)) {
374
+ const end = cursor + 1;
375
+ while (cursor > 0 and identifierContinue(source[cursor - 1])) cursor -= 1;
376
+ return wordInList(source[cursor..end], "return throw yield", false);
377
+ }
245
378
  return htmlPreviousAllowsTag(byte);
246
379
  }
247
380
  return htmlPreviousAllowsTag(state.html_previous_significant);
@@ -260,13 +393,13 @@ fn updateHtmlPreviousSignificant(state: *HighlightState, source: []const u8) voi
260
393
 
261
394
  fn stringQuote(language: Language, byte: u8) bool {
262
395
  return switch (language) {
263
- .plain, .html => false,
396
+ .plain, .html, .markdown => false,
264
397
  .json => byte == '"',
265
398
  // A Rust apostrophe begins a character only when a closing quote
266
399
  // follows one scalar or escape; otherwise it introduces a lifetime
267
400
  // (`'a`, `'static`) and must not open multiline string state.
268
401
  .rust => byte == '"',
269
- .shell, .javascript, .typescript, .go => byte == '"' or byte == '\'' or byte == '`',
402
+ .shell, .javascript, .typescript, .jsx, .tsx, .go => byte == '"' or byte == '\'' or byte == '`',
270
403
  else => byte == '"' or byte == '\'',
271
404
  };
272
405
  }
@@ -318,19 +451,27 @@ fn backslashEscapesQuote(language: Language, state: HighlightState, quote: u8) b
318
451
  // Plain HTML attributes do not use JavaScript escapes for either
319
452
  // quote style, but strings inside JSX expressions do.
320
453
  .html => state.html_expression_depth > 0,
454
+ // JSX attribute text follows HTML quote rules; JavaScript strings
455
+ // at top level or inside an attribute expression use escapes.
456
+ .jsx, .tsx => !state.html_in_tag or state.html_expression_depth > state.html_tag_expression_base,
321
457
  // Shell single quotes are literal. SQL quotes are escaped by
322
458
  // doubling them, never with a backslash.
323
459
  .shell => quote != '\'',
460
+ .yaml => quote == '"',
324
461
  .sql => false,
325
462
  else => true,
326
463
  };
327
464
  }
328
465
 
329
- fn lineCommentPrefix(language: Language, rest: []const u8) usize {
466
+ fn lineCommentPrefix(language: Language, source: []const u8, index: usize) usize {
467
+ const rest = source[index..];
330
468
  if (rest.len == 0) return 0;
331
469
  return switch (language) {
332
- .zig, .javascript, .typescript, .rust, .c_like, .go => if (std.mem.startsWith(u8, rest, "//")) 2 else 0,
470
+ .zig, .javascript, .typescript, .jsx, .tsx, .rust, .c_like, .go => if (std.mem.startsWith(u8, rest, "//")) 2 else 0,
333
471
  .shell, .python => if (rest[0] == '#') 1 else 0,
472
+ .yaml => if (rest[0] == '#' and
473
+ (index == 0 or source[index - 1] == ' ' or source[index - 1] == '\t' or
474
+ source[index - 1] == '\r' or source[index - 1] == '\n')) 1 else 0,
334
475
  .sql => if (std.mem.startsWith(u8, rest, "--")) 2 else 0,
335
476
  else => 0,
336
477
  };
@@ -338,7 +479,7 @@ fn lineCommentPrefix(language: Language, rest: []const u8) usize {
338
479
 
339
480
  fn hasBlockComments(language: Language) bool {
340
481
  return switch (language) {
341
- .zig, .javascript, .typescript, .rust, .c_like, .go, .css, .sql => true,
482
+ .zig, .javascript, .typescript, .jsx, .tsx, .rust, .c_like, .go, .css, .sql => true,
342
483
  else => false,
343
484
  };
344
485
  }
@@ -371,6 +512,243 @@ fn appendSpan(
371
512
  return true;
372
513
  }
373
514
 
515
+ fn appendMarkdownSpan(
516
+ storage: *[text_spans.max_text_spans_per_paragraph]TextSpan,
517
+ len: *usize,
518
+ source: []const u8,
519
+ start: usize,
520
+ end: usize,
521
+ color: text_spans.TextSpanColor,
522
+ styling_full: *bool,
523
+ ) void {
524
+ if (styling_full.*) return;
525
+ if (!appendSpan(storage, len, source, start, end, color)) styling_full.* = true;
526
+ }
527
+
528
+ fn markdownDelimiterRun(source: []const u8, start: usize, byte: u8) usize {
529
+ var end = start;
530
+ while (end < source.len and source[end] == byte) end += 1;
531
+ return end - start;
532
+ }
533
+
534
+ fn markdownFenceLine(
535
+ source: []const u8,
536
+ start: usize,
537
+ state: HighlightState,
538
+ ) ?struct { marker_start: usize, marker_end: usize, line_end: usize, closes: bool } {
539
+ var marker_start = start;
540
+ var indent: usize = 0;
541
+ while (marker_start < source.len and indent < 3 and source[marker_start] == ' ') : (indent += 1) marker_start += 1;
542
+ if (marker_start >= source.len) return null;
543
+ const byte = source[marker_start];
544
+ if (byte != '`' and byte != '~') return null;
545
+ const run = markdownDelimiterRun(source, marker_start, byte);
546
+ if (run < 3) return null;
547
+
548
+ const line_break = std.mem.indexOfScalarPos(u8, source, marker_start + run, '\n');
549
+ const line_end = if (line_break) |newline| newline + 1 else source.len;
550
+ if (state.markdown_fence_byte == 0) {
551
+ return .{ .marker_start = marker_start, .marker_end = marker_start + run, .line_end = line_end, .closes = false };
552
+ }
553
+ if (byte != state.markdown_fence_byte or run < state.markdown_fence_len) return null;
554
+ const suffix_end = if (line_break) |newline| newline else source.len;
555
+ if (std.mem.trim(u8, source[marker_start + run .. suffix_end], " \t\r").len != 0) return null;
556
+ return .{ .marker_start = marker_start, .marker_end = marker_start + run, .line_end = line_end, .closes = true };
557
+ }
558
+
559
+ fn markdownListMarkerEnd(source: []const u8, start: usize) ?usize {
560
+ if (start >= source.len) return null;
561
+ if ((source[start] == '-' or source[start] == '+' or source[start] == '*') and
562
+ start + 1 < source.len and (source[start + 1] == ' ' or source[start + 1] == '\t'))
563
+ {
564
+ return start + 1;
565
+ }
566
+ if (!std.ascii.isDigit(source[start])) return null;
567
+ var end = start + 1;
568
+ while (end < source.len and std.ascii.isDigit(source[end])) end += 1;
569
+ if (end >= source.len or (source[end] != '.' and source[end] != ')')) return null;
570
+ if (end + 1 >= source.len or (source[end + 1] != ' ' and source[end + 1] != '\t')) return null;
571
+ return end + 1;
572
+ }
573
+
574
+ fn markdownInlineCodeEnd(source: []const u8, start: usize, delimiter_len: usize) ?usize {
575
+ var cursor = start;
576
+ while (cursor < source.len) {
577
+ if (source[cursor] == '`') {
578
+ const run = markdownDelimiterRun(source, cursor, '`');
579
+ if (run == delimiter_len) return cursor + run;
580
+ cursor += run;
581
+ continue;
582
+ }
583
+ cursor += 1;
584
+ }
585
+ return null;
586
+ }
587
+
588
+ fn highlightMarkdownWithState(
589
+ source: []const u8,
590
+ storage: *[text_spans.max_text_spans_per_paragraph]TextSpan,
591
+ state: *HighlightState,
592
+ ) []const TextSpan {
593
+ var len: usize = 0;
594
+ var styling_full = false;
595
+ var index: usize = 0;
596
+ while (index < source.len) {
597
+ if (state.markdown_line_start) {
598
+ if (markdownFenceLine(source, index, state.*)) |fence| {
599
+ appendMarkdownSpan(storage, &len, source, index, fence.marker_start, .syntax_plain, &styling_full);
600
+ appendMarkdownSpan(storage, &len, source, fence.marker_start, fence.marker_end, .syntax_keyword, &styling_full);
601
+ appendMarkdownSpan(storage, &len, source, fence.marker_end, fence.line_end, if (fence.closes) .syntax_plain else .syntax_constant, &styling_full);
602
+ if (fence.closes) {
603
+ state.markdown_fence_byte = 0;
604
+ state.markdown_fence_len = 0;
605
+ } else {
606
+ state.markdown_fence_byte = source[fence.marker_start];
607
+ state.markdown_fence_len = @intCast(@min(fence.marker_end - fence.marker_start, std.math.maxInt(u8)));
608
+ }
609
+ state.markdown_line_start = fence.line_end > fence.marker_end and source[fence.line_end - 1] == '\n';
610
+ index = fence.line_end;
611
+ continue;
612
+ }
613
+ if (state.markdown_fence_byte != 0) {
614
+ const newline = std.mem.indexOfScalarPos(u8, source, index, '\n');
615
+ const end = if (newline) |line_break| line_break + 1 else source.len;
616
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_literal, &styling_full);
617
+ state.markdown_line_start = newline != null;
618
+ index = end;
619
+ continue;
620
+ }
621
+
622
+ var content = index;
623
+ var indent: usize = 0;
624
+ while (content < source.len and indent < 3 and source[content] == ' ') : (indent += 1) content += 1;
625
+ appendMarkdownSpan(storage, &len, source, index, content, .syntax_plain, &styling_full);
626
+ if (content < source.len and source[content] == '#') {
627
+ const run = markdownDelimiterRun(source, content, '#');
628
+ if (run <= 6 and content + run < source.len and (source[content + run] == ' ' or source[content + run] == '\t')) {
629
+ appendMarkdownSpan(storage, &len, source, content, content + run, .syntax_keyword, &styling_full);
630
+ index = content + run;
631
+ state.markdown_line_start = false;
632
+ continue;
633
+ }
634
+ }
635
+ if (content < source.len and source[content] == '>') {
636
+ appendMarkdownSpan(storage, &len, source, content, content + 1, .syntax_keyword, &styling_full);
637
+ index = content + 1;
638
+ state.markdown_line_start = false;
639
+ continue;
640
+ }
641
+ if (markdownListMarkerEnd(source, content)) |marker_end| {
642
+ appendMarkdownSpan(storage, &len, source, content, marker_end, .syntax_keyword, &styling_full);
643
+ index = marker_end;
644
+ state.markdown_line_start = false;
645
+ continue;
646
+ }
647
+ index = content;
648
+ state.markdown_line_start = false;
649
+ if (index >= source.len) break;
650
+ }
651
+
652
+ if (source[index] == '\n') {
653
+ appendMarkdownSpan(storage, &len, source, index, index + 1, .syntax_plain, &styling_full);
654
+ index += 1;
655
+ state.markdown_line_start = true;
656
+ continue;
657
+ }
658
+ if (state.html_comment) {
659
+ const closing = std.mem.indexOfPos(u8, source, index, "-->");
660
+ const end = if (closing) |close| close + 3 else source.len;
661
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_comment, &styling_full);
662
+ state.html_comment = closing == null;
663
+ index = end;
664
+ continue;
665
+ }
666
+ if (std.mem.startsWith(u8, source[index..], "<!--")) {
667
+ const closing = std.mem.indexOfPos(u8, source, index + 4, "-->");
668
+ const end = if (closing) |close| close + 3 else source.len;
669
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_comment, &styling_full);
670
+ state.html_comment = closing == null;
671
+ index = end;
672
+ continue;
673
+ }
674
+ if (state.markdown_inline_code_len != 0) {
675
+ const delimiter_len = state.markdown_inline_code_len;
676
+ if (markdownInlineCodeEnd(source, index, delimiter_len)) |end| {
677
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_literal, &styling_full);
678
+ state.markdown_inline_code_len = 0;
679
+ index = end;
680
+ } else {
681
+ appendMarkdownSpan(storage, &len, source, index, source.len, .syntax_literal, &styling_full);
682
+ index = source.len;
683
+ }
684
+ continue;
685
+ }
686
+ if (source[index] == '`') {
687
+ const delimiter_len = markdownDelimiterRun(source, index, '`');
688
+ const content_start = index + delimiter_len;
689
+ if (markdownInlineCodeEnd(source, content_start, delimiter_len)) |end| {
690
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_literal, &styling_full);
691
+ state.markdown_inline_code_len = 0;
692
+ index = end;
693
+ } else {
694
+ appendMarkdownSpan(storage, &len, source, index, source.len, .syntax_literal, &styling_full);
695
+ state.markdown_inline_code_len = @intCast(@min(delimiter_len, std.math.maxInt(u8)));
696
+ index = source.len;
697
+ }
698
+ continue;
699
+ }
700
+ if (source[index] == '\\' and index + 1 < source.len) {
701
+ appendMarkdownSpan(storage, &len, source, index, index + 2, .syntax_constant, &styling_full);
702
+ index += 2;
703
+ continue;
704
+ }
705
+ if (std.mem.startsWith(u8, source[index..], "![")) {
706
+ appendMarkdownSpan(storage, &len, source, index, index + 2, .syntax_keyword, &styling_full);
707
+ index += 2;
708
+ continue;
709
+ }
710
+ if (source[index] == '[' or source[index] == ']') {
711
+ appendMarkdownSpan(storage, &len, source, index, index + 1, .syntax_property, &styling_full);
712
+ index += 1;
713
+ continue;
714
+ }
715
+ if (source[index] == '(' and index > 0 and source[index - 1] == ']') {
716
+ const close = std.mem.indexOfScalarPos(u8, source, index + 1, ')');
717
+ const end = if (close) |value| value + 1 else source.len;
718
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_literal, &styling_full);
719
+ index = end;
720
+ continue;
721
+ }
722
+ if (source[index] == '<' and
723
+ (std.mem.startsWith(u8, source[index..], "<http://") or std.mem.startsWith(u8, source[index..], "<https://")))
724
+ {
725
+ const close = std.mem.indexOfScalarPos(u8, source, index + 1, '>');
726
+ const end = if (close) |value| value + 1 else source.len;
727
+ appendMarkdownSpan(storage, &len, source, index, end, .syntax_literal, &styling_full);
728
+ index = end;
729
+ continue;
730
+ }
731
+ if (source[index] == '*' or source[index] == '_' or source[index] == '~') {
732
+ const run = @min(markdownDelimiterRun(source, index, source[index]), 2);
733
+ appendMarkdownSpan(storage, &len, source, index, index + run, .syntax_keyword, &styling_full);
734
+ index += run;
735
+ continue;
736
+ }
737
+
738
+ const start = index;
739
+ while (index < source.len and
740
+ source[index] != '\n' and source[index] != '`' and source[index] != '\\' and
741
+ source[index] != '[' and source[index] != ']' and source[index] != '(' and
742
+ source[index] != '<' and source[index] != '*' and source[index] != '_' and source[index] != '~')
743
+ {
744
+ index += 1;
745
+ }
746
+ if (index == start) index += 1;
747
+ appendMarkdownSpan(storage, &len, source, start, index, .syntax_plain, &styling_full);
748
+ }
749
+ return storage[0..len];
750
+ }
751
+
374
752
  /// Tokenize `source` into theme-colored monospace spans.
375
753
  pub fn highlight(
376
754
  source: []const u8,
@@ -395,6 +773,7 @@ pub fn highlightWithState(
395
773
  storage[0] = .{ .text = source, .monospace = true, .color = .syntax_plain };
396
774
  return storage[0..1];
397
775
  }
776
+ if (language == .markdown) return highlightMarkdownWithState(source, storage, state);
398
777
 
399
778
  var len: usize = 0;
400
779
  var styling_full = false;
@@ -471,7 +850,7 @@ pub fn highlightWithState(
471
850
  state.block_comment = true;
472
851
  }
473
852
  color = .syntax_comment;
474
- } else if (lineCommentPrefix(language, rest) != 0) {
853
+ } else if (lineCommentPrefix(language, source, index) != 0) {
475
854
  while (index < source.len and source[index] != '\n') index += 1;
476
855
  state.line_comment = index == source.len;
477
856
  color = .syntax_comment;
@@ -479,41 +858,74 @@ pub fn highlightWithState(
479
858
  while (index < source.len and source[index] != '\n') index += 1;
480
859
  state.preprocessor_line = index == source.len;
481
860
  color = .syntax_constant;
482
- } else if (language == .html and
861
+ } else if (isHtmlFamily(language) and
483
862
  rest[0] == '<' and
484
- htmlLessThanStartsTag(source, index, state.*))
863
+ htmlLessThanStartsTag(source, index, language, state.*))
485
864
  {
486
865
  index += 1;
487
- if (index < source.len and source[index] == '/') index += 1;
866
+ const opener = if (index < source.len) source[index] else 0;
867
+ const closing = opener == '/';
868
+ if (closing) index += 1;
488
869
  pushHtmlTagContext(state);
489
870
  state.html_in_tag = true;
490
871
  state.html_expect_tag_name = true;
491
872
  state.html_tag_expression_base = state.html_expression_depth;
873
+ state.html_tag_opened_element = false;
874
+ if (closing) {
875
+ if (state.html_element_len > 0) state.html_element_len -= 1;
876
+ } else if ((identifierStart(opener) or opener == '>') and
877
+ state.html_element_len < state.html_element_expression_bases.len)
878
+ {
879
+ state.html_element_expression_bases[state.html_element_len] = state.html_expression_depth;
880
+ state.html_element_len += 1;
881
+ state.html_tag_opened_element = true;
882
+ }
492
883
  color = .syntax_plain;
493
- } else if (language == .html and
884
+ } else if (isHtmlFamily(language) and
494
885
  state.html_in_tag and
495
886
  state.html_expression_depth == state.html_tag_expression_base and
496
887
  rest[0] == '>')
497
888
  {
889
+ if (state.html_tag_opened_element and index > 0 and source[index - 1] == '/' and state.html_element_len > 0) {
890
+ state.html_element_len -= 1;
891
+ }
498
892
  index += 1;
499
893
  if (!restoreHtmlTagContext(state)) {
500
894
  state.html_in_tag = false;
501
895
  state.html_expect_tag_name = false;
896
+ state.html_tag_opened_element = false;
502
897
  }
503
898
  color = .syntax_plain;
504
- } else if (language == .html and rest[0] == '{') {
899
+ } else if (isHtmlFamily(language) and rest[0] == '{') {
505
900
  index += 1;
506
901
  state.html_expression_depth += 1;
507
902
  color = .syntax_plain;
508
- } else if (language == .html and state.html_expression_depth > 0 and rest[0] == '}') {
903
+ } else if (isHtmlFamily(language) and state.html_expression_depth > 0 and rest[0] == '}') {
509
904
  index += 1;
510
905
  state.html_expression_depth -= 1;
511
906
  color = .syntax_plain;
512
907
  } else if (if (language == .rust) rustCharLiteralLength(rest) else null) |literal_len| {
513
908
  index += literal_len;
514
909
  color = .syntax_literal;
910
+ } else if (if (language == .yaml) yamlMappingKeyEnd(source, index) else null) |key_end| {
911
+ index = key_end;
912
+ color = .syntax_property;
913
+ } else if (language == .yaml and yamlDocumentMarkerLength(rest) != 0) {
914
+ index += yamlDocumentMarkerLength(rest);
915
+ color = .syntax_keyword;
916
+ } else if (language == .yaml and
917
+ (rest[0] == '&' or rest[0] == '*' or rest[0] == '!' or rest[0] == '%'))
918
+ {
919
+ index = yamlSymbolEnd(source, index);
920
+ color = .syntax_constant;
921
+ } else if (language == .yaml and rest[0] == '~') {
922
+ index += 1;
923
+ color = .syntax_literal;
924
+ } else if (language == .yaml and (rest[0] == '|' or rest[0] == '>')) {
925
+ index += 1;
926
+ color = .syntax_keyword;
515
927
  } else if (stringQuote(language, rest[0]) or
516
- (language == .html and
928
+ (isHtmlFamily(language) and
517
929
  (state.html_in_tag or state.html_expression_depth > 0) and
518
930
  (rest[0] == '"' or rest[0] == '\'' or rest[0] == '`')))
519
931
  {
@@ -549,24 +961,30 @@ pub fn highlightWithState(
549
961
  index += 1;
550
962
  while (index < source.len and
551
963
  (identifierContinue(source[index]) or
552
- ((language == .html or language == .css) and source[index] == '-')))
964
+ ((language == .css or
965
+ (isHtmlFamily(language) and state.html_in_tag)) and
966
+ source[index] == '-')))
553
967
  {
554
968
  index += 1;
555
969
  }
556
- if (language == .html and state.html_in_tag) {
970
+ if (isHtmlFamily(language) and state.html_in_tag) {
557
971
  if (state.html_expect_tag_name) {
558
972
  color = .syntax_literal;
559
973
  state.html_expect_tag_name = false;
560
- } else if (state.html_expression_depth == 0) {
974
+ } else if (state.html_expression_depth == state.html_tag_expression_base) {
561
975
  color = .syntax_function;
562
976
  } else {
563
- color = wordColor(.typescript, source[start..index]) orelse
564
- identifierStructuralColor(.typescript, source, index) orelse
977
+ const script_language = embeddedScriptLanguage(language);
978
+ color = wordColor(script_language, source[start..index]) orelse
979
+ identifierStructuralColor(script_language, source, index) orelse
565
980
  .syntax_plain;
566
981
  }
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
982
+ } else if (isHtmlFamily(language) and
983
+ (state.html_expression_depth > 0 or language == .jsx or language == .tsx))
984
+ {
985
+ const script_language = embeddedScriptLanguage(language);
986
+ color = wordColor(script_language, source[start..index]) orelse
987
+ identifierStructuralColor(script_language, source, index) orelse
570
988
  .syntax_plain;
571
989
  } else {
572
990
  color = wordColor(language, source[start..index]) orelse
@@ -577,7 +995,7 @@ pub fn highlightWithState(
577
995
  index += 1;
578
996
  }
579
997
 
580
- if (language == .html) updateHtmlPreviousSignificant(state, source[start..index]);
998
+ if (isHtmlFamily(language)) updateHtmlPreviousSignificant(state, source[start..index]);
581
999
 
582
1000
  // The last span already covers the entire plain-syntax remainder once
583
1001
  // capacity fills, but keep scanning it so state handed to the next