diff options
Diffstat (limited to 'src/edit.zig')
| -rw-r--r-- | src/edit.zig | 168 |
1 files changed, 84 insertions, 84 deletions
diff --git a/src/edit.zig b/src/edit.zig index 5b2630fb..a458a034 100644 --- a/src/edit.zig +++ b/src/edit.zig @@ -35,14 +35,14 @@ pub const Error = error{ OutOfMemory, WriteFailed, UnknownPoint }; /// names (`"<reg>`), the default one otherwise. `_` swallows it, and the /// registers computed from the editor (`#`, `.`, `%`) cannot be written. /// `+` and `*` are the system clipboard: the value is sent there too. -pub fn setYank(p: *Pardes, text: []const u8) void { +pub fn setYank(p: *Pardes, text: []const u8) error{OutOfMemory}!void { var name: u21 = if (p.view().register != 0) p.view().register else Registers.default; switch (name) { '_', '#', '.', '%' => return, '*' => name = '+', else => {}, } - p.registers.put(p.gpa, name, text, p.multi_index, p.multi_count, p.multi_first); + try p.registers.put(p.gpa, name, text, p.multi_index, p.multi_count, p.multi_first); if (name == '+' and p.multi_first) p.emit(.{ .set_clipboard = {} }); } @@ -181,7 +181,7 @@ pub fn clipYank(p: *Pardes, pane: *Pane, main_only: bool) void { defer v.register = named; // A kind's pane: what it has selected (Kind.Slot.selection). if (pane.addon) |k| { - if (k.selection.len > 0) setYank(p, k.selection); + if (k.selection.len > 0) setYank(p, k.selection) catch |err| p.reportError(p.view().active, "yank", err); return; } if (pane.body.nsel > 0 and !main_only) { @@ -467,21 +467,21 @@ fn editTextEol(p: *Pardes, t: *Text, b: Bounds) Error!?panes.EditText { } /// install a rewritten editable text (frees the old one) -pub fn setEditText(p: *Pardes, t: *Text, new: []u8) void { - setEditTextEnd(p, t, new, null); +pub fn setEditText(p: *Pardes, t: *Text, new: []u8) Error!void { + return setEditTextEnd(p, t, new, null); } /// `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. -pub fn setEditTextEnd(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor) void { - setEditTextSpan(p, t, new, known_end, null); +pub fn setEditTextEnd(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor) Error!void { + return setEditTextSpan(p, t, new, known_end, null); } /// `setEditTextEnd` with what a file's splice left alone at either end, when /// the caller knows it (typing): the line index is then not found again by /// comparing the whole old and new texts, which a keystroke in a 7 MB file did. -fn setEditTextSpan(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor, span: ?panes.File.Span) void { +fn setEditTextSpan(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor, span: ?panes.File.Span) Error!void { if (t.what == .tag) { // A tag is edited as it is shown, but only what follows its // prefix is its own: an edit that changed the prefix is refused. @@ -489,12 +489,12 @@ fn setEditTextSpan(p: *Pardes, t: *Text, new: []u8, known_end: ?modal.Cursor, sp // caller reads the new text back, as a body's is kept. defer retireEditText(p, new); const pane = t.pane() orelse return; - const prefix = tagline.pathPrefix(p, p.scratch.allocator(), pane) catch |err| return p.reportError(p.view().active, "edit", err); + const prefix = try tagline.pathPrefix(p, p.scratch.allocator(), pane); if (!std.mem.startsWith(u8, new, prefix)) { t.refused = true; return; } - const tail = p.gpa.dupe(u8, new[prefix.len..]) catch |err| return p.reportError(p.view().active, "edit", err); + const tail = try p.gpa.dupe(u8, new[prefix.len..]); if (t.own) |old| p.gpa.free(old); t.own = tail; return; @@ -570,13 +570,13 @@ pub fn clampFileCursor(t: *Text) bool { return old_row != t.cur_row or old_col != t.cur_col; } -pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) void { +pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) Error!void { if (t.what == .body) if (t.pane()) |pane| if (pane.addon != null) return; pinPaneCursor(p, t); _ = clampFileCursor(t); // snapshot once per insert session (WITH the pre-insert selection) so // `u` undoes the whole session and restores what was selected - pushUndo(p, t); + try pushUndo(p, t); t.select = false; t.restore_cursor = false; t.sticky_col = -1; @@ -612,8 +612,8 @@ pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) void { const terminal = t.what == .body and if (t.pane()) |pane| pane.isTerminal() else false; var now = text; if (hi < text.len and gap >= text.len and !terminal) { - const grown = std.mem.concat(p.gpa, u8, &.{ text, "\n" }) catch |err| return p.reportError(p.view().active, "edit", err); - setEditText(p, t, grown); + const grown = try std.mem.concat(p.gpa, u8, &.{ text, "\n" }); + try setEditText(p, t, grown); now = grown; // what setEditText keeps; `text` may be gone } const gc = modal.positionAt(now, gap); @@ -637,12 +637,12 @@ pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) void { t.vsel.active = false; const below = where == .open_below; const abs: i32 = if (b) |bb| (if (below) bb.hi_row else bb.lo_row) else t.cur_row; - const eb = (editText(p, t, abs, abs, -1) catch |err| return p.reportError(p.view().active, "edit", err)) orelse return; + const eb = (try editText(p, t, abs, abs, -1)) orelse return; const row: usize = @intCast(@max(0, abs - eb.row0)); const ind = modal.indentText(modal.lineSlice(eb.text, row)); const arena = p.scratch.allocator(); - const block_len = std.math.mul(usize, cnt, ind.len + 1) catch return p.reportError(p.view().active, "edit", error.OutOfMemory); - const block = arena.alloc(u8, block_len) catch |err| return p.reportError(p.view().active, "edit", err); + const block_len = std.math.mul(usize, cnt, ind.len + 1) catch return error.OutOfMemory; + const block = try arena.alloc(u8, block_len); var block_at: usize = 0; for (0..cnt) |_| { if (below) { @@ -660,8 +660,8 @@ pub fn enterInsert(p: *Pardes, t: *Text, where: InsertAt, cnt: usize) void { .{ .row = row, .col = modal.lineSlice(eb.text, row).len } else .{ .row = row, .col = 0 }; - const new = modal.insertAt(p.gpa, eb.text, at, block) catch |err| return p.reportError(p.view().active, "edit", err); - setEditText(p, t, new); + const new = try modal.insertAt(p.gpa, eb.text, at, block); + try setEditText(p, t, new); t.cur_row = @as(i32, @intCast(if (below) row + 1 else row)) + eb.row0; t.cur_col = @intCast(ind.len); t.cur_pinned = true; @@ -848,7 +848,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { t.last_edit = .{ .row = @intCast(end.row), .col = @intCast(end.col) }; } else { const new = try modal.spliceAlloc(p.gpa, text, at, at, key.text); - setEditTextSpan(p, t, new, end, .{ .head = at, .tail = text.len - at }); + try 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); @@ -869,7 +869,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { const from = @min(wr.anchor, wr.head); const fc = modal.positionAt(e2.text, from); const new = try modal.deleteSpan(p.gpa, e2.text, fc, c2); - setEditTextEnd(p, t, new, fc); + try setEditTextEnd(p, t, new, fc); t.cur_row = @as(i32, @intCast(fc.row)) + e2.row0; t.cur_col = @intCast(fc.col); t.cur_pinned = true; @@ -887,13 +887,13 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { const to = @max(wr.anchor, wr.head); if (to <= g) return; const new = try modal.deleteSpan(p.gpa, e2.text, c2, modal.positionAt(e2.text, to)); - setEditTextEnd(p, t, new, c2); + try setEditTextEnd(p, t, new, c2); t.cur_pinned = true; return; } if (hit(key, config.kill_to_line_start)) { const new = try modal.deleteSpan(p.gpa, text, .{ .row = c.row, .col = 0 }, c); - setEditTextEnd(p, t, new, .{ .row = c.row, .col = 0 }); + try setEditTextEnd(p, t, new, .{ .row = c.row, .col = 0 }); t.cur_col = 0; t.cur_pinned = true; t.show(); @@ -902,7 +902,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { if (hit(key, config.kill_to_line_end)) { const llen = modal.lineSlice(text, c.row).len; const new = try modal.deleteSpan(p.gpa, text, c, .{ .row = c.row, .col = llen }); - setEditTextEnd(p, t, new, c); + try setEditTextEnd(p, t, new, c); t.cur_pinned = true; return; } @@ -915,7 +915,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { // cursor go and the new line takes the indent if (std.mem.indexOfNone(u8, before, " \t") == null) { const new = try modal.insertAt(p.gpa, text, .{ .row = c.row, .col = 0 }, "\n"); - setEditTextEnd(p, t, new, .{ .row = c.row + 1, .col = 0 }); + try setEditTextEnd(p, t, new, .{ .row = c.row + 1, .col = 0 }); t.cur_row += 1; t.cur_pinned = true; t.show(); @@ -939,7 +939,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { const cut = try modal.deleteSpan(p.gpa, text, .{ .row = c.row, .col = kept }, c); defer p.gpa.free(cut); const new = try modal.insertAt(p.gpa, cut, .{ .row = c.row, .col = kept }, block); - setEditTextEnd(p, t, new, .{ .row = c.row + 1, .col = indent }); + try setEditTextEnd(p, t, new, .{ .row = c.row + 1, .col = indent }); t.cur_row += 1; t.cur_col = @intCast(indent); t.cur_pinned = true; @@ -972,7 +972,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { } else { const new = if (e > s) try modal.spliceAlloc(p.gpa, text, s, e, "") else try p.gpa.dupe(u8, text); const span: panes.File.Span = if (e > s) .{ .head = s, .tail = text.len - e } else .{ .head = text.len, .tail = 0 }; - setEditTextSpan(p, t, new, .{ .row = c.row, .col = prev }, span); + try setEditTextSpan(p, t, new, .{ .row = c.row, .col = prev }, span); } t.cur_col = @intCast(prev); t.cur_pinned = true; @@ -987,7 +987,7 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { if (row == 0) return; const prevlen = modal.lineSlice(e2.text, row - 1).len; const new = try modal.deleteSpan(p.gpa, e2.text, .{ .row = row - 1, .col = prevlen }, .{ .row = row, .col = 0 }); - setEditTextEnd(p, t, new, .{ .row = row - 1, .col = prevlen }); + try setEditTextEnd(p, t, new, .{ .row = row - 1, .col = prevlen }); t.cur_row -= 1; t.cur_col = @intCast(prevlen); t.cur_pinned = true; @@ -1000,13 +1000,13 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { if (c.col < line.len) { const at = modal.offsetAt(text, c); const new = try modal.spliceAlloc(p.gpa, text, at, modal.nextGrapheme(text, at), ""); - setEditTextEnd(p, t, new, c); + try setEditTextEnd(p, t, new, c); } else { const e2 = (try editText(p, t, t.cur_row, t.cur_row + 1, t.cur_col)) orelse return; const row: usize = @intCast(@max(0, t.cur_row - e2.row0)); if (row + 1 >= modal.lineCount(e2.text)) return; const new = try modal.deleteSpan(p.gpa, e2.text, .{ .row = row, .col = c.col }, .{ .row = row + 1, .col = 0 }); - setEditTextEnd(p, t, new, .{ .row = row, .col = c.col }); + try setEditTextEnd(p, t, new, .{ .row = row, .col = c.col }); } t.cur_pinned = true; }, @@ -1031,21 +1031,21 @@ fn insertEdit(p: *Pardes, t: *Text, key: Key) Error!void { if (!syntax.supportsPath(f.path)) break :smart; return normal.nodeSelect(p, pane, t, .parent_node_end); } - insertTab(p, t); + try insertTab(p, t); }, else => {}, } } -pub fn insertTab(p: *Pardes, t: *Text) void { - const eb = (editText(p, t, t.cur_row, t.cur_row, t.cur_col) catch |err| return p.reportError(p.view().active, "edit", err)) orelse return; +pub fn insertTab(p: *Pardes, t: *Text) Error!void { + const eb = (try editText(p, t, t.cur_row, t.cur_row, 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 pad = modal.INDENT_W - (c.col % modal.INDENT_W); - const new = modal.insertAt(p.gpa, eb.text, c, " "[0..pad]) catch |err| return p.reportError(p.view().active, "edit", err); - setEditTextEnd(p, t, new, .{ .row = c.row, .col = c.col + pad }); + const new = try modal.insertAt(p.gpa, eb.text, c, " "[0..pad]); + try setEditTextEnd(p, t, new, .{ .row = c.row, .col = c.col + pad }); t.cur_col += @intCast(pad); t.cur_pinned = true; t.show(); @@ -1078,14 +1078,14 @@ pub fn normalDelete(p: *Pardes, t: *Text, yank: bool) Error!void { // cell too, where the delete joins the lines. if (!t.vsel.active) t.vsel = .{ .active = true, .row = t.cur_row, .col = t.cur_col, .explicit = false }; const b = vselBounds(t); - if (yank) setYank(p, try vselText(p, t)); // read the range before the move - pushUndo(p, t); + if (yank) try setYank(p, try vselText(p, t)); // read the range before the move + try pushUndo(p, t); const eb = (try editTextEol(p, t, b)) orelse { t.vsel.active = false; return; }; const new = try deleteSelection(p, t, eb); - setEditText(p, t, new); + try setEditText(p, t, new); t.cur_row = b.lo_row; t.cur_col = b.lo_col; t.clampCursor(new, eb.row0); @@ -1094,15 +1094,15 @@ pub fn normalDelete(p: *Pardes, t: *Text, yank: bool) Error!void { pub fn normalYank(p: *Pardes, t: *Text) Error!void { t.select = false; if (t.what == .body) if (t.pane()) |pane| if (pane.rawPointerText(pane.sel[sel_slot])) |text| { - setYank(p, text); + try setYank(p, text); return; }; - setYank(p, try if (t.vsel.active) vselText(p, t) else selText(p, t)); + try setYank(p, try if (t.vsel.active) vselText(p, t) else selText(p, t)); } pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usize) Error!void { if (y0.len == 0) return; - pushUndo(p, t); + try pushUndo(p, t); t.select = false; t.sticky_col = -1; const arena = p.scratch.allocator(); @@ -1133,7 +1133,7 @@ pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usiz out = try modal.pasteLineBelow(p.gpa, eb.text, row, block_text); t.cur_row = @as(i32, @intCast(row + n)) + row0; } - setEditText(p, t, out); + try setEditText(p, t, out); t.vsel = .{ .active = true, .row = t.cur_row - @as(i32, @intCast(n - 1)), .col = 0, .explicit = false }; // cursor ON the last pasted line's '\n' (helix) const llen = modal.lineSlice(out, @intCast(@max(0, t.cur_row - row0))).len; @@ -1150,7 +1150,7 @@ pub fn pasteText(p: *Pardes, t: *Text, y0: []const u8, before: bool, count: usiz break :blk .{ .row = hrow, .col = gcol }; }; const out = try modal.insertAt(p.gpa, eb.text, at, y); - setEditText(p, t, out); + try setEditText(p, t, out); t.vsel = .{ .active = modal.nextGrapheme(y, 0) < y.len, .row = @as(i32, @intCast(at.row)) + row0, .col = @intCast(at.col), .explicit = false }; const end = modal.advanceBy(at, y); if (end.col > 0) { @@ -1173,17 +1173,17 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void { const eb = (try editTextEol(p, t, b)) orelse return; const hi_len = modal.lineSlice(eb.text, @intCast(@max(0, b.hi_row - eb.row0))).len; if (b.lo_col == 0 and @as(usize, @intCast(@max(0, b.hi_col))) >= hi_len) { - setYank(p, try vselText(p, t)); - pushUndo(p, t); + try setYank(p, try vselText(p, t)); + try pushUndo(p, t); const d = try modal.deleteLines(p.gpa, eb.text, @intCast(@max(0, b.lo_row - eb.row0)), @intCast(@max(0, b.hi_row - eb.row0))); - setEditText(p, t, d); + try 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, row)); const arena = p.scratch.allocator(); const block = try std.fmt.allocPrint(arena, "{s}\n", .{ind}); const new = try modal.insertAt(p.gpa, d, .{ .row = row, .col = 0 }, block); - setEditText(p, t, new); + try setEditText(p, t, new); t.cur_row = @as(i32, @intCast(row)) + eb.row0; t.cur_col = @intCast(ind.len); t.vsel.active = false; @@ -1193,10 +1193,10 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void { t.show(); return; } - setYank(p, try vselText(p, t)); - pushUndo(p, t); + try setYank(p, try vselText(p, t)); + try pushUndo(p, t); const new = try deleteSelection(p, t, eb); - setEditText(p, t, new); + try setEditText(p, t, new); t.cur_row = b.lo_row; t.cur_col = b.lo_col; t.clampCursor(new, eb.row0); @@ -1204,12 +1204,12 @@ pub fn normalChange(p: *Pardes, t: *Text) Error!void { t.normal.clear(); return; } - pushUndo(p, t); + try pushUndo(p, t); const eb = (try editTextEol(p, t, selRows(t))) orelse return; const r = t.primaryRange(eb.text, eb.row0); - setYank(p, eb.text[r.anchor..r.head]); + try setYank(p, eb.text[r.anchor..r.head]); const new = try modal.spliceAlloc(p.gpa, eb.text, r.anchor, r.head, ""); - setEditText(p, t, new); + try 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; @@ -1248,7 +1248,7 @@ fn setEditCells(p: *Pardes, t: *Text, new: []u8, row0: i32, cells: []const Text. const ac = modal.positionAt(new, c.anc); s.* = .{ .row = @as(i32, @intCast(cc.row)) + row0, .col = @intCast(cc.col), .arow = @as(i32, @intCast(ac.row)) + row0, .acol = @intCast(ac.col) }; } - setEditText(p, t, new); + try setEditText(p, t, new); const text = try flatSurface(p, t); var rs: [Text.max_selections]modal.Selection = undefined; for (sels[0..cells.len], rs[0..cells.len]) |s, *r| r.* = Text.cellRange(text, s.arow, s.acol, s.row, s.col); @@ -1270,9 +1270,9 @@ pub fn normalReplaceChar(p: *Pardes, t: *Text, ch: u21) Error!void { const encoded_len = std.unicode.utf8Encode(ch, &encoded) catch return; // a surrogate half types nothing const rep = try p.scratch.allocator().alloc(u8, graphemes * encoded_len); for (0..graphemes) |i| @memcpy(rep[i * encoded_len ..][0..encoded_len], encoded[0..encoded_len]); - pushUndo(p, t); + try pushUndo(p, t); const new = try modal.spliceAlloc(p.gpa, eb.text, lo, hi, rep); - setEditText(p, t, new); + try setEditText(p, t, new); const end = lo + rep.len; const mapped: modal.Selection = if (before.head < before.anchor) .{ .anchor = end, .head = lo } @@ -1297,9 +1297,9 @@ fn replaceWithText(p: *Pardes, t: *Text, y: []const u8) Error!void { const eb = (try editTextEol(p, t, selRows(t))) orelse return; const r = t.primaryRange(eb.text, eb.row0); const lo = @min(r.anchor, r.head); - pushUndo(p, t); + try pushUndo(p, t); const new = try modal.spliceAlloc(p.gpa, eb.text, lo, @max(r.anchor, r.head), y); - setEditText(p, t, new); + try setEditText(p, t, new); 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); } @@ -1310,9 +1310,9 @@ pub fn normalCase(p: *Pardes, t: *Text, op: modal.CaseOp) Error!void { t.select = false; const eb = (try editTextEol(p, t, selRows(t))) orelse return; const r = t.primaryRange(eb.text, eb.row0); - pushUndo(p, t); + try pushUndo(p, t); const new = try modal.changeCase(p.gpa, eb.text, @min(r.anchor, r.head), @max(r.anchor, r.head), op); - setEditText(p, t, new); + try setEditText(p, t, new); } /// Where `pos` lands after `chs`, which are ascending and disjoint. @@ -1371,7 +1371,7 @@ pub fn normalJoin(p: *Pardes, t: *Text, select_space: bool) Error!void { const chs = changes.items; const chs_len = chs.len; if (chs_len == 0) return; - pushUndo(p, t); + try pushUndo(p, t); const new = try sam.apply(p.gpa, text, chs[0..chs_len]); // every range's cells ride through the edit; Alt-J replaces them with // the spaces, when it put any in @@ -1422,7 +1422,7 @@ pub fn normalRotateContents(p: *Pardes, t: *Text, forward: bool, cnt: usize) Err .text = text[@min(src.anchor, src.head)..@max(src.anchor, src.head)], }; } - pushUndo(p, t); + try pushUndo(p, t); const new = try sam.apply(p.gpa, text, chs[0..n]); var cells: [Text.max_selections]Text.Cells = undefined; var delta: i64 = 0; @@ -1490,7 +1490,7 @@ pub fn normalAlign(p: *Pardes, t: *Text) Error!void { m += 1; } if (m == 0) return; - pushUndo(p, t); + try pushUndo(p, t); const new = try sam.apply(p.gpa, text, chs[0..m]); var cells: [Text.max_selections]Text.Cells = undefined; for (rs[0..n], cells[0..n]) |r, *cell| { @@ -1544,7 +1544,7 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) Error!void { } } if (chs_len == 0) return; - pushUndo(p, t); + try pushUndo(p, t); const cur_off = modal.offsetAt(text, .{ .row = @intCast(@max(0, t.cur_row - eb.row0)), .col = @intCast(@max(0, t.cur_col)) }); const anc_off = if (t.vsel.active) modal.offsetAt(text, .{ .row = @intCast(@max(0, t.vsel.row - eb.row0)), .col = @intCast(@max(0, t.vsel.col)) }) @@ -1553,7 +1553,7 @@ pub fn normalIndent(p: *Pardes, t: *Text, cnt: usize, add: bool) Error!void { const new = try sam.apply(p.gpa, text, chs[0..chs_len]); const nc = mapThroughChanges(chs[0..chs_len], cur_off); const na = mapThroughChanges(chs[0..chs_len], anc_off); - setEditText(p, t, new); + try setEditText(p, t, new); const cc = modal.positionAt(new, nc); const ac = modal.positionAt(new, na); t.cur_row = @as(i32, @intCast(cc.row)) + eb.row0; @@ -1635,7 +1635,7 @@ pub fn normalToggleComment(p: *Pardes, t: *Text) Error!void { .{ .q0 = @intCast(at), .q1 = @intCast(at), .text = ins }; chs_len += 1; } - pushUndo(p, t); + try pushUndo(p, t); // one edit, and the WHOLE selection rides through it (helix maps the // selection with the transaction) var cells: [Text.max_selections]Text.Cells = undefined; @@ -1653,10 +1653,10 @@ pub fn normalAdjustNumber(p: *Pardes, t: *Text, delta: i64) Error!void { 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); + try pushUndo(p, t); t.select = false; const new = try modal.spliceAlloc(p.gpa, eb.text, lo, hi, rep); - setEditText(p, t, new); + try setEditText(p, t, new); t.setRange(new, eb.row0, .{ .anchor = lo, .head = lo + rep.len }, false); } @@ -1667,14 +1667,14 @@ pub fn addNewline(p: *Pardes, t: *Text, below: bool, cnt: usize) Error!void { const sr = selRows(t); const at = if (below) sr.hi_row else sr.lo_row; const eb = (try editText(p, t, at, at, -1)) orelse return; - pushUndo(p, t); + try pushUndo(p, t); const row: usize = @intCast(@max(0, at - eb.row0)); const arena = p.scratch.allocator(); const nl = try arena.alloc(u8, cnt); @memset(nl, '\n'); const llen = modal.lineSlice(eb.text, row).len; const new = try modal.insertAt(p.gpa, eb.text, .{ .row = row, .col = if (below) llen else 0 }, nl); - setEditText(p, t, new); + try setEditText(p, t, new); if (!below) { // the whole selection moved down; stay on it const n: i32 = @intCast(cnt); @@ -1760,11 +1760,11 @@ pub fn surroundAdd(p: *Pardes, t: *Text, ch: u21) Error!void { 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); + try pushUndo(p, t); const open = [1]u8{pr.o}; const close = [1]u8{pr.c}; const new = try sam.apply(p.gpa, eb.text, &.{ .{ .q0 = @intCast(lo), .q1 = @intCast(lo), .text = &open }, .{ .q0 = @intCast(hi), .q1 = @intCast(hi), .text = &close } }); - setEditText(p, t, new); + try setEditText(p, t, new); // 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); } @@ -1785,7 +1785,7 @@ pub fn surroundDelete(p: *Pardes, t: *Text, ch: u21) Error!void { const drow = -eb.row0; // surface row -> buffer row const ra: modal.Cursor = .{ .row = @intCast(@as(i32, @intCast(r.a.row)) + drow), .col = r.a.col }; const rb: modal.Cursor = .{ .row = @intCast(@as(i32, @intCast(r.b.row)) + drow), .col = r.b.col }; - pushUndo(p, t); + try pushUndo(p, t); 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 }); @@ -1793,7 +1793,7 @@ pub fn surroundDelete(p: *Pardes, t: *Text, ch: u21) Error!void { .{ .q0 = @intCast(a_off), .q1 = @intCast(modal.nextGrapheme(text, a_off)), .text = "" }, .{ .q0 = @intCast(b_off), .q1 = @intCast(modal.nextGrapheme(text, b_off)), .text = "" }, }); - setEditText(p, t, new); + try setEditText(p, t, new); var nc = cur_off; if (nc > b_off) nc -= 1; if (nc > a_off) nc -= 1; @@ -1819,14 +1819,14 @@ pub fn surroundReplace(p: *Pardes, t: *Text, from: u21, to: u21) Error!void { const drow = -eb.row0; // surface row -> buffer row const ar: usize = @intCast(@as(i32, @intCast(r.a.row)) + drow); const br: usize = @intCast(@as(i32, @intCast(r.b.row)) + drow); - pushUndo(p, t); + try pushUndo(p, t); const out = try p.gpa.dupe(u8, eb.text); out[modal.lineStartOffset(out, ar) + r.a.col] = tp.o; out[modal.lineStartOffset(out, br) + r.b.col] = tp.c; - setEditText(p, t, out); + try setEditText(p, t, out); } -pub fn pushUndo(p: *Pardes, t: *Text) void { +pub fn pushUndo(p: *Pardes, t: *Text) error{OutOfMemory}!void { // one keystroke, one undo step — even when it edited at ten cursors. if (!p.multi_first) return; switch (t.what) { @@ -1838,23 +1838,23 @@ pub fn pushUndo(p: *Pardes, t: *Text) void { } const pane = t.pane() orelse return; if (pane.file != null) return panes.File.pushUndo(p, pane); - panes.terminal.pushUndo(p, pane); + return panes.terminal.pushUndo(p, pane); } /// Undo (back) or redo one step of the text's history: its file's, its /// terminal overlay's, or a tag's or header's own. -pub fn undoStep(p: *Pardes, t: *Text, back: bool) void { +pub fn undoStep(p: *Pardes, t: *Text, back: bool) error{OutOfMemory}!void { if (t.what == .tag or t.what == .column or t.what == .workspace) return stepOwn(p, t, back); if (t.what != .body) return; const pane = t.pane() orelse return; if (pane.file != null) return panes.File.undoStep(p, pane, back); - panes.terminal.undoStep(p, pane, back); + return panes.terminal.undoStep(p, pane, back); } /// Undo or redo an edit of a text's own characters; stepping back past its /// first edit gives a tag its default again. -fn stepOwn(p: *Pardes, t: *Text, back: bool) void { - const to = t.step(p.gpa, if (t.own) |own| .{ .text = own } else null, back) orelse return; +fn stepOwn(p: *Pardes, t: *Text, back: bool) error{OutOfMemory}!void { + const to = (try t.step(p.gpa, if (t.own) |own| .{ .text = own } else null, back)) orelse return; if (t.own) |own| p.gpa.free(own); t.own = if (to.ovl) |buffer| buffer.text else null; } @@ -2206,10 +2206,10 @@ test "a typed edit's known end is where the texts' comparison would find it" { const base = try p.gpa.dupe(u8, pane.file.?.content); defer p.gpa.free(base); const scanned = try modal.insertAt(p.gpa, base, c.at, c.s); - setEditTextEnd(p, t, scanned, null); + try 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, modal.advanceBy(c.at, c.s)); + try 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); } |
