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 line rendering when newline is added at the end of the buffer #507

Merged
merged 1 commit into from
Feb 6, 2023
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
17 changes: 12 additions & 5 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def rerender
rendered = false
if @add_newline_to_end_of_buffer
clear_dialog_with_content
rerender_added_newline(prompt, prompt_width)
rerender_added_newline(prompt, prompt_width, prompt_list)
@add_newline_to_end_of_buffer = false
else
if @just_cursor_moving and not @rerender_all
Expand Down Expand Up @@ -969,11 +969,19 @@ def add_dialog_proc(name, p, context = nil)
end
end

private def rerender_added_newline(prompt, prompt_width)
scroll_down(1)
private def rerender_added_newline(prompt, prompt_width, prompt_list)
@buffer_of_lines[@previous_line_index] = @line
@line = @buffer_of_lines[@line_index]
unless @in_pasting
@previous_line_index = nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you move this line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I move it because I am using whole_lines in line 979.

In other place of line_editor.rb, whole_lines is called in this form

if @previous_line_index
  whole_lines(index: @previous_line_index, line: @line)
else
  whole_lines
end

whole_lines also depends on @buffer_of_lines, @line and @line_index. Calling whole_lines between changing these instance variables seems dangerous for me.
After moving it, @buffer_of_lines @line @previous_line_index is changed all at once.

If I don't move it, it still works but won't work after #460 is merged.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I understood. Thank you for your reply.

if @in_pasting
scroll_down(1)
else
lines = whole_lines
prev_line_prompt = @prompt_proc ? prompt_list[@line_index - 1] : prompt
prev_line_prompt_width = @prompt_proc ? calculate_width(prev_line_prompt, true) : prompt_width
prev_line = modify_lines(lines)[@line_index - 1]
render_partial(prev_line_prompt, prev_line_prompt_width, prev_line, @first_line_started_from + @started_from, with_control: false)
scroll_down(1)
render_partial(prompt, prompt_width, @line, @first_line_started_from + @started_from + 1, with_control: false)
end
@cursor = @cursor_max = calculate_width(@line)
Expand All @@ -982,7 +990,6 @@ def add_dialog_proc(name, p, context = nil)
@highest_in_this = calculate_height_by_width(prompt_width + @cursor_max)
@first_line_started_from += @started_from + 1
@started_from = calculate_height_by_width(prompt_width + @cursor) - 1
@previous_line_index = nil
end

def just_move_cursor
Expand Down
13 changes: 13 additions & 0 deletions test/reline/yamatanooroti/test_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,19 @@ def test_auto_indent_when_inserting_line
EOC
end

def test_newline_after_wrong_indent
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --auto-indent}, startup_message: 'Multiline REPL.')
write "if 1\n aa"
write "\n"
close
assert_screen(<<~EOC)
Multiline REPL.
prompt> if 1
prompt> aa
prompt>
EOC
end

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