-
Notifications
You must be signed in to change notification settings - Fork 87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve key binding match/matching check #709
Changes from all commits
265959e
7251d19
34d39c1
346e2da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,18 +29,20 @@ class InvalidInputrc < RuntimeError | |
attr_accessor :autocompletion | ||
|
||
def initialize | ||
@additional_key_bindings = {} # from inputrc | ||
@additional_key_bindings[:emacs] = {} | ||
@additional_key_bindings[:vi_insert] = {} | ||
@additional_key_bindings[:vi_command] = {} | ||
@oneshot_key_bindings = {} | ||
@additional_key_bindings = { # from inputrc | ||
emacs: Reline::KeyActor::Base.new, | ||
vi_insert: Reline::KeyActor::Base.new, | ||
vi_command: Reline::KeyActor::Base.new | ||
} | ||
@oneshot_key_bindings = Reline::KeyActor::Base.new | ||
@editing_mode_label = :emacs | ||
@keymap_label = :emacs | ||
@keymap_prefix = [] | ||
@key_actors = {} | ||
@key_actors[:emacs] = Reline::KeyActor::Emacs.new | ||
@key_actors[:vi_insert] = Reline::KeyActor::ViInsert.new | ||
@key_actors[:vi_command] = Reline::KeyActor::ViCommand.new | ||
@default_key_bindings = { | ||
emacs: Reline::KeyActor::Base.new(Reline::KeyActor::EMACS_MAPPING), | ||
vi_insert: Reline::KeyActor::Base.new(Reline::KeyActor::VI_INSERT_MAPPING), | ||
vi_command: Reline::KeyActor::Base.new(Reline::KeyActor::VI_COMMAND_MAPPING) | ||
} | ||
@vi_cmd_mode_string = '(cmd)' | ||
@vi_ins_mode_string = '(ins)' | ||
@emacs_mode_string = '@' | ||
|
@@ -62,7 +64,7 @@ def reset | |
end | ||
|
||
def editing_mode | ||
@key_actors[@editing_mode_label] | ||
@default_key_bindings[@editing_mode_label] | ||
end | ||
|
||
def editing_mode=(val) | ||
|
@@ -74,7 +76,7 @@ def editing_mode_is?(*val) | |
end | ||
|
||
def keymap | ||
@key_actors[@keymap_label] | ||
@default_key_bindings[@keymap_label] | ||
end | ||
|
||
def loaded? | ||
|
@@ -133,26 +135,26 @@ def read(file = nil) | |
|
||
def key_bindings | ||
# The key bindings for each editing mode will be overwritten by the user-defined ones. | ||
kb = @key_actors[@editing_mode_label].default_key_bindings.dup | ||
kb.merge!(@additional_key_bindings[@editing_mode_label]) | ||
kb.merge!(@oneshot_key_bindings) | ||
kb | ||
Reline::KeyActor::Composite.new([@oneshot_key_bindings, @additional_key_bindings[@editing_mode_label], @default_key_bindings[@editing_mode_label]]) | ||
end | ||
|
||
def add_oneshot_key_binding(keystroke, target) | ||
@oneshot_key_bindings[keystroke] = target | ||
# IRB sets invalid keystroke [Reline::Key]. We should ignore it. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have we removed that from IRB? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I'm aware of that and I'm not arguing we need to change the approach here 🙂 I just hadn't see any related PR on IRB so want to confirm. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opened ruby/irb#963 |
||
return unless keystroke.all? { |c| c.is_a?(Integer) } | ||
|
||
@oneshot_key_bindings.add(keystroke, target) | ||
end | ||
|
||
def reset_oneshot_key_bindings | ||
@oneshot_key_bindings.clear | ||
end | ||
|
||
def add_default_key_binding_by_keymap(keymap, keystroke, target) | ||
@key_actors[keymap].default_key_bindings[keystroke] = target | ||
@default_key_bindings[keymap].add(keystroke, target) | ||
end | ||
|
||
def add_default_key_binding(keystroke, target) | ||
@key_actors[@keymap_label].default_key_bindings[keystroke] = target | ||
add_default_key_binding_by_keymap(@keymap_label, keystroke, target) | ||
end | ||
|
||
def read_lines(lines, file = nil) | ||
|
@@ -192,7 +194,7 @@ def read_lines(lines, file = nil) | |
func_name = func_name.split.first | ||
keystroke, func = bind_key(key, func_name) | ||
next unless keystroke | ||
@additional_key_bindings[@keymap_label][@keymap_prefix + keystroke] = func | ||
@additional_key_bindings[@keymap_label].add(@keymap_prefix + keystroke, func) | ||
end | ||
end | ||
unless if_stack.empty? | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,31 @@ | ||
class Reline::KeyActor::Base | ||
MAPPING = Array.new(256) | ||
def initialize(mapping = []) | ||
@mapping = mapping | ||
@matching_bytes = {} | ||
@key_bindings = {} | ||
end | ||
|
||
def get_method(key) | ||
self.class::MAPPING[key] | ||
@mapping[key] | ||
end | ||
|
||
def add(key, func) | ||
(1...key.size).each do |size| | ||
@matching_bytes[key.take(size)] = true | ||
end | ||
@key_bindings[key] = func | ||
end | ||
|
||
def matching?(key) | ||
@matching_bytes[key] | ||
end | ||
|
||
def initialize | ||
@default_key_bindings = {} | ||
def get(key) | ||
@key_bindings[key] | ||
end | ||
|
||
def default_key_bindings | ||
@default_key_bindings | ||
def clear | ||
@matching_bytes.clear | ||
@key_bindings.clear | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Reline::KeyActor::Composite | ||
def initialize(key_actors) | ||
@key_actors = key_actors | ||
end | ||
|
||
def matching?(key) | ||
@key_actors.any? { |key_actor| key_actor.matching?(key) } | ||
end | ||
|
||
def get(key) | ||
@key_actors.each do |key_actor| | ||
func = key_actor.get(key) | ||
return func if func | ||
end | ||
nil | ||
st0012 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we also convert this into a normal class?