diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-04 23:13:36 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-05 01:16:28 -0300 |
| commit | 3671eb9eafc19549bb94fe839e944e18707316e1 (patch) | |
| tree | b7220666204dc3df37d963a2b9884c84b9f714af /src/edit.zig | |
| parent | 71df23d63da76c6aedf91a2458d3057ed6da9a35 (diff) | |
| download | pardes-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/edit.zig')
| -rw-r--r-- | src/edit.zig | 19 |
1 files changed, 5 insertions, 14 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); } |
