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 wrong byte_pointer passed to auto_indent_proc #562

Merged
merged 1 commit into from
Jul 5, 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
2 changes: 1 addition & 1 deletion lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1645,7 +1645,7 @@ def call_completion_proc_with_checking_args(pre, target, post)
@line = ' ' * new_indent + @line.lstrip

new_indent = nil
result = @auto_indent_proc.(new_lines[0..-2], @line_index - 1, (new_lines[-2].size + 1), false)
result = @auto_indent_proc.(new_lines[0..-2], @line_index - 1, (new_lines[@line_index - 1].bytesize + 1), false)
Copy link
Member Author

Choose a reason for hiding this comment

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

Third arg is a byte_pointer, so it should use bytesize instead of size

I think the intention of this line is to pass line_number and new_lines[line_number].bytesize + 1.
But new_lines[-2] was not always new_lines[@line_index - 1]. Fixed the index.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation! Would you mind extracting these arguments into local variables so they'd be self-documenting?

Copy link
Member Author

Choose a reason for hiding this comment

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

Can I let it as is?
It would be readable if it is assigned to local variable, but I have problem with naming.

prev_line_index = @line_index - 1
prev_line = lines[prev_line_index]

This seems good, but there is @previous_line_index with probably different meaning. I don't have alternate idea that is not confusing.

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. Let's keep it as it is then 👍

if result
new_indent = result
end
Expand Down
18 changes: 18 additions & 0 deletions test/reline/yamatanooroti/test_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,24 @@ def test_auto_indent_when_inserting_line
EOC
end

def test_auto_indent_multibyte_insert_line
start_terminal(10, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --auto-indent}, startup_message: 'Multiline REPL.')
write "if true\n"
write "あいうえお\n"
4.times { write "\C-b\C-b\C-b\C-b\e\r" }
close
assert_screen(<<~EOC)
Multiline REPL.
prompt> if true
prompt> あ
prompt> い
prompt> う
prompt> え
prompt> お
prompt>
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"
Expand Down