feat: implement helix search_selection and add_next_match_helix

This commit is contained in:
CJ van den Berg 2025-12-04 17:33:43 +01:00
parent 791b184583
commit 58622a5531
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 35 additions and 7 deletions

View file

@ -384,7 +384,7 @@
["?", "rfind"],
["N", "extend_search_next"],
["*", "extend_search_prev"],
["*", "search_selection"],
["r", "replace"],
["P", "paste_clipboard_before"],
@ -500,7 +500,7 @@
["] space", "add_newline_below"],
["/", "find"],
["n", "goto_next_match"],
["n", "add_next_match_helix"],
["u", "undo"],
["U", "redo"],

View file

@ -5282,7 +5282,7 @@ pub const Editor = struct {
(history.addOne(self.allocator) catch return).* = new;
}
fn set_last_find_query(self: *Self, query: []const u8, match_type: Match.Type) void {
pub fn set_last_find_query(self: *Self, query: []const u8, match_type: Match.Type) void {
self.last_find_query_match_type = match_type;
if (self.last_find_query) |last| {
if (query.ptr != last.ptr) {
@ -5523,14 +5523,18 @@ pub const Editor = struct {
}
pub const move_cursor_next_match_meta: Meta = .{ .description = "Move cursor to next hightlighted match" };
pub fn repeat_last_find(self: *Self) Result {
if (self.last_find_query) |last| {
self.find_operation = .goto_next_match;
try self.find_in_buffer(last, self.last_find_query_match_type, .exact);
}
}
pub fn goto_next_match(self: *Self, ctx: Context) Result {
try self.send_editor_jump_source();
self.cancel_all_selections();
if (self.matches.items.len == 0) {
if (self.last_find_query) |last| {
self.find_operation = .goto_next_match;
try self.find_in_buffer(last, self.last_find_query_match_type, .exact);
}
try self.repeat_last_find();
}
try self.move_cursor_next_match(ctx);
try self.send_editor_jump_destination();

View file

@ -498,6 +498,30 @@ const cmds_ = struct {
ed.get_primary().* = primary;
}
pub const keep_primary_selection_meta: Meta = .{};
pub fn search_selection(_: *void, _: Ctx) Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;
const sel = ed.get_primary().selection orelse {
ed.logger.print("no selection", .{});
return;
};
const query = try ed.get_selection(sel, ed.allocator);
defer ed.allocator.free(query);
ed.match_type = .find;
ed.set_last_find_query(query, .find);
ed.logger.print("set find register to '{s}'", .{query});
}
pub const search_selection_meta: Meta = .{};
pub fn add_next_match_helix(_: *void, _: Ctx) Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;
if (ed.matches.items.len == 0)
try ed.repeat_last_find();
try ed.add_cursor_next_match(.{});
}
pub const add_next_match_helix_meta: Meta = .{};
};
fn match_bracket(root: Buffer.Root, cursel: *CurSel, ctx: command.Context, metrics: Buffer.Metrics) error{Stop}!void {