feat(buffers): move buffer lifetime management to new Buffer.Manager module

This commit is contained in:
CJ van den Berg 2025-01-21 21:42:36 +01:00
parent fbeaefe7ff
commit aa1e0674cc
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
6 changed files with 109 additions and 48 deletions

View file

@ -10,6 +10,7 @@ const max_imbalance = 7;
pub const Root = *const Node;
pub const unicode = @import("unicode.zig");
pub const Manager = @import("Manager.zig");
pub const Cursor = @import("Cursor.zig");
pub const View = @import("View.zig");
pub const Selection = @import("Selection.zig");

79
src/buffer/Manager.zig Normal file
View file

@ -0,0 +1,79 @@
const std = @import("std");
const Buffer = @import("Buffer.zig");
const Self = @This();
allocator: std.mem.Allocator,
buffers: std.StringHashMapUnmanaged(*Buffer),
pub fn init(allocator: std.mem.Allocator) Self {
return .{
.allocator = allocator,
.buffers = .{},
};
}
pub fn deinit(self: *Self) void {
var i = self.buffers.iterator();
while (i.next()) |p| {
self.allocator.free(p.key_ptr.*);
p.value_ptr.*.deinit();
}
self.buffers.deinit(self.allocator);
}
pub fn open_file(self: *Self, file_path: []const u8) Buffer.LoadFromFileError!*Buffer {
if (self.buffers.get(file_path)) |buffer| {
return buffer;
} else {
var buffer = try Buffer.create(self.allocator);
errdefer buffer.deinit();
try buffer.load_from_file_and_update(file_path);
try self.buffers.put(self.allocator, try self.allocator.dupe(u8, file_path), buffer);
return buffer;
}
}
pub fn open_scratch(self: *Self, file_path: []const u8, content: []const u8) Buffer.LoadFromStringError!*Buffer {
if (self.buffers.get(file_path)) |buffer| {
return buffer;
} else {
var buffer = try Buffer.create(self.allocator);
errdefer buffer.deinit();
try buffer.load_from_string_and_update(file_path, content);
buffer.file_exists = true;
try self.buffers.put(self.allocator, try self.allocator.dupe(u8, file_path), buffer);
return buffer;
}
}
pub fn retire(self: *Self, buffer: *Buffer) void {
_ = self;
_ = buffer;
}
pub fn list(self: *Self, allocator: std.mem.Allocator) []*const Buffer {
_ = self;
_ = allocator;
unreachable;
}
pub fn is_dirty(self: *const Self) bool {
var i = self.buffers.iterator();
while (i.next()) |kv|
if (kv.value_ptr.*.is_dirty())
return true;
return false;
}
pub fn is_buffer_dirty(self: *const Self, file_path: []const u8) bool {
return if (self.buffers.get(file_path)) |buffer| buffer.is_dirty() else false;
}
pub fn save_all(self: *const Self) Buffer.StoreToFileError!void {
var i = self.buffers.iterator();
while (i.next()) |kv| {
const buffer = kv.value_ptr.*;
try buffer.store_to_file_and_clean(buffer.file_path);
}
}