1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Handle unless and until in indent

This commit is contained in:
Conrad Irwin 2011-10-09 21:34:16 -07:00
parent 0648d9c60d
commit ab0dbfbe6d
2 changed files with 15 additions and 1 deletions

View file

@ -25,7 +25,9 @@ class Pry
'module' => 'end', 'module' => 'end',
'do' => 'end', 'do' => 'end',
'if' => 'end', 'if' => 'end',
'unless' => 'end',
'while' => 'end', 'while' => 'end',
'until' => 'end',
'for' => 'end', 'for' => 'end',
'case' => 'end', 'case' => 'end',
'[' => ']', '[' => ']',
@ -33,6 +35,10 @@ class Pry
'(' => ')' '(' => ')'
} }
# Which tokens can either be open tokens, or appear as modifiers on
# a single-line.
SINGLELINE_TOKENS = %w(if while until unless)
# Collection of token types that should be ignored. Without this list # Collection of token types that should be ignored. Without this list
# keywords such as "class" inside strings would cause the code to be # keywords such as "class" inside strings would cause the code to be
# indented incorrectly. # indented incorrectly.
@ -135,7 +141,7 @@ class Pry
# If the list of tokens contains a matching closing token the line should # If the list of tokens contains a matching closing token the line should
# not be indented (and thus we should return true). # not be indented (and thus we should return true).
tokens.each do |token, kind| tokens.each do |token, kind|
is_singleline_if = (token == "if" || token == "while") && end_of_statement?(last_token, last_kind) is_singleline_if = (SINGLELINE_TOKENS.include?(token)) && end_of_statement?(last_token, last_kind)
is_optional_do = (token == "do" && seen_for_at.include?(add_after - 1)) is_optional_do = (token == "do" && seen_for_at.include?(add_after - 1))
last_token, last_kind = token, kind unless kind == :space last_token, last_kind = token, kind unless kind == :space

View file

@ -204,4 +204,12 @@ TXT
@indent.reset.indent("[if bar\n#").should == "[if bar\n #" @indent.reset.indent("[if bar\n#").should == "[if bar\n #"
@indent.reset.indent("true; while bar\n#").should == "true; while bar\n #" @indent.reset.indent("true; while bar\n#").should == "true; while bar\n #"
end end
it "should differentiate single/multi-line unless" do
@indent.indent("foo unless bar\nunless foo\nbar\nend").should == "foo unless bar\nunless foo\n bar\nend"
end
it "should not indent single/multi-line until" do
@indent.indent("%w{baz} until bar\nuntil foo\nbar\nend").should == "%w{baz} until bar\nuntil foo\n bar\nend"
end
end end