config: implement clone for RepeatableLink

Fixes #2819
This commit is contained in:
Mitchell Hashimoto
2024-11-26 10:49:45 -08:00
parent e95b2eaace
commit 9171cb5c29
2 changed files with 41 additions and 7 deletions

View File

@ -4610,17 +4610,33 @@ pub const RepeatableLink = struct {
} }
/// Deep copy of the struct. Required by Config. /// Deep copy of the struct. Required by Config.
pub fn clone(self: *const Self, alloc: Allocator) error{}!Self { pub fn clone(
_ = self; self: *const Self,
_ = alloc; alloc: Allocator,
return .{}; ) 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. /// Compare if two of our value are requal. Required by Config.
pub fn equal(self: Self, other: Self) bool { pub fn equal(self: Self, other: Self) bool {
_ = self; const itemsA = self.links.items;
_ = other; const itemsB = other.links.items;
return true; if (itemsA.len != itemsB.len) return false;
for (itemsA, itemsB) |*a, *b| {
if (!a.equal(b)) return false;
} else return true;
} }
/// Used by Formatter /// Used by Formatter

View File

@ -4,6 +4,8 @@
//! action types. //! action types.
const Link = @This(); const Link = @This();
const std = @import("std");
const Allocator = std.mem.Allocator;
const oni = @import("oniguruma"); const oni = @import("oniguruma");
const Mods = @import("key.zig").Mods; const Mods = @import("key.zig").Mods;
@ -59,3 +61,19 @@ pub fn oniRegex(self: *const Link) !oni.Regex {
null, 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);
}