Skip to content

Commit

Permalink
Improving formatting of Conditional in Return
Browse files Browse the repository at this point in the history
closes #416
  • Loading branch information
belav committed Sep 11, 2021
1 parent 1f99049 commit d195f14
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,18 @@ public class ClassName
encoding ? GetEncoding(1252) : encoding,
cancellationToken
);

return
someLongCondition____________________________________
&& someOtherLongCondition____________________________________
? trueValue________________________________
: falseValue_______________________________;

return
someLongCondition____________________________________
== someThingElse______________________
&& someOtherLongCondition____________________________________
? trueValue________________________________
: falseValue_______________________________;
}
}
2 changes: 1 addition & 1 deletion Src/CSharpier/DocTypes/Doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static Group GroupWithId(string groupId, params Doc[] contents)

public static IndentDoc Indent(List<Doc> contents) => new() { Contents = Concat(contents) };

public static Doc IndentIf(bool condition, Concat contents)
public static Doc IndentIf(bool condition, Doc contents)
{
return condition ? Doc.Indent(contents) : contents;
}
Expand Down
20 changes: 10 additions & 10 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BinaryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ public static Doc Print(BinaryExpressionSyntax node)

var shouldNotIndent =
node.Parent
is ReturnStatementSyntax
or WhereClauseSyntax
is ArrowExpressionClauseSyntax
or AssignmentExpressionSyntax
or CatchFilterClauseSyntax
or CheckedExpressionSyntax
or DoStatementSyntax
or EqualsValueClauseSyntax
or ArrowExpressionClauseSyntax
or IfStatementSyntax
or ParenthesizedExpressionSyntax
or ParenthesizedLambdaExpressionSyntax
or AssignmentExpressionSyntax
or SimpleLambdaExpressionSyntax
or IfStatementSyntax
or WhileStatementSyntax
or ReturnStatementSyntax
or SwitchExpressionSyntax
or DoStatementSyntax
or CheckedExpressionSyntax
or CatchFilterClauseSyntax
or ParenthesizedExpressionSyntax
or SwitchStatementSyntax
or WhereClauseSyntax
or WhileStatementSyntax
|| node.Parent is ConditionalExpressionSyntax
&& node.Parent.Parent is not ArgumentSyntax;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ public static Doc Print(ConditionalExpressionSyntax node)
};

return Doc.Group(
Node.Print(node.Condition),
node.Parent is ConditionalExpressionSyntax or ArgumentSyntax
Doc.IndentIf(
node.Parent is ReturnStatementSyntax,
Doc.Concat(
node.Parent is ReturnStatementSyntax ? Doc.SoftLine : Doc.Null,
Node.Print(node.Condition)
)
),
node.Parent
is ConditionalExpressionSyntax
or ArgumentSyntax
or ReturnStatementSyntax
? Doc.Align(2, contents)
: Doc.Indent(contents)
);
Expand Down

0 comments on commit d195f14

Please sign in to comment.