Skip to content

Commit 7c24276

Browse files
aycabtaJuanitoFatas
andcommitted
Suppress crashing when auto_indent_proc returns broken indent info
Co-authored-by: Juanito Fatas <me@juanitofatas.com>
1 parent d7fbaed commit 7c24276

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

bin/multiline_repl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ opt.on('--dynamic-prompt') {
2222
}
2323
}
2424
}
25+
opt.on('--auto-indent') {
26+
AutoIndent.new
27+
}
2528
opt.parse!(ARGV)
2629

2730
begin

bin/termination_checker.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,15 @@ def terminated?(code)
1616
end
1717
end
1818
end
19+
20+
class AutoIndent < RubyLex
21+
def initialize
22+
set_input(self)
23+
context = Struct.new(:auto_indent_mode).new(true)
24+
set_auto_indent(context)
25+
end
26+
27+
def auto_indent(&block)
28+
Reline.auto_indent_proc = block
29+
end
30+
end

lib/reline/line_editor.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,7 @@ def call_completion_proc
11311131
new_lines = whole_lines
11321132
end
11331133
new_indent = @auto_indent_proc.(new_lines, @line_index, @byte_pointer, @check_new_auto_indent)
1134+
new_indent = @cursor_max if new_indent&.> @cursor_max
11341135
if new_indent&.>= 0
11351136
md = new_lines[@line_index].match(/\A */)
11361137
prev_indent = md[0].count(' ')

test/reline/yamatanooroti/test_rendering.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,20 @@ def test_update_cursor_correctly_when_just_cursor_moving
637637
EOC
638638
end
639639

640+
def test_suppress_auto_indent_just_after_pasted
641+
start_terminal(5, 30, %W{ruby -I#{@pwd}/lib #{@pwd}/bin/multiline_repl --auto-indent}, startup_message: 'Multiline REPL.')
642+
write("def hoge\n [[\n 3]]\ned")
643+
write("\C-bn")
644+
close
645+
assert_screen(<<~EOC)
646+
Multiline REPL.
647+
prompt> def hoge
648+
prompt> [[
649+
prompt> 3]]
650+
prompt> end
651+
EOC
652+
end
653+
640654
private def write_inputrc(content)
641655
File.open(@inputrc_file, 'w') do |f|
642656
f.write content

0 commit comments

Comments
 (0)