From 0a4ccb9821b1cf102c20875c1b87eda2f12785e3 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Fri, 10 Nov 2023 16:20:11 +0200 Subject: [PATCH] Sort global System usings before other global usings --- ...ectives_SortsGlobalSystemToTop.expected.test | 4 ++++ .../UsingDirectives_SortsGlobalSystemToTop.test | 4 ++++ ...Directives_SortsMixedCorrectly.expected.test | 8 ++++++++ .../cs/UsingDirectives_SortsMixedCorrectly.test | 8 ++++++++ Src/CSharpier/SyntaxPrinter/UsingDirectives.cs | 17 ++++++++++++++--- 5 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsGlobalSystemToTop.expected.test create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsGlobalSystemToTop.test create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsMixedCorrectly.expected.test create mode 100644 Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsMixedCorrectly.test diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsGlobalSystemToTop.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsGlobalSystemToTop.expected.test new file mode 100644 index 000000000..6ece74ca5 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsGlobalSystemToTop.expected.test @@ -0,0 +1,4 @@ +global using System; +global using System.Web; +global using AWord; +global using ZWord; diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsGlobalSystemToTop.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsGlobalSystemToTop.test new file mode 100644 index 000000000..653fdeff5 --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsGlobalSystemToTop.test @@ -0,0 +1,4 @@ +global using ZWord; +global using AWord; +global using System.Web; +global using System; diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsMixedCorrectly.expected.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsMixedCorrectly.expected.test new file mode 100644 index 000000000..2ce2ff17b --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsMixedCorrectly.expected.test @@ -0,0 +1,8 @@ +global using System; +global using System.Web; +global using AWord; +global using ZWord; +using System; +using System.Web; +using AWord; +using ZWord; diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsMixedCorrectly.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsMixedCorrectly.test new file mode 100644 index 000000000..622e2a3be --- /dev/null +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/UsingDirectives_SortsMixedCorrectly.test @@ -0,0 +1,8 @@ +global using ZWord; +using ZWord; +global using AWord; +using AWord; +global using System.Web; +using System.Web; +global using System; +using System; diff --git a/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs b/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs index 1d2f7e5f1..2406b53a3 100644 --- a/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs +++ b/Src/CSharpier/SyntaxPrinter/UsingDirectives.cs @@ -134,6 +134,7 @@ private static IEnumerable> GroupUsings( FormattingContext context ) { + var globalSystemUsings = new List(); var globalUsings = new List(); var globalAliasUsings = new List(); var systemUsings = new List(); @@ -187,9 +188,18 @@ FormattingContext context if (usingDirective.GlobalKeyword.RawSyntaxKind() != SyntaxKind.None) { - (usingDirective.Alias is not null ? globalAliasUsings : globalUsings).Add( - usingData - ); + if (usingDirective.Alias is not null) + { + globalAliasUsings.Add(usingData); + } + else if (usingDirective.Name is not null && IsSystemName(usingDirective.Name)) + { + globalSystemUsings.Add(usingData); + } + else + { + globalUsings.Add(usingData); + } } else if (usingDirective.StaticKeyword.RawSyntaxKind() != SyntaxKind.None) { @@ -214,6 +224,7 @@ FormattingContext context } } + yield return globalSystemUsings.OrderBy(o => o.Using, Comparer).ToList(); 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();