From 9171cb5c295614a71d50f399cee6eb048f37ca0d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 26 Nov 2024 10:49:45 -0800 Subject: [PATCH] config: implement `clone` for RepeatableLink Fixes #2819 --- src/config/Config.zig | 30 +++++++++++++++++++++++------- src/input/Link.zig | 18 ++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index 253e96420..bf29994c3 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -4610,17 +4610,33 @@ pub const RepeatableLink = struct { } /// Deep copy of the struct. Required by Config. - pub fn clone(self: *const Self, alloc: Allocator) error{}!Self { - _ = self; - _ = alloc; - return .{}; + pub fn clone( + self: *const Self, + alloc: Allocator, + ) Allocator.Error!Self { + // Note: we don't do any errdefers below since the allocation + // is expected to be arena allocated. + + var list = try std.ArrayListUnmanaged(inputpkg.Link).initCapacity( + alloc, + self.links.items.len, + ); + for (self.links.items) |item| { + const copy = try item.clone(alloc); + list.appendAssumeCapacity(copy); + } + + return .{ .links = list }; } /// Compare if two of our value are requal. Required by Config. pub fn equal(self: Self, other: Self) bool { - _ = self; - _ = other; - return true; + const itemsA = self.links.items; + const itemsB = other.links.items; + if (itemsA.len != itemsB.len) return false; + for (itemsA, itemsB) |*a, *b| { + if (!a.equal(b)) return false; + } else return true; } /// Used by Formatter diff --git a/src/input/Link.zig b/src/input/Link.zig index adc52a270..37b45dbd1 100644 --- a/src/input/Link.zig +++ b/src/input/Link.zig @@ -4,6 +4,8 @@ //! action types. const Link = @This(); +const std = @import("std"); +const Allocator = std.mem.Allocator; const oni = @import("oniguruma"); const Mods = @import("key.zig").Mods; @@ -59,3 +61,19 @@ pub fn oniRegex(self: *const Link) !oni.Regex { null, ); } + +/// Deep clone the link. +pub fn clone(self: *const Link, alloc: Allocator) Allocator.Error!Link { + return .{ + .regex = try alloc.dupe(u8, self.regex), + .action = self.action, + .highlight = self.highlight, + }; +} + +/// Check if two links are equal. +pub fn equal(self: *const Link, other: *const Link) bool { + return std.meta.eql(self.action, other.action) and + std.meta.eql(self.highlight, other.highlight) and + std.mem.eql(u8, self.regex, other.regex); +}