diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 5d7bf006be..3568e66a66 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -303,7 +303,31 @@ class RubyLex def process_nesting_level indent = 0 + in_oneliner_def = nil @tokens.each_with_index { |t, index| + # detecting one-liner method definition + if in_oneliner_def.nil? + if t[3].allbits?(Ripper::EXPR_ENDFN) + in_oneliner_def = :ENDFN + end + else + if t[3].allbits?(Ripper::EXPR_ENDFN) + # continuing + elsif t[3].allbits?(Ripper::EXPR_BEG) + if t[2] == '=' + in_oneliner_def = :BODY + end + elsif t[3].allbits?(Ripper::EXPR_END) + if in_oneliner_def == :BODY + # one-liner method definition + indent -= 1 + end + in_oneliner_def = nil + else + in_oneliner_def = nil + end + end + case t[1] when :on_lbracket, :on_lbrace, :on_lparen indent += 1 @@ -338,7 +362,31 @@ class RubyLex def check_newline_depth_difference 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? + if t[3].allbits?(Ripper::EXPR_ENDFN) + in_oneliner_def = :ENDFN + end + else + if t[3].allbits?(Ripper::EXPR_ENDFN) + # continuing + elsif t[3].allbits?(Ripper::EXPR_BEG) + if t[2] == '=' + in_oneliner_def = :BODY + end + elsif t[3].allbits?(Ripper::EXPR_END) + if in_oneliner_def == :BODY + # one[-liner method definition + depth_difference -= 1 + end + in_oneliner_def = nil + else + in_oneliner_def = nil + end + end + case t[1] when :on_ignored_nl, :on_nl, :on_comment if index != (@tokens.size - 1) @@ -389,7 +437,36 @@ class RubyLex spaces_of_nest = [] spaces_at_line_head = 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? + if t[3].allbits?(Ripper::EXPR_ENDFN) + in_oneliner_def = :ENDFN + end + else + if t[3].allbits?(Ripper::EXPR_ENDFN) + # continuing + elsif t[3].allbits?(Ripper::EXPR_BEG) + if t[2] == '=' + in_oneliner_def = :BODY + end + elsif t[3].allbits?(Ripper::EXPR_END) + if in_oneliner_def == :BODY + # one-liner method definition + if is_first_printable_of_line + corresponding_token_depth = spaces_of_nest.pop + else + spaces_of_nest.pop + corresponding_token_depth = nil + end + end + in_oneliner_def = nil + else + in_oneliner_def = nil + end + end + case t[1] when :on_ignored_nl, :on_nl, :on_comment corresponding_token_depth = nil diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index afbad63eed..db15593f37 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -205,5 +205,34 @@ module TestIRB assert_indenting(lines, row.new_line_spaces, true) end end + + def test_oneliner_method_definition + input_with_correct_indents = [ + Row.new(%q(class A), nil, 2), + Row.new(%q( def foo0), nil, 4), + Row.new(%q( 3), nil, 4), + Row.new(%q( end), 2, 2), + Row.new(%q( def foo1()), nil, 4), + Row.new(%q( 3), nil, 4), + Row.new(%q( end), 2, 2), + Row.new(%q( def foo2(a, b)), nil, 4), + Row.new(%q( a + b), nil, 4), + Row.new(%q( end), 2, 2), + Row.new(%q( def foo3 a, b), nil, 4), + Row.new(%q( a + b), nil, 4), + Row.new(%q( end), 2, 2), + Row.new(%q( def bar0() = 3), nil, 2), + Row.new(%q( def bar1(a) = a), nil, 2), + Row.new(%q( def bar2(a, b) = a + b), nil, 2), + Row.new(%q(end), 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) + end + end end end