refactor: reduce code duplication in case transform functions

This commit is contained in:
CJ van den Berg 2026-04-13 22:40:08 +02:00
parent c7e6906efd
commit 2ae8d3048d
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -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_; var root = root_;
const saved = cursel.*; const saved = cursel.*;
const sel = try self.get_selection_or_select_word(root, 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 sfa_allocator = sfa.get();
const cut_text = copy_selection(root, sel.*, sfa_allocator, self.metrics) catch return error.Stop; const cut_text = copy_selection(root, sel.*, sfa_allocator, self.metrics) catch return error.Stop;
defer sfa_allocator.free(cut_text); defer sfa_allocator.free(cut_text);
const ucased = Buffer.unicode.to_upper(sfa_allocator, cut_text) catch return error.Stop; const transformed = transform(sfa_allocator, cut_text) catch return error.Stop;
defer sfa_allocator.free(ucased); defer sfa_allocator.free(transformed);
root = try self.delete_selection(root, cursel, allocator); 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; cursel.* = saved;
return root; 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 { pub fn to_upper(self: *Self, _: Context) Result {
const b = try self.buf_for_update(); const b = try self.buf_for_update();
const root = try self.with_cursels_mut_once(b.root, to_upper_cursel, b.allocator); 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" }; 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 { fn to_lower_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root {
var root = root_; return transform_cursel(Buffer.unicode.to_lower, self, root_, cursel, allocator);
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;
} }
pub fn to_lower(self: *Self, _: Context) Result { 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" }; 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 { fn toggle_case_cursel(self: *Self, root_: Buffer.Root, cursel: *CurSel, allocator: Allocator) error{Stop}!Buffer.Root {
var root = root_; return transform_cursel(Buffer.unicode.toggle_case, self, root_, cursel, allocator);
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;
} }
pub fn toggle_case(self: *Self, _: Context) Result { pub fn toggle_case(self: *Self, _: Context) Result {