summaryrefslogtreecommitdiff
path: root/src/modal.zig
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 /src/modal.zig
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
Diffstat (limited to 'src/modal.zig')
-rw-r--r--src/modal.zig236
1 files changed, 16 insertions, 220 deletions
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);
}