From 7eaf502ea9986e0da691860377665d377cadc489 Mon Sep 17 00:00:00 2001 From: Guillermo Ruffino Date: Wed, 19 Oct 2022 19:24:49 -0300 Subject: [PATCH 01/10] order modifiers --- .../FormattingTests/TestFiles/cs/AttributeLists.test | 2 +- .../TestFiles/cs/FunctionPointerTypes.test | 2 +- .../TestFiles/cs/InterfaceDeclarations.test | 2 +- .../FormattingTests/TestFiles/cs/TupleExpressions.test | 6 +++--- Src/CSharpier/SyntaxPrinter/Modifiers.cs | 8 +++++--- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AttributeLists.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AttributeLists.test index 815492798..e87e39a94 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AttributeLists.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AttributeLists.test @@ -29,7 +29,7 @@ class ClassName private volatile int f2; [DllImport("kernel32", SetLastError = true)] - static extern bool NameEquals(string name, SecurityAttribute sa); + extern static bool NameEquals(string name, SecurityAttribute sa); [Obsolete("Error", error: true)] void NameColon() { } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FunctionPointerTypes.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FunctionPointerTypes.test index 83ac44eda..39efdad0a 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FunctionPointerTypes.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FunctionPointerTypes.test @@ -19,7 +19,7 @@ class ClassName return; } - public static extern unsafe int InvokeCallbackFuncPtr_Inline_NoGCTransition( + extern public static unsafe int InvokeCallbackFuncPtr_Inline_NoGCTransition( delegate* unmanaged[Cdecl] cb, int* n ); diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InterfaceDeclarations.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InterfaceDeclarations.test index cabfbf13c..f3ca7b63d 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InterfaceDeclarations.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InterfaceDeclarations.test @@ -8,7 +8,7 @@ public interface BasicInterface : BaseInterface { } public interface BasicInterface { void SomeMethod(); - static abstract void SomeOtherMethod(); + abstract static void SomeOtherMethod(); } interface CoContra { } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TupleExpressions.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TupleExpressions.test index b6f1318b7..5da874046 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TupleExpressions.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TupleExpressions.test @@ -11,7 +11,7 @@ class ClassName return new() { (href: "one", title: "two") }; } - public async Task TupleAssignment() + async public Task TupleAssignment() { var (noteTexts, matcher) = await ( GetAndAnalyzeNoteTexts(), @@ -21,7 +21,7 @@ class ClassName var (a, b) = await (c(), d()); } - public async Task<( + async public Task<( ILookup someLookup, ILookup reverseLookup, ILookup thirdLookup @@ -30,7 +30,7 @@ class ClassName // do something } - public async ( + async public ( ILookup someLookup, ILookup reverseLookup, ILookup thirdLookup diff --git a/Src/CSharpier/SyntaxPrinter/Modifiers.cs b/Src/CSharpier/SyntaxPrinter/Modifiers.cs index 07e71af64..4d0c66fb7 100644 --- a/Src/CSharpier/SyntaxPrinter/Modifiers.cs +++ b/Src/CSharpier/SyntaxPrinter/Modifiers.cs @@ -22,12 +22,14 @@ FormattingContext context return Doc.Null; } + var sortedModifiers = modifiers.OrderBy(o => o.Text); + return Doc.Group( - Token.PrintWithoutLeadingTrivia(modifiers[0], context), + Token.PrintWithoutLeadingTrivia(sortedModifiers.First(), context), " ", - modifiers.Count > 1 + sortedModifiers.Count() > 1 ? Doc.Concat( - modifiers.Skip(1).Select(o => Token.PrintWithSuffix(o, " ", context)).ToArray() + sortedModifiers.Skip(1).Select(o => Token.PrintWithSuffix(o, " ", context)).ToArray() ) : Doc.Null ); From 91f701d70f057b763054ecc7b79a17f4f9dabd2a Mon Sep 17 00:00:00 2001 From: Guillermo Ruffino Date: Fri, 21 Oct 2022 20:06:42 -0300 Subject: [PATCH 02/10] use arbitrary order --- .../TestFiles/cs/AttributeLists.test | 2 +- .../TestFiles/cs/FunctionPointerTypes.test | 2 +- .../TestFiles/cs/InterfaceDeclarations.test | 2 +- .../TestFiles/cs/TupleExpressions.test | 6 +++--- Src/CSharpier/SyntaxPrinter/Modifiers.cs | 15 ++++++++++++++- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AttributeLists.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AttributeLists.test index e87e39a94..815492798 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AttributeLists.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AttributeLists.test @@ -29,7 +29,7 @@ class ClassName private volatile int f2; [DllImport("kernel32", SetLastError = true)] - extern static bool NameEquals(string name, SecurityAttribute sa); + static extern bool NameEquals(string name, SecurityAttribute sa); [Obsolete("Error", error: true)] void NameColon() { } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FunctionPointerTypes.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FunctionPointerTypes.test index 39efdad0a..83ac44eda 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FunctionPointerTypes.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FunctionPointerTypes.test @@ -19,7 +19,7 @@ class ClassName return; } - extern public static unsafe int InvokeCallbackFuncPtr_Inline_NoGCTransition( + public static extern unsafe int InvokeCallbackFuncPtr_Inline_NoGCTransition( delegate* unmanaged[Cdecl] cb, int* n ); diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InterfaceDeclarations.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InterfaceDeclarations.test index f3ca7b63d..cabfbf13c 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InterfaceDeclarations.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InterfaceDeclarations.test @@ -8,7 +8,7 @@ public interface BasicInterface : BaseInterface { } public interface BasicInterface { void SomeMethod(); - abstract static void SomeOtherMethod(); + static abstract void SomeOtherMethod(); } interface CoContra { } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TupleExpressions.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TupleExpressions.test index 5da874046..b6f1318b7 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TupleExpressions.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/TupleExpressions.test @@ -11,7 +11,7 @@ class ClassName return new() { (href: "one", title: "two") }; } - async public Task TupleAssignment() + public async Task TupleAssignment() { var (noteTexts, matcher) = await ( GetAndAnalyzeNoteTexts(), @@ -21,7 +21,7 @@ class ClassName var (a, b) = await (c(), d()); } - async public Task<( + public async Task<( ILookup someLookup, ILookup reverseLookup, ILookup thirdLookup @@ -30,7 +30,7 @@ class ClassName // do something } - async public ( + public async ( ILookup someLookup, ILookup reverseLookup, ILookup thirdLookup diff --git a/Src/CSharpier/SyntaxPrinter/Modifiers.cs b/Src/CSharpier/SyntaxPrinter/Modifiers.cs index 4d0c66fb7..8e7995249 100644 --- a/Src/CSharpier/SyntaxPrinter/Modifiers.cs +++ b/Src/CSharpier/SyntaxPrinter/Modifiers.cs @@ -12,6 +12,19 @@ public static Doc Print(SyntaxTokenList modifiers, FormattingContext context) return Doc.Group(Doc.Join(" ", modifiers.Select(o => Token.Print(o, context))), " "); } + + + class DefaultOrder : IComparer + { + static readonly string[] DefaultOrdered = new string[] { "public", "private", "protected", "internal", "file", "static", "extern", "new", "virtual", "abstract", "sealed", "override", "readonly", "unsafe", "required", "volatile", "async" }; + public int Compare(string? x, string? y) + { + return Array.IndexOf(DefaultOrdered, x) - Array.IndexOf(DefaultOrdered, y); + } + } + + private static readonly DefaultOrder Comparer = new(); + public static Doc PrintWithoutLeadingTrivia( SyntaxTokenList modifiers, FormattingContext context @@ -22,7 +35,7 @@ FormattingContext context return Doc.Null; } - var sortedModifiers = modifiers.OrderBy(o => o.Text); + var sortedModifiers = modifiers.OrderBy(o => o.Text, Comparer); return Doc.Group( Token.PrintWithoutLeadingTrivia(sortedModifiers.First(), context), From 237d205c52f64d02d2abfb6f83674a37ea94edd7 Mon Sep 17 00:00:00 2001 From: Guillermo Ruffino Date: Fri, 21 Oct 2022 20:15:24 -0300 Subject: [PATCH 03/10] format --- Src/CSharpier/SyntaxPrinter/Modifiers.cs | 29 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/Src/CSharpier/SyntaxPrinter/Modifiers.cs b/Src/CSharpier/SyntaxPrinter/Modifiers.cs index 8e7995249..cf49374ff 100644 --- a/Src/CSharpier/SyntaxPrinter/Modifiers.cs +++ b/Src/CSharpier/SyntaxPrinter/Modifiers.cs @@ -12,11 +12,29 @@ public static Doc Print(SyntaxTokenList modifiers, FormattingContext context) return Doc.Group(Doc.Join(" ", modifiers.Select(o => Token.Print(o, context))), " "); } - - class DefaultOrder : IComparer { - static readonly string[] DefaultOrdered = new string[] { "public", "private", "protected", "internal", "file", "static", "extern", "new", "virtual", "abstract", "sealed", "override", "readonly", "unsafe", "required", "volatile", "async" }; + static readonly string[] DefaultOrdered = new string[] + { + "public", + "private", + "protected", + "internal", + "file", + "static", + "extern", + "new", + "virtual", + "abstract", + "sealed", + "override", + "readonly", + "unsafe", + "required", + "volatile", + "async" + }; + public int Compare(string? x, string? y) { return Array.IndexOf(DefaultOrdered, x) - Array.IndexOf(DefaultOrdered, y); @@ -42,7 +60,10 @@ FormattingContext context " ", sortedModifiers.Count() > 1 ? Doc.Concat( - sortedModifiers.Skip(1).Select(o => Token.PrintWithSuffix(o, " ", context)).ToArray() + sortedModifiers + .Skip(1) + .Select(o => Token.PrintWithSuffix(o, " ", context)) + .ToArray() ) : Doc.Null ); From 29b99df537705863c171bf0d0f586b57aacec61c Mon Sep 17 00:00:00 2001 From: Guillermo Ruffino Date: Fri, 21 Oct 2022 20:37:01 -0300 Subject: [PATCH 04/10] do all modifiers --- .../cs/AnonymousMethodExpressions.test | 2 +- .../TestFiles/cs/FieldDeclarations.test | 2 +- .../cs/ParenthesizedLambdaExpressions.test | 2 +- .../TestFiles/cs/PropertyDeclarations.test | 2 +- Src/CSharpier/SyntaxPrinter/Modifiers.cs | 26 ++++++++++++------- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AnonymousMethodExpressions.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AnonymousMethodExpressions.test index 86e2f7a4d..2362d2df9 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AnonymousMethodExpressions.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/AnonymousMethodExpressions.test @@ -17,7 +17,7 @@ class ClassName return await !a; }; - Func f4 = async static delegate(bool a) + Func f4 = static async delegate(bool a) { return await !a; }; diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FieldDeclarations.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FieldDeclarations.test index b1dd0df5d..1903d86a1 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FieldDeclarations.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FieldDeclarations.test @@ -1,6 +1,6 @@ public class ClassName { - private static const string SomeName = "1"; + const private static string SomeName = "1"; public SomeClass SomeName; private string SomeFieldWithSadsfasdfasdfasdfuperLongNameYeahMan = diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ParenthesizedLambdaExpressions.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ParenthesizedLambdaExpressions.test index a69ea08d7..0713d6c54 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ParenthesizedLambdaExpressions.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/ParenthesizedLambdaExpressions.test @@ -14,7 +14,7 @@ public class ClassName this.Where(async () => true); this.Where([Attribute] async () => true); this.Where(static () => true); - this.Where(async static () => true); + this.Where(static async () => true); this.SomeMethod( (longParameter__________________, longParameter_________________) => longParameter________________ diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/PropertyDeclarations.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/PropertyDeclarations.test index 98d7e5ce5..4dd25a8ab 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/PropertyDeclarations.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/PropertyDeclarations.test @@ -8,7 +8,7 @@ public class ClassName string PropertyWithModifiersThatIsLongEnoughToBreak { protected internal get; - internal protected set; + protected internal set; } private string backingValue; diff --git a/Src/CSharpier/SyntaxPrinter/Modifiers.cs b/Src/CSharpier/SyntaxPrinter/Modifiers.cs index cf49374ff..a27684c90 100644 --- a/Src/CSharpier/SyntaxPrinter/Modifiers.cs +++ b/Src/CSharpier/SyntaxPrinter/Modifiers.cs @@ -2,16 +2,6 @@ namespace CSharpier.SyntaxPrinter; internal static class Modifiers { - public static Doc Print(SyntaxTokenList modifiers, FormattingContext context) - { - if (modifiers.Count == 0) - { - return Doc.Null; - } - - return Doc.Group(Doc.Join(" ", modifiers.Select(o => Token.Print(o, context))), " "); - } - class DefaultOrder : IComparer { static readonly string[] DefaultOrdered = new string[] @@ -43,6 +33,22 @@ public int Compare(string? x, string? y) private static readonly DefaultOrder Comparer = new(); + public static Doc Print(SyntaxTokenList modifiers, FormattingContext context) + { + if (modifiers.Count == 0) + { + return Doc.Null; + } + + return Doc.Group( + Doc.Join( + " ", + modifiers.OrderBy(o => o.Text, Comparer).Select(o => Token.Print(o, context)) + ), + " " + ); + } + public static Doc PrintWithoutLeadingTrivia( SyntaxTokenList modifiers, FormattingContext context From 8b3b9aedf617ecb59234c9ebc33959e1e6713fdc Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 5 Jun 2023 11:37:44 -0500 Subject: [PATCH 05/10] Fixing syntax node validation. Fix issue with const needing to be last --- .../SyntaxNodeComparerGenerator.cs | 7 +++ .../TestFiles/cs/FieldDeclarations.test | 2 +- .../TestFiles/cs/Modifiers_Sort.expected.test | 4 ++ .../TestFiles/cs/Modifiers_Sort.test | 4 ++ .../SyntaxNodeComparerTests.cs | 12 ++++ Src/CSharpier/SyntaxNodeComparer.cs | 32 ++++++++++ Src/CSharpier/SyntaxNodeComparer.generated.cs | 58 +++++++++---------- Src/CSharpier/SyntaxPrinter/Modifiers.cs | 9 ++- 8 files changed, 95 insertions(+), 33 deletions(-) create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.expected.test create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.test diff --git a/Src/CSharpier.FakeGenerators/SyntaxNodeComparerGenerator.cs b/Src/CSharpier.FakeGenerators/SyntaxNodeComparerGenerator.cs index 22b5e9aad..79cad1212 100644 --- a/Src/CSharpier.FakeGenerators/SyntaxNodeComparerGenerator.cs +++ b/Src/CSharpier.FakeGenerators/SyntaxNodeComparerGenerator.cs @@ -166,6 +166,13 @@ private static void GenerateMethod(StringBuilder sourceBuilder, Type type) $" formattedStack.Push((formattedNode.{propertyName}, formattedNode));" ); } + else if (propertyType == typeof(SyntaxTokenList) && propertyName == "Modifiers") + { + sourceBuilder.AppendLine( + $" result = this.CompareModifiers(originalNode.{propertyName}, formattedNode.{propertyName}, originalNode.Span, formattedNode.Span);" + ); + sourceBuilder.AppendLine($" if (result.IsInvalid) return result;"); + } else if ( propertyType == typeof(SyntaxTokenList) || ( diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FieldDeclarations.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FieldDeclarations.test index 1903d86a1..b1dd0df5d 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FieldDeclarations.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/FieldDeclarations.test @@ -1,6 +1,6 @@ public class ClassName { - const private static string SomeName = "1"; + private static const string SomeName = "1"; public SomeClass SomeName; private string SomeFieldWithSadsfasdfasdfasdfuperLongNameYeahMan = diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.expected.test new file mode 100644 index 000000000..300e36ceb --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.expected.test @@ -0,0 +1,4 @@ +public class ClassName +{ + public static async MethodName() { } +} diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.test new file mode 100644 index 000000000..0c5f669ec --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.test @@ -0,0 +1,4 @@ +public class ClassName +{ + async static public MethodName() { } +} \ No newline at end of file diff --git a/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs b/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs index 8c5e11a7e..ede29a497 100644 --- a/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs +++ b/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs @@ -382,6 +382,18 @@ void MethodName() result.Should().BeEmpty(); } + [Test] + public void Sorted_Modifiers() + { + var left = @"static public class { }"; + + var right = @"public static class { }"; + + var result = AreEqual(left, right); + + result.Should().BeEmpty(); + } + private static void ResultShouldBe(string result, string be) { if (Environment.GetEnvironmentVariable("NormalizeLineEndings") != null) diff --git a/Src/CSharpier/SyntaxNodeComparer.cs b/Src/CSharpier/SyntaxNodeComparer.cs index 97167b03a..1ddd0ba8a 100644 --- a/Src/CSharpier/SyntaxNodeComparer.cs +++ b/Src/CSharpier/SyntaxNodeComparer.cs @@ -163,6 +163,38 @@ originalList[x] is SyntaxNode originalNode return Equal; } + private CompareResult CompareModifiers( + SyntaxTokenList originalList, + SyntaxTokenList formattedList, + TextSpan originalParentSpan, + TextSpan newParentSpan + ) + { + var orderedOriginalList = originalList.OrderBy(o => o.Text).ToList(); + var orderedFormattedList = formattedList.OrderBy(o => o.Text).ToList(); + + for (var x = 0; x < orderedOriginalList.Count || x < orderedFormattedList.Count; x++) + { + if (x == orderedOriginalList.Count) + { + return NotEqual(originalParentSpan, orderedFormattedList[x].Span); + } + + if (x == orderedFormattedList.Count) + { + return NotEqual(orderedOriginalList[x].Span, newParentSpan); + } + + var result = this.Compare(orderedOriginalList[x], orderedFormattedList[x]); + if (result.IsInvalid) + { + return result; + } + } + + return Equal; + } + private static CompareResult NotEqual(SyntaxNode? originalNode, SyntaxNode? formattedNode) { return new() diff --git a/Src/CSharpier/SyntaxNodeComparer.generated.cs b/Src/CSharpier/SyntaxNodeComparer.generated.cs index 15b383674..f39e84931 100644 --- a/Src/CSharpier/SyntaxNodeComparer.generated.cs +++ b/Src/CSharpier/SyntaxNodeComparer.generated.cs @@ -535,7 +535,7 @@ private CompareResult CompareAccessorDeclarationSyntax(AccessorDeclarationSyntax if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); result = this.Compare(originalNode.Keyword, formattedNode.Keyword, originalNode, formattedNode); if (result.IsInvalid) return result; - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.SemicolonToken, formattedNode.SemicolonToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -584,7 +584,7 @@ private CompareResult CompareAnonymousMethodExpressionSyntax(AnonymousMethodExpr originalStack.Push((originalNode.ExpressionBody, originalNode)); formattedStack.Push((formattedNode.ExpressionBody, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -1019,7 +1019,7 @@ private CompareResult CompareClassDeclarationSyntax(ClassDeclarationSyntax origi if (result.IsInvalid) return result; result = this.CompareLists(originalNode.Members, formattedNode.Members, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OpenBraceToken, formattedNode.OpenBraceToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -1117,7 +1117,7 @@ private CompareResult CompareConstructorDeclarationSyntax(ConstructorDeclaration originalStack.Push((originalNode.Initializer, originalNode)); formattedStack.Push((formattedNode.Initializer, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -1165,7 +1165,7 @@ private CompareResult CompareConversionOperatorDeclarationSyntax(ConversionOpera result = this.Compare(originalNode.ImplicitOrExplicitKeyword, formattedNode.ImplicitOrExplicitKeyword, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OperatorKeyword, formattedNode.OperatorKeyword, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -1314,7 +1314,7 @@ private CompareResult CompareDelegateDeclarationSyntax(DelegateDeclarationSyntax result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -1338,7 +1338,7 @@ private CompareResult CompareDestructorDeclarationSyntax(DestructorDeclarationSy result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -1517,7 +1517,7 @@ private CompareResult CompareEnumDeclarationSyntax(EnumDeclarationSyntax origina if (result.IsInvalid) return result; result = this.CompareLists(originalNode.Members.GetSeparators().ToList(), formattedNode.Members.GetSeparators().ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OpenBraceToken, formattedNode.OpenBraceToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -1535,7 +1535,7 @@ private CompareResult CompareEnumMemberDeclarationSyntax(EnumMemberDeclarationSy result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; return Equal; } @@ -1578,7 +1578,7 @@ private CompareResult CompareEventDeclarationSyntax(EventDeclarationSyntax origi result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.SemicolonToken, formattedNode.SemicolonToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -1596,7 +1596,7 @@ private CompareResult CompareEventFieldDeclarationSyntax(EventFieldDeclarationSy result = this.Compare(originalNode.EventKeyword, formattedNode.EventKeyword, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.SemicolonToken, formattedNode.SemicolonToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -1657,7 +1657,7 @@ private CompareResult CompareFieldDeclarationSyntax(FieldDeclarationSyntax origi originalStack.Push((originalNode.Declaration, originalNode)); formattedStack.Push((formattedNode.Declaration, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.SemicolonToken, formattedNode.SemicolonToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -1673,7 +1673,7 @@ private CompareResult CompareFileScopedNamespaceDeclarationSyntax(FileScopedName if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); result = this.CompareLists(originalNode.Members, formattedNode.Members, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Name, originalNode)); formattedStack.Push((formattedNode.Name, formattedNode)); @@ -1841,7 +1841,7 @@ private CompareResult CompareFunctionPointerParameterSyntax(FunctionPointerParam result = this.CompareLists(originalNode.AttributeLists, formattedNode.AttributeLists, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Type, originalNode)); formattedStack.Push((formattedNode.Type, formattedNode)); @@ -1910,7 +1910,7 @@ private CompareResult CompareGlobalStatementSyntax(GlobalStatementSyntax origina result = this.CompareLists(originalNode.AttributeLists, formattedNode.AttributeLists, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Statement, originalNode)); formattedStack.Push((formattedNode.Statement, formattedNode)); @@ -2054,7 +2054,7 @@ private CompareResult CompareIncompleteMemberSyntax(IncompleteMemberSyntax origi result = this.CompareLists(originalNode.AttributeLists, formattedNode.AttributeLists, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Type, originalNode)); formattedStack.Push((formattedNode.Type, formattedNode)); @@ -2072,7 +2072,7 @@ private CompareResult CompareIndexerDeclarationSyntax(IndexerDeclarationSyntax o originalStack.Push((originalNode.ExpressionBody, originalNode)); formattedStack.Push((formattedNode.ExpressionBody, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -2126,7 +2126,7 @@ private CompareResult CompareInterfaceDeclarationSyntax(InterfaceDeclarationSynt if (result.IsInvalid) return result; result = this.CompareLists(originalNode.Members, formattedNode.Members, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OpenBraceToken, formattedNode.OpenBraceToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -2390,7 +2390,7 @@ private CompareResult CompareLocalDeclarationStatementSyntax(LocalDeclarationSta formattedStack.Push((formattedNode.Declaration, formattedNode)); if (originalNode.IsConst != formattedNode.IsConst) return NotEqual(originalNode, formattedNode); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.SemicolonToken, formattedNode.SemicolonToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -2412,7 +2412,7 @@ private CompareResult CompareLocalFunctionStatementSyntax(LocalFunctionStatement result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -2494,7 +2494,7 @@ private CompareResult CompareMethodDeclarationSyntax(MethodDeclarationSyntax ori result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -2550,7 +2550,7 @@ private CompareResult CompareNamespaceDeclarationSyntax(NamespaceDeclarationSynt if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); result = this.CompareLists(originalNode.Members, formattedNode.Members, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Name, originalNode)); formattedStack.Push((formattedNode.Name, formattedNode)); @@ -2647,7 +2647,7 @@ private CompareResult CompareOperatorDeclarationSyntax(OperatorDeclarationSyntax originalStack.Push((originalNode.ExpressionBody, originalNode)); formattedStack.Push((formattedNode.ExpressionBody, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OperatorKeyword, formattedNode.OperatorKeyword, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -2721,7 +2721,7 @@ private CompareResult CompareParameterSyntax(ParameterSyntax originalNode, Param result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Type, originalNode)); formattedStack.Push((formattedNode.Type, formattedNode)); @@ -2755,7 +2755,7 @@ private CompareResult CompareParenthesizedLambdaExpressionSyntax(ParenthesizedLa originalStack.Push((originalNode.ExpressionBody, originalNode)); formattedStack.Push((formattedNode.ExpressionBody, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -2923,7 +2923,7 @@ private CompareResult ComparePropertyDeclarationSyntax(PropertyDeclarationSyntax originalStack.Push((originalNode.Initializer, originalNode)); formattedStack.Push((formattedNode.Initializer, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.SemicolonToken, formattedNode.SemicolonToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -3040,7 +3040,7 @@ private CompareResult CompareRecordDeclarationSyntax(RecordDeclarationSyntax ori if (result.IsInvalid) return result; result = this.CompareLists(originalNode.Members, formattedNode.Members, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OpenBraceToken, formattedNode.OpenBraceToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -3245,7 +3245,7 @@ private CompareResult CompareSimpleLambdaExpressionSyntax(SimpleLambdaExpression originalStack.Push((originalNode.ExpressionBody, originalNode)); formattedStack.Push((formattedNode.ExpressionBody, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Parameter, originalNode)); formattedStack.Push((formattedNode.Parameter, formattedNode)); @@ -3321,7 +3321,7 @@ private CompareResult CompareStructDeclarationSyntax(StructDeclarationSyntax ori if (result.IsInvalid) return result; result = this.CompareLists(originalNode.Members, formattedNode.Members, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareLists(originalNode.Modifiers, formattedNode.Modifiers, Compare, o => o.Span, originalNode.Span, formattedNode.Span); + result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OpenBraceToken, formattedNode.OpenBraceToken, originalNode, formattedNode); if (result.IsInvalid) return result; diff --git a/Src/CSharpier/SyntaxPrinter/Modifiers.cs b/Src/CSharpier/SyntaxPrinter/Modifiers.cs index a27684c90..be1cdb68d 100644 --- a/Src/CSharpier/SyntaxPrinter/Modifiers.cs +++ b/Src/CSharpier/SyntaxPrinter/Modifiers.cs @@ -2,9 +2,10 @@ namespace CSharpier.SyntaxPrinter; internal static class Modifiers { - class DefaultOrder : IComparer + private class DefaultOrder : IComparer { - static readonly string[] DefaultOrdered = new string[] + // use the default order from https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0036 + private static readonly string[] DefaultOrdered = { "public", "private", @@ -22,7 +23,9 @@ class DefaultOrder : IComparer "unsafe", "required", "volatile", - "async" + "async", + // not in the default list but needs to be last or it causes compilation errors + "const" }; public int Compare(string? x, string? y) From d2154f64e9fef0d46e22be2895f2618b46d7b4cb Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 5 Jun 2023 12:53:57 -0500 Subject: [PATCH 06/10] Fixing bug with not found modifiers --- .../FormattingTests/TestFiles/cs/Modifiers_Sort.test | 4 +++- Src/CSharpier/SyntaxPrinter/Modifiers.cs | 10 +++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.test index 0c5f669ec..2097b6a0b 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.test @@ -1,4 +1,6 @@ public class ClassName { async static public MethodName() { } -} \ No newline at end of file +} + +public partial class PartialClass { } diff --git a/Src/CSharpier/SyntaxPrinter/Modifiers.cs b/Src/CSharpier/SyntaxPrinter/Modifiers.cs index be1cdb68d..18d073f95 100644 --- a/Src/CSharpier/SyntaxPrinter/Modifiers.cs +++ b/Src/CSharpier/SyntaxPrinter/Modifiers.cs @@ -24,13 +24,17 @@ private class DefaultOrder : IComparer "required", "volatile", "async", - // not in the default list but needs to be last or it causes compilation errors - "const" }; public int Compare(string? x, string? y) { - return Array.IndexOf(DefaultOrdered, x) - Array.IndexOf(DefaultOrdered, y); + int GetIndex(string? value) + { + var result = Array.IndexOf(DefaultOrdered, value); + return result == -1 ? int.MaxValue : result; + } + + return GetIndex(x) - GetIndex(y); } } From ac2df5cdc808e333438f45b5c219e855b2637553 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 5 Jun 2023 16:14:41 -0500 Subject: [PATCH 07/10] Fixing bug with modifiers inside of #ifs --- .../TestFiles/cs/Modifiers_Sort.expected.test | 2 ++ .../cs/Modifiers_Sort_Preprocessors.test | 15 +++++++++++++++ Src/CSharpier/SyntaxPrinter/Modifiers.cs | 5 ++++- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors.test diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.expected.test index 300e36ceb..535ec0a77 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.expected.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort.expected.test @@ -2,3 +2,5 @@ public class ClassName { public static async MethodName() { } } + +public partial class PartialClass { } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors.test new file mode 100644 index 000000000..5b484fda5 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors.test @@ -0,0 +1,15 @@ +public class DontReorderThis +{ +#if !SILVERLIGHT + [SecuritySafeCritical] +#endif + internal +#if !SILVERLIGHT + unsafe +#endif + static String Method() { } + +#if MONO + public void NewSymbolSet() { } +#endif +} diff --git a/Src/CSharpier/SyntaxPrinter/Modifiers.cs b/Src/CSharpier/SyntaxPrinter/Modifiers.cs index 18d073f95..dacef02f3 100644 --- a/Src/CSharpier/SyntaxPrinter/Modifiers.cs +++ b/Src/CSharpier/SyntaxPrinter/Modifiers.cs @@ -66,7 +66,10 @@ FormattingContext context return Doc.Null; } - var sortedModifiers = modifiers.OrderBy(o => o.Text, Comparer); + // reordering modifiers inside of #ifs can lead to code that doesn't compile + var sortedModifiers = modifiers.Any(o => o.LeadingTrivia.Any(p => p.IsDirective)) + ? modifiers.AsEnumerable() + : modifiers.OrderBy(o => o.Text, Comparer); return Doc.Group( Token.PrintWithoutLeadingTrivia(sortedModifiers.First(), context), From 475b7812e0232a23eac597803b1635f03c018846 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 5 Jun 2023 16:49:57 -0500 Subject: [PATCH 08/10] And another weird edge case --- .../cs/Modifiers_Sort_Preprocessors.test | 11 +++- .../cs/Modifiers_Sort_Preprocessors2.test | 10 ++++ Src/CSharpier/CSharpFormatter.cs | 2 + Src/CSharpier/SyntaxPrinter/Modifiers.cs | 58 ++++++++++--------- 4 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors2.test diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors.test index 5b484fda5..b1b449670 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors.test @@ -1,7 +1,7 @@ public class DontReorderThis { #if !SILVERLIGHT - [SecuritySafeCritical] + [Attribute] #endif internal #if !SILVERLIGHT @@ -12,4 +12,13 @@ public class DontReorderThis #if MONO public void NewSymbolSet() { } #endif + +#if !SILVERLIGHT + [Attribute] +#endif + private +#if !SILVERLIGHT + static +#endif + string Field; } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors2.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors2.test new file mode 100644 index 000000000..56932b2ec --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors2.test @@ -0,0 +1,10 @@ +class AnotherEdgeCase +{ +#if MONO + static internal string Field1; +#endif + +#if DEBUG + // some comment +#endif +} diff --git a/Src/CSharpier/CSharpFormatter.cs b/Src/CSharpier/CSharpFormatter.cs index 0f6ed73ce..9925dd3d7 100644 --- a/Src/CSharpier/CSharpFormatter.cs +++ b/Src/CSharpier/CSharpFormatter.cs @@ -109,6 +109,8 @@ bool TryGetCompilationFailure(out CodeFormatterResult compilationResult) foreach (var symbolSet in PreprocessorSymbols.GetSets(syntaxTree)) { + DebugLogger.Log(formattedCode); + syntaxTree = ParseText(formattedCode, symbolSet, cancellationToken); if (TryGetCompilationFailure(out result)) diff --git a/Src/CSharpier/SyntaxPrinter/Modifiers.cs b/Src/CSharpier/SyntaxPrinter/Modifiers.cs index dacef02f3..5d60af7ce 100644 --- a/Src/CSharpier/SyntaxPrinter/Modifiers.cs +++ b/Src/CSharpier/SyntaxPrinter/Modifiers.cs @@ -42,17 +42,10 @@ int GetIndex(string? value) public static Doc Print(SyntaxTokenList modifiers, FormattingContext context) { - if (modifiers.Count == 0) - { - return Doc.Null; - } - - return Doc.Group( - Doc.Join( - " ", - modifiers.OrderBy(o => o.Text, Comparer).Select(o => Token.Print(o, context)) - ), - " " + return PrintWithSortedModifiers( + modifiers, + sortedModifiers => + Doc.Group(Doc.Join(" ", sortedModifiers.Select(o => Token.Print(o, context))), " ") ); } @@ -60,6 +53,29 @@ public static Doc PrintWithoutLeadingTrivia( SyntaxTokenList modifiers, FormattingContext context ) + { + return PrintWithSortedModifiers( + modifiers, + sortedModifiers => + Doc.Group( + Token.PrintWithoutLeadingTrivia(sortedModifiers.First(), context), + " ", + sortedModifiers.Count() > 1 + ? Doc.Concat( + sortedModifiers + .Skip(1) + .Select(o => Token.PrintWithSuffix(o, " ", context)) + .ToArray() + ) + : Doc.Null + ) + ); + } + + private static Doc PrintWithSortedModifiers( + SyntaxTokenList modifiers, + Func, Doc> print + ) { if (modifiers.Count == 0) { @@ -67,21 +83,11 @@ FormattingContext context } // reordering modifiers inside of #ifs can lead to code that doesn't compile - var sortedModifiers = modifiers.Any(o => o.LeadingTrivia.Any(p => p.IsDirective)) - ? modifiers.AsEnumerable() - : modifiers.OrderBy(o => o.Text, Comparer); + var sortedModifiers = + modifiers.Count == 1 || modifiers.Any(o => o.LeadingTrivia.Any(p => p.IsDirective)) + ? modifiers.AsEnumerable() + : modifiers.OrderBy(o => o.Text, Comparer); - return Doc.Group( - Token.PrintWithoutLeadingTrivia(sortedModifiers.First(), context), - " ", - sortedModifiers.Count() > 1 - ? Doc.Concat( - sortedModifiers - .Skip(1) - .Select(o => Token.PrintWithSuffix(o, " ", context)) - .ToArray() - ) - : Doc.Null - ); + return print(sortedModifiers); } } From 1732d6f6bf75b39a78b35ca5f8c2724c5885661a Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Mon, 5 Jun 2023 17:09:23 -0500 Subject: [PATCH 09/10] Self code review --- .../SyntaxNodeComparerGenerator.cs | 11 ++-- .../SyntaxNodeComparerTests.cs | 2 +- Src/CSharpier/CSharpFormatter.cs | 2 - Src/CSharpier/SyntaxNodeComparer.cs | 32 ---------- Src/CSharpier/SyntaxNodeComparer.generated.cs | 58 +++++++++---------- 5 files changed, 34 insertions(+), 71 deletions(-) diff --git a/Src/CSharpier.FakeGenerators/SyntaxNodeComparerGenerator.cs b/Src/CSharpier.FakeGenerators/SyntaxNodeComparerGenerator.cs index 79cad1212..ed508bd55 100644 --- a/Src/CSharpier.FakeGenerators/SyntaxNodeComparerGenerator.cs +++ b/Src/CSharpier.FakeGenerators/SyntaxNodeComparerGenerator.cs @@ -166,13 +166,6 @@ private static void GenerateMethod(StringBuilder sourceBuilder, Type type) $" formattedStack.Push((formattedNode.{propertyName}, formattedNode));" ); } - else if (propertyType == typeof(SyntaxTokenList) && propertyName == "Modifiers") - { - sourceBuilder.AppendLine( - $" result = this.CompareModifiers(originalNode.{propertyName}, formattedNode.{propertyName}, originalNode.Span, formattedNode.Span);" - ); - sourceBuilder.AppendLine($" if (result.IsInvalid) return result;"); - } else if ( propertyType == typeof(SyntaxTokenList) || ( @@ -182,6 +175,10 @@ private static void GenerateMethod(StringBuilder sourceBuilder, Type type) ) { var compare = propertyType == typeof(SyntaxTokenList) ? "Compare" : "null"; + if (propertyName == "Modifiers") + { + propertyName += ".OrderBy(o => o.Text).ToList()"; + } sourceBuilder.AppendLine( $" result = this.CompareLists(originalNode.{propertyName}, formattedNode.{propertyName}, {compare}, o => o.Span, originalNode.Span, formattedNode.Span);" ); diff --git a/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs b/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs index ede29a497..74526ac44 100644 --- a/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs +++ b/Src/CSharpier.Tests/SyntaxNodeComparerTests.cs @@ -383,7 +383,7 @@ void MethodName() } [Test] - public void Sorted_Modifiers() + public void Unsorted_Modifiers_Pass_Validation() { var left = @"static public class { }"; diff --git a/Src/CSharpier/CSharpFormatter.cs b/Src/CSharpier/CSharpFormatter.cs index 9925dd3d7..0f6ed73ce 100644 --- a/Src/CSharpier/CSharpFormatter.cs +++ b/Src/CSharpier/CSharpFormatter.cs @@ -109,8 +109,6 @@ bool TryGetCompilationFailure(out CodeFormatterResult compilationResult) foreach (var symbolSet in PreprocessorSymbols.GetSets(syntaxTree)) { - DebugLogger.Log(formattedCode); - syntaxTree = ParseText(formattedCode, symbolSet, cancellationToken); if (TryGetCompilationFailure(out result)) diff --git a/Src/CSharpier/SyntaxNodeComparer.cs b/Src/CSharpier/SyntaxNodeComparer.cs index 1ddd0ba8a..97167b03a 100644 --- a/Src/CSharpier/SyntaxNodeComparer.cs +++ b/Src/CSharpier/SyntaxNodeComparer.cs @@ -163,38 +163,6 @@ originalList[x] is SyntaxNode originalNode return Equal; } - private CompareResult CompareModifiers( - SyntaxTokenList originalList, - SyntaxTokenList formattedList, - TextSpan originalParentSpan, - TextSpan newParentSpan - ) - { - var orderedOriginalList = originalList.OrderBy(o => o.Text).ToList(); - var orderedFormattedList = formattedList.OrderBy(o => o.Text).ToList(); - - for (var x = 0; x < orderedOriginalList.Count || x < orderedFormattedList.Count; x++) - { - if (x == orderedOriginalList.Count) - { - return NotEqual(originalParentSpan, orderedFormattedList[x].Span); - } - - if (x == orderedFormattedList.Count) - { - return NotEqual(orderedOriginalList[x].Span, newParentSpan); - } - - var result = this.Compare(orderedOriginalList[x], orderedFormattedList[x]); - if (result.IsInvalid) - { - return result; - } - } - - return Equal; - } - private static CompareResult NotEqual(SyntaxNode? originalNode, SyntaxNode? formattedNode) { return new() diff --git a/Src/CSharpier/SyntaxNodeComparer.generated.cs b/Src/CSharpier/SyntaxNodeComparer.generated.cs index f39e84931..eb5d43abb 100644 --- a/Src/CSharpier/SyntaxNodeComparer.generated.cs +++ b/Src/CSharpier/SyntaxNodeComparer.generated.cs @@ -535,7 +535,7 @@ private CompareResult CompareAccessorDeclarationSyntax(AccessorDeclarationSyntax if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); result = this.Compare(originalNode.Keyword, formattedNode.Keyword, originalNode, formattedNode); if (result.IsInvalid) return result; - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.SemicolonToken, formattedNode.SemicolonToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -584,7 +584,7 @@ private CompareResult CompareAnonymousMethodExpressionSyntax(AnonymousMethodExpr originalStack.Push((originalNode.ExpressionBody, originalNode)); formattedStack.Push((formattedNode.ExpressionBody, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -1019,7 +1019,7 @@ private CompareResult CompareClassDeclarationSyntax(ClassDeclarationSyntax origi if (result.IsInvalid) return result; result = this.CompareLists(originalNode.Members, formattedNode.Members, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OpenBraceToken, formattedNode.OpenBraceToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -1117,7 +1117,7 @@ private CompareResult CompareConstructorDeclarationSyntax(ConstructorDeclaration originalStack.Push((originalNode.Initializer, originalNode)); formattedStack.Push((formattedNode.Initializer, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -1165,7 +1165,7 @@ private CompareResult CompareConversionOperatorDeclarationSyntax(ConversionOpera result = this.Compare(originalNode.ImplicitOrExplicitKeyword, formattedNode.ImplicitOrExplicitKeyword, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OperatorKeyword, formattedNode.OperatorKeyword, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -1314,7 +1314,7 @@ private CompareResult CompareDelegateDeclarationSyntax(DelegateDeclarationSyntax result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -1338,7 +1338,7 @@ private CompareResult CompareDestructorDeclarationSyntax(DestructorDeclarationSy result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -1517,7 +1517,7 @@ private CompareResult CompareEnumDeclarationSyntax(EnumDeclarationSyntax origina if (result.IsInvalid) return result; result = this.CompareLists(originalNode.Members.GetSeparators().ToList(), formattedNode.Members.GetSeparators().ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OpenBraceToken, formattedNode.OpenBraceToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -1535,7 +1535,7 @@ private CompareResult CompareEnumMemberDeclarationSyntax(EnumMemberDeclarationSy result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; return Equal; } @@ -1578,7 +1578,7 @@ private CompareResult CompareEventDeclarationSyntax(EventDeclarationSyntax origi result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.SemicolonToken, formattedNode.SemicolonToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -1596,7 +1596,7 @@ private CompareResult CompareEventFieldDeclarationSyntax(EventFieldDeclarationSy result = this.Compare(originalNode.EventKeyword, formattedNode.EventKeyword, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.SemicolonToken, formattedNode.SemicolonToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -1657,7 +1657,7 @@ private CompareResult CompareFieldDeclarationSyntax(FieldDeclarationSyntax origi originalStack.Push((originalNode.Declaration, originalNode)); formattedStack.Push((formattedNode.Declaration, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.SemicolonToken, formattedNode.SemicolonToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -1673,7 +1673,7 @@ private CompareResult CompareFileScopedNamespaceDeclarationSyntax(FileScopedName if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); result = this.CompareLists(originalNode.Members, formattedNode.Members, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Name, originalNode)); formattedStack.Push((formattedNode.Name, formattedNode)); @@ -1841,7 +1841,7 @@ private CompareResult CompareFunctionPointerParameterSyntax(FunctionPointerParam result = this.CompareLists(originalNode.AttributeLists, formattedNode.AttributeLists, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Type, originalNode)); formattedStack.Push((formattedNode.Type, formattedNode)); @@ -1910,7 +1910,7 @@ private CompareResult CompareGlobalStatementSyntax(GlobalStatementSyntax origina result = this.CompareLists(originalNode.AttributeLists, formattedNode.AttributeLists, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Statement, originalNode)); formattedStack.Push((formattedNode.Statement, formattedNode)); @@ -2054,7 +2054,7 @@ private CompareResult CompareIncompleteMemberSyntax(IncompleteMemberSyntax origi result = this.CompareLists(originalNode.AttributeLists, formattedNode.AttributeLists, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Type, originalNode)); formattedStack.Push((formattedNode.Type, formattedNode)); @@ -2072,7 +2072,7 @@ private CompareResult CompareIndexerDeclarationSyntax(IndexerDeclarationSyntax o originalStack.Push((originalNode.ExpressionBody, originalNode)); formattedStack.Push((formattedNode.ExpressionBody, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -2126,7 +2126,7 @@ private CompareResult CompareInterfaceDeclarationSyntax(InterfaceDeclarationSynt if (result.IsInvalid) return result; result = this.CompareLists(originalNode.Members, formattedNode.Members, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OpenBraceToken, formattedNode.OpenBraceToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -2390,7 +2390,7 @@ private CompareResult CompareLocalDeclarationStatementSyntax(LocalDeclarationSta formattedStack.Push((formattedNode.Declaration, formattedNode)); if (originalNode.IsConst != formattedNode.IsConst) return NotEqual(originalNode, formattedNode); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.SemicolonToken, formattedNode.SemicolonToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -2412,7 +2412,7 @@ private CompareResult CompareLocalFunctionStatementSyntax(LocalFunctionStatement result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -2494,7 +2494,7 @@ private CompareResult CompareMethodDeclarationSyntax(MethodDeclarationSyntax ori result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -2550,7 +2550,7 @@ private CompareResult CompareNamespaceDeclarationSyntax(NamespaceDeclarationSynt if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); result = this.CompareLists(originalNode.Members, formattedNode.Members, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Name, originalNode)); formattedStack.Push((formattedNode.Name, formattedNode)); @@ -2647,7 +2647,7 @@ private CompareResult CompareOperatorDeclarationSyntax(OperatorDeclarationSyntax originalStack.Push((originalNode.ExpressionBody, originalNode)); formattedStack.Push((formattedNode.ExpressionBody, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OperatorKeyword, formattedNode.OperatorKeyword, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -2721,7 +2721,7 @@ private CompareResult CompareParameterSyntax(ParameterSyntax originalNode, Param result = this.Compare(originalNode.Identifier, formattedNode.Identifier, originalNode, formattedNode); if (result.IsInvalid) return result; if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Type, originalNode)); formattedStack.Push((formattedNode.Type, formattedNode)); @@ -2755,7 +2755,7 @@ private CompareResult CompareParenthesizedLambdaExpressionSyntax(ParenthesizedLa originalStack.Push((originalNode.ExpressionBody, originalNode)); formattedStack.Push((formattedNode.ExpressionBody, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.ParameterList, originalNode)); formattedStack.Push((formattedNode.ParameterList, formattedNode)); @@ -2923,7 +2923,7 @@ private CompareResult ComparePropertyDeclarationSyntax(PropertyDeclarationSyntax originalStack.Push((originalNode.Initializer, originalNode)); formattedStack.Push((formattedNode.Initializer, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.SemicolonToken, formattedNode.SemicolonToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -3040,7 +3040,7 @@ private CompareResult CompareRecordDeclarationSyntax(RecordDeclarationSyntax ori if (result.IsInvalid) return result; result = this.CompareLists(originalNode.Members, formattedNode.Members, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OpenBraceToken, formattedNode.OpenBraceToken, originalNode, formattedNode); if (result.IsInvalid) return result; @@ -3245,7 +3245,7 @@ private CompareResult CompareSimpleLambdaExpressionSyntax(SimpleLambdaExpression originalStack.Push((originalNode.ExpressionBody, originalNode)); formattedStack.Push((formattedNode.ExpressionBody, formattedNode)); if (originalNode.IsMissing != formattedNode.IsMissing) return NotEqual(originalNode, formattedNode); - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; originalStack.Push((originalNode.Parameter, originalNode)); formattedStack.Push((formattedNode.Parameter, formattedNode)); @@ -3321,7 +3321,7 @@ private CompareResult CompareStructDeclarationSyntax(StructDeclarationSyntax ori if (result.IsInvalid) return result; result = this.CompareLists(originalNode.Members, formattedNode.Members, null, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; - result = this.CompareModifiers(originalNode.Modifiers, formattedNode.Modifiers, originalNode.Span, formattedNode.Span); + result = this.CompareLists(originalNode.Modifiers.OrderBy(o => o.Text).ToList(), formattedNode.Modifiers.OrderBy(o => o.Text).ToList(), Compare, o => o.Span, originalNode.Span, formattedNode.Span); if (result.IsInvalid) return result; result = this.Compare(originalNode.OpenBraceToken, formattedNode.OpenBraceToken, originalNode, formattedNode); if (result.IsInvalid) return result; From 26f2b3e213d67927cb8dd44651145c4b4c3d3457 Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Sat, 10 Jun 2023 11:24:12 -0500 Subject: [PATCH 10/10] Code review changes --- .gitignore | 1 + Directory.Packages.props | 6 +++--- .../CSharpier.Benchmarks.csproj | 6 +++--- Src/CSharpier/SyntaxPrinter/Modifiers.cs | 20 +++++++++---------- 4 files changed, 17 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index b1a2c06c8..40ccc06c3 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,4 @@ Src/CSharpier.VSCode/.idea/prettier.xml /Src/CSharpier.Tests/Samples/Scratch.cst .idea/.idea.CSharpier/.idea/riderMarkupCache.xml +/Src/CSharpier.Benchmarks/BenchmarkDotNet.Artifacts/ diff --git a/Directory.Packages.props b/Directory.Packages.props index f76236be2..ea54e97e6 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -3,8 +3,8 @@ true - - + + @@ -30,4 +30,4 @@ - + \ No newline at end of file diff --git a/Src/CSharpier.Benchmarks/CSharpier.Benchmarks.csproj b/Src/CSharpier.Benchmarks/CSharpier.Benchmarks.csproj index 8383dbe27..8837d923c 100644 --- a/Src/CSharpier.Benchmarks/CSharpier.Benchmarks.csproj +++ b/Src/CSharpier.Benchmarks/CSharpier.Benchmarks.csproj @@ -3,10 +3,10 @@ Exe - - + + - + diff --git a/Src/CSharpier/SyntaxPrinter/Modifiers.cs b/Src/CSharpier/SyntaxPrinter/Modifiers.cs index 5d60af7ce..2cef92fff 100644 --- a/Src/CSharpier/SyntaxPrinter/Modifiers.cs +++ b/Src/CSharpier/SyntaxPrinter/Modifiers.cs @@ -28,14 +28,14 @@ private class DefaultOrder : IComparer public int Compare(string? x, string? y) { - int GetIndex(string? value) - { - var result = Array.IndexOf(DefaultOrdered, value); - return result == -1 ? int.MaxValue : result; - } - return GetIndex(x) - GetIndex(y); } + + private static int GetIndex(string? value) + { + var result = Array.IndexOf(DefaultOrdered, value); + return result == -1 ? int.MaxValue : result; + } } private static readonly DefaultOrder Comparer = new(); @@ -58,9 +58,9 @@ FormattingContext context modifiers, sortedModifiers => Doc.Group( - Token.PrintWithoutLeadingTrivia(sortedModifiers.First(), context), + Token.PrintWithoutLeadingTrivia(sortedModifiers[0], context), " ", - sortedModifiers.Count() > 1 + sortedModifiers.Count > 1 ? Doc.Concat( sortedModifiers .Skip(1) @@ -74,7 +74,7 @@ FormattingContext context private static Doc PrintWithSortedModifiers( SyntaxTokenList modifiers, - Func, Doc> print + Func, Doc> print ) { if (modifiers.Count == 0) @@ -88,6 +88,6 @@ Func, Doc> print ? modifiers.AsEnumerable() : modifiers.OrderBy(o => o.Text, Comparer); - return print(sortedModifiers); + return print(sortedModifiers.ToArray()); } }