From f7de6f49ee3d9ac241575f5244743193c9943841 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 21 Nov 2026 07:02:01 -0300 Subject: layers: what round 1 left, three places where a revision's pane or a patch's did not do what the design's words say: a B3 click on path@rev[:line] in running text opens the file at that revision there (the click already expanded the whole token, @ being a file character; jj's test now drives it through the mouse); a revision's text is read-only over 9P too: an open of its body, data, xdata or sel to write is refused in words ("a revision is read-only", errno EROFS, as enameErrno reads the words: core/ninep/tree.zig's open, the one road 9P has to the text; the editor's own two are File.edit's splice and edit.zig's whole text), where before the write reached File.edit and was answered "out of memory"; Syntax off in a patch's pane takes the language's ink and leaves the patch: its bytes are made again once a paint without the language's (syntax.highlightDiffPlain), grounds, markers and changed words as they were (round 1 took them too); tests: the click, the four opens, a .zig patch's cells' grounds the same and its ink not; tui core-test 893/1053 (160 skipped) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/File.zig | 57 +++++++++++++++++++++++++++++++++++++++++++++++------ core/jj.zig | 49 +++++++++++++++++++++++++++++++++++++++++++++ core/ninep/tree.zig | 8 ++++++++ core/syntax.zig | 19 ++++++++++++++---- 4 files changed, 123 insertions(+), 10 deletions(-) (limited to 'core') diff --git a/core/File.zig b/core/File.zig index 01aaeec8..450e4ec9 100644 --- a/core/File.zig +++ b/core/File.zig @@ -1864,8 +1864,8 @@ fn fillBody(tab: u16, dst: ?[]u8, pane: *Pane, f: *State, width: usize, record_w /// 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). +/// 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). Ruled, not @@ -1886,10 +1886,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 { - // 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; + // 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; @@ -2614,3 +2620,42 @@ test "Syntax takes a text's ink off in this view and puts it back, and another v 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); + } +} diff --git a/core/jj.zig b/core/jj.zig index 429ea9c9..7784c1c1 100644 --- a/core/jj.zig +++ b/core/jj.zig @@ -419,3 +419,52 @@ test "provenance: a value a line, which pinned change wrote it, from what jj ans }, } } + +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; + defer repo.deinit(gpa); + try repo.tmp.dir.writeFile(testing.io, .{ .sub_path = "notes.txt", .data = "see a.zig@@-:2 here\n" }); + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 30 }); + defer p.deinit(); + var line: [4200]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/notes.txt", .{repo.dir}) }); + const notes = p.view().active; + p.sync(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const s = try p.render(arena.allocator()); + const body = s.region(.body, notes).?.rect; + // On the `@` of the address, the body's first row. + const x: u16 = body.x + File.gutterWidth(p.panes[notes].?) + @as(u16, "see a.zig".len); + p.update(.{ .mouse = .{ .button = .right, .kind = .press, .col = x, .row = body.y } }); + p.update(.{ .mouse = .{ .button = .right, .kind = .release, .col = x, .row = body.y } }); + const shown = p.view().active; + try testing.expect(shown != notes); + const f = &p.panes[shown].?.file.?; + try testing.expect(of(f) != null); + try testing.expectEqualStrings(Repo.two, f.content); + try testing.expectEqual(@as(i32, 1), p.panes[shown].?.body.cur_row); +} + +test "a revision's text is read-only over 9P too: an open to write it is refused in words" { + 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 = 30 }); + 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.expect(of(&pane.file.?) != null); + const th = @import("ninep/harness.zig"); + const tree = @import("ninep/tree.zig"); + for ([_]tree.PaneFile{ .body, .data, .xdata, .sel }) |file| { + const got = th.call(p, .{ .tag = 1, .op = .open, .node = tree.Node.of(pane.serial, file), .omode = 1 }); + try testing.expectEqual(@as(u16, 30), got.errno()); + try testing.expectEqual(@as(u16, 30), @import("cloud9").fs.enameErrno(got.reply.ename)); + } + // Reading it is as any text's. + try testing.expectEqual(@as(u16, 0), th.call(p, .{ .tag = 1, .op = .open, .node = tree.Node.of(pane.serial, .body), .omode = 0 }).errno()); + try testing.expectEqualStrings(Repo.two, pane.file.?.content); +} diff --git a/core/ninep/tree.zig b/core/ninep/tree.zig index 2c0ce1b0..01cfe297 100644 --- a/core/ninep/tree.zig +++ b/core/ninep/tree.zig @@ -57,6 +57,10 @@ pub fn failText(tag: u64, errno: u16, text: []const u8) Reply { } pub const e_bad_ctl = "ill-formed control message"; +/// A revision's text refused a write (cloud9's E has no EROFS; its words +/// map back to it: enameErrno). +pub const e_revision = "a revision is read-only"; +const rofs: u16 = 30; pub const e_bad_event = "bad event syntax"; /// A second open reading a file whose reads consume: rio's word for it /// (windows/rio/xfid.c:25), which 9ns turns into EBUSY. @@ -933,6 +937,10 @@ fn open(p: *Pardes, req: Req, target: Target) Reply { const pn = p.panes[p.paneBySerial(t.serial) orelse return Reply.fail(req.tag, E.NOENT)].?; if (t.file.inPty() and !pn.isTerminal()) return Reply.fail(req.tag, E.NOENT); pane_of = pn; + // A file at a revision is read-only (jj.zig): no open writes its + // text, the one road 9P has to it. + if (req.omode & 3 != 0 and (t.file == .body or t.file == .data or t.file == .xdata or t.file == .sel)) + if (pn.file) |*f| if (@import("../jj.zig").of(f) != null) return failText(req.tag, rofs, e_revision); const reader = req.omode & 3 != 1; // OWRITE is the one mode that never reads break :what switch (t.file) { .body => if (pn.isTerminal()) .{ .snapshot = null } else if (req.omode & 3 == 0) return .{ .tag = req.tag } else .writes, diff --git a/core/syntax.zig b/core/syntax.zig index 1bb8fb52..08ea1fb4 100644 --- a/core/syntax.zig +++ b/core/syntax.zig @@ -1014,6 +1014,17 @@ pub fn highlightDiff(gpa: std.mem.Allocator, content: []const u8, start_raw: usi /// The same for a terminal's rows, where a row the terminal wrapped /// continues the diff line above it. pub fn highlightDiffRows(gpa: std.mem.Allocator, content: []const u8, start_raw: usize, end_raw: usize, continued_rows: bool) ![]u8 { + return diffStyles(gpa, content, start_raw, end_raw, continued_rows, true); +} + +/// `highlightDiff` without the language's ink: each hunk line in one +/// colour as a file in no language pardes knows has it, the grounds, markers +/// and changed words as they are (a patch's pane with `Syntax` off). +pub fn highlightDiffPlain(gpa: std.mem.Allocator, content: []const u8, start_raw: usize, end_raw: usize) ![]u8 { + return diffStyles(gpa, content, start_raw, end_raw, false, false); +} + +fn diffStyles(gpa: std.mem.Allocator, content: []const u8, start_raw: usize, end_raw: usize, continued_rows: bool, language: bool) ![]u8 { const tz = tracy.zone(@src(), "highlightDiff"); defer tz.end(); const start_byte = @min(start_raw, content.len); @@ -1041,7 +1052,7 @@ pub fn highlightDiffRows(gpa: std.mem.Allocator, content: []const u8, start_raw: else => false, }; if (piece_start) |ps| if (!body or nkinds == diff_piece_cut) { - if (piece_end >= start_byte) try paintDiffPiece(gpa, window, content[ps..piece_end], ps, kinds[0..nkinds], piece_path); + if (piece_end >= start_byte) try paintDiffPiece(gpa, window, content[ps..piece_end], ps, kinds[0..nkinds], piece_path, language); piece_start = null; nkinds = 0; }; @@ -1066,7 +1077,7 @@ pub fn highlightDiffRows(gpa: std.mem.Allocator, content: []const u8, start_raw: at = nl + 1; } if (piece_start) |ps| if (piece_end >= start_byte) - try paintDiffPiece(gpa, window, content[ps..piece_end], ps, kinds[0..nkinds], piece_path); + try paintDiffPiece(gpa, window, content[ps..piece_end], ps, kinds[0..nkinds], piece_path, language); return styles; } @@ -1087,8 +1098,8 @@ const DiffWindow = struct { } }; -fn paintDiffPiece(gpa: std.mem.Allocator, window: DiffWindow, bytes: []const u8, at: usize, kinds: []const diff.Kind, path: []const u8) !void { - const selected: ?Selected = if (syntax_started) (forExt(std.Io.Dir.path.extension(path)) catch null) else null; +fn paintDiffPiece(gpa: std.mem.Allocator, window: DiffWindow, bytes: []const u8, at: usize, kinds: []const diff.Kind, path: []const u8, language: bool) !void { + const selected: ?Selected = if (language and syntax_started) (forExt(std.Io.Dir.path.extension(path)) catch null) else null; if (selected) |sel| { const hash = std.hash.Wyhash.hash(std.hash.Wyhash.hash(0, sel.name), bytes); diff_clock += 1; -- cgit v1.3