From e0e97e94602e6e8742322aabf0254b34184c8253 Mon Sep 17 00:00:00 2001 From: belav Date: Thu, 27 May 2021 21:49:54 -0500 Subject: [PATCH] Better(?) way of dealing with breaking in VariableDeclarations closes #47 --- .../AnonymousObjectCreationExpressions.cst | 13 ++++ ...BasicAnonymousObjectCreationExpression.cst | 4 -- .../MultipleProperties.cst | 9 --- .../NoNames.cst | 4 -- ..._AnonymousObjectCreationExpressionTests.cs | 17 +---- .../TestFiles/Comments/SpecialCases.cst | 6 -- .../_ConditionalExpressionTests.cs | 2 +- .../FieldDeclarationComments.cst | 7 -- .../{BasicField.cst => FieldDeclarations.cst} | 23 ++++++ .../FieldDeclaration/FixedFieldWithSize.cst | 4 -- .../FieldDeclaration/NamespacedField.cst | 4 -- .../_FieldDeclarationTests.cs | 19 +---- ...Access.cst => ImplicitElementAccesses.cst} | 0 .../_ImplicitElementAccessTests.cs | 4 +- .../InitializerExpressions.cst | 9 +++ .../PropertyDeclarations.cst | 4 +- .../QueryExpression/QueryExpressions.cst | 7 ++ .../BasicSwitchExpression.cst | 13 ---- ...rsivePattern.cst => SwitchExpressions.cst} | 10 ++- .../_SwitchExpressionTests.cs | 9 +-- Src/CSharpier/CommandLineFormatter.cs | 1 + .../AssignmentExpression.cs | 5 +- .../BaseFieldDeclaration.cs | 2 +- .../SyntaxNodePrinters/EqualsValueClause.cs | 17 ++--- .../SyntaxNodePrinters/QueryExpression.cs | 3 +- .../SyntaxNodePrinters/TypeArgumentList.cs | 6 +- .../SyntaxNodePrinters/VariableDeclaration.cs | 72 ++++++++++++++++--- 27 files changed, 155 insertions(+), 119 deletions(-) create mode 100644 Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/AnonymousObjectCreationExpressions.cst delete mode 100644 Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/BasicAnonymousObjectCreationExpression.cst delete mode 100644 Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/MultipleProperties.cst delete mode 100644 Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/NoNames.cst delete mode 100644 Src/CSharpier.Tests/TestFiles/FieldDeclaration/FieldDeclarationComments.cst rename Src/CSharpier.Tests/TestFiles/FieldDeclaration/{BasicField.cst => FieldDeclarations.cst} (59%) delete mode 100644 Src/CSharpier.Tests/TestFiles/FieldDeclaration/FixedFieldWithSize.cst delete mode 100644 Src/CSharpier.Tests/TestFiles/FieldDeclaration/NamespacedField.cst rename Src/CSharpier.Tests/TestFiles/ImplicitElementAccess/{BasicImplicitElementAccess.cst => ImplicitElementAccesses.cst} (100%) delete mode 100644 Src/CSharpier.Tests/TestFiles/SwitchExpression/BasicSwitchExpression.cst rename Src/CSharpier.Tests/TestFiles/SwitchExpression/{SwitchWithRecursivePattern.cst => SwitchExpressions.cst} (68%) diff --git a/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/AnonymousObjectCreationExpressions.cst b/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/AnonymousObjectCreationExpressions.cst new file mode 100644 index 000000000..6882dcab4 --- /dev/null +++ b/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/AnonymousObjectCreationExpressions.cst @@ -0,0 +1,13 @@ +public class ClassName +{ + public dynamic ShortValue = new { Property = true }; + + public dynamic LongValue = new + { + One = "One", + Two = "Two", + ThreeThreeThree = "ThreeThreeThree" + }; + + public dynamic Value = new { NoName }; +} diff --git a/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/BasicAnonymousObjectCreationExpression.cst b/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/BasicAnonymousObjectCreationExpression.cst deleted file mode 100644 index b92da3b6d..000000000 --- a/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/BasicAnonymousObjectCreationExpression.cst +++ /dev/null @@ -1,4 +0,0 @@ -public class ClassName -{ - public dynamic Value = new { Property = true }; -} diff --git a/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/MultipleProperties.cst b/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/MultipleProperties.cst deleted file mode 100644 index 52282b715..000000000 --- a/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/MultipleProperties.cst +++ /dev/null @@ -1,9 +0,0 @@ -public class ClassName -{ - public dynamic Value = new - { - One = "One", - Two = "Two", - ThreeThreeThree = "ThreeThreeThree" - }; -} diff --git a/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/NoNames.cst b/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/NoNames.cst deleted file mode 100644 index 51d41f3ba..000000000 --- a/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/NoNames.cst +++ /dev/null @@ -1,4 +0,0 @@ -public class ClassName -{ - public dynamic Value = new { Property }; -} diff --git a/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/_AnonymousObjectCreationExpressionTests.cs b/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/_AnonymousObjectCreationExpressionTests.cs index 039990101..dd139045f 100644 --- a/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/_AnonymousObjectCreationExpressionTests.cs +++ b/Src/CSharpier.Tests/TestFiles/AnonymousObjectCreationExpression/_AnonymousObjectCreationExpressionTests.cs @@ -6,22 +6,9 @@ namespace CSharpier.Tests.TestFiles public class AnonymousObjectCreationExpressionTests : BaseTest { [Test] - public void BasicAnonymousObjectCreationExpression() + public void AnonymousObjectCreationExpressions() { - this.RunTest( - "AnonymousObjectCreationExpression", - "BasicAnonymousObjectCreationExpression" - ); - } - [Test] - public void MultipleProperties() - { - this.RunTest("AnonymousObjectCreationExpression", "MultipleProperties"); - } - [Test] - public void NoNames() - { - this.RunTest("AnonymousObjectCreationExpression", "NoNames"); + this.RunTest("AnonymousObjectCreationExpression", "AnonymousObjectCreationExpressions"); } } } diff --git a/Src/CSharpier.Tests/TestFiles/Comments/SpecialCases.cst b/Src/CSharpier.Tests/TestFiles/Comments/SpecialCases.cst index aceba378b..16dc03216 100644 --- a/Src/CSharpier.Tests/TestFiles/Comments/SpecialCases.cst +++ b/Src/CSharpier.Tests/TestFiles/Comments/SpecialCases.cst @@ -10,12 +10,6 @@ class ClassName { // indent } - - var x = new SomeClass - { - Property1 = true - // indent - }; } void MethodName() diff --git a/Src/CSharpier.Tests/TestFiles/ConditionalExpression/_ConditionalExpressionTests.cs b/Src/CSharpier.Tests/TestFiles/ConditionalExpression/_ConditionalExpressionTests.cs index 640f10380..a36faf06c 100644 --- a/Src/CSharpier.Tests/TestFiles/ConditionalExpression/_ConditionalExpressionTests.cs +++ b/Src/CSharpier.Tests/TestFiles/ConditionalExpression/_ConditionalExpressionTests.cs @@ -6,7 +6,7 @@ namespace CSharpier.Tests.TestFiles public class ConditionalExpressionTests : BaseTest { [Test] - public void BasicConditionalExpression() + public void ConditionalExpressions() { this.RunTest("ConditionalExpression", "ConditionalExpressions"); } diff --git a/Src/CSharpier.Tests/TestFiles/FieldDeclaration/FieldDeclarationComments.cst b/Src/CSharpier.Tests/TestFiles/FieldDeclaration/FieldDeclarationComments.cst deleted file mode 100644 index 42c0b55c2..000000000 --- a/Src/CSharpier.Tests/TestFiles/FieldDeclaration/FieldDeclarationComments.cst +++ /dev/null @@ -1,7 +0,0 @@ -public class ClassName -{ - // leading comments - private static const string SomeName = "1"; - - public SomeClass SomeName; // trailing comments -} diff --git a/Src/CSharpier.Tests/TestFiles/FieldDeclaration/BasicField.cst b/Src/CSharpier.Tests/TestFiles/FieldDeclaration/FieldDeclarations.cst similarity index 59% rename from Src/CSharpier.Tests/TestFiles/FieldDeclaration/BasicField.cst rename to Src/CSharpier.Tests/TestFiles/FieldDeclaration/FieldDeclarations.cst index c0767e1cb..309de83f7 100644 --- a/Src/CSharpier.Tests/TestFiles/FieldDeclaration/BasicField.cst +++ b/Src/CSharpier.Tests/TestFiles/FieldDeclaration/FieldDeclarations.cst @@ -30,4 +30,27 @@ public class ClassName Property4 = false, Property5 = false }; + + // leading comments + private static const string SomeName = "1"; + + public SomeClass SomeName; // trailing comments + + public System.Text.StringBuilder NamespacedField; + + public static Dictionary PropertiesByType + = new Dictionary(); + + public static Dictionary< + SomeLongTypeName, + SomeOtherLongTypeName_______________________________ + > PropertiesByType = new Dictionary< + SomeLongTypeName, + SomeOtherLongTypeName_______________________________ + >(); +} + +struct S +{ + fixed int field[10]; } diff --git a/Src/CSharpier.Tests/TestFiles/FieldDeclaration/FixedFieldWithSize.cst b/Src/CSharpier.Tests/TestFiles/FieldDeclaration/FixedFieldWithSize.cst deleted file mode 100644 index fce0a1e92..000000000 --- a/Src/CSharpier.Tests/TestFiles/FieldDeclaration/FixedFieldWithSize.cst +++ /dev/null @@ -1,4 +0,0 @@ -struct S -{ - fixed int field[10]; -} diff --git a/Src/CSharpier.Tests/TestFiles/FieldDeclaration/NamespacedField.cst b/Src/CSharpier.Tests/TestFiles/FieldDeclaration/NamespacedField.cst deleted file mode 100644 index 98189d4e2..000000000 --- a/Src/CSharpier.Tests/TestFiles/FieldDeclaration/NamespacedField.cst +++ /dev/null @@ -1,4 +0,0 @@ -public class ClassName -{ - public System.Text.StringBuilder StringBuilder; -} diff --git a/Src/CSharpier.Tests/TestFiles/FieldDeclaration/_FieldDeclarationTests.cs b/Src/CSharpier.Tests/TestFiles/FieldDeclaration/_FieldDeclarationTests.cs index 6ba74006b..9f2bd82ec 100644 --- a/Src/CSharpier.Tests/TestFiles/FieldDeclaration/_FieldDeclarationTests.cs +++ b/Src/CSharpier.Tests/TestFiles/FieldDeclaration/_FieldDeclarationTests.cs @@ -6,24 +6,9 @@ namespace CSharpier.Tests.TestFiles public class FieldDeclarationTests : BaseTest { [Test] - public void BasicField() + public void FieldDeclarations() { - this.RunTest("FieldDeclaration", "BasicField"); - } - [Test] - public void FieldDeclarationComments() - { - this.RunTest("FieldDeclaration", "FieldDeclarationComments"); - } - [Test] - public void FixedFieldWithSize() - { - this.RunTest("FieldDeclaration", "FixedFieldWithSize"); - } - [Test] - public void NamespacedField() - { - this.RunTest("FieldDeclaration", "NamespacedField"); + this.RunTest("FieldDeclaration", "FieldDeclarations"); } } } diff --git a/Src/CSharpier.Tests/TestFiles/ImplicitElementAccess/BasicImplicitElementAccess.cst b/Src/CSharpier.Tests/TestFiles/ImplicitElementAccess/ImplicitElementAccesses.cst similarity index 100% rename from Src/CSharpier.Tests/TestFiles/ImplicitElementAccess/BasicImplicitElementAccess.cst rename to Src/CSharpier.Tests/TestFiles/ImplicitElementAccess/ImplicitElementAccesses.cst diff --git a/Src/CSharpier.Tests/TestFiles/ImplicitElementAccess/_ImplicitElementAccessTests.cs b/Src/CSharpier.Tests/TestFiles/ImplicitElementAccess/_ImplicitElementAccessTests.cs index 69ee5afc2..e75dfa4f6 100644 --- a/Src/CSharpier.Tests/TestFiles/ImplicitElementAccess/_ImplicitElementAccessTests.cs +++ b/Src/CSharpier.Tests/TestFiles/ImplicitElementAccess/_ImplicitElementAccessTests.cs @@ -6,9 +6,9 @@ namespace CSharpier.Tests.TestFiles public class ImplicitElementAccessTests : BaseTest { [Test] - public void BasicImplicitElementAccess() + public void ImplicitElementAccesses() { - this.RunTest("ImplicitElementAccess", "BasicImplicitElementAccess"); + this.RunTest("ImplicitElementAccess", "ImplicitElementAccesses"); } } } diff --git a/Src/CSharpier.Tests/TestFiles/InitializerExpression/InitializerExpressions.cst b/Src/CSharpier.Tests/TestFiles/InitializerExpression/InitializerExpressions.cst index dcc23ddbe..72aa50d16 100644 --- a/Src/CSharpier.Tests/TestFiles/InitializerExpression/InitializerExpressions.cst +++ b/Src/CSharpier.Tests/TestFiles/InitializerExpression/InitializerExpressions.cst @@ -39,4 +39,13 @@ class ClassName longValue______________________________ } }; + + void MethodName() + { + var x = new SomeClass + { + Property1 = true + // should indent + }; + } } diff --git a/Src/CSharpier.Tests/TestFiles/PropertyDeclaration/PropertyDeclarations.cst b/Src/CSharpier.Tests/TestFiles/PropertyDeclaration/PropertyDeclarations.cst index 18ff79465..9fb5680f7 100644 --- a/Src/CSharpier.Tests/TestFiles/PropertyDeclaration/PropertyDeclarations.cst +++ b/Src/CSharpier.Tests/TestFiles/PropertyDeclaration/PropertyDeclarations.cst @@ -54,7 +54,9 @@ public class ClassName set; } - public virtual ICollection SomeLongNameThatForcesALineBreak { get; set; } = + public virtual ICollection< + SomeObject + > SomeLongNameThatForcesALineBreak { get; set; } = new HashSet(); public Dictionary Properties { get; set; } = diff --git a/Src/CSharpier.Tests/TestFiles/QueryExpression/QueryExpressions.cst b/Src/CSharpier.Tests/TestFiles/QueryExpression/QueryExpressions.cst index e2a2909e1..c0ac3d526 100644 --- a/Src/CSharpier.Tests/TestFiles/QueryExpression/QueryExpressions.cst +++ b/Src/CSharpier.Tests/TestFiles/QueryExpression/QueryExpressions.cst @@ -4,6 +4,13 @@ class ClassName { var query1 = from c in customers select c; query1 = from c in customers select c into d select d; + query1 = + from c in customers + join c1 in customers11111111111111111111111111111 + on c1.GetHashCode() equals c.GetHashCode() + into e + select c into d + select d; var query2 = from Customer c in customers select c; diff --git a/Src/CSharpier.Tests/TestFiles/SwitchExpression/BasicSwitchExpression.cst b/Src/CSharpier.Tests/TestFiles/SwitchExpression/BasicSwitchExpression.cst deleted file mode 100644 index 1ee905d07..000000000 --- a/Src/CSharpier.Tests/TestFiles/SwitchExpression/BasicSwitchExpression.cst +++ /dev/null @@ -1,13 +0,0 @@ -class ClassName -{ - int MethodName() - { - return 1 switch - { - 1 => 100, - 2 => 200, - 3 when false => 300, - _ => throw new global::System.Exception() - }; - } -} diff --git a/Src/CSharpier.Tests/TestFiles/SwitchExpression/SwitchWithRecursivePattern.cst b/Src/CSharpier.Tests/TestFiles/SwitchExpression/SwitchExpressions.cst similarity index 68% rename from Src/CSharpier.Tests/TestFiles/SwitchExpression/SwitchWithRecursivePattern.cst rename to Src/CSharpier.Tests/TestFiles/SwitchExpression/SwitchExpressions.cst index 1860bf6e1..1cc833d68 100644 --- a/Src/CSharpier.Tests/TestFiles/SwitchExpression/SwitchWithRecursivePattern.cst +++ b/Src/CSharpier.Tests/TestFiles/SwitchExpression/SwitchExpressions.cst @@ -1,7 +1,15 @@ class ClassName { - void MethodName() + int MethodName() { + return 1 switch + { + 1 => 100, + 2 => 200, + 3 when false => 300, + _ => throw new global::System.Exception() + }; + var newState = (GetState(), action, hasKey) switch { (DoorState.Closed, Action.Open, _) => DoorState.Opened, diff --git a/Src/CSharpier.Tests/TestFiles/SwitchExpression/_SwitchExpressionTests.cs b/Src/CSharpier.Tests/TestFiles/SwitchExpression/_SwitchExpressionTests.cs index 8fdc197f8..fbf8c5f2b 100644 --- a/Src/CSharpier.Tests/TestFiles/SwitchExpression/_SwitchExpressionTests.cs +++ b/Src/CSharpier.Tests/TestFiles/SwitchExpression/_SwitchExpressionTests.cs @@ -6,14 +6,9 @@ namespace CSharpier.Tests.TestFiles public class SwitchExpressionTests : BaseTest { [Test] - public void BasicSwitchExpression() + public void SwitchExpressions() { - this.RunTest("SwitchExpression", "BasicSwitchExpression"); - } - [Test] - public void SwitchWithRecursivePattern() - { - this.RunTest("SwitchExpression", "SwitchWithRecursivePattern"); + this.RunTest("SwitchExpression", "SwitchExpressions"); } } } diff --git a/Src/CSharpier/CommandLineFormatter.cs b/Src/CSharpier/CommandLineFormatter.cs index ba8c665bb..5b426aab0 100644 --- a/Src/CSharpier/CommandLineFormatter.cs +++ b/Src/CSharpier/CommandLineFormatter.cs @@ -254,6 +254,7 @@ private async Task FormatFile(string file, CancellationToken cancellationToken) && result.Code != fileReaderResult.FileContents ) { // purposely avoid async here, that way the file completely writes if the process gets cancelled while running. + // ReSharper disable once MethodHasAsyncOverloadWithCancellation this.FileSystem.File.WriteAllText(file, result.Code, fileReaderResult.Encoding); } } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/AssignmentExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/AssignmentExpression.cs index 55cc586a3..5c64c3406 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/AssignmentExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/AssignmentExpression.cs @@ -12,8 +12,9 @@ public static Doc Print(AssignmentExpressionSyntax node) Node.Print(node.Left), " ", Token.Print(node.OperatorToken), - node.Right is QueryExpressionSyntax ? Doc.Null : " ", - Node.Print(node.Right) + node.Right is QueryExpressionSyntax + ? Doc.Indent(Doc.Line, Node.Print(node.Right)) + : Doc.Concat(" ", Node.Print(node.Right)) ); } } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseFieldDeclaration.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseFieldDeclaration.cs index 1ad784766..c30a0195a 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseFieldDeclaration.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseFieldDeclaration.cs @@ -19,7 +19,7 @@ public static Doc Print(BaseFieldDeclarationSyntax node) docs.Add(Token.PrintWithSuffix(eventFieldDeclarationSyntax.EventKeyword, " ")); } - docs.Add(Node.Print(node.Declaration), Token.Print(node.SemicolonToken)); + docs.Add(VariableDeclaration.Print(node.Declaration), Token.Print(node.SemicolonToken)); return Doc.Concat(docs); } } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/EqualsValueClause.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/EqualsValueClause.cs index dc6ba04d0..077ea0116 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/EqualsValueClause.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/EqualsValueClause.cs @@ -18,14 +18,15 @@ public static Doc Print(EqualsValueClauseSyntax node) separator = Doc.Null; } else if ( - node.Value is AnonymousObjectCreationExpressionSyntax - || node.Value is AnonymousMethodExpressionSyntax - || node.Value is ConditionalExpressionSyntax - || node.Value is ObjectCreationExpressionSyntax - || node.Value is InitializerExpressionSyntax - || node.Value is ParenthesizedLambdaExpressionSyntax - || node.Value is InvocationExpressionSyntax - || node.Value is SwitchExpressionSyntax + node.Value + is AnonymousObjectCreationExpressionSyntax + or AnonymousMethodExpressionSyntax + or ConditionalExpressionSyntax + or ObjectCreationExpressionSyntax + or InitializerExpressionSyntax + or ParenthesizedLambdaExpressionSyntax + or InvocationExpressionSyntax + or SwitchExpressionSyntax ) { separator = " "; } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/QueryExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/QueryExpression.cs index 64ef37a1c..87ce53059 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/QueryExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/QueryExpression.cs @@ -7,8 +7,7 @@ public static class QueryExpression { public static Doc Print(QueryExpressionSyntax node) { - return Doc.Indent( - Doc.Line, + return Doc.Concat( FromClause.Print(node.FromClause), Doc.Line, QueryBody.Print(node.Body) diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/TypeArgumentList.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/TypeArgumentList.cs index 44cce728e..dbc218deb 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/TypeArgumentList.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/TypeArgumentList.cs @@ -11,7 +11,11 @@ public static Doc Print(TypeArgumentListSyntax node) { return Doc.Concat( Token.Print(node.LessThanToken), - Doc.Indent(SeparatedSyntaxList.Print(node.Arguments, Node.Print, Doc.Line)), + Doc.Indent( + Doc.SoftLine, + SeparatedSyntaxList.Print(node.Arguments, Node.Print, Doc.Line) + ), + Doc.SoftLine, Token.Print(node.GreaterThanToken) ); } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/VariableDeclaration.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/VariableDeclaration.cs index 2f89310a6..05714268e 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/VariableDeclaration.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/VariableDeclaration.cs @@ -8,19 +8,71 @@ public static class VariableDeclaration { public static Doc Print(VariableDeclarationSyntax node) { - var docs = Doc.Concat( - SeparatedSyntaxList.Print( - node.Variables, - VariableDeclarator.Print, - node.Parent is ForStatementSyntax ? Doc.Line : Doc.HardLine - ) - ); + if (node.Variables.Count > 1) + { + var docs = Doc.Concat( + SeparatedSyntaxList.Print( + node.Variables, + VariableDeclarator.Print, + node.Parent is ForStatementSyntax ? Doc.Line : Doc.HardLine + ) + ); + + return Doc.Concat(Node.Print(node.Type), " ", Doc.Indent(docs)); + } + + var variable = node.Variables[0]; + var initializer = variable.Initializer; + + var useLineInIndent = + initializer?.Value + is BinaryExpressionSyntax + // TODO we might need IndentIfBreak for the edge cases on invocation and object creation + //or InvocationExpressionSyntax + or InterpolatedStringExpressionSyntax + or IsPatternExpressionSyntax + or LiteralExpressionSyntax + //or ObjectCreationExpressionSyntax + or QueryExpressionSyntax + or StackAllocArrayCreationExpressionSyntax; return Doc.Concat( - Node.Print(node.Type), - " ", - node.Variables.Count > 1 ? Doc.Indent(docs) : docs + Doc.Group( + Node.Print(node.Type), + " ", + Token.Print(variable.Identifier), + variable.ArgumentList != null + ? BracketedArgumentList.Print(variable.ArgumentList) + : Doc.Null, + initializer != null + ? Doc.Concat(" ", Token.Print(initializer.EqualsToken)) + : Doc.Null + ), + initializer != null + ? Doc.Concat( + useLineInIndent ? Doc.Null : Doc.Group(Doc.Line), + Doc.Group(IndentIfNeeded(initializer.Value, useLineInIndent)) + ) + : Doc.Null ); } + + private static Doc IndentIfNeeded(ExpressionSyntax initializerValue, bool useLineInIndent) + { + if ( + initializerValue + is AnonymousObjectCreationExpressionSyntax + or AnonymousMethodExpressionSyntax + or InitializerExpressionSyntax + or InvocationExpressionSyntax + or ConditionalExpressionSyntax + or ObjectCreationExpressionSyntax + or SwitchExpressionSyntax + ) { + return Node.Print(initializerValue); + } + + return Doc.Indent(useLineInIndent ? Doc.Line : Doc.Null, Node.Print(initializerValue)); + } } }