Skip to content

Commit

Permalink
Fixing some formatting of initializer expressions
Browse files Browse the repository at this point in the history
closes #336
closes #335
  • Loading branch information
belav committed Jul 9, 2021
1 parent 00f6eb8 commit bfe593f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class ClassName
{
int[] array = { 1, 2 };

int[] array = {
int[] array =
{
"someLongValue_____________________________________",
"someLongValue_____________________________________"
};
Expand Down Expand Up @@ -32,7 +33,8 @@ class ClassName
}
};

private int[,] arrayInitializerWithoutSize = {
private int[,] arrayInitializerWithoutSize =
{
// leading comment should not interfere with the next line breaking
{
longValue______________________________,
Expand All @@ -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
Expand Down
11 changes: 5 additions & 6 deletions Src/CSharpier/SyntaxPrinter/RightHandSide.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using CSharpier.DocTypes;
using CSharpier.SyntaxPrinter.SyntaxNodePrinters;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit bfe593f

Please sign in to comment.