Skip to content

Commit 123ac5f

Browse files
feat: support reordering modifers with a leading comment (#1786)
Resolves #1784 It might be possible reorder all modiferes with comments and keep this logic. Perhaps this breaks something 🤷 Co-authored-by: Bela VanderVoort <twobitbela@gmail.com>
1 parent 6bf358f commit 123ac5f

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

Src/CSharpier.Core/CSharp/SyntaxPrinter/Modifiers.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ Func<IReadOnlyList<SyntaxToken>, Doc> print
101101
// reordering modifiers inside of #ifs can lead to code that doesn't compile
102102
var willReorderModifiers =
103103
modifiers.Count > 1
104-
&& !modifiers.Any(o => o.LeadingTrivia.Any(p => p.IsDirective || p.IsComment()));
104+
&& !modifiers.Skip(1).Any(o => o.LeadingTrivia.Any(p => p.IsDirective || p.IsComment()))
105+
&& !modifiers[0].LeadingTrivia.Any(p => p.IsDirective);
105106

106107
var sortedModifiers = modifiers.ToArray();
108+
var leadingToken = sortedModifiers.FirstOrDefault();
107109
if (willReorderModifiers)
108110
{
109111
Array.Sort(sortedModifiers, Comparer);
@@ -112,6 +114,15 @@ Func<IReadOnlyList<SyntaxToken>, Doc> print
112114
if (willReorderModifiers && !sortedModifiers.SequenceEqual(modifiers))
113115
{
114116
context.State.ReorderedModifiers = true;
117+
118+
var leadingTrivia = leadingToken.LeadingTrivia;
119+
var leadingTokenIndex = Array.FindIndex(
120+
sortedModifiers,
121+
token => token == leadingToken
122+
);
123+
sortedModifiers[leadingTokenIndex] = sortedModifiers[leadingTokenIndex]
124+
.WithLeadingTrivia(new SyntaxTriviaList());
125+
sortedModifiers[0] = sortedModifiers[0].WithLeadingTrivia(leadingTrivia);
115126
}
116127

117128
return print(sortedModifiers);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public class DontReorderThis
2+
{
3+
#if !SILVERLIGHT
4+
[Attribute]
5+
#endif
6+
internal
7+
#if !SILVERLIGHT
8+
unsafe
9+
#endif
10+
static String Method() { }
11+
12+
#if MONO
13+
public void NewSymbolSet() { }
14+
#endif
15+
16+
#if !SILVERLIGHT
17+
[Attribute]
18+
#endif
19+
private
20+
#if !SILVERLIGHT
21+
static
22+
#endif
23+
string Field;
24+
25+
// leading comment should reorder modifiers
26+
public static void Method() { }
27+
28+
static
29+
// midway comment should not reorder modifiers
30+
public void Method1() { }
31+
}

Src/CSharpier.Tests/FormattingTests/TestFiles/cs/Modifiers_Sort_Preprocessors.test

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public class DontReorderThis
2222
#endif
2323
string Field;
2424

25-
// comment should not reorder modifiers
25+
// leading comment should reorder modifiers
2626
static public void Method() { }
27+
28+
static
29+
// midway comment should not reorder modifiers
30+
public void Method1() { }
2731
}

0 commit comments

Comments
 (0)