diff options
Diffstat (limited to 'core/look.zig')
| -rw-r--r-- | core/look.zig | 40 |
1 files changed, 34 insertions, 6 deletions
diff --git a/core/look.zig b/core/look.zig index 53c26943..41b48393 100644 --- a/core/look.zig +++ b/core/look.zig @@ -747,20 +747,29 @@ pub fn focusPaneLine(p: *Pardes, id: usize, at: Spot, landing: enum { center, ke } } } - pane.body.cur_row = @intCast(at.line - 1); - pane.body.cur_col = if (at.col > 0) @intCast(at.col - 1) else 0; + 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); pane.body.cur_pinned = true; if (at.end_line != 0) return selectSpan(p, pane, at); pane.ensureCursorVisible(p.settings.tab); } +/// A place's column (from 1) as a cursor's on `row`: past the line's end (a +/// server's answer, a typed number: any) it is the end, as a search's +/// landing and clampCursor have it. A terminal's (a jump's) as it was. +fn columnOn(pane: *const Pane, row: i32, col: usize) i32 { + if (pane.file == null) return std.math.lossyCast(i32, col -| 1); + return @intCast(@min(col -| 1, panes.File.sourceLine(pane, row).len)); +} + fn selectSpan(p: *Pardes, pane: *Pane, at: Spot) void { const text = edit.flatSurface(p.editor(), &pane.body) catch |err| return p.reportError(.at(p.view().active), "look", err); // saturating, not `- 1`: `f.zig:0-5` is a legal thing to type and a // 1-based zero is the same nothing an absent number is - const acol: i32 = @intCast(at.col -| 1); - const ecol: i32 = if (at.end_col > 0) @intCast(at.end_col - 1) else std.math.maxInt(i32); - const r = panes.Text.cellRange(text, @intCast(at.line -| 1), acol, @intCast(at.end_line -| 1), ecol); + // Any number a place has: cellRange clamps each to the text. + const acol = std.math.lossyCast(i32, at.col -| 1); + const ecol: i32 = if (at.end_col > 0) std.math.lossyCast(i32, at.end_col - 1) else std.math.maxInt(i32); + const r = panes.Text.cellRange(text, std.math.lossyCast(i32, at.line -| 1), acol, std.math.lossyCast(i32, at.end_line -| 1), ecol); pane.body.setRange(p.settings.tab, text, 0, r, true); } @@ -1590,7 +1599,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { const free = p.freeSlot() orelse return p.reportError(.at(id), "look", error.NoPaneSlots); const nt = panes.File.open(p, free, target.path, target.at.line) catch |err| return lookOpenFailed(p, id, target.path, err); - if (target.at.col > 0) nt.body.cur_col = @intCast(target.at.col - 1); + nt.body.cur_col = columnOn(nt, nt.body.cur_row, target.at.col); const failures = p.fs.failures; layout.placeNew(p, id, id, free, .doc); // Refused a place: that is the look's one failure, not a @@ -2010,6 +2019,25 @@ test "Look copies its operand before releasing pointer selection storage" { try std.testing.expectEqual(.none, p.view().drag); } +test "a Look whose column is past its line's end lands at the end, however big; a line past the file's is a miss" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("alpha\n\tbeta\n"); + // Up to 2026-11-23 a column past i32 panicked the session (`@intCast` + // in focusPaneLine): any text a B3 or Look takes, a server's row. + for ([_][]const u8{ "/test.txt:2:9", "/test.txt:2:3000000000", "/test.txt:2:18446744073709551615" }) |place| { + lookAt(p, 0, place); + try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); + try std.testing.expectEqual(@as(i32, 5), pane.body.cur_col); + } + // A span past the text: selected to its end. + lookAt(p, 0, "/test.txt:1:2-3000000000:3000000000"); + try std.testing.expect(pane.body.vsel.active); + pane.body.cur_row = 0; + lookAt(p, 0, "/test.txt:3000000000:1"); + try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row); +} + test "@p names a pane by its serial, as /index does" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); defer p.deinit(); |
