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

Use tokens
  • Loading branch information
kaiquekandykoga committed Sep 20, 2021
1 parent 1ca678b commit ec70c14
Showing 2 changed files with 25 additions and 12 deletions.
21 changes: 17 additions & 4 deletions lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
@@ -199,15 +199,15 @@ def set_auto_indent(context)
if is_newline
@tokens = self.class.ripper_lex_without_warning(lines[0..line_index].join("\n"), context: context)
prev_spaces = find_prev_spaces(line_index)
depth_difference = check_newline_depth_difference
depth_difference = check_newline_depth_difference(lines, line_index)
depth_difference = 0 if depth_difference < 0
prev_spaces + depth_difference * 2
else
code = line_index.zero? ? '' : lines[0..(line_index - 1)].map{ |l| l + "\n" }.join
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
@@ -534,10 +534,11 @@ def is_the_in_correspond_to_a_for(tokens, index)
syntax_of_in
end

def check_newline_depth_difference
def check_newline_depth_difference(lines, line_index)
depth_difference = 0
open_brace_on_line = 0
in_oneliner_def = nil

@tokens.each_with_index do |t, index|
# detecting one-liner method definition
if in_oneliner_def.nil?
@@ -603,14 +604,19 @@ 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
spaces_of_nest = []
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 +823,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:
16 changes: 8 additions & 8 deletions test/irb/test_ruby_lex.rb
Original file line number Diff line number Diff line change
@@ -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([<<FOO]), nil, 0),
Row.new(%q(hello), nil, 0),
Row.new(%q([<<FOO]), 0, 0),
Row.new(%q(hello), 0, 0),
Row.new(%q(FOO), nil, 0),
]

@@ -465,10 +465,10 @@ 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(<<~Q), 0, 0, 0),
Row.new(%q({), 0, 0, 0),
Row.new(%q( #), 2, 0, 0),
Row.new(%q(}), 0, 0, 0)
]

lines = []
@@ -503,8 +503,8 @@ def test_broken_heredoc
end
input_with_correct_indents = [
Row.new(%q(def foo), nil, 2, 1),
Row.new(%q( <<~Q), nil, 2, 1),
Row.new(%q( Qend), nil, 2, 1),
Row.new(%q( <<~Q), 2, 2, 1),
Row.new(%q( Qend), 2, 2, 1),
]

lines = []

0 comments on commit ec70c14

Please sign in to comment.