summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/File.zig95
-rw-r--r--core/Output.zig5
-rw-r--r--core/builtins.zig20
-rw-r--r--core/config.zig2
-rw-r--r--core/edit.zig2
-rw-r--r--core/hunks.zig5
-rw-r--r--core/jj.zig421
-rw-r--r--core/lib_syntax.zig3
-rw-r--r--core/look.zig2
-rw-r--r--core/panes.zig4
-rw-r--r--core/root.zig1
-rw-r--r--core/tape.zig3
-rw-r--r--core/views.zig3
13 files changed, 558 insertions, 8 deletions
diff --git a/core/File.zig b/core/File.zig
index 8a8c05de..01aaeec8 100644
--- a/core/File.zig
+++ b/core/File.zig
@@ -697,6 +697,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 };
@@ -1607,7 +1610,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 +1644,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 {
@@ -1847,6 +1856,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 +1886,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 +1944,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 +1984,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 +1998,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 +2561,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/Output.zig b/core/Output.zig
index c7bfde14..09ec2a51 100644
--- a/core/Output.zig
+++ b/core/Output.zig
@@ -38,6 +38,8 @@ pub const Origin = union(enum) {
/// A query pane's +Preview (Query.zig): a file's text shown, not yet
/// its pane; Enter promotes it to one where it stands.
preview,
+ /// A file at a revision (jj.zig), its arg the revision.
+ revision,
};
/// An output's argument is typed in the bounded one-line tag storage.
@@ -163,6 +165,7 @@ pub fn traits(o: Origin) Traits {
.kind => .{ .name = "", .steps = true, .locations = true },
.unsaved => .{ .name = config.unsaved_buffer, .doc = true },
.preview => .{ .name = config.preview_buffer, .doc = true },
+ .revision => .{ .name = "", .doc = true },
// Named for its directory, not a `+` name in it.
.dir => .{ .name = "", .doc = true },
.cmd => |b| builtins.registry.outputTraits(b) orelse unreachable,
@@ -264,7 +267,7 @@ fn sortResults(arena: std.mem.Allocator, from: Origin, content: []u8, anchor: ?u
=> {},
else => return anchor,
},
- .errors, .kind, .unsaved, .dir, .preview => return anchor,
+ .errors, .kind, .unsaved, .dir, .preview, .revision => return anchor,
}
if (content.len == 0) return anchor;
const trailing_newline = content[content.len - 1] == '\n';
diff --git a/core/builtins.zig b/core/builtins.zig
index 7ad70e7c..18b49a41 100644
--- a/core/builtins.zig
+++ b/core/builtins.zig
@@ -983,6 +983,11 @@ pub const Save = struct {
};
const path = std.mem.trim(u8, c.arg orelse "", " \t\r\n");
if (path.len > 0) return exec.saveTo(c.p, c.id, path);
+ // A file at a revision (jj.zig) is read-only: written elsewhere only.
+ if (c.pane.file) |*file| if (@import("jj.zig").of(file) != null) {
+ var said: pardes.Kind.Buf(limits.host_path_cap + 64) = .{};
+ return c.p.reportFailure(c.id, said.print("Save: {s} is a revision, read-only (Save <path> writes it)", .{file.path[0..@min(file.path.len, limits.host_path_cap)]}));
+ };
// A `+` name is a generated pane's (+Tutor), no file of its own:
// a bare Save asks for a path, as a +New scratch's does.
if (c.pane.file) |file| if (file.output == null and !std.mem.startsWith(u8, std.Io.Dir.path.basename(file.path), "+")) return exec.saveFile(c.p, c.id);
@@ -1060,6 +1065,21 @@ 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;
+ }
+};
+
+/// Show a file as it was at revision `rev` (jj), read-only, beside its pane; in such a pane, that file at another revision in its place, or bare, the file.
+pub const At = struct {
+ pub const takes_arg = true;
+ pub const run = @import("jj.zig").word;
+};
+
/// 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 <file>` stacks that diff's, `Diff <revision>` 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..361a59ed 100644
--- a/core/config.zig
+++ b/core/config.zig
@@ -76,6 +76,7 @@ pub const leader_path = paths: {
.DelBelow = null,
.Filter = null,
.Diff = null,
+ .At = null,
.Mode = null,
.Togglettymode = null,
.Collapse = null,
@@ -121,6 +122,7 @@ pub const leader_path = paths: {
.Msg = null,
.Verbose = null,
.Wrap = "tw",
+ .Syntax = "th",
.NextColor = "tn",
.Themes = "tt",
.Theme = null,
diff --git a/core/edit.zig b/core/edit.zig
index 0014f681..7a57fd09 100644
--- a/core/edit.zig
+++ b/core/edit.zig
@@ -705,6 +705,8 @@ fn setEditTextSpan(e: Editor, t: *Text, new: []u8, known_end: ?modal.Cursor, spa
return;
}
const pane = t.pane() orelse return retireEditText(e, new);
+ // A file at a revision is read-only (jj.zig).
+ if (pane.file) |*f| if (f.output) |o| if (o.from == .revision) return retireEditText(e, new);
// A live query pane's rows are its line 1's (Query.zig): an edit there
// is refused, as a tag's prefix refuses one, and one from line 1 into
// them is cut at its end. The rows' metadata stays theirs across an
diff --git a/core/hunks.zig b/core/hunks.zig
index 98584980..bee06cc2 100644
--- a/core/hunks.zig
+++ b/core/hunks.zig
@@ -80,7 +80,10 @@ pub const Ctx = struct {
pub const Source = struct { path: []const u8, bytes: []const u8 };
/// A diff stacked: its text, and a row for each of its lines (row i is
-/// line i), the arena's; a row's path may be the diff's own bytes.
+/// line i), the arena's; a row's path may be the diff's own bytes. A row's
+/// text is its line without its end, the `\n` and a `\r` before it: a CRLF
+/// diff's rows have no `\r` (one that must show it reads it off the diff),
+/// and a source's lines are compared with the diff's so too.
pub const Patch = struct { text: []const u8, rows: []const Row };
pub const Row = struct {
diff --git a/core/jj.zig b/core/jj.zig
new file mode 100644
index 00000000..429ea9c9
--- /dev/null
+++ b/core/jj.zig
@@ -0,0 +1,421 @@
+//! A file at a revision, as jj shows it (PARDES.md "Changes on the source
+//! view"): the address `path@rev`, with a place `path@rev:line`, which a
+//! Look resolves anywhere, and `At <rev>` in a file's pane.
+//!
+//! What jj says is the session machine's: it is run out of the turn as a
+//! site of the step's tape (`Site.jj`), its answer recorded; a replica
+//! replays the answer and never runs jj. A revision's pane is an output
+//! (`Output.Origin.revision`, its argument the revision), named
+//! `path@rev`, coloured as its file (File.langPath), read-only (File.edit
+//! refuses its text a change; a bare Save says why).
+const std = @import("std");
+const pardes = @import("root.zig");
+const Pardes = pardes.Pardes;
+const Pane = pardes.Pane;
+const File = pardes.panes.File;
+const Output = pardes.panes.Output;
+const look = @import("look.zig");
+const layout = @import("layout.zig");
+
+/// What jj answered: why not, in its own words (its first line, without
+/// `Error: `), or the file's bytes. The caller's, in the allocator the site
+/// was given.
+pub const Shown = union(enum) { refused: []u8, bytes: []u8 };
+
+/// The longest text a revision's pane takes.
+const bytes_max = 256 << 20;
+
+/// `text`, not a file of its own, read as `path@rev`: the text before its
+/// first `@` the path, the rest the revision (`main`, `@-`, `main@origin`).
+pub fn split(text: []const u8) ?struct { path: []const u8, rev: []const u8 } {
+ const at = std.mem.indexOfScalar(u8, text, '@') orelse return null;
+ if (at == 0 or at + 1 == text.len or text[at - 1] == '/') return null;
+ return .{ .path = text[0..at], .rev = text[at + 1 ..] };
+}
+
+/// The file at `path` (absolute) at revision `rev`, as jj shows it.
+pub fn show(p: *Pardes, path: []const u8, rev: []const u8) Shown {
+ const set = fileset(p.scratch.allocator(), path) catch return refusal(p.gpa, "out of memory");
+ return p.tape.site(.jj, p.gpa, runHost, .{ p.gpa, dirOf(path), @as([]const []const u8, &.{ "file", "show", "-r", rev, "--", set }) });
+}
+
+fn dirOf(path: []const u8) []const u8 {
+ return std.Io.Dir.path.dirname(path) orelse "/";
+}
+
+/// The name of `path` as a fileset of that one file in its directory,
+/// taken literally whatever it is called.
+fn fileset(arena: std.mem.Allocator, path: []const u8) ![]const u8 {
+ var set: std.ArrayList(u8) = .empty;
+ try set.appendSlice(arena, "file:\"");
+ for (std.Io.Dir.path.basename(path)) |c| {
+ if (c == '"' or c == '\\') try set.append(arena, '\\');
+ try set.append(arena, c);
+ }
+ try set.append(arena, '"');
+ return set.items;
+}
+
+/// jj with `args`, run in `dir`: what it printed, or its refusal. The
+/// working copy is left alone (`--ignore-working-copy`): an editor's read
+/// makes no operation, and `@` is the working copy as jj last saw it.
+fn runHost(gpa: std.mem.Allocator, dir: []const u8, args: []const []const u8) Shown {
+ // The tape's io spawns nothing (its allocator fails, its environment is
+ // empty): one of its own, with this process's environment.
+ var threaded: std.Io.Threaded = .init_single_threaded;
+ threaded.allocator = gpa;
+ threaded.environ = .{ .process_environ = .{ .block = .{ .slice = std.mem.span(std.c.environ) } } };
+ threaded.environ_initialized = false;
+ const io = threaded.io();
+ var argv: std.ArrayList([]const u8) = .empty;
+ defer argv.deinit(gpa);
+ argv.appendSlice(gpa, &.{ "jj", "--ignore-working-copy", "--color=never", "--no-pager" }) catch return refusal(gpa, "out of memory");
+ argv.appendSlice(gpa, args) catch return refusal(gpa, "out of memory");
+ // As Stream's children: this thread's mask is the child's.
+ const none = std.posix.sigemptyset();
+ var kept: std.posix.sigset_t = undefined;
+ std.posix.sigprocmask(std.posix.SIG.SETMASK, &none, &kept);
+ var child = std.process.spawn(io, .{ .argv = argv.items, .cwd = .{ .path = dir }, .stdin = .ignore, .stdout = .pipe, .stderr = .pipe }) catch |err| {
+ std.posix.sigprocmask(std.posix.SIG.SETMASK, &kept, null);
+ return refusal(gpa, if (err == error.FileNotFound) "no jj" else @errorName(err));
+ };
+ std.posix.sigprocmask(std.posix.SIG.SETMASK, &kept, null);
+ defer child.kill(io);
+ // One after the other, on an io with no thread to read both at once:
+ // jj says a refusal in a line and nothing on stdout.
+ var out: std.ArrayList(u8) = .empty;
+ defer out.deinit(gpa);
+ var err: std.ArrayList(u8) = .empty;
+ defer err.deinit(gpa);
+ for ([_]std.Io.File{ child.stdout.?, child.stderr.? }, [_]*std.ArrayList(u8){ &out, &err }, [_]usize{ bytes_max, 64 << 10 }) |from, to, cap| {
+ var chunk: [64 * 1024]u8 = undefined;
+ while (to.items.len <= cap) {
+ const n = from.readStreaming(io, &.{&chunk}) catch break;
+ if (n == 0) break;
+ to.appendSlice(gpa, chunk[0..n]) catch return refusal(gpa, "out of memory");
+ }
+ }
+ const term = child.wait(io) catch |e| return refusal(gpa, @errorName(e));
+ if (out.items.len > bytes_max) return refusal(gpa, "too much to show");
+ if (term == .exited and term.exited == 0) return .{ .bytes = out.toOwnedSlice(gpa) catch return refusal(gpa, "out of memory") };
+ var first = std.mem.trim(u8, err.items[0 .. std.mem.indexOfScalar(u8, err.items, '\n') orelse err.items.len], " \t\r");
+ if (std.mem.startsWith(u8, first, "Error: ")) first = first["Error: ".len..];
+ return refusal(gpa, if (first.len > 0) first else "jj failed");
+}
+
+fn refusal(gpa: std.mem.Allocator, why: []const u8) Shown {
+ return .{ .refused = gpa.dupe(u8, why) catch &.{} };
+}
+
+/// A revision's pane: the file it is of (its name without `@rev`) and the
+/// revision. Null for any other pane.
+pub fn of(f: *const File.State) ?struct { path: []const u8, rev: []const u8 } {
+ const o = f.output orelse return null;
+ if (o.from != .revision) return null;
+ return .{ .path = f.path[0 .. f.path.len - o.arg().len - 1], .rev = o.arg() };
+}
+
+/// `path` (absolute) at `rev` in a pane: the one showing it focused, else
+/// one made beside pane `near` (or, with `replace`, in its place: it goes),
+/// wearing `near`'s layers; its cursor on line `line` (0: the first). Said
+/// at `near` when jj refuses. The pane's id.
+pub fn open(p: *Pardes, near: usize, path: []const u8, rev: []const u8, at: look.Spot, replace: bool) ?usize {
+ const name = std.fmt.allocPrint(p.scratch.allocator(), "{s}@{s}", .{ path, rev }) catch return null;
+ for (p.panes, 0..) |slot, i| if (slot) |shown| if (shown.file) |*f| if (of(f) != null and std.mem.eql(u8, f.path, name)) {
+ look.focusPaneLine(p, i, at, .center);
+ return i;
+ };
+ if (rev.len > Output.max_arg) return refused(p, near, "a revision that long");
+ const free = p.freeSlot() orelse {
+ p.reportError(.at(near), "At", error.NoPaneSlots);
+ return null;
+ };
+ const bytes = switch (show(p, path, rev)) {
+ .refused => |why| {
+ defer p.gpa.free(why);
+ return refused(p, near, why);
+ },
+ .bytes => |b| b,
+ };
+ const pane = Output.open(p, free, std.Io.Dir.path.dirname(path) orelse "/", .revision, rev, bytes) catch |err| {
+ p.gpa.free(bytes);
+ p.reportError(.at(near), "At", err);
+ return null;
+ };
+ const f = &pane.file.?;
+ const owned = p.gpa.dupe(u8, name) catch {
+ p.removePane(free, near) catch {};
+ return null;
+ };
+ p.gpa.free(f.path);
+ f.path = owned;
+ if (p.panes[near]) |from| pane.layers = from.layers;
+ layout.placeNew(p, near, near, free, .doc);
+ if (replace) p.removePane(near, free) catch |err| p.reportError(.at(free), "At", err);
+ layout.compute(p);
+ // Sized for its place before it is centred there.
+ p.sync();
+ look.focusPaneLine(p, free, at, .center);
+ return free;
+}
+
+fn refused(p: *Pardes, id: usize, why: []const u8) ?usize {
+ var line: pardes.Kind.Buf(512) = .{};
+ p.reportFailure(id, line.print("At: {s}", .{why[0..@min(why.len, 480)]}));
+ return null;
+}
+
+/// The `At` word (builtins.zig registers it): in a file's pane, that file
+/// at revision `rev` beside it; in a revision's pane, the same file at
+/// `rev` in its place, its cursor's line kept; bare there, the file.
+pub fn word(c: pardes.builtins.Ctx) void {
+ const p = c.p;
+ const f = if (c.pane.file) |*file| file else return p.reportFailure(c.id, "At: only a file's pane takes it");
+ const rev = std.mem.trim(u8, c.arg orelse "", " \t\r\n");
+ const at: look.Spot = .{ .line = @intCast(@max(0, c.pane.body.cur_row) + 1) };
+ if (of(f)) |r| {
+ if (rev.len == 0) return look.lookAt(p, c.id, r.path);
+ const path = p.scratch.allocator().dupe(u8, r.path) catch return;
+ _ = open(p, c.id, path, rev, at, true);
+ return;
+ }
+ if (f.output != null or pardes.filesystem.localPath(f.path) == null) return p.reportFailure(c.id, "At: only a file's pane takes it");
+ if (rev.len == 0) return p.reportFailure(c.id, "At: which revision?");
+ _ = open(p, c.id, f.path, rev, at, false);
+}
+
+/// A Look at `text` (a path relative to `dir`, then `@rev`) that names no
+/// file: a file at a revision, if the path is a file. False when it is not
+/// one, for the look to go on.
+pub fn looked(p: *Pardes, id: usize, dir: []const u8, text: []const u8, at: look.Spot) bool {
+ const r = split(text) orelse return false;
+ const path = std.Io.Dir.path.resolvePosix(p.scratch.allocator(), &.{ dir, r.path }) catch return false;
+ if (pardes.filesystem.localPath(path) == null or pardes.exec.kindOf(p.tape, path) != .file) return false;
+ _ = open(p, id, path, r.rev, at, false);
+ return true;
+}
+
+// ---- what `Changes` reads (PARDES.md: pinned changes as lanes), the
+// data alone: flat arrays, no drawing yet ----
+
+/// Who last wrote each line of `path` (absolute) at `rev`: a change id
+/// (`change_id.short(8)`) a line, as `jj file annotate` says it.
+pub fn annotate(p: *Pardes, path: []const u8, rev: []const u8) Shown {
+ // A path, not a fileset (annotate takes one file as it is named).
+ return p.tape.site(.jj, p.gpa, runHost, .{ p.gpa, dirOf(path), @as([]const []const u8, &.{ "file", "annotate", "-r", rev, "-T", "commit.change_id().short(8) ++ \"\\n\"", "--", std.Io.Dir.path.basename(path) }) });
+}
+
+/// The changes `revset` names, oldest first: a change id a line, as
+/// `annotate` writes them. Asked in directory `dir`, the file's.
+pub fn pinned(p: *Pardes, dir: []const u8, revset: []const u8) Shown {
+ return p.tape.site(.jj, p.gpa, runHost, .{ p.gpa, dir, @as([]const []const u8, &.{ "log", "--no-graph", "--reversed", "-r", revset, "-T", "change_id.short(8) ++ \"\\n\"" }) });
+}
+
+/// A line no pinned change wrote (`provenance`).
+pub const unpinned = std.math.maxInt(u16);
+
+/// Which pinned change wrote each line: a value a line, the change's place
+/// in `changes` (`pinned`'s answer, oldest first) or `unpinned`, from
+/// `annotated` (`annotate`'s answer). The caller's, in `gpa`.
+pub fn provenance(gpa: std.mem.Allocator, annotated: []const u8, changes: []const u8) error{ OutOfMemory, TooManyChanges }![]u16 {
+ var ids: std.ArrayList([]const u8) = .empty;
+ defer ids.deinit(gpa);
+ var it = std.mem.tokenizeScalar(u8, changes, '\n');
+ while (it.next()) |id| try ids.append(gpa, id);
+ if (ids.items.len >= unpinned) return error.TooManyChanges;
+ var out: std.ArrayList(u16) = .empty;
+ errdefer out.deinit(gpa);
+ var lines = std.mem.splitScalar(u8, annotated, '\n');
+ while (lines.next()) |id| {
+ // The empty text after the last newline is no line.
+ if (id.len == 0 and lines.peek() == null) break;
+ const at = for (ids.items, 0..) |pin, i| {
+ if (std.mem.eql(u8, pin, id)) break i;
+ } else unpinned;
+ try out.append(gpa, @intCast(at));
+ }
+ return out.toOwnedSlice(gpa);
+}
+
+// ---- tests ----
+
+const testing = std.testing;
+
+test "path@rev splits at its first @: a path before it, a revision after" {
+ const cases = [_]struct { []const u8, []const u8, []const u8 }{
+ .{ "a.zig@main", "a.zig", "main" },
+ .{ "core/File.zig@@-", "core/File.zig", "@-" },
+ .{ "x.zig@main@origin", "x.zig", "main@origin" },
+ };
+ for (cases) |c| {
+ const got = split(c[0]).?;
+ try testing.expectEqualStrings(c[1], got.path);
+ try testing.expectEqualStrings(c[2], got.rev);
+ }
+ for ([_][]const u8{ "@p12", "a.zig@", "a.zig", "dir/@x" }) |text| try testing.expect(split(text) == null);
+}
+
+/// A jj repository in a test's directory: `a.zig` committed as `one`, then
+/// as `two`, and `three` in its working copy, never snapshotted. Null with
+/// no jj to run.
+const Repo = struct {
+ tmp: testing.TmpDir,
+ dir: []const u8,
+ file: []const u8,
+
+ const one = "const one = 1;\nconst x = 2;\n";
+ const two = "const two = 2;\nconst x = 2;\nconst y = 3;\n";
+ const three = "const three = 3;\n";
+
+ fn init(gpa: std.mem.Allocator) !?Repo {
+ var tmp = testing.tmpDir(.{});
+ errdefer tmp.cleanup();
+ var buf: [std.Io.Dir.max_path_bytes]u8 = undefined;
+ const dir = try gpa.dupe(u8, buf[0..try tmp.dir.realPath(testing.io, &buf)]);
+ errdefer gpa.free(dir);
+ if (!try run(gpa, dir, &.{ "git", "init" })) return null;
+ for ([_][]const u8{ one, two }, [_][]const u8{ "one", "two" }) |text, message| {
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.zig", .data = text });
+ if (!try run(gpa, dir, &.{ "commit", "-m", message })) return null;
+ }
+ try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.zig", .data = three });
+ return .{ .tmp = tmp, .dir = dir, .file = try std.fmt.allocPrint(gpa, "{s}/a.zig", .{dir}) };
+ }
+
+ fn run(gpa: std.mem.Allocator, dir: []const u8, args: []const []const u8) !bool {
+ var argv: std.ArrayList([]const u8) = .empty;
+ defer argv.deinit(gpa);
+ try argv.appendSlice(gpa, &.{ "jj", "--config=user.name=t", "--config=user.email=t@t", "--quiet" });
+ try argv.appendSlice(gpa, args);
+ const r = std.process.run(gpa, testing.io, .{ .argv = argv.items, .cwd = .{ .path = dir } }) catch return false;
+ gpa.free(r.stdout);
+ gpa.free(r.stderr);
+ return r.term == .exited and r.term.exited == 0;
+ }
+
+ fn deinit(r: *Repo, gpa: std.mem.Allocator) void {
+ gpa.free(r.file);
+ gpa.free(r.dir);
+ r.tmp.cleanup();
+ }
+};
+
+fn told(p: *Pardes, id: usize) []const u8 {
+ const pane = p.panes[id].?;
+ return pane.msg[0..pane.msg_len];
+}
+
+test "At shows a file at a revision beside it, read-only, coloured as its file; a Look at path@rev:line goes to it; At in it turns it in place; jj's refusals are said" {
+ const gpa = testing.allocator;
+ var repo = try Repo.init(gpa) orelse return error.SkipZigTest;
+ defer repo.deinit(gpa);
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 40 });
+ defer p.deinit();
+ var line: [4200]u8 = undefined;
+ p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) });
+ const file = p.view().active;
+ try testing.expectEqualStrings(Repo.three, p.panes[file].?.file.?.content);
+ p.update(.{ .command = "At @-" });
+ const shown = p.view().active;
+ try testing.expect(shown != file);
+ const pane = p.panes[shown].?;
+ try testing.expectEqualStrings(Repo.two, pane.file.?.content);
+ const name = try std.fmt.allocPrint(gpa, "{s}@@-", .{repo.file});
+ defer gpa.free(name);
+ try testing.expectEqualStrings(name, try pardes.tagline.tagPrefix(p, pane));
+ try testing.expectEqualStrings(repo.file, File.langPath(&pane.file.?));
+ try testing.expect(pane.colorAlgo() == .source);
+ // Read-only: a key's edit and a whole-text edit (dd) change nothing, and a bare Save says so.
+ for ("iz") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } });
+ p.update(.{ .key = .{ .cp = pardes.Key.escape } });
+ for ("dd") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } });
+ try testing.expectEqualStrings(Repo.two, pane.file.?.content);
+ p.update(.{ .command = "Save" });
+ try testing.expect(std.mem.indexOf(u8, told(p, shown), "is a revision, read-only") != null);
+ // From the file's pane, a Look at the address with a line goes there.
+ p.view().active = file;
+ p.update(.{ .command = "Look a.zig@@-:3" });
+ try testing.expectEqual(shown, p.view().active);
+ try testing.expectEqual(@as(i32, 2), pane.body.cur_row);
+ // Turned to another revision in its place: one pane, the older text.
+ p.update(.{ .command = "At @--" });
+ const turned = p.view().active;
+ try testing.expect(p.panes[shown] == null);
+ try testing.expectEqualStrings(Repo.one, p.panes[turned].?.file.?.content);
+ var n: usize = 0;
+ for (p.panes) |slot| if (slot) |s| if (s.file) |*f| {
+ n += @intFromBool(of(f) != null);
+ };
+ try testing.expectEqual(@as(usize, 1), n);
+ // jj's refusals, in its words.
+ p.update(.{ .command = "At nosuch" });
+ try testing.expect(std.mem.indexOf(u8, told(p, turned), "At: Revision `nosuch` doesn't exist") != null);
+ p.view().active = file;
+ p.update(.{ .command = "At" });
+ try testing.expect(std.mem.indexOf(u8, told(p, file), "At: which revision?") != null);
+ // Bare in a revision's pane: the file.
+ p.view().active = turned;
+ p.update(.{ .command = "At" });
+ try testing.expectEqual(file, p.view().active);
+}
+
+test "a client replays the open of a file at a revision and ends equal" {
+ const gpa = testing.allocator;
+ var repo = try Repo.init(gpa) orelse return error.SkipZigTest;
+ defer repo.deinit(gpa);
+ const R = pardes.replica;
+ const a = try R.Authority.init(gpa, .{ .cols = 80, .rows = 24 }, .{}, false);
+ defer a.deinit();
+ const r = try R.Replica.init(gpa, 1, try a.unit(0, 0), false);
+ defer r.deinit();
+ var line: [4200]u8 = undefined;
+ r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}@@-:2", .{repo.file}) }));
+ r.settle();
+ try testing.expect(r.diverged == null);
+ const id = a.core.view().active;
+ try testing.expectEqualStrings(Repo.two, a.core.panes[id].?.file.?.content);
+ try testing.expectEqualStrings(Repo.two, r.core().panes[id].?.file.?.content);
+ try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core()));
+}
+
+test "provenance: a value a line, which pinned change wrote it, from what jj answers" {
+ const gpa = testing.allocator;
+ var repo = try Repo.init(gpa) orelse return error.SkipZigTest;
+ defer repo.deinit(gpa);
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ defer p.deinit();
+ // `two` wrote lines 1 and 3 of its text, `one` line 2.
+ const annotated = switch (annotate(p, repo.file, "@-")) {
+ .bytes => |b| b,
+ .refused => |why| {
+ defer gpa.free(why);
+ std.debug.print("annotate: {s}\n", .{why});
+ return error.Refused;
+ },
+ };
+ defer gpa.free(annotated);
+ for ([_][]const u8{ "@--::@-", "@-" }, [_][3]u16{ .{ 1, 0, 1 }, .{ 0, unpinned, 0 } }) |revset, want| {
+ const changes = switch (pinned(p, repo.dir, revset)) {
+ .bytes => |b| b,
+ .refused => |why| {
+ gpa.free(why);
+ return error.Refused;
+ },
+ };
+ defer gpa.free(changes);
+ const lines = try provenance(gpa, annotated, changes);
+ defer gpa.free(lines);
+ try testing.expectEqualSlices(u16, &want, lines);
+ }
+ // A revset jj refuses, in its words.
+ switch (pinned(p, repo.dir, "nosuch")) {
+ .bytes => |b| {
+ gpa.free(b);
+ return error.NotRefused;
+ },
+ .refused => |why| {
+ defer gpa.free(why);
+ try testing.expect(std.mem.indexOf(u8, why, "nosuch") != null);
+ },
+ }
+}
diff --git a/core/lib_syntax.zig b/core/lib_syntax.zig
index 0bb3f510..759720ec 100644
--- a/core/lib_syntax.zig
+++ b/core/lib_syntax.zig
@@ -72,7 +72,8 @@ pub const ground_line = syntax.ground_line;
pub const Grounds = colors.Grounds;
/// A unified diff stacked as pardes's patch view (its +Diff) shows it:
-/// `formatPatch` gives a `Patch`, its text and a `PatchRow` a line: a
+/// `formatPatch` gives a `Patch`, its text (each line without its end: no
+/// `\r` from a CRLF diff) and a `PatchRow` a line: a
/// hunk's `path:line`, the declarations it stands in (or git's function
/// line), its lines with both their numbers, each byte's style (a `Syn`
/// when `colors` are given, the grounds, the words a changed line changed).
diff --git a/core/look.zig b/core/look.zig
index 1d296775..53c26943 100644
--- a/core/look.zig
+++ b/core/look.zig
@@ -1489,6 +1489,8 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void {
// What is looked for is the place without its trailing `:`, `@p33:`
// and `a.txt:` as much as `a.txt:2:`.
const found = resolveLookTarget(p, id, trimmed, &realbuf);
+ // `path@rev`, `path@rev:12`: no file, a file at a revision (jj.zig).
+ if (found == .none and @import("jj.zig").looked(p, id, Pardes.paneDir(pane), pl.path, pl.at)) return;
// A line past the end of an open pane is a miss (the target's line is
// already clamped, so the one written is asked).
if (found == .pane and found.pane.id < MAX_PANES) if (p.panes[found.pane.id]) |shown| {
diff --git a/core/panes.zig b/core/panes.zig
index 658de219..5277ebba 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,
@@ -556,7 +558,7 @@ pub const Pane = struct {
if (std.mem.endsWith(u8, f.path, ".diff") or std.mem.endsWith(u8, f.path, ".patch") or f.pager_diff) return .diff;
if (f.output != null) {
// A +Preview is a file's text: coloured as the file.
- if (f.output.?.from == .preview) return .source;
+ if (f.output.?.from == .preview or f.output.?.from == .revision) return .source;
const tr = Output.fileTraits(f.output);
if (tr.locations) return .locations;
if (!tr.saves) return .none;
diff --git a/core/root.zig b/core/root.zig
index 572204a1..65f61e0d 100644
--- a/core/root.zig
+++ b/core/root.zig
@@ -628,6 +628,7 @@ const machine_lines = [_]struct { []const u8, usize, []const u8 }{
.{ "exec.zig", 3, "kindOf's stat (tape.site(.kind), its io the tape's) and Shell's check (tape.site(.shell))" },
.{ "fs.zig", 38, "the file system behind the tape's sites (open, read, write, stat, mkdir, access; their io the tape's), the process's home (read once at the start, taped), the 9P transport" },
.{ "hunks.zig", 1, "a test helper's file read" },
+ .{ "jj.zig", 8, "jj run under tape.site(.jj) on an io of its own (the tape's spawns nothing) with this process's environment, the signal mask cleared as Stream's children's: the session's, a replica replays its answer" },
.{ "look.zig", 6, "openLink's fork (the host's: a view's frontend opens a link) and a remote's error name" },
.{ "memory.zig", 7, "the memory regions themselves (mmap, madvise, their locks)" },
.{ "replica.zig", 75, "the client and session engines: sockets, threads, clocks, the blob cache, traces" },
diff --git a/core/tape.zig b/core/tape.zig
index 6af332a3..d1e0527c 100644
--- a/core/tape.zig
+++ b/core/tape.zig
@@ -112,6 +112,8 @@ pub const Site = enum(u16) {
shell,
/// The machine's fonts (Font <name>, Fonts): its disk too.
fonts,
+ /// What jj says of a file (jj.zig): a file at a revision.
+ jj,
// Read in place (`value`).
clock,
wake,
@@ -161,6 +163,7 @@ pub const results = .{
?@import("Store.zig").Record,
?@import("recent.zig").Entry, []const @import("Store.zig").Named,
@import("Kind.zig").Outcome, pardes.Term.Prompt,
+ @import("jj.zig").Shown,
};
/// What a host reports to the core as failed (`shell_failed`, `save_failed`,
diff --git a/core/views.zig b/core/views.zig
index 2ddbb523..704fc859 100644
--- a/core/views.zig
+++ b/core/views.zig
@@ -40,7 +40,7 @@ pub const away_kept_ns: u64 = 30 * std.time.ns_per_s;
/// 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" };
@@ -56,6 +56,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,