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

Ditching some ugly thread statics #670

Merged
merged 3 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 3 additions & 6 deletions Src/CSharpier.Generators/NodePrinterGenerator.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ namespace CSharpier.SyntaxPrinter
{
internal static class Node
{
[ThreadStatic]
private static int depth;

public static Doc Print(SyntaxNode syntaxNode, FormattingContext context)
{
if (syntaxNode == null)
{
return Doc.Null;
}

if (depth > 200)
if (context.PrintingDepth > 200)
{
throw new InTooDeepException();
}
Expand All @@ -29,7 +26,7 @@ namespace CSharpier.SyntaxPrinter
return CSharpierIgnore.PrintWithoutFormatting(syntaxNode);
}

depth++;
context.PrintingDepth++;
try
{
switch (syntaxNode)
Expand All @@ -44,7 +41,7 @@ namespace CSharpier.SyntaxPrinter
}
finally
{
depth--;
context.PrintingDepth--;
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion Src/CSharpier/SyntaxPrinter/FormattingContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
namespace CSharpier.SyntaxPrinter;

public class FormattingContext { }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: you should consider making this a record, since it's probably never going to have methods on it.

public class FormattingContext
{
public int PrintingDepth { get; set; }
public bool NextTriviaNeedsLine { get; set; }
public bool ShouldSkipNextLeadingTrivia { get; set; }
}
2 changes: 1 addition & 1 deletion Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ or SyntaxKind.EndRegionDirectiveTrivia
)
)
{
Token.NextTriviaNeedsLine = true;
context.NextTriviaNeedsLine = true;
}

result.Add(Doc.HardLine, Node.Print(member, context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ public static Doc Print(CSharpSyntaxNode node, FormattingContext context)
{
if (modifiers is not { Count: > 0 })
{
Token.ShouldSkipNextLeadingTrivia = true;
context.ShouldSkipNextLeadingTrivia = true;
}

declarationGroup.Add(Node.Print(returnType, context), " ");
Token.ShouldSkipNextLeadingTrivia = false;
context.ShouldSkipNextLeadingTrivia = false;
}

if (explicitInterfaceSpecifier != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ node.Parent is ConditionalExpressionSyntax conditionalExpressionSyntax
: Doc.Group(docs[0], Doc.Indent(docs.Skip(1).ToList()));
}

[ThreadStatic]
private static int depth;

// The goal of this is to group operators of the same precedence such that they all break or none of them break
// for example the following should break on the && before it breaks on the !=
/* (
Expand All @@ -52,12 +49,12 @@ private static List<Doc> PrintBinaryExpression(SyntaxNode node, FormattingContex
return new List<Doc> { Doc.Group(Node.Print(node, context)) };
}

if (depth > 200)
if (context.PrintingDepth > 200)
{
throw new InTooDeepException();
}

depth++;
context.PrintingDepth++;
try
{
var docs = new List<Doc>();
Expand Down Expand Up @@ -126,7 +123,7 @@ possibleBinary is BinaryExpressionSyntax childBinary
}
finally
{
depth--;
context.PrintingDepth--;
}
}

Expand Down
14 changes: 4 additions & 10 deletions Src/CSharpier/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ namespace CSharpier.SyntaxPrinter;

internal static class Token
{
[ThreadStatic]
public static bool ShouldSkipNextLeadingTrivia;

[ThreadStatic]
public static bool NextTriviaNeedsLine;

public static Doc PrintWithoutLeadingTrivia(SyntaxToken syntaxToken, FormattingContext context)
{
return PrintSyntaxToken(syntaxToken, context, skipLeadingTrivia: true);
Expand Down Expand Up @@ -40,7 +34,7 @@ private static Doc PrintSyntaxToken(
}

var docs = new List<Doc>();
if (!skipLeadingTrivia && !ShouldSkipNextLeadingTrivia)
if (!skipLeadingTrivia && !context.ShouldSkipNextLeadingTrivia)
{
var leadingTrivia = PrintLeadingTrivia(syntaxToken, context);
if (leadingTrivia != Doc.Null)
Expand All @@ -49,7 +43,7 @@ private static Doc PrintSyntaxToken(
}
}

ShouldSkipNextLeadingTrivia = false;
context.ShouldSkipNextLeadingTrivia = false;

if (
(
Expand Down Expand Up @@ -235,7 +229,7 @@ void AddLeadingComment(CommentType commentType)
docs.RemoveAt(docs.Count - 1);
}

if (NextTriviaNeedsLine)
if (context.NextTriviaNeedsLine)
{
if (leadingTrivia.Any(o => o.RawSyntaxKind() is SyntaxKind.IfDirectiveTrivia))
{
Expand All @@ -261,7 +255,7 @@ void AddLeadingComment(CommentType commentType)
docs.Insert(index + 1, Doc.HardLineSkipBreakIfFirstInGroup);
}
}
NextTriviaNeedsLine = false;
context.NextTriviaNeedsLine = false;
}

return docs.Count > 0 ? Doc.Concat(docs) : Doc.Null;
Expand Down