feat(buffers): add support for ephemeral buffers
Ephemeral buffers are not hidden and kept when closed. Ephemeral buffers can be turned into regular buffers by saving them with save_as.
This commit is contained in:
parent
8062923068
commit
939537ed84
7 changed files with 92 additions and 18 deletions
|
@ -82,6 +82,11 @@ pub fn State(ctx_type: type) type {
|
|||
allocator.destroy(self);
|
||||
}
|
||||
|
||||
pub fn update_label(self: *Self, label: []const u8) error{OutOfMemory}!void {
|
||||
self.allocator.free(self.opts.label);
|
||||
self.opts.label = try self.allocator.dupe(u8, label);
|
||||
}
|
||||
|
||||
pub fn layout(self: *Self) Widget.Layout {
|
||||
return self.opts.on_layout(&self.opts.ctx, self);
|
||||
}
|
||||
|
|
|
@ -537,7 +537,13 @@ pub const Editor = struct {
|
|||
else
|
||||
syntax.create_guess_file_type(self.allocator, content.items, self.file_path) catch null;
|
||||
if (syn) |syn_|
|
||||
project_manager.did_open(file_path, syn_.file_type, self.lsp_version, try content.toOwnedSlice()) catch |e|
|
||||
project_manager.did_open(
|
||||
file_path,
|
||||
syn_.file_type,
|
||||
self.lsp_version,
|
||||
try content.toOwnedSlice(),
|
||||
new_buf.is_ephemeral(),
|
||||
) catch |e|
|
||||
self.logger.print("project_manager.did_open failed: {any}", .{e});
|
||||
break :syntax syn;
|
||||
};
|
||||
|
@ -563,6 +569,7 @@ pub const Editor = struct {
|
|||
|
||||
fn save(self: *Self) !void {
|
||||
const b = self.buffer orelse return error.Stop;
|
||||
if (b.is_ephemeral()) return self.logger.print_err("save", "ephemeral buffer, use save as", .{});
|
||||
if (!b.is_dirty()) return self.logger.print("no changes to save", .{});
|
||||
if (self.file_path) |file_path| {
|
||||
if (self.buffer) |b_mut| try b_mut.store_to_file_and_clean(file_path);
|
||||
|
@ -3785,23 +3792,24 @@ pub const Editor = struct {
|
|||
pub const save_file_as_meta = .{ .arguments = &.{.string} };
|
||||
|
||||
pub fn close_file(self: *Self, _: Context) Result {
|
||||
const buffer_ = self.buffer;
|
||||
if (buffer_) |buffer| if (buffer.is_dirty())
|
||||
return tp.exit("unsaved changes");
|
||||
self.cancel_all_selections();
|
||||
if (self.buffer) |buffer| {
|
||||
if (buffer.is_dirty())
|
||||
return tp.exit("unsaved changes");
|
||||
buffer.hidden = true;
|
||||
}
|
||||
try self.close();
|
||||
if (buffer_) |buffer|
|
||||
self.buffer_manager.close_buffer(buffer);
|
||||
}
|
||||
pub const close_file_meta = .{ .description = "Close file" };
|
||||
|
||||
pub fn close_file_without_saving(self: *Self, _: Context) Result {
|
||||
self.cancel_all_selections();
|
||||
if (self.buffer) |buffer| {
|
||||
const buffer_ = self.buffer;
|
||||
if (buffer_) |buffer|
|
||||
buffer.reset_to_last_saved();
|
||||
buffer.hidden = true;
|
||||
}
|
||||
try self.close();
|
||||
if (buffer_) |buffer|
|
||||
self.buffer_manager.close_buffer(buffer);
|
||||
}
|
||||
pub const close_file_without_saving_meta = .{ .description = "Close file without saving" };
|
||||
|
||||
|
@ -4781,7 +4789,13 @@ pub const Editor = struct {
|
|||
try root.store(content.writer(), try self.buf_eol_mode());
|
||||
const syn = syntax.create_file_type(self.allocator, file_type) catch null;
|
||||
if (syn) |syn_| if (self.file_path) |file_path|
|
||||
project_manager.did_open(file_path, syn_.file_type, self.lsp_version, try content.toOwnedSlice()) catch |e|
|
||||
project_manager.did_open(
|
||||
file_path,
|
||||
syn_.file_type,
|
||||
self.lsp_version,
|
||||
try content.toOwnedSlice(),
|
||||
if (self.buffer) |p| p.is_ephemeral() else true,
|
||||
) catch |e|
|
||||
self.logger.print("project_manager.did_open failed: {any}", .{e});
|
||||
break :syntax syn;
|
||||
};
|
||||
|
|
|
@ -412,6 +412,22 @@ const cmds = struct {
|
|||
}
|
||||
pub const delete_buffer_meta = .{ .arguments = &.{.string} };
|
||||
|
||||
pub fn close_buffer(self: *Self, ctx: Ctx) Result {
|
||||
var file_path: []const u8 = undefined;
|
||||
if (!(ctx.args.match(.{tp.extract(&file_path)}) catch false))
|
||||
return error.InvalidDeleteBufferArgument;
|
||||
const buffer = self.buffer_manager.get_buffer_for_file(file_path) orelse return;
|
||||
if (buffer.is_dirty())
|
||||
return tp.exit("unsaved changes");
|
||||
if (self.get_active_editor()) |editor| if (editor.buffer == buffer) {
|
||||
editor.close_file(.{}) catch |e| return e;
|
||||
return;
|
||||
};
|
||||
_ = self.buffer_manager.close_buffer(buffer);
|
||||
tui.need_render();
|
||||
}
|
||||
pub const close_buffer_meta = .{ .arguments = &.{.string} };
|
||||
|
||||
pub fn restore_session(self: *Self, _: Ctx) Result {
|
||||
if (tp.env.get().str("project").len == 0) {
|
||||
try open_project_cwd(self, .{});
|
||||
|
@ -859,16 +875,17 @@ pub fn location_update(self: *Self, m: tp.message) tp.result {
|
|||
var row: usize = 0;
|
||||
var col: usize = 0;
|
||||
const file_path = self.get_active_file_path() orelse return;
|
||||
const ephemeral = if (self.get_active_buffer()) |buffer| buffer.is_ephemeral() else false;
|
||||
|
||||
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 {};
|
||||
project_manager.update_mru(file_path, row, col, ephemeral) 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) })) {
|
||||
project_manager.update_mru(file_path, row, col) catch {};
|
||||
project_manager.update_mru(file_path, row, col, ephemeral) catch {};
|
||||
return self.location_history.update(file_path, .{ .row = row + 1, .col = col + 1 }, sel);
|
||||
}
|
||||
}
|
||||
|
@ -911,6 +928,10 @@ pub fn get_active_file_path(self: *Self) ?[]const u8 {
|
|||
return if (self.get_active_editor()) |editor| editor.file_path orelse null else null;
|
||||
}
|
||||
|
||||
pub fn get_active_buffer(self: *Self) ?*Buffer {
|
||||
return if (self.get_active_editor()) |editor| editor.buffer orelse null else null;
|
||||
}
|
||||
|
||||
pub fn walk(self: *Self, ctx: *anyopaque, f: Widget.WalkFn, w: *Widget) bool {
|
||||
return self.floating_views.walk(ctx, f) or self.widgets.walk(ctx, f, &self.widgets_widget) or f(ctx, w);
|
||||
}
|
||||
|
|
|
@ -117,6 +117,8 @@ const TabBar = struct {
|
|||
try self.widget_list.add(try self.make_spacer());
|
||||
}
|
||||
try self.widget_list.add(tab.widget);
|
||||
if (tab.widget.dynamic_cast(Button.State(Tab))) |btn|
|
||||
try btn.update_label(Tab.name_from_buffer(tab.buffer));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -212,7 +214,7 @@ const Tab = struct {
|
|||
}
|
||||
|
||||
fn on_click2(self: *@This(), _: *Button.State(@This())) void {
|
||||
tp.self_pid().send(.{ "cmd", "delete_buffer", .{self.buffer.file_path} }) catch {};
|
||||
tp.self_pid().send(.{ "cmd", "close_buffer", .{self.buffer.file_path} }) catch {};
|
||||
}
|
||||
|
||||
fn render(self: *@This(), btn: *Button.State(@This()), theme: *const Widget.Theme) bool {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue