-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathModifiers.cs
More file actions
132 lines (117 loc) · 3.97 KB
/
Copy pathModifiers.cs
File metadata and controls
132 lines (117 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using CSharpier.Core.DocTypes;
using CSharpier.Core.Utilities;
using Microsoft.CodeAnalysis;
namespace CSharpier.Core.CSharp.SyntaxPrinter;
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",
"closed",
"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;
}
}
private static readonly DefaultOrder Comparer = new();
public static Doc Print(SyntaxTokenList modifiers, PrintingContext context)
{
if (modifiers.Count == 0)
{
return Doc.Null;
}
return Doc.Group(Doc.Join(" ", modifiers.Select(o => Token.Print(o, context))), " ");
}
public static Doc PrintSorted(SyntaxTokenList modifiers, PrintingContext context)
{
return PrintWithSortedModifiers(
modifiers,
context,
sortedModifiers =>
Doc.Group(Doc.Join(" ", sortedModifiers.Select(o => Token.Print(o, context))), " ")
);
}
public static Doc PrintSorterWithoutLeadingTrivia(
SyntaxTokenList modifiers,
PrintingContext context
)
{
return PrintWithSortedModifiers(
modifiers,
context,
sortedModifiers =>
Doc.Group(
Token.PrintWithoutLeadingTrivia(sortedModifiers[0], context),
" ",
sortedModifiers.Count > 1
? Doc.Concat(
sortedModifiers
.Skip(1)
.Select(o => Token.PrintWithSuffix(o, " ", context))
.ToArray()
)
: Doc.Null
)
);
}
private static Doc PrintWithSortedModifiers(
in SyntaxTokenList modifiers,
PrintingContext context,
Func<IReadOnlyList<SyntaxToken>, Doc> print
)
{
if (modifiers.Count == 0)
{
return Doc.Null;
}
// reordering modifiers inside of #ifs can lead to code that doesn't compile
var willReorderModifiers =
modifiers.Count > 1
&& !modifiers.Skip(1).Any(o => o.LeadingTrivia.Any(p => p.IsDirective || p.IsComment()))
&& !modifiers[0].LeadingTrivia.Any(p => p.IsDirective);
var sortedModifiers = modifiers.ToArray();
var leadingToken = sortedModifiers.FirstOrDefault();
if (willReorderModifiers)
{
Array.Sort(sortedModifiers, Comparer);
}
if (willReorderModifiers && !sortedModifiers.SequenceEqual(modifiers))
{
context.State.ReorderedModifiers = true;
var leadingTrivia = leadingToken.LeadingTrivia;
var leadingTokenIndex = Array.FindIndex(
sortedModifiers,
token => token == leadingToken
);
sortedModifiers[leadingTokenIndex] = sortedModifiers[leadingTokenIndex]
.WithLeadingTrivia(new SyntaxTriviaList());
sortedModifiers[0] = sortedModifiers[0].WithLeadingTrivia(leadingTrivia);
}
return print(sortedModifiers);
}
}