Skip to content

Commit 351fc69

Browse files
committed
Wrap white space in enclosing curly braces
1 parent e78e63f commit 351fc69

File tree

7 files changed

+54
-10
lines changed

7 files changed

+54
-10
lines changed

Rubberduck.Core/UI/RegexAssistant/RegexAssistant.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@
7979
<CheckBox Content="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=RegexAssistant_IgnoreCaseFlag}"
8080
IsChecked="{Binding IgnoreCaseFlag}"
8181
Margin="0,0,5,0"/>
82+
<CheckBox Content="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=RegexAssistant_EncloseWhitespaceFlag}"
83+
IsChecked="{Binding SurroundWhitespaceCharWithIdentifier}"
84+
Margin="0,0,5,0" />
85+
8286
</StackPanel>
8387
</StackPanel>
8488
</GroupBox>

Rubberduck.Core/UI/RegexAssistant/RegexAssistantViewModel.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,20 @@ public string Pattern
4747
}
4848
}
4949

50+
public bool SurroundWhitespaceCharWithIdentifier
51+
{
52+
get => _surroundWhitespaceCharWithIdentifier;
53+
set
54+
{
55+
_surroundWhitespaceCharWithIdentifier = value;
56+
RecalculateDescription();
57+
}
58+
}
59+
5060
private bool _globalFlag;
5161
private bool _ignoreCaseFlag;
5262
private string _pattern;
63+
private bool _surroundWhitespaceCharWithIdentifier;
5364

5465
private List<TreeViewItem> _resultItems;
5566
public List<TreeViewItem> ResultItems
@@ -78,7 +89,7 @@ private void RecalculateDescription()
7889
ResultItems = results;
7990
return;
8091
}
81-
ResultItems = ToTreeViewItems(new Pattern(_pattern, _ignoreCaseFlag, _globalFlag));
92+
ResultItems = ToTreeViewItems(new Pattern(_pattern, _ignoreCaseFlag, _globalFlag, _surroundWhitespaceCharWithIdentifier));
8293
}
8394

8495
private List<TreeViewItem> ToTreeViewItems(Pattern pattern)

Rubberduck.RegexAssistant/Atoms/CharacterClass.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Text.RegularExpressions;
6+
using Rubberduck.Resources;
67

78
namespace Rubberduck.RegexAssistant.Atoms
89
{
@@ -11,7 +12,7 @@ internal class CharacterClass : IAtom
1112
public bool InverseMatching { get; }
1213
public IList<string> CharacterSpecifiers { get; }
1314

14-
public CharacterClass(string specifier, Quantifier quantifier)
15+
public CharacterClass(string specifier, Quantifier quantifier, bool surroundWhitespaceCharWithIdentifier = false)
1516
{
1617
if (specifier == null || quantifier == null)
1718
{
@@ -27,15 +28,17 @@ public CharacterClass(string specifier, Quantifier quantifier)
2728
// trim leading and closing bracket
2829
var actualSpecifier = specifier.Substring(1, specifier.Length - 2);
2930
InverseMatching = actualSpecifier.StartsWith("^");
30-
CharacterSpecifiers= ExtractCharacterSpecifiers(InverseMatching ? actualSpecifier.Substring(1) : actualSpecifier);
31+
CharacterSpecifiers= ExtractCharacterSpecifiers(InverseMatching ? actualSpecifier.Substring(1) : actualSpecifier,
32+
surroundWhitespaceCharWithIdentifier);
3133
}
3234

3335
public string Specifier { get; }
3436

3537
public Quantifier Quantifier { get; }
3638

3739
private static readonly Regex CharacterRanges = new Regex(@"(\\[dDwWsS]|(\\[ntfvr]|\\([0-7]{3}|x[\dA-F]{2}|u[\dA-F]{4}|[\\\.\[\]])|.)(-(\\[ntfvr]|\\([0-7]{3}|x[A-F]{2}|u[\dA-F]{4}|[\.\\\[\]])|.))?)", RegexOptions.Compiled);
38-
private IList<string> ExtractCharacterSpecifiers(string characterClass)
40+
private static readonly Regex whitespace = new Regex("\\s");
41+
private IList<string> ExtractCharacterSpecifiers(string characterClass, bool surroundWhitespaceCharWithIdentifier)
3942
{
4043
var specifiers = CharacterRanges.Matches(characterClass);
4144
var result = new List<string>();
@@ -55,7 +58,10 @@ private IList<string> ExtractCharacterSpecifiers(string characterClass)
5558
continue;
5659
}
5760
}
58-
result.Add(specifier.Value);
61+
62+
result.Add(whitespace.IsMatch(specifier.Value) && surroundWhitespaceCharWithIdentifier
63+
? string.Format(RubberduckUI.RegexAssistant_EncloseWhitespace_EnclosingFormat, specifier.Value)
64+
: specifier.Value);
5965
}
6066
return result;
6167
}

