From 54b0c95fc5522b5ad48d857bb0a08c253c17ece3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 23:56:13 -0300 Subject: File: recolorSyntax paints a grapheme's cells in one loop (syntax colour, a diff's emphasis ground, a result's emphasis, a pager's colours, in that order) instead of four loops over the same cells Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/File.zig | 93 ++++++++++++++++++++++++------------------------------------ 1 file changed, 37 insertions(+), 56 deletions(-) (limited to 'src') diff --git a/src/File.zig b/src/File.zig index 43eccc49..d2e8352a 100644 --- a/src/File.zig +++ b/src/File.zig @@ -1929,67 +1929,48 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. const grapheme_end = @min(limit, modal.nextGrapheme(line, hs + c)); const cells = graphemeDisplayWidth(line[hs + c .. grapheme_end]); const idx = base + hs + c; - if (idx >= highlight_start) { - const hidx = idx - highlight_start; - if (hidx < highlights.len) { - if (if (tint != null and hs + c == 0) - SynStyle{ .fg = tint.?.fg, .bold = true } - else - synStyle(p, @fromBackingInt(@intCast(highlights[hidx] & syntax.syn_bits)))) |ss| - { - var fill: usize = 0; - while (fill < cells and prefix_width + screen_c + fill < tw) : (fill += 1) { - const cell = s.at(tx + @as(u16, @intCast(prefix_width + screen_c + fill)), body_y + vr); - if (cell.default) continue; - cell.style.fg = .{ .rgb = ss.fg }; - cell.style.bold = ss.bold; - } - } - if (tint) |t| if (highlights[hidx] & syntax.diff_emph != 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 }; - }; - } - } - // Results keep their exact live text. Only the location, - // matched source range and diagnostic label gain emphasis. - if (if (chrome) |ch| decoration.styleAt(ch, hs + c) else null) |style| { - var fill: usize = 0; - while (fill < cells and prefix_width + screen_c + fill < tw) : (fill += 1) { - const cell = s.at(tx + @as(u16, @intCast(prefix_width + screen_c + fill)), body_y + vr); - if (cell.default) continue; + // What paints the grapheme's cells, in this order: its syntax + // colour (a diff line's prefix its tint's ink), the words a diff + // changed on their stronger ground, a result's emphasis (only + // the location, matched source range and diagnostic label gain + // it: results keep their exact live text), a +Pager's colours. + const highlight: ?u8 = if (idx >= highlight_start and idx - highlight_start < highlights.len) highlights[idx - highlight_start] else null; + const syn = if (highlight) |h| (if (tint != null and hs + c == 0) SynStyle{ .fg = tint.?.fg, .bold = true } else synStyle(p, @fromBackingInt(@intCast(h & syntax.syn_bits)))) else null; + const emph = if (highlight != null and tint != null and highlight.? & syntax.diff_emph != 0) tint.?.emph_bg else null; + const deco = if (chrome) |ch| decoration.styleAt(ch, hs + c) else null; + while (span_at < spans.len and spans[span_at].end <= idx) span_at += 1; + const span = if (span_at < spans.len and spans[span_at].start <= idx) spans[span_at].style else null; + for (0..@min(cells, @as(usize, tw) - prefix_width - screen_c)) |fill| { + const cell = s.at(tx + @as(u16, @intCast(prefix_width + screen_c + fill)), body_y + vr); + if (!cell.default) if (syn) |ss| { + cell.style.fg = .{ .rgb = ss.fg }; + cell.style.bold = ss.bold; + }; + if (emph) |bg| cell.style.bg = .{ .rgb = bg }; + if (cell.default) continue; + if (deco) |style| { cell.style.fg = style.fg; cell.style.bold = style.bold; if (style.bg != .default) cell.style.bg = style.bg; } - } - while (span_at < spans.len and spans[span_at].end <= idx) span_at += 1; - if (span_at < spans.len and spans[span_at].start <= idx) { - const st = spans[span_at].style; - var fill: usize = 0; - while (fill < cells and prefix_width + screen_c + fill < tw) : (fill += 1) { - const cell = s.at(tx + @as(u16, @intCast(prefix_width + screen_c + fill)), body_y + vr); - if (cell.default) continue; - // As a terminal pane's cell: through Filter, or a - // palette colour through the theme's sixteen and - // truecolor as it is. - if (filtered) |colors| { - if (st.fg != .default) cell.style.fg = colors.spanColor(st.fg, false); - if (st.bg != .default) cell.style.bg = colors.spanColor(st.bg, true); - } else { - if (st.fg != .default) cell.style.fg = if (st.fg == .index) terminal.palColor(p, st.fg.index) else st.fg; - if (st.bg != .default) cell.style.bg = if (st.bg == .index) terminal.palColor(p, st.bg.index) else st.bg; - } - cell.style.bold = cell.style.bold or st.bold; - cell.style.dim = st.dim; - cell.style.italic = st.italic; - cell.style.blink = st.blink; - cell.style.reverse = st.reverse; - cell.style.invisible = st.invisible; - cell.style.strikethrough = st.strikethrough; - if (st.ul != .off) cell.style.ul = st.ul; + const st = span orelse continue; + // As a terminal pane's cell: through Filter, or a palette + // colour through the theme's sixteen and truecolor as it is. + if (filtered) |colors| { + if (st.fg != .default) cell.style.fg = colors.spanColor(st.fg, false); + if (st.bg != .default) cell.style.bg = colors.spanColor(st.bg, true); + } else { + if (st.fg != .default) cell.style.fg = if (st.fg == .index) terminal.palColor(p, st.fg.index) else st.fg; + if (st.bg != .default) cell.style.bg = if (st.bg == .index) terminal.palColor(p, st.bg.index) else st.bg; } + cell.style.bold = cell.style.bold or st.bold; + cell.style.dim = st.dim; + cell.style.italic = st.italic; + cell.style.blink = st.blink; + cell.style.reverse = st.reverse; + cell.style.invisible = st.invisible; + cell.style.strikethrough = st.strikethrough; + if (st.ul != .off) cell.style.ul = st.ul; } screen_c += cells; c = grapheme_end - hs; -- cgit v1.3