From ced130b4f438913648d40d2c691cce7454972813 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 12 Nov 2024 22:31:26 +0100 Subject: [PATCH] refactor: move helix mode keybindings to keybind module --- .../static}/input/helix/insert.zig | 30 +++++++----------- .../static}/input/helix/normal.zig | 31 ++++++------------- .../static}/input/helix/select.zig | 31 ++++++------------- src/keybind/static/keybind.zig | 5 +++ src/tui/tui.zig | 16 +++++++--- 5 files changed, 48 insertions(+), 65 deletions(-) rename src/{tui/mode => keybind/static}/input/helix/insert.zig (95%) rename src/{tui/mode => keybind/static}/input/helix/normal.zig (97%) rename src/{tui/mode => keybind/static}/input/helix/select.zig (97%) diff --git a/src/tui/mode/input/helix/insert.zig b/src/keybind/static/input/helix/insert.zig similarity index 95% rename from src/tui/mode/input/helix/insert.zig rename to src/keybind/static/input/helix/insert.zig index 3cf6e77..5458cb3 100644 --- a/src/tui/mode/input/helix/insert.zig +++ b/src/keybind/static/input/helix/insert.zig @@ -1,40 +1,30 @@ +const std = @import("std"); const tp = @import("thespian"); - const key = @import("renderer").input.key; const mod = @import("renderer").input.modifier; const event_type = @import("renderer").input.event_type; const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8; const command = @import("command"); const EventHandler = @import("EventHandler"); - -const tui = @import("../../../tui.zig"); - -const Allocator = @import("std").mem.Allocator; -const ArrayList = @import("std").ArrayList; -const eql = @import("std").mem.eql; +const keybind = @import("../../keybind.zig"); const Self = @This(); const input_buffer_size = 1024; -allocator: Allocator, -input: ArrayList(u8), +allocator: std.mem.Allocator, +input: std.ArrayList(u8), last_cmd: []const u8 = "", leader: ?struct { keypress: u32, modifiers: u32 } = null, commands: Commands = undefined, -pub fn create(allocator: Allocator) !tui.Mode { +pub fn create(allocator: std.mem.Allocator, _: anytype) !EventHandler { const self: *Self = try allocator.create(Self); self.* = .{ .allocator = allocator, - .input = try ArrayList(u8).initCapacity(allocator, input_buffer_size), + .input = try std.ArrayList(u8).initCapacity(allocator, input_buffer_size), }; try self.commands.init(self); - return .{ - .input_handler = EventHandler.to_owned(self), - .name = "INS", - .line_numbers = if (tui.current().config.vim_insert_gutter_line_numbers_relative) .relative else .absolute, - .cursor_shape = .beam, - }; + return EventHandler.to_owned(self); } pub fn deinit(self: *Self) void { @@ -287,9 +277,9 @@ fn cmd(self: *Self, name_: []const u8, ctx: command.Context) tp.result { } fn cmd_cycle3(self: *Self, name1: []const u8, name2: []const u8, name3: []const u8, ctx: command.Context) tp.result { - return if (eql(u8, self.last_cmd, name2)) + return if (std.mem.eql(u8, self.last_cmd, name2)) self.cmd(name3, ctx) - else if (eql(u8, self.last_cmd, name1)) + else if (std.mem.eql(u8, self.last_cmd, name1)) self.cmd(name2, ctx) else self.cmd(name1, ctx); @@ -300,6 +290,8 @@ fn cmd_async(self: *Self, name_: []const u8) tp.result { return tp.self_pid().send(.{ "cmd", name_ }); } +pub const hints = keybind.KeybindHints.initComptime(.{}); + const Commands = command.Collection(cmds_); const cmds_ = struct { pub const Target = Self; diff --git a/src/tui/mode/input/helix/normal.zig b/src/keybind/static/input/helix/normal.zig similarity index 97% rename from src/tui/mode/input/helix/normal.zig rename to src/keybind/static/input/helix/normal.zig index e79f861..3aaf984 100644 --- a/src/tui/mode/input/helix/normal.zig +++ b/src/keybind/static/input/helix/normal.zig @@ -1,42 +1,31 @@ +const std = @import("std"); const tp = @import("thespian"); - const key = @import("renderer").input.key; const mod = @import("renderer").input.modifier; const event_type = @import("renderer").input.event_type; const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8; const command = @import("command"); const EventHandler = @import("EventHandler"); - -const tui = @import("../../../tui.zig"); - -const Allocator = @import("std").mem.Allocator; -const ArrayList = @import("std").ArrayList; -const eql = @import("std").mem.eql; +const keybind = @import("../../keybind.zig"); const Self = @This(); const input_buffer_size = 1024; -allocator: Allocator, -input: ArrayList(u8), +allocator: std.mem.Allocator, +input: std.ArrayList(u8), last_cmd: []const u8 = "", leader: ?struct { keypress: u32, modifiers: u32 } = null, count: usize = 0, commands: Commands = undefined, -pub fn create(allocator: Allocator) !tui.Mode { +pub fn create(allocator: std.mem.Allocator, _: anytype) !EventHandler { const self: *Self = try allocator.create(Self); self.* = .{ .allocator = allocator, - .input = try ArrayList(u8).initCapacity(allocator, input_buffer_size), + .input = try std.ArrayList(u8).initCapacity(allocator, input_buffer_size), }; try self.commands.init(self); - return .{ - .input_handler = EventHandler.to_owned(self), - .name = "NOR", - .line_numbers = if (tui.current().config.vim_normal_gutter_line_numbers_relative) .relative else .absolute, - .keybind_hints = &hints, - .cursor_shape = .block, - }; + return EventHandler.to_owned(self); } pub fn deinit(self: *Self) void { @@ -504,9 +493,9 @@ fn cmd_count(self: *Self, name_: []const u8, ctx: command.Context) tp.result { } fn cmd_cycle3(self: *Self, name1: []const u8, name2: []const u8, name3: []const u8, ctx: command.Context) tp.result { - return if (eql(u8, self.last_cmd, name2)) + return if (std.mem.eql(u8, self.last_cmd, name2)) self.cmd(name3, ctx) - else if (eql(u8, self.last_cmd, name1)) + else if (std.mem.eql(u8, self.last_cmd, name1)) self.cmd(name2, ctx) else self.cmd(name1, ctx); @@ -536,7 +525,7 @@ fn seq_count(self: *Self, cmds: anytype, ctx: command.Context) tp.result { try self.cmd(@field(cmds, field_info.name), ctx); } -const hints = tui.KeybindHints.initComptime(.{ +pub const hints = keybind.KeybindHints.initComptime(.{ .{ "add_cursor_all_matches", "C-S-l" }, .{ "add_cursor_down", "S-A-down" }, .{ "add_cursor_next_match", "C-d" }, diff --git a/src/tui/mode/input/helix/select.zig b/src/keybind/static/input/helix/select.zig similarity index 97% rename from src/tui/mode/input/helix/select.zig rename to src/keybind/static/input/helix/select.zig index a350110..49b17d3 100644 --- a/src/tui/mode/input/helix/select.zig +++ b/src/keybind/static/input/helix/select.zig @@ -1,42 +1,31 @@ +const std = @import("std"); const tp = @import("thespian"); - const key = @import("renderer").input.key; const mod = @import("renderer").input.modifier; const event_type = @import("renderer").input.event_type; const ucs32_to_utf8 = @import("renderer").ucs32_to_utf8; const command = @import("command"); const EventHandler = @import("EventHandler"); - -const tui = @import("../../../tui.zig"); - -const Allocator = @import("std").mem.Allocator; -const ArrayList = @import("std").ArrayList; -const eql = @import("std").mem.eql; +const keybind = @import("../../keybind.zig"); const Self = @This(); const input_buffer_size = 1024; -allocator: Allocator, -input: ArrayList(u8), +allocator: std.mem.Allocator, +input: std.ArrayList(u8), last_cmd: []const u8 = "", leader: ?struct { keypress: u32, modifiers: u32 } = null, count: usize = 0, commands: Commands = undefined, -pub fn create(allocator: Allocator) !tui.Mode { +pub fn create(allocator: std.mem.Allocator, _: anytype) !EventHandler { const self: *Self = try allocator.create(Self); self.* = .{ .allocator = allocator, - .input = try ArrayList(u8).initCapacity(allocator, input_buffer_size), + .input = try std.ArrayList(u8).initCapacity(allocator, input_buffer_size), }; try self.commands.init(self); - return .{ - .input_handler = EventHandler.to_owned(self), - .name = "SEL", - .line_numbers = if (tui.current().config.vim_visual_gutter_line_numbers_relative) .relative else .absolute, - .keybind_hints = &hints, - .cursor_shape = .block, - }; + return EventHandler.to_owned(self); } pub fn deinit(self: *Self) void { @@ -504,9 +493,9 @@ fn cmd_count(self: *Self, name_: []const u8, ctx: command.Context) tp.result { } fn cmd_cycle3(self: *Self, name1: []const u8, name2: []const u8, name3: []const u8, ctx: command.Context) tp.result { - return if (eql(u8, self.last_cmd, name2)) + return if (std.mem.eql(u8, self.last_cmd, name2)) self.cmd(name3, ctx) - else if (eql(u8, self.last_cmd, name1)) + else if (std.mem.eql(u8, self.last_cmd, name1)) self.cmd(name2, ctx) else self.cmd(name1, ctx); @@ -536,7 +525,7 @@ fn seq_count(self: *Self, cmds: anytype, ctx: command.Context) tp.result { try self.cmd(@field(cmds, field_info.name), ctx); } -const hints = tui.KeybindHints.initComptime(.{ +pub const hints = keybind.KeybindHints.initComptime(.{ .{ "add_cursor_all_matches", "C-S-l" }, .{ "add_cursor_down", "S-A-down" }, .{ "add_cursor_next_match", "C-d" }, diff --git a/src/keybind/static/keybind.zig b/src/keybind/static/keybind.zig index 5a9f47f..6c00c39 100644 --- a/src/keybind/static/keybind.zig +++ b/src/keybind/static/keybind.zig @@ -7,6 +7,11 @@ pub const mode = struct { pub const insert = @import("input/vim/insert.zig"); pub const visual = @import("input/vim/visual.zig"); }; + pub const helix = struct { + pub const normal = @import("input/helix/normal.zig"); + pub const insert = @import("input/helix/insert.zig"); + pub const visual = @import("input/helix/select.zig"); + }; }; pub const overlay = struct { pub const palette = @import("overlay/palette.zig"); diff --git a/src/tui/tui.zig b/src/tui/tui.zig index fe10001..5531424 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -562,7 +562,6 @@ fn enter_overlay_mode(self: *Self, mode: type) command.Result { } fn static_mode(self: *Self, mode: anytype, name: []const u8, opts: anytype) !Mode { - // .line_numbers_relative = if (self.config.vim_normal_gutter_line_numbers_relative) .relative else .absolute, return .{ .input_handler = try mode.create(self.allocator, opts), .name = name, @@ -681,11 +680,20 @@ const cmds = struct { .cursor_shape = .underline, }) else if (std.mem.eql(u8, mode, "helix/normal")) - try @import("mode/input/helix/normal.zig").create(self.allocator) + try self.static_mode(keybind.mode.input.helix.normal, "NOR", .{ + .line_numbers_relative = self.config.vim_normal_gutter_line_numbers_relative, + .cursor_shape = .block, + }) else if (std.mem.eql(u8, mode, "helix/insert")) - try @import("mode/input/helix/insert.zig").create(self.allocator) + try self.static_mode(keybind.mode.input.helix.insert, "INS", .{ + .line_numbers_relative = self.config.vim_insert_gutter_line_numbers_relative, + .cursor_shape = .beam, + }) else if (std.mem.eql(u8, mode, "helix/select")) - try @import("mode/input/helix/select.zig").create(self.allocator) + try self.static_mode(keybind.mode.input.helix.visual, "SEL", .{ + .line_numbers_relative = self.config.vim_visual_gutter_line_numbers_relative, + .cursor_shape = .block, + }) else if (std.mem.eql(u8, mode, "flow")) try self.static_mode(keybind.mode.input.flow, "flow", .{}) else if (std.mem.eql(u8, mode, "home"))