Skip to content

Commit 51fe874

Browse files
Simplify control flow in formatter for Call nodes (#16170)
1 parent b820665 commit 51fe874

File tree

1 file changed

+53
-50
lines changed

1 file changed

+53
-50
lines changed

src/compiler/crystal/tools/formatter.cr

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,12 +2554,7 @@ module Crystal
25542554
accept obj
25552555

25562556
passed_backslash_newline = @token.passed_backslash_newline
2557-
2558-
if @token.type.space?
2559-
needs_space = true
2560-
else
2561-
needs_space = node.name != "*" && node.name != "/" && node.name != "**" && node.name != "//"
2562-
end
2557+
needs_space = @token.type.space? || !node.name.in?("*", "/", "**", "//")
25632558

25642559
slash_is_not_regex!
25652560
skip_space
@@ -2642,18 +2637,18 @@ module Crystal
26422637
end
26432638

26442639
return false
2645-
else
2646-
write " " if needs_space && !passed_backslash_newline
2647-
write node.name
2640+
end
26482641

2649-
# This is the case of a-1 and a+1
2650-
if @token.type.number?
2651-
@lexer.current_pos = @token.start + 1
2652-
end
2642+
write " " if needs_space && !passed_backslash_newline
2643+
write node.name
26532644

2654-
slash_is_regex!
2645+
# This is the case of a-1 and a+1
2646+
if @token.type.number?
2647+
@lexer.current_pos = @token.start + 1
26552648
end
26562649

2650+
slash_is_regex!
2651+
26572652
next_token
26582653
passed_backslash_newline = @token.passed_backslash_newline
26592654
found_comment = skip_space
@@ -2823,50 +2818,58 @@ module Crystal
28232818
needs_space = !has_parentheses || has_args
28242819
block_indent = base_indent
28252820
skip_space
2826-
if has_parentheses && @token.type.op_comma?
2827-
next_token
2828-
wrote_newline = skip_space(block_indent, write_comma: true)
2829-
if wrote_newline || @token.type.newline?
2830-
unless wrote_newline
2831-
next_token_skip_space_or_newline
2832-
write ","
2833-
write_line
2821+
if has_parentheses
2822+
if @token.type.op_comma?
2823+
next_token
2824+
wrote_newline = skip_space(block_indent, write_comma: true)
2825+
if wrote_newline || @token.type.newline?
2826+
unless wrote_newline
2827+
next_token_skip_space_or_newline
2828+
write ","
2829+
write_line
2830+
end
2831+
needs_space = false
2832+
block_indent += 2 if !@token.type.op_rparen? # foo(1, ↵ &.foo) case
2833+
write_indent(block_indent)
2834+
else
2835+
write "," if !@token.type.op_rparen? # foo(1, &.foo) case
28342836
end
2835-
needs_space = false
2836-
block_indent += 2 if !@token.type.op_rparen? # foo(1, ↵ &.foo) case
2837-
write_indent(block_indent)
2838-
else
2839-
write "," if !@token.type.op_rparen? # foo(1, &.foo) case
28402837
end
2841-
end
2842-
if has_parentheses && @token.type.op_rparen?
2843-
if ends_with_newline
2844-
write_line unless found_comment || @wrote_newline
2845-
write_indent
2838+
if @token.type.op_rparen?
2839+
if ends_with_newline
2840+
write_line unless found_comment || @wrote_newline
2841+
write_indent
2842+
end
2843+
write ")"
2844+
next_token_skip_space_or_newline
2845+
2846+
indent(block_indent) { format_block block, needs_space }
2847+
return false
2848+
else
2849+
indent(block_indent) { format_block block, needs_space }
2850+
2851+
skip_space
2852+
if @token.type.newline?
2853+
ends_with_newline = true
2854+
end
2855+
skip_space_or_newline
28462856
end
2847-
write ")"
2848-
next_token_skip_space_or_newline
2857+
2858+
finish_args(has_parentheses, has_newlines, ends_with_newline, found_comment, base_indent)
2859+
2860+
return false
2861+
else
28492862
indent(block_indent) { format_block block, needs_space }
2863+
2864+
finish_args(has_parentheses, has_newlines, ends_with_newline, found_comment, base_indent)
2865+
28502866
return false
28512867
end
2852-
indent(block_indent) { format_block block, needs_space }
2853-
if has_parentheses
2854-
skip_space
2855-
if @token.type.newline?
2856-
ends_with_newline = true
2857-
end
2858-
skip_space_or_newline
2859-
end
2860-
end
2861-
2862-
if has_args || node.block_arg
2868+
else
28632869
finish_args(has_parentheses, has_newlines, ends_with_newline, found_comment, base_indent)
2864-
elsif has_parentheses
2865-
skip_space_or_newline
2866-
write_token :OP_RPAREN
2867-
end
28682870

2869-
false
2871+
false
2872+
end
28702873
end
28712874

28722875
def format_call_args(node : ASTNode, has_parentheses, base_indent)

0 commit comments

Comments
 (0)