feat: open most recent file on project switch

This commit is contained in:
CJ van den Berg 2024-08-19 22:44:57 +02:00
parent 53310cc2dd
commit bd27db46d1
3 changed files with 27 additions and 0 deletions

View file

@ -140,6 +140,11 @@ pub fn sort_files_by_mtime(self: *Self) void {
std.mem.sort(File, self.files.items, {}, less_fn);
}
pub fn request_most_recent_file(self: *Self, from: tp.pid_ref) error{ OutOfMemory, Exit }!void {
const file_path = if (self.files.items.len > 0) self.files.items[0].path else null;
try from.send(.{file_path});
}
pub fn request_recent_files(self: *Self, from: tp.pid_ref, max: usize) error{ OutOfMemory, Exit }!void {
defer from.send(.{ "PRJ", "recent_done", self.longest_file_path, "" }) catch {};
for (self.files.items, 0..) |file, i| {

View file

@ -45,6 +45,16 @@ pub fn open(rel_project_directory: []const u8) !void {
return (try get()).pid.send(.{ "open", project_directory });
}
pub fn request_most_recent_file(a: std.mem.Allocator) !?[]const u8 {
const project = tp.env.get().str("project");
if (project.len == 0)
return tp.exit("No project");
const rsp = try (try get()).pid.call(a, request_timeout, .{ "request_most_recent_file", project });
defer a.free(rsp.buf);
var file_path: []const u8 = undefined;
return if (try rsp.match(.{tp.extract(&file_path)})) try a.dupe(u8, file_path) else null;
}
pub fn request_recent_files(max: usize) !void {
const project = tp.env.get().str("project");
if (project.len == 0)
@ -225,6 +235,8 @@ const Process = struct {
self.logger.print_err("lsp-handling", "child '{s}' terminated", .{path});
} else if (try m.match(.{ "open", tp.extract(&project_directory) })) {
self.open(project_directory) catch |e| return from.forward_error(e, @errorReturnTrace());
} else if (try m.match(.{ "request_most_recent_file", tp.extract(&project_directory) })) {
self.request_most_recent_file(from, project_directory) catch |e| return from.forward_error(e, @errorReturnTrace());
} else if (try m.match(.{ "request_recent_files", tp.extract(&project_directory), tp.extract(&max) })) {
self.request_recent_files(from, project_directory, max) catch |e| return from.forward_error(e, @errorReturnTrace());
} else if (try m.match(.{ "request_recent_projects", tp.extract(&project_directory) })) {
@ -286,6 +298,12 @@ const Process = struct {
});
}
fn request_most_recent_file(self: *Process, from: tp.pid_ref, project_directory: []const u8) error{ OutOfMemory, Exit }!void {
const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project");
project.sort_files_by_mtime();
return project.request_most_recent_file(from);
}
fn request_recent_files(self: *Process, from: tp.pid_ref, project_directory: []const u8, max: usize) error{ OutOfMemory, Exit }!void {
const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project");
project.sort_files_by_mtime();

View file

@ -258,6 +258,10 @@ const cmds = struct {
const project = tp.env.get().str("project");
tui.current().rdr.set_terminal_working_directory(project);
_ = try self.statusbar.msg(.{ "PRJ", "open" });
if (try project_manager.request_most_recent_file(self.a)) |file_path| {
defer self.a.free(file_path);
try tp.self_pid().send(.{ "cmd", "navigate", .{ .file = file_path } });
}
}
pub fn navigate(self: *Self, ctx: Ctx) Result {