Skip to content

Commit d4ad9b9

Browse files
tompngst0012
andauthored
Fix cursor position overrun (#515)
* Fix cursor position overrun * Remove unnecessary local variable Co-authored-by: Stan Lo <[email protected]> --------- Co-authored-by: Stan Lo <[email protected]>
1 parent af54185 commit d4ad9b9

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

lib/reline/line_editor.rb

+3-7
Original file line numberDiff line numberDiff line change
@@ -995,22 +995,18 @@ def just_move_cursor
995995
calculate_height_by_lines(@buffer_of_lines[0..(@line_index - 1)], prompt_list || prompt)
996996
end
997997
first_line_diff = new_first_line_started_from - @first_line_started_from
998-
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)
999-
new_started_from = calculate_height_by_width(prompt_width + new_cursor) - 1
998+
@cursor, @cursor_max, _, @byte_pointer = calculate_nearest_cursor(@buffer_of_lines[@line_index], @cursor, @started_from, @byte_pointer, false)
999+
new_started_from = calculate_height_by_width(prompt_width + @cursor) - 1
10001000
calculate_scroll_partial_screen(@highest_in_all, new_first_line_started_from + new_started_from)
10011001
@previous_line_index = nil
1002+
@line = @buffer_of_lines[@line_index]
10021003
if @rerender_all
1003-
@line = @buffer_of_lines[@line_index]
10041004
rerender_all_lines
10051005
@rerender_all = false
10061006
true
10071007
else
1008-
@line = @buffer_of_lines[@line_index]
10091008
@first_line_started_from = new_first_line_started_from
10101009
@started_from = new_started_from
1011-
@cursor = new_cursor
1012-
@cursor_max = new_cursor_max
1013-
@byte_pointer = new_byte_pointer
10141010
move_cursor_down(first_line_diff + @started_from)
10151011
Reline::IOGate.move_cursor_column((prompt_width + @cursor) % @screen_size.last)
10161012
false

test/reline/yamatanooroti/test_rendering.rb

+24
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,30 @@ def each_top_level_statement
661661
EOC
662662
end
663663

664+
def test_longer_than_screen_height_nearest_cursor_with_scroll_back
665+
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl}, startup_message: 'Multiline REPL.')
666+
write(<<~EOC.chomp)
667+
if 1
668+
if 2
669+
if 3
670+
if 4
671+
puts
672+
end
673+
end
674+
end
675+
end
676+
EOC
677+
write("\C-p" * 4 + "\C-e" + "\C-p" * 4)
678+
write("2")
679+
assert_screen(<<~EOC)
680+
prompt> if 12
681+
prompt> if 2
682+
prompt> if 3
683+
prompt> if 4
684+
prompt> puts
685+
EOC
686+
end
687+
664688
def test_update_cursor_correctly_when_just_cursor_moving
665689
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl}, startup_message: 'Multiline REPL.')
666690
write("def hoge\n 01234678")

0 commit comments

Comments
 (0)