Skip to content

Commit

Permalink
[ruby/reline] Motions e, E, t, f should include a char on cursor if f…
Browse files Browse the repository at this point in the history
…ollows operator

ruby/reline@86e9a76499
  • Loading branch information
aycabta committed Dec 4, 2020
1 parent 6be3b2d commit ba8e5f7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 22 deletions.
81 changes: 61 additions & 20 deletions lib/reline/line_editor.rb
Expand Up @@ -698,7 +698,7 @@ def editing_mode
if @waiting_operator_proc
if VI_MOTIONS.include?(method_symbol)
old_cursor, old_byte_pointer = @cursor, @byte_pointer
block.()
block.(true)
unless @waiting_proc
cursor_diff, byte_pointer_diff = @cursor - old_cursor, @byte_pointer - old_byte_pointer
@cursor, @byte_pointer = old_cursor, old_byte_pointer
Expand All @@ -718,27 +718,41 @@ def editing_mode
end
else
# Ignores operator when not motion is given.
block.()
block.(false)
end
@waiting_operator_proc = nil
else
block.()
block.(false)
end
end

private def argumentable?(method_obj)
method_obj and method_obj.parameters.length != 1
method_obj and method_obj.parameters.any? { |param| param[0] == :key and param[1] == :arg }
end

def wrap_method_call(method_symbol, method_obj, key)
private def inclusive?(method_obj)
# If a motion method with the keyword argument "inclusive" follows the
# operator, it must contain the character at the cursor position.
method_obj and method_obj.parameters.any? { |param| param[0] == :key and param[1] == :inclusive }
end

def wrap_method_call(method_symbol, method_obj, key, with_operator = false)
if @config.editing_mode_is?(:emacs, :vi_insert) and @waiting_proc.nil? and @waiting_operator_proc.nil?
not_insertion = method_symbol != :ed_insert
process_insert(force: not_insertion)
end
if @vi_arg and argumentable?(method_obj)
method_obj.(key, arg: @vi_arg)
if with_operator and inclusive?(method_obj)
method_obj.(key, arg: @vi_arg, inclusive: true)
else
method_obj.(key, arg: @vi_arg)
end
else
method_obj.(key)
if with_operator and inclusive?(method_obj)
method_obj.(key, inclusive: true)
else
method_obj.(key)
end
end
end

Expand All @@ -750,8 +764,8 @@ def wrap_method_call(method_symbol, method_obj, key)
end
if method_symbol and key.is_a?(Symbol)
if @vi_arg and argumentable?(method_obj)
run_for_operators(key, method_symbol) do
wrap_method_call(method_symbol, method_obj, key)
run_for_operators(key, method_symbol) do |with_operator|
wrap_method_call(method_symbol, method_obj, key, with_operator)
end
else
wrap_method_call(method_symbol, method_obj, key) if method_obj
Expand All @@ -763,8 +777,8 @@ def wrap_method_call(method_symbol, method_obj, key)
ed_argument_digit(key)
else
if argumentable?(method_obj)
run_for_operators(key, method_symbol) do
wrap_method_call(method_symbol, method_obj, key)
run_for_operators(key, method_symbol) do |with_operator|
wrap_method_call(method_symbol, method_obj, key, with_operator)
end
elsif @waiting_proc
@waiting_proc.(key)
Expand All @@ -783,8 +797,8 @@ def wrap_method_call(method_symbol, method_obj, key)
if method_symbol == :ed_argument_digit
wrap_method_call(method_symbol, method_obj, key)
else
run_for_operators(key, method_symbol) do
wrap_method_call(method_symbol, method_obj, key)
run_for_operators(key, method_symbol) do |with_operator|
wrap_method_call(method_symbol, method_obj, key, with_operator)
end
end
@kill_ring.process
Expand Down Expand Up @@ -1962,13 +1976,22 @@ def finish
vi_prev_word(key, arg: arg) if arg > 0
end

