Skip to content

Commit

Permalink
Use typed spaces when the line is inside the here documents
Browse files Browse the repository at this point in the history
Use first method instead of square brackets to support 2.5 and 2.6 versions
  • Loading branch information
kaiquekandykoga committed Sep 20, 2021
1 parent 1ca678b commit f3481ef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 12 additions & 1 deletion lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ 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 = if heredoc_scope?(line_index)
lines[line_index][/^ */].length
else
check_corresponding_token_depth
end
if corresponding_token_depth
corresponding_token_depth
else
Expand Down Expand Up @@ -817,5 +821,12 @@ def check_termination_in_prev_line(code, context: nil)
end
false
end

private

def heredoc_scope?(line_index)
tokens = @tokens.first(line_index).select { |t| [:on_heredoc_beg, :on_heredoc_end].include?(t.event) }
tokens[-1]&.event == :on_heredoc_beg
end
end
# :startdoc:
6 changes: 3 additions & 3 deletions test/irb/test_ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,9 @@ def test_do_corresponding_to_loop
def test_heredoc_with_indent
input_with_correct_indents = [
Row.new(%q(<<~Q), nil, 0, 0),
Row.new(%q({), nil, 0, 0),
Row.new(%q( #), nil, 0, 0),
Row.new(%q(}), nil, 0, 0),
Row.new(%q({), 0, 0, 0),
Row.new(%q( #), 2, 0, 0),
Row.new(%q(}), 0, 0, 0)
]

lines = []
Expand Down

0 comments on commit f3481ef

Please sign in to comment.