diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-05 00:49:57 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 01:17:28 -0300 |
| commit | f0515f28bd28703da85fbe6a95c8e819792a6002 (patch) | |
| tree | 2ac434d7cee58c6848c88a1815caed60673feb99 /src/edit.zig | |
| parent | d5158f6a69ce5a1af6372c140b07379ee0ba2446 (diff) | |
| download | pardes-f0515f28bd28703da85fbe6a95c8e819792a6002.tar.gz pardes-f0515f28bd28703da85fbe6a95c8e819792a6002.zip | |
modal, edit, normal: the selection ops splice their primary range's offsets (Text.primaryRange): d, c, the selection's text, r, R and the chords' replace, ~ ` Alt-`, C-a and C-x, ms, md and insert-mode Delete; selRange and the cursor-pair half of modal's row/col edit layer go (rangeBytes, rangeText, deleteRange, replaceRange, replaceChars, deleteChar, clearLine, lineFrom), with their unit tests, and deleteLines no longer builds the removed text no caller read; a key-level test pins r and Delete on whole graphemes; the insert-mode and paste ops keep insertAt, deleteSpan, pasteLineBelow and advanceBy, whose callers hold cells; no key does anything new
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/edit.zig')
| -rw-r--r-- | src/edit.zig | 164 |
1 files changed, 57 insertions, 107 deletions
diff --git a/src/edit.zig b/src/edit.zig index 964cda79..c20eac04 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -985,7 +985,8 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) void { // a terminal means the buffer swallowing the row below const line = modal.lineSlice(text, c.row); if (c.col < line.len) { - const new = modal.deleteChar(p.gpa, text, c) catch return; + const at = modal.offsetAt(text, c); + const new = modal.spliceAlloc(p.gpa, text, at, modal.nextGrapheme(text, at), "") catch return; setEditTextEnd(p, t, new, c); } else { const e2 = editText(p, t, t.cur_row, t.cur_row + 1, t.cur_col) orelse return; @@ -1052,10 +1053,9 @@ fn vselBounds(t: *Text) Bounds { } fn vselText(p: *Pardes, t: *Text) []const u8 { - const arena = p.scratch.allocator(); - const b = vselBounds(t); const text = flatSurface(p, t) catch return ""; - return modal.rangeText(arena, text, .{ .row = @intCast(@max(0, b.lo_row)), .col = @intCast(@max(0, b.lo_col)) }, .{ .row = @intCast(@max(0, b.hi_row)), .col = @intCast(@max(0, b.hi_col)) }) catch ""; + const r = t.primaryRange(text, 0); + return p.scratch.allocator().dupe(u8, text[@min(r.anchor, r.head)..@max(r.anchor, r.head)]) catch ""; } pub fn normalDelete(p: *Pardes, t: *Text, yank: bool) void { @@ -1071,15 +1071,11 @@ pub fn normalDelete(p: *Pardes, t: *Text, yank: bool) void { t.vsel.active = false; return; }; - const d = modal.deleteRange(p.gpa, eb.text, .{ .row = @intCast(@max(0, b.lo_row - eb.row0)), .col = @intCast(@max(0, b.lo_col)) }, .{ .row = @intCast(@max(0, b.hi_row - eb.row0)), .col = @intCast(@max(0, b.hi_col)) }) catch { - t.vsel.active = false; - return; - }; - setEditText(p, t, d.content); - p.gpa.free(d.deleted); + const new = deleteSelection(p, t, eb) orelse return; + setEditText(p, t, new); t.cur_row = b.lo_row; t.cur_col = b.lo_col; - t.clampCursor(d.content, eb.row0); + t.clampCursor(new, eb.row0); } pub fn normalYank(p: *Pardes, t: *Text) void { @@ -1167,14 +1163,13 @@ pub fn normalChange(p: *Pardes, t: *Text) void { setYank(p, vselText(p, t)); pushUndo(p, t); const d = modal.deleteLines(p.gpa, eb.text, @intCast(@max(0, b.lo_row - eb.row0)), @intCast(@max(0, b.hi_row - eb.row0))) catch return; - setEditText(p, t, d.content); - p.gpa.free(d.deleted); - const n = modal.lineCount(d.content); + setEditText(p, t, d); + const n = modal.lineCount(d); const row: usize = @min(@as(usize, @intCast(@max(0, b.lo_row - eb.row0))), if (n == 0) 0 else n - 1); - const ind = modal.indentText(modal.lineSlice(d.content, row)); + const ind = modal.indentText(modal.lineSlice(d, row)); const arena = p.scratch.allocator(); const block = std.fmt.allocPrint(arena, "{s}\n", .{ind}) catch return; - const new = modal.insertAt(p.gpa, d.content, .{ .row = row, .col = 0 }, block) catch return; + const new = modal.insertAt(p.gpa, d, .{ .row = row, .col = 0 }, block) catch return; setEditText(p, t, new); t.cur_row = @as(i32, @intCast(row)) + eb.row0; t.cur_col = @intCast(ind.len); @@ -1187,43 +1182,35 @@ pub fn normalChange(p: *Pardes, t: *Text) void { } setYank(p, vselText(p, t)); pushUndo(p, t); - const d = modal.deleteRange(p.gpa, eb.text, .{ .row = @intCast(@max(0, b.lo_row - eb.row0)), .col = @intCast(@max(0, b.lo_col)) }, .{ .row = @intCast(@max(0, b.hi_row - eb.row0)), .col = @intCast(@max(0, b.hi_col)) }) catch { - t.vsel.active = false; - return; - }; - setEditText(p, t, d.content); - p.gpa.free(d.deleted); + const new = deleteSelection(p, t, eb) orelse return; + setEditText(p, t, new); t.cur_row = b.lo_row; t.cur_col = b.lo_col; - t.clampCursor(d.content, eb.row0); + t.clampCursor(new, eb.row0); t.mode = .insert; t.normal.clear(); return; } pushUndo(p, t); - const eb = editTextEol(p, t, .{ .lo_row = t.cur_row, .lo_col = t.cur_col, .hi_row = t.cur_row, .hi_col = t.cur_col }) orelse return; - const c = modal.Cursor{ .row = @intCast(@max(0, t.cur_row - eb.row0)), .col = @intCast(@max(0, t.cur_col)) }; - const arena = p.scratch.allocator(); - setYank(p, modal.rangeText(arena, eb.text, c, c) catch return); - const d = modal.deleteRange(p.gpa, eb.text, c, c) catch return; - setEditText(p, t, d.content); - p.gpa.free(d.deleted); - const llen = modal.lineSlice(d.content, c.row).len; + const eb = editTextEol(p, t, selRows(t)) orelse return; + const r = t.primaryRange(eb.text, eb.row0); + setYank(p, eb.text[r.anchor..r.head]); + const new = modal.spliceAlloc(p.gpa, eb.text, r.anchor, r.head, "") catch return; + setEditText(p, t, new); + const llen = modal.lineSlice(new, @intCast(@max(0, t.cur_row - eb.row0))).len; t.cur_col = @min(t.cur_col, @as(i32, @intCast(llen))); t.mode = .insert; t.normal.clear(); } -fn selRange(t: *Text, row0: i32) modal.Range { - if (t.vsel.active) { - const b = vselBounds(t); - return .{ - .a = .{ .row = @intCast(@max(0, b.lo_row - row0)), .col = @intCast(@max(0, b.lo_col)) }, - .b = .{ .row = @intCast(@max(0, b.hi_row - row0)), .col = @intCast(@max(0, b.hi_col)) }, - }; - } - const c = modal.Cursor{ .row = @intCast(@max(0, t.cur_row - row0)), .col = @intCast(@max(0, t.cur_col)) }; - return .{ .a = c, .b = c }; +/// `eb` without the primary range, or null (the selection dropped) when +/// there is no memory for it. +fn deleteSelection(p: *Pardes, t: *Text, eb: panes.EditText) ?[]u8 { + const r = t.primaryRange(eb.text, eb.row0); + return modal.spliceAlloc(p.gpa, eb.text, @min(r.anchor, r.head), @max(r.anchor, r.head), "") catch { + t.vsel.active = false; + return null; + }; } /// the rows an op's selection spans, absolute (what editText must cover) @@ -1272,11 +1259,12 @@ pub fn normalReplaceChar(p: *Pardes, t: *Text, ch: u21) void { while (at < hi) : (graphemes += 1) at = modal.nextGrapheme(eb.text, at); var encoded: [4]u8 = undefined; const encoded_len = std.unicode.utf8Encode(ch, &encoded) catch return; - const r = selRange(t, eb.row0); + const rep = p.scratch.allocator().alloc(u8, graphemes * encoded_len) catch return; + for (0..graphemes) |i| @memcpy(rep[i * encoded_len ..][0..encoded_len], encoded[0..encoded_len]); pushUndo(p, t); - const new = modal.replaceChars(p.gpa, eb.text, r.a, r.b, ch) catch return; + const new = modal.spliceAlloc(p.gpa, eb.text, lo, hi, rep) catch return; setEditText(p, t, new); - const end = lo + graphemes * encoded_len; + const end = lo + rep.len; const mapped: modal.Selection = if (before.head < before.anchor) .{ .anchor = end, .head = lo } else @@ -1298,35 +1286,13 @@ fn replaceWithText(p: *Pardes, t: *Text, y: []const u8) void { if (y.len == 0) return; t.select = false; const eb = editTextEol(p, t, selRows(t)) orelse return; - const r = selRange(t, eb.row0); - const back = t.vsel.active and (t.cur_row < t.vsel.row or (t.cur_row == t.vsel.row and t.cur_col < t.vsel.col)); + const r = t.primaryRange(eb.text, eb.row0); + const lo = @min(r.anchor, r.head); pushUndo(p, t); - const new = modal.replaceRange(p.gpa, eb.text, r.a, r.b, y) catch return; + const new = modal.spliceAlloc(p.gpa, eb.text, lo, @max(r.anchor, r.head), y) catch return; setEditText(p, t, new); - const end = modal.advanceBy(r.a, y); - var last_row: i32 = undefined; - var last_col: i32 = undefined; - if (end.col > 0) { - last_row = @as(i32, @intCast(end.row)) + eb.row0; - last_col = @intCast(modal.prevGrapheme(modal.lineSlice(new, end.row), end.col)); - } else { - // the yank ended in '\n': its last cell is that newline - last_row = @as(i32, @intCast(end.row -| 1)) + eb.row0; - last_col = @intCast(modal.lineSlice(new, end.row -| 1).len); - } - const first_row = @as(i32, @intCast(r.a.row)) + eb.row0; - const first_col: i32 = @intCast(r.a.col); - t.vsel = .{ - .active = modal.nextGrapheme(y, 0) < y.len, - .row = if (back) last_row else first_row, - .col = if (back) last_col else first_col, - .explicit = false, - }; - t.cur_row = if (back) first_row else last_row; - t.cur_col = if (back) first_col else last_col; - t.cur_pinned = true; - t.sticky_col = -1; - t.show(); + const end = lo + y.len; + t.setRange(new, eb.row0, if (r.head < r.anchor) .{ .anchor = end, .head = lo } else .{ .anchor = lo, .head = end }, false); } /// `~` / `` ` `` / ``Alt-` ``: case-map the selection (or the cursor char), @@ -1334,9 +1300,9 @@ fn replaceWithText(p: *Pardes, t: *Text, y: []const u8) void { pub fn normalCase(p: *Pardes, t: *Text, op: modal.CaseOp) void { t.select = false; const eb = editTextEol(p, t, selRows(t)) orelse return; - const r = selRange(t, eb.row0); + const r = t.primaryRange(eb.text, eb.row0); pushUndo(p, t); - const new = modal.changeCase(p.gpa, eb.text, r.a, r.b, op) catch return; + const new = modal.changeCase(p.gpa, eb.text, @min(r.anchor, r.head), @max(r.anchor, r.head), op) catch return; setEditText(p, t, new); } @@ -1674,22 +1640,15 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) void { pub fn normalAdjustNumber(p: *Pardes, t: *Text, delta: i64) void { const eb = editTextEol(p, t, selRows(t)) orelse return; - const r = selRange(t, eb.row0); - const arena = p.scratch.allocator(); - const frag = modal.rangeText(arena, eb.text, r.a, r.b) catch return; - const rep = (modal.incrementDecimal(arena, frag, delta) catch null) orelse return; + const r = t.primaryRange(eb.text, eb.row0); + const lo = @min(r.anchor, r.head); + const hi = @max(r.anchor, r.head); + const rep = (modal.incrementDecimal(p.scratch.allocator(), eb.text[lo..hi], delta) catch null) orelse return; pushUndo(p, t); t.select = false; - const new = modal.replaceRange(p.gpa, eb.text, r.a, r.b, rep) catch return; + const new = modal.spliceAlloc(p.gpa, eb.text, lo, hi, rep) catch return; setEditText(p, t, new); - const start = modal.lineStartOffset(new, r.a.row) + r.a.col; - const cc = modal.positionAt(new, modal.prevGrapheme(new, start + rep.len)); - t.vsel = .{ .active = modal.nextGrapheme(rep, 0) < rep.len, .row = @as(i32, @intCast(r.a.row)) + eb.row0, .col = @intCast(r.a.col), .explicit = false }; - t.cur_row = @as(i32, @intCast(cc.row)) + eb.row0; - t.cur_col = @intCast(cc.col); - t.cur_pinned = true; - t.sticky_col = -1; - t.show(); + t.setRange(new, eb.row0, .{ .anchor = lo, .head = lo + rep.len }, false); } /// `]Space` / `[Space`: add blank lines below/above, the cursor staying @@ -1789,25 +1748,16 @@ pub fn surroundAdd(p: *Pardes, t: *Text, ch: u21) void { const pr = pairFor(ch) orelse return; t.select = false; const eb = editTextEol(p, t, selRows(t)) orelse return; - const r = selRange(t, eb.row0); - // the wrapped range keeps the direction it had (helix with_direction) - const back = t.vsel.active and (t.cur_row < t.vsel.row or (t.cur_row == t.vsel.row and t.cur_col < t.vsel.col)); + const r = t.primaryRange(eb.text, eb.row0); + const lo = @min(r.anchor, r.head); + const hi = @max(r.anchor, r.head); pushUndo(p, t); - const close_col = modal.nextGrapheme(modal.lineSlice(eb.text, r.b.row), r.b.col); - var new = modal.insertAt(p.gpa, eb.text, .{ .row = r.b.row, .col = close_col }, &[1]u8{pr.c}) catch return; + const open = [1]u8{pr.o}; + const close = [1]u8{pr.c}; + const new = sam.apply(p.gpa, eb.text, &.{ .{ .q0 = @intCast(lo), .q1 = @intCast(lo), .text = &open }, .{ .q0 = @intCast(hi), .q1 = @intCast(hi), .text = &close } }) catch return; setEditText(p, t, new); - new = modal.insertAt(p.gpa, new, .{ .row = r.a.row, .col = r.a.col }, &[1]u8{pr.o}) catch return; - setEditText(p, t, new); - const open_row = @as(i32, @intCast(r.a.row)) + eb.row0; - const open_col: i32 = @intCast(r.a.col); - const close_row = @as(i32, @intCast(r.b.row)) + eb.row0; - const close_at: i32 = @intCast(close_col + @as(usize, if (r.a.row == r.b.row) 1 else 0)); - t.vsel = .{ .active = true, .row = if (back) close_row else open_row, .col = if (back) close_at else open_col, .explicit = false }; - t.cur_row = if (back) open_row else close_row; - t.cur_col = if (back) open_col else close_at; - t.cur_pinned = true; - t.sticky_col = -1; - t.show(); + // the wrapped range keeps the direction it had (helix with_direction) + t.setRange(new, eb.row0, if (r.head < r.anchor) .{ .anchor = hi + 2, .head = lo } else .{ .anchor = lo, .head = hi + 2 }, false); } /// `md<ch>`: delete the enclosing pair's chars; the cursor maps through @@ -1830,10 +1780,10 @@ pub fn surroundDelete(p: *Pardes, t: *Text, ch: u21) void { const a_off = modal.offsetAt(text, ra); const b_off = modal.offsetAt(text, rb); const cur_off = modal.offsetAt(text, .{ .row = @intCast(@as(i32, @intCast(cur.row)) + drow), .col = cur.col }); - // the close first, so the open's position stays valid - var new = modal.deleteChar(p.gpa, text, rb) catch return; - setEditText(p, t, new); - new = modal.deleteChar(p.gpa, new, ra) catch return; + const new = sam.apply(p.gpa, text, &.{ + .{ .q0 = @intCast(a_off), .q1 = @intCast(modal.nextGrapheme(text, a_off)), .text = "" }, + .{ .q0 = @intCast(b_off), .q1 = @intCast(modal.nextGrapheme(text, b_off)), .text = "" }, + }) catch return; setEditText(p, t, new); var nc = cur_off; if (nc > b_off) nc -= 1; |
