summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 05:57:17 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commitb06543725a84ac5ce8cc48810b526c00032247cf (patch)
treedf38c0d6594ed768e386aa91441d08207eb29ece /src
parent3dd2072fc525cdadf4f55a69f745c73c0b8c2b21 (diff)
downloadpardes-b06543725a84ac5ce8cc48810b526c00032247cf.tar.gz
pardes-b06543725a84ac5ce8cc48810b526c00032247cf.zip
normal: a normal-mode action splits into the text's roles and the core's (docs/next-editor.md, R2, third slice): textAction takes `e: Editor` and does the 58 roles of the text alone (motions, selections, edits, undo, registers, Esc's and `*`'s own part), textRole says which those are, and executeNormalAction keeps the 34 that reach past the text (views, builtins, prompts, LSP, the look ring, jump labels, macros), handing the rest over; replaySels, handleNormal, searchStep and searchSelection take the context, keys.zig hands `p.editor()`; nothing a key does changes; core-test 810 of 949, shell-test 989 of 992, hxdiff 658 / parity 738 / hxgolf 124 with 0 mismatches, replicas pass
Diffstat (limited to 'src')
-rw-r--r--src/edit.zig4
-rw-r--r--src/keys.zig2
-rw-r--r--src/normal.zig360
3 files changed, 196 insertions, 170 deletions
diff --git a/src/edit.zig b/src/edit.zig
index 9ae00079..4b0e59fe 100644
--- a/src/edit.zig
+++ b/src/edit.zig
@@ -187,7 +187,7 @@ pub fn clipYank(p: *Pardes, pane: *Pane, main_only: bool) void {
return;
}
if (pane.body.nsel > 0 and !main_only) {
- normal.replaySels(p, &pane.body, .{ .normal = .{ .role = .yank } });
+ normal.replaySels(p.editor(), &pane.body, .{ .normal = .{ .role = .yank } });
} else {
const others = pane.body.nsel;
pane.body.nsel = 0;
@@ -901,7 +901,7 @@ pub fn enterInsert(e: Editor, t: *Text, where: InsertAt, cnt: usize) Error!void
pub fn handleInsert(e: Editor, t: *Text, key: Key) void {
if (t.nsel == 0) return insertKey(e, t, key) catch |err| e.reportError(err);
- normal.replaySels(e.p, t, .{ .insert = key });
+ normal.replaySels(e, t, .{ .insert = key });
}
pub fn insertKey(e: Editor, t: *Text, key: Key) Error!void {
diff --git a/src/keys.zig b/src/keys.zig
index 03a3b307..0277128b 100644
--- a/src/keys.zig
+++ b/src/keys.zig
@@ -442,7 +442,7 @@ pub fn handleKey(p: *Pardes, key: Key) void {
};
const was = t.mode;
switch (t.mode) {
- .normal => normal.handleNormal(p, t, key),
+ .normal => normal.handleNormal(p.editor(), t, key),
.insert => if (hit(key, config.escape) and !t.reg_wait)
edit.exitInsert(p.editor(), t)
else
diff --git a/src/normal.zig b/src/normal.zig
index 29addd66..77f0c24c 100644
--- a/src/normal.zig
+++ b/src/normal.zig
@@ -395,8 +395,8 @@ fn selStart(s: Text.SelRange) struct { row: i32, col: i32 } {
return .{ .row = s.row, .col = s.col };
}
-pub fn replaySels(p: *Pardes, t: *Text, what: Replay) void {
- const id = p.view().active;
+pub fn replaySels(e: Editor, t: *Text, what: Replay) void {
+ const id = e.p.view().active;
// a header belongs to no pane and cannot go away under the replay
const owner = t.pane();
const serial = if (owner) |pane| pane.serial else 0;
@@ -438,14 +438,14 @@ pub fn replaySels(p: *Pardes, t: *Text, what: Replay) void {
// each pass's result: cursor and anchor cells as distances from the
// end of the surface text, plus the range's own j/k goal column
var res: [Text.max_selections]struct { cur: usize, anc: usize, sticky: i32 } = undefined;
- p.multi_stop = false;
+ e.multi_stop.* = false;
var passes: usize = 0;
var i: usize = n;
while (i > 0) {
i -= 1;
- p.multi_first = passes == 0;
- p.multi_index = i;
- p.multi_count = n;
+ e.multi_first.* = passes == 0;
+ e.multi_index.* = i;
+ e.multi_count.* = n;
passes += 1;
saved.into(t);
t.nsel = 0; // the handler underneath sees ONE selection
@@ -459,16 +459,16 @@ pub fn replaySels(p: *Pardes, t: *Text, what: Replay) void {
};
t.sticky_col = list[i].sticky;
(switch (what) {
- .normal => |normal_action| executeNormalAction(p, t, normal_action),
- .insert => |insert_key| edit.insertKey(p.editor(), t, insert_key),
- }) catch |err| p.reportError(p.view().active, "edit", err);
- if (p.multi_stop) break;
+ .normal => |normal_action| executeNormalAction(e.p, t, normal_action),
+ .insert => |insert_key| edit.insertKey(e, t, insert_key),
+ }) catch |err| e.reportError(err);
+ if (e.multi_stop.*) break;
if (i == pri) {
after = KeyState.of(t);
after_expl = t.vsel.explicit;
}
- const text = edit.flatSurface(p.editor(), t) catch {
- p.multi_stop = true;
+ const text = edit.flatSurface(e, t) catch {
+ e.multi_stop.* = true;
break;
};
const co = modal.offsetAt(text, .{ .row = @intCast(@max(0, t.cur_row)), .col = @intCast(@max(0, t.cur_col)) });
@@ -478,19 +478,19 @@ pub fn replaySels(p: *Pardes, t: *Text, what: Replay) void {
co;
res[i] = .{ .cur = text.len - @min(co, text.len), .anc = text.len - @min(ao, text.len), .sticky = t.sticky_col };
}
- p.multi_first = true;
- p.multi_index = 0;
- p.multi_count = 1;
- if (p.multi_stop) {
- p.multi_stop = false;
+ e.multi_first.* = true;
+ e.multi_index.* = 0;
+ e.multi_count.* = 1;
+ if (e.multi_stop.*) {
+ e.multi_stop.* = false;
if (owner != null) {
- const pn = p.panes[id] orelse return;
+ const pn = e.p.panes[id] orelse return;
if (pn.serial != serial) return;
}
t.nsel = 0;
return;
}
- const text = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(p.view().active, "edit", err);
+ const text = edit.flatSurface(e, t) catch |err| return e.reportError(err);
var rs: [Text.max_selections]modal.Selection = undefined;
var st: [Text.max_selections]i32 = undefined;
for (res[0..n], 0..) |r, k| {
@@ -522,7 +522,7 @@ fn normalInput(key: Key) modal.Normal.Input {
return out;
}
-pub fn handleNormal(p: *Pardes, t: *Text, key: Key) void {
+pub fn handleNormal(e: Editor, t: *Text, key: Key) void {
// `"<reg>` is held by the parser until the command it names arrives
const named = t.normal.register;
const parsed = modal.Normal.parse(&t.normal, normalInput(key));
@@ -531,11 +531,11 @@ pub fn handleNormal(p: *Pardes, t: *Text, key: Key) void {
.action => |value| value,
};
t.normal.register = 0;
- p.view().register = named;
- defer p.view().register = 0;
+ e.register.* = named;
+ defer e.register.* = 0;
if (t.nsel == 0 or action.once())
- return executeNormalAction(p, t, action) catch |err| p.reportError(p.view().active, "edit", err);
- replaySels(p, t, .{ .normal = action });
+ return executeNormalAction(e.p, t, action) catch |err| e.reportError(err);
+ replaySels(e, t, .{ .normal = action });
}
fn startSelRegex(p: *Pardes, pane: *Pane, t: *Text, mode: modal.Normal.RegexMode) edit.Error!void {
@@ -549,18 +549,18 @@ fn startSelRegex(p: *Pardes, pane: *Pane, t: *Text, mode: modal.Normal.RegexMode
/// 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) 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;
- var re = SearchRegex.compile(p.scratch.allocator(), pat) catch {
- p.setMessage(v.active, "invalid search pattern");
+fn searchStep(e: Editor, t: *Text, text: []const u8, forward: bool, count: u32) edit.Error!void {
+ const v = e.p.view();
+ const name = if (e.register.* != 0) e.register.* else e.registers.last_search;
+ const pat = e.registers.value(name, 0) orelse return;
+ var re = SearchRegex.compile(e.scratch, pat) catch {
+ e.p.setMessage(v.active, "invalid search pattern");
return;
};
var hay = text;
if (for (pat) |c| {
if (std.ascii.isUpper(c)) break false;
- } else true) hay = try std.ascii.allocLowerString(p.scratch.allocator(), text);
+ } else true) hay = try std.ascii.allocLowerString(e.scratch, text);
for (0..count) |_| {
var ranges: [Text.max_selections]modal.Selection = undefined;
const got = t.ranges(text, 0, &ranges);
@@ -577,7 +577,7 @@ fn searchStep(p: *Pardes, t: *Text, text: []const u8, forward: bool, count: u32)
wrapped = true;
break :blk lastMatch(&re, hay, from, hay.len);
}) orelse {
- p.setMessage(v.active, "no more matches");
+ e.p.setMessage(v.active, "no more matches");
return;
};
if (found.end == 0) return;
@@ -592,16 +592,16 @@ fn searchStep(p: *Pardes, t: *Text, text: []const u8, forward: bool, count: u32)
ranges[got.pri] = next;
t.setRanges(text, ranges[0..got.n], &.{}, got.pri, true);
}
- if (wrapped) p.setMessage(v.active, "wrapped around document");
+ if (wrapped) e.p.setMessage(v.active, "wrapped around document");
}
t.show();
}
-pub fn searchSelection(p: *Pardes, t: *Text, text: []const u8, boundary: bool) edit.Error!void {
+pub fn searchSelection(e: Editor, 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;
- const arena = p.scratch.allocator();
+ const arena = e.scratch;
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));
@@ -610,9 +610,9 @@ pub fn searchSelection(p: *Pardes, t: *Text, text: []const u8, boundary: bool) e
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 '/';
- try p.registers.put(p.gpa, name, pattern.items, 0, 1, true);
- p.registers.last_search = name;
+ const name = if (e.register.* != 0) e.register.* else '/';
+ try e.registers.put(e.gpa, name, pattern.items, 0, 1, true);
+ e.registers.last_search = name;
}
fn multiSelAction(t: *Text, text: []const u8, role: modal.Normal.Role, cnt: usize) void {
@@ -945,20 +945,21 @@ fn shrinkSelToLineBounds(t: *Text, text: []const u8, range: modal.Selection) voi
t.setRange(text, 0, r, expl);
}
-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.
- const pane = t.pane() orelse p.panes[v.active] orelse return;
- const raw_selection = t.what == .body and pane.rawPointerText(pane.sel[sel_slot]) != null;
- const before = .{ t.cur_row, t.cur_col, t.vsel, t.select, t.nsel, t.mode };
- defer if (raw_selection and !std.meta.eql(before, .{ t.cur_row, t.cur_col, t.vsel, t.select, t.nsel, t.mode })) {
- pane.clearPointerSelection(sel_slot);
- pane.sel[sel_slot].state = .none;
+/// Whether a normal-mode role is the text's alone (`textAction`), or
+/// reaches past it: a view, a builtin, a prompt, a language server, the
+/// look ring, the jump labels, a macro.
+pub fn textRole(role: modal.Normal.Role) bool {
+ return switch (role) {
+ .all_children, .all_siblings, .append_output, .command_line, .expand_node, .format, .goto_callstack, .goto_class, .goto_comment, .goto_declaration, .goto_definition, .goto_diagnostic, .goto_diagnostic_end, .goto_entry, .goto_function, .goto_implementation, .goto_parameter, .goto_references, .goto_test, .goto_type_definition, .goto_view_bottom, .goto_view_top, .goto_word, .goto_xml_element, .half_page_down, .half_page_up, .insert_output, .keep_pipe, .keep_regex, .leader, .macro_record, .macro_replay, .next_sibling, .page_down, .page_up, .parent_node_end, .parent_node_start, .pipe_selection, .pipe_selection_to, .prev_sibling, .remove_regex, .repeat_insert, .search, .search_next, .search_prev, .select_regex, .shrink_node, .split_regex, .view_bottom, .view_center, .view_middle, .view_scroll_down, .view_scroll_up, .view_top => false,
+ else => true,
};
- if (raw_selection and a.role == .yank) return edit.normalYank(p.editor(), t);
- edit.pinPaneCursor(p.editor(), t);
- const text = try edit.flatSurface(p.editor(), t);
+}
+
+/// A normal-mode action of the text's own (`textRole`): a motion, a
+/// selection, an edit, an undo, a register.
+pub fn textAction(e: Editor, t: *Text, a: modal.Normal.Action) edit.Error!void {
+ edit.pinPaneCursor(e, t);
+ const text = try edit.flatSurface(e, t);
const range = t.primaryRange(text, 0);
const cur = modal.selectionCursor(text, range);
@@ -966,9 +967,9 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!
.escape => {
t.select = false;
// typed again by a macro, Esc is only helix's
- if (p.macro.replaying > 0) return;
- tagline.exitHeader(p); // out of a header too, as Shift-Esc
- return exec.runBuiltin(p, .Last, v.active, "", null);
+ if (e.p.macro.replaying > 0) return;
+ tagline.exitHeader(e.p); // out of a header too, as Shift-Esc
+ return exec.runBuiltin(e.p, .Last, e.p.view().active, "", null);
},
// gg and G: a count names the line; bare G is no move (helix)
.goto_file_start, .goto_line => {
@@ -991,43 +992,17 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!
if (nw == slice.len) return;
return pointMove(t, text, range, ls + nw);
},
- .goto_line_down => return verticalMove(t, text, range, true, a.count, edit.paneWrapWidth(p.editor(), t)),
- .goto_line_up => return verticalMove(t, text, range, false, a.count, edit.paneWrapWidth(p.editor(), t)),
+ .goto_line_down => return verticalMove(t, text, range, true, a.count, edit.paneWrapWidth(e, t)),
+ .goto_line_up => return verticalMove(t, text, range, false, a.count, edit.paneWrapWidth(e, t)),
.goto_column => {
const line = modal.lineAtOffset(text, cur);
const ls = modal.lineStartOffset(text, line);
return pointMove(t, text, range, ls + modal.graphemeAtColumn(text[ls..modal.lineEndOffset(text, line)], @as(usize, a.count) - 1));
},
- .goto_view_top => return gotoWindow(t, text, range, .top, a.count),
- .goto_view_bottom => return gotoWindow(t, text, range, .bottom, a.count),
.goto_last_modification => {
const at = t.last_edit orelse return;
return pointMove(t, text, range, modal.offsetAt(text, .{ .row = @intCast(@max(0, at.row)), .col = @intCast(@max(0, at.col)) }));
},
- // helix align_view: the cursor's row goes `above` rows down the
- // view, the middle of an odd one and the upper middle of an even one
- .view_top, .view_center, .view_bottom => if (t.what == .body) {
- const above: i32 = switch (a.role) {
- .view_top => 0,
- .view_center => @divTrunc(@as(i32, pane.rows) - 1, 2),
- else => @as(i32, pane.rows) - 1,
- };
- pane.scrollBy(t.cur_row - (pane.scroll() + above));
- pane.ensureCursorVisible();
- },
- // helix align_view_middle: the cursor's column to the middle of an
- // unwrapped view
- .view_middle => if (t.what == .body) {
- panes.File.geometry(pane);
- if (pane.body_wrapped) return;
- const line = modal.lineAtOffset(text, cur);
- const slice = text[modal.lineStartOffset(text, line)..modal.lineEndOffset(text, line)];
- const w: usize = @intCast(@max(1, @as(i32, pane.cols) - @as(i32, panes.File.gutterWidth(pane))));
- const col = panes.File.rawDisplayCol(slice, @intCast(@max(0, t.cur_col)));
- pane.hscroll = @intCast(panes.File.rawAtDisplay(slice, col -| w / 2));
- },
- .view_scroll_down => if (t.what == .body) return t.scrollViewMove(text, range, 1),
- .view_scroll_up => if (t.what == .body) return t.scrollViewMove(text, range, -1),
.find_char_fwd, .find_char_back, .till_char_fwd, .till_char_back => {
t.find_op = switch (a.role) {
.find_char_fwd => .forward,
@@ -1038,37 +1013,19 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!
t.find_ch = a.char;
return findMove(t, text, range, a.char, t.find_op.?, a.count);
},
- .replace_prefix => return edit.normalReplaceChar(p.editor(), t, a.char),
+ .replace_prefix => return edit.normalReplaceChar(e, t, a.char),
// `mm`
.match_prefix => {
- const lines = try edit.paneCursorLines(p.editor(), t);
+ const lines = try edit.paneCursorLines(e, t);
const mc = modal.matchBracket(lines, modal.positionAt(text, cur)) orelse return;
return pointMove(t, text, range, modal.offsetAt(text, mc));
},
- .match_inside, .match_around => return edit.textobjectSelect(p.editor(), t, text, a.char, a.role == .match_around),
- .surround_add => return edit.surroundAdd(p.editor(), t, a.char),
- .surround_delete => return edit.surroundDelete(p.editor(), t, a.char),
- .surround_replace => return edit.surroundReplace(p.editor(), t, a.from, a.char),
+ .match_inside, .match_around => return edit.textobjectSelect(e, t, text, a.char, a.role == .match_around),
+ .surround_add => return edit.surroundAdd(e, t, a.char),
+ .surround_delete => return edit.surroundDelete(e, t, a.char),
+ .surround_replace => return edit.surroundReplace(e, t, a.from, a.char),
.goto_paragraph => return t.setRange(text, 0, modal.moveParagraph(text, range, a.count, a.forward, t.select), false),
- .add_newline => return edit.addNewline(p.editor(), t, a.forward, a.count),
- .goto_diagnostic, .goto_diagnostic_end => {
- // Once a Diagnostics query fed them, the diagnostics are this
- // text's places: ]d and [d step them as n and N do, ]D and [D
- // go to the last and the first. Until then, asked for.
- const set = &pane.places;
- const fed = set.started and set.provenance == .list and if (p.paneBySerial(set.from)) |qid|
- (if (p.panes[qid].?.query) |q| q.source == .diagnostics else false)
- else
- false;
- if (!fed) return p.lspRequest(v.active, .diagnostics, "");
- if (a.role == .goto_diagnostic) return set.step(p, pane, a.forward, 1, false);
- if (set.hits.items.len == 0) return;
- set.goTo(pane, if (a.forward) set.hits.items.len - 1 else 0, false);
- },
- inline .goto_function, .goto_class, .goto_parameter, .goto_comment, .goto_test, .goto_entry, .goto_xml_element => |role| {
- const object = @field(modal.Normal.Object, @tagName(role)["goto_".len..]);
- return objectJump(p, pane, t, text, range, object, a.forward, a.count);
- },
+ .add_newline => return edit.addNewline(e, t, a.forward, a.count),
.move_left, .move_right => {
var target = cur;
for (0..a.count) |_| target = if (a.role == .move_left) modal.prevGrapheme(text, target) else modal.nextGrapheme(text, target);
@@ -1093,11 +1050,6 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!
if (!moved) return;
return t.setRange(text, 0, repeated, false);
},
- // These four move exactly one viewport unit, whatever the count.
- .half_page_down => return halfPageMove(t, text, range, true),
- .half_page_up => return halfPageMove(t, text, range, false),
- .page_down => return t.scrollViewMove(text, range, @as(i32, pane.rows)),
- .page_up => return t.scrollViewMove(text, range, -@as(i32, pane.rows)),
.insert, .append, .insert_line_start, .insert_line_end, .open_below, .open_above => {
const where: edit.InsertAt = switch (a.role) {
.insert => .at,
@@ -1107,7 +1059,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!
.open_below => .open_below,
else => .open_above,
};
- return edit.enterInsert(p.editor(), t, where, a.count);
+ return edit.enterInsert(e, t, where, a.count);
},
.select_mode => {
if (t.select) {
@@ -1133,30 +1085,118 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!
.select_all => return t.setRange(text, 0, .{ .anchor = 0, .head = text.len }, false),
.keep_primary_sel => return t.dropSels(),
.copy_sel_below, .copy_sel_above, .remove_primary_sel, .rotate_sel_fwd, .rotate_sel_back, .split_sel_newline, .merge_sels, .merge_consecutive_sels, .trim_sels, .ensure_forward => return multiSelAction(t, text, a.role, a.count),
+ .delete => return edit.normalDelete(e, t, true),
+ .delete_noyank => return edit.normalDelete(e, t, false),
+ .change => return edit.normalChange(e, t),
+ .yank => return edit.normalYank(e, t),
+ .replace_with_yank => return edit.pasteRegister(e, t, .replace, a.count),
+ .paste_after => return edit.pasteRegister(e, t, .after, a.count),
+ .paste_before => return edit.pasteRegister(e, t, .before, a.count),
+ .switch_case => return edit.normalCase(e, t, .toggle),
+ .to_lowercase => return edit.normalCase(e, t, .lower),
+ .to_uppercase => return edit.normalCase(e, t, .upper),
+ .join_lines => return edit.normalJoin(e, t, false),
+ .join_select_space => return edit.normalJoin(e, t, true),
+ .rotate_contents_fwd => return edit.normalRotateContents(e, t, true, a.count),
+ .rotate_contents_back => return edit.normalRotateContents(e, t, false, a.count),
+ .align_selections => return edit.normalAlign(e, t),
+ .indent => return edit.normalIndent(e, t, a.count, true),
+ .unindent => return edit.normalIndent(e, t, a.count, false),
+ .comment_toggle => return edit.normalToggleComment(e, t),
+ .undo => return edit.undoStep(e, t, true),
+ .redo => return edit.undoStep(e, t, false),
+ .increment => return edit.normalAdjustNumber(e, t, @intCast(a.count)),
+ .decrement => return edit.normalAdjustNumber(e, t, -@as(i64, @intCast(a.count))),
+ .search_selection, .search_selection_word => {
+ try searchSelection(e, 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.
+ const pane = t.pane() orelse return;
+ if (t.what == .body and pane.file != null and pane.query == null) {
+ const name = if (e.register.* != 0) e.register.* else '/';
+ const pat = e.registers.value(name, 0) orelse return;
+ panes.Query.open(e.p, e.p.view().active, .text, pat, false);
+ pane.places.provenance = .search;
+ }
+ },
+ // prefix keys: what they begin is the command
+ .goto_prefix, .view_prefix, .next_prefix, .prev_prefix, .register_prefix, .view_prefix_sticky => {},
+ else => unreachable, // the core's (textRole)
+ }
+}
+
+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.
+ const pane = t.pane() orelse p.panes[v.active] orelse return;
+ const raw_selection = t.what == .body and pane.rawPointerText(pane.sel[sel_slot]) != null;
+ const before = .{ t.cur_row, t.cur_col, t.vsel, t.select, t.nsel, t.mode };
+ defer if (raw_selection and !std.meta.eql(before, .{ t.cur_row, t.cur_col, t.vsel, t.select, t.nsel, t.mode })) {
+ pane.clearPointerSelection(sel_slot);
+ pane.sel[sel_slot].state = .none;
+ };
+ if (raw_selection and a.role == .yank) return edit.normalYank(p.editor(), t);
+ if (textRole(a.role)) return textAction(p.editor(), t, a);
+ edit.pinPaneCursor(p.editor(), t);
+ const text = try edit.flatSurface(p.editor(), t);
+ const range = t.primaryRange(text, 0);
+ const cur = modal.selectionCursor(text, range);
+
+ switch (a.role) {
+ .goto_view_top => return gotoWindow(t, text, range, .top, a.count),
+ .goto_view_bottom => return gotoWindow(t, text, range, .bottom, a.count),
+ // helix align_view: the cursor's row goes `above` rows down the
+ // view, the middle of an odd one and the upper middle of an even one
+ .view_top, .view_center, .view_bottom => if (t.what == .body) {
+ const above: i32 = switch (a.role) {
+ .view_top => 0,
+ .view_center => @divTrunc(@as(i32, pane.rows) - 1, 2),
+ else => @as(i32, pane.rows) - 1,
+ };
+ pane.scrollBy(t.cur_row - (pane.scroll() + above));
+ pane.ensureCursorVisible();
+ },
+ // helix align_view_middle: the cursor's column to the middle of an
+ // unwrapped view
+ .view_middle => if (t.what == .body) {
+ panes.File.geometry(pane);
+ if (pane.body_wrapped) return;
+ const line = modal.lineAtOffset(text, cur);
+ const slice = text[modal.lineStartOffset(text, line)..modal.lineEndOffset(text, line)];
+ const w: usize = @intCast(@max(1, @as(i32, pane.cols) - @as(i32, panes.File.gutterWidth(pane))));
+ const col = panes.File.rawDisplayCol(slice, @intCast(@max(0, t.cur_col)));
+ pane.hscroll = @intCast(panes.File.rawAtDisplay(slice, col -| w / 2));
+ },
+ .view_scroll_down => if (t.what == .body) return t.scrollViewMove(text, range, 1),
+ .view_scroll_up => if (t.what == .body) return t.scrollViewMove(text, range, -1),
+ .goto_diagnostic, .goto_diagnostic_end => {
+ // Once a Diagnostics query fed them, the diagnostics are this
+ // text's places: ]d and [d step them as n and N do, ]D and [D
+ // go to the last and the first. Until then, asked for.
+ const set = &pane.places;
+ const fed = set.started and set.provenance == .list and if (p.paneBySerial(set.from)) |qid|
+ (if (p.panes[qid].?.query) |q| q.source == .diagnostics else false)
+ else
+ false;
+ if (!fed) return p.lspRequest(v.active, .diagnostics, "");
+ if (a.role == .goto_diagnostic) return set.step(p, pane, a.forward, 1, false);
+ if (set.hits.items.len == 0) return;
+ set.goTo(pane, if (a.forward) set.hits.items.len - 1 else 0, false);
+ },
+ inline .goto_function, .goto_class, .goto_parameter, .goto_comment, .goto_test, .goto_entry, .goto_xml_element => |role| {
+ const object = @field(modal.Normal.Object, @tagName(role)["goto_".len..]);
+ return objectJump(p, pane, t, text, range, object, a.forward, a.count);
+ },
+ // These four move exactly one viewport unit, whatever the count.
+ .half_page_down => return halfPageMove(t, text, range, true),
+ .half_page_up => return halfPageMove(t, text, range, false),
+ .page_down => return t.scrollViewMove(text, range, @as(i32, pane.rows)),
+ .page_up => return t.scrollViewMove(text, range, -@as(i32, pane.rows)),
// s, S and | answer for the text they are pressed in; `/` searches
// the body wherever it is pressed, as acme's Look from a tag does.
inline .select_regex, .split_regex, .keep_regex, .remove_regex => |role| return startSelRegex(p, pane, t, @field(modal.Normal.RegexMode, @tagName(role)[0 .. @tagName(role).len - "_regex".len])),
inline .expand_node, .shrink_node, .next_sibling, .prev_sibling, .all_siblings, .all_children, .parent_node_end, .parent_node_start => |role| return nodeSelect(p, pane, t, @field(modal.Normal.NodeWalk, @tagName(role))),
- .delete => return edit.normalDelete(p.editor(), t, true),
- .delete_noyank => return edit.normalDelete(p.editor(), t, false),
- .change => return edit.normalChange(p.editor(), t),
- .yank => return edit.normalYank(p.editor(), t),
- .replace_with_yank => return edit.pasteRegister(p.editor(), t, .replace, a.count),
- .paste_after => return edit.pasteRegister(p.editor(), t, .after, a.count),
- .paste_before => return edit.pasteRegister(p.editor(), t, .before, a.count),
- .switch_case => return edit.normalCase(p.editor(), t, .toggle),
- .to_lowercase => return edit.normalCase(p.editor(), t, .lower),
- .to_uppercase => return edit.normalCase(p.editor(), t, .upper),
- .join_lines => return edit.normalJoin(p.editor(), t, false),
- .join_select_space => return edit.normalJoin(p.editor(), t, true),
- .rotate_contents_fwd => return edit.normalRotateContents(p.editor(), t, true, a.count),
- .rotate_contents_back => return edit.normalRotateContents(p.editor(), t, false, a.count),
- .align_selections => return edit.normalAlign(p.editor(), t),
- .indent => return edit.normalIndent(p.editor(), t, a.count, true),
- .unindent => return edit.normalIndent(p.editor(), t, a.count, false),
- .comment_toggle => return edit.normalToggleComment(p.editor(), t),
- .undo => return edit.undoStep(p.editor(), t, true),
- .redo => return edit.undoStep(p.editor(), t, false),
// Language shortcuts run the same builtins as tags and leader commands.
.goto_definition => return exec.runBuiltin(p, .Def, v.active, "", null),
.goto_references => return exec.runBuiltin(p, .Refs, v.active, "", null),
@@ -1165,8 +1205,6 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!
.goto_type_definition => return p.lspRequest(v.active, .type_definition, ""),
.goto_implementation => return p.lspRequest(v.active, .implementation, ""),
.format => return p.lspRequest(v.active, .format, ""),
- .increment => return edit.normalAdjustNumber(p.editor(), t, @intCast(a.count)),
- .decrement => return edit.normalAdjustNumber(p.editor(), t, -@as(i64, @intCast(a.count))),
.leader => if (p.macro.replaying == 0) {
v.prefix = .leader;
v.leader_n = 0;
@@ -1197,18 +1235,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!
else if (t.what == .body and pane.file != null and pane.places.answersN(p, pane))
return pane.places.step(p, pane, a.role == .search_next, a.count, t.select)
else
- return searchStep(p, t, text, a.role == .search_next, a.count),
- .search_selection, .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) {
- const name = if (v.register != 0) v.register else '/';
- const pat = p.registers.value(name, 0) orelse return;
- panes.Query.open(p, v.active, .text, pat, false);
- pane.places.provenance = .search;
- }
- },
+ return searchStep(p.editor(), t, text, a.role == .search_next, a.count),
.goto_word => return startJump(p, pane, t),
.macro_record => {
const m = &p.macro;
@@ -1247,8 +1274,7 @@ fn executeNormalAction(p: *Pardes, t: *Text, a: modal.Normal.Action) edit.Error!
for (0..a.count) |_| keyboard.replayKeys(p, keys[typed .. keys.len - 1]);
keyboard.replayKeys(p, keys[keys.len - 1 ..]);
},
- // prefix keys: what they begin is the command
- .goto_prefix, .view_prefix, .next_prefix, .prev_prefix, .register_prefix, .view_prefix_sticky => {},
+ else => unreachable, // the text's (textAction)
}
}
@@ -1333,12 +1359,12 @@ test "flat text movement and selection replay need no scratch rows" {
p.scratch = .init(denied.allocator());
pane.body.cur_row = 4094;
- handleNormal(p, &pane.body, .{ .cp = 'l' });
+ handleNormal(p.editor(), &pane.body, .{ .cp = 'l' });
try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col);
- handleNormal(p, &pane.body, .{ .cp = 'j' });
+ handleNormal(p.editor(), &pane.body, .{ .cp = 'j' });
try std.testing.expectEqual(@as(i32, 4095), pane.body.cur_row);
try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col);
- handleNormal(p, &pane.body, .{ .cp = 'h' });
+ handleNormal(p.editor(), &pane.body, .{ .cp = 'h' });
try std.testing.expectEqual(@as(i32, 0), pane.body.cur_col);
try executeNormalAction(p, &pane.body, .{ .role = .match_inside, .char = 'w' });
try std.testing.expectEqual(@as(i32, 2), pane.body.cur_col);
@@ -1350,8 +1376,8 @@ test "flat text movement and selection replay need no scratch rows" {
pane.body.nsel = 1;
_ = pane.body.reserveSels();
pane.body.sels[0] = .{ .row = 3072, .col = 0, .arow = 3072, .acol = 0 };
- handleNormal(p, &pane.body, .{ .cp = 'l' });
- handleNormal(p, &pane.body, .{ .cp = 'j' });
+ handleNormal(p.editor(), &pane.body, .{ .cp = 'l' });
+ handleNormal(p.editor(), &pane.body, .{ .cp = 'j' });
try std.testing.expectEqual(@as(u16, 1), pane.body.nsel);
try std.testing.expectEqual(@as(i32, 1025), pane.body.cur_row);
try std.testing.expectEqual(@as(i32, 1), pane.body.cur_col);
@@ -1454,19 +1480,19 @@ test "one cursor holds no room for other ranges; a second range makes it and one
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 12 });
defer p.deinit();
const pane = try p.setTestFile("alpha beta\nalpha beta\n");
- for ("wxe%;gg") |cp| handleNormal(p, &pane.body, .{ .cp = cp });
+ for ("wxe%;gg") |cp| handleNormal(p.editor(), &pane.body, .{ .cp = cp });
try std.testing.expectEqual(@as(usize, 0), pane.body.sels.len);
- handleNormal(p, &pane.body, .{ .cp = 'C' });
+ handleNormal(p.editor(), &pane.body, .{ .cp = 'C' });
try std.testing.expectEqual(@as(u16, 1), pane.body.nsel);
try std.testing.expect(pane.body.sels.len > 0);
- handleNormal(p, &pane.body, .{ .cp = 'l' }); // replayed at both, still two
+ handleNormal(p.editor(), &pane.body, .{ .cp = 'l' }); // replayed at both, still two
try std.testing.expectEqual(@as(u16, 1), pane.body.nsel);
- handleNormal(p, &pane.body, .{ .cp = ',' });
+ handleNormal(p.editor(), &pane.body, .{ .cp = ',' });
try std.testing.expectEqual(@as(usize, 0), pane.body.sels.len);
- handleNormal(p, &pane.body, .{ .cp = 'C' });
- handleNormal(p, &pane.body, .{ .cp = ';' });
- handleNormal(p, &pane.body, .{ .cp = 'x' }); // x at both: still two lines
- handleNormal(p, &pane.body, .{ .cp = '_', .alt = true }); // merged to one
+ handleNormal(p.editor(), &pane.body, .{ .cp = 'C' });
+ handleNormal(p.editor(), &pane.body, .{ .cp = ';' });
+ handleNormal(p.editor(), &pane.body, .{ .cp = 'x' }); // x at both: still two lines
+ handleNormal(p.editor(), &pane.body, .{ .cp = '_', .alt = true }); // merged to one
try std.testing.expectEqual(@as(u16, 0), pane.body.nsel);
try std.testing.expectEqual(@as(usize, 0), pane.body.sels.len);
}
@@ -1477,9 +1503,9 @@ test "cursors that land on one place are one: a key typed there is typed once, a
defer p.deinit();
const pane = try p.setTestFile("ab\ncd\n");
// A cursor below, then both up: the second lands on the first.
- handleNormal(p, &pane.body, .{ .cp = 'C' });
+ handleNormal(p.editor(), &pane.body, .{ .cp = 'C' });
try std.testing.expectEqual(@as(u16, 1), pane.body.nsel);
- handleNormal(p, &pane.body, .{ .cp = 'k' });
+ handleNormal(p.editor(), &pane.body, .{ .cp = 'k' });
try std.testing.expectEqual(@as(u16, 0), pane.body.nsel);
p.update(.{ .key = .{ .cp = 'i', .text = "i" } });
p.update(.{ .key = .{ .cp = 'x', .text = "x" } });
@@ -1492,10 +1518,10 @@ test "r and Delete take whole graphemes" {
defer p.deinit();
const pane = try p.setTestFile("A" ++ "e\u{301}" ++ "👩🏽\u{200d}🚀" ++ "🇧🇷" ++ "界" ++ "Z\n");
// é, the astronaut and the flag: three graphemes, three 界
- for ("lv2lr") |c| handleNormal(p, &pane.body, .{ .cp = c });
- handleNormal(p, &pane.body, .{ .cp = '界' });
+ for ("lv2lr") |c| handleNormal(p.editor(), &pane.body, .{ .cp = c });
+ handleNormal(p.editor(), &pane.body, .{ .cp = '界' });
try std.testing.expectEqualStrings("A界界界界Z\n", pane.file.?.content);
- for ("u0li") |c| handleNormal(p, &pane.body, .{ .cp = c });
+ for ("u0li") |c| handleNormal(p.editor(), &pane.body, .{ .cp = c });
p.update(.{ .key = .{ .cp = Key.delete } });
try std.testing.expectEqualStrings("A" ++ "👩🏽\u{200d}🚀" ++ "🇧🇷" ++ "界" ++ "Z\n", pane.file.?.content);
}