@@ -13,6 +13,7 @@ namespace StyleCop.Analyzers.OrderingRules
1313 using Microsoft . CodeAnalysis ;
1414 using Microsoft . CodeAnalysis . CSharp ;
1515 using Microsoft . CodeAnalysis . CSharp . Syntax ;
16+ using Microsoft . CodeAnalysis . Text ;
1617 using StyleCop . Analyzers . Helpers ;
1718 using StyleCop . Analyzers . Lightup ;
1819 using StyleCop . Analyzers . Settings . ObjectModel ;
@@ -92,29 +93,29 @@ public List<UsingDirectiveSyntax> GetContainedUsings(TreeTextSpan directiveSpan)
9293 return result ;
9394 }
9495
95- public SyntaxList < UsingDirectiveSyntax > GenerateGroupedUsings ( TreeTextSpan directiveSpan , string indentation , bool withLeadingBlankLine , bool withTrailingBlankLine , bool qualifyNames , bool includeGlobal , bool includeLocal )
96+ public SyntaxList < UsingDirectiveSyntax > GenerateGroupedUsings ( TreeTextSpan directiveSpan , string indentation , SyntaxTrivia endOfLine , bool withLeadingBlankLine , bool withTrailingBlankLine , bool qualifyNames , bool includeGlobal , bool includeLocal )
9697 {
9798 var usingList = new List < UsingDirectiveSyntax > ( ) ;
9899 List < SyntaxTrivia > triviaToMove = new List < SyntaxTrivia > ( ) ;
99100 int lastGlobalDirective = - 1 ;
100101
101102 if ( includeGlobal )
102103 {
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 ) ) ;
104+ usingList . AddRange ( this . GenerateUsings ( this . systemUsings , directiveSpan , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : true ) ) ;
105+ usingList . AddRange ( this . GenerateUsings ( this . namespaceUsings , directiveSpan , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : true ) ) ;
106+ usingList . AddRange ( this . GenerateUsings ( this . systemStaticImports , directiveSpan , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : true ) ) ;
107+ usingList . AddRange ( this . GenerateUsings ( this . staticImports , directiveSpan , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : true ) ) ;
108+ usingList . AddRange ( this . GenerateUsings ( this . aliases , directiveSpan , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : true ) ) ;
108109 lastGlobalDirective = usingList . Count - 1 ;
109110 }
110111
111112 if ( includeLocal )
112113 {
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 ) ) ;
114+ usingList . AddRange ( this . GenerateUsings ( this . systemUsings , directiveSpan , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : false ) ) ;
115+ usingList . AddRange ( this . GenerateUsings ( this . namespaceUsings , directiveSpan , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : false ) ) ;
116+ usingList . AddRange ( this . GenerateUsings ( this . systemStaticImports , directiveSpan , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : false ) ) ;
117+ usingList . AddRange ( this . GenerateUsings ( this . staticImports , directiveSpan , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : false ) ) ;
118+ usingList . AddRange ( this . GenerateUsings ( this . aliases , directiveSpan , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : false ) ) ;
118119 }
119120
120121 if ( ! this . insertBlankLinesBetweenGroups && lastGlobalDirective >= 0 && lastGlobalDirective < usingList . Count - 1 )
@@ -123,7 +124,7 @@ public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(TreeTextSpan direc
123124 // usings
124125 var last = usingList [ lastGlobalDirective ] ;
125126
126- usingList [ lastGlobalDirective ] = last . WithTrailingTrivia ( last . GetTrailingTrivia ( ) . Add ( SyntaxFactory . CarriageReturnLineFeed ) ) ;
127+ usingList [ lastGlobalDirective ] = last . WithTrailingTrivia ( last . GetTrailingTrivia ( ) . Add ( endOfLine ) ) ;
127128 }
128129
129130 if ( triviaToMove . Count > 0 )
@@ -135,43 +136,43 @@ public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(TreeTextSpan direc
135136 if ( withLeadingBlankLine && usingList . Count > 0 )
136137 {
137138 var firstUsing = usingList [ 0 ] ;
138- usingList [ 0 ] = firstUsing . WithLeadingTrivia ( firstUsing . GetLeadingTrivia ( ) . Insert ( 0 , SyntaxFactory . CarriageReturnLineFeed ) ) ;
139+ usingList [ 0 ] = firstUsing . WithLeadingTrivia ( firstUsing . GetLeadingTrivia ( ) . Insert ( 0 , endOfLine ) ) ;
139140 }
140141
141142 if ( withTrailingBlankLine && ( usingList . Count > 0 ) )
142143 {
143144 var lastUsing = usingList [ usingList . Count - 1 ] ;
144- usingList [ usingList . Count - 1 ] = lastUsing . WithTrailingTrivia ( lastUsing . GetTrailingTrivia ( ) . Add ( SyntaxFactory . CarriageReturnLineFeed ) ) ;
145+ usingList [ usingList . Count - 1 ] = lastUsing . WithTrailingTrivia ( lastUsing . GetTrailingTrivia ( ) . Add ( endOfLine ) ) ;
145146 }
146147
147148 return SyntaxFactory . List ( usingList ) ;
148149 }
149150
150- public SyntaxList < UsingDirectiveSyntax > GenerateGroupedUsings ( List < UsingDirectiveSyntax > usingsList , string indentation , bool withLeadingBlankLine , bool withTrailingBlankLine , bool qualifyNames )
151+ public SyntaxList < UsingDirectiveSyntax > GenerateGroupedUsings ( List < UsingDirectiveSyntax > usingsList , string indentation , SyntaxTrivia endOfLine , bool withLeadingBlankLine , bool withTrailingBlankLine , bool qualifyNames )
151152 {
152153 var usingList = new List < UsingDirectiveSyntax > ( ) ;
153154 List < SyntaxTrivia > triviaToMove = new List < SyntaxTrivia > ( ) ;
154155
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 ) ) ;
156+ usingList . AddRange ( this . GenerateUsings ( this . systemUsings , usingsList , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : true ) ) ;
157+ usingList . AddRange ( this . GenerateUsings ( this . namespaceUsings , usingsList , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : true ) ) ;
158+ usingList . AddRange ( this . GenerateUsings ( this . systemStaticImports , usingsList , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : true ) ) ;
159+ usingList . AddRange ( this . GenerateUsings ( this . staticImports , usingsList , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : true ) ) ;
160+ usingList . AddRange ( this . GenerateUsings ( this . aliases , usingsList , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : true ) ) ;
160161 int lastGlobalDirective = usingList . Count - 1 ;
161162
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 ) ) ;
163+ usingList . AddRange ( this . GenerateUsings ( this . systemUsings , usingsList , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : false ) ) ;
164+ usingList . AddRange ( this . GenerateUsings ( this . namespaceUsings , usingsList , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : false ) ) ;
165+ usingList . AddRange ( this . GenerateUsings ( this . systemStaticImports , usingsList , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : false ) ) ;
166+ usingList . AddRange ( this . GenerateUsings ( this . staticImports , usingsList , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : false ) ) ;
167+ usingList . AddRange ( this . GenerateUsings ( this . aliases , usingsList , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal : false ) ) ;
167168
168169 if ( ! this . insertBlankLinesBetweenGroups && lastGlobalDirective >= 0 && lastGlobalDirective < usingList . Count - 1 )
169170 {
170171 // Need to ensure there is a blank line after the global usings so they are separated from the local
171172 // usings
172173 var last = usingList [ lastGlobalDirective ] ;
173174
174- usingList [ lastGlobalDirective ] = last . WithTrailingTrivia ( last . GetTrailingTrivia ( ) . Add ( SyntaxFactory . CarriageReturnLineFeed ) ) ;
175+ usingList [ lastGlobalDirective ] = last . WithTrailingTrivia ( last . GetTrailingTrivia ( ) . Add ( endOfLine ) ) ;
175176 }
176177
177178 if ( triviaToMove . Count > 0 )
@@ -183,19 +184,19 @@ public SyntaxList<UsingDirectiveSyntax> GenerateGroupedUsings(List<UsingDirectiv
183184 if ( withLeadingBlankLine && usingList . Count > 0 )
184185 {
185186 var firstUsing = usingList [ 0 ] ;
186- usingList [ 0 ] = firstUsing . WithLeadingTrivia ( firstUsing . GetLeadingTrivia ( ) . Insert ( 0 , SyntaxFactory . CarriageReturnLineFeed ) ) ;
187+ usingList [ 0 ] = firstUsing . WithLeadingTrivia ( firstUsing . GetLeadingTrivia ( ) . Insert ( 0 , endOfLine ) ) ;
187188 }
188189
189190 if ( withTrailingBlankLine && ( usingList . Count > 0 ) )
190191 {
191192 var lastUsing = usingList [ usingList . Count - 1 ] ;
192- usingList [ usingList . Count - 1 ] = lastUsing . WithTrailingTrivia ( lastUsing . GetTrailingTrivia ( ) . Add ( SyntaxFactory . CarriageReturnLineFeed ) ) ;
193+ usingList [ usingList . Count - 1 ] = lastUsing . WithTrailingTrivia ( lastUsing . GetTrailingTrivia ( ) . Add ( endOfLine ) ) ;
193194 }
194195
195196 return SyntaxFactory . List ( usingList ) ;
196197 }
197198
198- private List < UsingDirectiveSyntax > GenerateUsings ( Dictionary < TreeTextSpan , List < UsingDirectiveSyntax > > usingsGroup , TreeTextSpan directiveSpan , string indentation , List < SyntaxTrivia > triviaToMove , bool qualifyNames , bool isGlobal )
199+ private List < UsingDirectiveSyntax > GenerateUsings ( Dictionary < TreeTextSpan , List < UsingDirectiveSyntax > > usingsGroup , TreeTextSpan directiveSpan , string indentation , SyntaxTrivia endOfLine , List < SyntaxTrivia > triviaToMove , bool qualifyNames , bool isGlobal )
199200 {
200201 List < UsingDirectiveSyntax > result = new List < UsingDirectiveSyntax > ( ) ;
201202 List < UsingDirectiveSyntax > usingsList ;
@@ -205,10 +206,10 @@ private List<UsingDirectiveSyntax> GenerateUsings(Dictionary<TreeTextSpan, List<
205206 return result ;
206207 }
207208
208- return this . GenerateUsings ( usingsList , indentation , triviaToMove , qualifyNames , isGlobal ) ;
209+ return this . GenerateUsings ( usingsList , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal ) ;
209210 }
210211
211- private List < UsingDirectiveSyntax > GenerateUsings ( List < UsingDirectiveSyntax > usingsList , string indentation , List < SyntaxTrivia > triviaToMove , bool qualifyNames , bool isGlobal )
212+ private List < UsingDirectiveSyntax > GenerateUsings ( List < UsingDirectiveSyntax > usingsList , string indentation , SyntaxTrivia endOfLine , List < SyntaxTrivia > triviaToMove , bool qualifyNames , bool isGlobal )
212213 {
213214 List < UsingDirectiveSyntax > result = new List < UsingDirectiveSyntax > ( ) ;
214215
@@ -365,7 +366,7 @@ private List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSyntax> usi
365366 var newTrailingTrivia = currentTrailingTrivia ;
366367 if ( ! currentTrailingTrivia . Any ( ) || ! currentTrailingTrivia . Last ( ) . IsKind ( SyntaxKind . EndOfLineTrivia ) )
367368 {
368- newTrailingTrivia = newTrailingTrivia . Add ( SyntaxFactory . CarriageReturnLineFeed ) ;
369+ newTrailingTrivia = newTrailingTrivia . Add ( endOfLine ) ;
369370 }
370371
371372 var processedUsing = ( qualifyNames ? this . QualifyUsingDirective ( currentUsing ) : currentUsing )
@@ -382,7 +383,7 @@ private List<UsingDirectiveSyntax> GenerateUsings(List<UsingDirectiveSyntax> usi
382383 {
383384 var last = result [ result . Count - 1 ] ;
384385
385- result [ result . Count - 1 ] = last . WithTrailingTrivia ( last . GetTrailingTrivia ( ) . Add ( SyntaxFactory . CarriageReturnLineFeed ) ) ;
386+ result [ result . Count - 1 ] = last . WithTrailingTrivia ( last . GetTrailingTrivia ( ) . Add ( endOfLine ) ) ;
386387 }
387388
388389 return result ;
@@ -576,11 +577,11 @@ private void AddUsingDirective(Dictionary<TreeTextSpan, List<UsingDirectiveSynta
576577 usingList . Add ( usingDirective ) ;
577578 }
578579
579- private List < UsingDirectiveSyntax > GenerateUsings ( Dictionary < TreeTextSpan , List < UsingDirectiveSyntax > > usingsGroup , List < UsingDirectiveSyntax > usingsList , string indentation , List < SyntaxTrivia > triviaToMove , bool qualifyNames , bool isGlobal )
580+ private List < UsingDirectiveSyntax > GenerateUsings ( Dictionary < TreeTextSpan , List < UsingDirectiveSyntax > > usingsGroup , List < UsingDirectiveSyntax > usingsList , string indentation , SyntaxTrivia endOfLine , List < SyntaxTrivia > triviaToMove , bool qualifyNames , bool isGlobal )
580581 {
581582 var filteredUsingsList = this . FilterRelevantUsings ( usingsGroup , usingsList ) ;
582583
583- return this . GenerateUsings ( filteredUsingsList , indentation , triviaToMove , qualifyNames , isGlobal ) ;
584+ return this . GenerateUsings ( filteredUsingsList , indentation , endOfLine , triviaToMove , qualifyNames , isGlobal ) ;
584585 }
585586
586587 private List < UsingDirectiveSyntax > FilterRelevantUsings ( Dictionary < TreeTextSpan , List < UsingDirectiveSyntax > > usingsGroup , List < UsingDirectiveSyntax > usingsList )
0 commit comments