Skip to content

Commit 033c83f

Browse files
committed
Fixing edgecase with lambda expressions
closes #1594
1 parent 8b9df8c commit 033c83f

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

Src/CSharpier.Core/CSharp/SyntaxPrinter/SyntaxNodePrinters/InvocationExpression.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ is ArrayCreationExpressionSyntax
116116
parent is ExpressionStatementSyntax expressionStatementSyntax
117117
&& expressionStatementSyntax.SemicolonToken.LeadingTrivia.Any(o => o.IsComment())
118118
)
119+
|| groups.Count == 1
119120
? expanded
120121
: Doc.ConditionalGroup(Doc.Concat(oneLine), expanded);
121122
}

Src/CSharpier.Core/CSharp/SyntaxPrinter/SyntaxNodePrinters/ParenthesizedLambdaExpression.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,24 @@ public static Doc PrintHead(ParenthesizedLambdaExpressionSyntax node, PrintingCo
2727

2828
public static Doc PrintBody(ParenthesizedLambdaExpressionSyntax node, PrintingContext context)
2929
{
30-
return node.Body switch
30+
if (node.Body is BlockSyntax block)
3131
{
32-
BlockSyntax block => Doc.Concat(
32+
return Doc.Concat(
3333
block.Statements.Count > 0 ? Doc.HardLine : " ",
3434
Block.Print(block, context)
35-
),
36-
_ => Doc.Group(Doc.Indent(Doc.Line, Node.Print(node.Body, context))),
37-
};
35+
);
36+
}
37+
38+
var body = Node.Print(node.Body, context);
39+
40+
if (
41+
node.ParameterList.Parameters.Count == 0
42+
&& node.Parent?.Parent is ArgumentListSyntax { Arguments.Count: 1 }
43+
)
44+
{
45+
return Doc.IfBreak(Doc.Indent(Doc.Line, body), Doc.Group(Doc.Indent(Doc.Line, body)));
46+
}
47+
48+
return Doc.Group(Doc.Indent(Doc.Line, body));
3849
}
3950
}

Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ParenthesizedLambdaExpressions.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,21 @@ public class ClassName
9292
var returnType = object (bool o) => o ? 1 : "two";
9393
var returnTypeWithAsync = async object (bool o) => o ? 1 : "two";
9494
var returnTypeWithAttribute = [Attribute] object (bool o) => o ? 1 : "two";
95+
96+
CallMethod(() => CallAnotherMethod______________________________________________________());
97+
CallMethod(() =>
98+
CallAnotherMethod______________________________________________________1()
99+
);
100+
CallMethod(() =>
101+
CallAnotherMethod______________________________________________________12()
102+
);
103+
CallMethod(() =>
104+
CallAnotherMethod______________________________________________________123()
105+
);
106+
107+
CallMethod(
108+
CallAnotherMethod_________________(),
109+
() => CallAnotherMethod_________________()
110+
);
95111
}
96112
}

0 commit comments

Comments
 (0)