Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion Src/CSharpier.Core/CSharp/SyntaxPrinter/Modifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ Func<IReadOnlyList<SyntaxToken>, Doc> print
// reordering modifiers inside of #ifs can lead to code that doesn't compile
var willReorderModifiers =
modifiers.Count > 1
&& !modifiers.Any(o => o.LeadingTrivia.Any(p => p.IsDirective || p.IsComment()));
&& !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);
Expand All @@ -112,6 +114,15 @@ Func<IReadOnlyList<SyntaxToken>, Doc> print
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class DontReorderThis
{
#if !SILVERLIGHT
[Attribute]
#endif
internal
#if !SILVERLIGHT
unsafe
#endif
static String Method() { }

#if MONO
public void NewSymbolSet() { }
#endif

#if !SILVERLIGHT
[Attribute]
#endif
private
#if !SILVERLIGHT
static
#endif
string Field;

// leading comment should reorder modifiers
public static void Method() { }

static
// midway comment should not reorder modifiers
public void Method1() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public class DontReorderThis
#endif
string Field;

// comment should not reorder modifiers
// leading comment should reorder modifiers
static public void Method() { }

static
// midway comment should not reorder modifiers
public void Method1() { }
}