From 223b603d8fe23701fdd60046fafb38676e183c07 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Thu, 30 Dec 2021 11:34:22 -0600 Subject: [PATCH 1/3] Fixing indentation issues with conditionals inside initializers closes #529 --- .../TestFiles/InitializerExpressions.cst | 21 +++++++++++++++++++ .../TestFiles/ObnoxiousEdgeCases.cst | 4 ++-- .../TestFiles/VariableDeclarations.cst | 18 +++++++++++++--- .../ConditionalExpression.cs | 1 + .../InvocationExpression.cs | 5 ++--- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/InitializerExpressions.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/InitializerExpressions.cst index 6c1403752..d2b5c1ae0 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/InitializerExpressions.cst +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/InitializerExpressions.cst @@ -141,6 +141,27 @@ class ClassName new Thing { One = 1 }, Two = 2 }; + + var conditionalsAndInvocations = new List + { + this.CallSomeMethod________________________________________() + .CallSomeMethod________________________________________() + ? one + : two, + SomeOtherMethod(), + SomethingElse + .CallSomeMethod________________________________________() + .CallSomeMethod________________________________________() + ? one + : two, + SomeOtherMethod(), + this.CallSomeMethod________________________________________() + .CallSomeMethod________________________________________(), + someLongCondition___________________________________ + && someLongCondition___________________________________ + ? one + : two + }; } private SomeObject someObject = diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/ObnoxiousEdgeCases.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/ObnoxiousEdgeCases.cst index 8ca8dd1b1..9df964d9f 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/ObnoxiousEdgeCases.cst +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/ObnoxiousEdgeCases.cst @@ -130,8 +130,8 @@ class ClassName expression.Type.IsValueType ? ( expression.Type.IsNullableType() - ? Condition(Property(expression, "HasValue"), ToType(@else, then.Type), then) - : @else + ? Condition(Property(expression, "HasValue"), ToType(@else, then.Type), then) + : @else ) : Condition(ReferenceEqual(expression, Null), then, ToType(@else, then.Type)); diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/VariableDeclarations.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/VariableDeclarations.cst index fc47de40a..0fbd773d0 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/VariableDeclarations.cst +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/VariableDeclarations.cst @@ -138,8 +138,8 @@ withLineEnding" someParameter_____________________, someParameter_____________________ ) - ? someValue - : someOtherValue; + ? someValue + : someOtherValue; var someValue = await SomeObject.CallLongAsyncMethod____________________________________________( @@ -155,7 +155,19 @@ withLineEnding" var someValue = SomeObject.CallLongMethod____________________________________________________() + ? someValue + : someOtherValue; + + var someValue = SomeObject + .CallLongMethod____________________________________________________() + .CallLongMethod____________________________________________________() + ? someValue + : someOtherValue; + + var someValue = + someLongCondition___________________________________ + && someOtheLongCondition___________________________________ ? someValue - : someOtherValue; + : someValue; } } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ConditionalExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ConditionalExpression.cs index 492c73899..cddc6088a 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ConditionalExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ConditionalExpression.cs @@ -24,6 +24,7 @@ node.Parent is ReturnStatementSyntax ) : Node.Print(node.Condition), node.Parent is ConditionalExpressionSyntax or ArgumentSyntax or ReturnStatementSyntax + || node.Condition is InvocationExpressionSyntax ? Doc.Align(2, innerContents) : Doc.Indent(innerContents) }; diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs index 0d9d89c56..7b3c7bfa3 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs @@ -221,13 +221,12 @@ private static Doc PrintIndentedGroup(ExpressionSyntax node, IList Doc.Group(o.Select(o => o.Doc).ToArray())) + groups.Select(o => Doc.Group(o.Select(p => p.Doc).ToArray())) ) ) ); From 636b529a4deff461aea43b9a8eb5d5fc5f2f71cb Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Thu, 30 Dec 2021 12:14:14 -0600 Subject: [PATCH 2/3] Fixing an edge case --- .../FormattingTests/TestFiles/ConditionalExpressions.cst | 8 ++++++++ .../SyntaxNodePrinters/InvocationExpression.cs | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/ConditionalExpressions.cst b/Src/CSharpier.Tests/FormattingTests/TestFiles/ConditionalExpressions.cst index 888a86303..01b91925c 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/ConditionalExpressions.cst +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/ConditionalExpressions.cst @@ -123,5 +123,13 @@ public class ClassName : c ? d : e; + + var invocationIndent = someCondition + ? SomeObject + .CallLongMethod__________________________________________() + .CallLongMethod__________________________________________() + : SomeObject + .CallLongMethod__________________________________________() + .CallLongMethod__________________________________________(); } } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs index 7b3c7bfa3..25b1a0fa4 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs @@ -221,7 +221,13 @@ private static Doc PrintIndentedGroup(ExpressionSyntax node, IList Date: Thu, 30 Dec 2021 12:26:28 -0600 Subject: [PATCH 3/3] Update script --- Scripts/CreateTestingPR.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Scripts/CreateTestingPR.ps1 b/Scripts/CreateTestingPR.ps1 index 2dca2a721..e76c78dc6 100644 --- a/Scripts/CreateTestingPR.ps1 +++ b/Scripts/CreateTestingPR.ps1 @@ -82,4 +82,4 @@ if ($firstRun) { Write-Output $newPr } -Pop-Location \ No newline at end of file +Set-Location $repositoryRoot \ No newline at end of file