feat(buffers): save and restore editor metadata on buffer switch
This commit is contained in:
parent
ecca2d0b4c
commit
b45b5910ee
3 changed files with 20 additions and 8 deletions
|
@ -62,7 +62,8 @@ pub fn delete_buffer(self: *Self, file_path: []const u8) bool {
|
|||
return did_remove;
|
||||
}
|
||||
|
||||
pub fn retire(_: *Self, buffer: *Buffer) void {
|
||||
pub fn retire(_: *Self, buffer: *Buffer, meta: ?[]const u8) void {
|
||||
if (meta) |buf| buffer.set_meta(buf) catch {};
|
||||
tp.trace(tp.channel.debug, .{ "buffer", "retire", buffer.file_path, "hidden", buffer.hidden, "ephemeral", buffer.ephemeral });
|
||||
}
|
||||
|
||||
|
|
|
@ -367,7 +367,7 @@ pub const Editor = struct {
|
|||
try self.get_primary().cursor.write(writer);
|
||||
}
|
||||
|
||||
pub fn extract_state(self: *Self, buf: []const u8) !void {
|
||||
pub fn extract_state(self: *Self, buf: []const u8, comptime op: enum { none, open_file }) !void {
|
||||
var file_path: []const u8 = undefined;
|
||||
var view_cbor: []const u8 = undefined;
|
||||
var primary_cbor: []const u8 = undefined;
|
||||
|
@ -383,6 +383,7 @@ pub const Editor = struct {
|
|||
tp.extract_cbor(&primary_cbor),
|
||||
}))
|
||||
return error.RestoreStateMatch;
|
||||
if (op == .open_file)
|
||||
try self.open(file_path);
|
||||
self.clipboard = if (clipboard.len > 0) try self.allocator.dupe(u8, clipboard) else null;
|
||||
self.last_find_query = if (query.len > 0) try self.allocator.dupe(u8, clipboard) else null;
|
||||
|
@ -430,6 +431,9 @@ pub const Editor = struct {
|
|||
}
|
||||
|
||||
fn deinit(self: *Self) void {
|
||||
var meta = std.ArrayList(u8).init(self.allocator);
|
||||
defer meta.deinit();
|
||||
self.write_state(meta.writer()) catch {};
|
||||
for (self.diagnostics.items) |*d| d.deinit(self.diagnostics.allocator);
|
||||
self.diagnostics.deinit();
|
||||
if (self.syntax) |syn| syn.destroy();
|
||||
|
@ -437,7 +441,7 @@ pub const Editor = struct {
|
|||
self.matches.deinit();
|
||||
self.handlers.deinit();
|
||||
self.logger.deinit();
|
||||
if (self.buffer) |p| self.buffer_manager.retire(p);
|
||||
if (self.buffer) |p| self.buffer_manager.retire(p, meta.items);
|
||||
if (self.case_data) |cd| cd.deinit();
|
||||
}
|
||||
|
||||
|
@ -512,7 +516,7 @@ pub const Editor = struct {
|
|||
}
|
||||
|
||||
fn open_buffer(self: *Self, file_path: []const u8, new_buf: *Buffer, file_type: ?[]const u8) !void {
|
||||
errdefer self.buffer_manager.retire(new_buf);
|
||||
errdefer self.buffer_manager.retire(new_buf, null);
|
||||
self.cancel_all_selections();
|
||||
self.get_primary().reset();
|
||||
self.file_path = try self.allocator.dupe(u8, file_path);
|
||||
|
@ -553,11 +557,17 @@ pub const Editor = struct {
|
|||
const ftn = if (self.syntax) |syn| syn.file_type.name else "text";
|
||||
const fti = if (self.syntax) |syn| syn.file_type.icon else "🖹";
|
||||
const ftc = if (self.syntax) |syn| syn.file_type.color else 0x000000;
|
||||
|
||||
if (self.buffer) |buffer| if (buffer.get_meta()) |meta|
|
||||
try self.extract_state(meta, .none);
|
||||
try self.send_editor_open(file_path, new_buf.file_exists, ftn, fti, ftc);
|
||||
}
|
||||
|
||||
fn close(self: *Self) !void {
|
||||
if (self.buffer) |b_mut| self.buffer_manager.retire(b_mut);
|
||||
var meta = std.ArrayList(u8).init(self.allocator);
|
||||
defer meta.deinit();
|
||||
self.write_state(meta.writer()) catch {};
|
||||
if (self.buffer) |b_mut| self.buffer_manager.retire(b_mut, meta.items);
|
||||
self.buffer = null;
|
||||
self.plane.erase();
|
||||
self.plane.home();
|
||||
|
|
|
@ -341,6 +341,7 @@ const cmds = struct {
|
|||
|
||||
const f = project_manager.normalize_file_path(file orelse return);
|
||||
const same_file = if (self.get_active_file_path()) |fp| std.mem.eql(u8, fp, f) else false;
|
||||
const have_editor_metadata = if (self.buffer_manager.get_buffer_for_file(f)) |_| true else false;
|
||||
|
||||
if (!same_file) {
|
||||
if (self.get_active_editor()) |editor| {
|
||||
|
@ -358,7 +359,7 @@ const cmds = struct {
|
|||
if (column) |col|
|
||||
try command.executeName("goto_column", command.fmt(.{col}));
|
||||
} else {
|
||||
if (!same_file)
|
||||
if (!same_file and !have_editor_metadata)
|
||||
try project_manager.get_mru_position(f);
|
||||
}
|
||||
tui.need_render();
|
||||
|
@ -1053,7 +1054,7 @@ fn read_restore_info(self: *Self) !void {
|
|||
var buf = try self.allocator.alloc(u8, @intCast(stat.size));
|
||||
defer self.allocator.free(buf);
|
||||
const size = try file.readAll(buf);
|
||||
try editor.extract_state(buf[0..size]);
|
||||
try editor.extract_state(buf[0..size], .open_file);
|
||||
}
|
||||
|
||||
fn get_next_mru_buffer(self: *Self) ?[]const u8 {
|
||||
|
|
Loading…
Add table
Reference in a new issue