From 383b60b79146937e12c82abaaca2ce213d210b7f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 22:43:18 -0300 Subject: syntax: the highlight queries are filtered by the build, not in every compile: colorQuery kept each grammar's patterns whose captures pardes colours by scanning the 29 highlights.scm byte by byte at comptime, a quarter of a full recompile of the core (and again in each of the ~15 artifacts that import it); build.zig's treeSitter now runs the same filter once over the query it reads (grammar_manifest.colorQuery, a plain function, with Syn and synFor's capture-to-class table beside it, the one copy both the build and syntax.zig read), so ts_queries' _highlights is the filtered query and Spec.query_src a field read; typst's supplement is the manifest's highlights_extra, appended before the filter as before; _highlights_unfiltered is the whole query, for the test that the filter keeps every colour; a package that depends on pardes gets the same through library's treeSitter, for its own grammar set; proven byte-identical before the comptime path went (29 grammars, 28,398 filtered bytes of 79,678); core-test's forced full recompile 12.7-12.9 s wall / 17.5 s CPU to 9.7-10.1 s / 14.6 s; syntax snapshots unchanged; the changelog says so --- src/syntax.zig | 114 +++------------------------------------------------------ 1 file changed, 5 insertions(+), 109 deletions(-) (limited to 'src/syntax.zig') diff --git a/src/syntax.zig b/src/syntax.zig index 2526e113..8533aaa9 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -22,7 +22,7 @@ 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 }; +pub const Syn = grammar_manifest.Syn; /// A query compiled when first asked for: "" for none, null when it fails. const Lazy = struct { src: []const u8 = "", query: ?*ts.Query = null, failed: bool = false }; @@ -55,81 +55,6 @@ fn specCount() comptime_int { } return count; } -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"); - const source = if (comptime std.mem.eql(u8, g.name, "typst")) base ++ typst_supplement else base; - return comptime colorQuery(source); -} - -fn colorQuery(comptime source: []const u8) []const u8 { - @setEvalBranchQuota(2_000_000); - var result: [source.len]u8 = undefined; - var written: usize = 0; - var at: usize = 0; - while (at < source.len) { - while (at < source.len) { - if (std.ascii.isWhitespace(source[at])) { - at += 1; - } else if (source[at] == ';') { - while (at < source.len and source[at] != '\n') at += 1; - } else break; - } - const begin = at; - var depth: usize = 0; - var colored = false; - var complete = false; - while (at < source.len) { - const byte = source[at]; - if (complete and (byte == '(' or byte == '[' or byte == '"')) break; - if (byte == ';') { - while (at < source.len and source[at] != '\n') at += 1; - continue; - } - if (byte == '"') { - at += 1; - while (at < source.len) : (at += 1) { - if (source[at] == '\\') { - at += 1; - } else if (source[at] == '"') { - at += 1; - break; - } - } - if (depth == 0) complete = true; - continue; - } - if (byte == '(' or byte == '[') depth += 1; - if (byte == ')' or byte == ']') { - depth -= 1; - if (depth == 0) complete = true; - } - if (byte == '@') { - const name = at + 1; - at = name; - while (at < source.len and (std.ascii.isAlphanumeric(source[at]) or - source[at] == '_' or source[at] == '.' or source[at] == '-')) at += 1; - colored = colored or synFor(source[name..at]) != .none; - continue; - } - at += 1; - } - if (colored) { - @memcpy(result[written..][0 .. at - begin], source[begin..at]); - written += at - begin; - } - } - const filtered = result[0..written].*; - return &filtered; -} - fn initSpecs() [specCount()]Spec { var out: [specCount()]Spec = undefined; var i = 0; @@ -139,7 +64,7 @@ fn initSpecs() [specCount()]Spec { .name = g.name, .exts = g.exts, .language = @extern(LanguageFn, .{ .name = "tree_sitter_" ++ g.name }), - .query_src = querySrc(g), + .query_src = @field(ts_queries, g.name ++ "_highlights"), .objects = .{ .src = @field(ts_queries, g.name ++ "_textobjects") }, .indents = .{ .src = @field(ts_queries, g.name ++ "_indents") }, }; @@ -219,7 +144,7 @@ fn ensure(spec: *Spec) !Selected { const capture_styles = spec.capture_styles[0..query.captureCount()]; for (capture_styles, 0..) |*style, id| { const name = query.captureNameForId(@intCast(id)) orelse ""; - style.* = @backingInt(synFor(name)); + style.* = @backingInt(grammar_manifest.synFor(name)); if (style.* == 0) query.disableCapture(name); } const parser = ts.Parser.create(); @@ -260,34 +185,6 @@ fn runQuery(styles: []u8, sel: Selected, tree: *ts.Tree, base: usize) void { } } -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 }, - .{ "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; -} pub fn highlightFileRange(gpa: std.mem.Allocator, path: []const u8, content: []const u8, start_raw: usize, end_raw: usize) ![]u8 { const zone = tracy.zone(@src(), "highlightFileRange"); defer zone.end(); @@ -1909,8 +1806,7 @@ test "syntax query filtering preserves upstream colors" { inline for (grammar_manifest.all) |grammar| { if (comptime grammarSelected(grammar)) { const selected = (try forLang(grammar.name)).?; - const raw_source = @field(ts_queries, grammar.name ++ "_highlights") ++ - (if (comptime std.mem.eql(u8, grammar.name, "typst")) typst_supplement else ""); + const raw_source = @field(ts_queries, grammar.name ++ "_highlights_unfiltered"); var error_offset: u32 = 0; const raw_query = try ts.Query.create(selected.lang, raw_source, &error_offset); defer raw_query.destroy(); @@ -1918,7 +1814,7 @@ test "syntax query filtering preserves upstream colors" { reference.query = raw_query; reference.capture_styles = try std.testing.allocator.alloc(u8, raw_query.captureCount()); defer std.testing.allocator.free(reference.capture_styles); - for (reference.capture_styles, 0..) |*style, id| style.* = @backingInt(synFor(raw_query.captureNameForId(@intCast(id)) orelse "")); + for (reference.capture_styles, 0..) |*style, id| style.* = @backingInt(grammar_manifest.synFor(raw_query.captureNameForId(@intCast(id)) orelse "")); const tree = selected.parser.parseString(source, null) orelse return error.ParseFailed; defer tree.destroy(); var expected: [source.len]u8 = @splat(0); -- cgit v1.3 From c50226bdad46ed69643079e7c6f3e8ade3a034a1 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:05:09 -0300 Subject: colors: a style byte's four high bits are grounds a theme resolves, not a diff's: syntax.ground_add and ground_del (a row's hue), ground_emph (the stronger ground), ground_line (the faint one), the byte as it was; DiffTints moves from File.zig to colors.zig as Grounds (an add and a del Ground: row, emph, line, ink), the same Oklab mixes and marker contrast; the painter has one rule, a row's first byte says its ground (emph the stronger to the pane's edge, line the faint, neither none, its marker in the hue) and a later byte's emph is its cell's, so it stops asking the pane's colour algorithm (`row_tint = algo == .diff`) and works the grounds out once a frame only for a row that names a hue; highlightDiff marks an added or removed line's first byte emph, the strong row a .diff's rows always had; recolorDiffOutput reads the same Grounds; goldens unchanged (diffview, pagercolor, diffstack), render of the 50k-line file 11,723,421 user instructions a frame before and 11,724,208 after (+0.007%, perf stat over ReleaseFast pardes-perf, 210 frames less 10) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/File.zig | 97 +++++++++++++--------------------------------------------- src/colors.zig | 37 ++++++++++++++++++++++ src/hunks.zig | 24 +++++++-------- src/syntax.zig | 57 +++++++++++++++++++--------------- 4 files changed, 102 insertions(+), 113 deletions(-) (limited to 'src/syntax.zig') diff --git a/src/File.zig b/src/File.zig index 5150f1cd..1988fb89 100644 --- a/src/File.zig +++ b/src/File.zig @@ -1835,59 +1835,6 @@ fn fillBody(dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) const SynStyle = struct { fg: [3]u8, bold: bool }; -/// How added and removed diff lines are told apart in every theme: a row -/// ground a little way from the page toward green or red (the theme's own -/// ANSI green and red where it has a palette), and the prefix column in -/// that hue, pushed toward the ink until it reads on that ground. The -/// syntax colours are drawn over the ground unchanged. -pub const DiffTints = struct { - added_bg: [3]u8, - removed_bg: [3]u8, - /// The words a changed line changed (hunks.zig), on a stronger ground. - added_emph_bg: [3]u8, - removed_emph_bg: [3]u8, - /// A +Diff line added or removed whole: a faint ground. - added_line_bg: [3]u8, - removed_line_bg: [3]u8, - added_fg: [3]u8, - removed_fg: [3]u8, - - pub fn of(p: *Pardes) DiffTints { - const th = p.theme(); - const page = th.bg orelse th.tag_bg; - const ink = th.fg orelse th.tag_fg; - const green: [3]u8 = if (th.palette) |pal| pal[2] else .{ 0x2e, 0xa0, 0x43 }; - const red: [3]u8 = if (th.palette) |pal| pal[1] else .{ 0xcf, 0x22, 0x2e }; - const added_bg = Colors.mixOklab(page, green, 0.18); - const removed_bg = Colors.mixOklab(page, red, 0.18); - return .{ - .added_bg = added_bg, - .removed_bg = removed_bg, - .added_emph_bg = Colors.mixOklab(page, green, 0.28), - .removed_emph_bg = Colors.mixOklab(page, red, 0.28), - .added_line_bg = Colors.mixOklab(page, green, 0.07), - .removed_line_bg = Colors.mixOklab(page, red, 0.07), - .added_fg = legible(green, ink, added_bg), - .removed_fg = legible(red, ink, removed_bg), - }; - } - - fn legible(hue: [3]u8, ink: [3]u8, ground: [3]u8) [3]u8 { - var fg = hue; - var step: usize = 0; - while (step < 4 and Colors.contrast(fg, ground) < 3) : (step += 1) fg = Colors.mixOklab(fg, ink, 0.4); - return fg; - } - - /// The row ground and prefix ink for a line's style byte, if it is an - /// added or removed line. - pub fn forStyle(t: *const DiffTints, style: u8) ?struct { bg: [3]u8, fg: [3]u8, emph_bg: [3]u8, line_bg: [3]u8 } { - if (style & syntax.diff_added != 0) return .{ .bg = t.added_bg, .fg = t.added_fg, .emph_bg = t.added_emph_bg, .line_bg = t.added_line_bg }; - if (style & syntax.diff_removed != 0) return .{ .bg = t.removed_bg, .fg = t.removed_fg, .emph_bg = t.removed_emph_bg, .line_bg = t.removed_line_bg }; - return null; - } -}; - fn synStyle(p: *Pardes, sy: syntax.Syn) ?SynStyle { return .{ .fg = Colors.synInk(p.theme(), sy) orelse return null, .bold = false }; } @@ -1902,12 +1849,9 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. const prefix_width = gutterWidth(pane); const tz_recolor = tracy.zone(@src(), "synRecolor"); defer tz_recolor.end(); - // A diff's rows, and a +Diff's (hunks.zig): their markers coloured. - // A diff's whole row is tinted; a +Diff tints only the words a - // changed line changed. - const algo = pane.colorAlgo(); - const tints: ?DiffTints = if ((algo == .diff or algo == .locations) and highlights.len > 0) DiffTints.of(p) else null; - const row_tint = algo == .diff; + // A row whose first byte names a hue (syntax.ground_add, _del): its + // grounds, the theme's, worked out once a frame and only for one. + var grounds: ?Colors.Grounds = null; var filtered_storage: terminal.FilteredColors = undefined; const filtered: ?*terminal.FilteredColors = if (pane.tty_filter and spans.len > 0) blk: { filtered_storage = .init(p, null); @@ -1945,19 +1889,20 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. const metadata: ?locations.Row = if (row_index < f.location_rows.len) f.location_rows[row_index] else null; const decoration = Output.decorateRow(f.output, line, metadata); const chrome: ?*const pardes.ChromeTheme = if (decoration.plain()) null else p.bodyChrome(); - // An added or removed line's row, every wrapped row of it, is tinted. - const tint = if (tints) |*t| if (base >= highlight_start and base - highlight_start < highlights.len) - t.forStyle(highlights[base - highlight_start]) - else - null else null; + // The row's first byte: its hue and its ground, on every wrapped + // row of it (syntax.ground_add). + const lead: u8 = if (base >= highlight_start and base - highlight_start < highlights.len) highlights[base - highlight_start] else 0; + const tint: ?Colors.Grounds.Ground = if (lead & (syntax.ground_add | syntax.ground_del) == 0) null else (grounds orelse ground: { + grounds = .of(p.theme()); + break :ground grounds.?; + }).forStyle(lead); if (tint) |t| if (prefix_width < tw) { - // A +Diff line added or removed whole: faintly (hunks.zig). - const whole = highlights[base - highlight_start] & syntax.diff_whole != 0; - if (row_tint or whole) s.tint(tx + @as(u16, @intCast(prefix_width)), body_y + vr, tw -| @as(u16, @intCast(prefix_width)), if (row_tint) t.bg else t.line_bg, null); + const ground: ?[3]u8 = if (lead & syntax.ground_emph != 0) t.row else if (lead & syntax.ground_line != 0) t.line else null; + if (ground) |g| s.tint(tx + @as(u16, @intCast(prefix_width)), body_y + vr, tw -| @as(u16, @intCast(prefix_width)), g, null); }; - // A +Pager's own colours (pager.zig) on every row but one the diff - // look tints: an added or removed line is drawn as pardes draws a - // diff, and git's red and green on it would only fight the tint. + // A +Pager's own colours (pager.zig) on every row but one in a hue: + // an added or removed line is drawn as pardes draws a diff, and + // git's red and green on it would only fight the tint. var span_at = if (tint != null) spans.len else std.sort.partitionPoint(pardes.pager.Span, spans, base + hs, struct { fn before(at: usize, span: pardes.pager.Span) bool { return span.end <= at; @@ -1991,7 +1936,7 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. const hidx = idx - highlight_start; if (hidx < highlights.len) { if (if (tint != null and hs + c == 0) - SynStyle{ .fg = tint.?.fg, .bold = true } + SynStyle{ .fg = tint.?.ink, .bold = true } else synStyle(p, @fromBackingInt(@intCast(highlights[hidx] & syntax.syn_bits)))) |ss| { @@ -2003,10 +1948,10 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. cell.style.bold = ss.bold; } } - if (tint) |t| if (highlights[hidx] & syntax.diff_emph != 0) { + if (tint) |t| if (highlights[hidx] & syntax.ground_emph != 0 and hs + c > 0) { var fill: usize = 0; while (fill < cells and prefix_width + screen_c + fill < tw) : (fill += 1) - s.at(tx + @as(u16, @intCast(prefix_width + screen_c + fill)), body_y + vr).style.bg = .{ .rgb = t.emph_bg }; + s.at(tx + @as(u16, @intCast(prefix_width + screen_c + fill)), body_y + vr).style.bg = .{ .rgb = t.emph }; }; } } @@ -2175,7 +2120,7 @@ pub fn recolorDiffOutput(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, t } if (lo > hi) return; const styles = syntax.highlightDiffRows(arena, flat, lo, hi, true) catch return; - const tints = DiffTints.of(p); + const tints: Colors.Grounds = .of(p.theme()); const ink = p.theme().fg orelse p.theme().tag_fg; const body_y = p.bodyTop(pane, r); shown = std.mem.splitScalar(u8, body, '\n'); @@ -2184,7 +2129,7 @@ pub fn recolorDiffOutput(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, t const view = maybe orelse continue; const base = @intFromPtr(rows[view.row].ptr) - flat_start - lo; const tint = if (base < styles.len) tints.forStyle(styles[base]) else null; - if (tint) |t| s.tint(tx, body_y + @as(u16, @intCast(vr)), tw, t.bg, null); + if (tint) |t| s.tint(tx, body_y + @as(u16, @intCast(vr)), tw, t.row, null); var c: usize = 0; var screen_c: usize = 0; while (c < line.len and screen_c < tw) { @@ -2196,7 +2141,7 @@ pub fn recolorDiffOutput(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, t // On an added or removed line, what the program coloured red or // green is the tint's to say now: its code takes the page's ink. const paint: ?SynStyle = if (tint != null and prefix) - .{ .fg = tint.?.fg, .bold = true } + .{ .fg = tint.?.ink, .bold = true } else if (synStyle(p, @fromBackingInt(@intCast(style & syntax.syn_bits)))) |ss| ss else if (tint != null) diff --git a/src/colors.zig b/src/colors.zig index 1713ba24..e2e00ea8 100644 --- a/src/colors.zig +++ b/src/colors.zig @@ -322,6 +322,43 @@ pub fn synInk(th: *const Theme, sy: @import("syntax.zig").Syn) ?[3]u8 { }; } +/// The grounds a style byte's high bits name (syntax.ground_add and the +/// rest) in a theme: a row added or removed on a ground a little way from +/// the page toward the theme's green or red (its own ANSI ones where it has +/// a palette), a stronger one, a faint one, and the row's marker in that +/// hue, pushed toward the ink until it reads on the row's ground. Syntax +/// colours are drawn over a ground unchanged. +pub const Grounds = struct { + add: Ground, + del: Ground, + + pub const Ground = struct { row: [3]u8, emph: [3]u8, line: [3]u8, ink: [3]u8 }; + + pub fn of(th: *const Theme) Grounds { + const page = th.bg orelse th.tag_bg; + const ink = th.fg orelse th.tag_fg; + const green: [3]u8 = if (th.palette) |pal| pal[2] else .{ 0x2e, 0xa0, 0x43 }; + const red: [3]u8 = if (th.palette) |pal| pal[1] else .{ 0xcf, 0x22, 0x2e }; + return .{ .add = ground(page, ink, green), .del = ground(page, ink, red) }; + } + + fn ground(page: [3]u8, ink: [3]u8, hue: [3]u8) Ground { + const row = mixOklab(page, hue, 0.18); + var marker = hue; + var step: usize = 0; + while (step < 4 and contrast(marker, row) < 3) : (step += 1) marker = mixOklab(marker, ink, 0.4); + return .{ .row = row, .emph = mixOklab(page, hue, 0.28), .line = mixOklab(page, hue, 0.07), .ink = marker }; + } + + /// The ground of a byte's hue, if it has one. + pub fn forStyle(g: *const Grounds, style: u8) ?Ground { + const syntax = @import("syntax.zig"); + if (style & syntax.ground_add != 0) return g.add; + if (style & syntax.ground_del != 0) return g.del; + return null; + } +}; + /// One family, its twelve backgrounds a theme each (`curated`). const seoul256 = @import("themes/seoul256.zig"); diff --git a/src/hunks.zig b/src/hunks.zig index 8f4b4233..19246392 100644 --- a/src/hunks.zig +++ b/src/hunks.zig @@ -331,10 +331,10 @@ fn flush(out: *Out, hunk: *Hunk, memo: *SourceMemo, dir: []const u8) !void { // has it: the marker coloured, the line as its file's. for (lines) |line| { const flag: u8 = switch (line.kind) { - .added => syntax.diff_added, - .removed => syntax.diff_removed, + .added => syntax.ground_add, + .removed => syntax.ground_del, else => 0, - } | @as(u8, if (line.kind != .context and !line.paired) syntax.diff_whole else 0); + } | @as(u8, if (line.kind != .context and !line.paired) syntax.ground_line else 0); const text = try std.fmt.allocPrint(arena, "{c}{s}", .{ @as(u8, switch (line.kind) { .added => '+', .removed => '-', @@ -389,10 +389,10 @@ fn inferEdits(arena: std.mem.Allocator, minus: []HunkLine, plus: []HunkLine) !vo const marks = annotate(pair, &alignment, m.code, plus[j].code) catch continue; if (marks.distance > max_line_distance) continue; for (marks.minus) |r| for (m.styles[r[0]..r[1]]) |*b| { - b.* |= syntax.diff_emph; + b.* |= syntax.ground_emph; }; for (marks.plus) |r| for (plus[j].styles[r[0]..r[1]]) |*b| { - b.* |= syntax.diff_emph; + b.* |= syntax.ground_emph; }; m.paired = true; plus[j].paired = true; @@ -583,7 +583,7 @@ const testing = std.testing; fn emphasised(line: HunkLine) ![]const u8 { var out: std.ArrayList(u8) = .empty; - for (line.code, line.styles) |c, s| try out.append(testing.allocator, if (s & syntax.diff_emph != 0) c else '.'); + for (line.code, line.styles) |c, s| try out.append(testing.allocator, if (s & syntax.ground_emph != 0) c else '.'); return out.toOwnedSlice(testing.allocator); } @@ -653,13 +653,13 @@ test "a diff stacks to a label a hunk and its lines, markers gone, tinted and st try testing.expectEqual(@as(usize, "nowhere/x.txt:2".len), result.rows[0].location_end); try testing.expect(result.rows[1].declaration); const removed = result.rows[3]; - try testing.expect(removed.colors[0] & syntax.diff_removed != 0); + try testing.expect(removed.colors[0] & syntax.ground_del != 0); // Paired with the line it changed: no whole-line tint. - try testing.expect(removed.colors[0] & syntax.diff_whole == 0); - try testing.expect(removed.colors[1] & syntax.diff_emph != 0); - try testing.expect(removed.colors[5] & syntax.diff_emph == 0); - try testing.expect(result.rows[4].colors[0] & syntax.diff_added != 0); - try testing.expect(result.rows[2].colors[0] & (syntax.diff_added | syntax.diff_removed) == 0); + try testing.expect(removed.colors[0] & syntax.ground_line == 0); + try testing.expect(removed.colors[1] & syntax.ground_emph != 0); + try testing.expect(removed.colors[5] & syntax.ground_emph == 0); + try testing.expect(result.rows[4].colors[0] & syntax.ground_add != 0); + try testing.expect(result.rows[2].colors[0] & (syntax.ground_add | syntax.ground_del) == 0); } test "a hunk the file has moved since is found where it went, inside its declaration" { diff --git a/src/syntax.zig b/src/syntax.zig index 8533aaa9..70399bf7 100644 --- a/src/syntax.zig +++ b/src/syntax.zig @@ -933,16 +933,17 @@ fn childOfKind(node: ts.Node, kind: []const u8) ?ts.Node { return null; } -/// A diff line's style byte carries whether the line was added or removed -/// over its syntax style (the low bits), so the painter can tint its row -/// and mark its prefix; Diff marks the words a changed line changed -/// (hunks.zig), drawn on a stronger tint. -pub const diff_added: u8 = 0x80; -pub const diff_removed: u8 = 0x40; -pub const diff_emph: u8 = 0x20; -/// A +Diff line added or removed whole, paired with no line it changed: -/// its row takes a faint tint, on its marker's byte. -pub const diff_whole: u8 = 0x10; +/// A style byte: a Syn in its low bits (syn_bits), and over them the +/// grounds a theme resolves (colors.Grounds): `ground_add` or `ground_del`, +/// a row added or removed (its hue); `ground_emph`, the stronger ground; +/// `ground_line`, the faint one. A row's first byte says its row's ground: +/// emph the stronger to the pane's edge, line the faint one, neither none +/// (only its marker in the hue). Any later byte's emph is its own cell's +/// ground: the words a changed line changed (hunks.zig). +pub const ground_add: u8 = 0x80; +pub const ground_del: u8 = 0x40; +pub const ground_emph: u8 = 0x20; +pub const ground_line: u8 = 0x10; pub const syn_bits: u8 = 0x0f; /// A file section's hunks are parsed together, each side as one text, in @@ -967,7 +968,8 @@ var diff_clock: u64 = 0; /// the file its section names. A hunk's old side (context and removed /// lines) and new side (context and added lines) are each parsed whole, so /// a string or comment that spans lines colours as it does in the file. -/// Added and removed lines carry `diff_added`/`diff_removed`. A file in no +/// Added and removed lines carry `ground_add`/`ground_del`, the first byte +/// the strong row ground too (`ground_emph`). A file in no /// language pardes knows keeps the plain colours: added lines as strings, /// removed ones as numbers. pub fn highlightDiff(gpa: std.mem.Allocator, content: []const u8, start_raw: usize, end_raw: usize) ![]u8 { @@ -1082,13 +1084,14 @@ fn paintDiffPiece(gpa: std.mem.Allocator, window: DiffWindow, bytes: []const u8, for (kinds) |kind| { const line = lines.next() orelse break; const style: u8 = switch (kind) { - .added => @backingInt(Syn.string) | diff_added, - .removed => @backingInt(Syn.number) | diff_removed, + .added => @backingInt(Syn.string) | ground_add, + .removed => @backingInt(Syn.number) | ground_del, .no_newline => @backingInt(Syn.comment), .hunk => @backingInt(Syn.keyword), else => 0, }; if (style != 0) window.fill(at + offset, line.len, style); + if (style & (ground_add | ground_del) != 0 and line.len > 0) window.fill(at + offset, 1, style | ground_emph); offset += line.len + 1; } } @@ -1170,13 +1173,14 @@ fn paintHunk(gpa: std.mem.Allocator, out: []u8, bytes: []const u8, kinds: []cons if (kind == .context or kind == .removed) old_at += code_len + 1; if (kind == .context or kind == .added) new_at += code_len + 1; const flag: u8 = switch (kind) { - .added => diff_added, - .removed => diff_removed, + .added => ground_add, + .removed => ground_del, else => 0, }; if (flag != 0) for (out[offset..][0..line.len]) |*b| { b.* |= flag; }; + if (flag != 0 and line.len > 0) out[offset] |= ground_emph; } } @@ -1577,9 +1581,12 @@ test "syntax highlightDiff colors unified diff lines by prefix" { // A file in no known language: each line in one colour, and tinted. try std.testing.expectEqual(Syn.number, diffSynAt(styles, diff_text, "-old line", 0)); try std.testing.expectEqual(Syn.string, diffSynAt(styles, diff_text, "+new line", 0)); - try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "-old line").?] & diff_removed != 0); - try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "+new line").? + 4] & diff_added != 0); - try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, " context").?] & (diff_added | diff_removed) == 0); + try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "-old line").?] & ground_del != 0); + try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "+new line").? + 4] & ground_add != 0); + try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, " context").?] & (ground_add | ground_del) == 0); + // The row's ground on its first byte: the strong one, its other bytes none. + try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "-old line").?] & ground_emph != 0); + try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "+new line").? + 4] & (ground_emph | ground_line) == 0); } test "syntax a Zig hunk is coloured as Zig, each side parsed whole" { @@ -1611,9 +1618,9 @@ test "syntax a Zig hunk is coloured as Zig, each side parsed whole" { try std.testing.expectEqual(Syn.string, diffSynAt(styles, diff_text, "new words defer", 10)); // The prefix is no code: it carries the tint alone. const added = std.mem.indexOf(u8, diff_text, "+ side").?; - try std.testing.expectEqual(diff_added, styles[added]); - try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "42").?] & diff_added != 0); - try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "7,").?] & diff_removed != 0); + try std.testing.expectEqual(ground_add | ground_emph, styles[added]); + try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "42").?] & ground_add != 0); + try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "7,").?] & ground_del != 0); try std.testing.expectEqual(Syn.comment, diffSynAt(styles, diff_text, "index 111", 0)); // A window in the middle is coloured as the whole is. @@ -1660,7 +1667,7 @@ test "syntax a deleted file's lines are coloured in its language" { defer std.testing.allocator.free(styles); try std.testing.expectEqual(Syn.keyword, diffSynAt(styles, diff_text, "enum", 0)); try std.testing.expectEqual(Syn.comment, diffSynAt(styles, diff_text, "// the end", 3)); - try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "enum").?] & diff_removed != 0); + try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "enum").?] & ground_del != 0); try std.testing.expectEqual(Syn.comment, diffSynAt(styles, diff_text, "deleted file", 0)); } @@ -1709,9 +1716,9 @@ test "syntax git diff output in a command pane is told and coloured by its rows" try std.testing.expectEqual(Syn.keyword, diffSynAt(styles, text, "fn main", 0)); try std.testing.expectEqual(Syn.number, diffSynAt(styles, text, "42", 0)); // The wrapped row stays an added line, and inside the comment. - try std.testing.expect(styles[std.mem.indexOf(u8, text, "wrapped rest").?] & diff_added != 0); + try std.testing.expect(styles[std.mem.indexOf(u8, text, "wrapped rest").?] & ground_add != 0); try std.testing.expectEqual(Syn.comment, diffSynAt(styles, text, "wrapped rest", 0)); - try std.testing.expect(styles[std.mem.indexOf(u8, text, "return").?] & diff_removed != 0); + try std.testing.expect(styles[std.mem.indexOf(u8, text, "return").?] & ground_del != 0); } test "syntax a section's small hunks share one parse" { @@ -1733,7 +1740,7 @@ test "syntax a section's small hunks share one parse" { try std.testing.expectEqual(@as(usize, 2), cached); // x.zig's three hunks, and y.zig's try std.testing.expectEqual(Syn.keyword, diffSynAt(styles, diff_text, "@@ -10", 0)); try std.testing.expectEqual(Syn.number, diffSynAt(styles, diff_text, "2;\n@@ -20", 0)); - try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "@@ -10").?] & (diff_added | diff_removed) == 0); + try std.testing.expect(styles[std.mem.indexOf(u8, diff_text, "@@ -10").?] & (ground_add | ground_del) == 0); } test "syntax a hunk painted once is kept, and a long one is parsed in pieces" { -- cgit v1.3