diff --git a/src/Project.zig b/src/Project.zig index aa41570..7d39f8c 100644 --- a/src/Project.zig +++ b/src/Project.zig @@ -17,6 +17,8 @@ const Self = @This(); const File = struct { path: []const u8, mtime: i128, + row: usize = 0, + col: usize = 0, }; pub fn init(a: std.mem.Allocator, name: []const u8) error{OutOfMemory}!Self { @@ -109,7 +111,32 @@ pub fn query_recent_files(self: *Self, from: tp.pid_ref, max: usize, query: []co return i; } -pub fn did_open(self: *Self, file_path: []const u8, file_type: []const u8, language_server: []const u8, version: usize, text: []const u8) tp.result { +pub fn update_mru(self: *Self, file_path: []const u8, row: usize, col: usize) !void { + defer self.sort_files_by_mtime(); + for (self.files.items) |*file| { + if (!std.mem.eql(u8, file.path, file_path)) continue; + file.mtime = std.time.nanoTimestamp(); + if (row != 0) { + file.row = row; + file.col = col; + } + return; + } + return self.add_file(file_path, std.time.nanoTimestamp()); +} + +pub fn get_mru_position(self: *Self, from: tp.pid_ref, file_path: []const u8) !void { + for (self.files.items) |*file| { + if (!std.mem.eql(u8, file.path, file_path)) continue; + if (file.row != 0) + try from.send(.{ "cmd", "goto", .{ file.row, file.col } }); + return; + } +} + +pub fn did_open(self: *Self, from: tp.pid_ref, file_path: []const u8, file_type: []const u8, language_server: []const u8, version: usize, text: []const u8) tp.result { + self.update_mru(file_path, 0, 0) catch {}; + self.get_mru_position(from, file_path) catch {}; const lsp = self.get_lsp(language_server) catch |e| return tp.exit_error(e); if (!self.file_language_server.contains(file_path)) { const key = self.a.dupe(u8, file_path) catch |e| return tp.exit_error(e); diff --git a/src/project_manager.zig b/src/project_manager.zig index a5bc84c..05370d8 100644 --- a/src/project_manager.zig +++ b/src/project_manager.zig @@ -73,6 +73,20 @@ pub fn goto_definition(file_path: []const u8, row: usize, col: usize) tp.result return (try get()).pid.send(.{ "goto_definition", project, file_path, row, col }); } +pub fn update_mru(file_path: []const u8, row: usize, col: usize) tp.result { + const project = tp.env.get().str("project"); + if (project.len == 0) + return tp.exit("No project"); + return (try get()).pid.send(.{ "update_mru", project, file_path, row, col }); +} + +pub fn get_mru_position(file_path: []const u8) tp.result { + const project = tp.env.get().str("project"); + if (project.len == 0) + return tp.exit("No project"); + return (try get()).pid.send(.{ "get_mru_position", project, file_path }); +} + const Process = struct { a: std.mem.Allocator, parent: tp.pid, @@ -146,6 +160,8 @@ const Process = struct { project.files.items.len, std.time.milliTimestamp() - project.open_time, }); + } else if (try m.match(.{ "update_mru", tp.extract(&project_directory), tp.extract(&path), tp.extract(&row), tp.extract(&col) })) { + self.update_mru(project_directory, path, row, col) catch |e| return from.forward_error(e); } else if (try m.match(.{ "open", tp.extract(&project_directory) })) { self.open(project_directory) catch |e| return from.forward_error(e); } else if (try m.match(.{ "request_recent_files", tp.extract(&project_directory), tp.extract(&max) })) { @@ -154,9 +170,11 @@ const Process = struct { self.query_recent_files(from, project_directory, max, query) catch |e| return from.forward_error(e); } else if (try m.match(.{ "did_open", tp.extract(&project_directory), tp.extract(&path), tp.extract(&file_type), tp.extract_cbor(&language_server), tp.extract(&version), tp.extract(&text_ptr), tp.extract(&text_len) })) { const text = if (text_len > 0) @as([*]const u8, @ptrFromInt(text_ptr))[0..text_len] else ""; - self.did_open(project_directory, path, file_type, language_server, version, text) catch |e| return from.forward_error(e); + self.did_open(from, project_directory, path, file_type, language_server, version, text) catch |e| return from.forward_error(e); } else if (try m.match(.{ "goto_definition", tp.extract(&project_directory), tp.extract(&path), tp.extract(&row), tp.extract(&col) })) { self.goto_definition(from, project_directory, path, row, col) catch |e| return from.forward_error(e); + } else if (try m.match(.{ "get_mru_position", tp.extract(&project_directory), tp.extract(&path) })) { + self.get_mru_position(from, project_directory, path) catch |e| return from.forward_error(e); } else if (try m.match(.{"shutdown"})) { if (self.walker) |pid| pid.send(.{"stop"}) catch {}; try from.send(.{ "project_manager", "shutdown" }); @@ -193,11 +211,11 @@ const Process = struct { // self.logger.print("queried: {s} for {s} match {d} in {d} ms", .{ project_directory, query, matched, std.time.milliTimestamp() - start_time }); } - fn did_open(self: *Process, project_directory: []const u8, file_path: []const u8, file_type: []const u8, language_server: []const u8, version: usize, text: []const u8) tp.result { + fn did_open(self: *Process, from: tp.pid_ref, project_directory: []const u8, file_path: []const u8, file_type: []const u8, language_server: []const u8, version: usize, text: []const u8) tp.result { const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".did_open" }); defer frame.deinit(); const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project"); - return project.did_open(file_path, file_type, language_server, version, text); + return project.did_open(from, file_path, file_type, language_server, version, text); } fn goto_definition(self: *Process, from: tp.pid_ref, project_directory: []const u8, file_path: []const u8, row: usize, col: usize) tp.result { @@ -206,6 +224,18 @@ const Process = struct { const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project"); return project.goto_definition(from, file_path, row, col) catch |e| tp.exit_error(e); } + + fn get_mru_position(self: *Process, from: tp.pid_ref, project_directory: []const u8, file_path: []const u8) tp.result { + const frame = tracy.initZone(@src(), .{ .name = module_name ++ ".get_mru_position" }); + defer frame.deinit(); + const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project"); + return project.get_mru_position(from, file_path) catch |e| tp.exit_error(e); + } + + fn update_mru(self: *Process, project_directory: []const u8, file_path: []const u8, row: usize, col: usize) tp.result { + const project = if (self.projects.get(project_directory)) |p| p else return tp.exit("No project"); + return project.update_mru(file_path, row, col) catch |e| tp.exit_error(e); + } }; fn walk_tree_async(a_: std.mem.Allocator, root_path_: []const u8) error{Exit}!tp.pid { diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 4187833..63e5084 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -204,11 +204,12 @@ const cmds = struct { } if (line) |l| { try command.executeName("goto_line", command.fmt(.{l})); + if (!same_file) + try command.executeName("scroll_view_center", .{}); } if (column) |col| { try command.executeName("goto_column", command.fmt(.{col})); } - try command.executeName("scroll_view_center", .{}); tui.need_render(); } @@ -300,12 +301,15 @@ pub fn location_update(self: *Self, m: tp.message) tp.result { if (try m.match(.{ tp.any, tp.any, tp.any, tp.extract(&row), tp.extract(&col) })) { if (row == 0 and col == 0) return; + project_manager.update_mru(file_path, row, col) catch {}; return self.location_history.update(file_path, .{ .row = row + 1, .col = col + 1 }, null); } var sel: location_history.Selection = .{}; - if (try m.match(.{ tp.any, tp.any, tp.any, tp.extract(&row), tp.extract(&col), tp.extract(&sel.begin.row), tp.extract(&sel.begin.col), tp.extract(&sel.end.row), tp.extract(&sel.end.col) })) + if (try m.match(.{ tp.any, tp.any, tp.any, tp.extract(&row), tp.extract(&col), tp.extract(&sel.begin.row), tp.extract(&sel.begin.col), tp.extract(&sel.end.row), tp.extract(&sel.end.col) })) { + project_manager.update_mru(file_path, row, col) catch {}; return self.location_history.update(file_path, .{ .row = row + 1, .col = col + 1 }, sel); + } } fn location_jump(from: tp.pid_ref, file_path: []const u8, cursor: location_history.Cursor, selection: ?location_history.Selection) void {