summaryrefslogtreecommitdiff
path: root/src/syntax.zig
blob: 09ff12cc85f90319b436eaddf826429619ac4035 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
//! Tree-sitter syntax highlighting: one style byte per content byte, filled by
//! running each grammar's highlights.scm query (slurped at build time into the
//! ts_queries options module). Grammar set is tiered: `zig`; `minimal` (c, cpp,
//! zig); and `full`, which adds ~24 languages lazily on first use.
const std = @import("std");
const config = @import("pardes_config");
const tracy = @import("tracy.zig");
const grammar_manifest = @import("grammar_manifest.zig");

pub const enabled = config.syntax_highlighting;
const zig_grammar = config.syntax_zig_grammar;
const minimal_grammars = config.syntax_minimal_grammars;
const full_grammars = config.syntax_full_grammars;

const ts = if (enabled) @import("tree-sitter") else struct {
    pub const Language = opaque {};
    pub const Query = opaque {};
};
const ts_queries = if (enabled) @import("ts_queries") else struct {};

const LanguageFn = *const fn () callconv(.c) *const ts.Language;

pub const Syn = enum(u8) { none, keyword, string, number, comment };

const Spec = struct {
    name: []const u8,
    exts: []const []const u8,
    language: *const fn () callconv(.c) *const ts.Language,
    query_src: []const u8,
    compiled_query: ?*ts.Query = null,
};

fn grammarSelected(comptime g: grammar_manifest.Grammar) bool {
    return switch (g.tier) {
        .zig => zig_grammar,
        .minimal => minimal_grammars,
        .full => full_grammars,
    };
}

fn specCount() comptime_int {
    var count = 0;
    for (grammar_manifest.all) |g| {
        if (grammarSelected(g)) count += 1;
    }
    return count;
}

// Upstream's typst highlights.scm names its markup honestly —
// @markup.heading.*, @markup.bold, @markup.italic, @markup.raw.block — and
// `synFor` now understands that vocabulary, so headings, bold, italics and raw
// blocks paint on their own. What is left here is exactly the two captures we
// refuse to map globally:
//
//   - upstream tags call callees @function/@function.method; mapping "function"
//     in `synFor` would recolour every function call in every language.
//   - upstream tags the code sigil "#" @operator, and mapping "operator" would
//     likewise light up every +, -, == in the codebase.
//
// Both are worth colouring *in typst specifically*: the sigil in front of every
// #let/#if/#import/#call is the visual anchor of the code/markup split, and
// upstream leaves it uncoloured even though the keyword behind it is not.
const typst_supplement =
    \\
    \\(call item: (ident) @keyword)
    \\(call item: (field field: (ident) @keyword))
    \\"#" @keyword
    \\
;

fn querySrc(comptime g: grammar_manifest.Grammar) []const u8 {
    const base = @field(ts_queries, g.name ++ "_highlights");
    if (comptime std.mem.eql(u8, g.name, "typst")) return base ++ typst_supplement;
    return base;
}

fn initSpecs() [specCount()]Spec {
    var out: [specCount()]Spec = undefined;
    var i = 0;
    inline for (grammar_manifest.all) |g| {
        if (grammarSelected(g)) {
            out[i] = .{
                .name = g.name,
                .exts = g.exts,
                .language = @extern(LanguageFn, .{ .name = "tree_sitter_" ++ g.name }),
                .query_src = querySrc(g),
            };
            i += 1;
        }
    }
    return out;
}

var specs = initSpecs();

const allocation_header_size = 16;
var syntax_allocator: std.mem.Allocator = undefined;
var syntax_started = false;

extern fn ts_set_allocator(
    new_malloc: ?*const fn (size: usize) callconv(.c) ?*anyopaque,
    new_calloc: ?*const fn (nmemb: usize, size: usize) callconv(.c) ?*anyopaque,
    new_realloc: ?*const fn (ptr: ?*anyopaque, size: usize) callconv(.c) ?*anyopaque,
    new_free: ?*const fn (ptr: ?*anyopaque) callconv(.c) void,
) void;

