From 6b64e788234c19560070192927ae7b35b19b4587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ka=C3=ADque=20Kandy=20Koga?= Date: Mon, 25 Oct 2021 18:32:40 -0300 Subject: [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 --- lib/irb/ruby-lex.rb | 22 +++++++++++++++++++++- test/irb/test_ruby_lex.rb | 17 +++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 751d6ec526..f2069a2b5f 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -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: diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index 32463f7fb6..47435d675e 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -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), -- cgit v1.2.3