refactor: simplify getting the active editor and selection
This commit is contained in:
parent
4b3904d5f2
commit
038ed4da2b
10 changed files with 138 additions and 143 deletions
|
@ -4,26 +4,24 @@ const root = @import("root");
|
|||
const command = @import("command");
|
||||
|
||||
const tui = @import("../../tui.zig");
|
||||
const mainview = @import("../../mainview.zig");
|
||||
|
||||
pub const Type = @import("file_browser.zig").Create(@This());
|
||||
|
||||
pub const create = Type.create;
|
||||
|
||||
pub fn load_entries(self: *Type) error{ Exit, OutOfMemory }!void {
|
||||
if (tui.current().mainview.dynamic_cast(mainview)) |mv_| if (mv_.get_editor()) |editor| {
|
||||
if (editor.is_dirty()) return tp.exit("unsaved changes");
|
||||
if (editor.file_path) |old_path|
|
||||
if (std.mem.lastIndexOf(u8, old_path, "/")) |pos|
|
||||
try self.file_path.appendSlice(old_path[0 .. pos + 1]);
|
||||
if (editor.get_primary().selection) |sel| ret: {
|
||||
const text = editor.get_selection(sel, self.allocator) catch break :ret;
|
||||
defer self.allocator.free(text);
|
||||
if (!(text.len > 2 and std.mem.eql(u8, text[0..2], "..")))
|
||||
self.file_path.clearRetainingCapacity();
|
||||
try self.file_path.appendSlice(text);
|
||||
}
|
||||
};
|
||||
const editor = tui.get_active_editor() orelse return;
|
||||
if (editor.is_dirty()) return tp.exit("unsaved changes");
|
||||
if (editor.file_path) |old_path|
|
||||
if (std.mem.lastIndexOf(u8, old_path, "/")) |pos|
|
||||
try self.file_path.appendSlice(old_path[0 .. pos + 1]);
|
||||
if (editor.get_primary().selection) |sel| ret: {
|
||||
const text = editor.get_selection(sel, self.allocator) catch break :ret;
|
||||
defer self.allocator.free(text);
|
||||
if (!(text.len > 2 and std.mem.eql(u8, text[0..2], "..")))
|
||||
self.file_path.clearRetainingCapacity();
|
||||
try self.file_path.appendSlice(text);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(_: *Type) []const u8 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue