Skip to content

Commit

Permalink
[ruby/reline] Fix cursor position overrun
Browse files Browse the repository at this point in the history
(ruby/reline#515)

* Fix cursor position overrun

* Remove unnecessary local variable

Co-authored-by: Stan Lo <stan001212@gmail.com>

---------

ruby/reline@d4ad9b96c8

Co-authored-by: Stan Lo <stan001212@gmail.com>
  • Loading branch information
2 people authored and matzbot committed Mar 27, 2023
1 parent a2ff942 commit 8f569d4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
10 changes: 3 additions & 7 deletions lib/reline/line_editor.rb
Expand Up @@ -995,22 +995,18 @@ def just_move_cursor
calculate_height_by_lines(@buffer_of_lines[0..(@line_index - 1)], prompt_list || prompt)
end
first_line_diff = new_first_line_started_from - @first_line_started_from
new_cursor, new_cursor_max, new_started_from, new_byte_pointer = calculate_nearest_cursor(@buffer_of_lines[@line_index], @cursor, @started_from, @byte_pointer, false)
new_started_from = calculate_height_by_width(prompt_width + new_cursor) - 1
@cursor, @cursor_max, _, @byte_pointer = calculate_nearest_cursor(@buffer_of_lines[@line_index], @cursor, @started_from, @byte_pointer, false)
new_started_from = calculate_height_by_width(prompt_width + @cursor) - 1
calculate_scroll_partial_screen(@highest_in_all, new_first_line_started_from + new_started_from)
@previous_line_index = nil
@line = @buffer_of_lines[@line_index]
if @rerender_all
@line = @buffer_of_lines[@line_index]
rerender_all_lines
@rerender_all = false
true
else
@line = @buffer_of_lines[@line_index]
@first_line_started_from = new_first_line_started_from
@started_from = new_started_from
@cursor = new_cursor
@cursor_max = new_cursor_max
@byte_pointer = new_byte_pointer
move_cursor_down(first_line_diff + @started_from)
Reline::IOGate.move_cursor_column((prompt_width + @cursor) % @screen_size.last)
false
Expand Down
24 changes: 24 additions & 0 deletions test/reline/yamatanooroti/test_rendering.rb
Expand Up @@ -661,6 +661,30 @@ def each_top_level_statement
EOC
end

def test_longer_than_screen_height_nearest_cursor_with_scroll_back
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl}, startup_message: 'Multiline REPL.')
write(<<~EOC.chomp)
if 1
if 2
if 3
if 4
puts
end
end
end
end
EOC
write("\C-p" * 4 + "\C-e" + "\C-p" * 4)
write("2")
assert_screen(<<~EOC)
prompt> if 12
prompt> if 2
prompt> if 3
prompt> if 4
prompt> puts
EOC
end

def test_update_cursor_correctly_when_just_cursor_moving
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl}, startup_message: 'Multiline REPL.')
write("def hoge\n 01234678")
Expand Down

0 comments on commit 8f569d4

Please sign in to comment.