Skip to content

Commit f7b12e0

Browse files
committed
Use whitespace converter class & rename variable
Got excited when renamed variable came to mind and crossed the streams.
1 parent 12eabb7 commit f7b12e0

File tree

7 files changed

+47
-26
lines changed

7 files changed

+47
-26
lines changed

Rubberduck.Core/UI/RegexAssistant/RegexAssistant.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
IsChecked="{Binding IgnoreCaseFlag}"
8181
Margin="0,0,5,0"/>
8282
<CheckBox Content="{Resx ResxName=Rubberduck.Resources.RubberduckUI, Key=RegexAssistant_EncloseWhitespaceFlag}"
83-
IsChecked="{Binding SurroundWhitespaceCharWithIdentifier}"
83+
IsChecked="{Binding SpellOutWhiteSpace}"
8484
Margin="0,0,5,0" />
8585

8686
</StackPanel>

Rubberduck.Core/UI/RegexAssistant/RegexAssistantViewModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ public string Pattern
4747
}
4848
}
4949

50-
public bool SurroundWhitespaceCharWithIdentifier
50+
public bool SpellOutWhiteSpace
5151
{
52-
get => _surroundWhitespaceCharWithIdentifier;
52+
get => _spellOutWhiteSpace;
5353
set
5454
{
55-
_surroundWhitespaceCharWithIdentifier = value;
55+
_spellOutWhiteSpace = value;
5656
RecalculateDescription();
5757
}
5858
}
5959

6060
private bool _globalFlag;
6161
private bool _ignoreCaseFlag;
6262
private string _pattern;
63-
private bool _surroundWhitespaceCharWithIdentifier;
63+
private bool _spellOutWhiteSpace;
6464

6565
private List<TreeViewItem> _resultItems;
6666
public List<TreeViewItem> ResultItems
@@ -89,7 +89,7 @@ private void RecalculateDescription()
8989
ResultItems = results;
9090
return;
9191
}
92-
ResultItems = ToTreeViewItems(new Pattern(_pattern, _ignoreCaseFlag, _globalFlag, _surroundWhitespaceCharWithIdentifier));
92+
ResultItems = ToTreeViewItems(new Pattern(_pattern, _ignoreCaseFlag, _globalFlag, _spellOutWhiteSpace));
9393
}
9494

9595
private List<TreeViewItem> ToTreeViewItems(Pattern pattern)

Rubberduck.RegexAssistant/Atoms/CharacterClass.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal class CharacterClass : IAtom
1212
public bool InverseMatching { get; }
1313
public IList<string> CharacterSpecifiers { get; }
1414

15-
public CharacterClass(string specifier, Quantifier quantifier, bool surroundWhitespaceCharWithIdentifier = false)
15+
public CharacterClass(string specifier, Quantifier quantifier, bool spellOutWhiteSpace = false)
1616
{
1717
if (specifier == null || quantifier == null)
1818
{
@@ -28,17 +28,17 @@ public CharacterClass(string specifier, Quantifier quantifier, bool surroundWhit
2828
// trim leading and closing bracket
2929
var actualSpecifier = specifier.Substring(1, specifier.Length - 2);
3030
InverseMatching = actualSpecifier.StartsWith("^");
31-
CharacterSpecifiers= ExtractCharacterSpecifiers(InverseMatching ? actualSpecifier.Substring(1) : actualSpecifier,
32-
surroundWhitespaceCharWithIdentifier);
31+
CharacterSpecifiers = ExtractCharacterSpecifiers(InverseMatching ? actualSpecifier.Substring(1) : actualSpecifier,
32+
spellOutWhiteSpace);
3333
}
3434

3535
public string Specifier { get; }
3636

3737
public Quantifier Quantifier { get; }
3838

3939
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);
40-
private static readonly Regex whitespace = new Regex("\\s");
41-
private IList<string> ExtractCharacterSpecifiers(string characterClass, bool surroundWhitespaceCharWithIdentifier)
40+
41+
private IList<string> ExtractCharacterSpecifiers(string characterClass, bool spellOutWhitespace)
4242
{
4343
var specifiers = CharacterRanges.Matches(characterClass);
4444
var result = new List<string>();
@@ -59,9 +59,15 @@ private IList<string> ExtractCharacterSpecifiers(string characterClass, bool sur
5959
}
6060
}
6161

62-
result.Add(whitespace.IsMatch(specifier.Value) && surroundWhitespaceCharWithIdentifier
63-
? string.Format(RubberduckUI.RegexAssistant_EncloseWhitespace_EnclosingFormat, specifier.Value)
64-
: specifier.Value);
62+
if (spellOutWhitespace
63+
&& WhitespaceToString.IsFullySpellingOutApplicable(specifier.Value, out var spelledOutWhiteSpace))
64+
{
65+
result.Add(spelledOutWhiteSpace);
66+
}
67+
else
68+
{
69+
result.Add(specifier.Value);
70+
}
6571
}
6672
return result;
6773
}

