Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions IncludeToolbox.vsix
Git LFS file not shown
45 changes: 21 additions & 24 deletions IncludeToolbox/Formatter/IncludeFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Copy link
Collaborator Author

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 :)

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)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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;
Expand Down
23 changes: 13 additions & 10 deletions IncludeToolbox/IncludeToolbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.OLE.Interop, Version=7.1.40304.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<HintPath>..\packages\Microsoft.VisualStudio.OLE.Interop.7.10.6070\lib\Microsoft.VisualStudio.OLE.Interop.dll</HintPath>
<HintPath>..\packages\Microsoft.VisualStudio.OLE.Interop.7.10.6071\lib\Microsoft.VisualStudio.OLE.Interop.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.Shell.14.0, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
Expand Down Expand Up @@ -191,14 +191,17 @@
<HintPath>..\packages\Microsoft.VisualStudio.TextManager.Interop.8.0.8.0.50727\lib\Microsoft.VisualStudio.TextManager.Interop.8.0.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.Threading, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<Private>False</Private>
<EmbedInteropTypes>False</EmbedInteropTypes>
<Reference Include="Microsoft.VisualStudio.Threading, Version=15.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Threading.15.4.4\lib\net45\Microsoft.VisualStudio.Threading.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.Utilities, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<Private>False</Private>
<EmbedInteropTypes>False</EmbedInteropTypes>
<Reference Include="Microsoft.VisualStudio.Utilities, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Utilities.15.0.26607\lib\net46\Microsoft.VisualStudio.Utilities.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.Validation, Version=15.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Validation.15.3.32\lib\net45\Microsoft.VisualStudio.Validation.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
Expand Down Expand Up @@ -275,14 +278,13 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(VSToolsPath)\VSSDK\Microsoft.VsSDK.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.9\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets" Condition="Exists('..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.9\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.9\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.9\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.props'))" />
<Error Condition="!Exists('..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.targets'))" />
<Error Condition="!Exists('..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.12\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.12\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets'))" />
</Target>
<Import Project="..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.targets" Condition="Exists('..\packages\Microsoft.VSSDK.BuildTools.15.1.192\build\Microsoft.VSSDK.BuildTools.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand All @@ -293,4 +295,5 @@
<Target Name="AfterBuild" Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Copy SourceFiles="$(OutputPath)$(TargetName).vsix" DestinationFolder="$(SolutionDir)" OverwriteReadOnlyFiles="true" SkipUnchangedFiles="true" />
</Target>
<Import Project="..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.12\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets" Condition="Exists('..\packages\Microsoft.VisualStudio.SDK.EmbedInteropTypes.15.0.12\build\Microsoft.VisualStudio.SDK.EmbedInteropTypes.targets')" />
</Project>
2 changes: 1 addition & 1 deletion IncludeToolbox/Package/source.extension.vsixmanifest
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" />
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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>
Expand Down
10 changes: 5 additions & 5 deletions IncludeToolbox/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<packages>
<package id="Microsoft.VisualStudio.CoreUtility" version="15.0.26606" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Imaging" version="15.0.26606" targetFramework="net461" />
<package id="Microsoft.VisualStudio.OLE.Interop" version="7.10.6070" targetFramework="net461" />
<package id="Microsoft.VisualStudio.SDK.EmbedInteropTypes" version="15.0.9" targetFramework="net461" />
<package id="Microsoft.VisualStudio.OLE.Interop" version="7.10.6071" targetFramework="net461" />
<package id="Microsoft.VisualStudio.SDK.EmbedInteropTypes" version="15.0.12" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.15.0" version="15.0.26606" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.Framework" version="15.0.26606" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Shell.Interop" version="7.10.6071" targetFramework="net461" />
Expand All @@ -15,8 +15,8 @@
<package id="Microsoft.VisualStudio.Shell.Interop.9.0" version="9.0.30729" targetFramework="net461" />
<package id="Microsoft.VisualStudio.TextManager.Interop" version="7.10.6070" targetFramework="net461" />
<package id="Microsoft.VisualStudio.TextManager.Interop.8.0" version="8.0.50727" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Threading" version="15.3.23" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Utilities" version="15.0.26606" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Validation" version="15.3.15" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Threading" version="15.4.4" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Utilities" version="15.0.26607" targetFramework="net461" />
<package id="Microsoft.VisualStudio.Validation" version="15.3.32" targetFramework="net461" />
<package id="Microsoft.VSSDK.BuildTools" version="15.1.192" targetFramework="net461" developmentDependency="true" />
</packages>
21 changes: 19 additions & 2 deletions Tests/IncludeFormatingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public void Sorting_BlanksAfterRegexGroup()

#include ""a.h""
#include <b.hpp>
#include <c.hpp>";
#include <c.hpp>



";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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""
Expand All @@ -56,7 +60,11 @@ public void Sorting_BlanksAfterRegexGroup()
#include ""c_third""

#include ""z_first""

// A comment



";


Expand Down Expand Up @@ -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 =
Expand All @@ -98,7 +110,11 @@ public void Sorting_AngleBracketsFirst()
#include ""c_third""

#include ""z_first""

// A comment



";


Expand Down Expand Up @@ -244,6 +260,7 @@ public void OtherPreprocessorDirectives()
// A comment
#include <d>
#include ""a9""

#else
#include <a2>

Expand Down