Skip to content

Commit

Permalink
Merge pull request #178 from aycabta/indent-correctly-with-complex-co…
Browse files Browse the repository at this point in the history
…rredponding

Indent correctly with complex corredponding keyword
  • Loading branch information
aycabta authored Jan 24, 2021
2 parents fc3e1d9 + e7c68e7 commit 97cb2e2
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 3 deletions.
55 changes: 52 additions & 3 deletions lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,24 @@ def process_nesting_level(tokens = @tokens)
indent
end

def is_method_calling?(tokens, index)
tk = tokens[index]
if tk[3].anybits?(Ripper::EXPR_CMDARG) and tk[1] == :on_ident
# The target method call to pass the block with "do".
return true
elsif tk[3].anybits?(Ripper::EXPR_ARG) and tk[1] == :on_ident
non_sp_index = tokens[0..(index - 1)].rindex{ |t| t[1] != :on_sp }
if non_sp_index
prev_tk = tokens[non_sp_index]
if prev_tk[3].anybits?(Ripper::EXPR_DOT) and prev_tk[1] == :on_period
# The target method call with receiver to pass the block with "do".
return true
end
end
end
false
end

def take_corresponding_syntax_to_kw_do(tokens, index)
syntax_of_do = nil
# Finding a syntax correnponding to "do".
Expand All @@ -437,8 +455,7 @@ def take_corresponding_syntax_to_kw_do(tokens, index)
elsif [:on_ignored_nl, :on_nl, :on_comment].include?(tokens[non_sp_index][1])
first_in_fomula = true
end
if tk[3].anybits?(Ripper::EXPR_CMDARG) and tk[1] == :on_ident
# The target method call to pass the block with "do".
if is_method_calling?(tokens, i)
syntax_of_do = :method_calling
break if first_in_fomula
elsif tk[1] == :on_kw && %w{while until for}.include?(tk[2])
Expand All @@ -456,6 +473,34 @@ def take_corresponding_syntax_to_kw_do(tokens, index)
syntax_of_do
end

def is_the_in_correspond_to_a_for(tokens, index)
syntax_of_in = nil
# Finding a syntax correnponding to "do".
index.downto(0) do |i|
tk = tokens[i]
# In "continue", the token isn't the corresponding syntax to "do".
non_sp_index = tokens[0..(i - 1)].rindex{ |t| t[1] != :on_sp }
first_in_fomula = false
if non_sp_index.nil?
first_in_fomula = true
elsif [:on_ignored_nl, :on_nl, :on_comment].include?(tokens[non_sp_index][1])
first_in_fomula = true
end
if tk[1] == :on_kw && tk[2] == 'for'
# A loop syntax in front of "do" found.
#
# while cond do # also "until" or "for"
# end
#
# This "do" doesn't increment indent because the loop syntax already
# incremented.
syntax_of_in = :for
end
break if first_in_fomula
end
syntax_of_in
end

def check_newline_depth_difference
depth_difference = 0
open_brace_on_line = 0
Expand Down Expand Up @@ -511,8 +556,12 @@ def check_newline_depth_difference
unless t[3].allbits?(Ripper::EXPR_LABEL)
depth_difference += 1
end
when 'else', 'elsif', 'ensure', 'when', 'in'
when 'else', 'elsif', 'ensure', 'when'
depth_difference += 1
when 'in'
unless is_the_in_correspond_to_a_for(@tokens, index)
depth_difference += 1
end
when 'end'
depth_difference -= 1
end
Expand Down
96 changes: 96 additions & 0 deletions test/irb/test_ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,102 @@ def test_corresponding_syntax_to_keyword_do
end
end

def test_corresponding_syntax_to_keyword_for
input_with_correct_indents = [
Row.new(%q(for i in [1]), nil, 2, 1),
Row.new(%q( puts i), nil, 2, 1),
Row.new(%q(end), 0, 0, 0),
]

lines = []
input_with_correct_indents.each do |row|
lines << row.content
assert_indenting(lines, row.current_line_spaces, false)
assert_indenting(lines, row.new_line_spaces, true)
assert_nesting_level(lines, row.nesting_level)
end
end

def test_corresponding_syntax_to_keyword_for_with_do
input_with_correct_indents = [
Row.new(%q(for i in [1] do), nil, 2, 1),
Row.new(%q( puts i), nil, 2, 1),
Row.new(%q(end), 0, 0, 0),
]

lines = []
input_with_correct_indents.each do |row|
lines << row.content
assert_indenting(lines, row.current_line_spaces, false)
assert_indenting(lines, row.new_line_spaces, true)
assert_nesting_level(lines, row.nesting_level)
end
end

def test_bracket_corresponding_to_times
input_with_correct_indents = [
Row.new(%q(3.times { |i|), nil, 2, 1),
Row.new(%q( puts i), nil, 2, 1),
Row.new(%q(}), 0, 0, 0),
]

lines = []
input_with_correct_indents.each do |row|
lines << row.content
assert_indenting(lines, row.current_line_spaces, false)
assert_indenting(lines, row.new_line_spaces, true)
assert_nesting_level(lines, row.nesting_level)
end
end

def test_do_corresponding_to_times
input_with_correct_indents = [
Row.new(%q(3.times do |i|), nil, 2, 1),
#Row.new(%q( puts i), nil, 2, 1),
#Row.new(%q(end), 0, 0, 0),
]

lines = []
input_with_correct_indents.each do |row|
lines << row.content
assert_indenting(lines, row.current_line_spaces, false)
assert_indenting(lines, row.new_line_spaces, true)
assert_nesting_level(lines, row.nesting_level)
end
end

def test_bracket_corresponding_to_loop
input_with_correct_indents = [
Row.new(%q(loop {), nil, 2, 1),
Row.new(%q( 3), nil, 2, 1),
Row.new(%q(}), 0, 0, 0),
]

lines = []
input_with_correct_indents.each do |row|
lines << row.content
assert_indenting(lines, row.current_line_spaces, false)
assert_indenting(lines, row.new_line_spaces, true)
assert_nesting_level(lines, row.nesting_level)
end
end

def test_do_corresponding_to_loop
input_with_correct_indents = [
Row.new(%q(loop do), nil, 2, 1),
Row.new(%q( 3), nil, 2, 1),
Row.new(%q(end), 0, 0, 0),
]

lines = []
input_with_correct_indents.each do |row|
lines << row.content
assert_indenting(lines, row.current_line_spaces, false)
assert_indenting(lines, row.new_line_spaces, true)
assert_nesting_level(lines, row.nesting_level)
end
end

def test_heredoc_with_indent
input_with_correct_indents = [
Row.new(%q(<<~Q), nil, 0, 0),
Expand Down

0 comments on commit 97cb2e2

Please sign in to comment.