diff options
Diffstat (limited to 'src/normal.zig')
| -rw-r--r-- | src/normal.zig | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/src/normal.zig b/src/normal.zig index 1fa5bf11..bd62482e 100644 --- a/src/normal.zig +++ b/src/normal.zig @@ -52,8 +52,8 @@ pub fn multiOnce(p: *Pardes) bool { return p.multi_first; } -fn startSelRegex(p: *Pardes, pane: *Pane, t: *Text, mode: modal.Normal.RegexMode) void { - const text = edit.flatSurface(p, t) catch return; +fn startSelRegex(p: *Pardes, pane: *Pane, t: *Text, mode: modal.Normal.RegexMode) edit.Error!void { + const text = try edit.flatSurface(p, t); const got = t.ranges(text, 0, &pane.sel_snap); pane.nsel_snap = @intCast(got.n); pane.sel_snap_pri = @intCast(got.pri); @@ -84,7 +84,7 @@ pub fn selRegexArmed(pane: *Pane) ?struct { pat: []const u8, mode: modal.Normal. /// had when s/S/K/Alt-K was pressed; the prompt's snapshot of them lives on /// `pane`. pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: modal.Normal.RegexMode) void { - const text = edit.flatSurface(p, t) catch return; + const text = edit.flatSurface(p, t) catch |err| return p.reportError(p.view().active, "edit", err); const snap = pane.sel_snap[0..pane.nsel_snap]; var out: [Text.max_selections]modal.Selection = undefined; var m: usize = 0; @@ -93,7 +93,7 @@ pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: m var hay_all = text; if (for (pat) |c| { if (std.ascii.isUpper(c)) break false; - } else true) hay_all = std.ascii.allocLowerString(p.scratch.allocator(), text) catch return; + } else true) hay_all = std.ascii.allocLowerString(p.scratch.allocator(), text) catch |err| return p.reportError(p.view().active, "edit", err); for (snap) |r| { const from = @min(r.anchor, r.head); const to = @min(@max(r.anchor, r.head), text.len); @@ -147,7 +147,7 @@ pub fn applySelRegex(p: *Pardes, pane: *Pane, t: *Text, pat: []const u8, mode: m /// Search only the primary range, as Helix does. Select mode adds a new /// match and makes it primary; normal mode replaces only the primary. -fn searchStep(p: *Pardes, t: *Text, text: []const u8, forward: bool, count: u32) void { +fn searchStep(p: *Pardes, t: *Text, text: []const u8, forward: bool, count: u32) edit.Error!void { const v = p.view(); const name = if (v.register != 0) v.register else p.registers.last_search; const pat = p.registers.value(name, 0) orelse return; @@ -158,7 +158,7 @@ fn searchStep(p: *Pardes, t: *Text, text: []const u8, forward: bool, count: u32) var hay = text; if (for (pat) |c| { if (std.ascii.isUpper(c)) break false; - } else true) hay = std.ascii.allocLowerString(p.scratch.allocator(), text) catch return; + } else true) hay = try std.ascii.allocLowerString(p.scratch.allocator(), text); for (0..count) |_| { var ranges: [Text.max_selections]modal.Selection = undefined; const got = t.ranges(text, 0, &ranges); @@ -207,7 +207,7 @@ fn lastMatch(re: *SearchRegex, hay: []const u8, from: usize, to: usize) ?regexp. return last; } -pub fn searchSelection(p: *Pardes, t: *Text, text: []const u8, boundary: bool) void { +pub fn searchSelection(p: *Pardes, t: *Text, text: []const u8, boundary: bool) edit.Error!void { var ranges: [Text.max_selections]modal.Selection = undefined; const got = t.ranges(text, 0, &ranges); var pattern: std.ArrayList(u8) = .empty; @@ -215,10 +215,10 @@ pub fn searchSelection(p: *Pardes, t: *Text, text: []const u8, boundary: bool) v for (ranges[0..got.n], 0..) |r, i| { const from = @min(r.anchor, r.head); const to = @min(text.len, @max(r.anchor, r.head)); - if (i > 0) pattern.append(arena, '|') catch return; - if (boundary and modal.searchWordAt(text, from) and !modal.searchWordBefore(text, from)) pattern.appendSlice(arena, "\\b") catch return; - appendSearchLiteral(&pattern, arena, text[from..to]) catch return; - if (boundary and to < text.len and modal.searchWordBefore(text, to) and !modal.searchWordAt(text, to)) pattern.appendSlice(arena, "\\b") catch return; + if (i > 0) try pattern.append(arena, '|'); + if (boundary and modal.searchWordAt(text, from) and !modal.searchWordBefore(text, from)) try pattern.appendSlice(arena, "\\b"); + try appendSearchLiteral(&pattern, arena, text[from..to]); + if (boundary and to < text.len and modal.searchWordBefore(text, to) and !modal.searchWordAt(text, to)) try pattern.appendSlice(arena, "\\b"); } const name = if (p.view().register != 0) p.view().register else '/'; p.registers.put(p.gpa, name, pattern.items, 0, 1, true); @@ -342,14 +342,14 @@ fn objectJump(p: *Pardes, pane: *Pane, t: *Text, text: []const u8, range: modal. /// which is made on the first of these keys after an edit and kept, never /// made again for a key that changed nothing. A range the tree has nothing /// to say about stays as it was; a text without a grammar, all of them. -pub fn nodeSelect(p: *Pardes, pane: *Pane, t: *Text, walk: modal.Normal.NodeWalk) void { +pub fn nodeSelect(p: *Pardes, pane: *Pane, t: *Text, walk: modal.Normal.NodeWalk) edit.Error!void { if (t.what != .body) return; const f = if (pane.file) |*file| file else return; const tree = fileTree(p, f) orelse return; const text = f.content; const expl = t.vsel.active and t.vsel.explicit; const arena = p.scratch.allocator(); - const rs = arena.alloc(modal.Selection, Text.max_selections) catch return; + const rs = try arena.alloc(modal.Selection, Text.max_selections); const got = t.ranges(text, 0, rs[0..Text.max_selections]); if (walk == .shrink_node) if (f.node_history.pop()) |prev| { defer p.gpa.free(prev.ranges); @@ -366,8 +366,8 @@ pub fn nodeSelect(p: *Pardes, pane: *Pane, t: *Text, walk: modal.Normal.NodeWalk for (f.node_history.items) |step| p.gpa.free(step.ranges); f.node_history.clearRetainingCapacity(); }; - const out = arena.alloc(modal.Selection, Text.max_selections) catch return; - const spans = arena.alloc(syntax.Span, Text.max_selections) catch return; + const out = try arena.alloc(modal.Selection, Text.max_selections); + const spans = try arena.alloc(syntax.Span, Text.max_selections); var m: usize = 0; for (rs[0..got.n]) |r| { if (m == out.len) break; @@ -410,15 +410,15 @@ pub fn nodeSelect(p: *Pardes, pane: *Pane, t: *Text, walk: modal.Normal.NodeWalk /// `gw` (helix goto_word): label the words in view, nearest the cursor first, /// with two letters each; the next two keys choose one (`jumpKey`). -fn startJump(p: *Pardes, pane: *Pane, t: *Text) void { +fn startJump(p: *Pardes, pane: *Pane, t: *Text) edit.Error!void { if (t.what != .body) return; - const text = edit.flatSurface(p, t) catch return; + const text = try edit.flatSurface(p, t); const cursor = modal.selectionCursor(text, t.primaryRange(text, 0)); const top: usize = @intCast(@max(0, pane.scroll())); const start = @min(modal.lineStartOffset(text, top), text.len); const end = @min(modal.lineStartOffset(text, top + pane.rows), text.len); const letters = config.jump_label_alphabet.len; - pane.jump.resize(p.gpa, letters * letters) catch return; + try pane.jump.resize(p.gpa, letters * letters); pane.jump.shrinkRetainingCapacity(modal.jumpWords(text, cursor, start, end, pane.jump.items)); pane.jump_first = null; } @@ -438,7 +438,7 @@ pub fn jumpKey(p: *Pardes, pane: *Pane, t: *Text, key: Key) void { defer pane.jump.clearRetainingCapacity(); if (chosen >= pane.jump.items.len) return; const r = pane.jump.items[chosen]; - const text = edit.flatSurface(p, t) catch return; + const text = edit.flatSurface(p, t) catch |err| return p.reportError(p.view().active, "edit", err); if (!t.select) return t.setRange(text, 0, .{ .anchor = @min(r.anchor, r.head), .head = @max(r.anchor, r.head) }, true); // select mode stretches the primary to the word (helix Movement::Extend) const pr = t.primaryRange(text, 0); @@ -547,10 +547,10 @@ pub fn replaySels(p: *Pardes, t: *Text, what: Replay) void { .explicit = explicit, }; t.sticky_col = list[i].sticky; - switch (what) { + (switch (what) { .normal => |normal_action| executeNormalAction(p, t, normal_action), .insert => |insert_key| edit.insertKey(p, t, insert_key), - } + }) catch |err| p.reportError(p.view().active, "edit", err); if (p.multi_stop) break; if (i == pri) { after = KeyState.of(t); @@ -579,7 +579,7 @@ pub fn replaySels(p: *Pardes, t: *Text, what: Replay) void { t.nsel = 0; return; } - const text = edit.flatSurface(p, t) catch return; + const text = edit.flatSurface(p, t) catch |err| return p.reportError(p.view().active, "edit", err); var rs: [Text.max_selections]modal.Selection = undefined; var st: [Text.max_selections]i32 = undefined; for (res[0..n], 0..) |r, k| { @@ -603,11 +603,11 @@ pub fn handleNormal(p: *Pardes, t: *Text, key: Key) void { p.view().register = named; defer p.view().register = 0; if (t.nsel == 0 or action.once()) - return executeNormalAction(p, t, action); + return executeNormalAction(p, t, action) catch |err| p.reportError(p.view().active, "edit", err); replaySels(p, t, .{ .normal = action }); } -fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) void { +fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!void { const v = p.view(); // What reaches past the text -- a builtin, a search, a language query -- // acts for the pane the text belongs to; a header's is the active one. @@ -620,7 +620,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) void { }; if (raw_selection and a.role == .yank) return edit.normalYank(p, t); edit.pinPaneCursor(p, t); - const text = edit.flatSurface(p, t) catch return; + const text = try edit.flatSurface(p, t); const range = t.primaryRange(text, 0); const cur = modal.selectionCursor(text, range); @@ -701,7 +701,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) void { .replace_prefix => return edit.normalReplaceChar(p, t, a.char), // `mm` .match_prefix => { - const lines = edit.paneCursorLines(p, t) catch return; + const lines = try edit.paneCursorLines(p, t); const mc = modal.matchBracket(lines, modal.positionAt(text, cur)) orelse return; return t.pointMove(text, range, modal.offsetAt(text, mc)); }, @@ -859,7 +859,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) void { else return searchStep(p, t, text, a.role == .search_next, a.count), .search_selection, .search_selection_word => { - searchSelection(p, t, text, a.role == .search_selection_word); + try searchSelection(p, t, text, a.role == .search_selection_word); // The pattern is line 1 of this text's query pane, made or // reused, the keyboard staying here: n and N step its places. if (t.what == .body and pane.file != null and pane.query == null) { @@ -880,7 +880,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) void { // the keys of this command (a count, `"<reg>`, the `Q`) are not // the macro's m.recorded.shrinkRetainingCapacity(m.recorded.items.len -| (m.command.items.len + 1)); - const keys = Macro.encode(p.gpa, m.recorded.items) catch return; + const keys = try Macro.encode(p.gpa, m.recorded.items); defer p.gpa.free(keys); p.registers.put(p.gpa, m.recording, keys, 0, 1, true); m.recording = 0; @@ -888,7 +888,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) void { .macro_replay => { const name: u21 = if (v.register != 0) v.register else '@'; const notation = p.registers.value(name, 0) orelse return; - const keys = Macro.decode(p.scratch.allocator(), notation) catch return; + const keys = try Macro.decode(p.scratch.allocator(), notation); v.register = 0; for (0..a.count) |_| keyboard.replayKeys(p, keys); }, @@ -896,7 +896,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) void { const m = &p.macro; if (m.last_insert.items.len == 0 or m.repeating) return; if (m.inserting) return; // the session is still open - const keys = p.scratch.allocator().dupe(Macro.Stored, m.last_insert.items) catch return; + const keys = try p.scratch.allocator().dupe(Macro.Stored, m.last_insert.items); m.repeating = true; defer m.repeating = false; // helix repeat_last_insert: the command once, what was typed @@ -1000,7 +1000,7 @@ test "flat text movement and selection replay need no scratch rows" { try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col); handleNormal(p, &pane.body, .{ .cp = 'h' }); try std.testing.expectEqual(@as(i32, 0), pane.body.cur_col); - executeNormalAction(p, &pane.body, .{ .role = .match_inside, .char = 'w' }); + try executeNormalAction(p, &pane.body, .{ .role = .match_inside, .char = 'w' }); try std.testing.expectEqual(@as(i32, 2), pane.body.cur_col); try std.testing.expectEqual(@as(i32, 0), pane.body.vsel.col); @@ -1022,11 +1022,11 @@ test "flat text movement and selection replay need no scratch rows" { pane.body.nsel = 0; pane.body.cur_col = 4; - executeNormalAction(p, &pane.body, .{ .role = .match_prefix }); + try std.testing.expectError(error.OutOfMemory, executeNormalAction(p, &pane.body, .{ .role = .match_prefix })); try std.testing.expect(denied.has_induced_failure); try std.testing.expectEqual(@as(i32, 4), pane.body.cur_col); denied.fail_index = std.math.maxInt(usize); - executeNormalAction(p, &pane.body, .{ .role = .match_prefix }); + try executeNormalAction(p, &pane.body, .{ .role = .match_prefix }); try std.testing.expectEqual(@as(i32, 8), pane.body.cur_col); } |
