Skip to content

Commit 9e93ad5

Browse files
authored
Fix vi_to_column which was broken (#679)
1 parent 155f704 commit 9e93ad5

2 files changed

Lines changed: 18 additions & 15 deletions

File tree

lib/reline/line_editor.rb

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -540,10 +540,6 @@ def render_differential
540540
new_lines.size - y
541541
end
542542

543-
def current_row
544-
wrapped_lines.flatten[wrapped_cursor_y]
545-
end
546-
547543
def upper_space_height(wrapped_cursor_y)
548544
wrapped_cursor_y - screen_scroll_top
549545
end
@@ -2483,18 +2479,11 @@ def finish
24832479
end
24842480

24852481
private def vi_to_column(key, arg: 0)
2486-
current_row_width = calculate_width(current_row)
2487-
@byte_pointer, = current_line.grapheme_clusters.inject([0, 0]) { |total, gc|
2488-
# total has [byte_size, cursor]
2482+
# Implementing behavior of vi, not Readline's vi-mode.
2483+
@byte_pointer, = current_line.grapheme_clusters.inject([0, 0]) { |(total_byte_size, total_width), gc|
24892484
mbchar_width = Reline::Unicode.get_mbchar_width(gc)
2490-
if (total.last + mbchar_width) >= arg
2491-
break total
2492-
elsif (total.last + mbchar_width) >= current_row_width
2493-
break total
2494-
else
2495-
total = [total.first + gc.bytesize, total.last + mbchar_width]
2496-
total
2497-
end
2485+
break [total_byte_size, total_width] if (total_width + mbchar_width) >= arg
2486+
[total_byte_size + gc.bytesize, total_width + mbchar_width]
24982487
}
24992488
end
25002489

test/reline/test_key_actor_vi.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,20 @@ def test_ed_move_to_beg
711711
assert_line_around_cursor('', ' abcde ABCDE ')
712712
end
713713

714+
def test_vi_to_column
715+
input_keys("a一二三\C-[0")
716+
input_keys('1|')
717+
assert_line_around_cursor('', 'a一二三')
718+
input_keys('2|')
719+
assert_line_around_cursor('a', '一二三')
720+
input_keys('3|')
721+
assert_line_around_cursor('a', '一二三')
722+
input_keys('4|')
723+
assert_line_around_cursor('a一', '二三')
724+
input_keys('9|')
725+
assert_line_around_cursor('a一二', '三')
726+
end
727+
714728
def test_vi_delete_meta
715729
input_keys("aaa bbb ccc ddd eee\C-[02w")
716730
assert_line_around_cursor('aaa bbb ', 'ccc ddd eee')

0 commit comments

Comments
 (0)