summaryrefslogtreecommitdiff
path: root/src/modal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/modal.zig')
-rw-r--r--src/modal.zig74
1 files changed, 11 insertions, 63 deletions
diff --git a/src/modal.zig b/src/modal.zig
index ad06833f..7e10a877 100644
--- a/src/modal.zig
+++ b/src/modal.zig
@@ -454,25 +454,12 @@ pub const Normal = struct {
switch (state.prefix) {
.goto => {
state.prefix = .none;
- if (key.has(.goto_file_start)) return resultAction(.{ .goto = .{ .target = .file_start, .count = count, .explicit_count = counted.explicit } });
- if (key.has(.goto_last_line)) return resultAction(.{ .goto = .{ .target = .last_line, .count = count, .explicit_count = counted.explicit } });
- if (key.has(.goto_line_start)) return resultAction(.{ .goto = .{ .target = .line_start, .count = count, .explicit_count = counted.explicit } });
- if (key.has(.goto_line_end)) return resultAction(.{ .goto = .{ .target = .line_end, .count = count, .explicit_count = counted.explicit } });
- if (key.has(.goto_first_nonws)) return resultAction(.{ .goto = .{ .target = .first_nonws, .count = count, .explicit_count = counted.explicit } });
- if (key.has(.goto_line_down)) return resultAction(.{ .goto = .{ .target = .line_down, .count = count, .explicit_count = counted.explicit } });
- if (key.has(.goto_line_up)) return resultAction(.{ .goto = .{ .target = .line_up, .count = count, .explicit_count = counted.explicit } });
- if (key.has(.goto_column)) return resultAction(.{ .goto = .{ .target = .column, .count = count, .explicit_count = counted.explicit } });
- if (key.has(.goto_view_top)) return resultAction(.{ .goto = .{ .target = .view_top, .count = count, .explicit_count = counted.explicit } });
- if (key.has(.goto_view_center)) return resultAction(.{ .goto = .{ .target = .view_center, .count = count, .explicit_count = counted.explicit } });
- if (key.has(.goto_view_bottom)) return resultAction(.{ .goto = .{ .target = .view_bottom, .count = count, .explicit_count = counted.explicit } });
+ // 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);
- if (key.has(.goto_last_modification)) return resultAction(.{ .goto = .{ .target = .last_modification, .count = count, .explicit_count = counted.explicit } });
- if (key.has(.goto_definition)) return resultAction(.{ .lsp = .definition });
- if (key.has(.goto_declaration)) return resultAction(.{ .lsp = .declaration });
- if (key.has(.goto_type_definition)) return resultAction(.{ .lsp = .type_definition });
- if (key.has(.goto_implementation)) return resultAction(.{ .lsp = .implementation });
- if (key.has(.goto_references)) return resultAction(.{ .lsp = .references });
- if (key.has(.goto_callstack)) return resultAction(.{ .lsp = .callstack });
+ 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 => {
@@ -608,20 +595,11 @@ pub const Normal = struct {
if (key.has(.page_down)) return resultAction(.{ .page = .{ .kind = .down, .count = count } });
if (key.has(.page_up)) return resultAction(.{ .page = .{ .kind = .up, .count = count } });
- if (key.has(.insert)) return resultAction(.{ .insert = .{ .kind = .at, .count = count } });
- if (key.has(.append)) return resultAction(.{ .insert = .{ .kind = .append, .count = count } });
- if (key.has(.insert_line_start)) return resultAction(.{ .insert = .{ .kind = .line_start, .count = count } });
- if (key.has(.insert_line_end)) return resultAction(.{ .insert = .{ .kind = .line_end, .count = count } });
- if (key.has(.open_below)) return resultAction(.{ .insert = .{ .kind = .open_below, .count = count } });
- if (key.has(.open_above)) return resultAction(.{ .insert = .{ .kind = .open_above, .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 } });
- if (key.has(.select_mode)) return resultAction(.{ .select = .{ .kind = .mode, .count = count } });
- if (key.has(.select_line)) return resultAction(.{ .select = .{ .kind = .line, .count = count } });
- if (key.has(.select_line_bounds)) return resultAction(.{ .select = .{ .kind = .line_bounds, .count = count } });
- if (key.has(.shrink_to_line_bounds)) return resultAction(.{ .select = .{ .kind = .shrink_to_line_bounds, .count = count } });
- if (key.has(.collapse_selection)) return resultAction(.{ .select = .{ .kind = .collapse, .count = count } });
- if (key.has(.flip_selection)) return resultAction(.{ .select = .{ .kind = .flip, .count = count } });
- if (key.has(.select_all)) return resultAction(.{ .select = .{ .kind = .all, .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 } });
@@ -788,7 +766,7 @@ fn codepointAt(text: []const u8, off: usize) u21 {
return std.unicode.utf8Decode(text[off .. off + n]) catch 0xFFFD;
}
-fn isUnicodeWhitespace(cp: u21) bool {
+pub fn isUnicodeWhitespace(cp: u21) bool {
if (cp == ' ' or (cp >= '\t' and cp <= '\r') or cp == 0x85) return true;
return switch (uucode.get(.general_category, cp)) {
.separator_space, .separator_line, .separator_paragraph => true,
@@ -857,13 +835,6 @@ pub fn firstNonWs(line: []const u8) usize {
return i;
}
-pub fn firstNonWsOf(lines: []const []const u8, c: Cursor) Cursor {
- // the row can sit past the content (mouse click below a short pane's
- // last line) — out of range reads as an empty line, like lineLenOf
- if (c.row >= lines.len) return .{ .row = c.row, .col = 0 };
- return .{ .row = c.row, .col = firstNonWs(lines[c.row]) };
-}
-
pub fn charLeft(lines: []const []const u8, c: Cursor) Cursor {
if (c.row >= lines.len) return .{ .row = c.row, .col = 0 };
return .{ .row = c.row, .col = prevGrapheme(lines[c.row], @min(c.col, lines[c.row].len)) };
@@ -1717,18 +1688,6 @@ pub fn moveParagraph(text: []const u8, r: Selection, count: usize, fwd: bool, ex
return .{ .anchor = anchor, .head = head };
}
-/// j/k target: helix move_vertically — goal_col clamps to the line's content
-/// length, i.e. the cursor may land ON the '\n' of a shorter line.
-pub fn verticalTarget(text: []const u8, pos: usize, down: bool, count: usize, goal_col: usize) usize {
- const nlines = cursorLineCount(text);
- const line = lineAtOffset(text, pos);
- const nline = if (down) @min(line + @max(1, count), nlines - 1) else line -| @max(1, count);
- const s = lineStartOffset(text, nline);
- // lineEndOffset(text, nline) without its second walk to nline (see offsetAt)
- const e = std.mem.indexOfScalarPos(u8, text, s, '\n') orelse text.len;
- return s + graphemeStart(text[s..e], @min(goal_col, e - s));
-}
-
/// f/F/t/T target cell. helix find_char: the exclusive (till) search starts
/// one further out so repeats make progress; not-found = null (no move).
pub fn findTarget(text: []const u8, cursor: usize, ch: u21, fwd: bool, till: bool, count: usize) ?usize {
@@ -1976,14 +1935,6 @@ test "paragraph selections cross blank lines" {
try std.testing.expectEqual(@as(usize, 7), r.head);
}
-test "vertical target clamps the goal column onto the newline cell" {
- const t = "abcdef\nab\nabcdef\n";
- // from (0,5) down: line "ab" clamps to its '\n' at offset 9
- try std.testing.expectEqual(@as(usize, 9), verticalTarget(t, 5, true, 1, 5));
- // two down with the same goal restores col 5
- try std.testing.expectEqual(@as(usize, 15), verticalTarget(t, 9, true, 1, 5));
-}
-
test "find targets count matches and skip adjacent till targets" {
const t = "abcabc\n";
try std.testing.expectEqual(@as(usize, 2), findTarget(t, 0, 'c', true, false, 1).?);
@@ -2148,14 +2099,11 @@ test "codepoint classes distinguish words punctuation and whitespace" {
try std.testing.expectEqual(Kind.ws, kindOfCodepoint('\n'));
}
-test "insert cursor steps characters and skips indentation" {
+test "insert cursor steps characters" {
const lines = [_][]const u8{ "alpha beta", " two words", "x" };
const c = Cursor{ .row = 0, .col = 5 };
try std.testing.expectEqual(Cursor{ .row = 0, .col = 4 }, charLeft(&.{"hello"}, c));
try std.testing.expectEqual(Cursor{ .row = 0, .col = 6 }, charRight(&lines, c));
- try std.testing.expectEqual(Cursor{ .row = 1, .col = 2 }, firstNonWsOf(&lines, Cursor{ .row = 1, .col = 0 }));
- // cursor row past the content (mouse click below a short pane): no panic
- try std.testing.expectEqual(Cursor{ .row = 24, .col = 0 }, firstNonWsOf(&lines, Cursor{ .row = 24, .col = 3 }));
}
test "lineSpan agrees with the whole-document scans it replaces" {