Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions Src/CSharpier.Core/CSharp/SyntaxPrinter/Modifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,39 @@ internal static class Modifiers
{
private class DefaultOrder : IComparer<SyntaxToken>
{
// 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);
}

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<SyntaxToken> Comparer = new DefaultOrder().Compare;

public static Doc Print(SyntaxTokenList modifiers, PrintingContext context)
{
Expand All @@ -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))), " ")
);
}
Expand All @@ -71,7 +69,7 @@ PrintingContext context
return PrintWithSortedModifiers(
modifiers,
context,
sortedModifiers =>
static (sortedModifiers, context) =>
Doc.Group(
Token.PrintWithoutLeadingTrivia(sortedModifiers[0], context),
" ",
Expand All @@ -90,7 +88,7 @@ PrintingContext context
private static Doc PrintWithSortedModifiers(
in SyntaxTokenList modifiers,
PrintingContext context,
Func<IReadOnlyList<SyntaxToken>, Doc> print
Func<IReadOnlyList<SyntaxToken>, PrintingContext, Doc> print
)
{
if (modifiers.Count == 0)
Expand All @@ -114,6 +112,6 @@ Func<IReadOnlyList<SyntaxToken>, Doc> print
context.State.ReorderedModifiers = true;
}

return print(sortedModifiers);
return print(sortedModifiers, context);
}
}