Skip to content

Commit

Permalink
Fix pasting tab-indented code crash (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Jan 4, 2024
1 parent 38e9cb2 commit 90155fd
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/reline/line_editor.rb
Expand Up @@ -1313,7 +1313,7 @@ def editing_mode
end
if not just_show_list and target < completed
@line = (preposing + completed + completion_append_character.to_s + postposing).split("\n")[@line_index] || String.new(encoding: @encoding)
line_to_pointer = (preposing + completed + completion_append_character.to_s).split("\n").last || String.new(encoding: @encoding)
line_to_pointer = (preposing + completed + completion_append_character.to_s).split("\n")[@line_index] || String.new(encoding: @encoding)
@cursor_max = calculate_width(@line)
@cursor = calculate_width(line_to_pointer)
@byte_pointer = line_to_pointer.bytesize
Expand Down Expand Up @@ -1360,7 +1360,7 @@ def editing_mode
completed = @completion_journey_data.list[@completion_journey_data.pointer]
new_line = (@completion_journey_data.preposing + completed + @completion_journey_data.postposing).split("\n")[@line_index]
@line = new_line.nil? ? String.new(encoding: @encoding) : new_line
line_to_pointer = (@completion_journey_data.preposing + completed).split("\n").last
line_to_pointer = (@completion_journey_data.preposing + completed).split("\n")[@line_index]
line_to_pointer = String.new(encoding: @encoding) if line_to_pointer.nil?
@cursor_max = calculate_width(@line)
@cursor = calculate_width(line_to_pointer)
Expand Down
4 changes: 4 additions & 0 deletions test/reline/yamatanooroti/multiline_repl
Expand Up @@ -153,6 +153,10 @@ opt.on('--autocomplete') {
%w{String Struct Symbol ScriptError SyntaxError Signal}.select{ |c| c.start_with?(target) }
}
}
opt.on('--autocomplete-empty') {
Reline.autocompletion = true
Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil| [] }
}
opt.on('--autocomplete-long') {
Reline.autocompletion = true
Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil|
Expand Down
26 changes: 26 additions & 0 deletions test/reline/yamatanooroti/test_rendering.rb
Expand Up @@ -1213,6 +1213,32 @@ def test_autocomplete
EOC
end

def test_autocomplete_empty_string
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --autocomplete}, startup_message: 'Multiline REPL.')
write("\C-i")
close
assert_screen(<<~'EOC')
Multiline REPL.
prompt> String
String █
Struct ▀
Symbol
EOC
end

def test_paste_code_with_tab_indent_does_not_fail
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/test/reline/yamatanooroti/multiline_repl --autocomplete-empty}, startup_message: 'Multiline REPL.')
write("2.times do\n\tputs\n\tputs\nend")
close
assert_screen(<<~'EOC')
Multiline REPL.
prompt> 2.times do
prompt> puts
prompt> puts
prompt> end
EOC
end

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

0 comments on commit 90155fd

Please sign in to comment.