feat: [hx] r to replace with a character

If no selection, the character under the cursor is replaced, if
selection, each character is replaced by the typed character.
This commit is contained in:
Igor Támara 2025-10-29 00:51:18 -05:00 committed by CJ van den Berg
parent 411b26d4aa
commit 8246f2b0ba
4 changed files with 164 additions and 0 deletions

View file

@ -441,6 +441,17 @@ const cmds_ = struct {
try paste_helix(ctx, insert_before);
}
pub const paste_clipboard_before_meta: Meta = .{ .description = "Paste from clipboard before selection" };
pub fn replace_with_character_helix(_: *void, ctx: Ctx) Result {
const mv = tui.mainview() orelse return;
const ed = mv.get_active_editor() orelse return;
var root = ed.buf_root() catch return;
root = try ed.with_cursels_mut_once_arg(root, replace_cursel_with_character, ed.allocator, ctx);
try ed.update_buf(root);
ed.clamp();
ed.need_render();
}
pub const replace_with_character_helix_meta: Meta = .{ .description = "Replace with character" };
};
fn to_char_helix(ctx: command.Context, move: Editor.cursel_operator_mut_once_arg) command.Result {
@ -598,6 +609,40 @@ fn move_cursor_word_left_helix(root: Buffer.Root, cursor: *Cursor, metrics: Buff
}
}
fn replace_cursel_with_character(ed: *Editor, root: Buffer.Root, cursel: *CurSel, allocator: Allocator, ctx: command.Context) error{Stop}!Buffer.Root {
var egc: []const u8 = undefined;
if (!(ctx.args.match(.{tp.extract(&egc)}) catch return error.Stop))
return error.Stop;
const no_selection = try select_char_if_no_selection(cursel, root, ed.metrics);
var begin: Cursor = undefined;
if (cursel.selection) |*sel| {
sel.normalize();
begin = sel.*.begin;
}
const sel_length = Editor.cursel_length(root, cursel.*, ed.metrics);
const total_length = sel_length * egc.len;
var sfa = std.heap.stackFallback(4096, ed.allocator);
const sfa_allocator = sfa.get();
const replacement = sfa_allocator.alloc(u8, total_length) catch return error.Stop;
errdefer allocator.free(replacement);
for (0..sel_length) |i| {
for (0..egc.len) |j| {
replacement[i * egc.len + j] = egc[j];
}
}
const root_ = insert_replace_selection(ed, root, cursel, replacement, allocator) catch return error.Stop;
if (no_selection) {
try cursel.cursor.move_left(root, ed.metrics);
cursel.disable_selection(root, ed.metrics);
} else {
cursel.selection = Selection{ .begin = begin, .end = cursel.cursor };
}
return root_;
}
fn move_noop(_: Buffer.Root, _: *Cursor, _: Buffer.Metrics) error{Stop}!void {}
fn move_cursor_word_right_end_helix(root: Buffer.Root, cursor: *Cursor, metrics: Buffer.Metrics) error{Stop}!void {
@ -888,6 +933,21 @@ fn move_cursor_carriage_return(root: Buffer.Root, cursel: CurSel, cursor: *Curso
try Editor.move_cursor_right(root, cursor, metrics);
}
fn select_char_if_no_selection(cursel: *CurSel, root: Buffer.Root, metrics: Buffer.Metrics) !bool {
if (cursel.selection) |*sel_| {
const sel: *Selection = sel_;
if (sel.*.empty()) {
sel.*.begin = .{ .row = cursel.cursor.row, .col = cursel.cursor.col + 1, .target = cursel.cursor.target + 1 };
return true;
}
return false;
} else {
const sel = try cursel.enable_selection(root, metrics);
sel.begin = .{ .row = cursel.cursor.row, .col = cursel.cursor.col + 1, .target = cursel.cursor.target + 1 };
return true;
}
}
fn is_cursel_from_extend_line_below(cursel: CurSel) bool {
if (cursel.selection) |sel_| {
var sel = sel_;