From 28178d8871ce9444c8e8b558f15c5f0b4a53e724 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Fri, 22 Mar 2024 20:58:51 +0100 Subject: [PATCH] refactor: store/retrieve project_manager ondemand from actor environment --- src/project_manager.zig | 49 ++++++++++++++++++++++++----------------- src/tui/mainview.zig | 9 +------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/project_manager.zig b/src/project_manager.zig index 31c8e5d..4d746a5 100644 --- a/src/project_manager.zig +++ b/src/project_manager.zig @@ -4,37 +4,45 @@ const cbor = @import("cbor"); const log = @import("log"); const tracy = @import("tracy"); -pid: ?tp.pid, +pid: tp.pid_ref, const Self = @This(); const module_name = @typeName(Self); -pub const Error = error{ OutOfMemory, Exit }; -pub fn create(a: std.mem.Allocator) Error!Self { - return .{ .pid = try Process.create(a) }; +pub fn get() error{Exit}!Self { + const pid = tp.env.get().proc(module_name); + return if (pid.expired()) create() else .{ .pid = pid }; } -pub fn from_pid(pid: tp.pid_ref) Error!Self { - return .{ .pid = pid.clone() }; +fn create() error{Exit}!Self { + const pid = Process.create() catch |e| return tp.exit_error(e); + defer pid.deinit(); + tp.env.get().proc_set(module_name, pid.ref()); + return .{ .pid = tp.env.get().proc(module_name) }; } -pub fn deinit(self: *Self) void { - if (self.pid) |pid| { - self.pid = null; - pid.deinit(); - } +pub fn shutdown() void { + const pid = tp.env.get().proc(module_name); + if (pid.expired()) return; + pid.send(.{"shutdown"}) catch {}; } -pub fn shutdown(self: *Self) void { - if (self.pid) |pid| { - pid.send(.{"shutdown"}) catch {}; - self.deinit(); - } +pub fn open_cwd() tp.result { + var cwd_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; + const cwd = std.fs.cwd().realpath(".", &cwd_buf) catch "(none)"; + return open(cwd); } -pub fn open(self: *const Self, project_directory: []const u8) tp.result { - const pid = if (self.pid) |pid| pid else return tp.exit_error(error.Shutdown); - try pid.send(.{ "open", project_directory }); +pub fn open(project_directory: []const u8) tp.result { + tp.env.get().str_set("project", project_directory); + return (try get()).pid.send(.{ "open", project_directory }); +} + +pub fn request_recent_files() tp.result { + const project = tp.env.get().str("project"); + if (project.len == 0) + return tp.exit("No project"); + return (try get()).pid.send(.{ "request_recent_files", project }); } const Process = struct { @@ -47,7 +55,8 @@ const Process = struct { const Receiver = tp.Receiver(*Process); const ProjectsMap = std.StringHashMap(*Project); - pub fn create(a: std.mem.Allocator) Error!tp.pid { + fn create() !tp.pid { + const a = std.heap.c_allocator; const self = try a.create(Process); self.* = .{ .a = a, diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index ab91f9c..035db41 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -32,7 +32,6 @@ last_match_text: ?[]const u8 = null, logview_enabled: bool = false, location_history: location_history, -project_manager: project_manager, const NavState = struct { time: i64 = 0, @@ -44,12 +43,7 @@ const NavState = struct { }; pub fn create(a: std.mem.Allocator, n: nc.Plane) !Widget { - const project_manager_ = try project_manager.create(a); - tp.env.get().proc_set("project", project_manager_.pid.?.ref()); - var project_name_buf: [std.fs.MAX_PATH_BYTES]u8 = undefined; - const project_name = std.fs.cwd().realpath(".", &project_name_buf) catch "(none)"; - tp.env.get().str_set("project", project_name); - // try project_manager_.open(project_name); + try project_manager.open_cwd(); const self = try a.create(Self); self.* = .{ .a = a, @@ -59,7 +53,6 @@ pub fn create(a: std.mem.Allocator, n: nc.Plane) !Widget { .floating_views = WidgetStack.init(a), .statusbar = undefined, .location_history = try location_history.create(), - .project_manager = project_manager_, }; try self.commands.init(self); const w = Widget.to(self);