fn syntaxAlloc(size_arg: usize) callconv(.c) ?*anyopaque {
    const size = @max(size_arg, 1);
    const total = std.math.add(usize, allocation_header_size, size) catch return null;
    const bytes = syntax_allocator.alignedAlloc(u8, .@"16", total) catch return null;
    const header: *align(16) usize = @ptrCast(bytes.ptr);
    header.* = total;
    return @ptrCast(bytes.ptr + allocation_header_size);
}

fn syntaxCalloc(count: usize, size: usize) callconv(.c) ?*anyopaque {
    const len = std.math.mul(usize, count, size) catch return null;
    const pointer = syntaxAlloc(len) orelse return null;
    const bytes: [*]u8 = @ptrCast(pointer);
    @memset(bytes[0..len], 0);
    return pointer;
}

fn syntaxRealloc(ptr: ?*anyopaque, new_size: usize) callconv(.c) ?*anyopaque {
    const pointer = ptr orelse return syntaxAlloc(new_size);
    if (new_size == 0) {
        syntaxFree(pointer);
        return null;
    }
    const user: [*]u8 = @ptrCast(pointer);
    const header: *align(16) usize = @ptrCast(@alignCast(user - allocation_header_size));
    const old_total = header.*;
    const old_bytes: []align(16) u8 = @as([*]align(16) u8, @ptrCast(header))[0..old_total];
    const new_total = std.math.add(usize, allocation_header_size, new_size) catch return null;
    const new_bytes = syntax_allocator.realloc(old_bytes, new_total) catch return null;
    const new_header: *align(16) usize = @ptrCast(new_bytes.ptr);
    new_header.* = new_total;
    return @ptrCast(new_bytes.ptr + allocation_header_size);
}

fn syntaxFree(ptr: ?*anyopaque) callconv(.c) void {
    const pointer = ptr orelse return;
    const user: [*]u8 = @ptrCast(pointer);
    const header: *align(16) usize = @ptrCast(@alignCast(user - allocation_header_size));
    const total = header.*;
    const bytes: []align(16) u8 = @as([*]align(16) u8, @ptrCast(header))[0..total];
    syntax_allocator.free(bytes);
}

pub fn start(gpa: std.mem.Allocator) void {
    if (comptime enabled) {
        std.debug.assert(!syntax_started);
        syntax_allocator = gpa;
        syntax_started = true;
        ts_set_allocator(syntaxAlloc, syntaxCalloc, syntaxRealloc, syntaxFree);
    }
}

pub fn stop() void {
    if (comptime enabled) {
        std.debug.assert(syntax_started);
        for (&specs) |*spec| {
            if (spec.compiled_query) |query| query.destroy();
            spec.compiled_query = null;
        }
        ts_set_allocator(null, null, null, null);
        syntax_started = false;
        syntax_allocator = undefined;
    }
}

const Selected = struct { name: []const u8, lang: *const ts.Language, query: *ts.Query };

// NOTE: don't lang.destroy() — the tree_sitter_*() languages are static
// singletons reused on every open; destroying one use-after-frees the next.
fn ensure(spec: *Spec) !Selected {
    const lang = spec.language();
    if (spec.compiled_query) |query| return .{ .name = spec.name, .lang = lang, .query = query };
    var error_offset: u32 = 0;
    const query = try ts.Query.create(lang, spec.query_src, &error_offset);
    spec.compiled_query = query;
    return .{ .name = spec.name, .lang = lang, .query = query };
}

fn forExt(ext: []const u8) !?Selected {
    for (&specs) |*spec| {
        for (spec.exts) |choice| {
            if (std.ascii.eqlIgnoreCase(ext, choice)) return try ensure(spec);
        }
    }
    return null;
}

const LangAlias = struct { []const u8, []const u8 };
const lang_aliases = [_]LangAlias{
    .{ "js", "javascript" },
    .{ "jsx", "javascript" },
    .{ "mjs", "javascript" },
    .{ "py", "python" },
    .{ "sh", "bash" },
    .{ "shell", "bash" },
    .{ "zsh", "bash" },
    .{ "bash", "bash" },
    .{ "rs", "rust" },
    .{ "c++", "cpp" },
    .{ "cxx", "cpp" },
    .{ "cc", "cpp" },
    .{ "cs", "c_sharp" },
    .{ "kt", "kotlin" },
    .{ "rb", "ruby" },
    .{ "ml", "ocaml" },
    .{ "hs", "haskell" },
};

fn forLang(name: []const u8) !?Selected {
    var canonical = name;
    for (lang_aliases) |a| {
        if (std.ascii.eqlIgnoreCase(name, a[0])) {
            canonical = a[1];
            break;
        }
    }
    for (&specs) |*spec| {
        if (std.ascii.eqlIgnoreCase(canonical, spec.name)) return try ensure(spec);
    }
    return null;
}

fn runQuery(styles: []u8, sel: Selected, tree: *ts.Tree, base: usize) void {
    const cursor = ts.QueryCursor.create();
    defer cursor.destroy();
    cursor.exec(sel.query, tree.rootNode());
    while (cursor.nextMatch()) |match| {
        for (match.captures) |cap| {
            const syn = synFor(sel.query.captureNameForId(cap.index) orelse "");
            if (syn == .none) continue;
            var b: usize = base + cap.node.startByte();
            const end = @min(base + @as(usize, cap.node.endByte()), styles.len);
            while (b < end) : (b += 1) styles[b] = @intFromEnum(syn);
        }
    }
}

// Queries compile on first use (`ensure`), never at startup. Pre-compiling the
// compact tier in Pardes.init cost EVERY boot ~120ms of ts_query__perform_analysis
// (55% of a Debug startup) to save ~40ms on the first .zig/.c/.cpp open — a pane
// of prose or a shell paid for a language it never opened. Grammar availability
// is unchanged; only the timing moved.

fn synFor(name: []const u8) Syn {
    for ([_]struct { []const u8, Syn }{
        .{ "comment", .comment },
        .{ "string", .string },
        .{ "character", .string },
        .{ "number", .number },
        .{ "numeric", .number },
        .{ "float", .number },
        .{ "boolean", .number },
        .{ "keyword", .keyword },
        .{ "include", .keyword },
        .{ "conditional", .keyword },
        .{ "repeat", .keyword },
        .{ "title", .keyword },
        .{ "uri", .string },
        .{ "reference", .number },

        // Markup grammars (markdown, typst) name prose constructs in their own
        // vocabulary rather than the code vocabulary above, so none of the
        // needles so far reach them. These are appended, and first-match-wins
        // makes that strictly additive; the needles below were audited across
        // all 27 shipped queries and occur only in the markdown and typst ones,
        // so no other language is recoloured.
        //
        // The slot assignment is forced by the palette being four wide and by
        // `synStyle` attaching the real BOLD attribute to exactly two of them,
        // `keyword` and `comment`: headings take `keyword`, so bold spans have
        // to land on `comment` to render actually bold. Nothing is left that
        // renders italic, so emphasis can only get a colour shift (`number`).
        .{ "heading", .keyword },
        .{ "strong", .comment },
        .{ "bold", .comment },
        .{ "emphasis", .number },
        .{ "italic", .number },
        .{ "literal", .string },
        .{ "raw", .string },
    }) |m| {
        if (std.mem.indexOf(u8, name, m[0]) != null) return m[1];
    }
    return .none;
}

/// One Syn byte per content byte in [start, end). Caller frees.
pub fn highlightFileRange(gpa: std.mem.Allocator, path: []const u8, content: []const u8, start_byte_raw: usize, end_byte_raw: usize) ![]u8 {
    const tz = tracy.zone(@src(), "highlightFileRange");
    defer tz.end();
    if (!enabled) return &.{};
    const ext = std.fs.path.extension(path);
    const selected = (forExt(ext) catch return &.{}) orelse return &.{};

    const start_byte = @min(start_byte_raw, content.len);
    const end_byte = @max(start_byte, @min(end_byte_raw, content.len));
    const source = content[start_byte..end_byte];
    const styles = try gpa.alloc(u8, source.len);
    errdefer gpa.free(styles);
    @memset(styles, 0);

    const parser = ts.Parser.create();
    defer parser.destroy();
    parser.setLanguage(selected.lang) catch return styles;
    const tree = parser.parseString(source, null) orelse return styles;
    defer tree.destroy();

    runQuery(styles, selected, tree, 0);

    if (InjectSite.forGrammar(selected.name)) |site| injectCodeBlocks(styles, source, tree.rootNode(), site);
    // Disjoint from the fenced-block pass above: `code_fence_content` is never
    // an `inline` node, so the two never write the same byte.
    if (std.mem.eql(u8, selected.name, "markdown")) injectMarkdownInline(styles, source, tree.rootNode());
    return styles;
}

