feat: add helix mode close other buffers and reload all

* x and x! have expected behaviour in Helix mode
This commit is contained in:
Igor Támara 2025-10-03 00:24:33 -05:00 committed by CJ van den Berg
parent 6196c5fdd8
commit 552417091d
2 changed files with 63 additions and 9 deletions

View file

@ -149,6 +149,17 @@ pub fn save_all(self: *const Self) Buffer.StoreToFileError!void {
}
}
pub fn reload_all(self: *const Self) Buffer.LoadFromFileError!void {
var i = self.buffers.iterator();
while (i.next()) |kv| {
const buffer = kv.value_ptr.*;
if (buffer.is_ephemeral())
buffer.mark_clean()
else
try buffer.refresh_from_file();
}
}
pub fn delete_all(self: *Self) void {
var i = self.buffers.iterator();
while (i.next()) |p| {
@ -158,6 +169,32 @@ pub fn delete_all(self: *Self) void {
self.buffers.clearRetainingCapacity();
}
pub fn delete_others(self: *Self, protected: *Buffer) void {
var i = self.buffers.iterator();
while (i.next()) |p| {
const buffer = p.value_ptr.*;
if (buffer != protected) {
buffer.reset_to_last_saved();
self.close_buffer(buffer);
}
}
}
pub fn close_others(self: *Self, protected: *Buffer) usize {
var removed: usize = 0;
var i = self.buffers.iterator();
if (self.is_dirty()) return 0;
while (i.next()) |p| {
const buffer = p.value_ptr.*;
if (buffer != protected) {
if (self.buffers.remove(buffer.get_file_path()))
removed += 1;
buffer.deinit();
}
}
return removed;
}
pub fn buffer_from_ref(self: *Self, buffer_ref: usize) ?*Buffer {
var i = self.buffers.iterator();
while (i.next()) |p|

View file

@ -60,20 +60,16 @@ const cmds_ = struct {
}
pub const wq_meta: Meta = .{ .description = "wq (write/save file and quit)" };
pub fn @"x!"(_: *void, _: Ctx) Result {
try cmd("save_file", command.fmt(.{ "then", .{ "quit_without_saving", .{} } }));
}
pub const @"x!_meta": Meta = .{ .description = "x! (write/save file and close forcefully, ignoring other unsaved changes)" };
pub fn x(_: *void, _: Ctx) Result {
try cmd("save_file", command.fmt(.{ "then", .{ "quit", .{} } }));
}
pub const x_meta: Meta = .{ .description = "x (write/save file and quit)" };
// This one needs some help, the intention is to close only the current buffer
// , if is the only buffer, exit...
// TODO
// pub fn @"x!"(_: *void, _: Ctx) Result {
// try cmd("save_file", .{});
// try cmd("close_file_without_saving", .{});
// }
// pub const @"x!_meta": Meta = .{ .description = "x! (write/save file and close forcefully, ignoring unsaved changes)" };
pub fn wa(_: *void, _: Ctx) Result {
if (tui.get_buffer_manager()) |bm|
bm.save_all() catch |e| return tp.exit_error(e, @errorReturnTrace());
@ -116,6 +112,12 @@ const cmds_ = struct {
}
pub const rl_meta: Meta = .{ .description = "rl (force reload current file)" };
pub fn rla(_: *void, _: Ctx) Result {
if (tui.get_buffer_manager()) |bm|
bm.reload_all() catch |e| return tp.exit_error(e, @errorReturnTrace());
}
pub const rla_meta: Meta = .{ .description = "rla (reload all files discarding the current contents)" };
pub fn o(_: *void, _: Ctx) Result {
try cmd("open_file", .{});
}
@ -152,6 +154,21 @@ const cmds_ = struct {
}
pub const @"bc!_meta": Meta = .{ .description = "bc! (Close buffer/tab forcefully, ignoring changes)" };
pub fn @"bco!"(_: *void, _: Ctx) Result {
const mv = tui.mainview() orelse return;
if (tui.get_buffer_manager()) |bm| {
if (mv.get_active_buffer()) |buffer| bm.delete_others(buffer);
}
}
pub const @"bco!_meta": Meta = .{ .description = "bco! (Close other buffers/tabs forcefully, ignoring changes)" };
pub fn bco(_: *void, _: Ctx) Result {
const mv = tui.mainview() orelse return;
const bm = tui.get_buffer_manager() orelse return;
if (mv.get_active_buffer()) |buffer| _ = bm.close_others(buffer);
}
pub const bco_meta: Meta = .{ .description = "bco (Close other buffers/tabs, except this one)" };
pub fn save_selection(_: *void, _: Ctx) Result {
const logger = log.logger("helix-mode");
defer logger.deinit();