From 2b0d9d607094739133470c10af51fd670cd63ed3 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 21 Oct 2026 21:05:10 -0300 Subject: A drag started on a file's line numbers copies only the text --- src/edit.zig | 19 ++++++------------- src/panes.zig | 2 -- src/pardes.zig | 13 +++++++++++++ 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/edit.zig b/src/edit.zig index 5dbce0bf..466b5cdb 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -229,10 +229,10 @@ pub fn capturePointerSelection(p: *Pardes, pane: *Pane, slot: usize) !void { const prefix: i32 = if (pane.file != null) panes.File.gutterWidth(pane) else 0; for (rows, 0..) |*row, index| { const visible = first + @as(i32, @intCast(index)); - // A stream: only the first row starts where the press was; the - // others start after the line numbers, and all but the last run on. + // A stream: only the first row starts where the press was, and no + // row takes in the line numbers; all but the last run on. const cols = sweepCols(gesture, visible + @as(i32, pane.tag_rows)).?; - const left: i32 = if (cols.first) cols.lo else prefix; + const left: i32 = if (cols.first) @max(cols.lo, prefix) else prefix; const right: i32 = cols.hi orelse std.math.maxInt(i32) / 2; var source = pane.wrapAt(visible); if (raw) source = .{ .line = panes.terminal.gridOffset(pane) + visible, .at = 0 }; @@ -251,11 +251,6 @@ pub fn capturePointerSelection(p: *Pardes, pane: *Pane, slot: usize) !void { .raw_terminal = raw, .prompt_bytes = if (raw) panes.terminal.promptPrefixBytes(pane, source.line, line) else 0, }; - const rendered = modal.lineSlice(body, @intCast(visible)); - const prefix_end = @min(rendered.len, @as(usize, @intCast(@min(prefix, right + 1)))); - const prefix_start = @min(prefix_end, @as(usize, @intCast(left))); - row.prefix_len = @intCast(prefix_end - prefix_start); - @memcpy(row.prefix[0..row.prefix_len], rendered[prefix_start..prefix_end]); } pane.next_pointer_selection += 1; gesture.source_id = pane.next_pointer_selection; @@ -273,7 +268,7 @@ pub fn selectionText(p: *Pardes, pane: *Pane, sl: Pane.Sel) ![]const u8 { var total: usize = rows.len -| 1; for (rows) |row| { const line = pointerSourceLine(pane, terminal_lines, row.row); - total += row.prefix_len + @min(row.hi, line.len) - @min(row.lo, line.len); + total += @min(row.hi, line.len) - @min(row.lo, line.len); } const result = try arena.alloc(u8, total); var at: usize = 0; @@ -282,8 +277,6 @@ pub fn selectionText(p: *Pardes, pane: *Pane, sl: Pane.Sel) ![]const u8 { result[at] = '\n'; at += 1; } - @memcpy(result[at..][0..row.prefix_len], row.prefix[0..row.prefix_len]); - at += row.prefix_len; const line = pointerSourceLine(pane, terminal_lines, row.row); const bytes = line[@min(row.lo, line.len)..@min(row.hi, line.len)]; @memcpy(result[at..][0..bytes.len], bytes); @@ -307,9 +300,9 @@ pub fn selectionText(p: *Pardes, pane: *Pane, sl: Pane.Sel) ![]const u8 { ) != null; if (!first and !soft) try out.append(arena, '\n'); first = false; - // Past the first row a body row starts after its line numbers. + // A body row starts after its line numbers, wherever the press was. const gutter: i32 = if (pane.file != null and v >= pane.tag_rows) panes.File.gutterWidth(pane) else 0; - const lo: usize = @intCast(if (cols.first) cols.lo else gutter); + const lo: usize = @intCast(if (cols.first) @max(cols.lo, gutter) else gutter); const b0 = @min(panes.File.renderedLineByteCol(pane, v, line, lo), line.len); const b1 = if (cols.hi) |hi| @max(b0, modal.nextGrapheme(line, @min(panes.File.renderedLineByteCol(pane, v, line, @intCast(hi)), line.len))) diff --git a/src/panes.zig b/src/panes.zig index 07d5ccb1..7ce85419 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -49,8 +49,6 @@ pub const Pane = struct { hi: usize, /// Not the sweep's last row: its highlight runs to the right edge. to_edge: bool = false, - prefix: [32]u8 = @splat(0), - prefix_len: u8 = 0, raw_terminal: bool = false, prompt_bytes: usize = 0, }; diff --git a/src/pardes.zig b/src/pardes.zig index 7e166847..2a94c0f0 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -9001,6 +9001,19 @@ test "board heap: every allocation failure during boot is a clean OutOfMemory" { ); } +test "a sweep pressed on the line numbers copies only the text" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("alpha beta\ngamma delta\n"); + const numbers: i32 = panes.File.gutterWidth(pane); + const top: i32 = pane.tag_rows; + pane.sel[0] = .{ .state = .done, .c0 = 1, .c1 = numbers + 4, .r0 = top, .r1 = top + 1 }; + // Live, while the drag is held, and captured, once it is released. + try std.testing.expectEqualStrings("alpha beta\ngamma", try edit.selectionText(p, pane, pane.sel[0])); + try edit.capturePointerSelection(p, pane, 0); + try std.testing.expectEqualStrings("alpha beta\ngamma", try edit.selectionText(p, pane, pane.sel[0])); +} + test "jump history clears selections and lands at the recorded cursor" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); defer p.deinit(); -- cgit v1.3