refactor: make get_next_mru_buffer_for_view more versatile

This commit is contained in:
CJ van den Berg 2026-01-14 21:01:35 +01:00
parent d3ae5e0e09
commit 5dbd396365
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -1484,7 +1484,7 @@ pub fn handle_editor_event(self: *Self, editor: *ed.Editor, m: tp.message) tp.re
if (try m.match(.{ "E", "close" })) { if (try m.match(.{ "E", "close" })) {
if (!self.closing_project) { if (!self.closing_project) {
if (self.get_next_mru_buffer_same_view_only(.non_hidden)) |file_path| if (self.get_next_mru_buffer_for_view(self.active_view, .non_hidden)) |file_path|
self.show_file_async(file_path) self.show_file_async(file_path)
else { else {
if (self.views.widgets.items.len == 1) if (self.views.widgets.items.len == 1)
@ -1629,6 +1629,12 @@ pub fn get_view_for_file(self: *Self, file_path: []const u8) ?usize {
return null; return null;
} }
pub fn get_file_for_view(self: *Self, view_: usize) ?[]const u8 {
const view = self.views.get_at(view_) orelse return null;
const editor = view.widget.get("editor") orelse return null;
return if (editor.dynamic_cast(ed.EditorWidget)) |p| p.editor.file_path else null;
}
pub fn get_active_file_path(self: *Self) ?[]const u8 { pub fn get_active_file_path(self: *Self) ?[]const u8 {
return if (self.get_active_editor()) |editor| editor.file_path orelse null else null; return if (self.get_active_editor()) |editor| editor.file_path orelse null else null;
} }
@ -1927,19 +1933,19 @@ fn send_buffer_did_open(allocator: std.mem.Allocator, buffer: *Buffer) !void {
project_manager.request_vcs_id(buffer.get_file_path()) catch {}; project_manager.request_vcs_id(buffer.get_file_path()) catch {};
} }
fn get_next_mru_buffer_same_view_only(self: *Self, mode: enum { all, hidden, non_hidden }) ?[]const u8 { fn get_next_mru_buffer_for_view(self: *Self, view: usize, mode: enum { all, hidden, non_hidden }) ?[]const u8 {
const buffers = self.buffer_manager.list_most_recently_used(self.allocator) catch return null; const buffers = self.buffer_manager.list_most_recently_used(self.allocator) catch return null;
defer self.allocator.free(buffers); defer self.allocator.free(buffers);
const active_file_path = self.get_active_file_path(); const file_path = self.get_file_for_view(view);
for (buffers) |buffer| { for (buffers) |buffer| {
if (active_file_path) |fp| if (std.mem.eql(u8, fp, buffer.get_file_path())) if (file_path) |fp| if (std.mem.eql(u8, fp, buffer.get_file_path()))
continue; continue;
if (switch (mode) { if (switch (mode) {
.all => false, .all => false,
.hidden => !buffer.hidden, .hidden => !buffer.hidden,
.non_hidden => buffer.hidden, .non_hidden => buffer.hidden,
}) continue; }) continue;
if (buffer.get_last_view() != self.active_view) if (buffer.get_last_view() != view)
continue; continue;
return buffer.get_file_path(); return buffer.get_file_path();
} }