Skip to content

Commit

Permalink
Do auto indent only when closing token at first of line
Browse files Browse the repository at this point in the history
  if true
    3; end # this isn't auto-indented
  • Loading branch information
aycabta committed Jun 21, 2019
1 parent d1fa0f6 commit 5e20886
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -300,19 +301,24 @@ 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
indent += 1
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'
Expand All @@ -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
}
Expand Down

0 comments on commit 5e20886

Please sign in to comment.