Skip to content

Commit

Permalink
Format: work formatter against paren with newline (#5268)
Browse files Browse the repository at this point in the history
Fixed #5262
  • Loading branch information
makenowjust authored and RX14 committed Nov 9, 2017
1 parent 060f404 commit b9ea898
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ describe Crystal::Formatter do

assert_format "(1)"
assert_format " ( 1; 2; 3 ) ", "(1; 2; 3)"
assert_format "(\n a = 1\n a\n)"
assert_format "begin; 1; end", "begin\n 1\nend"
assert_format "begin\n1\n2\n3\nend", "begin\n 1\n 2\n 3\nend"
assert_format "begin\n1 ? 2 : 3\nend", "begin\n 1 ? 2 : 3\nend"
Expand Down
19 changes: 16 additions & 3 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,23 @@ module Crystal
base_indent = old_indent
next_needs_indent = false

has_newline = false
has_paren = false
has_begin = false

if node.keyword == :"(" && @token.type == :"("
write "("
next_needs_indent = false
next_token
has_paren = true
wrote_newline = next_token_skip_space
if @token.type == :NEWLINE || wrote_newline
@indent += 2
write_line unless wrote_newline
next_token_skip_space_or_newline
base_indent = @indent
next_needs_indent = true
has_newline = true
end
elsif node.keyword == :begin && @token.keyword?(:begin)
write "begin"
@indent += 2
Expand All @@ -197,6 +206,7 @@ module Crystal
has_begin = true
base_indent = @indent
next_needs_indent = true
has_newline = true
end

last_aligned_assign = nil
Expand Down Expand Up @@ -271,15 +281,18 @@ module Crystal

@indent = old_indent

if has_newline
write_line
write_indent
end

if has_paren
write_token :")"
end

if has_begin
check_end
next_token
write_line
write_indent
write "end"
end

Expand Down

0 comments on commit b9ea898

Please sign in to comment.