From ab0dbfbe6df61c75cffea63a1ed990682366a313 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Sun, 9 Oct 2011 21:34:16 -0700 Subject: [PATCH] Handle unless and until in indent --- lib/pry/indent.rb | 8 +++++++- test/test_indent.rb | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/pry/indent.rb b/lib/pry/indent.rb index ed37d97e..4ee05e96 100644 --- a/lib/pry/indent.rb +++ b/lib/pry/indent.rb @@ -25,7 +25,9 @@ class Pry 'module' => 'end', 'do' => 'end', 'if' => 'end', + 'unless' => 'end', 'while' => 'end', + 'until' => 'end', 'for' => '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 # keywords such as "class" inside strings would cause the code to be # indented incorrectly. @@ -135,7 +141,7 @@ class Pry # If the list of tokens contains a matching closing token the line should # not be indented (and thus we should return true). 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)) last_token, last_kind = token, kind unless kind == :space diff --git a/test/test_indent.rb b/test/test_indent.rb index 00fa87e1..8734069f 100644 --- a/test/test_indent.rb +++ b/test/test_indent.rb @@ -204,4 +204,12 @@ TXT @indent.reset.indent("[if bar\n#").should == "[if bar\n #" @indent.reset.indent("true; while bar\n#").should == "true; while bar\n #" 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