Skip to content

Commit

Permalink
Avoid breaking only around binary expression but not binary expressio…
Browse files Browse the repository at this point in the history
…n itself (#934)

closes #924
  • Loading branch information
belav authored Aug 21, 2023
1 parent 22a0d60 commit 81b6789
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
23 changes: 16 additions & 7 deletions Src/CSharpier.Tests/FormattingTests/TestFiles/cs/IfStatements.test
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,31 @@ public class ClassName

if (true && (true || false)) { }

if (exactly100 || moreTextToMakeItWork_____________________________________________________)
if (someLongStatement == true || someOtherStatement_______________________________ == false)
{
return;
// just fits
}

// leading comment doesn't break differently
if (exactly100 || moreTextToMakeItWork_____________________________________________________)
// comment doesn't affect fit
if (someLongStatement == true || someOtherStatement_______________________________ == false)
{
return;
// just fits
}

if (
justOver100 || moreTextToMakeItWork_____________________________________________________
someLongStatement == true
|| someOtherStatement________________________________ == false
)
{
return;
// just too big
}

if (
someLongStatement == true
|| someOtherStatement_________________________________ == false
)
{
// also too big
}

if (true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ public static Doc Print(BinaryExpressionSyntax node, FormattingContext context)
{
var docs = PrintBinaryExpression(node, context);

if (node.Parent is IfStatementSyntax)
{
// avoid grouping here so that the ifBreaks in IfStatement can understand when
// this BinaryExpression breaks
return Doc.Concat(docs);
}

var shouldNotIndent =
node.Parent
is ArrowExpressionClauseSyntax
Expand All @@ -15,7 +22,6 @@ or CatchFilterClauseSyntax
or CheckedExpressionSyntax
or DoStatementSyntax
or EqualsValueClauseSyntax
or IfStatementSyntax
or ParenthesizedExpressionSyntax
or ParenthesizedLambdaExpressionSyntax
or SimpleLambdaExpressionSyntax
Expand Down
13 changes: 7 additions & 6 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/IfStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ public static Doc Print(IfStatementSyntax node, FormattingContext context)
}

docs.Add(
Token.Print(node.IfKeyword, context),
" ",
Doc.Group(
Token.Print(node.IfKeyword, context),
" ",
Token.Print(node.OpenParenToken, context),
Doc.Group(
Doc.Indent(Doc.SoftLine, Node.Print(node.Condition, context)),
Doc.SoftLine
Doc.Indent(
Doc.IfBreak(Doc.SoftLine, Doc.Null),
Node.Print(node.Condition, context)
),
Token.Print(node.CloseParenToken, context)
Doc.IfBreak(Doc.SoftLine, Doc.Null)
),
Token.Print(node.CloseParenToken, context),
OptionalBraces.Print(node.Statement, context)
);

Expand Down

0 comments on commit 81b6789

Please sign in to comment.