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
1 change: 0 additions & 1 deletion Src/CSharpier.Core/CSharp/SyntaxPrinter/Modifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ PrintingContext context
sortedModifiers
.Skip(1)
.Select(o => Token.PrintWithSuffix(o, " ", context))
.ToArray()
)
: Doc.Null
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static Doc Print(ArrayTypeSyntax node, PrintingContext context)
{
return Doc.Concat(
Node.Print(node.ElementType, context),
Doc.Concat(node.RankSpecifiers.Select(o => Node.Print(o, context)).ToArray())
Doc.Concat(node.RankSpecifiers.Select(o => Node.Print(o, context)))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ is [

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

return Doc.Group(docs.AsSpan().ToArray());
return Doc.Group(Doc.Concat(ref docs));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ PrintingContext context
Token.Print(node.AccessorList.OpenBraceToken, context),
Doc.Indent(
node.AccessorList.Accessors.Select(o =>
PrintAccessorDeclarationSyntax(o, separator, context)
)
.ToArray()
PrintAccessorDeclarationSyntax(o, separator, context)
)
),
separator,
Token.Print(node.AccessorList.CloseBraceToken, context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ node.Parent is ConditionalExpressionSyntax conditionalExpressionSyntax
&& conditionalExpressionSyntax.WhenFalse != node
);

return shouldNotIndent
? Doc.Group(docs)
: Doc.Group(docs[0], Doc.Indent(docs.Skip(1).ToList()));
return shouldNotIndent ? Doc.Group(docs) : Doc.Group(docs[0], Doc.Indent(docs.Skip(1)));
}

// The goal of this is to group operators of the same precedence such that they all break or none of them break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal static class ImplicitArrayCreationExpression
{
public static Doc Print(ImplicitArrayCreationExpressionSyntax node, PrintingContext context)
{
var commas = node.Commas.Select(o => Token.Print(o, context)).ToArray();
var commas = node.Commas.Select(o => Token.Print(o, context));
return Doc.Group(
Token.Print(node.NewKeyword, context),
Token.Print(node.OpenBracketToken, context),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ is LiteralExpressionSyntax
}

var expanded = Doc.Concat(
Doc.Concat(groups[0].Select(o => o.Doc).ToArray()),
Doc.Concat(groups[0].Select(o => o.Doc)),
shouldMergeFirstTwoGroups
? Doc.IndentIf(
groups.Count > 2 && groups[1].Last().Doc is not Group { Contents: IndentDoc },
Doc.Concat(groups[1].Select(o => o.Doc).ToArray())
Doc.Concat(groups[1].Select(o => o.Doc))
)
: Doc.Null,
PrintIndentedGroup(groups.Skip(shouldMergeFirstTwoGroups ? 2 : 1).ToList())
Expand Down
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(Doc.Concat(ref docs)) : Doc.Concat(ref docs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ public static Doc Print(SwitchSectionSyntax node, PrintingContext context)
docs.Append(
Doc.Indent(
node.Statements.First() is BlockSyntax ? Doc.Null : Doc.HardLine,
Doc.Join(
Doc.HardLine,
node.Statements.Select(o => Node.Print(o, context)).ToArray()
)
Doc.Join(Doc.HardLine, node.Statements.Select(o => Node.Print(o, context)))
)
);
}
Expand Down
9 changes: 5 additions & 4 deletions Src/CSharpier.Core/CSharp/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,14 @@ public static Doc PrintLeadingTrivia(SyntaxToken syntaxToken, PrintingContext co
|| syntaxToken.Parent is CollectionExpressionSyntax
&& syntaxToken.RawSyntaxKind() == SyntaxKind.CloseBracketToken;

var leadingTrivia = syntaxToken.LeadingTrivia;
var printedTrivia = PrivatePrintLeadingTrivia(
syntaxToken.LeadingTrivia,
leadingTrivia,
context,
skipLastHardline: isClosingBrace
);

var hasDirective = Enumerable.Any(syntaxToken.LeadingTrivia, o => o.IsDirective);
var hasDirective = Enumerable.Any(leadingTrivia, o => o.IsDirective);

if (hasDirective)
{
Expand All @@ -198,9 +199,9 @@ public static Doc PrintLeadingTrivia(SyntaxToken syntaxToken, PrintingContext co

Doc extraNewLines = Doc.Null;

if (hasDirective || Enumerable.Any(syntaxToken.LeadingTrivia, o => o.IsComment()))
if (hasDirective || Enumerable.Any(leadingTrivia, o => o.IsComment()))
{
extraNewLines = ExtraNewLines.Print(syntaxToken.LeadingTrivia);
extraNewLines = ExtraNewLines.Print(leadingTrivia);
}

return printedTrivia != Doc.Null || extraNewLines != Doc.Null
Expand Down
30 changes: 26 additions & 4 deletions Src/CSharpier.Core/DocTypes/Doc.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
using CSharpier.Core.Utilities;

namespace CSharpier.Core.DocTypes;
Expand Down Expand Up @@ -53,6 +54,21 @@ public static Doc Concat(List<Doc> contents) =>

public static Doc Concat(params Doc[] contents) => new Concat(contents);

[SkipLocalsInit]
public static Doc Concat(IEnumerable<Doc> contents)
{
var docs = new ValueListBuilder<Doc>([null, null, null, null, null, null, null, null]);

foreach (var item in contents)
{
docs.Append(item);
}

var returnDoc = Concat(ref docs);
docs.Dispose();
return returnDoc;
}

public static Doc Concat(ref ValueListBuilder<Doc> contents)
{
return contents.Length switch
Expand All @@ -63,23 +79,26 @@ public static Doc Concat(ref ValueListBuilder<Doc> contents)
};
}

[SkipLocalsInit]
public static Doc Join(Doc separator, IEnumerable<Doc> enumerable)
{
var docs = new List<Doc>();
var docs = new ValueListBuilder<Doc>([null, null, null, null, null, null, null, null]);

var x = 0;
foreach (var doc in enumerable)
{
if (x != 0)
{
docs.Add(separator);
docs.Append(separator);
}

docs.Add(doc);
docs.Append(doc);
x++;
}

return docs.Count == 1 ? docs[0] : Concat(docs);
var returnDoc = Concat(ref docs);
docs.Dispose();
return returnDoc;
}

public static ForceFlat ForceFlat(List<Doc> contents) =>
Expand Down Expand Up @@ -135,6 +154,9 @@ public static Group GroupWithId(string groupId, params Doc[] contents)

public static IndentDoc Indent(List<Doc> contents) => new() { Contents = Concat(contents) };

public static IndentDoc Indent(IEnumerable<Doc> contents) =>
new() { Contents = Concat(contents) };

public static Doc IndentIf(bool condition, Doc contents)
{
return condition ? Indent(contents) : contents;
Expand Down