// Markdown fenced blocks and Typst raw blocks are the same construct — a
// language tag plus a literal payload — under different node shapes, so one
// walker drives both and only the (lang, content) extraction differs.
const InjectSite = enum {
    markdown_fence,
    typst_raw,

    fn forGrammar(name: []const u8) ?InjectSite {
        if (std.mem.eql(u8, name, "markdown")) return .markdown_fence;
        if (std.mem.eql(u8, name, "typst")) return .typst_raw;
        return null;
    }

    fn blockKind(self: InjectSite) []const u8 {
        return switch (self) {
            .markdown_fence => "fenced_code_block",
            .typst_raw => "raw_blck",
        };
    }

    /// null when the block carries no language tag (an untagged fence, or a
    /// Typst raw block written without one) — nothing to inject, leave it alone.
    fn parts(self: InjectSite, block: ts.Node) ?struct { lang: ts.Node, content: ts.Node } {
        switch (self) {
            .markdown_fence => {
                const info = childOfKind(block, "info_string") orelse return null;
                return .{
                    .lang = childOfKind(info, "language") orelse return null,
                    .content = childOfKind(block, "code_fence_content") orelse return null,
                };
            },
            .typst_raw => return .{
                .lang = block.childByFieldName("lang") orelse return null,
                .content = childOfKind(block, "blob") orelse return null,
            },
        }
    }
};

fn injectCodeBlocks(styles: []u8, source: []const u8, node: ts.Node, site: InjectSite) void {
    if (std.mem.eql(u8, node.kind(), site.blockKind())) {
        highlightCodeBlock(styles, source, node, site);
        return;
    }
    var i: u32 = 0;
    const count = node.childCount();
    while (i < count) : (i += 1) {
        if (node.child(i)) |c| injectCodeBlocks(styles, source, c, site);
    }
}

fn childOfKind(node: ts.Node, kind: []const u8) ?ts.Node {
    var i: u32 = 0;
    const count = node.childCount();
    while (i < count) : (i += 1) {
        if (node.child(i)) |c| {
            if (std.mem.eql(u8, c.kind(), kind)) return c;
        }
    }
    return null;
}

fn highlightCodeBlock(styles: []u8, source: []const u8, block: ts.Node, site: InjectSite) void {
    const p = site.parts(block) orelse return;
    const langtext = source[p.lang.startByte()..p.lang.endByte()];
    const sub_sel = (forLang(langtext) catch return) orelse return;
    const cs: usize = p.content.startByte();
    const ce: usize = p.content.endByte();
    if (ce > source.len or cs > ce) return;

    const parser = ts.Parser.create();
    defer parser.destroy();
    parser.setLanguage(sub_sel.lang) catch return;
    const tree = parser.parseString(source[cs..ce], null) orelse return;
    defer tree.destroy();
    // The outer grammar already painted these bytes (Typst blankets the whole
    // raw block `string`), and runQuery only writes bytes it captures, so the
    // outer colour would survive as a wash behind the injected code. The sub
    // grammar owns the payload outright: clear it first.
    @memset(styles[cs..ce], @intFromEnum(Syn.none));
    runQuery(styles, sub_sel, tree, cs);
}

