From 2d25d58f8132c54ca0f853899d6b525c4ae892e4 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 21 Nov 2026 01:37:10 -0300 Subject: layers: the source view's painters a seam with nothing to see but one word: File.Layers (core/File.zig, beside recolorSyntax), a pane's field that each view keeps its own (views.zig pane_fields), says which painters a file's body wears and writes down their order and places (1 the ink, tree-sitter's colour a code cell; 2 the patch, a diff's grounds, marker and changed words; next, ruled and not built: changes, a lane after the line number, green and red grounds, a changed line's words, ghost rows; churn, a heat lane and a strip by the scroll thumb), both still painted in recolorSyntax's one walk; Syntax (a tag word, SPC t h: SPC t s is the pdf addon's PdfSections, a ctl line) turns the ink off in this view and on again; the ink off is no style byte read at all (a test a cell, as a branch or a mask, cost the frame 0.1 to 0.5% of its instructions, 0.15 to 0.47% on the long line; a walk specialised at comptime for each state cost 0.5 to 3.4%: the gate measured all three), so in a patch Syntax takes its grounds too; File.zig's test draws a .zig text's first row with the ink, without it, in a second view that keeps its own, and with it again; tui core-test 876/1034 (158 skipped); the tip's gate: frames 77/77, the 121 counts within 0.1% of tree3 (largest +0.026%), perfab -0.013% Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/File.zig | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- core/builtins.zig | 9 ++++++ core/config.zig | 1 + core/panes.zig | 2 ++ core/views.zig | 3 +- 5 files changed, 95 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/File.zig b/core/File.zig index 8a8c05de..813e172f 100644 --- a/core/File.zig +++ b/core/File.zig @@ -1847,6 +1847,27 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w return written; } +/// The layers a file's body wears in a view (`Pane.layers`, each view its +/// own: views.zig). Not Layer.zig's band of cells: these are the painters +/// over a body's text, in this order, each in its own place: +/// +/// 1. ink: each code cell's colour, from tree-sitter (syntax.zig's +/// highlightFileRange): on until `Syntax` turns it off; +/// 2. patch: a diff's or a patch's grounds, a `+`/`-` row's ground and +/// marker and the words a changed line changed (syntax.zig's +/// highlightDiff): always, in a diff (its style bytes are the ink's +/// too: `Syntax` off takes both). +/// +/// Both are painted in recolorSyntax's one walk over the cells, each where +/// it says (one walk per painter cost a frame a sixth more). Ruled, not +/// built (PARDES.md "Changes on the source view"): 3. changes: a lane after +/// the line number, a line added whole green across it, a changed line's +/// words tinted, ghost rows for what the shown revision has not; 4. churn: +/// a heat lane after the number and a strip by the scroll thumb. +pub const Layers = struct { + ink: bool = true, +}; + pub const SynStyle = struct { fg: [3]u8, bold: bool }; pub fn synStyle(p: *Pardes, sy: syntax.Syn) ?SynStyle { @@ -1856,7 +1877,10 @@ pub fn synStyle(p: *Pardes, sy: syntax.Syn) ?SynStyle { /// syntax colors: recolor each content cell from its tree-sitter style byte; /// content starts after the lineno gutter pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes.Rect, tx: u16, tw: u16, body_h: u16) void { - const highlights = if (f.mini) |mini| mini.colors else f.highlights; + // The ink off (`Syntax`): no style byte is read, so a cell asks + // nothing of it (a test a cell cost a frame 0.1 to 0.5%). A patch's + // grounds are bytes of the same painter's: they go with it. + const highlights: []const u8 = if (!pane.layers.ink) &.{} else if (f.mini) |mini| mini.colors else f.highlights; const highlight_start = if (f.mini != null) 0 else f.highlight_start; const spans = f.pager_spans; if (highlights.len == 0 and f.output == null and spans.len == 0) return; @@ -1911,6 +1935,7 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. grounds = .of(p.theme()); break :ground grounds.?; }).forStyle(lead); + // 2, the patch: the row's ground. if (tint) |t| if (prefix_width < tw) { 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); @@ -1950,6 +1975,7 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. if (idx >= highlight_start) { const hidx = idx - highlight_start; if (hidx < highlights.len) { + // 1, the ink; a patch row's marker is 2's. if (if (tint != null and hs + c == 0) SynStyle{ .fg = tint.?.ink, .bold = true } else @@ -1963,6 +1989,7 @@ pub fn recolorSyntax(p: *Pardes, s: *Surface, pane: *Pane, f: *State, r: pardes. cell.style.bold = ss.bold; } } + // 2, the patch: a changed word's ground. 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) @@ -2525,3 +2552,56 @@ test "context cache frees each allocation with its original allocator" { try std.testing.expect(cache.allocator == null); try std.testing.expectEqual(@as(usize, 0), cache.declarations.len); } + +/// The inks of row `row`'s first `n` text cells of pane `id` as `s` draws them. +fn rowInks(s: *pardes.Surface, pane: *const Pane, id: usize, row: u16, out: []pardes.Color) void { + const body = s.region(.body, id).?.rect; + const y: usize = body.y + row; + for (out, 0..) |*ink, i| { + const cell = s.cells[y * s.cols + body.x + gutterWidth(pane) + i]; + ink.* = cell.style.fg; + } +} + +test "Syntax takes a text's ink off in this view and puts it back, and another view keeps its own" { + if (!syntax.supportsPath("/test.zig")) return error.SkipZigTest; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("pub const x = 1;\n"); + gpa.free(pane.file.?.path); + pane.file.?.path = try gpa.dupe(u8, "/test.zig"); + pane.file.?.syntax_dirty = true; + const id = p.view().active; + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const plain: pardes.Color = if (p.theme().fg) |c| .{ .rgb = c } else .default; + var inks: [16]pardes.Color = undefined; + const Frame = struct { + fn take(q: *Pardes, a: *std.heap.ArenaAllocator, shown: *const Pane, at: usize, out: []pardes.Color) !void { + q.sync(); + _ = a.reset(.retain_capacity); + rowInks(try q.render(a.allocator()), shown, at, 0, out); + } + fn colored(out: []const pardes.Color, fg: pardes.Color) bool { + for (out) |c| if (!std.meta.eql(c, fg)) return true; + return false; + } + }; + try Frame.take(p, &arena, pane, id, &inks); + try std.testing.expect(Frame.colored(&inks, plain)); + const other = p.openView().?; + p.update(.{ .command = "Syntax" }); + try std.testing.expect(!pane.layers.ink); + try Frame.take(p, &arena, pane, id, &inks); + try std.testing.expect(!Frame.colored(&inks, plain)); + // The other view's text keeps its ink; this one's stays off. + p.toView(other); + try Frame.take(p, &arena, pane, id, &inks); + try std.testing.expect(Frame.colored(&inks, plain)); + p.toView(.at(0)); + try std.testing.expect(!pane.layers.ink); + p.update(.{ .command = "Syntax" }); + try Frame.take(p, &arena, pane, id, &inks); + try std.testing.expect(Frame.colored(&inks, plain)); +} diff --git a/core/builtins.zig b/core/builtins.zig index 7ad70e7c..b516d543 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -1060,6 +1060,15 @@ pub const Filter = struct { } }; +/// Turn a text's syntax colours off in this view, or on again. +/// The ink layer (File.Layers). +pub const Syntax = struct { + pub fn run(c: Ctx) void { + if (c.pane.file == null) return c.p.reportFailure(c.id, "Syntax: only a text pane has it"); + c.pane.layers.ink = !c.pane.layers.ink; + } +}; + /// Stack a diff's hunks as a diff viewer shows them, each under its place and the declarations around it, in a +Diff; on a +Diff, show the diff itself as text. `Diff ` stacks that diff's, `Diff ` git show's. /// The diff viewer's one registration: its word and traits here, all else /// its own (hunks.zig). diff --git a/core/config.zig b/core/config.zig index f43604ad..998063cc 100644 --- a/core/config.zig +++ b/core/config.zig @@ -121,6 +121,7 @@ pub const leader_path = paths: { .Msg = null, .Verbose = null, .Wrap = "tw", + .Syntax = "th", .NextColor = "tn", .Themes = "tt", .Theme = null, diff --git a/core/panes.zig b/core/panes.zig index 174af241..e8f35de8 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -318,6 +318,8 @@ pub const Pane = struct { /// step's `sync` (normal.halfPageMove's scroll), and a core loaded from a /// dump has had none (Replica.initDump). view: SessionView = .{}, + /// The painters a file's body wears in this view (File.Layers). + layers: File.Layers = .{}, sel: [3]Sel = @splat(.{}), pointer_selections: [3]?PointerSelection = @splat(null), next_pointer_selection: u64 = 0, diff --git a/core/views.zig b/core/views.zig index 89dcb454..7230909a 100644 --- a/core/views.zig +++ b/core/views.zig @@ -33,7 +33,7 @@ pub const max = 4; /// A pane's fields that are each view's own: where it is in the view's /// layout, which of its texts has the keyboard, how far it is scrolled, and /// what the view's step measured of it (its tag's rows, its bands). -const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "docked_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "view", "notices", "pan", "follow_scroll" }; +const pane_fields = [_][:0]const u8{ "focus", "vweight", "collapsed", "docked_to", "hscroll", "tag_scroll", "tag_top", "tag_rows", "view", "layers", "notices", "pan", "follow_scroll" }; /// The body's: its cursor, selections and mode. Its undo stays the pane's. const body_fields = [_][:0]const u8{ "mode", "cur_pinned", "cur_row", "cur_col", "vsel", "sels", "nsel", "select", "sticky_col", "restore_cursor", "last_edit", "reg_wait", "normal", "find_op", "find_ch" }; @@ -49,6 +49,7 @@ pub const PaneView = struct { tag_top: u16 = 0, tag_rows: u16 = 1, view: Pane.SessionView = .{}, + layers: File.Layers = .{}, notices: Pane.Notices = .{}, pan: u16 = 0, follow_scroll: usize = 0, -- cgit v1.3