-
Notifications
You must be signed in to change notification settings - Fork 22
Remove check that prevents adding newlines before the last line #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,9 +120,7 @@ private static bool SortIncludeBatch(FormatterOptionsPage settings, string[] pre | |
| List<IncludeLineInfo> outSortedList, IEnumerable<IncludeLineInfo> 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) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ran into this issue again, and I think the above is a cleaner fix. |
||
| { | ||
| if (!originalLineEnumerator.Current.ContainsActiveInclude) | ||
| outSortedList.Add(originalLineEnumerator.Current); | ||
| hasElements = originalLineEnumerator.MoveNext(); | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <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"> | ||
| <Metadata> | ||
| <Identity Id="IncludeToolbox.Andreas Reich.075c2e2b-7b71-45ba-b2e6-c1dadc81cfac" Version="2.2.0.1" Language="en-US" Publisher="Andreas Reich" /> | ||
| <Identity Id="IncludeToolbox.Andreas Reich.075c2e2b-7b71-45ba-b2e6-c1dadc81cfac" Version="2.2.1" Language="en-US" Publisher="Andreas Reich" /> | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug fix versioned |
||
| <DisplayName>IncludeToolbox</DisplayName> | ||
| <Description xml:space="preserve">Various tools for managing C/C++ #includes: Formatting, sorting, exploring, pruning.</Description> | ||
| <License>license.txt</License> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,7 +46,11 @@ public void Sorting_BlanksAfterRegexGroup() | |
|
|
||
| #include ""a.h"" | ||
| #include <b.hpp> | ||
| #include <c.hpp>"; | ||
| #include <c.hpp> | ||
|
|
||
|
|
||
|
|
||
| "; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll see a lot of empty lines in the "expected output", because every test except one removes duplicates, and there are a lot of duplicates. |
||
|
|
||
| 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 <b.hpp> | ||
| #include <c.hpp> | ||
| #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 <d> | ||
| #include ""a9"" | ||
|
|
||
| #else | ||
| #include <a2> | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of doing what we were doing before to insert original non-include lines, this merges those in ahead of time. That way, there's less weird broken logic trying to do it on the fly :)