mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Support irregular auto indent
v = if true 3 end # this "end" is auto-indented correctly
This commit is contained in:
parent
38ccb8f747
commit
50841eca43
1 changed files with 24 additions and 8 deletions
|
@ -92,9 +92,9 @@ class RubyLex
|
||||||
last_line = lines[line_index]&.byteslice(0, byte_pointer)
|
last_line = lines[line_index]&.byteslice(0, byte_pointer)
|
||||||
code += last_line if last_line
|
code += last_line if last_line
|
||||||
@tokens = Ripper.lex(code)
|
@tokens = Ripper.lex(code)
|
||||||
indent, close_token = process_nesting_level(check_closing: true)
|
indent, corresponding_token_depth = process_nesting_level(check_closing: true)
|
||||||
if close_token
|
if corresponding_token_depth
|
||||||
indent * 2
|
corresponding_token_depth
|
||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
@ -290,32 +290,48 @@ class RubyLex
|
||||||
end
|
end
|
||||||
|
|
||||||
def process_nesting_level(check_closing: false)
|
def process_nesting_level(check_closing: false)
|
||||||
close_token = false
|
corresponding_token_depth = nil
|
||||||
|
is_first_spaces_of_line = true
|
||||||
|
spaces_of_nest = []
|
||||||
|
spaces_at_line_head = 0
|
||||||
indent = @tokens.inject(0) { |indent, t|
|
indent = @tokens.inject(0) { |indent, t|
|
||||||
close_token = false
|
corresponding_token_depth = nil
|
||||||
|
case t[1]
|
||||||
|
when :on_ignored_nl, :on_nl
|
||||||
|
spaces_at_line_head = nil
|
||||||
|
is_first_spaces_of_line = true
|
||||||
|
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
|
||||||
|
end
|
||||||
case t[1]
|
case t[1]
|
||||||
when :on_lbracket, :on_lbrace, :on_lparen
|
when :on_lbracket, :on_lbrace, :on_lparen
|
||||||
indent += 1
|
indent += 1
|
||||||
|
spaces_of_nest.push(spaces_at_line_head)
|
||||||
when :on_rbracket, :on_rbrace, :on_rparen
|
when :on_rbracket, :on_rbrace, :on_rparen
|
||||||
indent -= 1
|
indent -= 1
|
||||||
close_token = true
|
corresponding_token_depth = spaces_of_nest.pop
|
||||||
when :on_kw
|
when :on_kw
|
||||||
case t[2]
|
case t[2]
|
||||||
when 'def', 'do', 'case', 'for', 'begin', 'class', 'module'
|
when 'def', 'do', 'case', 'for', 'begin', 'class', 'module'
|
||||||
indent += 1
|
indent += 1
|
||||||
|
spaces_of_nest.push(spaces_at_line_head)
|
||||||
when 'if', 'unless', 'while', 'until'
|
when 'if', 'unless', 'while', 'until'
|
||||||
# postfix if/unless/while/until/rescue must be Ripper::EXPR_LABEL
|
# postfix if/unless/while/until/rescue must be Ripper::EXPR_LABEL
|
||||||
indent += 1 unless t[3].allbits?(Ripper::EXPR_LABEL)
|
indent += 1 unless t[3].allbits?(Ripper::EXPR_LABEL)
|
||||||
|
spaces_of_nest.push(spaces_at_line_head)
|
||||||
when 'end'
|
when 'end'
|
||||||
indent -= 1
|
indent -= 1
|
||||||
close_token = true
|
corresponding_token_depth = spaces_of_nest.pop
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# percent literals are not indented
|
# percent literals are not indented
|
||||||
indent
|
indent
|
||||||
}
|
}
|
||||||
if check_closing
|
if check_closing
|
||||||
[indent, close_token]
|
[indent, corresponding_token_depth]
|
||||||
else
|
else
|
||||||
indent
|
indent
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue