diff options
Diffstat (limited to 'core/look.zig')
| -rw-r--r-- | core/look.zig | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/core/look.zig b/core/look.zig index 72bb42eb..ee6debf9 100644 --- a/core/look.zig +++ b/core/look.zig @@ -499,9 +499,7 @@ pub fn jumpResult(p: *Pardes, id: usize, row: usize) bool { rp.body.nsel = 0; rp.body.cur_row = r; rp.body.cur_pinned = true; - const off = rp.scroll(); - const last = off + @as(i32, rp.rows) - 1; - if (r < off) rp.scrollBy(r - off) else if (r > last) rp.scrollBy(r - last); + if (rp.file != null) panes.File.show(p.settings.tab, rp, r, 0, 0); var realbuf: [4096]u8 = undefined; const span = if (tr.commands) wholeRowSpan(ln) @@ -820,14 +818,9 @@ pub fn focusPaneLine(p: *Pardes, id: usize, at_in: Spot, landing: enum { center, }; if (pane.file) |*f| { if (at.line > panes.File.nlines(p.gpa, f)) return; - if (landing == .center) { - // center, clamp at top; a text that fits is shown whole - const next = if (pane.textFits()) 0 else (at.line - 1) -| pane.rows / 2; - if (next != f.scroll) { - f.scroll = next; - f.syntax_dirty = true; - } - } + // centred (its first row of text half the body's rows down, as + // zz), clamped at the top; a text that fits is shown whole + if (landing == .center) panes.File.setTop(pane, if (pane.textFits()) .{ .line = 0 } else panes.File.walkRows(p.settings.tab, pane, .{ .line = at.line - 1, .row = panes.File.rowOf(p.settings.tab, pane, at.line - 1, 0) }, -@as(i64, pane.rows / 2)).to); } pane.body.cur_row = std.math.cast(i32, at.line - 1) orelse return; pane.body.cur_col = columnOn(pane, pane.body.cur_row, at.col); @@ -1697,11 +1690,13 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { // miss of its line too. if (p.fs.failures != failures) return; layout.compute(p); - nt.file.?.scroll -|= @max(1, v.rects[free].h -| BOX_H) / 2; + // Its line half the body's rows down, a wrapped line's + // rows above it counted. + const centered = panes.File.walkRows(p.settings.tab, nt, .{ .line = nt.file.?.scroll }, -@as(i64, @max(1, v.rects[free].h -| BOX_H) / 2)).to; + panes.File.setTop(nt, centered); if (target.at.end_line != 0) { - const centered = nt.file.?.scroll; selectSpan(p, nt, target.at); - nt.file.?.scroll = centered; + panes.File.setTop(nt, centered); } opened = free; } @@ -1932,6 +1927,35 @@ test "lookableLineSpan prefers the longest run, so a blank inside a path is one ); } +test "a Look that opens a file in a pane of its own puts its line half the body's rows down, a wrapped line's rows above it counted" { + const gpa = std.testing.allocator; + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + // Line 10 of 600 bytes, eleven rows or more in a pane 60 wide. + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + for (0..60) |i| if (i == 10) { + try text.appendNTimes(gpa, 'a', 600); + try text.append(gpa, '\n'); + } else try text.print(gpa, "line {d:0>2}\n", .{i}); + try tmp.dir.writeFile(io, .{ .sub_path = "c.txt", .data = text.items }); + var buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(io, &buf)]; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 40 }); + defer p.deinit(); + p.settings.wrap = true; + _ = try p.setTestFile("x\n"); + var line: [4200]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}/c.txt:14", .{dir}) }); + const id = p.view().active; + const pane = p.panes[id].?; + try std.testing.expect(panes.File.lineRows(p.settings.tab, pane, 10) >= 11); + try std.testing.expectEqual(@as(i32, 13), pane.body.cur_row); + const half: i64 = @max(1, p.view().rects[id].h -| BOX_H) / 2; + try std.testing.expectEqual(half, panes.File.rowsTo(p.settings.tab, pane, panes.File.top(p.settings.tab, pane), 13, 100).?); +} + test "a miss names a long path by its start and its end, its middle given up to …" { var cut: [miss_path_cap + "…".len]u8 = undefined; const long = "/home/someone/" ++ std.mem.asBytes(&@as([80][5]u8, @splat("deep/".*))) ++ "the-file.zig"; @@ -2313,6 +2337,7 @@ test "Look on a diff: headers and a hunk line's prefix are the whole line, hover // Back to the diff, scrolled to its top again. p.view().active = 0; pane.file.?.scroll = 0; + pane.file.?.scroll_row = 0; p.sync(); _ = frame.reset(.retain_capacity); _ = try p.render(frame.allocator()); @@ -2386,6 +2411,7 @@ test "a click, a 9P whole-line look and an empty event write-back take one expan for (points) |at| { p.view().active = 0; pane.file.?.scroll = 0; + pane.file.?.scroll_row = 0; pane.body.cur_row = 0; p.sync(); _ = frame.reset(.retain_capacity); @@ -2462,6 +2488,7 @@ test "every entry point to Look takes one expansion: a diff line and a file:12:5 pane.body.cur_col = case.col; pane.body.cur_pinned = true; pane.file.?.scroll = 0; + pane.file.?.scroll_row = 0; p.sync(); _ = frame.reset(.retain_capacity); _ = try p.render(frame.allocator()); |
