feat: expose typed action and mode configuration values
This commit is contained in:
+47
-4
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user