Skip to content

Commit ae82987

Browse files
committed
CurrentDocument key from precedence regex works now in Try And Error Removal ignore list as well.
Default for try and error removal ingore list contains a regex that matches what identifies usually as the "matching header file" Fixes #27
1 parent d670269 commit ae82987

File tree

6 files changed

+43
-25
lines changed

6 files changed

+43
-25
lines changed

IncludeToolbox/Formatter/IncludeFormatter.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,32 +90,14 @@ private static void FormatSlashes(IEnumerable<IncludeLineInfo> lines, FormatterO
9090
}
9191
}
9292

93-
public const string CurrentFileNameKey = "$(currentFilename)";
94-
95-
/// <summary>
96-
/// Replaces special macros in a precedence regex list.
97-
/// </summary>
98-
/// <param name="precedenceRegexes"></param>
99-
/// <param name="documentName">Name of the current document without extension.</param>
100-
/// <returns></returns>
101-
private static string[] FixupRegexes(string[] precedenceRegexes, string documentName)
102-
{
103-
string[] regexes = new string[precedenceRegexes.Length];
104-
for (int i = 0; i < precedenceRegexes.Length; ++i)
105-
{
106-
regexes[i] = precedenceRegexes[i].Replace(CurrentFileNameKey, documentName);
107-
}
108-
return regexes;
109-
}
110-
11193
private static void SortIncludes(ref List<IncludeLineInfo> lines, FormatterOptionsPage settings, string documentName)
11294
{
11395
FormatterOptionsPage.TypeSorting typeSorting = settings.SortByType;
11496
bool regexIncludeDelimiter = settings.RegexIncludeDelimiter;
11597
bool blankAfterRegexGroupMatch = settings.BlankAfterRegexGroupMatch;
11698
bool removeEmptyLines = settings.RemoveEmptyLines;
11799

118-
string[] precedenceRegexes = FixupRegexes(settings.PrecedenceRegexes, documentName);
100+
string[] precedenceRegexes = RegexUtils.FixupRegexes(settings.PrecedenceRegexes, documentName);
119101

120102
// Select only valid include lines and sort them. They'll stay in this relative sorted
121103
// order when rearranged by regex precedence groups.

IncludeToolbox/IncludeToolbox.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
<SubType>Component</SubType>
8888
</Compile>
8989
<Compile Include="GraphWindow\ViewModel\IncludeTreeViewItem.cs" />
90+
<Compile Include="RegexUtils.cs" />
9091
<Compile Include="TryAndErrorRemoval.cs" />
9192
<Compile Include="Commands\TryAndErrorRemoval_CodeWindow.cs" />
9293
<Compile Include="Commands\TryAndErrorRemoval_Project.cs" />

IncludeToolbox/Options/FormatterOptionsPage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ public enum SlashMode
8585

8686
[Category("Sorting")]
8787
[DisplayName("Precedence Regexes")]
88-
[Description("Earlier match means higher sorting priority.\n\"" + Formatter.IncludeFormatter.CurrentFileNameKey + "\" will be replaced with the current file name without extension.")]
88+
[Description("Earlier match means higher sorting priority.\n\"" + RegexUtils.CurrentFileNameKey + "\" will be replaced with the current file name without extension.")]
8989
public string[] PrecedenceRegexes {
9090
get { return precedenceRegexes; }
9191
set { precedenceRegexes = value.Where(x => x.Length > 0).ToArray(); } // Remove empty lines.
9292
}
93-
private string[] precedenceRegexes = new string[] { $"(?i){Formatter.IncludeFormatter.CurrentFileNameKey}\\.(h|hpp|hxx|inl|c|cpp|cxx)(?-i)" };
93+
private string[] precedenceRegexes = new string[] { $"(?i){RegexUtils.CurrentFileNameKey}\\.(h|hpp|hxx|inl|c|cpp|cxx)(?-i)" };
9494

9595
public enum TypeSorting
9696
{

IncludeToolbox/Options/TryAndErrorRemovalOptionsPage.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ public enum IncludeRemovalOrder
3131

3232
[Category("Try And Error Include Removal")]
3333
[DisplayName("Ignore List")]
34-
[Description("List of regexes. If the content of a #include directive match with any of these, it will be ignored.")]
35-
public string[] IgnoreList { get; set; } = new string[] { ".inl", "_inl.h" };
34+
[Description("List of regexes. If the content of a #include directive match with any of these, it will be ignored." +
35+
"\n\"" + RegexUtils.CurrentFileNameKey + "\" will be replaced with the current file name without extension.")]
36+
public string[] IgnoreList { get; set; } = new string[] { $"(\\/|\\\\|^){RegexUtils.CurrentFileNameKey}\\.(h|hpp|hxx|inl|c|cpp|cxx)$", ".inl", "_inl.h" };
3637

3738
[Category("Try And Error Include Removal")]
3839
[DisplayName("Keep Line Breaks")]

IncludeToolbox/RegexUtils.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IncludeToolbox
8+
{
9+
static class RegexUtils
10+
{
11+
public const string CurrentFileNameKey = "$(currentFilename)";
12+
13+
/// <summary>
14+
/// Replaces special macros in a regex list.
15+
/// </summary>
16+
/// <param name="precedenceRegexes"></param>
17+
/// <param name="documentName">Name of the current document without extension.</param>
18+
/// <returns></returns>
19+
public static string[] FixupRegexes(string[] precedenceRegexes, string documentName)
20+
{
21+
string[] regexes = new string[precedenceRegexes.Length];
22+
for (int i = 0; i < precedenceRegexes.Length; ++i)
23+
{
24+
regexes[i] = precedenceRegexes[i].Replace(CurrentFileNameKey, documentName);
25+
}
26+
return regexes;
27+
}
28+
}
29+
}

IncludeToolbox/TryAndErrorRemoval.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using EnvDTE;
88
using Microsoft.VisualStudio.Text;
99
using System.Collections.Generic;
10+
using System.IO;
1011

1112
namespace IncludeToolbox
1213
{
@@ -97,8 +98,12 @@ public void PerformTryAndErrorRemoval(EnvDTE.Document document, TryAndErrorRemov
9798
if (settings.IgnoreFirstInclude)
9899
includeLines = includeLines.Skip(1);
99100
// Apply filter ignore regex.
100-
includeLines = includeLines.Where(line => !settings.IgnoreList.Any(regexPattern =>
101-
new System.Text.RegularExpressions.Regex(regexPattern).Match(line.IncludeContent).Success));
101+
{
102+
string documentName = Path.GetFileNameWithoutExtension(document.FullName);
103+
string[] ignoreRegexList = RegexUtils.FixupRegexes(settings.IgnoreList, documentName);
104+
includeLines = includeLines.Where(line => !ignoreRegexList.Any(regexPattern =>
105+
new System.Text.RegularExpressions.Regex(regexPattern).Match(line.IncludeContent).Success));
106+
}
102107
// Reverse order if necessary.
103108
if (settings.RemovalOrder == TryAndErrorRemovalOptionsPage.IncludeRemovalOrder.BottomToTop)
104109
includeLines = includeLines.Reverse();

0 commit comments

Comments
 (0)