summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-05 00:49:57 -0300
committerGabriel Schneider <[email protected]>2026-11-05 01:17:28 -0300
commitf0515f28bd28703da85fbe6a95c8e819792a6002 (patch)
tree2ac434d7cee58c6848c88a1815caed60673feb99
parentd5158f6a69ce5a1af6372c140b07379ee0ba2446 (diff)
downloadpardes-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
-rw-r--r--docs/helix-keys.md8
-rw-r--r--src/edit.zig164
-rw-r--r--src/modal.zig236
-rw-r--r--src/normal.zig14
4 files changed, 91 insertions, 331 deletions
diff --git a/docs/helix-keys.md b/docs/helix-keys.md
index 1d1058ef..135e1610 100644
--- a/docs/helix-keys.md
+++ b/docs/helix-keys.md
@@ -169,10 +169,10 @@ capped 0xffff), the prefix key's role (`g` `z` `m` `f` `F` `t` `T` `r` `]` `[`
`<from>`. `find_op`/`find_ch` (the `Alt-.` repeat target) stay
on the body's `Text`, because the parser emits `repeat_find` without remembering what was
found. Pure text math in `modal.zig`: `findTarget`, `matchBracket`,
-`paragraphFwd`/`paragraphBwd`/`paragraphRange`, textobject/surround ranges,
-`replaceRange`/`replaceChars`, `changeCase`, `joinLine`, `indentLines`,
-`adjustNumber`, `deleteSpan`, `advanceBy` — each with inline tests that
-`zig build unit-test` runs.
+`moveParagraph`/`selectParagraph`, textobject/surround ranges,
+`changeCase`, `incrementDecimal`, `deleteSpan`, `advanceBy`, most with
+inline tests that `zig build unit-test` runs. An op on a selection splices
+the offsets of its range (`Text.primaryRange`).
### Counts
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;
diff --git a/src/modal.zig b/src/modal.zig
index 16a7ee8b..894fbaf8 100644
--- a/src/modal.zig
+++ b/src/modal.zig
@@ -757,29 +757,10 @@ test "lineStartOffset by blocks and offsetAt agree with a line at a time" {
break :span .{ .start = start, .end = std.mem.indexOfScalarPos(u8, text, start, '\n') orelse text.len };
};
try std.testing.expectEqual(span, lineSpan(text, row));
- // rangeBytes, as it was: each line looked for from the top, twice.
- const other: Cursor = .{ .row = random.uintLessThan(usize, len + 3), .col = random.uintLessThan(usize, 6) };
- var lo = cursor;
- var hi = other;
- if (hi.row < lo.row or (hi.row == lo.row and hi.col < lo.col)) std.mem.swap(Cursor, &lo, &hi);
- const lo_line = lineSlice(text, lo.row);
- const hi_line = lineSlice(text, hi.row);
- const s0 = lineStartOffset(text, lo.row) + graphemeStart(lo_line, @min(lo.col, lo_line.len));
- var e0 = lineStartOffset(text, hi.row) + graphemeStart(hi_line, @min(hi.col, hi_line.len));
- if (e0 < text.len) e0 = nextGrapheme(text, e0);
- const got = rangeBytes(text, cursor, other);
- try std.testing.expectEqual(s0, got.s);
- try std.testing.expectEqual(@max(s0, e0), got.e);
}
}
}
-/// The line that starts at `start` (lineSlice's, once its start is known).
-fn lineFrom(content: []const u8, start: usize) []const u8 {
- if (start >= content.len) return "";
- return content[start .. std.mem.indexOfScalarPos(u8, content, start, '\n') orelse content.len];
-}
-
// the text of line `row` (no terminator), a slice into `content`.
pub fn lineSlice(content: []const u8, row: usize) []const u8 {
const start = lineStartOffset(content, row);
@@ -838,98 +819,17 @@ pub fn insertOffset(content: []const u8, c: Cursor) usize {
return span.start + @min(c.col, span.end - span.start);
}
-// delete the character at (row, col). no-op if col is past the line end.
-pub fn deleteChar(alloc: std.mem.Allocator, content: []const u8, c: Cursor) ![]u8 {
- const line = lineSlice(content, c.row);
- if (c.col >= line.len) return alloc.dupe(u8, content);
- const col = graphemeStart(line, c.col);
- const line_start = lineStartOffset(content, c.row);
- return spliceAlloc(alloc, content, line_start + col, line_start + nextGrapheme(line, col), "");
-}
-
// delete whole lines [r0, r1] inclusive (the line content + their terminators).
-// returns the new content; `deleted` is the joined removed text (no terminators).
-pub const Deleted = struct { content: []u8, deleted: []u8 };
-
-pub fn deleteLines(alloc: std.mem.Allocator, content: []const u8, r0: usize, r1: usize) !Deleted {
+pub fn deleteLines(alloc: std.mem.Allocator, content: []const u8, r0: usize, r1: usize) ![]u8 {
const n = lineCount(content);
const lo = @min(r0, r1);
const hi = @min(@max(r0, r1), if (n == 0) 0 else n - 1);
- if (n == 0 or hi < lo) {
- const unchanged = try alloc.dupe(u8, content);
- errdefer alloc.free(unchanged);
- return .{ .content = unchanged, .deleted = try alloc.dupe(u8, "") };
- }
-
+ if (n == 0 or hi < lo) return alloc.dupe(u8, content);
const start = lineStartOffset(content, lo);
// end = start of line (hi+1), or content.len if hi is the last line.
const end = if (hi + 1 < n) lineStartOffset(content, hi + 1) else content.len;
- // if we're removing the last line and there's a preceding newline, also
- // drop that newline so we don't leave a trailing blank line.
- var cut_lo = start;
- const cut_hi = end;
- if (hi + 1 == n and start > 0) cut_lo -= 1; // remove the '\n' before the last line
-
- var deleted_len: usize = hi - lo;
- var r = lo;
- while (r <= hi) : (r += 1) deleted_len += lineSlice(content, r).len;
- const deleted = try alloc.alloc(u8, deleted_len);
- errdefer alloc.free(deleted);
- var write: usize = 0;
- r = lo;
- while (r <= hi) : (r += 1) {
- if (r > lo) {
- deleted[write] = '\n';
- write += 1;
- }
- const line = lineSlice(content, r);
- @memcpy(deleted[write..][0..line.len], line);
- write += line.len;
- }
- return .{ .content = try spliceAlloc(alloc, content, cut_lo, cut_hi, ""), .deleted = deleted };
-}
-
-// normalize two cursors into document order (lo <= hi), then byte offsets of the
-// INCLUSIVE range [lo .. hi] (the char under hi is included). e == s means empty.
-fn rangeBytes(content: []const u8, a: Cursor, b: Cursor) struct { s: usize, e: usize } {
- var lo = a;
- var hi = b;
- if (hi.row < lo.row or (hi.row == lo.row and hi.col < lo.col)) {
- lo = b;
- hi = a;
- }
- // Each line found once, the second from the first.
- const lo_start = lineStartOffset(content, lo.row);
- const hi_start = if (hi.row == lo.row) lo_start else lo_start + lineStartOffset(content[lo_start..], hi.row - lo.row);
- const lo_line = lineFrom(content, lo_start);
- const hi_line = lineFrom(content, hi_start);
- const lo_col = graphemeStart(lo_line, @min(lo.col, lo_line.len));
- const hi_col = graphemeStart(hi_line, @min(hi.col, hi_line.len));
- const s = lo_start + lo_col;
- var e = hi_start + hi_col;
- if (e < content.len) e = nextGrapheme(content, e); // include the grapheme under the head
- return .{ .s = s, .e = @max(s, e) };
-}
-
-// the text of the inclusive char range [a, b] (cursors in either order). Caller frees.
-pub fn rangeText(alloc: std.mem.Allocator, content: []const u8, a: Cursor, b: Cursor) ![]u8 {
- const r = rangeBytes(content, a, b);
- return alloc.dupe(u8, content[r.s..r.e]);
-}
-
-// delete the inclusive char range [a, b]. returns new content + the removed text.
-pub fn deleteRange(alloc: std.mem.Allocator, content: []const u8, a: Cursor, b: Cursor) !Deleted {
- const r = rangeBytes(content, a, b);
- const deleted = try alloc.dupe(u8, content[r.s..r.e]);
- errdefer alloc.free(deleted);
- return .{ .content = try spliceAlloc(alloc, content, r.s, r.e, ""), .deleted = deleted };
-}
-
-// replace line `row`'s text with "" (keep the line, empty it). For `c`hange line.
-pub fn clearLine(alloc: std.mem.Allocator, content: []const u8, row: usize) ![]u8 {
- const line = lineSlice(content, row);
- const start = lineStartOffset(content, row);
- return spliceAlloc(alloc, content, start, start + line.len, "");
+ // the last line takes the '\n' before it too: no trailing blank line is left
+ return spliceAlloc(alloc, content, if (hi + 1 == n and start > 0) start - 1 else start, end, "");
}
// paste `text` as a new line BELOW `row`. Multiline `text` becomes several lines.
@@ -963,40 +863,12 @@ pub fn advanceBy(c: Cursor, text: []const u8) Cursor {
return .{ .row = c.row + std.mem.count(u8, text, "\n"), .col = text.len - nl - 1 };
}
-// replace the inclusive char range [a, b] with `text` (R replace-with-yank).
-pub fn replaceRange(alloc: std.mem.Allocator, content: []const u8, a: Cursor, b: Cursor, text: []const u8) ![]u8 {
- const r = rangeBytes(content, a, b);
- return spliceAlloc(alloc, content, r.s, r.e, text);
-}
-
-// `r<ch>`: overwrite every char in the inclusive range [a, b] with `ch` —
-// NEWLINES TOO (helix replace maps every grapheme, so `xrz` joins lines).
-pub fn replaceChars(alloc: std.mem.Allocator, content: []const u8, a: Cursor, b: Cursor, ch: u21) ![]u8 {
- const r = rangeBytes(content, a, b);
- var encoded: [4]u8 = undefined;
- const encoded_len = try std.unicode.utf8Encode(ch, &encoded);
- var count: usize = 0;
- var at = r.s;
- while (at < r.e) : (count += 1) at = nextGrapheme(content, at);
- const replacement_len = count * encoded_len;
- const out = try alloc.alloc(u8, content.len - (r.e - r.s) + replacement_len);
- @memcpy(out[0..r.s], content[0..r.s]);
- var write = r.s;
- for (0..count) |_| {
- @memcpy(out[write..][0..encoded_len], encoded[0..encoded_len]);
- write += encoded_len;
- }
- @memcpy(out[write..], content[r.e..]);
- return out;
-}
-
-// `~` / `` ` `` / ``Alt-` ``: case-map the inclusive range [a, b].
+// `~` / `` ` `` / ``Alt-` ``: case-map the bytes [s, e).
pub const CaseOp = enum { toggle, lower, upper };
-pub fn changeCase(alloc: std.mem.Allocator, content: []const u8, a: Cursor, b: Cursor, op: CaseOp) ![]u8 {
- const r = rangeBytes(content, a, b);
+pub fn changeCase(alloc: std.mem.Allocator, content: []const u8, s: usize, e: usize, op: CaseOp) ![]u8 {
const out = try alloc.dupe(u8, content);
- for (out[r.s..r.e]) |*p| {
+ for (out[s..e]) |*p| {
p.* = switch (op) {
.toggle => if (std.ascii.isUpper(p.*)) std.ascii.toLower(p.*) else std.ascii.toUpper(p.*),
.lower => std.ascii.toLower(p.*),
@@ -1893,90 +1765,28 @@ test "insertAt multiline creates lines" {
try std.testing.expectEqual(@as(usize, 3), lineCount(r));
}
-test "deleteChar" {
- const content = "abc";
- const a = std.testing.allocator;
- const r = try deleteChar(a, content, .{ .row = 0, .col = 1 });
- defer a.free(r);
- try std.testing.expectEqualStrings("ac", r);
- // past end: no-op
- const r2 = try deleteChar(a, content, .{ .row = 0, .col = 5 });
- defer a.free(r2);
- try std.testing.expectEqualStrings("abc", r2);
-}
-
-test "Unicode edits replace and delete whole graphemes" {
- const a = std.testing.allocator;
- const content = "A" ++ "e\u{301}" ++ "👩🏽\u{200d}🚀" ++ "🇧🇷" ++ "界" ++ "Z";
-
- const deleted = try deleteChar(a, content, .{ .row = 0, .col = 2 });
- defer a.free(deleted);
- try std.testing.expectEqualStrings("A👩🏽\u{200d}🚀🇧🇷界Z", deleted);
-
- const replaced = try replaceChars(a, content, .{ .row = 0, .col = 1 }, .{ .row = 0, .col = 19 }, '界');
- defer a.free(replaced);
- try std.testing.expectEqualStrings("A界界界界Z", replaced);
-}
-
test "deleteLines middle" {
const content = "one\ntwo\nthree\nfour";
const a = std.testing.allocator;
const d = try deleteLines(a, content, 1, 2);
- defer a.free(d.content);
- defer a.free(d.deleted);
- try std.testing.expectEqualStrings("one\nfour", d.content);
- try std.testing.expectEqualStrings("two\nthree", d.deleted);
+ defer a.free(d);
+ try std.testing.expectEqualStrings("one\nfour", d);
}
test "deleteLines last line drops preceding newline" {
const content = "one\ntwo\nthree";
const a = std.testing.allocator;
const d = try deleteLines(a, content, 2, 2);
- defer a.free(d.content);
- defer a.free(d.deleted);
- try std.testing.expectEqualStrings("one\ntwo", d.content);
- try std.testing.expectEqualStrings("three", d.deleted);
+ defer a.free(d);
+ try std.testing.expectEqualStrings("one\ntwo", d);
}
test "deleteLines only line" {
const content = "only";
const a = std.testing.allocator;
const d = try deleteLines(a, content, 0, 0);
- defer a.free(d.content);
- defer a.free(d.deleted);
- try std.testing.expectEqualStrings("", d.content);
- try std.testing.expectEqualStrings("only", d.deleted);
-}
-
-test "rangeText + deleteRange (char-wise select)" {
- const a = std.testing.allocator;
- const content = "hello\nworld\nfoo";
- // same-line inclusive range: "hello"[1..3] -> "ell"
- const t1 = try rangeText(a, content, .{ .row = 0, .col = 1 }, .{ .row = 0, .col = 3 });
- defer a.free(t1);
- try std.testing.expectEqualStrings("ell", t1);
- // reversed cursors give the same range
- const t2 = try rangeText(a, content, .{ .row = 0, .col = 3 }, .{ .row = 0, .col = 1 });
- defer a.free(t2);
- try std.testing.expectEqualStrings("ell", t2);
- // cross-line range includes the newline: row0 col3 .. row1 col1 -> "lo\nwo"
- const t3 = try rangeText(a, content, .{ .row = 0, .col = 3 }, .{ .row = 1, .col = 1 });
- defer a.free(t3);
- try std.testing.expectEqualStrings("lo\nwo", t3);
- // delete the same cross-line range
- const d = try deleteRange(a, content, .{ .row = 0, .col = 3 }, .{ .row = 1, .col = 1 });
- defer a.free(d.content);
- defer a.free(d.deleted);
- try std.testing.expectEqualStrings("helrld\nfoo", d.content);
- try std.testing.expectEqualStrings("lo\nwo", d.deleted);
-}
-
-test "clearLine" {
- const content = "keep\nzap me\nkeep2";
- const a = std.testing.allocator;
- const r = try clearLine(a, content, 1);
- defer a.free(r);
- try std.testing.expectEqualStrings("keep\n\nkeep2", r);
+ defer a.free(d);
+ try std.testing.expectEqualStrings("", d);
}
test "matchBracket nesting both directions" {
@@ -2026,29 +1836,15 @@ test "advanceBy" {
try std.testing.expectEqual(Cursor{ .row = 2, .col = 1 }, advanceBy(.{ .row = 0, .col = 2 }, "a\nbc\nd"));
}
-test "replaceRange" {
- const a = std.testing.allocator;
- const r = try replaceRange(a, "hello world", .{ .row = 0, .col = 0 }, .{ .row = 0, .col = 4 }, "bye");
- defer a.free(r);
- try std.testing.expectEqualStrings("bye world", r);
-}
-
-test "replaceChars overwrites newlines too" {
- const a = std.testing.allocator;
- const r = try replaceChars(a, "ab\ncd", .{ .row = 0, .col = 1 }, .{ .row = 1, .col = 0 }, 'x');
- defer a.free(r);
- try std.testing.expectEqualStrings("axxxd", r);
-}
-
test "changeCase" {
const a = std.testing.allocator;
- const t = try changeCase(a, "aB cD", .{ .row = 0, .col = 0 }, .{ .row = 0, .col = 4 }, .toggle);
+ const t = try changeCase(a, "aB cD", 0, 5, .toggle);
defer a.free(t);
try std.testing.expectEqualStrings("Ab Cd", t);
- const lo = try changeCase(a, "AB CD", .{ .row = 0, .col = 0 }, .{ .row = 0, .col = 1 }, .lower);
+ const lo = try changeCase(a, "AB CD", 0, 2, .lower);
defer a.free(lo);
try std.testing.expectEqualStrings("ab CD", lo);
- const up = try changeCase(a, "ab cd", .{ .row = 0, .col = 3 }, .{ .row = 0, .col = 4 }, .upper);
+ const up = try changeCase(a, "ab cd", 3, 5, .upper);
defer a.free(up);
try std.testing.expectEqualStrings("ab CD", up);
}
diff --git a/src/normal.zig b/src/normal.zig
index 6b9508f8..fefdd88c 100644
--- a/src/normal.zig
+++ b/src/normal.zig
@@ -1154,6 +1154,20 @@ test "cursors that land on one place are one: a key typed there is typed once, a
try std.testing.expectEqualStrings("xab\ncd\n", pane.file.?.content);
}
+test "r and Delete take whole graphemes" {
+ const gpa = std.testing.allocator;
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 12 });
+ defer p.deinit();
+ const pane = try p.setTestFile("A" ++ "e\u{301}" ++ "👩🏽\u{200d}🚀" ++ "🇧🇷" ++ "界" ++ "Z\n");
+ // é, the astronaut and the flag: three graphemes, three 界
+ for ("lv2lr") |c| handleNormal(p, &pane.body, .{ .cp = c });
+ handleNormal(p, &pane.body, .{ .cp = '界' });
+ try std.testing.expectEqualStrings("A界界界界Z\n", pane.file.?.content);
+ for ("u0li") |c| handleNormal(p, &pane.body, .{ .cp = c });
+ p.update(.{ .key = .{ .cp = Key.delete } });
+ try std.testing.expectEqualStrings("A" ++ "👩🏽\u{200d}🚀" ++ "🇧🇷" ++ "界" ++ "Z\n", pane.file.?.content);
+}
+
test "tree-sitter textobject keys do in Python, JSON and Zig what hx 25.07.1 does" {
if (!syntax.supportsPath("a.py") or !syntax.supportsPath("a.json") or !syntax.supportsPath("a.zig")) return;
const gpa = std.testing.allocator;