// tree-sitter-markdown is a split grammar: the block parser bottoms out at named
// `inline` nodes whose bytes it never looks inside, and emphasis /
// strong_emphasis / code_span exist only in the companion inline parser. So
// every `inline` node is re-parsed with `markdown_inline`, which is what
// upstream's injections.scm, helix and nvim all do (per node, uncombined — the
// stray block_continuation markers inside a multi-line paragraph's range are
// just plain text to the inline parser).
//
// The grammar is resolved and the parser built once per file, not once per node:
// only the parse is inherently per node.
fn injectMarkdownInline(styles: []u8, source: []const u8, root: ts.Node) void {
    // Absent in builds below the `full` tier — nothing to inject, leave the
    // block grammar's colours alone.
    const sub_sel = (forLang("markdown_inline") catch return) orelse return;
    const parser = ts.Parser.create();
    defer parser.destroy();
    parser.setLanguage(sub_sel.lang) catch return;
    inlineNodes(styles, source, root, sub_sel, parser);
}

fn inlineNodes(styles: []u8, source: []const u8, node: ts.Node, sel: Selected, parser: *ts.Parser) void {
    if (std.mem.eql(u8, node.kind(), "inline")) {
        highlightInline(styles, source, node, sel, parser);
        return; // `inline` nodes never nest
    }
    var i: u32 = 0;
    const count = node.childCount();
    while (i < count) : (i += 1) {
        if (node.child(i)) |c| inlineNodes(styles, source, c, sel, parser);
    }
}

fn highlightInline(styles: []u8, source: []const u8, node: ts.Node, sel: Selected, parser: *ts.Parser) void {
    const s: usize = node.startByte();
    const e: usize = node.endByte();
    if (s >= e or e > source.len) return; // an empty atx heading has a zero-length inline
    const tree = parser.parseString(source[s..e], null) orelse return;
    defer tree.destroy();
    // Deliberately NOT clearing the range first, unlike highlightCodeBlock: the
    // block query already painted a heading's inline text `keyword`, and that is
    // the colour the heading must keep wherever the inline pass captures
    // nothing. Painting over instead of resetting is what makes *italic* inside
    // a heading recolour while the rest of the heading stays heading-coloured.
    runQuery(styles, sel, tree, s);
}

/// One Syn byte per byte of content[start, end) for unified diffs/patches.
/// Pure byte scan; independent of tree-sitter and the `enabled` flag. Caller frees.
pub fn highlightDiff(gpa: std.mem.Allocator, content: []const u8, start_byte_raw: usize, end_byte_raw: usize) ![]u8 {
    const start_byte = @min(start_byte_raw, content.len);
    const end_byte = @max(start_byte, @min(end_byte_raw, content.len));
    const source = content[start_byte..end_byte];
    const styles = try gpa.alloc(u8, source.len);
    errdefer gpa.free(styles);
    @memset(styles, 0);

    var offset: usize = 0;
    var lines = std.mem.splitScalar(u8, source, '\n');
    while (lines.next()) |line| {
        const syn = diffLineSyn(line);
        if (syn != .none) @memset(styles[offset .. offset + line.len], @intFromEnum(syn));
        offset += line.len + 1;
    }
    return styles;
}

fn diffLineSyn(line: []const u8) Syn {
    if (std.mem.startsWith(u8, line, "@@")) return .keyword;
    if (std.mem.startsWith(u8, line, "+++") or
        std.mem.startsWith(u8, line, "---") or
        std.mem.startsWith(u8, line, "diff ") or
        std.mem.startsWith(u8, line, "index ") or
        std.mem.startsWith(u8, line, "\\ No newline")) return .comment;
    if (line.len == 0) return .none;
    if (line[0] == '+') return .string;
    if (line[0] == '-') return .number;
    return .none;
}

test "tree-sitter allocator callbacks preserve and free exact allocations" {
    syntax_allocator = std.testing.allocator;
    defer syntax_allocator = undefined;

    var live: ?*anyopaque = syntaxCalloc(4, 1) orelse return error.OutOfMemory;
    defer if (live) |pointer| syntaxFree(pointer);
    const original: [*]u8 = @ptrCast(live.?);
    try std.testing.expectEqualSlices(u8, &.{ 0, 0, 0, 0 }, original[0..4]);
    @memcpy(original[0..4], "data");

    live = syntaxRealloc(live, 32) orelse return error.OutOfMemory;
    const grown: [*]u8 = @ptrCast(live.?);
    try std.testing.expectEqualSlices(u8, "data", grown[0..4]);

    try std.testing.expect(syntaxCalloc(std.math.maxInt(usize), 2) == null);
    try std.testing.expect(syntaxRealloc(live, 0) == null);
    live = null;
}

