@@ -27,7 +27,8 @@ namespace StyleCop.Analyzers.OrderingRules
2727 internal sealed class UsingCodeFixProvider : CodeFixProvider
2828 {
2929 private static readonly List < UsingDirectiveSyntax > EmptyUsingsList = new List < UsingDirectiveSyntax > ( ) ;
30- private static readonly SyntaxAnnotation UsingCodeFixAnnotation = new SyntaxAnnotation ( nameof ( UsingCodeFixProvider ) ) ;
30+ private static readonly SyntaxAnnotation UsingCodeFixAnnotation = new SyntaxAnnotation ( nameof ( UsingCodeFixAnnotation ) ) ;
31+ private static readonly SyntaxAnnotation FileHeaderStrippedAnnotation = new SyntaxAnnotation ( nameof ( FileHeaderStrippedAnnotation ) ) ;
3132
3233 /// <inheritdoc/>
3334 public override ImmutableArray < string > FixableDiagnosticIds { get ; } =
@@ -175,6 +176,13 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
175176
176177 private static SyntaxNode ReAddFileHeader ( SyntaxNode syntaxRoot , SyntaxNode newSyntaxRoot )
177178 {
179+ // Only re-add the file header if it was stripped.
180+ var usingDirectives = newSyntaxRoot . GetAnnotatedNodes ( FileHeaderStrippedAnnotation ) ;
181+ if ( ! usingDirectives . Any ( ) )
182+ {
183+ return newSyntaxRoot ;
184+ }
185+
178186 var oldFirstToken = syntaxRoot . GetFirstToken ( ) ;
179187 if ( ! oldFirstToken . HasLeadingTrivia )
180188 {
@@ -584,12 +592,6 @@ internal static List<SyntaxTrivia> GetFileHeader(SyntaxTriviaList newLeadingTriv
584592 return hasHeader ? fileHeader : new List < SyntaxTrivia > ( ) ;
585593 }
586594
587- private static List < SyntaxTrivia > StripFileHeader ( SyntaxTriviaList leadingTrivia )
588- {
589- var fileHeader = GetFileHeader ( leadingTrivia ) ;
590- return leadingTrivia . Skip ( fileHeader . Count ) . ToList ( ) ;
591- }
592-
593595 private List < UsingDirectiveSyntax > GenerateUsings ( Dictionary < DirectiveSpan , List < UsingDirectiveSyntax > > usingsGroup , DirectiveSpan directiveSpan , string indentation , List < SyntaxTrivia > triviaToMove , bool qualifyNames )
594596 {
595597 List < UsingDirectiveSyntax > result = new List < UsingDirectiveSyntax > ( ) ;
@@ -614,15 +616,29 @@ private List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSyntax> usi
614616
615617 for ( var i = 0 ; i < usingsList . Count ; i ++ )
616618 {
619+ bool strippedFileHeader = false ;
617620 var currentUsing = usingsList [ i ] ;
618621
619622 if ( qualifyNames )
620623 {
621624 currentUsing = this . QualifyUsingDirective ( currentUsing ) ;
622625 }
623626
627+ // strip the file header, if the using is the first node in the source file.
628+ List < SyntaxTrivia > leadingTrivia ;
629+ if ( ( i == 0 ) && currentUsing . GetFirstToken ( ) . GetPreviousToken ( ) . IsMissingOrDefault ( ) )
630+ {
631+ var trivia = currentUsing . GetLeadingTrivia ( ) ;
632+ var fileHeader = GetFileHeader ( trivia ) ;
633+ leadingTrivia = trivia . Skip ( fileHeader . Count ) . ToList ( ) ;
634+ strippedFileHeader = fileHeader . Count > 0 ;
635+ }
636+ else
637+ {
638+ leadingTrivia = currentUsing . GetLeadingTrivia ( ) . ToList ( ) ;
639+ }
640+
624641 // when there is a directive trivia, add it (and any trivia before it) to the triviaToMove collection.
625- var leadingTrivia = ( i == 0 ) ? StripFileHeader ( currentUsing . GetLeadingTrivia ( ) ) : currentUsing . GetLeadingTrivia ( ) . ToList ( ) ;
626642 for ( var m = leadingTrivia . Count - 1 ; m >= 0 ; m -- )
627643 {
628644 if ( leadingTrivia [ m ] . IsDirective )
@@ -689,6 +705,11 @@ private List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSyntax> usi
689705 . WithTrailingTrivia ( newTrailingTrivia )
690706 . WithAdditionalAnnotations ( UsingCodeFixAnnotation ) ;
691707
708+ if ( strippedFileHeader )
709+ {
710+ processedUsing = processedUsing . WithAdditionalAnnotations ( FileHeaderStrippedAnnotation ) ;
711+ }
712+
692713 // filter duplicate using declarations, preferring to keep the one with an alias
693714 var existingUsing = result . Find ( u => string . Equals ( u . Name . ToNormalizedString ( ) , processedUsing . Name . ToNormalizedString ( ) , StringComparison . Ordinal ) ) ;
694715 if ( existingUsing != null )
0 commit comments