feat: add support in keybind module for looking up key sequence prefix matches

This is the initial part of some sort of which-key like function.
This commit is contained in:
CJ van den Berg 2025-11-28 16:11:41 +01:00
parent d5eb4d8bd3
commit d9ab0fd5af
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 30 additions and 1 deletions

View file

@ -71,13 +71,15 @@ const Handler = struct {
opts.insert_command
else
"insert_chars";
const bindings = try get_mode_binding_set(mode_name, insert_command);
self.* = .{
.allocator = allocator,
.bindings = try get_mode_binding_set(mode_name, insert_command),
.bindings = bindings,
};
return .{
.allocator = allocator,
.input_handler = EventHandler.to_owned(self),
.bindings = bindings,
.keybind_hints = self.bindings.hints(),
.mode = try allocator.dupe(u8, mode_name),
.name = self.bindings.name,
@ -127,6 +129,7 @@ pub const Mode = struct {
mode: []const u8,
name: []const u8 = "",
line_numbers: LineNumbers = .inherit,
bindings: *const BindingSet,
keybind_hints: *const KeybindHints,
cursor_shape: ?CursorShape = null,
init_command: ?Command = null,
@ -168,6 +171,11 @@ pub const Mode = struct {
self.deinit_command = null;
self.initialized = false;
}
pub fn current_key_event_sequence_bindings(self: *const Mode, allocator: std.mem.Allocator) error{OutOfMemory}![]const Binding {
if (globals.current_sequence.items.len == 0) return &.{};
return self.bindings.get_matches_for_key_event_sequence(allocator, globals.current_sequence.items);
}
};
const NamespaceMap = std.StringHashMapUnmanaged(Namespace);
@ -760,6 +768,18 @@ const BindingSet = struct {
globals.current_sequence.clearRetainingCapacity();
}
}
/// Retreive bindings that will match a key event sequence
pub fn get_matches_for_key_event_sequence(self: *const @This(), allocator: std.mem.Allocator, sequence: []const KeyEvent) error{OutOfMemory}![]const Binding {
var matches: std.ArrayListUnmanaged(Binding) = .{};
for (self.press.items) |*binding| switch (binding.match(sequence)) {
.matched, .match_possible => {
(try matches.addOne(allocator)).* = binding.*;
},
.match_impossible => {},
};
return matches.toOwnedSlice(allocator);
}
};
pub const LineNumbers = enum {

View file

@ -297,6 +297,15 @@ fn handle_input_idle(self: *Self) void {
var buf: [32]u8 = undefined;
const m = tp.message.fmtbuf(&buf, .{"input_idle"}) catch return;
_ = self.send_widgets(tp.self_pid(), m) catch return;
if (self.input_mode_) |mode| {
const bindings = mode.current_key_event_sequence_bindings(self.allocator) catch return;
defer self.allocator.free(bindings);
for (bindings) |binding|
self.logger.print(" {f} => {s}", .{
keybind.key_event_sequence_fmt(binding.key_events),
binding.commands[0].command,
});
}
}
fn update_input_idle_timer(self: *Self) void {