Skip to content

Commit

Permalink
Fixing some edge cases with sorting usings (#984)
Browse files Browse the repository at this point in the history
* Tracking down bugs with sorting usings

closes #981

* Fixing the edge cases
  • Loading branch information
belav authored Nov 6, 2023
1 parent dd1594e commit 2621892
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;
using static SomeOtherNamespace;
using SomeAlias = SomeNamespace;
#if DEBUG
using SomeDebugNamespace;
#endif

public static class ODataEdmModelProvider { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
global using Nest7;
global using Newtonsoft.Json;
global using Nest = Nest7;
10 changes: 8 additions & 2 deletions Src/CSharpier/SyntaxPrinter/UsingDirectives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ FormattingContext context
)
{
var globalUsings = new List<UsingData>();
var globalAliasUsings = new List<UsingData>();
var systemUsings = new List<UsingData>();
var aliasNameUsings = new List<UsingData>();
var regularUsings = new List<UsingData>();
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down

0 comments on commit 2621892

Please sign in to comment.