[ruby/irb] Examine indentation of in keyword when trying to type include

Use in_keyword_case_scope?

Return fast

https://github.com/ruby/irb/commit/8acc7f8dc7
This commit is contained in:
Kaíque Kandy Koga 2021-10-25 18:32:40 -03:00 committed by git
parent d486286f1d
commit 6b64e78823
2 changed files with 38 additions and 1 deletions

View File

@ -693,8 +693,12 @@ class RubyLex
unless t.state.allbits?(Ripper::EXPR_LABEL)
spaces_of_nest.push(spaces_at_line_head)
end
when 'else', 'elsif', 'ensure', 'when', 'in'
when 'else', 'elsif', 'ensure', 'when'
corresponding_token_depth = spaces_of_nest.last
when 'in'
if in_keyword_case_scope?
corresponding_token_depth = spaces_of_nest.last
end
when 'end'
if is_first_printable_of_line
corresponding_token_depth = spaces_of_nest.pop
@ -837,5 +841,21 @@ class RubyLex
heredoc_tokens = @tokens.select { |t| [:on_heredoc_beg, :on_heredoc_end].include?(t.event) }
heredoc_tokens[-1]&.event == :on_heredoc_beg
end
def in_keyword_case_scope?
kw_tokens = @tokens.select { |t| t.event == :on_kw && ['case', 'for', 'end'].include?(t.tok) }
counter = 0
kw_tokens.reverse.each do |t|
if t.tok == 'case'
return true if counter.zero?
counter += 1
elsif t.tok == 'for'
counter += 1
elsif t.tok == 'end'
counter -= 1
end
end
false
end
end
# :startdoc:

View File

@ -399,6 +399,23 @@ module TestIRB
end
end
def test_corresponding_syntax_to_keyword_in
input_with_correct_indents = [
Row.new(%q(module E), nil, 2, 1),
Row.new(%q(end), 0, 0, 0),
Row.new(%q(class A), nil, 2, 1),
Row.new(%q( in), nil, 4, 1)
]
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),