private def vi_end_word(key, arg: 1)
private def vi_end_word(key, arg: 1, inclusive: false)
if @line.bytesize > @byte_pointer
byte_size, width = Reline::Unicode.vi_forward_end_word(@line, @byte_pointer)
@byte_pointer += byte_size
@cursor += width
end
arg -= 1
if inclusive and arg.zero?
byte_size = Reline::Unicode.get_next_mbchar_size(@line, @byte_pointer)
if byte_size > 0
c = @line.byteslice(@byte_pointer, byte_size)
width = Reline::Unicode.get_mbchar_width(c)
@byte_pointer += byte_size
@cursor += width
end
end
vi_end_word(key, arg: arg) if arg > 0
end

Expand All @@ -1992,13 +2015,22 @@ def finish
vi_prev_big_word(key, arg: arg) if arg > 0
end

private def vi_end_big_word(key, arg: 1)
private def vi_end_big_word(key, arg: 1, inclusive: false)
if @line.bytesize > @byte_pointer
byte_size, width = Reline::Unicode.vi_big_forward_end_word(@line, @byte_pointer)
@byte_pointer += byte_size
@cursor += width
end
arg -= 1
if inclusive and arg.zero?
byte_size = Reline::Unicode.get_next_mbchar_size(@line, @byte_pointer)
if byte_size > 0
c = @line.byteslice(@byte_pointer, byte_size)
width = Reline::Unicode.get_mbchar_width(c)
@byte_pointer += byte_size
@cursor += width
end
end
vi_end_big_word(key, arg: arg) if arg > 0
end

Expand Down Expand Up @@ -2235,15 +2267,15 @@ def finish
}
end

private def vi_next_char(key, arg: 1)
@waiting_proc = ->(key_for_proc) { search_next_char(key_for_proc, arg) }
private def vi_next_char(key, arg: 1, inclusive: false)
@waiting_proc = ->(key_for_proc) { search_next_char(key_for_proc, arg, inclusive: inclusive) }
end

private def vi_to_next_char(key, arg: 1)
@waiting_proc = ->(key_for_proc) { search_next_char(key_for_proc, arg, true) }
private def vi_to_next_char(key, arg: 1, inclusive: false)
@waiting_proc = ->(key_for_proc) { search_next_char(key_for_proc, arg, need_prev_char: true, inclusive: inclusive) }
end

private def search_next_char(key, arg, need_prev_char = false)
private def search_next_char(key, arg, need_prev_char: false, inclusive: false)
if key.instance_of?(String)
inputed_char = key
else
Expand Down Expand Up @@ -2280,6 +2312,15 @@ def finish
@byte_pointer += byte_size
@cursor += width
end
if inclusive
byte_size = Reline::Unicode.get_next_mbchar_size(@line, @byte_pointer)
if byte_size > 0
c = @line.byteslice(@byte_pointer, byte_size)
width = Reline::Unicode.get_mbchar_width(c)
@byte_pointer += byte_size
@cursor += width
end
end
@waiting_proc = nil
end

Expand Down
63 changes: 61 additions & 2 deletions test/reline/test_key_actor_vi.rb
Expand Up @@ -1224,8 +1224,8 @@ def test_vi_delete_meta_with_vi_next_char
input_keys('df_')
assert_byte_pointer_size('aaa bbb ')
assert_cursor(8)
assert_cursor_max(15)
assert_line('aaa bbb ___ ddd')
assert_cursor_max(14)
assert_line('aaa bbb __ ddd')
end

def test_vi_delete_meta_with_arg
Expand Down Expand Up @@ -1294,4 +1294,63 @@ def test_vi_yank
assert_cursor(2)
assert_cursor_max(10)
end

def test_vi_end_word_with_operator
input_keys("foo bar\C-[0")
assert_line('foo bar')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(7)
input_keys('de')
assert_line(' bar')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(4)
input_keys('de')
assert_line('')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(0)
input_keys('de')
assert_line('')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(0)
end

def test_vi_end_big_word_with_operator
input_keys("aaa b{b}}}b\C-[0")
assert_line('aaa b{b}}}b')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(13)
input_keys('dE')
assert_line(' b{b}}}b')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(10)
input_keys('dE')
assert_line('')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(0)
input_keys('dE')
assert_line('')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(0)
end

def test_vi_next_char_with_operator
input_keys("foo bar\C-[0")
assert_line('foo bar')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(7)
input_keys('df ')
assert_line('bar')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(3)
end
end

0 comments on commit ba8e5f7

Please sign in to comment.