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

[ruby/reline] Implement vi_to_next_char

066ecb0a21
This commit is contained in:
aycabta 2020-01-17 13:44:07 +09:00
parent 9f99760daf
commit b17797a694
2 changed files with 30 additions and 2 deletions

View file

@ -2087,12 +2087,17 @@ class Reline::LineEditor
@waiting_proc = ->(key_for_proc) { search_next_char(key_for_proc, arg) }
end
private def search_next_char(key, arg)
private def vi_to_next_char(key, arg: 1)
@waiting_proc = ->(key_for_proc) { search_next_char(key_for_proc, arg, true) }
end
private def search_next_char(key, arg, need_prev_char = false)
if key.instance_of?(String)
inputed_char = key
else
inputed_char = key.chr
end
prev_total = nil
total = nil
found = false
@line.byteslice(@byte_pointer..-1).grapheme_clusters.each do |mbchar|
@ -2110,13 +2115,18 @@ class Reline::LineEditor
end
end
width = Reline::Unicode.get_mbchar_width(mbchar)
prev_total = total
total = [total.first + mbchar.bytesize, total.last + width]
end
end
if found and total
if not need_prev_char and found and total
byte_size, width = total
@byte_pointer += byte_size
@cursor += width
elsif need_prev_char and found and prev_total
byte_size, width = prev_total
@byte_pointer += byte_size
@cursor += width
end
@waiting_proc = nil
end

View file

@ -633,6 +633,24 @@ class Reline::KeyActor::ViInsert::Test < Reline::TestCase
assert_cursor_max(6)
end
def test_vi_to_next_char
input_keys("abcdef\C-[0")
assert_line('abcdef')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(6)
input_keys('tz')
assert_line('abcdef')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(6)
input_keys('te')
assert_line('abcdef')
assert_byte_pointer_size('abc')
assert_cursor(3)
assert_cursor_max(6)
end
def test_vi_delete_next_char
input_keys("abc\C-[h")
assert_byte_pointer_size('a')