diff --git a/Src/CSharpier.Core/CSharp/SyntaxPrinter/Modifiers.cs b/Src/CSharpier.Core/CSharp/SyntaxPrinter/Modifiers.cs index 14688165a..68f0c03c5 100644 --- a/Src/CSharpier.Core/CSharp/SyntaxPrinter/Modifiers.cs +++ b/Src/CSharpier.Core/CSharp/SyntaxPrinter/Modifiers.cs @@ -7,28 +7,6 @@ internal static class Modifiers { private class DefaultOrder : IComparer { - // 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", - "protected", - "internal", - "file", - "static", - "extern", - "new", - "virtual", - "abstract", - "sealed", - "override", - "readonly", - "unsafe", - "required", - "volatile", - "async", - ]; - public int Compare(SyntaxToken x, SyntaxToken y) { return GetIndex(x.Text) - GetIndex(y.Text); @@ -36,12 +14,32 @@ public int Compare(SyntaxToken x, SyntaxToken y) private static int GetIndex(string? value) { - var result = Array.IndexOf(DefaultOrdered, value); - return result == -1 ? int.MaxValue : result; + // use the default order from https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0036 + return value switch + { + "public" => 0, + "private" => 1, + "protected" => 2, + "internal" => 3, + "file" => 4, + "static" => 5, + "extern" => 6, + "new" => 7, + "virtual" => 8, + "abstract" => 9, + "sealed" => 10, + "override" => 11, + "readonly" => 12, + "unsafe" => 13, + "required" => 14, + "volatile" => 15, + "async" => 16, + _ => int.MaxValue, + }; } } - private static readonly DefaultOrder Comparer = new(); + private static readonly Comparison Comparer = new DefaultOrder().Compare; public static Doc Print(SyntaxTokenList modifiers, PrintingContext context) { @@ -58,7 +56,7 @@ public static Doc PrintSorted(SyntaxTokenList modifiers, PrintingContext context return PrintWithSortedModifiers( modifiers, context, - sortedModifiers => + static (sortedModifiers, context) => Doc.Group(Doc.Join(" ", sortedModifiers.Select(o => Token.Print(o, context))), " ") ); } @@ -71,7 +69,7 @@ PrintingContext context return PrintWithSortedModifiers( modifiers, context, - sortedModifiers => + static (sortedModifiers, context) => Doc.Group( Token.PrintWithoutLeadingTrivia(sortedModifiers[0], context), " ", @@ -90,7 +88,7 @@ PrintingContext context private static Doc PrintWithSortedModifiers( in SyntaxTokenList modifiers, PrintingContext context, - Func, Doc> print + Func, PrintingContext, Doc> print ) { if (modifiers.Count == 0) @@ -114,6 +112,6 @@ Func, Doc> print context.State.ReorderedModifiers = true; } - return print(sortedModifiers); + return print(sortedModifiers, context); } }