Skip to content

Commit

Permalink
Implement vi_prev_char and vi_to_prev_char
Browse files Browse the repository at this point in the history
  • Loading branch information
aycabta committed Jan 17, 2020
1 parent 066ecb0 commit 0ad3ee6
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
48 changes: 48 additions & 0 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2131,6 +2131,54 @@ def finish
@waiting_proc = nil
end

private def vi_prev_char(key, arg: 1)
@waiting_proc = ->(key_for_proc) { search_prev_char(key_for_proc, arg) }
end

private def vi_to_prev_char(key, arg: 1)
@waiting_proc = ->(key_for_proc) { search_prev_char(key_for_proc, arg, true) }
end

private def search_prev_char(key, arg, need_next_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(0..@byte_pointer).grapheme_clusters.reverse_each do |mbchar|
# total has [byte_size, cursor]
unless total
# skip cursor point
width = Reline::Unicode.get_mbchar_width(mbchar)
total = [mbchar.bytesize, width]
else
if inputed_char == mbchar
arg -= 1
if arg.zero?
found = true
break
end
end
width = Reline::Unicode.get_mbchar_width(mbchar)
prev_total = total
total = [total.first + mbchar.bytesize, total.last + width]
end
end
if not need_next_char and found and total
byte_size, width = total
@byte_pointer -= byte_size
@cursor -= width
elsif need_next_char and found and prev_total
byte_size, width = prev_total
@byte_pointer -= byte_size
@cursor -= width
end
@waiting_proc = nil
end

private def vi_join_lines(key, arg: 1)
if @is_multiline and @buffer_of_lines.size > @line_index + 1
@cursor = calculate_width(@line)
Expand Down
36 changes: 36 additions & 0 deletions test/reline/test_key_actor_vi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,42 @@ def test_vi_to_next_char
assert_cursor_max(6)
end

def test_vi_prev_char
input_keys("abcdef\C-[")
assert_line('abcdef')
assert_byte_pointer_size('abcde')
assert_cursor(5)
assert_cursor_max(6)
input_keys('Fz')
assert_line('abcdef')
assert_byte_pointer_size('abcde')
assert_cursor(5)
assert_cursor_max(6)
input_keys('Fa')
assert_line('abcdef')
assert_byte_pointer_size('')
assert_cursor(0)
assert_cursor_max(6)
end

def test_vi_to_prev_char
input_keys("abcdef\C-[")
assert_line('abcdef')
assert_byte_pointer_size('abcde')
assert_cursor(5)
assert_cursor_max(6)
input_keys('Tz')
assert_line('abcdef')
assert_byte_pointer_size('abcde')
assert_cursor(5)
assert_cursor_max(6)
input_keys('Ta')
assert_line('abcdef')
assert_byte_pointer_size('a')
assert_cursor(1)
assert_cursor_max(6)
end

def test_vi_delete_next_char
input_keys("abc\C-[h")
assert_byte_pointer_size('a')
Expand Down

0 comments on commit 0ad3ee6

Please sign in to comment.