@@ -92,16 +92,39 @@ public List<UsingDirectiveSyntax> GetContainedUsings(TreeTextSpan directiveSpan)
9292 return result ;
9393 }
9494
95- public SyntaxList < UsingDirectiveSyntax > GenerateGroupedUsings ( TreeTextSpan directiveSpan , string indentation , bool withLeadingBlankLine , bool withTrailingBlankLine , bool qualifyNames )
95+ public SyntaxList < UsingDirectiveSyntax > GenerateGroupedUsings ( TreeTextSpan directiveSpan , string indentation , bool withLeadingBlankLine , bool withTrailingBlankLine , bool qualifyNames , bool includeGlobal , bool includeLocal )
9696 {
9797 var usingList = new List < UsingDirectiveSyntax > ( ) ;
9898 List < SyntaxTrivia > triviaToMove = new List < SyntaxTrivia > ( ) ;
99+ int lastGlobalDirective = - 1 ;
99100
100- usingList . AddRange ( this . GenerateUsings ( this . systemUsings , directiveSpan , indentation , triviaToMove , qualifyNames ) ) ;
101- usingList . AddRange ( this . GenerateUsings ( this . namespaceUsings , directiveSpan , indentation , triviaToMove , qualifyNames ) ) ;
102- usingList . AddRange ( this . GenerateUsings ( this . systemStaticImports , directiveSpan , indentation , triviaToMove , qualifyNames ) ) ;
103- usingList . AddRange ( this . GenerateUsings ( this . staticImports , directiveSpan , indentation , triviaToMove , qualifyNames ) ) ;
104- usingList . AddRange ( this . GenerateUsings ( this . aliases , directiveSpan , indentation , triviaToMove , qualifyNames ) ) ;
101+ if ( includeGlobal )
102+ {
103+ usingList . AddRange ( this . GenerateUsings ( this . systemUsings , directiveSpan , indentation , triviaToMove , qualifyNames , isGlobal : true ) ) ;
104+ usingList . AddRange ( this . GenerateUsings ( this . namespaceUsings , directiveSpan , indentation , triviaToMove , qualifyNames , isGlobal : true ) ) ;
105+ usingList . AddRange ( this . GenerateUsings ( this . systemStaticImports , directiveSpan , indentation , triviaToMove , qualifyNames , isGlobal : true ) ) ;
106+ usingList . AddRange ( this . GenerateUsings ( this . staticImports , directiveSpan , indentation , triviaToMove , qualifyNames , isGlobal : true ) ) ;
107+ usingList . AddRange ( this . GenerateUsings ( this . aliases , directiveSpan , indentation , triviaToMove , qualifyNames , isGlobal : true ) ) ;
108+ lastGlobalDirective = usingList . Count - 1 ;
109+ }
110+
111+ if ( includeLocal )
112+ {
113+ usingList . AddRange ( this . GenerateUsings ( this . systemUsings , directiveSpan , indentation , triviaToMove , qualifyNames , isGlobal : false ) ) ;
114+ usingList . AddRange ( this . GenerateUsings ( this . namespaceUsings , directiveSpan , indentation , triviaToMove , qualifyNames , isGlobal : false ) ) ;
115+ usingList . AddRange ( this . GenerateUsings ( this . systemStaticImports , directiveSpan , indentation , triviaToMove , qualifyNames , isGlobal : false ) ) ;
116+ usingList . AddRange ( this . GenerateUsings ( this . staticImports , directiveSpan , indentation , triviaToMove , qualifyNames , isGlobal : false ) ) ;
117+ usingList . AddRange ( this . GenerateUsings ( this . aliases , directiveSpan , indentation , triviaToMove , qualifyNames , isGlobal : false ) ) ;
118+ }
119+
120+ if ( ! this . insertBlankLinesBetweenGroups && lastGlobalDirective >= 0 && lastGlobalDirective < usingList . Count - 1 )
121+ {
122+ // Need to ensure there is a blank line after the global usings so they are separated from the local
123+ // usings
124+ var last = usingList [ lastGlobalDirective ] ;
125+
126+ usingList [ lastGlobalDirective ] = last . WithTrailingTrivia ( last . GetTrailingTrivia ( ) . Add ( SyntaxFactory . CarriageReturnLineFeed ) ) ;
127+ }
105128
106129 if ( triviaToMove . Count > 0 )
107130 {
@@ -129,11 +152,27 @@ public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(List<UsingDirectiv
129152 var usingList = new List < UsingDirectiveSyntax > ( ) ;
130153 List < SyntaxTrivia > triviaToMove = new List < SyntaxTrivia > ( ) ;
131154
132- usingList . AddRange ( this . GenerateUsings ( this . systemUsings , usingsList , indentation , triviaToMove , qualifyNames ) ) ;
133- usingList . AddRange ( this . GenerateUsings ( this . namespaceUsings , usingsList , indentation , triviaToMove , qualifyNames ) ) ;
134- usingList . AddRange ( this . GenerateUsings ( this . systemStaticImports , usingsList , indentation , triviaToMove , qualifyNames ) ) ;
135- usingList . AddRange ( this . GenerateUsings ( this . staticImports , usingsList , indentation , triviaToMove , qualifyNames ) ) ;
136- usingList . AddRange ( this . GenerateUsings ( this . aliases , usingsList , indentation , triviaToMove , qualifyNames ) ) ;
155+ usingList . AddRange ( this . GenerateUsings ( this . systemUsings , usingsList , indentation , triviaToMove , qualifyNames , isGlobal : true ) ) ;
156+ usingList . AddRange ( this . GenerateUsings ( this . namespaceUsings , usingsList , indentation , triviaToMove , qualifyNames , isGlobal : true ) ) ;
157+ usingList . AddRange ( this . GenerateUsings ( this . systemStaticImports , usingsList , indentation , triviaToMove , qualifyNames , isGlobal : true ) ) ;
158+ usingList . AddRange ( this . GenerateUsings ( this . staticImports , usingsList , indentation , triviaToMove , qualifyNames , isGlobal : true ) ) ;
159+ usingList . AddRange ( this . GenerateUsings ( this . aliases , usingsList , indentation , triviaToMove , qualifyNames , isGlobal : true ) ) ;
160+ int lastGlobalDirective = usingList . Count - 1 ;
161+
162+ usingList . AddRange ( this . GenerateUsings ( this . systemUsings , usingsList , indentation , triviaToMove , qualifyNames , isGlobal : false ) ) ;
163+ usingList . AddRange ( this . GenerateUsings ( this . namespaceUsings , usingsList , indentation , triviaToMove , qualifyNames , isGlobal : false ) ) ;
164+ usingList . AddRange ( this . GenerateUsings ( this . systemStaticImports , usingsList , indentation , triviaToMove , qualifyNames , isGlobal : false ) ) ;
165+ usingList . AddRange ( this . GenerateUsings ( this . staticImports , usingsList , indentation , triviaToMove , qualifyNames , isGlobal : false ) ) ;
166+ usingList . AddRange ( this . GenerateUsings ( this . aliases , usingsList , indentation , triviaToMove , qualifyNames , isGlobal : false ) ) ;
167+
168+ if ( ! this . insertBlankLinesBetweenGroups && lastGlobalDirective >= 0 && lastGlobalDirective < usingList . Count - 1 )
169+ {
170+ // Need to ensure there is a blank line after the global usings so they are separated from the local
171+ // usings
172+ var last = usingList [ lastGlobalDirective ] ;
173+
174+ usingList [ lastGlobalDirective ] = last . WithTrailingTrivia ( last . GetTrailingTrivia ( ) . Add ( SyntaxFactory . CarriageReturnLineFeed ) ) ;
175+ }
137176
138177 if ( triviaToMove . Count > 0 )
139178 {
@@ -156,7 +195,7 @@ public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(List<UsingDirectiv
156195 return SyntaxFactory . List ( usingList ) ;
157196 }
158197
159- private List < UsingDirectiveSyntax > GenerateUsings ( Dictionary < TreeTextSpan , List < UsingDirectiveSyntax > > usingsGroup , TreeTextSpan directiveSpan , string indentation , List < SyntaxTrivia > triviaToMove , bool qualifyNames )
198+ private List < UsingDirectiveSyntax > GenerateUsings ( Dictionary < TreeTextSpan , List < UsingDirectiveSyntax > > usingsGroup , TreeTextSpan directiveSpan , string indentation , List < SyntaxTrivia > triviaToMove , bool qualifyNames , bool isGlobal )
160199 {
161200 List < UsingDirectiveSyntax > result = new List < UsingDirectiveSyntax > ( ) ;
162201 List < UsingDirectiveSyntax > usingsList ;
@@ -166,10 +205,10 @@ private List<UsingDirectiveSyntax> GenerateUsings(Dictionary<TreeTextSpan, List<
166205 return result ;
167206 }
168207
169- return this . GenerateUsings ( usingsList , indentation , triviaToMove , qualifyNames ) ;
208+ return this . GenerateUsings ( usingsList , indentation , triviaToMove , qualifyNames , isGlobal ) ;
170209 }
171210
172- private List < UsingDirectiveSyntax > GenerateUsings ( List < UsingDirectiveSyntax > usingsList , string indentation , List < SyntaxTrivia > triviaToMove , bool qualifyNames )
211+ private List < UsingDirectiveSyntax > GenerateUsings ( List < UsingDirectiveSyntax > usingsList , string indentation , List < SyntaxTrivia > triviaToMove , bool qualifyNames , bool isGlobal )
173212 {
174213 List < UsingDirectiveSyntax > result = new List < UsingDirectiveSyntax > ( ) ;
175214
@@ -181,6 +220,10 @@ private List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSyntax> usi
181220 for ( var i = 0 ; i < usingsList . Count ; i ++ )
182221 {
183222 var currentUsing = usingsList [ i ] ;
223+ if ( currentUsing . GlobalKeyword ( ) . IsKind ( SyntaxKind . GlobalKeyword ) != isGlobal )
224+ {
225+ continue ;
226+ }
184227
185228 // strip the file header, if the using is the first node in the source file.
186229 List < SyntaxTrivia > leadingTrivia ;
@@ -335,7 +378,7 @@ private List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSyntax> usi
335378
336379 result . Sort ( this . CompareUsings ) ;
337380
338- if ( this . insertBlankLinesBetweenGroups )
381+ if ( this . insertBlankLinesBetweenGroups && result . Count > 0 )
339382 {
340383 var last = result [ result . Count - 1 ] ;
341384
@@ -533,11 +576,11 @@ private void AddUsingDirective(Dictionary<TreeTextSpan, List<UsingDirectiveSynta
533576 usingList . Add ( usingDirective ) ;
534577 }
535578
536- private List < UsingDirectiveSyntax > GenerateUsings ( Dictionary < TreeTextSpan , List < UsingDirectiveSyntax > > usingsGroup , List < UsingDirectiveSyntax > usingsList , string indentation , List < SyntaxTrivia > triviaToMove , bool qualifyNames )
579+ private List < UsingDirectiveSyntax > GenerateUsings ( Dictionary < TreeTextSpan , List < UsingDirectiveSyntax > > usingsGroup , List < UsingDirectiveSyntax > usingsList , string indentation , List < SyntaxTrivia > triviaToMove , bool qualifyNames , bool isGlobal )
537580 {
538581 var filteredUsingsList = this . FilterRelevantUsings ( usingsGroup , usingsList ) ;
539582
540- return this . GenerateUsings ( filteredUsingsList , indentation , triviaToMove , qualifyNames ) ;
583+ return this . GenerateUsings ( filteredUsingsList , indentation , triviaToMove , qualifyNames , isGlobal ) ;
541584 }
542585
543586 private List < UsingDirectiveSyntax > FilterRelevantUsings ( Dictionary < TreeTextSpan , List < UsingDirectiveSyntax > > usingsGroup , List < UsingDirectiveSyntax > usingsList )
0 commit comments