diff --git a/IncludeToolbox.vsix b/IncludeToolbox.vsix index 8454b46..364942f 100644 --- a/IncludeToolbox.vsix +++ b/IncludeToolbox.vsix @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:77be8c5da4736b2179aefe8d63416b22e9669705abb3b737253894c7e5ce1d9d -size 127968 +oid sha256:f4ec4aaa3219ec043b34823548bbf7c624755ee153c69cc8fedf607070e63771 +size 128172 diff --git a/IncludeToolbox/Formatter/IncludeFormatter.cs b/IncludeToolbox/Formatter/IncludeFormatter.cs index b54763b..0b5f61f 100644 --- a/IncludeToolbox/Formatter/IncludeFormatter.cs +++ b/IncludeToolbox/Formatter/IncludeFormatter.cs @@ -120,9 +120,7 @@ private static bool SortIncludeBatch(FormatterOptionsPage settings, string[] pre List outSortedList, IEnumerable includeBatch) { // Get enumerator and cancel if batch is empty. - var originalLineEnumerator = includeBatch.GetEnumerator(); - bool hasElements = originalLineEnumerator.MoveNext(); - if (!hasElements) + if (!includeBatch.Any()) return false; // Fetch settings. @@ -178,41 +176,40 @@ private static bool SortIncludeBatch(FormatterOptionsPage settings, string[] pre else if (typeSorting == FormatterOptionsPage.TypeSorting.QuotedFirst) sortedIncludes = sortedIncludes.OrderBy(x => x.LineDelimiterType == IncludeLineInfo.DelimiterType.Quotes ? 0 : 1); - // Finally, update the actual lines + // Merge sorted includes with original non-include lines + var sortedIncludeEnumerator = sortedIncludes.GetEnumerator(); + var sortedLines = includeBatch.Select(originalLine => { - bool firstLine = true; - - foreach (var sortedLine in sortedIncludes) + if (originalLine.ContainsActiveInclude) { - // Advance until there is an include line to replace. There *must* be one left if sortedIncludes is not empty. - while (!originalLineEnumerator.Current.ContainsActiveInclude) - { - outSortedList.Add(originalLineEnumerator.Current); - hasElements = originalLineEnumerator.MoveNext(); - 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."); - } + // Replace original include with sorted includes + return sortedIncludeEnumerator.MoveNext() ? sortedIncludeEnumerator.Current : new IncludeLineInfo(); + } + return originalLine; + }); - bool isLastLine = !originalLineEnumerator.MoveNext(); + if (settings.RemoveEmptyLines) + { + // Removing duplicates may have introduced new empty lines + sortedLines = sortedLines.Where(sortedLine => !string.IsNullOrWhiteSpace(sortedLine.RawLine)); + } + // Finally, update the actual lines + { + bool firstLine = true; + foreach (var sortedLine in sortedLines) + { // Handle prepending a newline if requested, as long as: // - this include is the begin of a new group // - it's not the first line - // - it's not the last line of the batch. // - the previous line isn't already a non-include - if (groupStarts.Contains(sortedLine) && !firstLine && !isLastLine && outSortedList[outSortedList.Count - 1].ContainsActiveInclude) + if (groupStarts.Contains(sortedLine) && !firstLine && outSortedList[outSortedList.Count - 1].ContainsActiveInclude) { outSortedList.Add(new IncludeLineInfo()); } outSortedList.Add(sortedLine); firstLine = false; } - - while (hasElements) - { - if (!originalLineEnumerator.Current.ContainsActiveInclude) - outSortedList.Add(originalLineEnumerator.Current); - hasElements = originalLineEnumerator.MoveNext(); - } } return true; diff --git a/IncludeToolbox/IncludeToolbox.csproj b/IncludeToolbox/IncludeToolbox.csproj index 4896489..086fae4 100644 --- a/IncludeToolbox/IncludeToolbox.csproj +++ b/IncludeToolbox/IncludeToolbox.csproj @@ -143,7 +143,7 @@ True - ..\packages\Microsoft.VisualStudio.OLE.Interop.7.10.6070\lib\Microsoft.VisualStudio.OLE.Interop.dll + ..\packages\Microsoft.VisualStudio.OLE.Interop.7.10.6071\lib\Microsoft.VisualStudio.OLE.Interop.dll True @@ -191,14 +191,17 @@ ..\packages\Microsoft.VisualStudio.TextManager.Interop.8.0.8.0.50727\lib\Microsoft.VisualStudio.TextManager.Interop.8.0.dll True - - False - False - False + + ..\packages\Microsoft.VisualStudio.Threading.15.4.4\lib\net45\Microsoft.VisualStudio.Threading.dll + True - - False - False + + ..\packages\Microsoft.VisualStudio.Utilities.15.0.26607\lib\net46\Microsoft.VisualStudio.Utilities.dll + True + + + ..\packages\Microsoft.VisualStudio.Validation.15.3.32\lib\net45\Microsoft.VisualStudio.Validation.dll + True @@ -275,14 +278,13 @@ - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - +