Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always break and indent nested conditionals #460

Merged
merged 2 commits into from
Oct 24, 2021
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
2 changes: 2 additions & 0 deletions Src/CSharpier.Tests/DocSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void Should_Format_Basic_Types()
Doc.SoftLine,
Doc.Null,
Doc.Trim,
Doc.BreakParent,
"1"
);

Expand All @@ -48,6 +49,7 @@ public void Should_Format_Basic_Types()
Doc.SoftLine,
Doc.Null,
Doc.Trim,
Doc.BreakParent,
""1""
)"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,29 @@ public class ClassName
)
? trueValue________________________________
: falseValue_______________________________;

return firstCondition
? firstValue
: secondCondition
? secondValue
: thirdCondition
? thirdValue
: fourthValue;

return firstCondition
? secondCondition
? firstValue
: secondValue
: thirdCondition
? thirdValue
: fourthValue;

return a ? b : c;

return a
? b
: c
? d
: e;
}
}
5 changes: 5 additions & 0 deletions Src/CSharpier/DocSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ void AppendNextIndent()
AppendIndent();
result.Append("Doc.LiteralLine");
}
else if (doc is BreakParent)
{
AppendIndent();
result.Append("Doc.BreakParent");
}
else if (doc is LineDoc lineDoc)
{
AppendIndent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public static class ConditionalExpression
{
public static Doc Print(ConditionalExpressionSyntax node)
{
Doc[] contents =
Doc[] innerContents =
{
Doc.Line,
Token.PrintWithSuffix(node.QuestionToken, " "),
Expand All @@ -18,20 +18,26 @@ public static Doc Print(ConditionalExpressionSyntax node)
Doc.Align(2, Node.Print(node.WhenFalse))
};

return Doc.Group(
Doc[] outerContents =
{
node.Parent is ConditionalExpressionSyntax ? Doc.BreakParent : Doc.Null,
node.Parent is ReturnStatementSyntax
&& node.Condition is BinaryExpressionSyntax or IsPatternExpressionSyntax
? Doc.Indent(
Doc.Group(Doc.IfBreak(Doc.SoftLine, Doc.Null), Node.Print(node.Condition))
)
: Node.Print(node.Condition),
&& node.Condition is BinaryExpressionSyntax or IsPatternExpressionSyntax
? Doc.Indent(
Doc.Group(Doc.IfBreak(Doc.SoftLine, Doc.Null), Node.Print(node.Condition))
)
: Node.Print(node.Condition),
node.Parent
is ConditionalExpressionSyntax
or ArgumentSyntax
or ReturnStatementSyntax
? Doc.Align(2, contents)
: Doc.Indent(contents)
);
? Doc.Align(2, innerContents)
: Doc.Indent(innerContents)
};

return node.Parent is ConditionalExpressionSyntax
? Doc.Concat(outerContents)
: Doc.Group(outerContents);
}
}
}