Skip to content

Commit

Permalink
Ditching some ugly thread statics (#670)
Browse files Browse the repository at this point in the history
* Ditching some ugly thread statics

* Switch to record
  • Loading branch information
belav authored May 23, 2022
1 parent 15061de commit 36c8b86
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 26 deletions.
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 { }
public record 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

0 comments on commit 36c8b86

Please sign in to comment.