Skip to content

Commit

Permalink
Allowing new lines in initializers (#1113)
Browse files Browse the repository at this point in the history
closes #1110
  • Loading branch information
belav committed Jan 15, 2024
1 parent b634ee3 commit 207353a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var someObject = new
{
NoLineAboveHere = 1,

ThisLineIsOkay = 2,

// comment
AndThisLine = 3,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var someObject = new
{

NoLineAboveHere = 1,

ThisLineIsOkay = 2,

// comment
AndThisLine = 3,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var someObject = new SomeObject
{
NoLineAboveHere = 1,

ThisLineIsOkay = 2,

// comment
AndThisLine = 3,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var someObject = new SomeObject
{

NoLineAboveHere = 1,

ThisLineIsOkay = 2,

// comment
AndThisLine = 3,
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ internal static class AnonymousObjectMemberDeclarator
public static Doc Print(AnonymousObjectMemberDeclaratorSyntax node, FormattingContext context)
{
var docs = new List<Doc>();
if (
node.Parent is AnonymousObjectCreationExpressionSyntax parent
&& node != parent.Initializers.First()
)
{
docs.Add(ExtraNewLines.Print(node));
}

if (node.NameEquals != null)
{
docs.Add(Token.PrintWithSuffix(node.NameEquals.Name.Identifier, " ", context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ internal static class AssignmentExpression
{
public static Doc Print(AssignmentExpressionSyntax node, FormattingContext context)
{
Doc possibleNewLines = Doc.Null;

if (node.Parent is InitializerExpressionSyntax parent && node != parent.Expressions.First())
{
possibleNewLines = ExtraNewLines.Print(node);
}

return RightHandSide.Print(
node,
Doc.Concat(Node.Print(node.Left, context), " "),
Doc.Concat(possibleNewLines, Node.Print(node.Left, context), " "),
Token.Print(node.OperatorToken, context),
node.Right,
context
Expand Down

0 comments on commit 207353a

Please sign in to comment.