From 2ae8d3048d06c02df5e3b8053ccb0b598eec8899 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Mon, 13 Apr 2026 22:40:08 +0200 Subject: [PATCH] refactor: reduce code duplication in case transform functions --- src/tui/editor.zig | 48 ++++++++++++++++------------------------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/src/tui/editor.zig b/src/tui/editor.zig index a4456dad..347c13a5 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -7268,7 +7268,13 @@ pub const Editor = struct { } } - fn to_upper_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { + fn transform_cursel( + comptime transform: fn (std.mem.Allocator, []const u8) Buffer.unicode.TransformError![]u8, + self: *Self, + root_: Buffer.Root, + cursel: *CurSel, + allocator: Allocator, + ) error{Stop}!Buffer.Root { var root = root_; const saved = cursel.*; const sel = try self.get_selection_or_select_word(root, cursel); @@ -7276,14 +7282,18 @@ pub const Editor = struct { const sfa_allocator = sfa.get(); const cut_text = copy_selection(root, sel.*, sfa_allocator, self.metrics) catch return error.Stop; defer sfa_allocator.free(cut_text); - const ucased = Buffer.unicode.to_upper(sfa_allocator, cut_text) catch return error.Stop; - defer sfa_allocator.free(ucased); + const transformed = transform(sfa_allocator, cut_text) catch return error.Stop; + defer sfa_allocator.free(transformed); root = try self.delete_selection(root, cursel, allocator); - root = self.insert(root, cursel, ucased, allocator) catch return error.Stop; + root = self.insert(root, cursel, transformed, allocator) catch return error.Stop; cursel.* = saved; return root; } + fn to_upper_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { + return transform_cursel(Buffer.unicode.to_upper, self, root_, cursel, allocator); + } + pub fn to_upper(self: *Self, _: Context) Result { const b = try self.buf_for_update(); const root = try self.with_cursels_mut_once(b.root, to_upper_cursel, b.allocator); @@ -7292,20 +7302,8 @@ pub const Editor = struct { } pub const to_upper_meta: Meta = .{ .description = "Convert selection or word to upper case" }; - fn to_lower_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, buffer_allocator: Allocator) error{Stop}!Buffer.Root { - var root = root_; - const saved = cursel.*; - const sel = try self.get_selection_or_select_word(root, cursel); - var sfa = std.heap.stackFallback(4096, self.allocator); - const sfa_allocator = sfa.get(); - const cut_text = copy_selection(root, sel.*, sfa_allocator, self.metrics) catch return error.Stop; - defer sfa_allocator.free(cut_text); - const ucased = Buffer.unicode.to_lower(sfa_allocator, cut_text) catch return error.Stop; - defer sfa_allocator.free(ucased); - root = try self.delete_selection(root, cursel, buffer_allocator); - root = self.insert(root, cursel, ucased, buffer_allocator) catch return error.Stop; - cursel.* = saved; - return root; + fn to_lower_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { + return transform_cursel(Buffer.unicode.to_lower, self, root_, cursel, allocator); } pub fn to_lower(self: *Self, _: Context) Result { @@ -7348,19 +7346,7 @@ pub const Editor = struct { pub const switch_case_meta: Meta = .{ .description = "Switch the case of selection or character at cursor" }; fn toggle_case_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root { - var root = root_; - const saved = cursel.*; - const sel = try self.get_selection_or_select_word(root, cursel); - var sfa = std.heap.stackFallback(4096, self.allocator); - const sfa_allocator = sfa.get(); - const cut_text = copy_selection(root, sel.*, sfa_allocator, self.metrics) catch return error.Stop; - defer sfa_allocator.free(cut_text); - const transformed = Buffer.unicode.toggle_case(sfa_allocator, cut_text) catch return error.Stop; - defer sfa_allocator.free(transformed); - root = try self.delete_selection(root, cursel, allocator); - root = self.insert(root, cursel, transformed, allocator) catch return error.Stop; - cursel.* = saved; - return root; + return transform_cursel(Buffer.unicode.toggle_case, self, root_, cursel, allocator); } pub fn toggle_case(self: *Self, _: Context) Result {