feat: lookup dynamic mode
This commit is contained in:
parent
ae3d21ad08
commit
3661148daf
1 changed files with 38 additions and 26 deletions
|
@ -16,32 +16,32 @@ const EventHandler = @import("EventHandler");
|
||||||
|
|
||||||
pub const mode = struct {
|
pub const mode = struct {
|
||||||
pub const input = struct {
|
pub const input = struct {
|
||||||
pub const flow = Handler("flow");
|
pub const flow = Handler("flow", "normal");
|
||||||
pub const home = Handler("home");
|
pub const home = Handler("home", "normal");
|
||||||
pub const vim = struct {
|
pub const vim = struct {
|
||||||
pub const normal = Handler("vim/normal");
|
pub const normal = Handler("vim", "normal");
|
||||||
pub const insert = Handler("vim/insert");
|
pub const insert = Handler("vim", "insert");
|
||||||
pub const visual = Handler("vim/visual");
|
pub const visual = Handler("vim", "visual");
|
||||||
};
|
};
|
||||||
pub const helix = struct {
|
pub const helix = struct {
|
||||||
pub const normal = Handler("helix/normal");
|
pub const normal = Handler("helix", "normal");
|
||||||
pub const insert = Handler("helix/insert");
|
pub const insert = Handler("helix", "insert");
|
||||||
pub const visual = Handler("helix/select");
|
pub const visual = Handler("helix", "select");
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
pub const overlay = struct {
|
pub const overlay = struct {
|
||||||
pub const palette = HandlerStatic("overlay/palette");
|
pub const palette = Handler("overlay", "palette");
|
||||||
};
|
};
|
||||||
pub const mini = struct {
|
pub const mini = struct {
|
||||||
pub const goto = HandlerStatic("mini/goto");
|
pub const goto = Handler("mini", "goto");
|
||||||
pub const move_to_char = HandlerStatic("mini/move_to_char");
|
pub const move_to_char = Handler("mini", "move_to_char");
|
||||||
pub const file_browser = HandlerStatic("mini/file_browser");
|
pub const file_browser = Handler("mini", "file_browser");
|
||||||
pub const find_in_files = HandlerStatic("mini/find_in_files");
|
pub const find_in_files = Handler("mini", "find_in_files");
|
||||||
pub const find = HandlerStatic("mini/find");
|
pub const find = Handler("mini", "find");
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
fn Handler(_: []const u8) type {
|
fn Handler(namespace_name: []const u8, mode_name: []const u8) type {
|
||||||
return struct {
|
return struct {
|
||||||
allocator: std.mem.Allocator,
|
allocator: std.mem.Allocator,
|
||||||
bindings: *Bindings,
|
bindings: *Bindings,
|
||||||
|
@ -52,6 +52,8 @@ fn Handler(_: []const u8) type {
|
||||||
.bindings = try Bindings.init(allocator),
|
.bindings = try Bindings.init(allocator),
|
||||||
};
|
};
|
||||||
try self.bindings.loadJson(@embedFile("keybindings.json"));
|
try self.bindings.loadJson(@embedFile("keybindings.json"));
|
||||||
|
try self.bindings.selectNamespace(namespace_name);
|
||||||
|
try self.bindings.selectMode(mode_name);
|
||||||
return EventHandler.to_owned(self);
|
return EventHandler.to_owned(self);
|
||||||
}
|
}
|
||||||
pub fn deinit(self: *@This()) void {
|
pub fn deinit(self: *@This()) void {
|
||||||
|
@ -65,17 +67,6 @@ fn Handler(_: []const u8) type {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn HandlerStatic(_: []const u8) type {
|
|
||||||
return struct {
|
|
||||||
pub fn create() EventHandler {
|
|
||||||
return EventHandler.static(@This());
|
|
||||||
}
|
|
||||||
pub fn receive(_: tp.pid_ref, _: tp.message) error{Exit}!bool {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const Mode = struct {
|
pub const Mode = struct {
|
||||||
input_handler: EventHandler,
|
input_handler: EventHandler,
|
||||||
event_handler: ?EventHandler = null,
|
event_handler: ?EventHandler = null,
|
||||||
|
@ -689,10 +680,31 @@ const Bindings = struct {
|
||||||
return self.namespaces.keys();
|
return self.namespaces.keys();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn selectNamespace(self: *Bindings, namespace_name: []const u8) error{NotFound}!void {
|
||||||
|
for (self.namespaces.keys(), 0..) |name, i| {
|
||||||
|
if (std.mem.eql(u8, name, namespace_name)) {
|
||||||
|
self.active_namespace = i;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return error.NotFound;
|
||||||
|
}
|
||||||
|
|
||||||
fn activeNamespace(self: *const Bindings) Namespace {
|
fn activeNamespace(self: *const Bindings) Namespace {
|
||||||
return self.namespaces.values()[self.active_namespace];
|
return self.namespaces.values()[self.active_namespace];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn selectMode(self: *Bindings, mode_name: []const u8) error{NotFound}!void {
|
||||||
|
const namespace = self.activeNamespace();
|
||||||
|
for (namespace.keys(), 0..) |name, i| {
|
||||||
|
if (std.mem.eql(u8, name, mode_name)) {
|
||||||
|
self.active_mode = i;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return error.NotFound;
|
||||||
|
}
|
||||||
|
|
||||||
fn activeMode(self: *Bindings) *BindingSet {
|
fn activeMode(self: *Bindings) *BindingSet {
|
||||||
return self.activeNamespace().values()[self.active_mode];
|
return self.activeNamespace().values()[self.active_mode];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue