Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vi_to_column which was broken #679

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,6 @@ def render_differential
new_lines.size - y
end

def current_row
wrapped_lines.flatten[wrapped_cursor_y]
end

def upper_space_height(wrapped_cursor_y)
wrapped_cursor_y - screen_scroll_top
end
Expand Down Expand Up @@ -2483,18 +2479,11 @@ def finish
end

private def vi_to_column(key, arg: 0)
current_row_width = calculate_width(current_row)
@byte_pointer, = current_line.grapheme_clusters.inject([0, 0]) { |total, gc|
# total has [byte_size, cursor]
# Implementing behavior of vi, not Readline's vi-mode.
@byte_pointer, = current_line.grapheme_clusters.inject([0, 0]) { |(total_byte_size, total_width), gc|
mbchar_width = Reline::Unicode.get_mbchar_width(gc)
if (total.last + mbchar_width) >= arg
break total
elsif (total.last + mbchar_width) >= current_row_width
break total
else
total = [total.first + gc.bytesize, total.last + mbchar_width]
total
end
break [total_byte_size, total_width] if (total_width + mbchar_width) >= arg
[total_byte_size + gc.bytesize, total_width + mbchar_width]
}
end

Expand Down
14 changes: 14 additions & 0 deletions test/reline/test_key_actor_vi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,20 @@ def test_ed_move_to_beg
assert_line_around_cursor('', ' abcde ABCDE ')
end

def test_vi_to_column
input_keys("a一二三\C-[0")
input_keys('1|')
assert_line_around_cursor('', 'a一二三')
input_keys('2|')
assert_line_around_cursor('a', '一二三')
input_keys('3|')
assert_line_around_cursor('a', '一二三')
input_keys('4|')
assert_line_around_cursor('a一', '二三')
input_keys('9|')
assert_line_around_cursor('a一二', '三')
end

def test_vi_delete_meta
input_keys("aaa bbb ccc ddd eee\C-[02w")
assert_line_around_cursor('aaa bbb ', 'ccc ddd eee')
Expand Down
Loading