Skip to content

Commit

Permalink
Always break and indent nested conditionals
Browse files Browse the repository at this point in the history
closes #434
  • Loading branch information
belav committed Oct 24, 2021
1 parent 8753397 commit d4698d8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
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);
}
}
}

0 comments on commit d4698d8

Please sign in to comment.