From 6cddaba55b83e6c164cdd04eb7972590bf195e34 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 23:48:34 -0300 Subject: edit, Text: the four one-edit multi-range ops (J, Alt-(, &, Ctrl-c) widen their rows with allSelRows and end with setEditCells, which puts the text in and selects the ranges' cells, instead of four copies of both Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/edit.zig | 138 ++++++++++++++++++++++------------------------------------- 1 file changed, 51 insertions(+), 87 deletions(-) (limited to 'src/edit.zig') diff --git a/src/edit.zig b/src/edit.zig index 14060f45..f6722499 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -1390,6 +1390,33 @@ fn selRows(t: *Text) Bounds { return .{ .lo_row = t.cur_row, .lo_col = t.cur_col, .hi_row = t.cur_row, .hi_col = t.cur_col }; } +/// `selRows` widened over every range: the rows a one-edit op's buffer +/// must cover. +fn allSelRows(t: *Text) Bounds { + var b = selRows(t); + for (t.sels[0..t.nsel]) |s| { + b.lo_row = @min(b.lo_row, @min(s.row, s.arow)); + b.hi_row = @max(b.hi_row, @max(s.row, s.arow)); + } + return b; +} + +/// Put in `new`, an edit of the buffer whose first line is surface row +/// `row0`, and select `cells`, each range's cursor and anchor cells in it. +fn setEditCells(p: *Pardes, t: *Text, new: []u8, row0: i32, cells: []const Text.Cells, pri: usize, expl: bool) void { + var sels: [Text.max_selections]Text.SelRange = undefined; + for (cells, sels[0..cells.len]) |c, *s| { + const cc = modal.positionAt(new, c.cur); + const ac = modal.positionAt(new, c.anc); + s.* = .{ .row = @as(i32, @intCast(cc.row)) + row0, .col = @intCast(cc.col), .arow = @as(i32, @intCast(ac.row)) + row0, .acol = @intCast(ac.col) }; + } + setEditText(p, t, new); + const text = flatSurface(p, t) catch return; + var rs: [Text.max_selections]modal.Selection = undefined; + for (sels[0..cells.len], rs[0..cells.len]) |s, *r| r.* = Text.cellRange(text, s.arow, s.acol, s.row, s.col); + t.setRanges(text, rs[0..cells.len], &.{}, pri, expl); +} + /// `r`: overwrite the selection (or the cursor char) with ch — /// newlines included (helix), so `xrz` joins the selected lines pub fn normalReplaceChar(p: *Pardes, t: *Text, ch: u21) void { @@ -1496,11 +1523,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void { const expl = (t.vsel.active and t.vsel.explicit) or t.msel.active; // a join always eats the newline of its last line, so the buffer has // to reach one row PAST every range - var b = selRows(t); - for (t.sels[0..t.nsel]) |s| { - b.lo_row = @min(b.lo_row, @min(s.row, s.arow)); - b.hi_row = @max(b.hi_row, @max(s.row, s.arow)); - } + const b = allSelRows(t); const eb = editText(p, t, b.lo_row, b.hi_row + 1, -1) orelse return; const text = eb.text; var rs: [Text.max_selections]modal.Selection = undefined; @@ -1537,7 +1560,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void { const new = sam.apply(p.gpa, text, chs[0..chs_len]) catch return; // every range's cells ride through the edit; Alt-J replaces them with // the spaces, when it put any in - var cells: [Text.max_selections]Text.SelRange = undefined; + var cells: [Text.max_selections]Text.Cells = undefined; var n: usize = 0; var pri = got.pri; if (select_space) { @@ -1546,31 +1569,20 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void { const at: usize = @intCast(@as(i64, @intCast(ch.q0)) + delta); delta += @as(i64, @intCast(ch.text.len)) - @as(i64, @intCast(ch.q1 - ch.q0)); if (ch.text.len == 0 or n == cells.len) continue; - const pos = modal.positionAt(new, at); - cells[n] = .{ .row = @as(i32, @intCast(pos.row)) + eb.row0, .col = @intCast(pos.col), .arow = @as(i32, @intCast(pos.row)) + eb.row0, .acol = @intCast(pos.col) }; + cells[n] = .{ .cur = at, .anc = at }; n += 1; } pri = 0; } if (n == 0) { - for (rs[0..got.n]) |r| { + for (rs[0..got.n], cells[0..got.n]) |r, *cell| { const c = Text.rangeCells(text, r); - const cc = modal.positionAt(new, mapThroughChanges(chs[0..chs_len], c.cur)); - const ac = modal.positionAt(new, mapThroughChanges(chs[0..chs_len], c.anc)); - cells[n] = .{ - .row = @as(i32, @intCast(cc.row)) + eb.row0, - .col = @intCast(cc.col), - .arow = @as(i32, @intCast(ac.row)) + eb.row0, - .acol = @intCast(ac.col), - }; - n += 1; + cell.* = .{ .cur = mapThroughChanges(chs[0..chs_len], c.cur), .anc = mapThroughChanges(chs[0..chs_len], c.anc) }; } + n = got.n; pri = got.pri; } - setEditText(p, t, new); - const t2 = flatSurface(p, t) catch return; - for (cells[0..n], 0..) |s, i| rs[i] = Text.cellRange(t2, s.arow, s.acol, s.row, s.col); - t.setRanges(t2, rs[0..n], &.{}, pri, expl); + setEditCells(p, t, new, eb.row0, cells[0..n], pri, expl); } /// `Alt-)` / `Alt-(`: rotate the TEXT of the selections forward / back by @@ -1579,12 +1591,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) void { /// and the primary moves with the text it had. pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) void { const expl = (t.vsel.active and t.vsel.explicit) or t.msel.active; - var b = selRows(t); - for (t.sels[0..t.nsel]) |s| { - b.lo_row = @min(b.lo_row, @min(s.row, s.arow)); - b.hi_row = @max(b.hi_row, @max(s.row, s.arow)); - } - const eb = editTextEol(p, t, b) orelse return; + const eb = editTextEol(p, t, allSelRows(t)) orelse return; const text = eb.text; var rs: [Text.max_selections]modal.Selection = undefined; const got = t.ranges(text, eb.row0, &rs); @@ -1602,26 +1609,15 @@ pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) voi } pushUndo(p, t); const new = sam.apply(p.gpa, text, chs[0..n]) catch return; - var cells: [Text.max_selections]Text.SelRange = undefined; + var cells: [Text.max_selections]Text.Cells = undefined; var delta: i64 = 0; - for (chs[0..n], rs[0..n], 0..) |ch, r, i| { + for (chs[0..n], rs[0..n], cells[0..n]) |ch, r, *cell| { const from: usize = @intCast(@as(i64, @intCast(ch.q0)) + delta); delta += @as(i64, @intCast(ch.text.len)) - @as(i64, @intCast(ch.q1 - ch.q0)); const to = from + ch.text.len; - const c = Text.rangeCells(new, if (r.anchor > r.head) .{ .anchor = to, .head = from } else .{ .anchor = from, .head = to }); - const cc = modal.positionAt(new, c.cur); - const ac = modal.positionAt(new, c.anc); - cells[i] = .{ - .row = @as(i32, @intCast(cc.row)) + eb.row0, - .col = @intCast(cc.col), - .arow = @as(i32, @intCast(ac.row)) + eb.row0, - .acol = @intCast(ac.col), - }; + cell.* = Text.rangeCells(new, if (r.anchor > r.head) .{ .anchor = to, .head = from } else .{ .anchor = from, .head = to }); } - setEditText(p, t, new); - const t2 = flatSurface(p, t) catch return; - for (cells[0..n], 0..) |s, i| rs[i] = Text.cellRange(t2, s.arow, s.acol, s.row, s.col); - t.setRanges(t2, rs[0..n], &.{}, if (forward) (got.pri + by) % n else (got.pri + n - by) % n, expl); + setEditCells(p, t, new, eb.row0, cells[0..n], if (forward) (got.pri + by) % n else (got.pri + n - by) % n, expl); } /// `&`: pad the selections into columns, in one edit (helix @@ -1630,12 +1626,7 @@ pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) voi /// in display cells. A range over several lines aligns nothing. pub fn normalAlign(p: *Pardes, t: *Text) void { const expl = (t.vsel.active and t.vsel.explicit) or t.msel.active; - var b = selRows(t); - for (t.sels[0..t.nsel]) |s| { - b.lo_row = @min(b.lo_row, @min(s.row, s.arow)); - b.hi_row = @max(b.hi_row, @max(s.row, s.arow)); - } - const eb = editTextEol(p, t, b) orelse return; + const eb = editTextEol(p, t, allSelRows(t)) orelse return; const text = eb.text; var rs: [Text.max_selections]modal.Selection = undefined; const got = t.ranges(text, eb.row0, &rs); @@ -1686,22 +1677,12 @@ pub fn normalAlign(p: *Pardes, t: *Text) void { if (m == 0) return; pushUndo(p, t); const new = sam.apply(p.gpa, text, chs[0..m]) catch return; - var cells: [Text.max_selections]Text.SelRange = undefined; - for (rs[0..n], 0..) |r, i| { + var cells: [Text.max_selections]Text.Cells = undefined; + for (rs[0..n], cells[0..n]) |r, *cell| { const c = Text.rangeCells(text, r); - const cc = modal.positionAt(new, mapThroughChanges(chs[0..m], c.cur)); - const ac = modal.positionAt(new, mapThroughChanges(chs[0..m], c.anc)); - cells[i] = .{ - .row = @as(i32, @intCast(cc.row)) + eb.row0, - .col = @intCast(cc.col), - .arow = @as(i32, @intCast(ac.row)) + eb.row0, - .acol = @intCast(ac.col), - }; + cell.* = .{ .cur = mapThroughChanges(chs[0..m], c.cur), .anc = mapThroughChanges(chs[0..m], c.anc) }; } - setEditText(p, t, new); - const t2 = flatSurface(p, t) catch return; - for (cells[0..n], 0..) |s, i| rs[i] = Text.cellRange(t2, s.arow, s.acol, s.row, s.col); - t.setRanges(t2, rs[0..n], &.{}, got.pri, expl); + setEditCells(p, t, new, eb.row0, cells[0..n], got.pri, expl); } pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) void { @@ -1776,11 +1757,7 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) void { const expl = (t.vsel.active and t.vsel.explicit) or t.msel.active; t.select = false; // helix exit_select_mode // the buffer must cover every cursor's rows, not just the primary's - var b = selRows(t); - for (t.sels[0..t.nsel]) |s| { - b.lo_row = @min(b.lo_row, @min(s.row, s.arow)); - b.hi_row = @max(b.hi_row, @max(s.row, s.arow)); - } + const b = allSelRows(t); const surface = flatSurface(p, t) catch return; const last_row = @as(i32, @intCast(panes.File.textLineCount(t, surface))) - 1; const eb = editText(p, t, b.lo_row, @min(b.hi_row + 1, last_row), -1) orelse return; @@ -1847,26 +1824,13 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) void { pushUndo(p, t); // one edit, and the WHOLE selection rides through it (helix maps the // selection with the transaction) - var cells: [Text.max_selections]Text.SelRange = undefined; + var cells: [Text.max_selections]Text.Cells = undefined; const new = sam.apply(p.gpa, text, chs[0..chs_len]) catch return; - for (rs[0..got.n], 0..) |r, i| { - const c = Text.rangeCells(new, .{ - .anchor = mapThroughChanges(chs[0..chs_len], r.anchor), - .head = mapThroughChanges(chs[0..chs_len], r.head), - }); - const cc = modal.positionAt(new, c.cur); - const ac = modal.positionAt(new, c.anc); - cells[i] = .{ - .row = @as(i32, @intCast(cc.row)) + eb.row0, - .col = @intCast(cc.col), - .arow = @as(i32, @intCast(ac.row)) + eb.row0, - .acol = @intCast(ac.col), - }; - } - setEditText(p, t, new); - const t2 = flatSurface(p, t) catch return; - for (cells[0..got.n], 0..) |s, i| rs[i] = Text.cellRange(t2, s.arow, s.acol, s.row, s.col); - t.setRanges(t2, rs[0..got.n], &.{}, got.pri, expl); + for (rs[0..got.n], cells[0..got.n]) |r, *cell| cell.* = Text.rangeCells(new, .{ + .anchor = mapThroughChanges(chs[0..chs_len], r.anchor), + .head = mapThroughChanges(chs[0..chs_len], r.head), + }); + setEditCells(p, t, new, eb.row0, cells[0..got.n], got.pri, expl); } pub fn normalAdjustNumber(p: *Pardes, t: *Text, delta: i64) void { -- cgit v1.3