Skip to content

Commit 881d9a4

Browse files
committed
Fix inconsistency with IDE for sorting usings for Windows.* namespaces when system using directives first is enabled
1 parent 7098339 commit 881d9a4

3 files changed

Lines changed: 10 additions & 2 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.UsingsSorter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ private bool IsSeparatedSystemUsing(UsingDirectiveSyntax syntax)
438438
var namespaceTypeName = namespaceSymbol.ToDisplayString(FullNamespaceDisplayFormat);
439439
var firstPart = namespaceTypeName.Split('.')[0];
440440

441-
return string.Equals(SystemUsingDirectiveIdentifier, firstPart, StringComparison.Ordinal);
441+
return string.Equals(SystemUsingDirectiveIdentifier, firstPart, StringComparison.Ordinal) || string.Equals(WindowsUsingDirectiveIdentifier, firstPart, StringComparison.Ordinal);
442442
}
443443

444444
private void ProcessMembers(SyntaxList<MemberDeclarationSyntax> members)

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/OrderingRules/UsingCodeFixProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace StyleCop.Analyzers.OrderingRules
2626
internal sealed partial class UsingCodeFixProvider : CodeFixProvider
2727
{
2828
private const string SystemUsingDirectiveIdentifier = nameof(System);
29+
private const string WindowsUsingDirectiveIdentifier = "Windows";
2930

3031
private static readonly List<UsingDirectiveSyntax> EmptyUsingsList = new List<UsingDirectiveSyntax>();
3132
private static readonly SyntaxAnnotation UsingCodeFixAnnotation = new SyntaxAnnotation(nameof(UsingCodeFixAnnotation));

StyleCop.Analyzers/StyleCop.Analyzers/Helpers/UsingDirectiveSyntaxHelpers.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ namespace StyleCop.Analyzers.Helpers
1616
internal static class UsingDirectiveSyntaxHelpers
1717
{
1818
private const string SystemUsingDirectiveIdentifier = nameof(System);
19+
private const string WindowsUsingDirectiveIdentifier = "Windows";
1920

2021
/// <summary>
2122
/// Check if <see cref="UsingDirectiveSyntax"/> is system using directive.
2223
/// </summary>
2324
/// <param name="usingDirective">The <see cref="UsingDirectiveSyntax"/> that will be checked.</param>
2425
/// <returns>Return true if the <see cref="UsingDirectiveSyntax"/>is system using directive, otherwise false.</returns>
25-
internal static bool IsSystemUsingDirective(this UsingDirectiveSyntax usingDirective) => string.Equals(SystemUsingDirectiveIdentifier, GetFirstIdentifierInUsingDirective(usingDirective)?.ValueText, StringComparison.Ordinal);
26+
internal static bool IsSystemUsingDirective(this UsingDirectiveSyntax usingDirective)
27+
{
28+
string firstIdentifier = GetFirstIdentifierInUsingDirective(usingDirective)?.ValueText;
29+
30+
// Visual Studio treats Windows.* namespaces as system namespaces.
31+
return string.Equals(SystemUsingDirectiveIdentifier, firstIdentifier, StringComparison.Ordinal) || string.Equals(WindowsUsingDirectiveIdentifier, firstIdentifier, StringComparison.Ordinal);
32+
}
2633

2734
/// <summary>
2835
/// Check if <see cref="UsingDirectiveSyntax"/> is preceded by a preprocessor directive.

0 commit comments

Comments
 (0)