Skip to content

Commit 3958825

Browse files
committed
Remove check that prevents adding newlines before the last line
de88fd1 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
1 parent 2779e83 commit 3958825

4 files changed

Lines changed: 43 additions & 29 deletions

File tree

IncludeToolbox.vsix

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:77be8c5da4736b2179aefe8d63416b22e9669705abb3b737253894c7e5ce1d9d
3-
size 127968
2+
oid sha256:50ac3952952fee7141aabb2a03e9cfd968a164a403c8d86529748c7df89d545f
3+
size 128269

IncludeToolbox/Formatter/IncludeFormatter.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

IncludeToolbox/Package/source.extension.vsixmanifest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
33
<Metadata>
4-
<Identity Id="IncludeToolbox.Andreas Reich.075c2e2b-7b71-45ba-b2e6-c1dadc81cfac" Version="2.2.0.1" Language="en-US" Publisher="Andreas Reich" />
4+
<Identity Id="IncludeToolbox.Andreas Reich.075c2e2b-7b71-45ba-b2e6-c1dadc81cfac" Version="2.2.1" Language="en-US" Publisher="Andreas Reich" />
55
<DisplayName>IncludeToolbox</DisplayName>
66
<Description xml:space="preserve">Various tools for managing C/C++ #includes: Formatting, sorting, exploring, pruning.</Description>
77
<License>license.txt</License>

Tests/IncludeFormatingTest.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public void Sorting_BlanksAfterRegexGroup()
4646
4747
#include ""a.h""
4848
#include <b.hpp>
49-
#include <c.hpp>";
49+
#include <c.hpp>
50+
51+
52+
53+
";
5054

5155
string expectedFormatedCode_WithBlanks =
5256
@"#include ""filename.h""
@@ -56,7 +60,11 @@ public void Sorting_BlanksAfterRegexGroup()
5660
#include ""c_third""
5761
5862
#include ""z_first""
63+
5964
// A comment
65+
66+
67+
6068
";
6169

6270

@@ -87,7 +95,11 @@ public void Sorting_AngleBracketsFirst()
8795
#include <b.hpp>
8896
#include <c.hpp>
8997
#include ""filename.h""
90-
#include ""a.h""";
98+
#include ""a.h""
99+
100+
101+
102+
";
91103

92104

93105
string expectedFormatedCode_WithBlanks =
@@ -98,7 +110,11 @@ public void Sorting_AngleBracketsFirst()
98110
#include ""c_third""
99111
100112
#include ""z_first""
113+
101114
// A comment
115+
116+
117+
102118
";
103119

104120

@@ -244,6 +260,7 @@ public void OtherPreprocessorDirectives()
244260
// A comment
245261
#include <d>
246262
#include ""a9""
263+
247264
#else
248265
#include <a2>
249266

0 commit comments

Comments
 (0)