Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions spec/compiler/parser/to_s_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,44 @@ describe "ASTNode#to_s" do
}
%}
CR

expect_to_s <<-'CR'
{%
vals = "foo".strip.strip.strip
%}
CR

expect_to_s <<-'CR'
{%
vals = "foo".strip.strip
.strip
%}
CR

expect_to_s <<-'CR'
{%
vals = "foo"
.strip
.strip.strip
%}
CR

expect_to_s <<-'CR'
{%
vals = [4, 1, 12]
.sort_by do |v| v end
.map do |v| v end
%}
CR

expect_to_s <<-'CR'
{%
vals = [4, 1, 12]
.sort_by do |v| v end
.join
.strip
.chars
.map do |v| v end
%}
CR
end
13 changes: 13 additions & 0 deletions src/compiler/crystal/syntax/to_s.cr
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ module Crystal
end

need_parens = need_parens(node_obj)
is_multiline = false

@str << "::" if node.global?
if node_obj.is_a?(ImplicitObj)
Expand Down Expand Up @@ -490,7 +491,17 @@ module Crystal
in_parenthesis(need_parens(arg), arg)
else
if node_obj
# A call is multiline if the call's name is on a diff line than the obj it's being called on.
is_multiline = (node_obj_end_loc = node_obj.end_location) && (name_loc = node.name_end_location) && (name_loc.line_number > node_obj_end_loc.line_number)

in_parenthesis(need_parens, node_obj)

if is_multiline
newline
@indent += 1
append_indent
end

@str << '.'
end
if Lexer.setter?(node.name)
Expand All @@ -516,6 +527,8 @@ module Crystal
block.accept self
end

@indent -= 1 if is_multiline

false
end

Expand Down