diff options
Diffstat (limited to 'core/normal.zig')
| -rw-r--r-- | core/normal.zig | 94 |
1 files changed, 88 insertions, 6 deletions
diff --git a/core/normal.zig b/core/normal.zig index ae6c2d18..982ab656 100644 --- a/core/normal.zig +++ b/core/normal.zig @@ -789,8 +789,12 @@ fn multiSelAction(tab: u16, t: *Text, text: []const u8, role: modal.Normal.Role, for (rs[0..n], 0..) |r, ri| { const is_pri = ri == got.pri; // head-exclusive: back the leading end off onto its own cell - const hp = modal.positionAt(text, if (r.anchor < r.head) modal.prevGrapheme(text, r.head) else r.head); - const ap = modal.positionAt(text, if (r.anchor < r.head) r.anchor else modal.prevGrapheme(text, r.anchor)); + const h_off = if (r.anchor < r.head) modal.prevGrapheme(text, r.head) else r.head; + const a_off = if (r.anchor < r.head) r.anchor else modal.prevGrapheme(text, r.anchor); + const hp = modal.positionAt(text, h_off); + const ap = modal.positionAt(text, a_off); + var al: LineWalk = .of(text, a_off, ap.row); + var hl: LineWalk = .of(text, h_off, hp.row); const height = @max(hp.row, ap.row) - @min(hp.row, ap.row) + 1; if (m >= Text.max_selections) break; if (is_pri) pri = m; @@ -803,12 +807,12 @@ fn multiSelAction(tab: u16, t: *Text, text: []const u8, role: modal.Normal.Role, const arow = if (below) ap.row + d else ap.row -| d; const hrow = if (below) hp.row + d else hp.row -| d; if (arow >= nlines or hrow >= nlines) break; - const a2 = modal.offsetAt(text, .{ .row = arow, .col = ap.col }); - const h2 = modal.offsetAt(text, .{ .row = hrow, .col = hp.col }); // a line too short to reach the column is skipped, not clamped - if (modal.positionAt(text, a2).col == ap.col and modal.positionAt(text, h2).col == hp.col) { + const a2 = al.cell(text, arow, ap.col); + const h2 = hl.cell(text, hrow, hp.col); + if (a2 != null and h2 != null) { if (is_pri) pri = m; - out[m] = modal.moveSelectionCursor(text, .{ .anchor = a2, .head = a2 }, h2, true); + out[m] = modal.moveSelectionCursor(text, .{ .anchor = a2.?, .head = a2.? }, h2.?, true); m += 1; made += 1; } @@ -818,6 +822,28 @@ fn multiSelAction(tab: u16, t: *Text, text: []const u8, role: modal.Normal.Role, t.setRanges(tab, text, out[0..m], &.{}, pri, expl); } +/// A line's start, walked on from the row C or Alt-C last tried: found +/// from the text's start, each try was a pass over all before it, and a +/// column no near line reaches hung the editor on a long text. +const LineWalk = struct { + row: usize, + start: usize, + + fn of(text: []const u8, off: usize, row: usize) LineWalk { + return .{ .row = row, .start = if (std.mem.lastIndexOfScalar(u8, text[0..off], '\n')) |nl| nl + 1 else 0 }; + } + + /// Column `col` of `row`, or null where the line ends before it or a + /// grapheme there spans it. + fn cell(w: *LineWalk, text: []const u8, row: usize, col: usize) ?usize { + while (w.row < row) : (w.row += 1) w.start = std.mem.indexOfScalarPos(u8, text, w.start, '\n').? + 1; + while (w.row > row) : (w.row -= 1) w.start = if (std.mem.lastIndexOfScalar(u8, text[0 .. w.start - 1], '\n')) |nl| nl + 1 else 0; + const e = std.mem.indexOfScalarPos(u8, text, w.start, '\n') orelse text.len; + const g = modal.graphemeStart(text[w.start..e], @min(col, e - w.start)); + return if (g == col) w.start + g else null; + } +}; + fn maxLine(text: []const u8) usize { const nl = modal.cursorLineCount(text); return if (text.len == 0 or text[text.len - 1] == '\n') nl -| 2 else nl - 1; @@ -1547,6 +1573,62 @@ test "one cursor holds no room for other ranges; a second range makes it and one try std.testing.expectEqual(@as(usize, 0), pane.body.sels.len); } +test "C and Alt-C on a long text of short lines walk its lines once, not each try from its start" { + // Work, not time: the instructions this thread runs, as pardes-perf's + // gate counts them. Each try walking from the text's start made C here + // cost billions (the bytes soak's hang, at row 41,775 of a flood). + if (@import("builtin").os.tag != .linux) return error.SkipZigTest; + const linux = std.os.linux; + var attr: linux.perf_event_attr = .{ + .type = .HARDWARE, + .config = @intFromEnum(linux.PERF.COUNT.HW.INSTRUCTIONS), + .flags = .{ .exclude_kernel = true, .exclude_hv = true }, + }; + const opened = linux.perf_event_open(&attr, 0, -1, -1, linux.PERF.FLAG.FD_CLOEXEC); + if (linux.errno(opened) != .SUCCESS) return error.SkipZigTest; // no counter here (perf_event_paranoid 3) + const fd: linux.fd_t = @intCast(opened); + defer _ = linux.close(fd); + const Count = struct { + fn now(f: linux.fd_t) u64 { + var n: u64 = 0; + _ = linux.read(f, std.mem.asBytes(&n), @sizeOf(u64)); + return n; + } + }; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 12 }); + defer p.deinit(); + // only the first line and the last reach column 3 + const lines = 50_000; + const text = try gpa.alloc(u8, 2 * 5 + (lines - 2) * 2); + defer gpa.free(text); + @memcpy(text[0..5], "abcd\n"); + for (0..lines - 2) |k| @memcpy(text[5 + 2 * k ..][0..2], "x\n"); + @memcpy(text[text.len - 5 ..], "abcd\n"); + const pane = try p.setTestFile(text); + const body = &pane.body; + // C walked once costs 194 million here in Debug, about 4,000 a line; + // walked from the start at every try it cost 306 billion + const bound = 10_000 * lines; + for ("lll") |cp| _ = handleNormal(p.editor(), body, .{ .cp = cp }); + var rs: [Text.max_selections]modal.Selection = undefined; + for ([_]Key{ .{ .cp = 'C' }, .{ .cp = 'C', .alt = true } }) |key| { + const t0 = Count.now(fd); + _ = handleNormal(p.editor(), body, key); + const cost = Count.now(fd) - t0; + const got = body.ranges(text, 0, &rs); + try std.testing.expectEqual(2, got.n); + try std.testing.expectEqual(3, @min(rs[0].anchor, rs[0].head)); + try std.testing.expectEqual(text.len - 2, @min(rs[1].anchor, rs[1].head)); + std.testing.expect(cost < bound) catch |err| { + std.debug.print("{u} (alt {}): {d} instructions, the bound {d}\n", .{ key.cp, key.alt, cost, bound }); + return err; + }; + // the new cursor alone, for the twin to walk back from + _ = handleNormal(p.editor(), body, .{ .cp = ',' }); + } +} + test "cursors that land on one place are one: a key typed there is typed once, as helix merges them" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 12 }); |
