Compare commits

...

4 commits

Author SHA1 Message Date
Igor Támara
1ac34340b4 test: [hx] Exercises some aspects of BEW and bew
closes #315
2025-11-03 22:48:20 +01:00
Igor Támara
8d2355dcb8 refactor: [hx] move and extend word sharing common code 2025-11-03 22:48:20 +01:00
Igor Támara
c3bd6b6bc3 refactor: [hx] group word movements 2025-11-03 22:48:20 +01:00
Igor Támara
a8c056c211 feat: hx extend word to right 2025-11-03 22:48:20 +01:00
4 changed files with 136 additions and 116 deletions

View file

@ -68,9 +68,12 @@
["F", "move_to_char", "select_to_char_left_helix"],
["T", "move_to_char", "select_till_char_left_helix"],
["W", "move_next_long_word_start"],
["B", "move_prev_long_word_start"],
["b", "move_prev_word_start"],
["e", "move_next_word_end"],
["w", "move_next_word_start"],
["E", "move_next_long_word_end"],
["W", "move_next_long_word_start"],
["t", "move_to_char", "select_till_char_right_helix"],
["f", "move_to_char", "select_to_char_right_helix"],
@ -123,13 +126,8 @@
["kp_home", "move_begin"],
["kp_end", "move_end"],
["w","move_next_word_start"],
["b","move_prev_word_start"],
["e","move_next_word_end"],
["v", "enter_mode", "select"],
["G", "goto_line"],
["g g", "goto_line_vim"],
["g e", "move_buffer_end"],
@ -278,9 +276,6 @@
"init_command": ["enable_selection"],
"press": [
["b", "select_word_left"],
["w", "select_word_right"],
["ctrl+b", "select_page_up"],
["ctrl+f", "select_page_down"],
["ctrl+u", "select_half_page_up"],
@ -355,9 +350,12 @@
["F", "move_to_char", "extend_to_char_left_helix"],
["T", "move_to_char", "extend_till_char_left_helix"],
["W", "extend_next_long_word_start"],
["B", "extend_prev_long_word_start"],
["b", "extend_prev_word_start"],
["e", "extend_next_word_end"],
["w", "extend_next_word_start"],
["E", "extend_next_long_word_end"],
["W", "extend_next_long_word_start"],
["t", "move_to_char", "extend_till_char_right_helix"],
["f", "move_to_char", "extend_to_char_right_helix"],
@ -422,11 +420,6 @@
["end", "extend_to_line_end"],
["kp_home", "extend_to_line_start"],
["kp_end", "extend_to_line_end"],
["w", "extend_next_word_start"],
["b", "extend_pre_word_start"],
["e", "extend_next_word_end"],
["v", "enter_mode", "normal"],
["g g", "goto_line_vim"],

View file

@ -1904,7 +1904,7 @@ pub const Editor = struct {
self.collapse_cursors();
}
fn with_cursors_const_repeat(self: *Self, root: Buffer.Root, move: cursor_operator_const, ctx: Context) error{Stop}!void {
pub fn with_cursors_const_repeat(self: *Self, root: Buffer.Root, move: cursor_operator_const, ctx: Context) error{Stop}!void {
var repeat: usize = 1;
_ = ctx.args.match(.{tp.extract(&repeat)}) catch false;
while (repeat > 0) : (repeat -= 1) {
@ -2138,7 +2138,7 @@ pub const Editor = struct {
}
const cursor_predicate = *const fn (root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) bool;
const cursor_operator_const = *const fn (root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void;
pub const cursor_operator_const = *const fn (root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void;
const cursor_operator_const_arg = *const fn (root: Buffer.Root, cursor: *Cursor, ctx: Context, metrics: Buffer.Metrics) error{Stop}!void;
pub const cursel_operator_mut_once_arg = *const fn (root: Buffer.Root, cursel: *CurSel, ctx: Context, metrics: Buffer.Metrics) error{Stop}!void;
const cursor_view_operator_const = *const fn (root: Buffer.Root, cursor: *Cursor, view: *const View, metrics: Buffer.Metrics) error{Stop}!void;
@ -3232,7 +3232,7 @@ pub const Editor = struct {
move_cursor_left_until(root, cursor, is_word_boundary_left, metrics);
}
fn move_cursor_word_left_vim(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void {
pub fn move_cursor_word_left_vim(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void {
try move_cursor_left(root, cursor, metrics);
move_cursor_left_until(root, cursor, is_word_boundary_left_vim, metrics);
}

View file

@ -220,89 +220,65 @@ const cmds_ = struct {
pub const extend_line_below_meta: Meta = .{ .arguments = &.{.integer}, .description = "Select current line, if already selected, extend to next line" };
pub fn move_next_word_start(_: *void, ctx: Ctx) Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;
const root = try ed.buf_root();
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
cursel.selection = null;
};
ed.with_selections_const_repeat(root, Editor.move_cursor_word_right_vim, ctx) catch {};
ed.clamp();
try move_to_word(ctx, Editor.move_cursor_word_right_vim);
}
pub const move_next_word_start_meta: Meta = .{ .description = "Move next word start", .arguments = &.{.integer} };
pub fn extend_next_word_start(_: *void, ctx: Ctx) Result {
try extend_to_word(ctx, Editor.move_cursor_word_right_vim);
}
pub const extend_next_word_start_meta: Meta = .{ .description = "Extend next word start", .arguments = &.{.integer} };
pub fn move_next_long_word_start(_: *void, ctx: Ctx) Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;
const root = try ed.buf_root();
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
cursel.selection = null;
};
ed.with_selections_const_repeat(root, move_cursor_long_word_right, ctx) catch {};
ed.clamp();
try move_to_word(ctx, move_cursor_long_word_right);
}
pub const move_next_long_word_start_meta: Meta = .{ .description = "Move next long word start", .arguments = &.{.integer} };
pub fn extend_next_long_word_start(_: *void, ctx: Ctx) Result {
try extend_to_word(ctx, move_cursor_long_word_right);
}
pub const extend_next_long_word_start_meta: Meta = .{ .description = "Extend next long word start", .arguments = &.{.integer} };
pub fn move_prev_word_start(_: *void, ctx: Ctx) Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;
const root = try ed.buf_root();
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
cursel.selection = null;
};
ed.with_selections_const_repeat(root, move_cursor_word_left_helix, ctx) catch {};
ed.clamp();
try move_to_word(ctx, move_cursor_word_left_helix);
}
pub const move_prev_word_start_meta: Meta = .{ .description = "Move previous word start", .arguments = &.{.integer} };
pub fn extend_prev_word_start(_: *void, ctx: Ctx) Result {
try extend_to_word(ctx, move_cursor_word_left_helix);
}
pub const extend_prev_word_start_meta: Meta = .{ .description = "Extend previous word start", .arguments = &.{.integer} };
pub fn move_prev_long_word_start(_: *void, ctx: Ctx) Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;
const root = try ed.buf_root();
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
cursel.selection = null;
};
ed.with_selections_const_repeat(root, move_cursor_long_word_left, ctx) catch {};
ed.clamp();
try move_to_word(ctx, move_cursor_long_word_left);
}
pub const move_prev_long_word_start_meta: Meta = .{ .description = "Move previous long word start", .arguments = &.{.integer} };
pub fn extend_prev_long_word_start(_: *void, ctx: Ctx) Result {
try extend_to_word(ctx, move_cursor_long_word_left);
}
pub const extend_prev_long_word_start_meta: Meta = .{ .description = "Extend previous word start", .arguments = &.{.integer} };
pub fn move_next_word_end(_: *void, ctx: Ctx) Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;
const root = try ed.buf_root();
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
cursel.selection = null;
};
ed.with_selections_const_repeat(root, move_cursor_word_right_end_helix, ctx) catch {};
ed.clamp();
try move_to_word(ctx, move_cursor_word_right_end_helix);
}
pub const move_next_word_end_meta: Meta = .{ .description = "Move next word end", .arguments = &.{.integer} };
pub fn extend_next_word_end(_: *void, ctx: Ctx) Result {
try extend_to_word(ctx, move_cursor_word_right_end_helix);
}
pub const extend_next_word_end_meta: Meta = .{ .description = "Extend next word end", .arguments = &.{.integer} };
pub fn move_next_long_word_end(_: *void, ctx: Ctx) Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;
const root = try ed.buf_root();
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
cursel.selection = null;
};
ed.with_selections_const_repeat(root, move_cursor_long_word_right_end, ctx) catch {};
ed.clamp();
try move_to_word(ctx, move_cursor_long_word_right_end);
}
pub const move_next_long_word_end_meta: Meta = .{ .description = "Move next long word end", .arguments = &.{.integer} };
pub fn extend_next_long_word_end(_: *void, ctx: Ctx) Result {
try extend_to_word(ctx, move_cursor_long_word_right_end);
}
pub const extend_next_long_word_end_meta: Meta = .{ .description = "Extend next long word end", .arguments = &.{.integer} };
pub fn cut_forward_internal_inclusive(_: *void, _: Ctx) Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;
@ -454,6 +430,32 @@ const cmds_ = struct {
pub const replace_with_character_helix_meta: Meta = .{ .description = "Replace with character" };
};
fn move_to_word(ctx: command.Context, move: Editor.cursor_operator_const) command.Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;
const root = try ed.buf_root();
// NOR mode moves n words selecting the last one
var repeat: usize = 0;
_ = ctx.args.match(.{tp.extract(&repeat)}) catch false;
if (repeat > 1) ed.with_cursors_const_repeat(root, move, command.fmt(.{repeat - 1})) catch {};
for (ed.cursels.items) |*cursel_| if (cursel_.*) |*cursel| {
cursel.selection = null;
};
ed.with_selections_const_repeat(root, move, command.fmt(.{1})) catch {};
ed.clamp();
}
fn extend_to_word(ctx: command.Context, move: Editor.cursor_operator_const) command.Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;
const root = try ed.buf_root();
ed.with_selections_const_repeat(root, move, ctx) catch {};
ed.clamp();
}
fn to_char_helix(ctx: command.Context, move: Editor.cursel_operator_mut_once_arg) command.Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;