test "default full grammar set highlights Typst source" {
    if (!enabled or !full_grammars) return;
    start(std.testing.allocator);
    defer stop();

    const source = "// note\n#let answer = 42\n#let text = \"hello\"\n";
    const styles = try highlightFileRange(std.testing.allocator, "paper.typst", source, 0, source.len);
    defer std.testing.allocator.free(styles);

    const comment_at = std.mem.indexOf(u8, source, "// note").?;
    const keyword_at = std.mem.indexOf(u8, source, "let").?;
    const number_at = std.mem.indexOf(u8, source, "42").?;
    const string_at = std.mem.indexOf(u8, source, "\"hello\"").?;
    try std.testing.expectEqual(Syn.comment, @as(Syn, @enumFromInt(styles[comment_at])));
    try std.testing.expectEqual(Syn.keyword, @as(Syn, @enumFromInt(styles[keyword_at])));
    try std.testing.expectEqual(Syn.number, @as(Syn, @enumFromInt(styles[number_at])));
    try std.testing.expectEqual(Syn.string, @as(Syn, @enumFromInt(styles[string_at])));

    const short_ext = try highlightFileRange(std.testing.allocator, "paper.typ", source, 0, source.len);
    defer std.testing.allocator.free(short_ext);
    try std.testing.expectEqual(Syn.keyword, @as(Syn, @enumFromInt(short_ext[keyword_at])));
}

test "Typst markup constructs paint and raw blocks inject their language" {
    if (!enabled or !full_grammars) return;
    start(std.testing.allocator);
    defer stop();

    const source =
        \\// note
        \\= Heading Title
        \\Some *bold* text with `raw` inline.
        \\#import "mod.typ": helper
        \\#let answer = 42
        \\#emit(7, "arg")
        \\```zig
        \\fn widget() u8 { return 99; }
        \\```
        \\
    ;
    const styles = try highlightFileRange(std.testing.allocator, "paper.typ", source, 0, source.len);
    defer std.testing.allocator.free(styles);

    const synAt = struct {
        fn f(s: []const u8, src: []const u8, needle: []const u8, offset: usize) Syn {
            return @enumFromInt(s[std.mem.indexOf(u8, src, needle).? + offset]);
        }
    }.f;

    // marker and text of the heading both take the bold accent
    try std.testing.expectEqual(Syn.keyword, synAt(styles, source, "= Heading", 0));
    try std.testing.expectEqual(Syn.keyword, synAt(styles, source, "Heading", 0));
    // *bold* is @markup.bold, which lands on the one slot that still renders
    // with the real bold attribute now that headings own `keyword`.
    try std.testing.expectEqual(Syn.comment, synAt(styles, source, "bold", 0));
    try std.testing.expectEqual(Syn.string, synAt(styles, source, "raw` inline", 0));
    // the callee and the code sigil in front of it
    try std.testing.expectEqual(Syn.keyword, synAt(styles, source, "emit", 0));
    try std.testing.expectEqual(Syn.keyword, synAt(styles, source, "#emit", 0));

    // fence and lang tag keep the literal colour of the raw block...
    try std.testing.expectEqual(Syn.string, synAt(styles, source, "```zig", 0));
    try std.testing.expectEqual(Syn.string, synAt(styles, source, "```zig", 3));
    // ...while the blob is reset and re-painted by the injected zig grammar. Its
    // `fn` and `99` prove the injection ran; `widget` proves the reset, since the
    // zig query names it @function (Syn.none here) and without clearing the blob
    // first the raw block's `string` would still be washing over it.
    try std.testing.expectEqual(Syn.keyword, synAt(styles, source, "fn widget", 0));
    try std.testing.expectEqual(Syn.number, synAt(styles, source, "99", 0));
    try std.testing.expectEqual(Syn.none, synAt(styles, source, "widget", 0));

    try std.testing.expectEqual(Syn.comment, synAt(styles, source, "// note", 0));
    try std.testing.expectEqual(Syn.keyword, synAt(styles, source, "import", 0));
    try std.testing.expectEqual(Syn.keyword, synAt(styles, source, "let", 0));
    try std.testing.expectEqual(Syn.number, synAt(styles, source, "42", 0));
    try std.testing.expectEqual(Syn.string, synAt(styles, source, "\"arg\"", 0));
}

