feat: add file_stack and return to previous file on file close
This commit is contained in:
parent
e3b8d513ea
commit
939a70b249
1 changed files with 28 additions and 2 deletions
|
@ -31,8 +31,8 @@ editor: ?*ed.Editor = null,
|
||||||
panels: ?*WidgetList = null,
|
panels: ?*WidgetList = null,
|
||||||
last_match_text: ?[]const u8 = null,
|
last_match_text: ?[]const u8 = null,
|
||||||
logview_enabled: bool = false,
|
logview_enabled: bool = false,
|
||||||
|
|
||||||
location_history: location_history,
|
location_history: location_history,
|
||||||
|
file_stack: std.ArrayList([]const u8),
|
||||||
|
|
||||||
const NavState = struct {
|
const NavState = struct {
|
||||||
time: i64 = 0,
|
time: i64 = 0,
|
||||||
|
@ -54,6 +54,7 @@ pub fn create(a: std.mem.Allocator, n: nc.Plane) !Widget {
|
||||||
.floating_views = WidgetStack.init(a),
|
.floating_views = WidgetStack.init(a),
|
||||||
.statusbar = undefined,
|
.statusbar = undefined,
|
||||||
.location_history = try location_history.create(),
|
.location_history = try location_history.create(),
|
||||||
|
.file_stack = std.ArrayList([]const u8).init(a),
|
||||||
};
|
};
|
||||||
try self.commands.init(self);
|
try self.commands.init(self);
|
||||||
const w = Widget.to(self);
|
const w = Widget.to(self);
|
||||||
|
@ -72,6 +73,8 @@ pub fn create(a: std.mem.Allocator, n: nc.Plane) !Widget {
|
||||||
|
|
||||||
pub fn deinit(self: *Self, a: std.mem.Allocator) void {
|
pub fn deinit(self: *Self, a: std.mem.Allocator) void {
|
||||||
self.close_all_panel_views();
|
self.close_all_panel_views();
|
||||||
|
for (self.file_stack.items) |file_path| self.a.free(file_path);
|
||||||
|
self.file_stack.deinit();
|
||||||
self.commands.deinit();
|
self.commands.deinit();
|
||||||
self.widgets.deinit(a);
|
self.widgets.deinit(a);
|
||||||
self.floating_views.deinit();
|
self.floating_views.deinit();
|
||||||
|
@ -312,8 +315,11 @@ pub fn handle_editor_event(self: *Self, _: tp.pid_ref, m: tp.message) tp.result
|
||||||
return self.location_update(m);
|
return self.location_update(m);
|
||||||
|
|
||||||
if (try m.match(.{ "E", "close" })) {
|
if (try m.match(.{ "E", "close" })) {
|
||||||
|
if (self.pop_file_stack(editor.file_path)) |file_path| {
|
||||||
|
defer self.a.free(file_path);
|
||||||
|
self.show_previous_async(file_path);
|
||||||
|
} else self.show_home_async();
|
||||||
self.editor = null;
|
self.editor = null;
|
||||||
self.show_home_async();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -392,6 +398,7 @@ pub fn walk(self: *Self, ctx: *anyopaque, f: Widget.WalkFn, w: *Widget) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_editor(self: *Self) tp.result {
|
fn create_editor(self: *Self) tp.result {
|
||||||
|
if (self.editor) |editor| if (editor.file_path) |file_path| self.push_file_stack(file_path) catch {};
|
||||||
self.widgets.replace(0, Widget.empty(self.a, self.plane, .dynamic) catch |e| return tp.exit_error(e));
|
self.widgets.replace(0, Widget.empty(self.a, self.plane, .dynamic) catch |e| return tp.exit_error(e));
|
||||||
command.executeName("enter_mode_default", .{}) catch {};
|
command.executeName("enter_mode_default", .{}) catch {};
|
||||||
var editor_widget = ed.create(self.a, Widget.to(self)) catch |e| return tp.exit_error(e);
|
var editor_widget = ed.create(self.a, Widget.to(self)) catch |e| return tp.exit_error(e);
|
||||||
|
@ -413,6 +420,10 @@ fn toggle_inputview_async(_: *Self) void {
|
||||||
tp.self_pid().send(.{ "cmd", "toggle_inputview" }) catch return;
|
tp.self_pid().send(.{ "cmd", "toggle_inputview" }) catch return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn show_previous_async(_: *Self, file_path: []const u8) void {
|
||||||
|
tp.self_pid().send(.{ "cmd", "navigate", .{ .file = file_path } }) catch return;
|
||||||
|
}
|
||||||
|
|
||||||
fn show_home_async(_: *Self) void {
|
fn show_home_async(_: *Self) void {
|
||||||
tp.self_pid().send(.{ "cmd", "show_home" }) catch return;
|
tp.self_pid().send(.{ "cmd", "show_home" }) catch return;
|
||||||
}
|
}
|
||||||
|
@ -459,3 +470,18 @@ fn normalize_file_path(file_path: []const u8) []const u8 {
|
||||||
return file_path[project.len + 1 ..];
|
return file_path[project.len + 1 ..];
|
||||||
return file_path;
|
return file_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn push_file_stack(self: *Self, file_path: []const u8) !void {
|
||||||
|
for (self.file_stack.items, 0..) |file_path_, i|
|
||||||
|
if (std.mem.eql(u8, file_path, file_path_))
|
||||||
|
self.a.free(self.file_stack.orderedRemove(i));
|
||||||
|
(try self.file_stack.addOne()).* = try self.a.dupe(u8, file_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pop_file_stack(self: *Self, closed: ?[]const u8) ?[]const u8 {
|
||||||
|
if (closed) |file_path|
|
||||||
|
for (self.file_stack.items, 0..) |file_path_, i|
|
||||||
|
if (std.mem.eql(u8, file_path, file_path_))
|
||||||
|
self.a.free(self.file_stack.orderedRemove(i));
|
||||||
|
return self.file_stack.popOrNull();
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue