Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,6 @@ node.Parent is CompilationUnitSyntax compilationUnitSyntax

docs.Append(Token.Print(node.CloseBracketToken, context));

return Doc.Group(docs.AsSpan().ToArray());
return Doc.Group(ref docs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,21 @@ public static Doc Print(CSharpSyntaxNode node, PrintingContext context)
semicolonToken = localFunctionStatementSyntax.SemicolonToken;
}

var docs = new List<Doc>();
var declarationGroup = new List<Doc>();
var docs = new ValueListBuilder<Doc>([null, null, null, null, null, null, null, null]);

if (node is LocalFunctionStatementSyntax)
{
docs.Add(ExtraNewLines.Print(node));
docs.Append(ExtraNewLines.Print(node));
}

if (attributeLists is { Count: > 0 })
{
docs.Add(AttributeLists.Print(node, attributeLists.Value, context));
docs.Append(AttributeLists.Print(node, attributeLists.Value, context));

void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
void PrintMethodUnformattedWithoutAttributes(
SyntaxTriviaList syntaxTriviaList,
ref ValueListBuilder<Doc> docs
)
{
var attributeStart = attributeLists
.Value[0]
Expand All @@ -110,7 +112,7 @@ void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
.ToString()
.Trim();

docs.Add(
docs.Append(
RemoveWhiteSpaceLineEndingsRegex.Replace(
methodWithoutAttributes,
context.Options.LineEnding
Expand All @@ -122,21 +124,32 @@ void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
{
if (CSharpierIgnore.HasIgnoreComment(modifiers.Value[0]))
{
PrintMethodUnformattedWithoutAttributes(modifiers.Value[0].LeadingTrivia);
return Doc.Group(docs);
PrintMethodUnformattedWithoutAttributes(
modifiers.Value[0].LeadingTrivia,
ref docs
);
var returnDoc = Doc.Group(ref docs);
docs.Dispose();
return returnDoc;
}
}
else if (returnType is not null && CSharpierIgnore.HasIgnoreComment(returnType))
{
PrintMethodUnformattedWithoutAttributes(returnType.GetLeadingTrivia());
return Doc.Group(docs);
PrintMethodUnformattedWithoutAttributes(returnType.GetLeadingTrivia(), ref docs);
var returnDoc = Doc.Group(ref docs);
docs.Dispose();
return returnDoc;
}
}

var declarationGroup = new ValueListBuilder<Doc>(
[null, null, null, null, null, null, null, null]
);

if (modifiers is { Count: > 0 })
{
docs.Add(Token.PrintLeadingTrivia(modifiers.Value[0], context));
declarationGroup.Add(
docs.Append(Token.PrintLeadingTrivia(modifiers.Value[0], context));
declarationGroup.Append(
Modifiers.PrintSorterWithoutLeadingTrivia(modifiers.Value, context)
);
}
Expand All @@ -145,30 +158,30 @@ void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
{
if (modifiers is not { Count: > 0 })
{
docs.Add(Token.PrintLeadingTrivia(returnType.GetLeadingTrivia(), context));
docs.Append(Token.PrintLeadingTrivia(returnType.GetLeadingTrivia(), context));
context.State.SkipNextLeadingTrivia = true;
}

declarationGroup.Add(Node.Print(returnType, context), " ");
declarationGroup.Append(Node.Print(returnType, context), " ");
context.State.SkipNextLeadingTrivia = false;
}

if (explicitInterfaceSpecifier != null)
{
declarationGroup.Add(
declarationGroup.Append(
Node.Print(explicitInterfaceSpecifier.Name, context),
Token.Print(explicitInterfaceSpecifier.DotToken, context)
);
}

if (identifier != null)
{
declarationGroup.Add(identifier());
declarationGroup.Append(identifier());
}

if (node is ConversionOperatorDeclarationSyntax conversionOperatorDeclarationSyntax)
{
declarationGroup.Add(
declarationGroup.Append(
Token.PrintWithSuffix(
conversionOperatorDeclarationSyntax.ImplicitOrExplicitKeyword,
" ",
Expand All @@ -189,7 +202,7 @@ void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
}
else if (node is OperatorDeclarationSyntax operatorDeclarationSyntax)
{
declarationGroup.Add(
declarationGroup.Append(
Node.Print(operatorDeclarationSyntax.ReturnType, context),
" ",
operatorDeclarationSyntax.ExplicitInterfaceSpecifier is not null
Expand All @@ -203,24 +216,24 @@ operatorDeclarationSyntax.ExplicitInterfaceSpecifier is not null

if (typeParameterList != null && typeParameterList.Parameters.Any())
{
declarationGroup.Add(TypeParameterList.Print(typeParameterList, context));
declarationGroup.Append(TypeParameterList.Print(typeParameterList, context));
}

if (parameterList != null)
{
if (parameterList.Parameters.Any())
{
declarationGroup.Add(ParameterList.Print(parameterList, context));
declarationGroup.Append(ParameterList.Print(parameterList, context));
}
else
{
declarationGroup.Add(
declarationGroup.Append(
Token.Print(parameterList.OpenParenToken, context),
Token.Print(parameterList.CloseParenToken, context)
);
}

declarationGroup.Add(Doc.IfBreak(Doc.Null, Doc.SoftLine));
declarationGroup.Append(Doc.IfBreak(Doc.Null, Doc.SoftLine));
}

if (constructorInitializer != null)
Expand All @@ -230,7 +243,7 @@ operatorDeclarationSyntax.ExplicitInterfaceSpecifier is not null
ArgumentList.Print(constructorInitializer.ArgumentList, context)
);

declarationGroup.Add(
declarationGroup.Append(
Doc.Group(
Doc.Indent(Doc.HardLine),
Doc.Indent(colonToken),
Expand All @@ -240,30 +253,31 @@ operatorDeclarationSyntax.ExplicitInterfaceSpecifier is not null
);
}

docs.Add(Doc.Group(declarationGroup));
docs.Append(Doc.Group(ref declarationGroup));
declarationGroup.Dispose();

if (constraintClauses != null)
{
docs.Add(ConstraintClauses.Print(constraintClauses.Value, context));
docs.Append(ConstraintClauses.Print(constraintClauses.Value, context));
}

if (body != null)
{
docs.Add(Block.Print(body, context));
docs.Append(Block.Print(body, context));
}
else
{
if (expressionBody != null)
{
docs.Add(ArrowExpressionClause.Print(expressionBody, context));
docs.Append(ArrowExpressionClause.Print(expressionBody, context));
}
}

if (semicolonToken.HasValue)
{
docs.Add(Token.Print(semicolonToken.Value, context));
docs.Append(Token.Print(semicolonToken.Value, context));
}

return Doc.Group(docs);
return Doc.Group(ref docs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public static Doc Print(ParameterSyntax node, PrintingContext context)
docs.Append(EqualsValueClause.Print(node.Default, context));
}

return hasAttribute ? Doc.Group(docs.AsSpan().ToArray()) : Doc.Concat(ref docs);
return hasAttribute ? Doc.Group(ref docs) : Doc.Concat(ref docs);
}
}
3 changes: 3 additions & 0 deletions Src/CSharpier.Core/DocTypes/Doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public static Group GroupWithId(string groupId, params Doc[] contents)

public static Group Group(params Doc[] contents) => new() { Contents = Concat(contents) };

public static Group Group(ref ValueListBuilder<Doc> contents) =>
new() { Contents = Concat(ref contents) };

// prevents allocating an array if there is only a single parameter
public static IndentDoc Indent(Doc contents) => new() { Contents = contents };

Expand Down