feat: expose typed action and mode configuration values

This commit is contained in:
Valdemar Lorenzen
2026-10-05 16:21:01 +02:00
parent bb242b35b6
commit 714f1ff4de
5 changed files with 125 additions and 45 deletions
+47 -4
View File
@@ -18,6 +18,30 @@ module Vamp
value.dup.force_encoding(Encoding::UTF_8)
end
Action = Struct.new(:name)
Mode = Struct.new(:name)
module Core
%w[
insert insert_first_nonblank append append_line_end open_below open_above
left right down up join_lines line_start line_end delete undo redo command
normal newline backspace submit
].each do |name|
action = Action.new("core.#{name}".freeze).freeze
define_singleton_method(name) { action }
end
end
module Modes
%w[normal insert command].each do |name|
mode = Mode.new(name.freeze).freeze
define_singleton_method(name) { mode }
end
end
Action.private_class_method :new
Mode.private_class_method :new
class Config
def initialize
@plugins = []
@@ -31,13 +55,32 @@ module Vamp
nil
end
def bind(key, command, mode: "normal")
def core
Core
end
def modes
Modes
end
def bind(key, command, mode: Modes.normal)
key = Vamp.string!(key, "binding key")
mode = Vamp.string!(mode, "binding mode")
command = Vamp.string!(command, "binding command") unless command.nil?
raise TypeError, "binding mode must be a value from config.modes" unless mode.is_a?(Mode)
command = case command
when Action
command.name
when String
name = Vamp.string!(command, "binding command")
raise TypeError, "use config.core for built-in actions" if name.start_with?("core.")
name
when nil
nil
else
raise TypeError, "binding command must be an Action, String, or nil"
end
raise ArgumentError, "binding key must not be empty" if key.empty?
raise ArgumentError, "binding command must not be empty" if command == ""
@bindings << { "mode" => mode, "key" => key, "command" => command }
@bindings << { "mode" => mode.name, "key" => key, "command" => command }
nil
end