Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Src/CSharpier.Core/CSharp/SyntaxPrinter/ArgumentListLikeSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CSharpier.Core.DocTypes;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;

namespace CSharpier.Core.CSharp.SyntaxPrinter;

Expand Down Expand Up @@ -78,6 +79,21 @@ lambda.Block is not null
{
args = SeparatedSyntaxList.Print(arguments, Argument.Print, Doc.Line, context);
}
else if (
arguments.Count > 1
&& arguments[^1].Expression is LambdaExpressionSyntax lastLambda
&& !arguments
.Take(arguments.Count - 1)
.Any(o => o.Expression is AnonymousFunctionExpressionSyntax)
&& !openParenToken
.Parent!.DescendantTrivia(
TextSpan.FromBounds(openParenToken.SpanStart, lastLambda.ArrowToken.SpanStart)
)
.Any(o => o.IsComment() || o.IsDirective)
)
{
args = ArgumentListWithTrailingLambda.Print(arguments, lastLambda, context);
}
else if (arguments.Count > 0)
{
args = Doc.Concat(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using CSharpier.Core.CSharp.SyntaxPrinter.SyntaxNodePrinters;
using CSharpier.Core.DocTypes;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace CSharpier.Core.CSharp.SyntaxPrinter;

internal static class ArgumentListWithTrailingLambda
{
public static Doc Print(
SeparatedSyntaxList<ArgumentSyntax> arguments,
LambdaExpressionSyntax lastLambda,
PrintingContext context
)
{
var chop = Doc.Concat(
Doc.Indent(
Doc.SoftLine,
SeparatedSyntaxList.Print(arguments, Argument.Print, Doc.Line, context)
),
Doc.SoftLine
);

Doc lambdaHead;
Doc lambdaBody;
bool bodyIsBraced;
if (lastLambda is SimpleLambdaExpressionSyntax simpleLambda)
{
bodyIsBraced =
simpleLambda.Body
is BlockSyntax
or ObjectCreationExpressionSyntax
or AnonymousObjectCreationExpressionSyntax;

lambdaHead = SimpleLambdaExpression.PrintHead(simpleLambda, context);
lambdaBody = SimpleLambdaExpression.PrintBody(simpleLambda, context);
}
else if (lastLambda is ParenthesizedLambdaExpressionSyntax parenthesizedLambda)
{
bodyIsBraced = parenthesizedLambda.Block is not null;
lambdaHead = ParenthesizedLambdaExpression.PrintHead(parenthesizedLambda, context);
lambdaBody = bodyIsBraced
? ParenthesizedLambdaExpression.PrintBody(parenthesizedLambda, context)
: Doc.Indent(Doc.Line, Node.Print(parenthesizedLambda.Body, context));
}
else
{
return chop;
}

var flatParts = new List<Doc>((arguments.Count - 1) * 3 + 2);
for (var x = 0; x < arguments.Count - 1; x++)
{
flatParts.Add(Argument.Print(arguments[x], context));
flatParts.Add(Token.Print(arguments.GetSeparator(x), context));
flatParts.Add(" ");
}

flatParts.Add(Argument.PrintModifiers(arguments[^1], context));
flatParts.Add(lambdaHead);

if (flatParts.Any(DocUtilities.ContainsBreak))
{
return chop;
}

var wrap = Doc.Concat(
Doc.ForceFlat(flatParts),
lambdaBody,
bodyIsBraced ? Doc.Null : Doc.SoftLine
);

return Doc.ConditionalGroup(wrap, wrap, chop);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class ClassName
internal StackObjectPool<
Dictionary<object, KeyedItemInfo>
> KeyedItemInfoDictionaryPool { get; } =
new StackObjectPool<Dictionary<object, KeyedItemInfo>>(
maxPreservedItems: 10,
() => new Dictionary<object, KeyedItemInfo>()
new StackObjectPool<Dictionary<object, KeyedItemInfo>>(maxPreservedItems: 10, () =>
new Dictionary<object, KeyedItemInfo>()
);

RenderFragment<AuthenticationState> customNotAuthorized = state =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,26 @@ public class ClassName
CallAnotherMethod______________________________________________________123()
);

CallMethod(CallAnotherMethod_________________(), () =>
CallAnotherMethod_________________()
);

CallMethod(
CallAnotherMethod_________________(),
// Comment
() => CallAnotherMethod_________________()
);

CallMethod(
#if DEBUG
CallAnotherMethod_________________(),
#endif
() => CallAnotherMethod_________________()
);

CallMethod(
CallAnotherMethod_________________(),
() => CallAnotherMethod_________________(),
() => CallAnotherMethod_________________()
);
}
Expand Down