From 75a8e1f3d5f76ac846801671c08d755d602e227f Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Tue, 2 Jan 2024 13:51:44 -0600 Subject: [PATCH] Make long parenthesized expressions not always break. closes #921 --- .../cs/ParenthesizedExpressions.test | 50 +++++++++++++------ .../InvocationExpression.cs | 1 + .../ParenthesizedExpression.cs | 13 +++-- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ParenthesizedExpressions.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ParenthesizedExpressions.test index a1248d1b5..72b8e5b10 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ParenthesizedExpressions.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ParenthesizedExpressions.test @@ -1,16 +1,34 @@ -class ClassName -{ - void MethodName() - { - var x = - query - && ( - currentChar == '=' - || currentChar == '<' - || currentChar == '!' - || currentChar == '>' - || currentChar == '|' - || currentChar == '&' - ); - } -} +var x = + query + && ( + currentChar == '=' + || currentChar == '<' + || currentChar == '!' + || currentChar == '>' + || currentChar == '|' + || currentChar == '&' + ); + +// some comment won't break the next line +(someObject as SomeType).CallMethod(); + +(someObject as Exactly100____________________________________________________________).CallMethod(); +(someObject as SomeLongType___________________________________________________________) + .CallMethod(); +(someObject as SomeLongType________________________________________________________________________) + .CallMethod(); + +( + someObject + as UnlikelyButThisIsWhatItWouldLookLike_________________________________________________ +) + .CallMethod(); + +( + someObject + as UnlikelyButThisIsWhatItWouldLookLike_________________________________________________ +) + .CallMethod______________() + .CallMethod______________() + .CallMethod______________(); + diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs index 781b45b7d..0acbaa746 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs @@ -34,6 +34,7 @@ public static Doc PrintMemberChain(ExpressionSyntax node, FormattingContext cont var forceOneLine = groups.Count <= cutoff + && groups[0].First().Node is not ParenthesizedExpressionSyntax && ( groups .Skip(shouldMergeFirstTwoGroups ? 1 : 0) diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ParenthesizedExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ParenthesizedExpression.cs index 36bdc289e..d3a1b675c 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ParenthesizedExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ParenthesizedExpression.cs @@ -4,11 +4,14 @@ internal static class ParenthesizedExpression { public static Doc Print(ParenthesizedExpressionSyntax node, FormattingContext context) { - return Doc.Group( - Token.Print(node.OpenParenToken, context), - Doc.Indent(Doc.SoftLine, Node.Print(node.Expression, context)), - Doc.SoftLine, - Token.Print(node.CloseParenToken, context) + return Doc.Concat( + Token.PrintLeadingTrivia(node.OpenParenToken, context), + Doc.Group( + Token.PrintWithoutLeadingTrivia(node.OpenParenToken, context), + Doc.Indent(Doc.SoftLine, Node.Print(node.Expression, context)), + Doc.SoftLine, + Token.Print(node.CloseParenToken, context) + ) ); } }