Rubberduck.RegexAssistant/Pattern.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Pattern : IDescribable
1010

1111
public string Description { get; }
1212

13-
public Pattern(string expression, bool ignoreCase = false, bool global = false)
13+
public Pattern(string expression, bool ignoreCase = false, bool global = false, bool surroundWhitespaceCharWithIdentifier = false)
1414
{
1515
if (expression == null)
1616
{
@@ -25,7 +25,7 @@ public Pattern(string expression, bool ignoreCase = false, bool global = false)
2525

2626
var start = AnchoredAtStart ? 1 : 0;
2727
var end = (AnchoredAtEnd ? 1 : 0) + start;
28-
RootExpression = VBRegexParser.Parse(expression.Substring(start, expression.Length - end));
28+
RootExpression = VBRegexParser.Parse(expression.Substring(start, expression.Length - end), surroundWhitespaceCharWithIdentifier);
2929
Description = AssembleDescription();
3030
}
3131

Rubberduck.RegexAssistant/VBRegexParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal class VBRegexParser
1212
private static readonly Regex LITERAL_PATTERN = new Regex("^" + Literal.Pattern);
1313
private static readonly Regex QUANTIFIER_PATTERN = new Regex("^" + Quantifier.Pattern);
1414

15-
public static IRegularExpression Parse(string specifier)
15+
public static IRegularExpression Parse(string specifier, bool surroundWhitespaceCharWithIdentifier = false)
1616
{
1717
if (specifier == null) throw new ArgumentNullException(nameof(specifier));
1818

@@ -45,7 +45,7 @@ public static IRegularExpression Parse(string specifier)
4545
if (expressionBody.Length != 0)
4646
{
4747
var quantifier = GetQuantifier(specifier, expressionBody.Length);
48-
concatenation.Add(new SingleAtomExpression(new CharacterClass(expressionBody, new Quantifier(quantifier))));
48+
concatenation.Add(new SingleAtomExpression(new CharacterClass(expressionBody, new Quantifier(quantifier), surroundWhitespaceCharWithIdentifier)));
4949
specifier = specifier.Substring(expressionBody.Length + quantifier.Length);
5050
continue;
5151
}

Rubberduck.Resources/RubberduckUI.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rubberduck.Resources/RubberduckUI.resx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,6 @@ NOTE: Restart is required for the setting to take effect.</value>
15481548
</data>
15491549
<data name="RefactoringFailure_BaseMessage" xml:space="preserve">
15501550
<value>Refactoring failed.</value>
1551-
15521551
</data>
15531552
<data name="RefactoringFailure_NoActiveSelection" xml:space="preserve">
15541553
<value>There is no active selection.</value>
@@ -1594,4 +1593,10 @@ NOTE: Restart is required for the setting to take effect.</value>
15941593
<value>Declaration type of target '{0}' is '{1}' instead of one of the expected '{2}'.</value>
15951594
<comment>{0}: name of target; {1}: actual declaration type; {2}: expected declaration types</comment>
15961595
</data>
1596+
<data name="RegexAssistant_EncloseWhitespaceFlag" xml:space="preserve">
1597+
<value>Enclose whitespace in identifier</value>
1598+
</data>
1599+
<data name="RegexAssistant_EncloseWhitespace_EnclosingFormat" xml:space="preserve">
1600+
<value>{{{0}}}</value>
1601+
</data>
15971602
</root>

0 commit comments

Comments
 (0)