summaryrefslogtreecommitdiff
path: root/core/File.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-21 01:37:10 -0300
committerGabriel Schneider <[email protected]>2026-11-21 06:19:35 -0300
commit2d25d58f8132c54ca0f853899d6b525c4ae892e4 (patch)
treebe6d88b78a0ac46e0347d1569b77ccbae27f6b57 /core/File.zig
parent6d937cd125ca04a2edb92b2d014014ee6c2eb45d (diff)
downloadpardes-2d25d58f8132c54ca0f853899d6b525c4ae892e4.tar.gz
pardes-2d25d58f8132c54ca0f853899d6b525c4ae892e4.zip
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 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/File.zig')
-rw-r--r--core/File.zig82
1 files changed, 81 insertions, 1 deletions
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));
+}