summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-04 23:13:36 -0300
committerGabriel Schneider <[email protected]>2026-11-05 01:16:28 -0300
commit3671eb9eafc19549bb94fe839e944e18707316e1 (patch)
treeb7220666204dc3df37d963a2b9884c84b9f714af /src
parent71df23d63da76c6aedf91a2458d3057ed6da9a35 (diff)
downloadpardes-3671eb9eafc19549bb94fe839e944e18707316e1.tar.gz
pardes-3671eb9eafc19549bb94fe839e944e18707316e1.zip
modal, edit: advanceBy takes insertedEnd's body (the last newline and a count, not a byte loop) and insertedEnd goes, and a typed run's end is computed once, for the splice and the cursor both
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src')
-rw-r--r--src/edit.zig19
-rw-r--r--src/modal.zig11
2 files changed, 7 insertions, 23 deletions
diff --git a/src/edit.zig b/src/edit.zig
index 2ba6d57a..707b576f 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -535,12 +535,6 @@ fn indexedLine(p: *Pardes, t: *Text, text: []const u8, row: usize) ?LineAt {
return .{ .start = start, .line = text[start..end] };
}
-/// Where text `s` put in at `c` ends: `g.`'s place after typing it.
-fn insertedEnd(c: modal.Cursor, s: []const u8) modal.Cursor {
- const nl = std.mem.lastIndexOfScalar(u8, s, '\n') orelse return .{ .row = c.row, .col = c.col + s.len };
- return .{ .row = c.row + std.mem.count(u8, s, "\n"), .col = s.len - nl - 1 };
-}
-
/// `setEditText` with where the edit ended in `new` (in the edit text's
/// rows) when the caller knows it, as typing does: then nothing scans the
/// whole text to find it. Null, it is found by comparing the texts.
@@ -901,13 +895,10 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) void {
// insertAt's offset (col clamped to the line), the line found once.
const at = if (indexedLine(p, t, text, c.row)) |l| l.start + @min(c.col, l.line.len) else modal.insertOffset(text, c);
const new = modal.spliceAlloc(p.gpa, text, at, at, key.text) catch return;
- setEditTextSpan(p, t, new, insertedEnd(c, key.text), .{ .head = at, .tail = text.len - at });
- for (key.text) |ch| {
- if (ch == '\n') {
- t.cur_row += 1;
- t.cur_col = 0;
- } else t.cur_col += 1;
- }
+ const end = modal.advanceBy(c, key.text);
+ setEditTextSpan(p, t, new, end, .{ .head = at, .tail = text.len - at });
+ t.cur_row = @as(i32, @intCast(end.row)) + eb.row0;
+ t.cur_col = @intCast(end.col);
t.cur_pinned = true;
t.show();
return;
@@ -2489,7 +2480,7 @@ test "a typed edit's known end is where the texts' comparison would find it" {
setEditTextEnd(p, t, scanned, null);
const by_scan = t.last_edit.?;
const known = try modal.insertAt(p.gpa, base, c.at, c.s);
- setEditTextEnd(p, t, known, insertedEnd(c.at, c.s));
+ setEditTextEnd(p, t, known, modal.advanceBy(c.at, c.s));
try std.testing.expectEqual(by_scan.row, t.last_edit.?.row);
try std.testing.expectEqual(by_scan.col, t.last_edit.?.col);
}
diff --git a/src/modal.zig b/src/modal.zig
index 71a9ddcb..0054afeb 100644
--- a/src/modal.zig
+++ b/src/modal.zig
@@ -1196,15 +1196,8 @@ pub fn pasteLineBelow(alloc: std.mem.Allocator, content: []const u8, row: usize,
// the cursor position AFTER `text` inserted at `c` (one past its last char).
pub fn advanceBy(c: Cursor, text: []const u8) Cursor {
- var r = c.row;
- var col = c.col;
- for (text) |ch| {
- if (ch == '\n') {
- r += 1;
- col = 0;
- } else col += 1;
- }
- return .{ .row = r, .col = col };
+ const nl = std.mem.lastIndexOfScalar(u8, text, '\n') orelse return .{ .row = c.row, .col = c.col + text.len };
+ 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).