1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/reline] Use expanded method definitions instead of refinements

@jhawthorn said, "this will make Ruby's integer comparisons slower
globally." It looks like "binding.irb" is going to cause serious
problems in Rails applications.

ee8d6c6a82
This commit is contained in:
aycabta 2021-09-13 04:04:29 +09:00 committed by git
parent a8fe7c9e2a
commit 8de53fb31c

View file

@ -1,23 +1,25 @@
class Reline::KeyStroke class Reline::KeyStroke
using Module.new { using Module.new {
refine Integer do }
def ==(other)
if other.is_a?(Reline::Key) def initialize(config)
if other.combined_char == "\e".ord @config = config
false
else
other.combined_char == self
end
else
super
end
end
end end
refine Array do def compress_meta_key(ary)
def start_with?(other) ary.inject([]) { |result, key|
compressed_me = compress_meta_key if result.size > 0 and result.last == "\e".ord
compressed_other = other.compress_meta_key result[result.size - 1] = Reline::Key.new(key, key | 0b10000000, true)
else
result << key
end
result
}
end
def start_with?(me, other)
compressed_me = compress_meta_key(me)
compressed_other = compress_meta_key(other)
i = 0 i = 0
loop do loop do
my_c = compressed_me[i] my_c = compressed_me[i]
@ -39,52 +41,50 @@ class Reline::KeyStroke
end end
end end
def ==(other) def equal?(me, other)
compressed_me = compress_meta_key case me
compressed_other = other.compress_meta_key when Array
compressed_me.size == compressed_other.size and [compressed_me, compressed_other].transpose.all?{ |i| i[0] == i[1] } compressed_me = compress_meta_key(me)
end compressed_other = compress_meta_key(other)
compressed_me.size == compressed_other.size and [compressed_me, compressed_other].transpose.all?{ |i| equal?(i[0], i[1]) }
def compress_meta_key when Integer
inject([]) { |result, key| if other.is_a?(Reline::Key)
if result.size > 0 and result.last == "\e".ord if other.combined_char == "\e".ord
result[result.size - 1] = Reline::Key.new(key, key | 0b10000000, true) false
else else
result << key other.combined_char == me
end end
result else
} me == other
end end
when Reline::Key
def bytes if other.is_a?(Integer)
self me.combined_char == other
else
me == other
end end
end end
}
def initialize(config)
@config = config
end end
def match_status(input) def match_status(input)
key_mapping.keys.select { |lhs| key_mapping.keys.select { |lhs|
lhs.start_with? input start_with?(lhs, input)
}.tap { |it| }.tap { |it|
return :matched if it.size == 1 && (it[0] == input) return :matched if it.size == 1 && equal?(it[0], input)
return :matching if it.size == 1 && (it[0] != input) return :matching if it.size == 1 && !equal?(it[0], input)
return :matched if it.max_by(&:size)&.size&.< input.size return :matched if it.max_by(&:size)&.size&.< input.size
return :matching if it.size > 1 return :matching if it.size > 1
} }
key_mapping.keys.select { |lhs| key_mapping.keys.select { |lhs|
input.start_with? lhs start_with?(input, lhs)
}.tap { |it| }.tap { |it|
return it.size > 0 ? :matched : :unmatched return it.size > 0 ? :matched : :unmatched
} }
end end
def expand(input) def expand(input)
input = input.compress_meta_key input = compress_meta_key(input)
lhs = key_mapping.keys.select { |item| input.start_with? item }.sort_by(&:size).last lhs = key_mapping.keys.select { |item| start_with?(input, item) }.sort_by(&:size).last
return input unless lhs return input unless lhs
rhs = key_mapping[lhs] rhs = key_mapping[lhs]