summaryrefslogtreecommitdiff
path: root/src/modal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/modal.zig')
-rw-r--r--src/modal.zig128
1 files changed, 64 insertions, 64 deletions
diff --git a/src/modal.zig b/src/modal.zig
index 89046820..6f0bdc9c 100644
--- a/src/modal.zig
+++ b/src/modal.zig
@@ -166,9 +166,9 @@ pub const Normal = struct {
};
/// 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);
+ fn roles(comptime from: Role, comptime to: Role) [@backingInt(to) - @backingInt(from) + 1]Role {
+ var out: [@backingInt(to) - @backingInt(from) + 1]Role = undefined;
+ for (&out, @backingInt(from)..) |*role, i| role.* = @fromBackingInt(@intCast(i));
return out;
}
const goto_roles: []const Role = &roles(.goto_file_start, .goto_callstack);
@@ -500,10 +500,6 @@ pub const Normal = struct {
}
};
-test {
- _ = Normal;
-}
-
// Cursor columns are UTF-8 byte offsets at grapheme boundaries; line.len is the terminator.
pub const Cursor = struct {
@@ -729,38 +725,6 @@ pub fn lineStartOffset(content: []const u8, row: usize) usize {
return content.len;
}
-test "lineStartOffset by blocks and offsetAt agree with a line at a time" {
- var prng: std.Random.DefaultPrng = .init(0x9e3779b9);
- const random = prng.random();
- var buf: [300]u8 = undefined;
- for (0..2000) |_| {
- const len = random.uintLessThan(usize, buf.len);
- for (buf[0..len]) |*b| b.* = if (random.uintLessThan(u8, 4) == 0) '\n' else 'a';
- const text = buf[0..len];
- for (0..len + 3) |row| {
- var off: usize = 0;
- var r: usize = 0;
- const want = while (r < row) : (r += 1) {
- off = (std.mem.indexOfScalarPos(u8, text, off, '\n') orelse break text.len) + 1;
- } else off;
- try std.testing.expectEqual(want, lineStartOffset(text, row));
- const cursor: Cursor = .{ .row = row, .col = random.uintLessThan(usize, 6) };
- const clamped = lineStartOffset(text, @min(row, cursorLineCount(text) - 1));
- const end = std.mem.indexOfScalarPos(u8, text, clamped, '\n') orelse text.len;
- try std.testing.expectEqual(clamped + graphemeStart(text[clamped..end], @min(cursor.col, end - clamped)), offsetAt(text, cursor));
- // lineSpan, as it was: a line at a time, null past the last.
- const span: ?LineSpan = span: {
- if (text.len == 0) break :span null;
- var start: usize = 0;
- for (0..row) |_| start = (std.mem.indexOfScalarPos(u8, text, start, '\n') orelse break :span null) + 1;
- if (start == text.len and !(row == 0 or text[text.len - 1] == '\n')) break :span null;
- break :span .{ .start = start, .end = std.mem.indexOfScalarPos(u8, text, start, '\n') orelse text.len };
- };
- try std.testing.expectEqual(span, lineSpan(text, row));
- }
- }
-}
-
// the text of line `row` (no terminator), a slice into `content`.
pub fn lineSlice(content: []const u8, row: usize) []const u8 {
const start = lineStartOffset(content, row);
@@ -1401,23 +1365,6 @@ pub fn selectParagraph(text: []const u8, r: Selection, around: bool, count: usiz
};
}
-test "selection textobjects distinguish words and paragraph boundaries" {
- const t = "alpha beta gamma\n";
- // miw mid-word
- var r = selectWord(t, .{ .anchor = 8, .head = 9 }, false, false);
- try std.testing.expectEqual(@as(usize, 6), r.anchor);
- try std.testing.expectEqual(@as(usize, 10), r.head);
- // maw on the space after "beta": collapses to the boundary
- r = selectWord(t, .{ .anchor = 10, .head = 11 }, true, false);
- try std.testing.expectEqual(@as(usize, 10), r.anchor);
- try std.testing.expectEqual(@as(usize, 10), r.head);
- const t2 = "aa\n\ncc\n";
- // mip from the blank line selects the NEXT paragraph
- r = selectParagraph(t2, .{ .anchor = 3, .head = 4 }, false, 1);
- try std.testing.expectEqual(@as(usize, 4), r.anchor);
- try std.testing.expectEqual(@as(usize, 7), r.head);
-}
-
/// A line's indent as it is, tabs and spaces both, as acme's autoindent
/// copies it: what o/O/insert-newline give the new line.
pub fn indentText(line: []const u8) []const u8 {
@@ -1434,14 +1381,6 @@ pub fn newlineIndentWidth(line: []const u8, col: usize) usize {
return indentText(line).len + @as(usize, if (extra) INDENT_W else 0);
}
-test "newline indent keeps levels and adds one after delimiters" {
- try std.testing.expectEqual(@as(usize, 4), newlineIndentWidth(" value", 9));
- try std.testing.expectEqual(@as(usize, 8), newlineIndentWidth(" call()", 10));
- try std.testing.expectEqual(@as(usize, 8), newlineIndentWidth(" callback({}) ", 16));
- try std.testing.expectEqual(@as(usize, 4), newlineIndentWidth("work(", 5));
- try std.testing.expectEqual(@as(usize, 4), newlineIndentWidth("list[tail", 5));
-}
-
/// Increment a selected decimal integer, preserving zero-padding across sign changes.
pub fn incrementDecimal(alloc: std.mem.Allocator, frag: []const u8, amount: i64) !?[]u8 {
if (frag.len == 0) return null;
@@ -1475,6 +1414,67 @@ pub fn incrementDecimal(alloc: std.mem.Allocator, frag: []const u8, amount: i64)
return out;
}
+test {
+ _ = Normal;
+}
+
+test "lineStartOffset by blocks and offsetAt agree with a line at a time" {
+ var prng: std.Random.DefaultPrng = .init(0x9e3779b9);
+ const random = prng.random();
+ var buf: [300]u8 = undefined;
+ for (0..2000) |_| {
+ const len = random.uintLessThan(usize, buf.len);
+ for (buf[0..len]) |*b| b.* = if (random.uintLessThan(u8, 4) == 0) '\n' else 'a';
+ const text = buf[0..len];
+ for (0..len + 3) |row| {
+ var off: usize = 0;
+ var r: usize = 0;
+ const want = while (r < row) : (r += 1) {
+ off = (std.mem.indexOfScalarPos(u8, text, off, '\n') orelse break text.len) + 1;
+ } else off;
+ try std.testing.expectEqual(want, lineStartOffset(text, row));
+ const cursor: Cursor = .{ .row = row, .col = random.uintLessThan(usize, 6) };
+ const clamped = lineStartOffset(text, @min(row, cursorLineCount(text) - 1));
+ const end = std.mem.indexOfScalarPos(u8, text, clamped, '\n') orelse text.len;
+ try std.testing.expectEqual(clamped + graphemeStart(text[clamped..end], @min(cursor.col, end - clamped)), offsetAt(text, cursor));
+ // lineSpan, as it was: a line at a time, null past the last.
+ const span: ?LineSpan = span: {
+ if (text.len == 0) break :span null;
+ var start: usize = 0;
+ for (0..row) |_| start = (std.mem.indexOfScalarPos(u8, text, start, '\n') orelse break :span null) + 1;
+ if (start == text.len and !(row == 0 or text[text.len - 1] == '\n')) break :span null;
+ break :span .{ .start = start, .end = std.mem.indexOfScalarPos(u8, text, start, '\n') orelse text.len };
+ };
+ try std.testing.expectEqual(span, lineSpan(text, row));
+ }
+ }
+}
+
+test "selection textobjects distinguish words and paragraph boundaries" {
+ const t = "alpha beta gamma\n";
+ // miw mid-word
+ var r = selectWord(t, .{ .anchor = 8, .head = 9 }, false, false);
+ try std.testing.expectEqual(@as(usize, 6), r.anchor);
+ try std.testing.expectEqual(@as(usize, 10), r.head);
+ // maw on the space after "beta": collapses to the boundary
+ r = selectWord(t, .{ .anchor = 10, .head = 11 }, true, false);
+ try std.testing.expectEqual(@as(usize, 10), r.anchor);
+ try std.testing.expectEqual(@as(usize, 10), r.head);
+ const t2 = "aa\n\ncc\n";
+ // mip from the blank line selects the NEXT paragraph
+ r = selectParagraph(t2, .{ .anchor = 3, .head = 4 }, false, 1);
+ try std.testing.expectEqual(@as(usize, 4), r.anchor);
+ try std.testing.expectEqual(@as(usize, 7), r.head);
+}
+
+test "newline indent keeps levels and adds one after delimiters" {
+ try std.testing.expectEqual(@as(usize, 4), newlineIndentWidth(" value", 9));
+ try std.testing.expectEqual(@as(usize, 8), newlineIndentWidth(" call()", 10));
+ try std.testing.expectEqual(@as(usize, 8), newlineIndentWidth(" callback({}) ", 16));
+ try std.testing.expectEqual(@as(usize, 4), newlineIndentWidth("work(", 5));
+ try std.testing.expectEqual(@as(usize, 4), newlineIndentWidth("list[tail", 5));
+}
+
test "word selections match Helix motions" {
const t = "alpha beta\n";
// w from a fresh 1-wide cursor selects "alpha " (cursor on the space)