diff --git a/Src/CSharpier.Tests/TestFiles/InitializerExpression/InitializerExpressions.cst b/Src/CSharpier.Tests/TestFiles/InitializerExpression/InitializerExpressions.cst index 3a94f5909..24bf28e13 100644 --- a/Src/CSharpier.Tests/TestFiles/InitializerExpression/InitializerExpressions.cst +++ b/Src/CSharpier.Tests/TestFiles/InitializerExpression/InitializerExpressions.cst @@ -2,7 +2,8 @@ class ClassName { int[] array = { 1, 2 }; - int[] array = { + int[] array = + { "someLongValue_____________________________________", "someLongValue_____________________________________" }; @@ -32,7 +33,8 @@ class ClassName } }; - private int[,] arrayInitializerWithoutSize = { + private int[,] arrayInitializerWithoutSize = + { // leading comment should not interfere with the next line breaking { longValue______________________________, @@ -56,7 +58,20 @@ class ClassName var objectInitializerExpression = new SomeObject() { - ShouldNotBreak = { SecurePolicy = CookieSecurePolicy.Always } + ShouldNotBreak = { SomeProperty = SomeValue }, + ShouldBreak = + { + SomeOtherProperty = SomeLongValue_______________________________________ + }, + SomeList = new List<SomeObject>() + { + new() { SomeProperty = SomeLongValue_____________ }, + new() + { + SomeProperty = SomeOtherValue, + AnotherProperty = SomeThirdValue_______________________ + } + } }; var collectionInitializerExpressions = new SomeObject diff --git a/Src/CSharpier/SyntaxPrinter/RightHandSide.cs b/Src/CSharpier/SyntaxPrinter/RightHandSide.cs index 11051f4b9..3a9ae2d1d 100644 --- a/Src/CSharpier/SyntaxPrinter/RightHandSide.cs +++ b/Src/CSharpier/SyntaxPrinter/RightHandSide.cs @@ -1,5 +1,6 @@ using System; using CSharpier.DocTypes; +using CSharpier.SyntaxPrinter.SyntaxNodePrinters; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -12,23 +13,21 @@ public static Doc Print(ExpressionSyntax node) var groupId = Guid.NewGuid().ToString(); return node switch { + InitializerExpressionSyntax initializerExpressionSyntax => InitializerExpression.PrintWithLine( + initializerExpressionSyntax + ), InvocationExpressionSyntax or ParenthesizedLambdaExpressionSyntax or ObjectCreationExpressionSyntax or ElementAccessExpressionSyntax or ArrayCreationExpressionSyntax + or InitializerExpressionSyntax or ImplicitArrayCreationExpressionSyntax => Doc.Group( Doc.GroupWithId(groupId, Doc.Indent(Doc.Line)), Doc.IndentIfBreak(Doc.Group(Node.Print(node)), groupId) ), - InitializerExpressionSyntax when node.Kind() - is SyntaxKind.CollectionInitializerExpression => Doc.Group( - Doc.Line, - Node.Print(node) - ), AnonymousObjectCreationExpressionSyntax or AnonymousMethodExpressionSyntax - or InitializerExpressionSyntax or ConditionalExpressionSyntax or SwitchExpressionSyntax or LambdaExpressionSyntax diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ImplicitObjectCreationExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ImplicitObjectCreationExpression.cs index acffc6114..cad9ba2fd 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ImplicitObjectCreationExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/ImplicitObjectCreationExpression.cs @@ -8,7 +8,7 @@ public static class ImplicitObjectCreationExpression { public static Doc Print(ImplicitObjectCreationExpressionSyntax node) { - return Doc.Concat( + return Doc.Group( Token.Print(node.NewKeyword), ArgumentList.Print(node.ArgumentList), node.Initializer != null diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InitializerExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InitializerExpression.cs index 8c5f7845e..415e6208c 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InitializerExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InitializerExpression.cs @@ -21,10 +21,20 @@ string groupId return Print(node, groupId); } - private static Doc Print(InitializerExpressionSyntax node, string? groupId) + public static Doc PrintWithLine(InitializerExpressionSyntax node) { + return Print(node, null, true); + } + + private static Doc Print( + InitializerExpressionSyntax node, + string? groupId, + bool useLine = false + ) { var result = Doc.Concat( - groupId != null ? Doc.IfBreak(" ", Doc.Line, groupId) : Doc.Null, + groupId != null + ? Doc.IfBreak(" ", Doc.Line, groupId) + : useLine ? Doc.Line : Doc.Null, Token.Print(node.OpenBraceToken), Doc.Indent( Doc.Line,