summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBen <kanobt61@gmail.com>2020-01-05 14:44:38 -0500
committeraycabta <aycabta@gmail.com>2020-01-14 15:40:38 +0900
commitc94025b63091be5b5e83a2f5ab5dc8d6c6147b84 (patch)
treeeaee6c40fb128d764e5fcc297a0da822e03ab76c /lib
parent9994eb8a5e72ff68ee2a13ddeff8d9307ba7cd84 (diff)
[ruby/irb] Fix crashing when multiple open braces per line
https://github.com/ruby/irb/issues/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 ``` https://github.com/ruby/irb/commit/80c69c8272
Diffstat (limited to 'lib')
-rw-r--r--lib/irb/ruby-lex.rb5
1 files changed, 4 insertions, 1 deletions
diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb
index 0a5e2c45b6..3d1478d8ce 100644
--- a/lib/irb/ruby-lex.rb
+++ b/lib/irb/ruby-lex.rb
@@ -368,6 +368,7 @@ class RubyLex
is_first_printable_of_line = true
spaces_of_nest = []
spaces_at_line_head = 0
+ open_brace_on_line = 0
@tokens.each_with_index do |t, index|
case t[1]
when :on_ignored_nl, :on_nl, :on_comment
@@ -375,6 +376,7 @@ class RubyLex
spaces_at_line_head = 0
is_first_spaces_of_line = true
is_first_printable_of_line = true
+ open_brace_on_line = 0
next
when :on_sp
spaces_at_line_head = t[2].count(' ') if is_first_spaces_of_line
@@ -383,7 +385,8 @@ class RubyLex
end
case t[1]
when :on_lbracket, :on_lbrace, :on_lparen
- spaces_of_nest.push(spaces_at_line_head)
+ spaces_of_nest.push(spaces_at_line_head + open_brace_on_line * 2)
+ open_brace_on_line += 1
when :on_rbracket, :on_rbrace, :on_rparen
if is_first_printable_of_line
corresponding_token_depth = spaces_of_nest.pop