From 36c8b86cd0f5ff7a6effe22d5b1652982035fb8e Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 23 May 2022 10:29:32 -0500 Subject: [PATCH] Ditching some ugly thread statics (#670) * Ditching some ugly thread statics * Switch to record --- .../NodePrinterGenerator.sbntxt | 9 +++------ Src/CSharpier/SyntaxPrinter/FormattingContext.cs | 7 ++++++- .../SyntaxPrinter/MembersWithForcedLines.cs | 2 +- .../SyntaxNodePrinters/BaseMethodDeclaration.cs | 4 ++-- .../SyntaxNodePrinters/BinaryExpression.cs | 9 +++------ Src/CSharpier/SyntaxPrinter/Token.cs | 14 ++++---------- 6 files changed, 19 insertions(+), 26 deletions(-) diff --git a/Src/CSharpier.Generators/NodePrinterGenerator.sbntxt b/Src/CSharpier.Generators/NodePrinterGenerator.sbntxt index 7e4a72a23..ba74c5596 100644 --- a/Src/CSharpier.Generators/NodePrinterGenerator.sbntxt +++ b/Src/CSharpier.Generators/NodePrinterGenerator.sbntxt @@ -9,9 +9,6 @@ namespace CSharpier.SyntaxPrinter { internal static class Node { - [ThreadStatic] - private static int depth; - public static Doc Print(SyntaxNode syntaxNode, FormattingContext context) { if (syntaxNode == null) @@ -19,7 +16,7 @@ namespace CSharpier.SyntaxPrinter return Doc.Null; } - if (depth > 200) + if (context.PrintingDepth > 200) { throw new InTooDeepException(); } @@ -29,7 +26,7 @@ namespace CSharpier.SyntaxPrinter return CSharpierIgnore.PrintWithoutFormatting(syntaxNode); } - depth++; + context.PrintingDepth++; try { switch (syntaxNode) @@ -44,7 +41,7 @@ namespace CSharpier.SyntaxPrinter } finally { - depth--; + context.PrintingDepth--; } } } diff --git a/Src/CSharpier/SyntaxPrinter/FormattingContext.cs b/Src/CSharpier/SyntaxPrinter/FormattingContext.cs index 20a3ac0e2..fdb307271 100644 --- a/Src/CSharpier/SyntaxPrinter/FormattingContext.cs +++ b/Src/CSharpier/SyntaxPrinter/FormattingContext.cs @@ -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; } +} diff --git a/Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs b/Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs index c88ff893a..baca51eaa 100644 --- a/Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs +++ b/Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs @@ -140,7 +140,7 @@ or SyntaxKind.EndRegionDirectiveTrivia ) ) { - Token.NextTriviaNeedsLine = true; + context.NextTriviaNeedsLine = true; } result.Add(Doc.HardLine, Node.Print(member, context)); diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseMethodDeclaration.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseMethodDeclaration.cs index de0de21c0..dbe7c3b49 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseMethodDeclaration.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BaseMethodDeclaration.cs @@ -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) diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BinaryExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BinaryExpression.cs index 78981f367..a32b24411 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BinaryExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/BinaryExpression.cs @@ -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 != /* ( @@ -52,12 +49,12 @@ private static List PrintBinaryExpression(SyntaxNode node, FormattingContex return new List { Doc.Group(Node.Print(node, context)) }; } - if (depth > 200) + if (context.PrintingDepth > 200) { throw new InTooDeepException(); } - depth++; + context.PrintingDepth++; try { var docs = new List(); @@ -126,7 +123,7 @@ possibleBinary is BinaryExpressionSyntax childBinary } finally { - depth--; + context.PrintingDepth--; } } diff --git a/Src/CSharpier/SyntaxPrinter/Token.cs b/Src/CSharpier/SyntaxPrinter/Token.cs index 4aba40355..a2f44727c 100644 --- a/Src/CSharpier/SyntaxPrinter/Token.cs +++ b/Src/CSharpier/SyntaxPrinter/Token.cs @@ -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); @@ -40,7 +34,7 @@ private static Doc PrintSyntaxToken( } var docs = new List(); - if (!skipLeadingTrivia && !ShouldSkipNextLeadingTrivia) + if (!skipLeadingTrivia && !context.ShouldSkipNextLeadingTrivia) { var leadingTrivia = PrintLeadingTrivia(syntaxToken, context); if (leadingTrivia != Doc.Null) @@ -49,7 +43,7 @@ private static Doc PrintSyntaxToken( } } - ShouldSkipNextLeadingTrivia = false; + context.ShouldSkipNextLeadingTrivia = false; if ( ( @@ -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)) { @@ -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;