diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_DirectiveGroupLast.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_DirectiveGroupLast.test new file mode 100644 index 000000000..f1d4daa7b --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_DirectiveGroupLast.test @@ -0,0 +1,8 @@ +using System; +using static SomeOtherNamespace; +using SomeAlias = SomeNamespace; +#if DEBUG +using SomeDebugNamespace; +#endif + +public static class ODataEdmModelProvider { } diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsAliasInGlobals.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsAliasInGlobals.test new file mode 100644 index 000000000..3890905c1 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsAliasInGlobals.test @@ -0,0 +1,3 @@ +global using Nest7; +global using Newtonsoft.Json; +global using Nest = Nest7; diff --git a/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs b/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs index 3bbf0e5b9..1d2f7e5f1 100644 --- a/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs +++ b/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs @@ -135,6 +135,7 @@ FormattingContext context ) { var globalUsings = new List(); + var globalAliasUsings = new List(); var systemUsings = new List(); var aliasNameUsings = new List(); var regularUsings = new List(); @@ -186,7 +187,9 @@ FormattingContext context if (usingDirective.GlobalKeyword.RawSyntaxKind() != SyntaxKind.None) { - globalUsings.Add(usingData); + (usingDirective.Alias is not null ? globalAliasUsings : globalUsings).Add( + usingData + ); } else if (usingDirective.StaticKeyword.RawSyntaxKind() != SyntaxKind.None) { @@ -212,12 +215,15 @@ FormattingContext context } yield return globalUsings.OrderBy(o => o.Using, Comparer).ToList(); + yield return globalAliasUsings.OrderBy(o => o.Using, Comparer).ToList(); yield return systemUsings.OrderBy(o => o.Using, Comparer).ToList(); yield return aliasNameUsings.OrderBy(o => o.Using, Comparer).ToList(); yield return regularUsings.OrderBy(o => o.Using, Comparer).ToList(); - yield return directiveGroup; yield return staticUsings.OrderBy(o => o.Using, Comparer).ToList(); yield return aliasUsings.OrderBy(o => o.Using, Comparer).ToList(); + // we need the directive groups at the end, the #endif directive + // will be attached to the first node after the usings making it very hard print it before any of these other groups + yield return directiveGroup; yield break; Doc PrintLeadingTrivia(UsingDirectiveSyntax value)