From 39588258997a52a7fc2ebbe86e51693fe3896282 Mon Sep 17 00:00:00 2001 From: Dakota Hawkins Date: Tue, 26 Sep 2017 15:06:29 -0400 Subject: [PATCH] Remove check that prevents adding newlines before the last line de88fd1c added a check to prevent adding a newline before the last line in a batch. Since we're prepending, however, a newline is desirable if the last line is the only one in its group. Fixes #48 --- IncludeToolbox.vsix | 4 +- IncludeToolbox/Formatter/IncludeFormatter.cs | 45 +++++++++---------- .../Package/source.extension.vsixmanifest | 2 +- Tests/IncludeFormatingTest.cs | 21 ++++++++- 4 files changed, 43 insertions(+), 29 deletions(-) diff --git a/IncludeToolbox.vsix b/IncludeToolbox.vsix index 8454b46..1a1e168 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:50ac3952952fee7141aabb2a03e9cfd968a164a403c8d86529748c7df89d545f +size 128269 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/Package/source.extension.vsixmanifest b/IncludeToolbox/Package/source.extension.vsixmanifest index 63c64ba..3dade45 100644 --- a/IncludeToolbox/Package/source.extension.vsixmanifest +++ b/IncludeToolbox/Package/source.extension.vsixmanifest @@ -1,7 +1,7 @@  - + IncludeToolbox Various tools for managing C/C++ #includes: Formatting, sorting, exploring, pruning. license.txt diff --git a/Tests/IncludeFormatingTest.cs b/Tests/IncludeFormatingTest.cs index 70e20a1..c6953ae 100644 --- a/Tests/IncludeFormatingTest.cs +++ b/Tests/IncludeFormatingTest.cs @@ -46,7 +46,11 @@ public void Sorting_BlanksAfterRegexGroup() #include ""a.h"" #include -#include "; +#include + + + +"; string expectedFormatedCode_WithBlanks = @"#include ""filename.h"" @@ -56,7 +60,11 @@ public void Sorting_BlanksAfterRegexGroup() #include ""c_third"" #include ""z_first"" + // A comment + + + "; @@ -87,7 +95,11 @@ public void Sorting_AngleBracketsFirst() #include #include #include ""filename.h"" -#include ""a.h"""; +#include ""a.h"" + + + +"; string expectedFormatedCode_WithBlanks = @@ -98,7 +110,11 @@ public void Sorting_AngleBracketsFirst() #include ""c_third"" #include ""z_first"" + // A comment + + + "; @@ -244,6 +260,7 @@ public void OtherPreprocessorDirectives() // A comment #include #include ""a9"" + #else #include