refactor: simplify getting the active editor and selection

This commit is contained in:
CJ van den Berg 2024-12-11 20:54:53 +01:00
parent 4b3904d5f2
commit 038ed4da2b
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
10 changed files with 138 additions and 143 deletions

View file

@ -6,7 +6,6 @@ const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const mainview = @import("../../mainview.zig");
const ed = @import("../../editor.zig");
const Allocator = @import("std").mem.Allocator;
@ -28,29 +27,27 @@ history_pos: ?usize = null,
commands: Commands = undefined,
pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } {
if (tui.current().mainview.dynamic_cast(mainview)) |mv_| if (mv_.get_editor()) |editor| {
const self: *Self = try allocator.create(Self);
self.* = .{
.allocator = allocator,
.input = ArrayList(u8).init(allocator),
.last_input = ArrayList(u8).init(allocator),
.start_view = editor.view,
.start_cursor = editor.get_primary().cursor,
.editor = editor,
};
try self.commands.init(self);
if (editor.get_primary().selection) |sel| ret: {
const text = editor.get_selection(sel, self.allocator) catch break :ret;
defer self.allocator.free(text);
try self.input.appendSlice(text);
}
var mode = try keybind.mode("mini/find", allocator, .{
.insert_command = "mini_mode_insert_bytes",
});
mode.event_handler = EventHandler.to_owned(self);
return .{ mode, .{ .name = name } };
const editor = tui.get_active_editor() orelse return error.NotFound;
const self: *Self = try allocator.create(Self);
self.* = .{
.allocator = allocator,
.input = ArrayList(u8).init(allocator),
.last_input = ArrayList(u8).init(allocator),
.start_view = editor.view,
.start_cursor = editor.get_primary().cursor,
.editor = editor,
};
return error.NotFound;
try self.commands.init(self);
if (editor.get_primary().selection) |sel| ret: {
const text = editor.get_selection(sel, self.allocator) catch break :ret;
defer self.allocator.free(text);
try self.input.appendSlice(text);
}
var mode = try keybind.mode("mini/find", allocator, .{
.insert_command = "mini_mode_insert_bytes",
});
mode.event_handler = EventHandler.to_owned(self);
return .{ mode, .{ .name = name } };
}
pub fn deinit(self: *Self) void {

View file

@ -6,7 +6,6 @@ const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const mainview = @import("../../mainview.zig");
const Allocator = @import("std").mem.Allocator;
const eql = @import("std").mem.eql;
@ -21,30 +20,22 @@ buf: [1024]u8 = undefined,
input: []u8 = "",
last_buf: [1024]u8 = undefined,
last_input: []u8 = "",
mainview: *mainview,
commands: Commands = undefined,
pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } {
const self: *Self = try allocator.create(Self);
if (tui.current().mainview.dynamic_cast(mainview)) |mv| {
self.* = .{
.allocator = allocator,
.mainview = mv,
};
try self.commands.init(self);
if (mv.get_editor()) |editor| if (editor.get_primary().selection) |sel| ret: {
const text = editor.get_selection(sel, self.allocator) catch break :ret;
defer self.allocator.free(text);
@memcpy(self.buf[0..text.len], text);
self.input = self.buf[0..text.len];
};
var mode = try keybind.mode("mini/find_in_files", allocator, .{
.insert_command = "mini_mode_insert_bytes",
});
mode.event_handler = EventHandler.to_owned(self);
return .{ mode, .{ .name = name } };
self.* = .{ .allocator = allocator };
try self.commands.init(self);
if (tui.get_active_selection(self.allocator)) |text| {
defer self.allocator.free(text);
@memcpy(self.buf[0..text.len], text);
self.input = self.buf[0..text.len];
}
return error.NotFound;
var mode = try keybind.mode("mini/find_in_files", allocator, .{
.insert_command = "mini_mode_insert_bytes",
});
mode.event_handler = EventHandler.to_owned(self);
return .{ mode, .{ .name = name } };
}
pub fn deinit(self: *Self) void {

View file

@ -8,7 +8,6 @@ const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const mainview = @import("../../mainview.zig");
const Allocator = @import("std").mem.Allocator;
const fmt = @import("std").fmt;
@ -26,19 +25,17 @@ commands: Commands = undefined,
pub fn create(allocator: Allocator, _: command.Context) !struct { tui.Mode, tui.MiniMode } {
const self: *Self = try allocator.create(Self);
if (tui.current().mainview.dynamic_cast(mainview)) |mv_| if (mv_.get_editor()) |editor| {
self.* = .{
.allocator = allocator,
.start = editor.get_primary().cursor.row + 1,
};
try self.commands.init(self);
var mode = try keybind.mode("mini/goto", allocator, .{
.insert_command = "mini_mode_insert_bytes",
});
mode.event_handler = EventHandler.to_owned(self);
return .{ mode, .{ .name = name } };
const editor = tui.get_active_editor() orelse return error.NotFound;
self.* = .{
.allocator = allocator,
.start = editor.get_primary().cursor.row + 1,
};
return error.NotFound;
try self.commands.init(self);
var mode = try keybind.mode("mini/goto", allocator, .{
.insert_command = "mini_mode_insert_bytes",
});
mode.event_handler = EventHandler.to_owned(self);
return .{ mode, .{ .name = name } };
}
pub fn deinit(self: *Self) void {

View file

@ -6,7 +6,6 @@ const command = @import("command");
const EventHandler = @import("EventHandler");
const tui = @import("../../tui.zig");
const mainview = @import("../../mainview.zig");
const Allocator = @import("std").mem.Allocator;
@ -32,7 +31,7 @@ const Operation = enum {
pub fn create(allocator: Allocator, ctx: command.Context) !struct { tui.Mode, tui.MiniMode } {
var right: bool = true;
const select = if (tui.current().mainview.dynamic_cast(mainview)) |mv| if (mv.get_editor()) |editor| if (editor.get_primary().selection) |_| true else false else false else false;
const select = if (tui.get_active_editor()) |editor| if (editor.get_primary().selection) |_| true else false else false;
_ = ctx.args.match(.{tp.extract(&right)}) catch return error.InvalidArgument;
const self: *Self = try allocator.create(Self);
self.* = .{

View file

@ -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 {

View file

@ -11,16 +11,15 @@ pub const Type = @import("file_browser.zig").Create(@This());
pub const create = Type.create;
pub fn load_entries(self: *Type) !void {
if (tui.current().mainview.dynamic_cast(mainview)) |mv_| if (mv_.get_editor()) |editor| {
try self.file_path.appendSlice(editor.file_path orelse "");
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;
try self.file_path.appendSlice(editor.file_path orelse "");
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 {

View file

@ -5,7 +5,6 @@ const syntax = @import("syntax");
const Widget = @import("../../Widget.zig");
const tui = @import("../../tui.zig");
const mainview = @import("../../mainview.zig");
pub const Type = @import("palette.zig").Create(@This());
@ -32,9 +31,9 @@ pub fn load_entries(palette: *Type) !usize {
var longest_hint: usize = 0;
var idx: usize = 0;
previous_file_type = blk: {
if (tui.current().mainview.dynamic_cast(mainview)) |mv_| if (mv_.get_editor()) |editor| {
if (editor.syntax) |editor_syntax| break :blk editor_syntax.file_type.name;
};
if (tui.get_active_editor()) |editor|
if (editor.syntax) |editor_syntax|
break :blk editor_syntax.file_type.name;
break :blk null;
};