Skip to content

Commit

Permalink
Modify ObjectCreationExpression with Initializer to use space brace (#…
Browse files Browse the repository at this point in the history
…309)

closes #113
  • Loading branch information
belav authored Jun 16, 2021
1 parent 4f4446b commit f82bd8b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public class ClassName
private SomeObject SomeLongerName = new SomeObject(
"lkjasdflkjasdfkljaskldjf",
"klasldkfaksdfasdfkjasdklf"
)
{
) {
Property1 = 1,
Property2 = 2
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public class ClassName
public ClassName WithComplexConstructor = new ClassName(
"parameter1",
new ClassName("parameter1", "parameter2") { Property1 = true }
)
{
) {
Property1 = true
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,26 @@ namespace CSharpier.SyntaxPrinter.SyntaxNodePrinters
public static class InitializerExpression
{
public static Doc Print(InitializerExpressionSyntax node)
{
return Print(node, null);
}

public static Doc PrintWithConditionalSpace(
InitializerExpressionSyntax node,
string groupId
) {
return Print(node, groupId);
}

private static Doc Print(InitializerExpressionSyntax node, string? groupId)
{
var result = Doc.Concat(
node.Kind()
is SyntaxKind.CollectionInitializerExpression
or SyntaxKind.ObjectInitializerExpression
&& node.Parent is AssignmentExpressionSyntax ? Doc.Line : Doc.Null,
groupId != null
? Doc.IfBreak(" ", Doc.Line, groupId)
: node.Kind()
is SyntaxKind.CollectionInitializerExpression
or SyntaxKind.ObjectInitializerExpression
&& node.Parent is AssignmentExpressionSyntax ? Doc.Line : Doc.Null,
Token.Print(node.OpenBraceToken),
Doc.Indent(
Doc.Line,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using CSharpier.DocTypes;
using Microsoft.CodeAnalysis.CSharp.Syntax;

Expand All @@ -7,13 +8,29 @@ public static class ObjectCreationExpression
{
public static Doc Print(ObjectCreationExpressionSyntax node)
{
var groupId = Guid.NewGuid().ToString();

return Doc.Group(
Token.PrintWithSuffix(node.NewKeyword, " "),
Node.Print(node.Type),
node.ArgumentList != null ? ArgumentList.Print(node.ArgumentList) : string.Empty,
node.ArgumentList != null
? Doc.GroupWithId(
groupId,
ArgumentListLike.Print(
node.ArgumentList.OpenParenToken,
node.ArgumentList.Arguments,
node.ArgumentList.CloseParenToken
)
)
: Doc.Null,
node.Initializer != null
? Doc.Concat(Doc.Line, InitializerExpression.Print(node.Initializer))
: string.Empty
? node.ArgumentList != null
? InitializerExpression.PrintWithConditionalSpace(
node.Initializer,
groupId
)
: Doc.Concat(Doc.Line, InitializerExpression.Print(node.Initializer))
: Doc.Null
);
}
}
Expand Down

0 comments on commit f82bd8b

Please sign in to comment.