diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index e28116978..f0c056e29 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -207,7 +207,7 @@ def set_auto_indent(context) last_line = lines[line_index]&.byteslice(0, byte_pointer) code += last_line if last_line @tokens = self.class.ripper_lex_without_warning(code, context: context) - corresponding_token_depth = check_corresponding_token_depth + corresponding_token_depth = check_corresponding_token_depth(lines, line_index) if corresponding_token_depth corresponding_token_depth else @@ -603,7 +603,7 @@ def check_newline_depth_difference depth_difference end - def check_corresponding_token_depth + def check_corresponding_token_depth(lines, line_index) corresponding_token_depth = nil is_first_spaces_of_line = true is_first_printable_of_line = true @@ -611,6 +611,11 @@ def check_corresponding_token_depth spaces_at_line_head = 0 open_brace_on_line = 0 in_oneliner_def = nil + + if heredoc_scope? + return lines[line_index][/^ */].length + end + @tokens.each_with_index do |t, index| # detecting one-liner method definition if in_oneliner_def.nil? @@ -817,5 +822,12 @@ def check_termination_in_prev_line(code, context: nil) end false end + + private + + def heredoc_scope? + heredoc_tokens = @tokens.select { |t| [:on_heredoc_beg, :on_heredoc_end].include?(t.event) } + heredoc_tokens[-1]&.event == :on_heredoc_beg + end end # :startdoc: diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index e255720e2..e02370d3f 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -109,8 +109,8 @@ def test_multiple_braces_in_a_line Row.new(%q( ]), 4, 4), Row.new(%q( ]), 2, 2), Row.new(%q(]), 0, 0), - Row.new(%q([<