diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 15:15:41 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 15:40:27 -0300 |
| commit | 2b9da7be7d9520701b24d46d8db2ec56517b45b8 (patch) | |
| tree | c434298ecf4b6d77c6bee55a6e485e6abca050ca /core/jj.zig | |
| parent | a86e213e3e8053906df5ed4a37821c41a4afd932 (diff) | |
| download | pardes-2b9da7be7d9520701b24d46d8db2ec56517b45b8.tar.gz pardes-2b9da7be7d9520701b24d46d8db2ec56517b45b8.zip | |
layers: the lanes tell the truth about the text in the pane: jj annotates its last snapshot (`--ignore-working-copy`), not the keys typed since, so annotate's answer now carries each line's text (`id\tcontent`, the same one run) and `jj.provenance` lines the annotated text up with the pane's by what the two agree on at either end (`File.spanOf`, the editor's differ; the one line its two ends meet inside counts when its bytes agree: an inserted line shares its indent with the line after it), a line only the pane has nobody's (no lane, no ground); and an edit after `Changes` carries the per-line values with the lines (`jj.edited`: one call in `File.edit`, the one place a text changes, an undo's too, made only while the session has pinned changes): the lines a splice takes out take their values with them, a line it puts in is nobody's, and a splice that starts in a line's leading blanks (as `o` puts a line in) leaves that line's value after what it puts in; jj is never asked again on a key; tests: a line on disk jj has not snapshotted is nobody's and the lines after it keep their changes; a line typed in a nested block (`o`) is nobody's and the rows around keep their lanes, its undo puts the values back, a deleted line takes its value, a client replaying the keys ends equal; core-test 901/1061 (160 skipped), shell-test 1103/1106 (3 skipped)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/jj.zig')
| -rw-r--r-- | core/jj.zig | 206 |
1 files changed, 190 insertions, 16 deletions
diff --git a/core/jj.zig b/core/jj.zig index 5f86dbe6..a381443c 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -460,7 +460,7 @@ fn answer(p: ?*Pardes, gpa: std.mem.Allocator, a: *const Ask) pardes.Stream.Batc if (!any) return .{ .gen = a.gen, .jj = a.serial, .rows = logged, .end = .done }; // Who wrote each line: a path, not a fileset (annotate takes one file // as it is named). - const annotate_args = [_][]const u8{ "file", "annotate", "-r", a.rev, "-T", "commit.change_id().short(8) ++ \"\\n\"", "--", std.Io.Dir.path.basename(a.path) }; + const annotate_args = [_][]const u8{ "file", "annotate", "-r", a.rev, "-T", "commit.change_id().short(8) ++ \"\\t\" ++ content", "--", std.Io.Dir.path.basename(a.path) }; return switch (runJj(p, gpa, dirOf(a.path), &annotate_args)) { .refused => |why| { gpa.free(logged); @@ -497,6 +497,50 @@ pub fn receive(p: *Pardes, b: *const pardes.Stream.Batch, serial: u32) void { pinAnswer(p, id, b, revset[0..revset_len]); } +/// A splice of `f`'s text, before its bytes go in (File.edit, the one +/// place a text changes, an undo's too): each pane showing it that has +/// pins keeps a value for each of its lines, so the values ride with the +/// lines, never asking jj again: the lines a splice takes out take their +/// values with them, and a line it puts in is nobody's. +pub fn edited(p: *Pardes, f: *const File.State, off: usize, len: usize, bytes: []const u8) void { + var i = p.changes.items.len; + while (i > 0) { + i -= 1; + const c = &p.changes.items[i]; + const id = p.paneBySerial(c.serial) orelse continue; + const shown = if (p.panes[id].?.file) |*shown| shown else continue; + if (shown.content.ptr != f.content.ptr) continue; + splice(p.gpa, c, f.content, off, len, bytes) catch { + // No room for it: its values would lie, so it goes. + c.deinit(p.gpa); + _ = p.changes.swapRemove(i); + }; + } +} + +/// `c`'s values spliced as `text`'s lines are by the splice of `len` bytes +/// at `off` for `bytes`. A splice that starts in a line's leading blanks +/// (at its start, or after its indent, as `o` puts a line in) leaves that +/// line's text after what it puts in: the lines it takes and puts come +/// before it. Else the line it starts in keeps its value, and the lines +/// taken and put come after it. +fn splice(gpa: std.mem.Allocator, c: *Changes, text: []const u8, off: usize, len: usize, bytes: []const u8) error{OutOfMemory}!void { + const taken = std.mem.count(u8, text[off..][0..len], "\n"); + const put = std.mem.count(u8, bytes, "\n"); + if (taken == 0 and put == 0) return; + const line_start = if (std.mem.lastIndexOfScalar(u8, text[0..off], '\n')) |nl| nl + 1 else 0; + const in_lead = std.mem.trimStart(u8, text[line_start..off], " \t").len == 0; + const from = std.mem.count(u8, text[0..line_start], "\n") + @intFromBool(!in_lead); + if (from > c.lines.len) return; + const take = @min(taken, c.lines.len - from); + const next = try gpa.alloc(Line, c.lines.len - take + put); + @memcpy(next[0..from], c.lines[0..from]); + @memset(next[from..][0..put], .{ .pin = .none }); + @memcpy(next[from + put ..], c.lines[from + take ..]); + gpa.free(c.lines); + c.lines = next; +} + /// The session's end: the worker canceled, the asks let go. pub fn cancel(s: *pardes.Stream) void { if (s.changes) |t| { @@ -573,7 +617,7 @@ fn parse(gpa: std.mem.Allocator, arena: std.mem.Allocator, logged: []const u8, a kept = true; return .{ .read = out }; } - out.lines = try provenance(gpa, annotated, out.pins); + out.lines = try provenance(gpa, arena, annotated, out.pins, text); // Each change's hunks, and which of its lines it added whole. var hunks: std.ArrayList(Hunk) = .empty; defer hunks.deinit(gpa); @@ -608,22 +652,67 @@ fn pinOf(l: []const u8) ?Pin { return pin; } -/// Which pin wrote each line: a value a line, from `annotated` -/// (`annotate`'s answer); `whole` is the hunks' to say. The caller's, in -/// `gpa`. -pub fn provenance(gpa: std.mem.Allocator, annotated: []const u8, pins: []const Pin) error{OutOfMemory}![]Line { - var out: std.ArrayList(Line) = .empty; - errdefer out.deinit(gpa); - var lines = std.mem.splitScalar(u8, annotated, '\n'); - while (lines.next()) |id| { +/// Which pin wrote each line of `text` (what the pane shows: `whole` is the +/// hunks' to say), a value a line, the caller's in `gpa`, from `annotated` +/// (`jj file annotate`'s answer: an id, a tab and the line, a line each, of +/// the revision it annotated, which is jj's last snapshot of a file and +/// not the keys typed since): the two texts lined up by what they agree on +/// at either end (`File.spanOf`, the editor's differ: a line inside both +/// ends keeps its owner, a line only the pane has is nobody's). +pub fn provenance(gpa: std.mem.Allocator, arena: std.mem.Allocator, annotated: []const u8, pins: []const Pin, text: []const u8) error{OutOfMemory}![]Line { + var owners: std.ArrayList(PinIndex) = .empty; + var was: std.ArrayList(u8) = .empty; + var rows = std.mem.splitScalar(u8, annotated, '\n'); + while (rows.next()) |row| { // The empty text after the last newline is no line. - if (id.len == 0 and lines.peek() == null) break; - const at: PinIndex = for (pins, 0..) |pin, i| { - if (std.mem.eql(u8, &pin.id, id)) break @enumFromInt(i); - } else .none; - try out.append(gpa, .{ .pin = at }); + if (row.len == 0 and rows.peek() == null) break; + const tab = std.mem.indexOfScalar(u8, row, '\t') orelse row.len; + try owners.append(arena, for (pins, 0..) |pin, i| { + if (std.mem.eql(u8, &pin.id, row[0..tab])) break @enumFromInt(i); + } else .none); + try was.appendSlice(arena, row[@min(tab + 1, row.len)..]); + try was.append(arena, '\n'); + } + if (!std.mem.endsWith(u8, annotated, "\n") and was.items.len > 0) was.items.len -= 1; + const out = try gpa.alloc(Line, lineCount(text)); + @memset(out, .{ .pin = .none }); + if (std.mem.eql(u8, was.items, text)) { + for (out[0..@min(out.len, owners.items.len)], owners.items[0..@min(out.len, owners.items.len)]) |*l, pin| l.pin = pin; + return out; } - return out.toOwnedSlice(gpa); + const span = File.spanOf(was.items, text); + const head = @min(std.mem.count(u8, was.items[0..span.head], "\n"), out.len, owners.items.len); + for (out[0..head], owners.items[0..head]) |*l, pin| l.pin = pin; + var tail = @min(tailLines(was.items, span.tail), out.len - head, owners.items.len - head); + // The line the two ends meet inside (an insertion before it shares its + // indent with the line above) is the same line when its bytes are. + if (head + tail < @min(out.len, owners.items.len) and std.mem.eql(u8, lineFromEnd(was.items, tail), lineFromEnd(text, tail))) tail += 1; + for (out[out.len - tail ..], owners.items[owners.items.len - tail ..]) |*l, pin| l.pin = pin; + return out; +} + +/// `text`'s line `k` from its end (0: its last). +fn lineFromEnd(text: []const u8, k: usize) []const u8 { + var end = text.len - @intFromBool(std.mem.endsWith(u8, text, "\n")); + for (0..k) |_| end = (std.mem.lastIndexOfScalar(u8, text[0..end], '\n') orelse return ""); + const from = if (std.mem.lastIndexOfScalar(u8, text[0..end], '\n')) |nl| nl + 1 else 0; + return text[from..end]; +} + +/// The lines of `text`: one after each newline but a last one, as the +/// annotation numbers them. +fn lineCount(text: []const u8) usize { + return std.mem.count(u8, text, "\n") + @intFromBool(text.len > 0 and text[text.len - 1] != '\n'); +} + +/// How many of `text`'s lines lie whole in its last `tail` bytes. +fn tailLines(text: []const u8, tail: usize) usize { + if (tail == 0) return 0; + const from = text.len - tail; + // A line starts at `from`, and after each newline in the tail but the + // text's last byte. + const starts = if (from == 0) 1 else @intFromBool(text[from - 1] == '\n'); + return starts + std.mem.count(u8, text[from .. text.len - 1], "\n"); } /// Pin `k`'s hunks in `diffed` (its diff of the file, `--git --context=0`) onto @@ -1234,6 +1323,91 @@ test "Changes draws a lane a pinned change after the line number, nested ones a } } +/// A function three changes wrote: `fn` all of it, `mid` its lines 3-6 +/// (rewriting two, adding two), `shift` line 4 again. +const Stack = struct { + const fn_text = "fn f() void {\n a();\n b();\n c();\n g();\n h();\n i();\n}\n"; + const mid_text = "fn f() void {\n a();\n b2();\n c2();\n x();\n y();\n g();\n h();\n i();\n}\n"; + const shift_text = "fn f() void {\n a();\n b2();\n c3();\n x();\n y();\n g();\n h();\n i();\n}\n"; + /// Who wrote each of shift_text's lines: fn 0, mid 1, shift 2. + const pins = [_]u16{ 0, 0, 1, 2, 1, 1, 0, 0, 0, 0 }; + + fn repo(working: ?[]const u8) !?Repo { + return Repo.with(testing.allocator, &.{ .{ fn_text, "fn" }, .{ mid_text, "mid" }, .{ shift_text, "shift" } }, working); + } +}; + +fn pinsOf(ch: *const Changes, out: []u16) []u16 { + for (ch.lines, out[0..ch.lines.len]) |l, *o| o.* = @intFromEnum(l.pin); + return out[0..ch.lines.len]; +} + +test "Changes lines jj's annotation up with the pane's text: a line jj has not seen (typed, not snapshotted) is nobody's, the lines after it keep their changes" { + // The file on disk has a line more than jj's last snapshot of it. + const working = "fn f() void {\n a();\n b2();\n c3();\n x();\n new();\n y();\n g();\n h();\n i();\n}\n"; + var repo = try Stack.repo(working) orelse return error.SkipZigTest; + defer repo.deinit(testing.allocator); + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 24, .windowed = false }); + defer p.deinit(); + var line: [4200]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) }); + const pane = p.panes[p.view().active].?; + try testing.expectEqualStrings(working, pane.file.?.content); + p.update(.{ .command = "Changes ::@" }); + const none = @intFromEnum(PinIndex.none); + var got: [16]u16 = undefined; + try testing.expectEqualSlices(u16, &.{ 0, 0, 1, 2, 1, none, 1, 0, 0, 0, 0 }, pinsOf(answered(p, pane.serial).?, &got)); +} + +test "the lanes ride with the text: a line typed in a nested block is nobody's, its undo puts the values back, a deleted line takes its value; a client replays it equal" { + var repo = try Stack.repo(null) orelse return error.SkipZigTest; + defer repo.deinit(testing.allocator); + const R = pardes.replica; + const a = try R.Authority.init(testing.allocator, .{ .cols = 80, .rows = 24 }, .{}, false); + defer a.deinit(); + const r = try R.Replica.init(testing.allocator, 1, try a.unit(0, 0), false); + defer r.deinit(); + const Keys = struct { + fn send(au: *R.Authority, re: *R.Replica, keys: []const u8) !void { + for (keys) |k| { + const ev: pardes.Event = if (k == 27) .{ .key = .{ .cp = pardes.Key.escape } } else .{ .key = .{ .cp = k, .text = &[_]u8{k} } }; + re.confirm(try au.local(ev)); + } + re.settle(); + } + }; + var line: [4200]u8 = undefined; + r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) })); + r.confirm(try a.local(.{ .command = "Changes ::@" })); + r.settle(); + const serial = a.core.panes[a.core.view().active].?.serial; + const ch = answered(a.core, serial).?; + var got: [16]u16 = undefined; + try testing.expectEqualSlices(u16, &Stack.pins, pinsOf(ch, &got)); + const before = try testing.allocator.dupe(Line, ch.lines); + defer testing.allocator.free(before); + const none = @intFromEnum(PinIndex.none); + // A line opened below `c3();` (line 4, inside mid's block): nobody's; + // the rows around keep their lanes. + r.confirm(try a.local(.{ .command = "Look a.zig:4" })); + try Keys.send(a, r, "oz\x1b"); + try testing.expectEqualSlices(u16, &.{ 0, 0, 1, 2, none, 1, 1, 0, 0, 0, 0 }, pinsOf(answered(a.core, serial).?, &got)); + try testing.expectEqual(before[3].lanes, answered(a.core, serial).?.lines[3].lanes); + try testing.expectEqual(before[4].lanes, answered(a.core, serial).?.lines[5].lanes); + // Undone: the values as they were. + try Keys.send(a, r, "u"); + try testing.expectEqualSlices(Line, before, answered(a.core, serial).?.lines); + // `x();` (line 5, mid's) deleted: its value goes with it. + r.confirm(try a.local(.{ .command = "Look a.zig:5" })); + try Keys.send(a, r, ";xd"); + try testing.expectEqualStrings(Stack.shift_text[0..41] ++ Stack.shift_text[50..], a.core.panes[a.core.view().active].?.file.?.content); + try testing.expectEqualSlices(u16, &.{ 0, 0, 1, 2, 1, 0, 0, 0, 0 }, pinsOf(answered(a.core, serial).?, &got)); + try testing.expectEqual(before[5].lanes, answered(a.core, serial).?.lines[4].lanes); + try testing.expect(r.diverged == null); + try testing.expectEqualSlices(Line, answered(a.core, serial).?.lines, answered(r.core(), serial).?.lines); + try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); +} + test "a B3 click on path@rev:line in running text opens the file at that revision there" { const gpa = testing.allocator; var repo = try Repo.init(gpa) orelse return error.SkipZigTest; |
