From 18bc89edf8fd4297081cf260d238685d3e3f7618 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Thu, 30 Oct 2025 22:32:57 +0100 Subject: [PATCH] refactor: replace cursel_length with Buffer.Node.get_range get_range is likely much faster because it only walks the buffer tree once. Besides the performance difference it should give identical results. --- src/tui/editor.zig | 20 -------------------- src/tui/mode/helix.zig | 4 ++-- 2 files changed, 2 insertions(+), 22 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index 59f55cd..f4c97c3 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -1632,26 +1632,6 @@ pub const Editor = struct { return self.primary.col - self.view.col; } - pub fn cursel_length(root: Buffer.Root, cursel_: CurSel, metrics: Buffer.Metrics) usize { - var length: usize = 0; - var cursel = cursel_; - cursel.check_selection(root, metrics); - if (cursel.selection) |*sel| { - sel.normalize(); - if (sel.begin.row == sel.end.row) { - length = sel.end.col - sel.begin.col; - } else { - var row = sel.begin.row; - while (row < sel.end.row) { - length += root.line_width(row, metrics) catch 0; - row += 1; - } - length = length + sel.end.col - sel.begin.col; - } - } - return length; - } - fn update_event(self: *Self) !void { const primary = self.get_primary(); const dirty = if (self.buffer) |buf| buf.is_dirty() else false; diff --git a/src/tui/mode/helix.zig b/src/tui/mode/helix.zig index 3857ae3..f5a33c0 100644 --- a/src/tui/mode/helix.zig +++ b/src/tui/mode/helix.zig @@ -615,12 +615,12 @@ fn replace_cursel_with_character(ed: *Editor, root: Buffer.Root, cursel: *CurSel return error.Stop; const no_selection = try select_char_if_no_selection(cursel, root, ed.metrics); var begin: Cursor = undefined; + var sel_length: usize = 1; if (cursel.selection) |*sel| { sel.normalize(); begin = sel.*.begin; + _ = root.get_range(sel.*, null, null, &sel_length, ed.metrics) catch return error.Stop; } - - 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();