summaryrefslogtreecommitdiff
path: root/core/File.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/File.zig')
-rw-r--r--core/File.zig204
1 files changed, 184 insertions, 20 deletions
diff --git a/core/File.zig b/core/File.zig
index 8a8c05de..8749417f 100644
--- a/core/File.zig
+++ b/core/File.zig
@@ -372,7 +372,13 @@ pub fn gutterWidth(pane: *const Pane) u16 {
var lines = if (file.line_starts.len > 0) file.line_starts.len else lineCount(file.content);
var digits: u16 = 1;
while (lines >= 10) : (lines /= 10) digits += 1;
- return @max(config.PREFIX_W, digits + 1);
+ return @max(config.PREFIX_W, digits + 1) + laneCells(pane);
+}
+
+/// The cells the changes' lanes take after the line number in this view
+/// (File.Layers), none while it pins no changes.
+pub fn laneCells(pane: *const Pane) u16 {
+ return pane.layers.lanes;
}
/// Convert between rendered cells and UTF-8 byte columns. Tag rows always
@@ -697,6 +703,9 @@ const swap_min = 64 << 10;
/// a new buffer in and gives the old to the journal whole (a swap); an
/// undo's swap back hands the buffer it replaces to `kept`.
fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, whole: ?[]u8, journaled: bool, kept: ?*[]u8) bool {
+ // A file at a revision is read-only (jj.zig): a key's splice changes
+ // nothing (a whole text is the editor's to refuse, edit.zig).
+ if (whole == null) if (f.output) |o| if (o.from == .revision) return false;
const old_len = f.content.len;
const new_len = old_len - len + bytes_in.len;
const span: Span = .{ .head = off, .tail = old_len - off - len };
@@ -747,6 +756,8 @@ fn edit(p: *Pardes, f: *State, off: usize, len: usize, bytes_in: []const u8, who
};
};
reportEdit(p, f, off, len, bytes);
+ // A pinned change's per-line values ride with the lines (jj.zig).
+ if (p.changes.answers.items.len > 0) @import("jj.zig").edited(p, f, off, len, bytes);
if (f.mini) |*mini| mini.deinit(p.gpa);
f.mini = null;
if (f.line_starts.len > 0) shiftIndex(p.gpa, f, off, len, bytes);
@@ -1607,7 +1618,7 @@ pub fn refreshHighlights(p: *Pardes) void {
syntax.highlightLocationRows(p.tree_sitter_gpa, f.content, f.location_rows)
else
syntax.highlightLocations(p.tree_sitter_gpa, f.content, start, end),
- else => syntax.highlightFileRange(p.tree_sitter_gpa, f.path, f.content, start, end),
+ else => syntax.highlightFileRange(p.tree_sitter_gpa, langPath(f), f.content, start, end),
}) catch {
f.syntax_dirty = false;
continue;
@@ -1641,7 +1652,13 @@ fn paintAhead(p: *Pardes, pane: *Pane, f: *State) void {
const end = @max(start, lineStart(p.gpa, f, f.scroll + (if (seen.dir > 0) 4 * rows else rows) + SYNTAX_CONTEXT_AFTER_ROWS));
// Painted already, as far as that goes.
if (f.highlights.len > 0 and start >= f.highlight_start and end <= f.highlight_start + f.highlights.len) return;
- Stream.paintAhead(p, pane.serial, f.revision, f.path, f.content, start, end);
+ Stream.paintAhead(p, pane.serial, f.revision, langPath(f), f.content, start, end);
+}
+
+/// The path a text's language is known by: a file at a revision's
+/// (jj.zig) without its `@rev`.
+pub fn langPath(f: *const State) []const u8 {
+ return if (@import("jj.zig").of(f)) |r| r.path else f.path;
}
pub fn supportsContext(pane: *const Pane) bool {
@@ -1747,7 +1764,7 @@ fn geometryKey(pane: *const Pane, f: *const State) u64 {
if (f.output) |o| pardes.walk.digest(Output.Origin, &o.from) else 0, std.hash.Wyhash.hash(0, f.path), f.tree_context, f.context.revision orelse 0,
f.context.declarations.len, f.line_starts.len, t.revision orelse 0, t.list.items.len,
pane.view.wrap, pane.view.layered, pane.view.body_h, pane.view.tagline_h,
- pane.view.rect_h,
+ pane.view.rect_h, laneCells(pane),
};
// One hash of them laid out as words: every reader of the map asks for
// this key, a hundred times a frame.
@@ -1762,6 +1779,8 @@ fn geometryKey(pane: *const Pane, f: *const State) u64 {
fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_wrap: bool) usize {
if (record_wrap) pane.wrap_n = 0;
const prefix_width = gutterWidth(pane);
+ // Where a number ends: a blank, then the lanes (laneCells), after it.
+ const number_end = prefix_width -| laneCells(pane) -| 1;
const total = nlines(pane.gpa, f);
// The slot after the text's last newline, found once: every numbered
// row below asks whether it is that one.
@@ -1799,23 +1818,25 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w
pane.wrap_n = @intCast(i + 1);
}
if (row < total) {
- var lbuf: [@max(config.PREFIX_W, 32)]u8 = undefined;
- const prefix = lbuf[0..prefix_width];
- @memset(prefix, ' ');
- if (at == 0 and prefix.len > 0) {
- var lineno = row + 1;
- var digit: usize = prefix.len - 1;
- if (row == end_slot) {
- prefix[digit - 1] = '~';
- } else while (true) {
- digit -= 1;
- prefix[digit] = '0' + @as(u8, @intCast(lineno % 10));
- lineno /= 10;
- if (lineno == 0) break;
+ // The number only where the text is wanted: a geometry pass (no
+ // `dst`, a hundred a frame) counts its cells and nothing more.
+ if (dst) |out| {
+ const prefix = out[written..][0..prefix_width];
+ @memset(prefix, ' ');
+ if (at == 0 and prefix.len > 0) {
+ var lineno = row + 1;
+ var digit: usize = number_end;
+ if (row == end_slot) {
+ prefix[digit - 1] = '~';
+ } else while (true) {
+ digit -= 1;
+ prefix[digit] = '0' + @as(u8, @intCast(lineno % 10));
+ lineno /= 10;
+ if (lineno == 0) break;
+ }
}
}
- if (dst) |out| @memcpy(out[written..][0..prefix.len], prefix);
- written += prefix.len;
+ written += prefix_width;
if (dst) |out| @memcpy(out[written..][0..guide.len], guide);
written += guide.len;
// A table row's pads are blanks before their bytes: the row
@@ -1847,6 +1868,45 @@ 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 (with `Syntax` off its bytes are
+/// made without the language's ink: highlightDiffPlain).
+///
+/// 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).
+///
+/// 3. changes (`Changes [revset]`, jj.zig): while `lanes` is set, a
+/// lane each of the pane's pinned changes after the line number (the
+/// gutter `lanes` cells wider), a line one added whole green across
+/// it (jj.paint). Ruled, not built (PARDES.md "Changes on the source
+/// view"): a changed line's words tinted, ghost rows for what the
+/// shown revision has not.
+/// 4. churn, ruled, not built: a heat lane after the number and a strip
+/// by the scroll thumb.
+///
+/// Two bytes, hashed in one update: a digest takes every view's of every
+/// pane at every step.
+pub const Layers = struct {
+ ink: bool = true,
+ /// Whether this view shows the pane's pinned changes (`Pardes.changes`:
+ /// the revset and jj's answers are the pane's), as the cells their
+ /// lanes take (laneCells), 0 while it does not: set from the view's
+ /// frontend, in a window two, which its strips of pixels fill one and
+ /// a half of (Surface.Lane), in a terminal three, a glyph each.
+ lanes: u8 = 0,
+
+ pub fn digest(l: *const Layers, h: *pardes.walk.Hasher) void {
+ h.update(&.{ @intFromBool(l.ink), l.lanes });
+ }
+};
+
pub const SynStyle = struct { fg: [3]u8, bold: bool };
pub fn synStyle(p: *Pardes, sy: syntax.Syn) ?SynStyle {
@@ -1856,7 +1916,16 @@ 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`): a file's style bytes are not read, so a cell
+ // asks nothing of it (a test a cell cost a frame 0.1 to 0.5%); a
+ // patch's are made again without the language's, once a paint, its
+ // grounds, markers and changed words kept.
+ const highlights: []const u8 = if (pane.layers.ink)
+ (if (f.mini) |mini| mini.colors else f.highlights)
+ else if (pane.colorAlgo() == .diff and f.highlights.len > 0)
+ syntax.highlightDiffPlain(p.scratch.allocator(), f.content, f.highlight_start, f.highlight_start + f.highlights.len) catch &.{}
+ else
+ &.{};
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 +1980,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 +2020,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 +2034,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 +2597,95 @@ 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));
+}
+
+test "Syntax off in a patch's pane takes the language's ink and leaves the patch: every ground as it was" {
+ if (!syntax.supportsPath("/x.zig")) return error.SkipZigTest;
+ const gpa = std.testing.allocator;
+ // A patch's hunks take their language's ink once the parsers run.
+ syntax.start(gpa);
+ defer syntax.stop();
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 14 });
+ defer p.deinit();
+ const pane = try p.setTestFile("diff --git a/x.zig b/x.zig\n--- a/x.zig\n+++ b/x.zig\n@@ -1,2 +1,2 @@\n const a = 1;\n-const b = 2;\n+pub const b = 3;\n");
+ gpa.free(pane.file.?.path);
+ pane.file.?.path = try gpa.dupe(u8, "/change.diff");
+ pane.file.?.syntax_dirty = true;
+ try std.testing.expect(pane.colorAlgo() == .diff);
+ const id = p.view().active;
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ var on: [7][2][24]pardes.Color = undefined;
+ for (0..2) |pass| {
+ if (pass == 1) p.update(.{ .command = "Syntax" });
+ p.sync();
+ _ = arena.reset(.retain_capacity);
+ const s = try p.render(arena.allocator());
+ const body = s.region(.body, id).?.rect;
+ var differ = false;
+ for (0..7) |row| for (0..24) |col| {
+ const cell = s.cells[(body.y + row) * s.cols + body.x + gutterWidth(pane) + col];
+ if (pass == 0) {
+ on[row][0][col] = cell.style.bg;
+ on[row][1][col] = cell.style.fg;
+ } else {
+ // The grounds stay; the ink of the code goes.
+ try std.testing.expect(std.meta.eql(on[row][0][col], cell.style.bg));
+ differ = differ or !std.meta.eql(on[row][1][col], cell.style.fg);
+ }
+ };
+ if (pass == 1) try std.testing.expect(differ);
+ }
+}