View file

@ -30,7 +30,9 @@ fn apply_movements(movements: []const u8, root: Buffer.Root, cursor: *Cursor, th
'e' => {
try helix.test_internal.move_cursor_word_right_end_helix(root, cursor, the_metrics);
},
else => {},
else => {
return error.Stop;
},
}
}
try std.testing.expectEqual(col, cursor.col);
@ -66,7 +68,9 @@ fn metrics() Buffer.Metrics {
};
}
const doc: []const u8 =
\\gawk '{print length($0) }' testflowhelixwbe.txt | tr '\n' ' '
\\gawk '{print length($0) }' testflowhelixwbe.txt | tr '\n' ' 'i
\\
\\Allows you to know what is the length of each line ^^^^
\\a small $% Test.here, with.things()to demo
\\ with surrounding.space a bb AA a small and long
\\
@ -78,20 +82,17 @@ const doc: []const u8 =
\\ $$%. []{{}. dart de
\\da
;
//60 44 54 0 2 8 138 0 0 22 2 0
var eol_mode: Buffer.EolMode = .lf;
var sanitized: bool = false;
var the_cursor = Cursor{ .row = 1, .col = 1, .target = 0 };
// To run a specific test
// zig build test -Dtest-filter=word_movement
test "words_movement" {
const buffer = try Buffer.create(a);
defer buffer.deinit();
buffer.update(try buffer.load_from_string(doc, &eol_mode, &sanitized));
const root: Buffer.Root = buffer.root;
var the_cursor = Cursor{ .row = 1, .col = 1, .target = 0 };
the_cursor.col = 1;
the_cursor.row = 0;
@ -99,8 +100,6 @@ test "words_movement" {
.{ .moves = "b", .row = 0, .col = 0 },
.{ .moves = "w", .row = 0, .col = 5 },
.{ .moves = "b", .row = 0, .col = 1 },
// TODO: Review the following line, an Stop is raising
// .{ .moves = "bb", .row = 0, .col = 0 },
.{ .moves = "ww", .row = 0, .col = 7 },
.{ .moves = "bb", .row = 0, .col = 1 },
.{ .moves = "www", .row = 0, .col = 13 },
@ -110,17 +109,40 @@ test "words_movement" {
.{ .moves = "wb", .row = 0, .col = 1 },
.{ .moves = "e", .row = 0, .col = 4 },
.{ .moves = "b", .row = 0, .col = 1 },
// TODO: b has a bug when at the end of the view, it's
// not getting back.
//
// TODO: Another bug detected is when there are multiple
// lines, b is not able to get to the first non
// newline.
};
for (movements) |move| {
try apply_movements(move.moves, root, &the_cursor, metrics(), move.row, move.col);
}
the_cursor.row = 11;
the_cursor.col = 1;
const more_movements: [2]MoveExpected = .{
.{ .moves = "b", .row = 8, .col = 135 },
.{ .moves = "w", .row = 11, .col = 2 },
};
for (more_movements) |move| {
try apply_movements(move.moves, root, &the_cursor, metrics(), move.row, move.col);
}
}
test "edge_word_movements" {
const buffer = try Buffer.create(a);
defer buffer.deinit();
buffer.update(try buffer.load_from_string(doc, &eol_mode, &sanitized));
const root: Buffer.Root = buffer.root;
var cursor = Cursor{ .row = 1, .col = 1, .target = 0 };
cursor.col = 0;
cursor.row = 0;
const expected_error = error.Stop;
var result = helix.test_internal.move_cursor_word_left_helix(root, &cursor, metrics());
try std.testing.expectError(expected_error, result);
try std.testing.expectEqual(0, cursor.row);
try std.testing.expectEqual(0, cursor.col);
result = helix.test_internal.move_cursor_long_word_left(root, &cursor, metrics());
try std.testing.expectError(expected_error, result);
try std.testing.expectEqual(0, cursor.row);
try std.testing.expectEqual(0, cursor.col);
}
test "long_words_movement" {
@ -128,6 +150,8 @@ test "long_words_movement" {
defer buffer.deinit();
buffer.update(try buffer.load_from_string(doc, &eol_mode, &sanitized));
const root: Buffer.Root = buffer.root;
var the_cursor = Cursor{ .row = 1, .col = 1, .target = 0 };
the_cursor.col = 1;
the_cursor.row = 0;
@ -135,19 +159,12 @@ test "long_words_movement" {
.{ .moves = "B", .row = 0, .col = 0 },
.{ .moves = "W", .row = 0, .col = 5 },
.{ .moves = "B", .row = 0, .col = 1 },
// TODO: Review the following line, an Stop is raising
// .{ .moves = "BB", .row = 0, .col = 0 },
.{ .moves = "WW", .row = 0, .col = 13 },
.{ .moves = "BB", .row = 0, .col = 1 },
.{ .moves = "WWW", .row = 0, .col = 24 },
.{ .moves = "BBB", .row = 0, .col = 1 },
.{ .moves = "WWWW", .row = 0, .col = 27 },
.{ .moves = "BBBB", .row = 0, .col = 1 },
// TODO:
// WWWWW should report 48, is reporting 49, when changing modes
// the others report 48. This is an specific hx mode
// .{ .moves = "WWWWW", .row = 0, .col = 48 },
// Same bugs detected in b are in B
.{ .moves = "WB", .row = 0, .col = 1 },
.{ .moves = "E", .row = 0, .col = 4 },
.{ .moves = "B", .row = 0, .col = 1 },
@ -163,6 +180,8 @@ test "to_char_right_beyond_eol" {
defer buffer.deinit();
buffer.update(try buffer.load_from_string(doc, &eol_mode, &sanitized));
const root: Buffer.Root = buffer.root;
var the_cursor = Cursor{ .row = 1, .col = 1, .target = 0 };
the_cursor.col = 0;
the_cursor.row = 0;
const expected_error = error.Stop;
@ -175,44 +194,50 @@ test "to_char_right_beyond_eol" {
// Move found in the next line
try helix.test_internal.move_cursor_to_char_right_beyond_eol(root, &the_cursor, metrics(), fmt_command(.{"T"}));
try std.testing.expectEqual(1, the_cursor.row);
try std.testing.expectEqual(3, the_cursor.row);
try std.testing.expectEqual(11, the_cursor.col);
// Move found in the previous line
try helix.test_internal.move_cursor_to_char_left_beyond_eol(root, &the_cursor, metrics(), fmt_command(.{"t"}));
try std.testing.expectEqual(0, the_cursor.row);
try std.testing.expectEqual(51, the_cursor.col);
try std.testing.expectEqual(2, the_cursor.row);
try std.testing.expectEqual(35, the_cursor.col);
// Not found to end of buffer, cursor not moved
result = helix.test_internal.move_cursor_to_char_right_beyond_eol(root, &the_cursor, metrics(), fmt_command(.{"Z"}));
try std.testing.expectError(expected_error, result);
try std.testing.expectEqual(0, the_cursor.row);
try std.testing.expectEqual(51, the_cursor.col);
try std.testing.expectEqual(2, the_cursor.row);
try std.testing.expectEqual(35, the_cursor.col);
// Not found to begin of buffer
result = helix.test_internal.move_cursor_to_char_left_beyond_eol(root, &the_cursor, metrics(), fmt_command(.{"Z"}));
try std.testing.expectError(expected_error, result);
try std.testing.expectEqual(0, the_cursor.row);
try std.testing.expectEqual(51, the_cursor.col);
try std.testing.expectEqual(2, the_cursor.row);
try std.testing.expectEqual(35, the_cursor.col);
// till char difference
// Move found in the next line
try helix.test_internal.move_cursor_till_char_right_beyond_eol(root, &the_cursor, metrics(), fmt_command(.{"T"}));
try std.testing.expectEqual(1, the_cursor.row);
try std.testing.expectEqual(3, the_cursor.row);
try std.testing.expectEqual(10, the_cursor.col);
// Move found in the previous line
try helix.test_internal.move_cursor_till_char_left_beyond_eol(root, &the_cursor, metrics(), fmt_command(.{"t"}));
try std.testing.expectEqual(0, the_cursor.row);
try std.testing.expectEqual(52, the_cursor.col);
try std.testing.expectEqual(2, the_cursor.row);
try std.testing.expectEqual(36, the_cursor.col);
// Move found in the same line
try helix.test_internal.move_cursor_till_char_left_beyond_eol(root, &the_cursor, metrics(), fmt_command(.{"x"}));
try std.testing.expectEqual(0, the_cursor.row);
try std.testing.expectEqual(46, the_cursor.col);
try helix.test_internal.move_cursor_till_char_left_beyond_eol(root, &the_cursor, metrics(), fmt_command(.{"u"}));
try std.testing.expectEqual(2, the_cursor.row);
try std.testing.expectEqual(10, the_cursor.col);
// Move found in the same line
try helix.test_internal.move_cursor_till_char_right_beyond_eol(root, &the_cursor, metrics(), fmt_command(.{"t"}));
try std.testing.expectEqual(0, the_cursor.row);
try std.testing.expectEqual(50, the_cursor.col);
try std.testing.expectEqual(2, the_cursor.row);
try std.testing.expectEqual(21, the_cursor.col);
}
// TODO: When at end of file, enter sel mode makes
// difficult to get back and is confusing for users.
// Related to that is the fact that when a selection
// is made, then trying to move to the right, the
// first movement is swallowed