test "markdown highlights markup, injects inline spans and fenced code blocks" {
    if (!enabled or !full_grammars) return;
    start(std.testing.allocator);
    defer stop();

    const source =
        "# Heading *slant* Title\n" ++
        "\n" ++
        "Prose with **stout** and *lean* plus `snippet` inline.\n" ++
        "\n" ++
        "```zig\n" ++
        "fn gadget() u8 { return 77; }\n" ++
        "```\n";
    const styles = try highlightFileRange(std.testing.allocator, "doc.md", source, 0, source.len);
    defer std.testing.allocator.free(styles);

    const synAt = struct {
        fn f(s: []const u8, src: []const u8, needle: []const u8, offset: usize) Syn {
            return @enumFromInt(s[std.mem.indexOf(u8, src, needle).? + offset]);
        }
    }.f;

    // The block grammar washes the whole heading `keyword`; the inline pass then
    // paints the emphasis over it without resetting, so the heading keeps its
    // colour everywhere the emphasis is not.
    try std.testing.expectEqual(Syn.keyword, synAt(styles, source, "Title", 0));
    try std.testing.expectEqual(Syn.number, synAt(styles, source, "slant", 0));

    // bold/italic/code-span live only in the inline grammar, so all three prove
    // the inline injection ran.
    try std.testing.expectEqual(Syn.comment, synAt(styles, source, "stout", 0));
    try std.testing.expectEqual(Syn.number, synAt(styles, source, "lean", 0));
    try std.testing.expectEqual(Syn.string, synAt(styles, source, "snippet", 0));

    // the fence is inside the block query's @text.literal wash...
    try std.testing.expectEqual(Syn.string, synAt(styles, source, "```zig", 0));
    // ...while the payload is cleared and re-painted by the injected zig
    // grammar: `fn` and `77` prove the injection ran, and `gadget` proves the
    // clear, since the zig query names it @function (Syn.none here) and without
    // clearing first the fence's `string` would still be washing over it.
    try std.testing.expectEqual(Syn.keyword, synAt(styles, source, "fn gadget", 0));
    try std.testing.expectEqual(Syn.number, synAt(styles, source, "77", 0));
    try std.testing.expectEqual(Syn.none, synAt(styles, source, "gadget", 0));
}

test "highlightDiff colors unified diff lines by prefix" {
    const diff =
        "diff --git a/x b/x\n" ++
        "--- a/x\n" ++
        "+++ b/x\n" ++
        "@@ -1,3 +1,3 @@\n" ++
        " context\n" ++
        "-old line\n" ++
        "+new line\n";
    const styles = try highlightDiff(std.testing.allocator, diff, 0, diff.len);
    defer std.testing.allocator.free(styles);

    const byteSyn = struct {
        fn at(s: []const u8, src: []const u8, needle: []const u8) Syn {
            const i = std.mem.indexOf(u8, src, needle).?;
            return @enumFromInt(s[i]);
        }
    }.at;

    try std.testing.expectEqual(Syn.comment, byteSyn(styles, diff, "diff --git"));
    try std.testing.expectEqual(Syn.comment, byteSyn(styles, diff, "--- a/x"));
    try std.testing.expectEqual(Syn.comment, byteSyn(styles, diff, "+++ b/x"));
    try std.testing.expectEqual(Syn.keyword, byteSyn(styles, diff, "@@ -1,3"));
    try std.testing.expectEqual(Syn.none, byteSyn(styles, diff, " context"));
    try std.testing.expectEqual(Syn.number, byteSyn(styles, diff, "-old line"));
    try std.testing.expectEqual(Syn.string, byteSyn(styles, diff, "+new line"));
}