Rubberduck.RegexAssistant/Atoms/Group.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Rubberduck.RegexAssistant.Atoms
88
{
99
public class Group : IAtom
1010
{
11-
public Group(IRegularExpression expression, string specifier, Quantifier quantifier) {
11+
public Group(IRegularExpression expression, string specifier, Quantifier quantifier, bool spellOutWhiteSpace = false) {
1212
if (expression == null || quantifier == null)
1313
{
1414
throw new ArgumentNullException();
@@ -17,6 +17,7 @@ public Group(IRegularExpression expression, string specifier, Quantifier quantif
1717
Quantifier = quantifier;
1818
Subexpression = expression;
1919
Specifier = specifier;
20+
_spellOutWhiteSpace = spellOutWhiteSpace;
2021
}
2122

2223
public IRegularExpression Subexpression { get; }
@@ -25,7 +26,12 @@ public Group(IRegularExpression expression, string specifier, Quantifier quantif
2526

2627
public string Specifier { get; }
2728

28-
public string Description => string.Format(AssistantResources.AtomDescription_Group, Specifier);
29+
private readonly bool _spellOutWhiteSpace;
30+
31+
public string Description => string.Format(AssistantResources.AtomDescription_Group,
32+
_spellOutWhiteSpace && WhitespaceToString.IsFullySpellingOutApplicable(Specifier, out var spelledOutWhiteSpace)
33+
? spelledOutWhiteSpace
34+
: Specifier);
2935

3036

3137
public override string ToString() => Specifier;

Rubberduck.RegexAssistant/Atoms/Literal.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal class Literal : IAtom
1111
public static readonly string Pattern = @"(?<expression>\\(u[\dA-F]{4}|x[\dA-F]{2}|[0-7]{3}|[bB\(\){}\\\[\]\.+*?1-9nftvrdDwWsS])|[^()\[\]{}\\*+?^$])";
1212
private static readonly Regex Matcher = new Regex($"^{Pattern}$", RegexOptions.Compiled);
1313
private static readonly ISet<char> EscapeLiterals = new HashSet<char>();
14+
private readonly bool _surroundWhitespaceCharWithIdentifier;
1415

1516
static Literal() {
1617
foreach (var escape in new[]{ '.', '+', '*', '?', '(', ')', '{', '}', '[', ']', '|', '\\' })
@@ -32,7 +33,7 @@ static Literal() {
3233
_escapeDescriptions.Add('t', AssistantResources.AtomDescription_HTab);
3334
}
3435

35-
public Literal(string specifier, Quantifier quantifier)
36+
public Literal(string specifier, Quantifier quantifier, bool spellOutWhitespace = false)
3637
{
3738
if (specifier == null || quantifier == null)
3839
{
@@ -45,6 +46,8 @@ public Literal(string specifier, Quantifier quantifier)
4546
{
4647
throw new ArgumentException("The given specifier does not denote a Literal");
4748
}
49+
50+
_surroundWhitespaceCharWithIdentifier = spellOutWhitespace;
4851
Specifier = specifier;
4952
}
5053

@@ -95,9 +98,15 @@ public string Description
9598
}
9699
}
97100

98-
return Specifier.Equals(".")
99-
? AssistantResources.AtomDescription_Dot
100-
: string.Format(AssistantResources.AtomDescription_Literal_ActualLiteral, Specifier);
101+
if (Specifier.Equals("."))
102+
{
103+
return AssistantResources.AtomDescription_Dot;
104+
}
105+
106+
return string.Format(AssistantResources.AtomDescription_Literal_ActualLiteral,
107+
_surroundWhitespaceCharWithIdentifier && WhitespaceToString.IsFullySpellingOutApplicable(Specifier, out var spelledOutWhiteSpace)
108+
? spelledOutWhiteSpace
109+
: Specifier);
101110
}
102111
}
103112

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, bool surroundWhitespaceCharWithIdentifier = false)
13+
public Pattern(string expression, bool ignoreCase = false, bool global = false, bool spellOutWhitespace = 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), surroundWhitespaceCharWithIdentifier);
28+
RootExpression = VBRegexParser.Parse(expression.Substring(start, expression.Length - end), spellOutWhitespace);
2929
Description = AssembleDescription();
3030
}
3131

Rubberduck.RegexAssistant/VBRegexParser.cs

Lines changed: 4 additions & 4 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, bool surroundWhitespaceCharWithIdentifier = false)
15+
public static IRegularExpression Parse(string specifier, bool spellOutWhiteSpace = false)
1616
{
1717
if (specifier == null) throw new ArgumentNullException(nameof(specifier));
1818

@@ -34,7 +34,7 @@ public static IRegularExpression Parse(string specifier, bool surroundWhitespace
3434
{
3535
var quantifier = GetQuantifier(specifier, expressionBody.Length);
3636
var expression = Parse(expressionBody.Substring(1, expressionBody.Length - 2));
37-
concatenation.Add(new SingleAtomExpression(new RdGroup(expression, expressionBody, new Quantifier(quantifier))));
37+
concatenation.Add(new SingleAtomExpression(new RdGroup(expression, expressionBody, new Quantifier(quantifier), spellOutWhiteSpace)));
3838
specifier = specifier.Substring(expressionBody.Length + quantifier.Length);
3939
continue;
4040
}
@@ -45,7 +45,7 @@ public static IRegularExpression Parse(string specifier, bool surroundWhitespace
4545
if (expressionBody.Length != 0)
4646
{
4747
var quantifier = GetQuantifier(specifier, expressionBody.Length);
48-
concatenation.Add(new SingleAtomExpression(new CharacterClass(expressionBody, new Quantifier(quantifier), surroundWhitespaceCharWithIdentifier)));
48+
concatenation.Add(new SingleAtomExpression(new CharacterClass(expressionBody, new Quantifier(quantifier), spellOutWhiteSpace)));
4949
specifier = specifier.Substring(expressionBody.Length + quantifier.Length);
5050
continue;
5151
}
@@ -61,7 +61,7 @@ public static IRegularExpression Parse(string specifier, bool surroundWhitespace
6161
continue;
6262
}
6363
var quantifier = GetQuantifier(specifier, expressionBody.Length);
64-
concatenation.Add(new SingleAtomExpression(new Literal(expressionBody, new Quantifier(quantifier))));
64+
concatenation.Add(new SingleAtomExpression(new Literal(expressionBody, new Quantifier(quantifier), spellOutWhiteSpace)));
6565
specifier = specifier.Substring(expressionBody.Length + quantifier.Length);
6666
continue;
6767
}

0 commit comments

Comments
 (0)