diff --git a/src/tui/filelist_view.zig b/src/tui/filelist_view.zig index 5c999ca..4ca0a54 100644 --- a/src/tui/filelist_view.zig +++ b/src/tui/filelist_view.zig @@ -6,11 +6,12 @@ const cbor = @import("cbor"); const Allocator = @import("std").mem.Allocator; const Mutex = @import("std").Thread.Mutex; const ArrayList = @import("std").ArrayList; -const Plane = @import("renderer").Plane; +const Plane = @import("renderer").Plane; const tp = @import("thespian"); const log = @import("log"); +const command = @import("command.zig"); const tui = @import("tui.zig"); const Widget = @import("Widget.zig"); const MessageFilter = @import("MessageFilter.zig"); @@ -23,11 +24,13 @@ const escape = fmt.fmtSliceEscapeLower; pub const name = @typeName(Self); const Self = @This(); +const Commands = command.Collection(cmds); allocator: std.mem.Allocator, plane: Plane, menu: *Menu.State(*Self), logger: log.Logger, +commands: Commands = undefined, items: usize = 0, view_pos: usize = 0, @@ -53,15 +56,16 @@ pub fn create(allocator: Allocator, parent: Plane) !Widget { .menu = try Menu.create(*Self, allocator, tui.current().mainview, .{ .ctx = self, .on_render = handle_render_menu, - // .on_resize = on_resize_menu, .on_scroll = EventHandler.bind(self, Self.handle_scroll), }), }; + try self.commands.init(self); return Widget.to(self); } pub fn deinit(self: *Self, a: Allocator) void { self.plane.deinit(); + self.commands.deinit(); a.destroy(self); } @@ -126,7 +130,7 @@ fn handle_render_menu(self: *Self, button: *Button.State(*Menu.State(*Self)), th const pointer = if (selected) "⏵" else " "; _ = button.plane.print("{s} ", .{pointer}) catch {}; button.plane.set_style(style_base); - _ = button.plane.print("{s}:{d} {s}", .{entry.path, entry.begin_line + 1, entry.lines}) catch {}; + _ = button.plane.print("{s}:{d} {s}", .{ entry.path, entry.begin_line + 1, entry.lines }) catch {}; return false; } @@ -169,3 +173,19 @@ fn handle_menu_action(menu: **Menu.State(*Self), button: *Button.State(*Menu.Sta }, } }) catch |e| self.logger.err("navigate", e); } + +const cmds = struct { + pub const Target = Self; + const Ctx = command.Context; + const Result = command.Result; + + pub fn goto_prev_file(self: *Self, _: Ctx) Result { + self.menu.select_up(); + self.menu.activate_selected(); + } + + pub fn goto_next_file(self: *Self, _: Ctx) Result { + self.menu.select_down(); + self.menu.activate_selected(); + } +}; diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 404eeb9..9eba7f1 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -110,14 +110,7 @@ fn add_find_in_files_result(self: *Self, path: []const u8, begin_line: usize, be self.find_in_files_done = false; fl.reset(); } - fl.add_item(.{ - .path = path, - .begin_line = begin_line - 1, - .begin_pos = begin_pos - 1, - .end_line = end_line - 1, - .end_pos = end_pos - 1, - .lines = lines - }) catch |e| return tp.exit_error(e, @errorReturnTrace()); + fl.add_item(.{ .path = path, .begin_line = begin_line - 1, .begin_pos = begin_pos - 1, .end_line = end_line - 1, .end_pos = end_pos - 1, .lines = lines }) catch |e| return tp.exit_error(e, @errorReturnTrace()); } pub fn update(self: *Self) void { @@ -376,6 +369,24 @@ const cmds = struct { gutter.relative = lnr; } } + + pub fn goto_next_file_or_diagnostic(self: *Self, ctx: Ctx) Result { + const filelist_view = @import("filelist_view.zig"); + if (self.is_panel_view_showing(filelist_view)) { + try command.executeName("goto_next_file", ctx); + } else { + try command.executeName("goto_next_diagnostic", ctx); + } + } + + pub fn goto_prev_file_or_diagnostic(self: *Self, ctx: Ctx) Result { + const filelist_view = @import("filelist_view.zig"); + if (self.is_panel_view_showing(filelist_view)) { + try command.executeName("goto_prev_file", ctx); + } else { + try command.executeName("goto_prev_diagnostic", ctx); + } + } }; pub fn handle_editor_event(self: *Self, _: tp.pid_ref, m: tp.message) tp.result { diff --git a/src/tui/mode/input/flow.zig b/src/tui/mode/input/flow.zig index d2f7799..39c29a5 100644 --- a/src/tui/mode/input/flow.zig +++ b/src/tui/mode/input/flow.zig @@ -141,8 +141,8 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void { }, mod.ALT => switch (keynormal) { 'J' => self.cmd("join_next_line", .{}), - 'N' => self.cmd("goto_next_diagnostic", .{}), - 'P' => self.cmd("goto_prev_diagnostic", .{}), + 'N' => self.cmd("goto_next_file_or_diagnostic", .{}), + 'P' => self.cmd("goto_prev_file_or_diagnostic", .{}), 'U' => self.cmd("to_upper", .{}), 'L' => self.cmd("to_lower", .{}), 'I' => self.cmd("toggle_inputview", .{}), @@ -336,9 +336,9 @@ const hints = tui.KeybindHints.initComptime(.{ // .{ "filter", "S-A-s" }, // self.cmd("filter", command.fmt(.{ "sort", "-u" })), .{ "format", "S-A-f" }, .{ "goto_definition", "F12" }, - .{ "goto_next_diagnostic", "A-n" }, + .{ "goto_next_file_or_diagnostic", "A-n" }, .{ "goto_next_match", "C-n, F3" }, - .{ "goto_prev_diagnostic", "A-p" }, + .{ "goto_prev_file_or_diagnostic", "A-p" }, .{ "goto_prev_match", "C-p, S-F3" }, .{ "gutter_mode_next", "A-F10" }, .{ "indent", "tab" }, diff --git a/src/tui/mode/input/vim/normal.zig b/src/tui/mode/input/vim/normal.zig index 4ba9deb..ea8b12c 100644 --- a/src/tui/mode/input/vim/normal.zig +++ b/src/tui/mode/input/vim/normal.zig @@ -523,9 +523,9 @@ const hints = tui.KeybindHints.initComptime(.{ // .{ "filter", "S-A-s" }, // self.cmd("filter", command.fmt(.{ "sort", "-u" })), .{ "format", "S-A-f" }, .{ "goto_definition", "F12" }, - .{ "goto_next_diagnostic", "A-n" }, + .{ "goto_next_file_or_diagnostic", "A-n" }, .{ "goto_next_match", "C-n, F3, n" }, - .{ "goto_prev_diagnostic", "A-p" }, + .{ "goto_prev_file_or_diagnostic", "A-p" }, .{ "goto_prev_match", "C-p, S-F3, N" }, .{ "gutter_mode_next", "A-F10" }, .{ "indent", "tab" }, diff --git a/src/tui/mode/input/vim/visual.zig b/src/tui/mode/input/vim/visual.zig index 1f2b662..c3fe455 100644 --- a/src/tui/mode/input/vim/visual.zig +++ b/src/tui/mode/input/vim/visual.zig @@ -483,9 +483,9 @@ const hints = tui.KeybindHints.initComptime(.{ // .{ "filter", "S-A-s" }, // self.cmd("filter", command.fmt(.{ "sort", "-u" })), .{ "format", "S-A-f" }, .{ "goto_definition", "F12" }, - .{ "goto_next_diagnostic", "A-n" }, + .{ "goto_next_file_or_diagnostic", "A-n" }, .{ "goto_next_match", "C-n, F3, n" }, - .{ "goto_prev_diagnostic", "A-p" }, + .{ "goto_prev_file_or_diagnostic", "A-p" }, .{ "goto_prev_match", "C-p, S-F3, N" }, .{ "gutter_mode_next", "A-F10" }, .{ "indent", "tab" }, diff --git a/src/tui/mode/mini/find_in_files.zig b/src/tui/mode/mini/find_in_files.zig index 97d30df..51cb9c7 100644 --- a/src/tui/mode/mini/find_in_files.zig +++ b/src/tui/mode/mini/find_in_files.zig @@ -108,8 +108,8 @@ fn mapPress(self: *Self, keypress: u32, egc: u32, modifiers: u32) !void { }, mod.ALT => switch (keynormal) { 'V' => self.cmd("system_paste", .{}), - 'N' => self.cmd("goto_next_match", .{}), - 'P' => self.cmd("goto_prev_match", .{}), + 'N' => self.cmd("goto_next_file", .{}), + 'P' => self.cmd("goto_prev_file", .{}), else => {}, }, mod.ALT | mod.SHIFT => switch (keynormal) {