Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring so that the threadstatic fields can go away. #663

Merged
merged 8 commits into from
May 21, 2022
Merged
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
6 changes: 3 additions & 3 deletions Src/CSharpier.Generators/NodePrinterGenerator.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace CSharpier.SyntaxPrinter
[ThreadStatic]
private static int depth;

public static Doc Print(SyntaxNode syntaxNode)
public static Doc Print(SyntaxNode syntaxNode, FormattingContext context)
{
if (syntaxNode == null)
{
Expand All @@ -36,10 +36,10 @@ namespace CSharpier.SyntaxPrinter
{
{{- for nodeType in NodeTypes }}
case {{ nodeType.SyntaxNodeName }} {{ nodeType.VariableName }}:
return {{ nodeType.PrinterName }}.Print({{ nodeType.VariableName }});
return {{ nodeType.PrinterName }}.Print({{ nodeType.VariableName }}, context);
{{- end }}
default:
return UnhandledNode.Print(syntaxNode);
return UnhandledNode.Print(syntaxNode, context);
}
}
finally
Expand Down
7 changes: 5 additions & 2 deletions Src/CSharpier/CodeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ bool TryGetCompilationFailure(out CodeFormatterResult compilationResult)
PreprocessorSymbols.Reset();
}

var document = Node.Print(rootNode);
var document = Node.Print(rootNode, new FormattingContext());
var lineEnding = GetLineEnding(syntaxTree.ToString(), printerOptions);
var formattedCode = DocPrinter.DocPrinter.Print(document, printerOptions, lineEnding);

Expand All @@ -148,7 +148,10 @@ bool TryGetCompilationFailure(out CodeFormatterResult compilationResult)
return result;
}

document = Node.Print(await syntaxTree.GetRootAsync(cancellationToken));
document = Node.Print(
await syntaxTree.GetRootAsync(cancellationToken),
new FormattingContext()
);
formattedCode = DocPrinter.DocPrinter.Print(document, printerOptions, lineEnding);
}

Expand Down
9 changes: 5 additions & 4 deletions Src/CSharpier/SyntaxPrinter/ArgumentListLikeSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ internal static class ArgumentListLike
public static Doc Print(
SyntaxToken openParenToken,
SeparatedSyntaxList<ArgumentSyntax> arguments,
SyntaxToken closeParenToken
SyntaxToken closeParenToken,
FormattingContext context
)
{
var docs = new List<Doc> { Token.Print(openParenToken) };
var docs = new List<Doc> { Token.Print(openParenToken, context) };

if (arguments.Any())
{
docs.Add(
Doc.Indent(
Doc.SoftLine,
SeparatedSyntaxList.Print(arguments, Argument.Print, Doc.Line)
SeparatedSyntaxList.Print(arguments, Argument.Print, Doc.Line, context)
),
Doc.SoftLine
);
}

docs.Add(Token.Print(closeParenToken));
docs.Add(Token.Print(closeParenToken, context));

return Doc.Concat(docs);
}
Expand Down
8 changes: 6 additions & 2 deletions Src/CSharpier/SyntaxPrinter/AttributeLists.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ namespace CSharpier.SyntaxPrinter;

