From 6260db7391486eb4a282b525f0827607f0862488 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Sun, 21 Jan 2024 14:35:27 -0600 Subject: [PATCH] Fixing regression with invocations --- .../cs/MemberChain_PropertiesConsistent.test | 7 +- .../TestFiles/cs/MemberChains.test | 7 +- .../InvocationExpression.cs | 82 +++++++------------ 3 files changed, 35 insertions(+), 61 deletions(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MemberChain_PropertiesConsistent.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MemberChain_PropertiesConsistent.test index 22452adda..20fca8f39 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MemberChain_PropertiesConsistent.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MemberChain_PropertiesConsistent.test @@ -28,9 +28,10 @@ var someVariable = this.Property.CallMethod(someValue => var someVariable = this.Property() .CallMethod(someValue => someValue.SomeProperty == someOtherValue___________________________); -var someVariable = this.Property.CallMethod(someValue => - someValue.SomeProperty == someOtherValue___________________________ -) +var someVariable = this + .Property.CallMethod(someValue => + someValue.SomeProperty == someOtherValue___________________________ + ) .CallMethod(); var someVariable = this.Property() diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MemberChains.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MemberChains.test index de22b143a..e3e13cfe1 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MemberChains.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/MemberChains.test @@ -235,8 +235,7 @@ class ClassName .CallMethod__________________(); someThing_______________________ - ?.Property - .CallMethod__________________() + ?.Property.CallMethod__________________() .CallMethod__________________(); someThing_______________________ @@ -246,8 +245,8 @@ class ClassName IEnumerable valueProviderFactories = new ModelBinderAttribute_______().GetValueProviderFactories(config); - var something________________________________________ = x.SomeProperty - .CallMethod(longParameter_____________, longParameter_____________) + var something________________________________________ = x + .SomeProperty.CallMethod(longParameter_____________, longParameter_____________) .CallMethod(); } } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs index 9b2ce0850..a7bd80e53 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs @@ -198,7 +198,7 @@ private static List> GroupPrintedNodesOnLines(List(); + currentGroup = []; groups.Add(currentGroup); } else if ( @@ -209,7 +209,7 @@ or IdentifierNameSyntax && printedNodes[index + -1].Node is not ConditionalAccessExpressionSyntax ) { - currentGroup = new List(); + currentGroup = []; groups.Add(currentGroup); } @@ -258,49 +258,20 @@ List printedNodes } } - if (printedNodes[0].Node is not InvocationExpressionSyntax) + if ( + printedNodes[0].Node is not (InvocationExpressionSyntax or PostfixUnaryExpressionSyntax) + && index < printedNodes.Count + && printedNodes[index].Node + is ElementAccessExpressionSyntax + or PostfixUnaryExpressionSyntax + ) { - for (; index + 1 < printedNodes.Count; ++index) - { - /* this handles the special case where we want ?.Property on the same line - someThing_______________________?.Property - .CallMethod__________________() - .CallMethod__________________(); - */ - if ( - printedNodes[index].Node is ConditionalAccessExpressionSyntax - && printedNodes[index + 1].Node - is MemberBindingExpressionSyntax { Parent: MemberAccessExpressionSyntax } - ) - { - currentGroup.Add(printedNodes[index]); - currentGroup.Add(printedNodes[index + 1]); - index++; - continue; - } - - if ( - ( - IsMemberish(printedNodes[index].Node) - && ( - IsMemberish(printedNodes[index + 1].Node) - || printedNodes[index + 1].Node is PostfixUnaryExpressionSyntax - ) - ) - || printedNodes[index].Node is PostfixUnaryExpressionSyntax - ) - { - currentGroup.Add(printedNodes[index]); - } - else - { - break; - } - } + currentGroup.Add(printedNodes[index]); + index++; } groups.Add(currentGroup); - currentGroup = new List(); + currentGroup = []; var hasSeenNodeThatRequiresBreak = false; for (; index < printedNodes.Count; index++) @@ -313,17 +284,13 @@ or ConditionalAccessExpressionSyntax ) { groups.Add(currentGroup); - currentGroup = new List(); + currentGroup = []; hasSeenNodeThatRequiresBreak = false; } if ( printedNodes[index].Node - is ( - InvocationExpressionSyntax - or ElementAccessExpressionSyntax - or MemberBindingExpressionSyntax - ) + is (InvocationExpressionSyntax or ElementAccessExpressionSyntax) ) { hasSeenNodeThatRequiresBreak = true; @@ -339,11 +306,6 @@ or MemberBindingExpressionSyntax return groups; } - private static bool IsMemberish(CSharpSyntaxNode node) - { - return node is MemberAccessExpressionSyntax or ConditionalAccessExpressionSyntax; - } - private static Doc PrintIndentedGroup(ExpressionSyntax node, IList> groups) { if (groups.Count == 0) @@ -383,11 +345,23 @@ private static bool ShouldMergeFirstTwoGroups(List> groups) var firstNode = groups[0][0].Node; - if (firstNode is IdentifierNameSyntax { Identifier.Text.Length: <= 4 }) + if ( + firstNode + is IdentifierNameSyntax { Identifier.Text.Length: <= 4 } + or ThisExpressionSyntax + or PredefinedTypeSyntax + or BaseExpressionSyntax + && ( + groups[1].Count == 1 + || groups[1].Skip(1).First().Node + is InvocationExpressionSyntax + or PostfixUnaryExpressionSyntax + ) + ) { return true; } - return firstNode is ThisExpressionSyntax or PredefinedTypeSyntax or BaseExpressionSyntax; + return false; } }