diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index b158028f7ade00..fa72c652523e31 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -292,6 +292,7 @@ def check_code_block(code) def process_nesting_level(check_closing: false) corresponding_token_depth = nil is_first_spaces_of_line = true + is_first_printable_of_line = true spaces_of_nest = [] spaces_at_line_head = 0 indent = @tokens.inject(0) { |indent, t| @@ -300,11 +301,12 @@ def process_nesting_level(check_closing: false) when :on_ignored_nl, :on_nl spaces_at_line_head = nil is_first_spaces_of_line = true + is_first_printable_of_line = true + next indent when :on_sp spaces_at_line_head = t[2].count(' ') if is_first_spaces_of_line is_first_spaces_of_line = false - else - is_first_spaces_of_line = false + next indent end case t[1] when :on_lbracket, :on_lbrace, :on_lparen @@ -312,7 +314,11 @@ def process_nesting_level(check_closing: false) spaces_of_nest.push(spaces_at_line_head) when :on_rbracket, :on_rbrace, :on_rparen indent -= 1 - corresponding_token_depth = spaces_of_nest.pop + if is_first_printable_of_line + corresponding_token_depth = spaces_of_nest.pop + else + corresponding_token_depth = nil + end when :on_kw case t[2] when 'def', 'do', 'case', 'for', 'begin', 'class', 'module' @@ -324,9 +330,15 @@ def process_nesting_level(check_closing: false) spaces_of_nest.push(spaces_at_line_head) when 'end' indent -= 1 - corresponding_token_depth = spaces_of_nest.pop + if is_first_printable_of_line + corresponding_token_depth = spaces_of_nest.pop + else + corresponding_token_depth = nil + end end end + is_first_spaces_of_line = false + is_first_printable_of_line = false # percent literals are not indented indent }