internal static class AttributeLists
{
public static Doc Print(SyntaxNode node, SyntaxList<AttributeListSyntax> attributeLists)
public static Doc Print(
SyntaxNode node,
SyntaxList<AttributeListSyntax> attributeLists,
FormattingContext context
)
{
if (attributeLists.Count == 0)
{
Expand All @@ -16,7 +20,7 @@ or ParameterSyntax
or ParenthesizedLambdaExpressionSyntax
? Doc.Line
: Doc.HardLine;
docs.Add(Doc.Join(separator, attributeLists.Select(AttributeList.Print)));
docs.Add(Doc.Join(separator, attributeLists.Select(o => AttributeList.Print(o, context))));

if (node is not (ParameterSyntax or TypeParameterSyntax))
{
Expand Down
7 changes: 5 additions & 2 deletions Src/CSharpier/SyntaxPrinter/ConstraintClauses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ namespace CSharpier.SyntaxPrinter;

internal static class ConstraintClauses
{
public static Doc Print(IEnumerable<TypeParameterConstraintClauseSyntax> constraintClauses)
public static Doc Print(
IEnumerable<TypeParameterConstraintClauseSyntax> constraintClauses,
FormattingContext context
)
{
var constraintClausesList = constraintClauses.ToList();

Expand All @@ -13,7 +16,7 @@ public static Doc Print(IEnumerable<TypeParameterConstraintClauseSyntax> constra
var prefix = constraintClausesList.Count >= 2 ? Doc.HardLine : Doc.Line;
var body = Doc.Join(
Doc.HardLine,
constraintClausesList.Select(TypeParameterConstraintClause.Print)
constraintClausesList.Select(o => TypeParameterConstraintClause.Print(o, context))
);

return Doc.Group(Doc.Indent(prefix, body));
Expand Down
3 changes: 3 additions & 0 deletions Src/CSharpier/SyntaxPrinter/FormattingContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace CSharpier.SyntaxPrinter;

public class FormattingContext { }
13 changes: 8 additions & 5 deletions Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ namespace CSharpier.SyntaxPrinter;

internal static class MembersWithForcedLines
{
public static List<Doc> Print<T>(CSharpSyntaxNode node, IReadOnlyList<T> members)
where T : MemberDeclarationSyntax
public static List<Doc> Print<T>(
CSharpSyntaxNode node,
IReadOnlyList<T> members,
FormattingContext context
) where T : MemberDeclarationSyntax
{
var result = new List<Doc> { Doc.HardLine };
var lastMemberForcedBlankLine = false;
Expand All @@ -15,7 +18,7 @@ void AddSeparatorIfNeeded()
{
if (members is SeparatedSyntaxList<T> list && x < list.SeparatorCount)
{
result.Add(Token.Print(list.GetSeparator(x)));
result.Add(Token.Print(list.GetSeparator(x), context));
}
}

Expand Down Expand Up @@ -54,7 +57,7 @@ member is MethodDeclarationSyntax methodDeclaration
if (x == 0)
{
lastMemberForcedBlankLine = blankLineIsForced;
result.Add(Node.Print(member));
result.Add(Node.Print(member, context));
AddSeparatorIfNeeded();
continue;
}
Expand Down Expand Up @@ -140,7 +143,7 @@ or SyntaxKind.EndRegionDirectiveTrivia
Token.NextTriviaNeedsLine = true;
}

result.Add(Doc.HardLine, Node.Print(member));
result.Add(Doc.HardLine, Node.Print(member, context));
AddSeparatorIfNeeded();

lastMemberForcedBlankLine = blankLineIsForced;
Expand Down
15 changes: 10 additions & 5 deletions Src/CSharpier/SyntaxPrinter/Modifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@ namespace CSharpier.SyntaxPrinter;

internal static class Modifiers
{
public static Doc Print(SyntaxTokenList modifiers)
public static Doc Print(SyntaxTokenList modifiers, FormattingContext context)
{
if (modifiers.Count == 0)
{
return Doc.Null;
}

return Doc.Group(Doc.Join(" ", modifiers.Select(Token.Print)), " ");
return Doc.Group(Doc.Join(" ", modifiers.Select(o => Token.Print(o, context))), " ");
}

public static Doc PrintWithoutLeadingTrivia(SyntaxTokenList modifiers)
public static Doc PrintWithoutLeadingTrivia(
SyntaxTokenList modifiers,
FormattingContext context
)
{
if (modifiers.Count == 0)
{
return Doc.Null;
}

return Doc.Group(
Token.PrintWithoutLeadingTrivia(modifiers[0]),
Token.PrintWithoutLeadingTrivia(modifiers[0], context),
" ",
modifiers.Count > 1
? Doc.Concat(modifiers.Skip(1).Select(o => Token.PrintWithSuffix(o, " ")).ToArray())
? Doc.Concat(
modifiers.Skip(1).Select(o => Token.PrintWithSuffix(o, " ", context)).ToArray()
)
: Doc.Null
);
}
Expand Down
25 changes: 16 additions & 9 deletions Src/CSharpier/SyntaxPrinter/NamespaceLikePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,34 @@ namespace CSharpier.SyntaxPrinter;

internal static class NamespaceLikePrinter
{
public static void Print(BaseNamespaceDeclarationSyntax node, List<Doc> docs)
public static void Print(
BaseNamespaceDeclarationSyntax node,
List<Doc> docs,
FormattingContext context
)
{
Print(node, node.Externs, node.Usings, node.Members, docs);
Print(node, node.Externs, node.Usings, node.Members, docs, context);
}

public static void Print(CompilationUnitSyntax node, List<Doc> docs)
public static void Print(CompilationUnitSyntax node, List<Doc> docs, FormattingContext context)
{
Print(node, node.Externs, node.Usings, node.Members, docs);
Print(node, node.Externs, node.Usings, node.Members, docs, context);
}

private static void Print(
CSharpSyntaxNode node,
SyntaxList<ExternAliasDirectiveSyntax> externs,
SyntaxList<UsingDirectiveSyntax> usings,
SyntaxList<MemberDeclarationSyntax> members,
List<Doc> docs
List<Doc> docs,
FormattingContext context
)
{
if (externs.Count > 0)
{
docs.Add(Doc.Join(Doc.HardLine, externs.Select(ExternAliasDirective.Print)));
docs.Add(
Doc.Join(Doc.HardLine, externs.Select(o => ExternAliasDirective.Print(o, context)))
);
}

if (usings.Count > 0)
Expand All @@ -31,7 +38,7 @@ List<Doc> docs
{
docs.Add(Doc.HardLine);
}
docs.Add(Doc.Join(Doc.HardLine, usings.Select(UsingDirective.Print)));
docs.Add(Doc.Join(Doc.HardLine, usings.Select(o => UsingDirective.Print(o, context))));
}

if (
Expand All @@ -56,7 +63,7 @@ node is CompilationUnitSyntax compilationUnitSyntax
}
docs.Add(
Doc.HardLine,
AttributeLists.Print(node, compilationUnitSyntax.AttributeLists)
AttributeLists.Print(node, compilationUnitSyntax.AttributeLists, context)
);
}

Expand Down Expand Up @@ -92,6 +99,6 @@ node is CompilationUnitSyntax compilationUnitSyntax
}
}

docs.AddRange(MembersWithForcedLines.Print(node, members));
docs.AddRange(MembersWithForcedLines.Print(node, members, context));
}
}
6 changes: 3 additions & 3 deletions Src/CSharpier/SyntaxPrinter/OptionalBraces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ namespace CSharpier.SyntaxPrinter;

internal static class OptionalBraces
{
public static Doc Print(StatementSyntax node)
public static Doc Print(StatementSyntax node, FormattingContext context)
{
return node is BlockSyntax blockSyntax
? Block.Print(blockSyntax)
: Doc.Indent(Doc.HardLine, Node.Print(node));
? Block.Print(blockSyntax, context)
: Doc.Indent(Doc.HardLine, Node.Print(node, context));
}
}
18 changes: 12 additions & 6 deletions Src/CSharpier/SyntaxPrinter/RightHandSide.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ public static Doc Print(
CSharpSyntaxNode leftNode,
Doc leftDoc,
Doc operatorDoc,
ExpressionSyntax rightNode
ExpressionSyntax rightNode,
FormattingContext context
)
{
var layout = DetermineLayout(leftNode, rightNode);
Expand All @@ -16,27 +17,32 @@ ExpressionSyntax rightNode
return layout switch
{
Layout.BasicConcatWithoutLine
=> Doc.Concat(leftDoc, operatorDoc, Node.Print(rightNode)),
=> Doc.Concat(leftDoc, operatorDoc, Node.Print(rightNode, context)),
Layout.BreakAfterOperator
=> Doc.Group(
Doc.Group(leftDoc),
operatorDoc,
Doc.Group(Doc.Indent(Doc.Line, Node.Print(rightNode)))
Doc.Group(Doc.Indent(Doc.Line, Node.Print(rightNode, context)))
),
Layout.Chain
=> Doc.Concat(Doc.Group(leftDoc), operatorDoc, Doc.Line, Node.Print(rightNode)),
=> Doc.Concat(
Doc.Group(leftDoc),
operatorDoc,
Doc.Line,
Node.Print(rightNode, context)
),
Layout.ChainTail
=> Doc.Concat(
Doc.Group(leftDoc),
operatorDoc,
Doc.Indent(Doc.Line, Node.Print(rightNode))
Doc.Indent(Doc.Line, Node.Print(rightNode, context))
),
Layout.Fluid
=> Doc.Group(
Doc.Group(leftDoc),
operatorDoc,
Doc.GroupWithId(groupId, Doc.Indent(Doc.Line)),
Doc.IndentIfBreak(Node.Print(rightNode), groupId)
Doc.IndentIfBreak(Node.Print(rightNode, context), groupId)
),
_ => throw new Exception("The layout type of " + layout + " was not handled.")
};
Expand Down
7 changes: 4 additions & 3 deletions Src/CSharpier/SyntaxPrinter/SeparatedSyntaxList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ internal static class SeparatedSyntaxList
{
public static Doc Print<T>(
SeparatedSyntaxList<T> list,
Func<T, Doc> printFunc,
Func<T, FormattingContext, Doc> printFunc,
Doc afterSeparator,
FormattingContext context,
int startingIndex = 0
) where T : SyntaxNode
{
var docs = new List<Doc>();
for (var x = startingIndex; x < list.Count; x++)
{
docs.Add(printFunc(list[x]));
docs.Add(printFunc(list[x], context));

if (x >= list.SeparatorCount)
{
Expand All @@ -21,7 +22,7 @@ public static Doc Print<T>(

var isTrailingSeparator = x == list.Count - 1;

docs.Add(Token.Print(list.GetSeparator(x)));
docs.Add(Token.Print(list.GetSeparator(x), context));
if (!isTrailingSeparator)
{
docs.Add(afterSeparator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ namespace CSharpier.SyntaxPrinter.SyntaxNodePrinters;

internal static class AliasQualifiedName
{
public static Doc Print(AliasQualifiedNameSyntax node)
public static Doc Print(AliasQualifiedNameSyntax node, FormattingContext context)
{
return Doc.Concat(
Node.Print(node.Alias),
Token.Print(node.ColonColonToken),
Node.Print(node.Name)
Node.Print(node.Alias, context),
Token.Print(node.ColonColonToken, context),
Node.Print(node.Name, context)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ namespace CSharpier.SyntaxPrinter.SyntaxNodePrinters;

internal static class AnonymousMethodExpression
{
public static Doc Print(AnonymousMethodExpressionSyntax node)
public static Doc Print(AnonymousMethodExpressionSyntax node, FormattingContext context)
{
var docs = new List<Doc>
{
Modifiers.Print(node.Modifiers),
Token.Print(node.DelegateKeyword)
Modifiers.Print(node.Modifiers, context),
Token.Print(node.DelegateKeyword, context)
};

if (node.ParameterList != null)
{
docs.Add(ParameterList.Print(node.ParameterList));
docs.Add(ParameterList.Print(node.ParameterList, context));
}

docs.Add(Block.Print(node.Block));
docs.Add(Block.Print(node.Block, context));

return Doc.Concat(docs);
}
Expand Down
Loading