diff options
Diffstat (limited to 'core/Text.zig')
| -rw-r--r-- | core/Text.zig | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/core/Text.zig b/core/Text.zig index 37feb081..3cf65b74 100644 --- a/core/Text.zig +++ b/core/Text.zig @@ -315,11 +315,18 @@ pub fn setRanges(t: *Text, tab: u16, text: []const u8, in: []const modal.Selecti if (n > 1 and !t.reserveSels()) return; // no room: the primary alone t.sticky_col = st[pri]; var w: usize = 0; + // The ranges are in the text's order now: each end's row counted on + // from the last one's, one pass to the last range, where a count from + // the top for each was a pass a range (an `s` over a big text's 1024 + // ranges, 445 million instructions a key). + var walked: Walked = .{}; for (r[0..n], 0..) |x, idx| { if (idx == pri) continue; const c = rangeCells(text, x); - const cc = modal.positionAt(text, c.cur); - const ac = modal.positionAt(text, c.anc); + const first = walked.at(text, @min(c.cur, c.anc)); + const last = walked.at(text, @max(c.cur, c.anc)); + const cc = if (c.cur <= c.anc) first else last; + const ac = if (c.cur <= c.anc) last else first; t.sels[w] = .{ .row = @as(i32, @intCast(cc.row)), .col = @intCast(cc.col), @@ -332,6 +339,24 @@ pub fn setRanges(t: *Text, tab: u16, text: []const u8, in: []const modal.Selecti t.nsel = @intCast(w); } +/// Offsets' positions in a text, asked in order: the rows counted on from +/// the last asked (modal.positionAt's answer); one asked before it, from +/// the top. +const Walked = struct { + off: usize = 0, + row: usize = 0, + + fn at(w: *Walked, text: []const u8, off: usize) modal.Cursor { + const bounded = @min(off, text.len); + if (bounded < w.off) return modal.positionAt(text, bounded); + w.row += std.mem.count(u8, text[w.off..bounded], "\n"); + w.off = bounded; + const s = if (std.mem.lastIndexOfScalar(u8, text[0..bounded], '\n')) |nl| nl + 1 else 0; + const e = std.mem.indexOfScalarPos(u8, text, s, '\n') orelse text.len; + return .{ .row = w.row, .col = modal.graphemeStart(text[s..e], @min(bounded - s, e - s)) }; + } +}; + /// helix `scroll` without cursor sync (Ctrl-f/b, PgUp/PgDn, zj/zk): shift /// the view, then snap a fallen-out cursor to the near scrolloff edge, col 0 pub fn scrollViewMove(t: *Text, tab: u16, text: []const u8, range: modal.Selection, delta: i32) void { @@ -698,3 +723,29 @@ pub fn step(t: *Text, gpa: std.mem.Allocator, current: ?EditBuffer, back: bool) t.vsel = back_to.vsel; return back_to; } + +test "setRanges puts each range's ends where positionAt says, counting rows on from the last" { + const pardes = @import("root.zig"); + const p = try pardes.Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const text = "ab\ncd ef\n\ngh ij kl\nmn\n"; + const t = &(try p.setTestFile(text)).body; + const in = [_]modal.Selection{ + .{ .anchor = 3, .head = 5 }, // cd + .{ .anchor = 8, .head = 6 }, // ef, backwards + .{ .anchor = 11, .head = 13 }, // gh + .{ .anchor = 14, .head = 16 }, // ij + .{ .anchor = 20, .head = 22 }, // mn + }; + t.setRanges(4, text, &in, &.{}, 0, true); + try std.testing.expectEqual(@as(u16, 4), t.nsel); + for (in[1..], t.sels[0..4]) |r, s| { + const c = rangeCells(text, r); + const cc = modal.positionAt(text, c.cur); + const ac = modal.positionAt(text, c.anc); + try std.testing.expectEqual(@as(i32, @intCast(cc.row)), s.row); + try std.testing.expectEqual(@as(i32, @intCast(cc.col)), s.col); + try std.testing.expectEqual(@as(i32, @intCast(ac.row)), s.arow); + try std.testing.expectEqual(@as(i32, @intCast(ac.col)), s.acol); + } +} |
