8000 Fix crashing when multiple open braces per line · ruby/irb@80c69c8 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 80c69c8

Browse files
committed
Fix crashing when multiple open braces per line
#55 If we had put multiple open braces on a line the with no closing brace spaces_of_nest array keeps getting '0' added to it. This means that when we pop off of this array we are saying that we should be in position zero for the next line. This is an issue because we don't always want to be in position 0 after a closing brace. Example: ``` [[[ ] ] ] ``` In the above example the 'spaces_of_nest' array looks like this after the first line is entered: [0,0,0]. We really want to be indented 4 spaces for the 1st closing brace 2 for the 2nd and 0 for the 3rd. i.e. we want it to be: [0,2,4]. We also saw this issue with a heredoc inside of an array. ``` [<<FOO] hello FOO ```
1 parent 7dc8af0 commit 80c69c8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lib/irb/ruby-lex.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,15 @@ def check_corresponding_token_depth
368368
is_first_printable_of_line = true
369369
spaces_of_nest = []
370370
spaces_at_line_head = 0
371+
open_brace_on_line = 0
371372
@tokens.each_with_index do |t, index|
372373
case t[1]
373374
when :on_ignored_nl, :on_nl, :on_comment
374375
corresponding_token_depth = nil
375376
spaces_at_line_head = 0
376377
is_first_spaces_of_line = true
377378
is_first_printable_of_line = true
379+
open_brace_on_line = 0
378380
next
379381
when :on_sp
380382
spaces_at_line_head = t[2].count(' ') if is_first_spaces_of_line
@@ -383,7 +385,8 @@ def check_corresponding_token_depth
383385
end
384386
case t[1]
385387
when :on_lbracket, :on_lbrace, :on_lparen
386-
spaces_of_nest.push(spaces_at_line_head)
388+
spaces_of_nest.push(spaces_at_line_head + open_brace_on_line * 2)
389+
open_brace_on_line += 1
387390
when :on_rbracket, :on_rbrace, :on_rparen
388391
if is_first_printable_of_line
389392
corresponding_token_depth = spaces_of_nest.pop

test/irb/test_ruby_lex.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,24 @@ def test_braces_on_thier_own_line
9090
assert_indenting(lines, row.new_line_spaces, true)
9191
end
9292
end
93+
94+
def test_multiple_braces_in_a_line
95+
input_with_correct_indents = [
96+
Row.new(%q([[[), nil, 6),
97+
Row.new(%q( ]), 4, 4),
98+
Row.new(%q( ]), 2, 2),
99+
Row.new(%q(]), 0, 0),
100+
Row.new(%q([<<FOO]), nil, 0),
101+
Row.new(%q(hello), nil, 0),
102+
Row.new(%q(FOO), nil, 0),
103+
]
104+
105+
lines = []
106+
input_with_correct_indents.each do |row|
107+
lines << row.content
108+
assert_indenting(lines, row.current_line_spaces, false)
109+
assert_indenting(lines, row.new_line_spaces, true)
110+
end
111+
end
93112
end
94113
end

0 commit comments

Comments
 (0)
0