Skip to content

Commit 43456dc

Browse files
committed
Newline in oneliner def doesn't reset indent
This closes #132.
1 parent ec83280 commit 43456dc

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

lib/irb/ruby-lex.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def check_newline_depth_difference
410410

411411
case t[1]
412412
when :on_ignored_nl, :on_nl, :on_comment
413-
if index != (@tokens.size - 1)
413+
if index != (@tokens.size - 1) and in_oneliner_def != :BODY
414414
depth_difference = 0
415415
open_brace_on_line = 0
416416
end
@@ -488,11 +488,13 @@ def check_corresponding_token_depth
488488

489489
case t[1]
490490
when :on_ignored_nl, :on_nl, :on_comment
491-
corresponding_token_depth = nil
492-
spaces_at_line_head = 0
493-
is_first_spaces_of_line = true
494-
is_first_printable_of_line = true
495-
open_brace_on_line = 0
491+
if in_oneliner_def != :BODY
492+
corresponding_token_depth = nil
493+
spaces_at_line_head = 0
494+
is_first_spaces_of_line = true
495+
is_first_printable_of_line = true
496+
open_brace_on_line = 0
497+
end
496498
next
497499
when :on_sp
498500
spaces_at_line_head = t[2].count(' ') if is_first_spaces_of_line

test/irb/test_ruby_lex.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,23 @@ def test_tlambda
263263
end
264264
end
265265

266+
def test_oneliner_def_in_multiple_lines
267+
input_with_correct_indents = [
268+
Row.new(%q(def a()=[), nil, 4, 2),
269+
Row.new(%q( 1,), nil, 4, 1),
270+
Row.new(%q(].), 0, 0, 0),
271+
Row.new(%q(to_s), nil, 0, 0),
272+
]
273+
274+
lines = []
275+
input_with_correct_indents.each do |row|
276+
lines << row.content
277+
assert_indenting(lines, row.current_line_spaces, false)
278+
assert_indenting(lines, row.new_line_spaces, true)
279+
assert_nesting_level(lines, row.nesting_level)
280+
end
281+
end
282+
266283
PromptRow = Struct.new(:prompt, :content)
267284

268285
class MockIO_DynamicPrompt

0 commit comments

Comments
 (0)