summaryrefslogtreecommitdiff
path: root/src/modal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/modal.zig')
-rw-r--r--src/modal.zig540
1 files changed, 153 insertions, 387 deletions
diff --git a/src/modal.zig b/src/modal.zig
index a879b1dd..16a7ee8b 100644
--- a/src/modal.zig
+++ b/src/modal.zig
@@ -2,6 +2,10 @@ const std = @import("std");
const uucode = @import("uucode");
pub const Normal = struct {
+ /// Every normal-mode command, by the name config.zig binds it under.
+ /// Grouped by the context a key is read in (after `g`, `z`, `m`, `]` or
+ /// `[`, or none), and within each group in the order a key is tried:
+ /// the first role a key has is the command.
pub const Role = enum {
escape,
@@ -161,6 +165,18 @@ pub const Normal = struct {
search_selection_word,
};
+ /// The roles from `from` to `to`, in the order a context tries them.
+ fn roles(comptime from: Role, comptime to: Role) [@intFromEnum(to) - @intFromEnum(from) + 1]Role {
+ var out: [@intFromEnum(to) - @intFromEnum(from) + 1]Role = undefined;
+ for (&out, @intFromEnum(from)..) |*role, i| role.* = @enumFromInt(i);
+ return out;
+ }
+ const goto_roles: []const Role = &roles(.goto_file_start, .goto_callstack);
+ const view_roles: []const Role = &(roles(.view_top, .view_scroll_up) ++ roles(.half_page_down, .page_up));
+ const match_roles: []const Role = &roles(.match_inside, .surround_delete);
+ const bracket_roles: []const Role = &roles(.goto_paragraph, .goto_xml_element);
+ const command_roles: []const Role = &roles(.move_left, .search_selection_word);
+
pub const Input = struct {
roles: std.EnumSet(Role) = .empty,
cp: u21,
@@ -177,34 +193,15 @@ pub const Normal = struct {
}
};
- pub const Prefix = enum(u8) {
- none,
- goto,
- view,
- match,
- find_fwd,
- find_back,
- till_fwd,
- till_back,
- replace,
- next,
- prev,
- register,
- };
-
- pub const MatchSub = enum(u8) {
- none,
- inside,
- around,
- surround_add,
- surround_replace,
- surround_delete,
- };
-
pub const State = struct {
count: u32 = 0,
- prefix: Prefix = .none,
- match_sub: MatchSub = .none,
+ /// The prefix key read (`g`, `z`, `m`, `f`, `]`, `"`...), its
+ /// continuation awaited.
+ prefix: ?Role = null,
+ /// After `m`: which of `mi`, `ma`, `ms`, `mr`, `md` waits for its
+ /// character.
+ match_sub: ?Role = null,
+ /// `mr`'s pair to replace, while its new one is awaited.
held_char: u21 = 0,
/// `"<reg>`: the register the next command names, 0 for none
register: u21 = 0,
@@ -213,7 +210,7 @@ pub const Normal = struct {
/// Drop what is pending; sticky view mode stays armed through it.
pub fn clear(state: *State) void {
- state.* = if (state.sticky) .{ .sticky = true, .prefix = .view } else .{};
+ state.* = if (state.sticky) .{ .sticky = true, .prefix = .view_prefix } else .{};
}
};
@@ -237,164 +234,83 @@ pub const Normal = struct {
}
};
- /// helix's tree-sitter selection keys: where a range goes in the tree
/// helix's tree-sitter textobjects, by what their queries call them
pub const Object = enum { function, class, parameter, comment, @"test", entry, xml_element };
- pub const NodeWalk = enum { expand, shrink, next_sibling, prev_sibling, all_siblings, all_children, parent_end, parent_start };
+ /// helix's tree-sitter selection keys: where a range goes in the tree
+ pub const NodeWalk = enum { expand_node, shrink_node, next_sibling, prev_sibling, all_siblings, all_children, parent_node_end, parent_node_start };
/// what the s/S/K/Alt-K prompt does with its pattern's matches
pub const RegexMode = enum { select, split, keep, remove };
- pub const Scope = enum { once, per_selection };
- pub const Direction = enum { backward, forward };
- pub const Motion = enum {
- left,
- right,
- down,
- up,
- next_word_start,
- prev_word_start,
- next_word_end,
- next_long_word_start,
- prev_long_word_start,
- next_long_word_end,
- };
- pub const Goto = enum {
- file_start,
- last_line,
- line_start,
- line_end,
- first_nonws,
- line_down,
- line_up,
- column,
- view_top,
- view_bottom,
- last_modification,
- };
- pub const View = enum { top, center, bottom, middle, scroll_down, scroll_up };
+ /// f/F/t/T: to the character (or till it), forward or back
pub const Find = enum { forward, backward, till_forward, till_backward };
- pub const Line = enum { start, end, first_nonws };
- pub const Page = enum { half_down, half_up, down, up };
- pub const Insert = enum { at, append, line_start, line_end, open_below, open_above };
- pub const Select = enum {
- mode,
- line,
- line_bounds,
- shrink_to_line_bounds,
- collapse,
- flip,
- all,
- };
- pub const Multi = enum {
- copy_below,
- copy_above,
- keep_primary,
- remove_primary,
- rotate_forward,
- rotate_backward,
- split_newline,
- merge,
- merge_consecutive,
- trim,
- ensure_forward,
- };
- pub const Edit = enum {
- delete,
- delete_noyank,
- change,
- yank,
- replace_with_yank,
- paste_after,
- paste_before,
- switch_case,
- lowercase,
- uppercase,
- join_lines,
- join_select_space,
- rotate_contents_forward,
- rotate_contents_backward,
- align_selections,
- indent,
- unindent,
- comment_toggle,
- undo,
- redo,
- };
- pub const Lsp = enum { definition, declaration, type_definition, implementation, references, callstack, format };
- pub const Counted = struct {
- count: u32,
- explicit: bool,
- };
+ /// What a key sequence asks for: the command and what it was given. The
+ /// text's executor (normal.zig) and a PDF's (pdf_view.zig) both take it.
+ pub const Action = struct {
+ role: Role,
+ count: u32 = 1,
+ /// The count was typed (`5G`, `3gg`), not the 1 a bare key means.
+ explicit: bool = false,
+ /// The literal the command takes: f/t's target, r's replacement, the
+ /// pair of `mi`, `ma`, `ms` and `md`, and `mr`'s new one.
+ char: u21 = 0,
+ /// `mr`'s pair to replace.
+ from: u21 = 0,
+ /// After `]` (true) or `[` (false).
+ forward: bool = true,
- pub const Action = union(enum) {
- escape,
- goto: struct { target: Goto, count: u32, explicit_count: bool },
- view: View,
- find: struct { kind: Find, char: u21, count: u32 },
- replace_char: u21,
- match_bracket,
- textobject: struct { char: u21, around: bool },
- surround_add: u21,
- surround_delete: u21,
- surround_replace: struct { from: u21, to: u21 },
- paragraph: struct { direction: Direction, count: u32 },
- add_newline: struct { direction: Direction, count: u32 },
- diagnostic: struct { direction: Direction, endpoint: bool },
- move: struct { motion: Motion, count: u32 },
- repeat_find: u32,
- line: Line,
- goto_line: Counted,
- page: struct { kind: Page, count: u32 },
- insert: struct { kind: Insert, count: u32 },
- select: struct { kind: Select, count: u32 },
- multi: struct { kind: Multi, count: u32 },
- select_regex: RegexMode,
- edit: struct { kind: Edit, count: u32 },
- lsp: Lsp,
- adjust_number: i64,
- leader,
- command_line,
- pipe_selection: PipeBehavior,
- search,
- search_step: struct { direction: Direction, count: u32 },
- search_selection: bool,
- goto_word,
- macro_record,
- macro_replay: u32,
- node: NodeWalk,
- object_jump: struct { object: Object, direction: Direction, count: u32 },
- repeat_insert: u32,
-
- pub fn scope(value: Action) Scope {
- return switch (value) {
+ /// Run once for the whole selection, not at each range.
+ pub fn once(a: Action) bool {
+ return switch (a.role) {
.escape,
- .multi,
+ .copy_sel_below,
+ .copy_sel_above,
+ .keep_primary_sel,
+ .remove_primary_sel,
+ .rotate_sel_fwd,
+ .rotate_sel_back,
+ .split_sel_newline,
+ .merge_sels,
+ .merge_consecutive_sels,
+ .trim_sels,
+ .ensure_forward,
.select_regex,
+ .split_regex,
+ .keep_regex,
+ .remove_regex,
+ .expand_node,
+ .shrink_node,
+ .next_sibling,
+ .prev_sibling,
+ .all_siblings,
+ .all_children,
+ .parent_node_end,
+ .parent_node_start,
+ .join_lines,
+ .join_select_space,
+ .rotate_contents_fwd,
+ .rotate_contents_back,
+ .align_selections,
+ .comment_toggle,
+ .undo,
+ .redo,
.leader,
.command_line,
.pipe_selection,
- .search,
- .search_step,
- .search_selection,
+ .pipe_selection_to,
+ .insert_output,
+ .append_output,
+ .keep_pipe,
.macro_record,
.macro_replay,
- .node,
- .goto_word,
.repeat_insert,
- => .once,
- .edit => |edit| switch (edit.kind) {
- .join_lines,
- .join_select_space,
- .rotate_contents_forward,
- .rotate_contents_backward,
- .align_selections,
- .comment_toggle,
- .undo,
- .redo,
- => .once,
- else => .per_selection,
- },
- else => .per_selection,
+ .search,
+ .search_next,
+ .search_prev,
+ .search_selection,
+ .search_selection_word,
+ .goto_word,
+ => true,
+ else => false,
};
}
};
@@ -406,18 +322,12 @@ pub const Normal = struct {
action: Action,
};
- fn resultAction(value: Action) Result {
- return .{ .action = value };
- }
-
- fn armPrefix(state: *State, prefix: Prefix, saved_count: u32) Result {
- state.prefix = prefix;
- state.count = saved_count;
- if (prefix == .match) {
- state.match_sub = .none;
- state.held_char = 0;
- }
- return .pending;
+ /// The first of `among` the key has, with the count it was given; `none`
+ /// when it has none of them.
+ fn first(key: Input, comptime among: []const Role, count: u32, explicit: bool, forward: bool, none: Result) Result {
+ inline for (among) |role| if (key.has(role))
+ return .{ .action = .{ .role = role, .count = count, .explicit = explicit, .forward = forward } };
+ return none;
}
pub fn parse(state: *State, key: Input) Result {
@@ -426,12 +336,12 @@ pub const Normal = struct {
const sticky = state.sticky;
state.* = .{};
if (sticky) return .ignored;
- return resultAction(.escape);
+ return .{ .action = .{ .role = .escape } };
}
// A digit is a count only before a command/prefix. A leading zero keeps
// its configured line-start role; after another digit it extends count.
- if (state.prefix == .none and !key.ctrl and !key.alt and
+ if (state.prefix == null and !key.ctrl and !key.alt and
key.cp >= '0' and key.cp <= '9' and
!(key.cp == '0' and state.count == 0))
{
@@ -440,228 +350,88 @@ pub const Normal = struct {
return .pending;
}
- const counted: Counted = .{ .count = @max(1, state.count), .explicit = state.count != 0 };
+ const explicit = state.count != 0;
+ const count = @max(1, state.count);
state.count = 0;
- const count = counted.count;
- switch (state.prefix) {
- .goto => {
- state.prefix = .none;
- // each target's role is its name after `goto_`
- inline for (comptime std.enums.values(Goto)) |target|
- if (key.has(@field(Role, "goto_" ++ @tagName(target)))) return resultAction(.{ .goto = .{ .target = target, .count = count, .explicit_count = counted.explicit } });
- if (key.has(.goto_word)) return resultAction(.goto_word);
- inline for (.{ .definition, .declaration, .type_definition, .implementation, .references, .callstack }) |lsp|
- if (key.has(@field(Role, "goto_" ++ @tagName(lsp)))) return resultAction(.{ .lsp = lsp });
- return .ignored;
- },
- .view => {
- if (!state.sticky) state.prefix = .none;
- if (key.has(.view_top)) return resultAction(.{ .view = .top });
- if (key.has(.view_middle)) return resultAction(.{ .view = .middle });
- if (key.has(.view_center)) return resultAction(.{ .view = .center });
- if (key.has(.view_bottom)) return resultAction(.{ .view = .bottom });
- if (key.has(.view_scroll_down)) return resultAction(.{ .view = .scroll_down });
- if (key.has(.view_scroll_up)) return resultAction(.{ .view = .scroll_up });
- if (key.has(.half_page_down)) return resultAction(.{ .page = .{ .kind = .half_down, .count = count } });
- if (key.has(.half_page_up)) return resultAction(.{ .page = .{ .kind = .half_up, .count = count } });
- if (key.has(.page_down)) return resultAction(.{ .page = .{ .kind = .down, .count = count } });
- if (key.has(.page_up)) return resultAction(.{ .page = .{ .kind = .up, .count = count } });
- return .ignored;
+ if (state.prefix) |prefix| switch (prefix) {
+ .goto_prefix => {
+ state.prefix = null;
+ return first(key, goto_roles, count, explicit, true, .ignored);
},
- .find_fwd, .find_back, .till_fwd, .till_back => |prefix| {
- state.prefix = .none;
- const char = key.literal() orelse return .ignored;
- const kind: Find = switch (prefix) {
- .find_fwd => .forward,
- .find_back => .backward,
- .till_fwd => .till_forward,
- .till_back => .till_backward,
- else => unreachable,
- };
- return resultAction(.{ .find = .{ .kind = kind, .char = char, .count = count } });
+ .view_prefix => {
+ if (!state.sticky) state.prefix = null;
+ return first(key, view_roles, count, explicit, true, .ignored);
},
- .replace => {
- state.prefix = .none;
+ .find_char_fwd, .find_char_back, .till_char_fwd, .till_char_back, .replace_prefix => {
+ state.prefix = null;
const char = key.literal() orelse return .ignored;
- return resultAction(.{ .replace_char = char });
+ return .{ .action = .{ .role = prefix, .count = count, .explicit = explicit, .char = char } };
},
- .match => {
- if (state.match_sub == .none) {
+ .match_prefix => {
+ const sub = state.match_sub orelse {
+ // `mm`: the bracket matching the one under the cursor
if (key.has(.match_prefix)) {
- state.prefix = .none;
- return resultAction(.match_bracket);
+ state.prefix = null;
+ return .{ .action = .{ .role = .match_prefix } };
}
- const sub: MatchSub = if (key.has(.match_inside))
- .inside
- else if (key.has(.match_around))
- .around
- else if (key.has(.surround_add))
- .surround_add
- else if (key.has(.surround_replace))
- .surround_replace
- else if (key.has(.surround_delete))
- .surround_delete
- else {
- state.prefix = .none;
- return .ignored;
+ inline for (match_roles) |role| if (key.has(role)) {
+ state.match_sub = role;
+ return .pending;
};
- state.match_sub = sub;
- return .pending;
- }
+ state.prefix = null;
+ return .ignored;
+ };
const char = key.literal() orelse {
state.clear();
return .ignored;
};
- if (state.match_sub == .surround_replace and state.held_char == 0) {
+ if (sub == .surround_replace and state.held_char == 0) {
state.held_char = char;
return .pending;
}
- const sub = state.match_sub;
const from = state.held_char;
state.clear();
- return switch (sub) {
- .inside => resultAction(.{ .textobject = .{ .char = char, .around = false } }),
- .around => resultAction(.{ .textobject = .{ .char = char, .around = true } }),
- .surround_add => resultAction(.{ .surround_add = char }),
- .surround_delete => resultAction(.{ .surround_delete = char }),
- .surround_replace => resultAction(.{ .surround_replace = .{ .from = from, .to = char } }),
- .none => unreachable,
- };
+ return .{ .action = .{ .role = sub, .char = char, .from = from } };
},
- .next, .prev => |prefix| {
- state.prefix = .none;
- const direction: Direction = if (prefix == .next) .forward else .backward;
- if (key.has(.goto_paragraph)) return resultAction(.{ .paragraph = .{ .direction = direction, .count = count } });
- if (key.has(.add_newline)) return resultAction(.{ .add_newline = .{ .direction = direction, .count = count } });
- if (key.has(.goto_diagnostic)) return resultAction(.{ .diagnostic = .{ .direction = direction, .endpoint = false } });
- if (key.has(.goto_diagnostic_end)) return resultAction(.{ .diagnostic = .{ .direction = direction, .endpoint = true } });
- inline for (.{ .{ .goto_function, .function }, .{ .goto_class, .class }, .{ .goto_parameter, .parameter }, .{ .goto_comment, .comment }, .{ .goto_test, .@"test" }, .{ .goto_entry, .entry }, .{ .goto_xml_element, .xml_element } }) |pair|
- if (key.has(pair[0])) return resultAction(.{ .object_jump = .{ .object = pair[1], .direction = direction, .count = count } });
- return .ignored;
+ .next_prefix, .prev_prefix => {
+ state.prefix = null;
+ return first(key, bracket_roles, count, explicit, prefix == .next_prefix, .ignored);
},
- .register => {
- state.prefix = .none;
+ .register_prefix => {
+ state.prefix = null;
const char = key.literal() orelse return .ignored;
state.register = char;
// the count typed before `"` is still the command's
- state.count = if (counted.explicit) count else 0;
+ state.count = if (explicit) count else 0;
return .pending;
},
- .none => {},
- }
+ else => unreachable,
+ };
// Prefix setters retain the count for their continuation.
- if (key.has(.goto_prefix)) return armPrefix(state, .goto, if (counted.explicit) count else 0);
- if (key.has(.view_prefix)) return armPrefix(state, .view, if (counted.explicit) count else 0);
+ inline for (.{ .goto_prefix, .view_prefix, .find_char_fwd, .find_char_back, .till_char_fwd, .till_char_back, .replace_prefix, .next_prefix, .prev_prefix, .register_prefix }) |prefix|
+ if (key.has(prefix)) {
+ state.prefix = prefix;
+ state.count = if (explicit) count else 0;
+ return .pending;
+ };
if (key.has(.view_prefix_sticky)) {
state.sticky = true;
- return armPrefix(state, .view, 0);
+ state.prefix = .view_prefix;
+ return .pending;
}
- if (key.has(.find_char_fwd)) return armPrefix(state, .find_fwd, if (counted.explicit) count else 0);
- if (key.has(.find_char_back)) return armPrefix(state, .find_back, if (counted.explicit) count else 0);
- if (key.has(.till_char_fwd)) return armPrefix(state, .till_fwd, if (counted.explicit) count else 0);
- if (key.has(.till_char_back)) return armPrefix(state, .till_back, if (counted.explicit) count else 0);
- if (key.has(.replace_prefix)) return armPrefix(state, .replace, if (counted.explicit) count else 0);
- if (key.has(.next_prefix)) return armPrefix(state, .next, if (counted.explicit) count else 0);
- if (key.has(.prev_prefix)) return armPrefix(state, .prev, if (counted.explicit) count else 0);
- if (key.has(.match_prefix)) return armPrefix(state, .match, 0);
- if (key.has(.register_prefix)) return armPrefix(state, .register, if (counted.explicit) count else 0);
-
- if (key.has(.move_left)) return resultAction(.{ .move = .{ .motion = .left, .count = count } });
- if (key.has(.move_right)) return resultAction(.{ .move = .{ .motion = .right, .count = count } });
- if (key.has(.move_down)) return resultAction(.{ .move = .{ .motion = .down, .count = count } });
- if (key.has(.move_up)) return resultAction(.{ .move = .{ .motion = .up, .count = count } });
- if (key.has(.next_word_start)) return resultAction(.{ .move = .{ .motion = .next_word_start, .count = count } });
- if (key.has(.prev_word_start)) return resultAction(.{ .move = .{ .motion = .prev_word_start, .count = count } });
- if (key.has(.next_word_end)) return resultAction(.{ .move = .{ .motion = .next_word_end, .count = count } });
- if (key.has(.next_long_word_start)) return resultAction(.{ .move = .{ .motion = .next_long_word_start, .count = count } });
- if (key.has(.prev_long_word_start)) return resultAction(.{ .move = .{ .motion = .prev_long_word_start, .count = count } });
- if (key.has(.next_long_word_end)) return resultAction(.{ .move = .{ .motion = .next_long_word_end, .count = count } });
- if (key.has(.repeat_find)) return resultAction(.{ .repeat_find = count });
- if (key.has(.line_start)) return resultAction(.{ .line = .start });
- if (key.has(.line_end)) return resultAction(.{ .line = .end });
- if (key.has(.line_first_nonws)) return resultAction(.{ .line = .first_nonws });
- if (key.has(.goto_line)) return resultAction(.{ .goto_line = counted });
- if (key.has(.half_page_down)) return resultAction(.{ .page = .{ .kind = .half_down, .count = count } });
- if (key.has(.half_page_up)) return resultAction(.{ .page = .{ .kind = .half_up, .count = count } });
- if (key.has(.page_down)) return resultAction(.{ .page = .{ .kind = .down, .count = count } });
- if (key.has(.page_up)) return resultAction(.{ .page = .{ .kind = .up, .count = count } });
-
- inline for (.{ .{ .insert, .at }, .{ .append, .append }, .{ .insert_line_start, .line_start }, .{ .insert_line_end, .line_end }, .{ .open_below, .open_below }, .{ .open_above, .open_above } }) |pair|
- if (key.has(pair[0])) return resultAction(.{ .insert = .{ .kind = pair[1], .count = count } });
-
- inline for (.{ .{ .select_mode, .mode }, .{ .select_line, .line }, .{ .select_line_bounds, .line_bounds }, .{ .shrink_to_line_bounds, .shrink_to_line_bounds }, .{ .collapse_selection, .collapse }, .{ .flip_selection, .flip }, .{ .select_all, .all } }) |pair|
- if (key.has(pair[0])) return resultAction(.{ .select = .{ .kind = pair[1], .count = count } });
-
- if (key.has(.copy_sel_below)) return resultAction(.{ .multi = .{ .kind = .copy_below, .count = count } });
- if (key.has(.copy_sel_above)) return resultAction(.{ .multi = .{ .kind = .copy_above, .count = count } });
- if (key.has(.keep_primary_sel)) return resultAction(.{ .multi = .{ .kind = .keep_primary, .count = count } });
- if (key.has(.remove_primary_sel)) return resultAction(.{ .multi = .{ .kind = .remove_primary, .count = count } });
- if (key.has(.rotate_sel_fwd)) return resultAction(.{ .multi = .{ .kind = .rotate_forward, .count = count } });
- if (key.has(.rotate_sel_back)) return resultAction(.{ .multi = .{ .kind = .rotate_backward, .count = count } });
- if (key.has(.split_sel_newline)) return resultAction(.{ .multi = .{ .kind = .split_newline, .count = count } });
- if (key.has(.merge_sels)) return resultAction(.{ .multi = .{ .kind = .merge, .count = count } });
- if (key.has(.merge_consecutive_sels)) return resultAction(.{ .multi = .{ .kind = .merge_consecutive, .count = count } });
- if (key.has(.trim_sels)) return resultAction(.{ .multi = .{ .kind = .trim, .count = count } });
- if (key.has(.select_regex)) return resultAction(.{ .select_regex = .select });
- if (key.has(.split_regex)) return resultAction(.{ .select_regex = .split });
- if (key.has(.keep_regex)) return resultAction(.{ .select_regex = .keep });
- if (key.has(.remove_regex)) return resultAction(.{ .select_regex = .remove });
- if (key.has(.ensure_forward)) return resultAction(.{ .multi = .{ .kind = .ensure_forward, .count = count } });
- if (key.has(.expand_node)) return resultAction(.{ .node = .expand });
- if (key.has(.shrink_node)) return resultAction(.{ .node = .shrink });
- if (key.has(.next_sibling)) return resultAction(.{ .node = .next_sibling });
- if (key.has(.prev_sibling)) return resultAction(.{ .node = .prev_sibling });
- if (key.has(.all_siblings)) return resultAction(.{ .node = .all_siblings });
- if (key.has(.all_children)) return resultAction(.{ .node = .all_children });
- if (key.has(.parent_node_end)) return resultAction(.{ .node = .parent_end });
- if (key.has(.parent_node_start)) return resultAction(.{ .node = .parent_start });
-
- if (key.has(.delete)) return resultAction(.{ .edit = .{ .kind = .delete, .count = count } });
- if (key.has(.delete_noyank)) return resultAction(.{ .edit = .{ .kind = .delete_noyank, .count = count } });
- if (key.has(.change)) return resultAction(.{ .edit = .{ .kind = .change, .count = count } });
- if (key.has(.yank)) return resultAction(.{ .edit = .{ .kind = .yank, .count = count } });
- if (key.has(.replace_with_yank)) return resultAction(.{ .edit = .{ .kind = .replace_with_yank, .count = count } });
- if (key.has(.paste_after)) return resultAction(.{ .edit = .{ .kind = .paste_after, .count = count } });
- if (key.has(.paste_before)) return resultAction(.{ .edit = .{ .kind = .paste_before, .count = count } });
- if (key.has(.switch_case)) return resultAction(.{ .edit = .{ .kind = .switch_case, .count = count } });
- if (key.has(.to_lowercase)) return resultAction(.{ .edit = .{ .kind = .lowercase, .count = count } });
- if (key.has(.to_uppercase)) return resultAction(.{ .edit = .{ .kind = .uppercase, .count = count } });
- if (key.has(.join_lines)) return resultAction(.{ .edit = .{ .kind = .join_lines, .count = count } });
- if (key.has(.join_select_space)) return resultAction(.{ .edit = .{ .kind = .join_select_space, .count = count } });
- if (key.has(.rotate_contents_fwd)) return resultAction(.{ .edit = .{ .kind = .rotate_contents_forward, .count = count } });
- if (key.has(.rotate_contents_back)) return resultAction(.{ .edit = .{ .kind = .rotate_contents_backward, .count = count } });
- if (key.has(.align_selections)) return resultAction(.{ .edit = .{ .kind = .align_selections, .count = count } });
- if (key.has(.indent)) return resultAction(.{ .edit = .{ .kind = .indent, .count = count } });
- if (key.has(.unindent)) return resultAction(.{ .edit = .{ .kind = .unindent, .count = count } });
- if (key.has(.format)) return resultAction(.{ .lsp = .format });
- if (key.has(.increment)) return resultAction(.{ .adjust_number = @intCast(count) });
- if (key.has(.decrement)) return resultAction(.{ .adjust_number = -@as(i64, @intCast(count)) });
- if (key.has(.comment_toggle)) return resultAction(.{ .edit = .{ .kind = .comment_toggle, .count = count } });
- if (key.has(.undo)) return resultAction(.{ .edit = .{ .kind = .undo, .count = count } });
- if (key.has(.redo)) return resultAction(.{ .edit = .{ .kind = .redo, .count = count } });
-
- if (key.has(.leader)) return resultAction(.leader);
- if (key.has(.command_line)) return resultAction(.command_line);
- if (key.has(.pipe_selection)) return resultAction(.{ .pipe_selection = .replace });
- if (key.has(.pipe_selection_to)) return resultAction(.{ .pipe_selection = .ignore });
- if (key.has(.insert_output)) return resultAction(.{ .pipe_selection = .insert });
- if (key.has(.append_output)) return resultAction(.{ .pipe_selection = .append });
- if (key.has(.keep_pipe)) return resultAction(.{ .pipe_selection = .keep });
- if (key.has(.macro_record)) return resultAction(.macro_record);
- if (key.has(.macro_replay)) return resultAction(.{ .macro_replay = count });
- if (key.has(.repeat_insert)) return resultAction(.{ .repeat_insert = count });
- if (key.has(.search)) return resultAction(.search);
- if (key.has(.search_selection)) return resultAction(.{ .search_selection = false });
- if (key.has(.search_selection_word)) return resultAction(.{ .search_selection = true });
- if (key.has(.search_next)) return resultAction(.{ .search_step = .{ .direction = .forward, .count = count } });
- if (key.has(.search_prev)) return resultAction(.{ .search_step = .{ .direction = .backward, .count = count } });
- return .unbound;
+ if (key.has(.match_prefix)) {
+ state.prefix = .match_prefix;
+ state.match_sub = null;
+ state.held_char = 0;
+ return .pending;
+ }
+ return first(key, command_roles, count, explicit, true, .unbound);
}
- fn input(cp: u21, roles: []const Role) Input {
- return .{ .cp = cp, .roles = .initMany(roles) };
+ fn input(cp: u21, among: []const Role) Input {
+ return .{ .cp = cp, .roles = .initMany(among) };
}
test "counts survive prefixes and identical parser actions can feed both adapters" {
@@ -679,11 +449,7 @@ pub const Normal = struct {
const ta = parse(&text, sequence[sequence.len - 1]);
const pa = parse(&pdf, sequence[sequence.len - 1]);
try std.testing.expectEqualDeep(ta, pa);
- try std.testing.expectEqualDeep(Result{ .action = .{ .goto = .{
- .target = .line_down,
- .count = 12,
- .explicit_count = true,
- } } }, ta);
+ try std.testing.expectEqualDeep(Result{ .action = .{ .role = .goto_line_down, .count = 12, .explicit = true } }, ta);
try std.testing.expectEqual(State{}, text);
try std.testing.expectEqual(State{}, pdf);
}
@@ -694,18 +460,18 @@ pub const Normal = struct {
try std.testing.expectEqual(Result.pending, parse(&state, input('g', &.{.goto_prefix})));
try std.testing.expectEqual(Result.ignored, parse(&state, input('?', &.{})));
try std.testing.expectEqual(State{}, state);
- try std.testing.expectEqualDeep(Result{ .action = .{ .move = .{ .motion = .down, .count = 1 } } }, parse(&state, input('j', &.{.move_down})));
+ try std.testing.expectEqualDeep(Result{ .action = .{ .role = .move_down } }, parse(&state, input('j', &.{.move_down})));
}
test "literal arguments retain conflicting command characters" {
var state: State = .{};
try std.testing.expectEqual(Result.pending, parse(&state, input('f', &.{.find_char_fwd})));
- try std.testing.expectEqualDeep(Result{ .action = .{ .find = .{ .kind = .forward, .char = 'p', .count = 1 } } }, parse(&state, input('p', &.{.paste_after})));
+ try std.testing.expectEqualDeep(Result{ .action = .{ .role = .find_char_fwd, .char = 'p' } }, parse(&state, input('p', &.{.paste_after})));
try std.testing.expectEqual(Result.pending, parse(&state, input('m', &.{.match_prefix})));
try std.testing.expectEqual(Result.pending, parse(&state, input('r', &.{.surround_replace})));
try std.testing.expectEqual(Result.pending, parse(&state, input('[', &.{.prev_prefix})));
- try std.testing.expectEqualDeep(Result{ .action = .{ .surround_replace = .{ .from = '[', .to = ']' } } }, parse(&state, input(']', &.{.next_prefix})));
+ try std.testing.expectEqualDeep(Result{ .action = .{ .role = .surround_replace, .from = '[', .char = ']' } }, parse(&state, input(']', &.{.next_prefix})));
try std.testing.expectEqual(State{}, state);
}
@@ -722,15 +488,15 @@ pub const Normal = struct {
test "replace accepts a Unicode literal" {
var state: State = .{};
try std.testing.expectEqual(Result.pending, parse(&state, input('r', &.{.replace_prefix})));
- try std.testing.expectEqualDeep(Result{ .action = .{ .replace_char = '界' } }, parse(&state, input('界', &.{})));
+ try std.testing.expectEqualDeep(Result{ .action = .{ .role = .replace_prefix, .char = '界' } }, parse(&state, input('界', &.{})));
try std.testing.expectEqual(State{}, state);
}
test "once versus per-selection is semantic action metadata" {
- try std.testing.expectEqual(Scope.once, (@as(Action, .search)).scope());
- try std.testing.expectEqual(Scope.once, (Action{ .edit = .{ .kind = .undo, .count = 1 } }).scope());
- try std.testing.expectEqual(Scope.per_selection, (Action{ .edit = .{ .kind = .delete, .count = 1 } }).scope());
- try std.testing.expectEqual(Scope.per_selection, (Action{ .move = .{ .motion = .down, .count = 3 } }).scope());
+ try std.testing.expect((Action{ .role = .search }).once());
+ try std.testing.expect((Action{ .role = .undo }).once());
+ try std.testing.expect(!(Action{ .role = .delete }).once());
+ try std.testing.expect(!(Action{ .role = .move_down, .count = 3 }).once());
}
};