1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/irb] Support oneline method definition

https://github.com/ruby/irb/commit/826ae909c9
This commit is contained in:
aycabta 2020-06-25 23:56:03 +09:00
parent 701217572f
commit 5474007d61
2 changed files with 106 additions and 0 deletions

View file

@ -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

View file

@ -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