@@ -120,9 +120,7 @@ private static bool SortIncludeBatch(FormatterOptionsPage settings, string[] pre
120120 List < IncludeLineInfo > outSortedList , IEnumerable < IncludeLineInfo > includeBatch )
121121 {
122122 // Get enumerator and cancel if batch is empty.
123- var originalLineEnumerator = includeBatch . GetEnumerator ( ) ;
124- bool hasElements = originalLineEnumerator . MoveNext ( ) ;
125- if ( ! hasElements )
123+ if ( ! includeBatch . Any ( ) )
126124 return false ;
127125
128126 // Fetch settings.
@@ -178,41 +176,40 @@ private static bool SortIncludeBatch(FormatterOptionsPage settings, string[] pre
178176 else if ( typeSorting == FormatterOptionsPage . TypeSorting . QuotedFirst )
179177 sortedIncludes = sortedIncludes . OrderBy ( x => x . LineDelimiterType == IncludeLineInfo . DelimiterType . Quotes ? 0 : 1 ) ;
180178
181- // Finally, update the actual lines
179+ // Merge sorted includes with original non-include lines
180+ var sortedIncludeEnumerator = sortedIncludes . GetEnumerator ( ) ;
181+ var sortedLines = includeBatch . Select ( originalLine =>
182182 {
183- bool firstLine = true ;
184-
185- foreach ( var sortedLine in sortedIncludes )
183+ if ( originalLine . ContainsActiveInclude )
186184 {
187- // Advance until there is an include line to replace. There *must* be one left if sortedIncludes is not empty.
188- while ( ! originalLineEnumerator . Current . ContainsActiveInclude )
189- {
190- outSortedList . Add ( originalLineEnumerator . Current ) ;
191- hasElements = originalLineEnumerator . MoveNext ( ) ;
192- System . Diagnostics . Debug . Assert ( hasElements , "There must be an element left in the original list if there are still sorted elements to put back in." ) ;
193- }
185+ // Replace original include with sorted includes
186+ return sortedIncludeEnumerator . MoveNext ( ) ? sortedIncludeEnumerator . Current : new IncludeLineInfo ( ) ;
187+ }
188+ return originalLine ;
189+ } ) ;
194190
195- bool isLastLine = ! originalLineEnumerator . MoveNext ( ) ;
191+ if ( settings . RemoveEmptyLines )
192+ {
193+ // Removing duplicates may have introduced new empty lines
194+ sortedLines = sortedLines . Where ( sortedLine => ! string . IsNullOrWhiteSpace ( sortedLine . RawLine ) ) ;
195+ }
196196
197+ // Finally, update the actual lines
198+ {
199+ bool firstLine = true ;
200+ foreach ( var sortedLine in sortedLines )
201+ {
197202 // Handle prepending a newline if requested, as long as:
198203 // - this include is the begin of a new group
199204 // - it's not the first line
200- // - it's not the last line of the batch.
201205 // - the previous line isn't already a non-include
202- if ( groupStarts . Contains ( sortedLine ) && ! firstLine && ! isLastLine && outSortedList [ outSortedList . Count - 1 ] . ContainsActiveInclude )
206+ if ( groupStarts . Contains ( sortedLine ) && ! firstLine && outSortedList [ outSortedList . Count - 1 ] . ContainsActiveInclude )
203207 {
204208 outSortedList . Add ( new IncludeLineInfo ( ) ) ;
205209 }
206210 outSortedList . Add ( sortedLine ) ;
207211 firstLine = false ;
208212 }
209-
210- while ( hasElements )
211- {
212- if ( ! originalLineEnumerator . Current . ContainsActiveInclude )
213- outSortedList . Add ( originalLineEnumerator . Current ) ;
214- hasElements = originalLineEnumerator . MoveNext ( ) ;
215- }
216213 }
217214
218215 return true ;
0 commit comments