Skip to content

Commit 0f7c5ca

Browse files
committed
Clean IDescribable conversion to method
This commit contains the classes which have no issue with `Description(bool spelledOutWhitespace=false).
1 parent 110b9a3 commit 0f7c5ca

File tree

6 files changed

+56
-67
lines changed

6 files changed

+56
-67
lines changed

Rubberduck.Core/UI/RegexAssistant/RegexAssistantViewModel.cs

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

50+
private bool _spellOutWhiteSpace;
5051
public bool SpellOutWhiteSpace
5152
{
5253
get => _spellOutWhiteSpace;
@@ -60,8 +61,7 @@ public bool SpellOutWhiteSpace
6061
private bool _globalFlag;
6162
private bool _ignoreCaseFlag;
6263
private string _pattern;
63-
private bool _spellOutWhiteSpace;
64-
64+
6565
private List<TreeViewItem> _resultItems;
6666
public List<TreeViewItem> ResultItems
6767
{
@@ -103,7 +103,7 @@ private List<TreeViewItem> ToTreeViewItems(Pattern pattern)
103103
{
104104
resultItems.Add(TreeViewItemFromHeader(pattern.StartAnchorDescription));
105105
}
106-
resultItems.Add(AsTreeViewItem((dynamic)pattern.RootExpression));
106+
resultItems.Add(AsTreeViewItem((dynamic)pattern.RootExpression, _spellOutWhiteSpace));
107107
if (pattern.AnchoredAtEnd)
108108
{
109109
resultItems.Add(TreeViewItemFromHeader(pattern.EndAnchorDescription));
@@ -123,60 +123,60 @@ private TreeViewItem TreeViewItemFromHeader(string header)
123123

124124
public string DescriptionResults { get; private set; }
125125

126-
private static TreeViewItem AsTreeViewItem(IRegularExpression expression)
126+
private static TreeViewItem AsTreeViewItem(IRegularExpression expression, bool spellOutWhitespace)
127127
{
128128
throw new InvalidOperationException($"Some unknown {typeof(IRegularExpression)} subtype was in RegexAssistantViewModel");
129129
}
130130

131-
private static TreeViewItem AsTreeViewItem(ErrorExpression expression)
131+
private static TreeViewItem AsTreeViewItem(ErrorExpression expression, bool spellOutWhitespace)
132132
{
133133
var result = new TreeViewItem
134134
{
135-
Header = expression.Description
135+
Header = expression.Description(spellOutWhitespace)
136136
};
137137

138138
return result;
139139
}
140140

141-
private static TreeViewItem AsTreeViewItem(ConcatenatedExpression expression)
141+
private static TreeViewItem AsTreeViewItem(ConcatenatedExpression expression, bool spellOutWhitespace)
142142
{
143143
var result = new TreeViewItem
144144
{
145-
Header = expression.Description
145+
Header = expression.Description(spellOutWhitespace)
146146
};
147147

148-
foreach (var subtree in expression.Subexpressions.Select(exp => AsTreeViewItem((dynamic)exp)))
148+
foreach (var subtree in expression.Subexpressions.Select(exp => AsTreeViewItem((dynamic)exp, spellOutWhitespace)))
149149
{
150150
result.Items.Add(subtree);
151151
}
152152
return result;
153153
}
154154

155-
private static TreeViewItem AsTreeViewItem(AlternativesExpression expression)
155+
private static TreeViewItem AsTreeViewItem(AlternativesExpression expression, bool spellOutWhitespace)
156156
{
157157
var result = new TreeViewItem
158158
{
159-
Header = expression.Description
159+
Header = expression.Description(spellOutWhitespace)
160160
};
161161

162-
foreach (var subtree in expression.Subexpressions.Select(exp => AsTreeViewItem((dynamic)exp)))
162+
foreach (var subtree in expression.Subexpressions.Select(exp => AsTreeViewItem((dynamic)exp, spellOutWhitespace)))
163163
{
164164
result.Items.Add(subtree);
165165
}
166166
return result;
167167
}
168168

169-
private static TreeViewItem AsTreeViewItem(SingleAtomExpression expression)
169+
private static TreeViewItem AsTreeViewItem(SingleAtomExpression expression, bool spellOutWhitespace)
170170
{
171171
var result = new TreeViewItem
172172
{
173-
Header = expression.Description
173+
Header = expression.Description(spellOutWhitespace)
174174
};
175175

176176
// no other Atom has Subexpressions we care about
177177
if (expression.Atom.GetType() == typeof(Group))
178178
{
179-
result.Items.Add(AsTreeViewItem((dynamic)(expression.Atom as Group).Subexpression));
179+
result.Items.Add(AsTreeViewItem((dynamic)(expression.Atom as Group).Subexpression, spellOutWhitespace));
180180
}
181181

182182
return result;

Rubberduck.RegexAssistant/Atoms/Group.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
using Rubberduck.RegexAssistant.i18n;
22
using Rubberduck.VBEditor;
33
using System;
4-
using System.Collections.Generic;
5-
using System.Text.RegularExpressions;
64

75
namespace Rubberduck.RegexAssistant.Atoms
86
{
97
public class Group : IAtom
108
{
11-
public Group(IRegularExpression expression, string specifier, Quantifier quantifier, bool spellOutWhiteSpace = false) {
9+
public Group(IRegularExpression expression, string specifier, Quantifier quantifier) {
1210
if (expression == null || quantifier == null)
1311
{
1412
throw new ArgumentNullException();
@@ -17,7 +15,6 @@ public Group(IRegularExpression expression, string specifier, Quantifier quantif
1715
Quantifier = quantifier;
1816
Subexpression = expression;
1917
Specifier = specifier;
20-
_spellOutWhiteSpace = spellOutWhiteSpace;
2118
}
2219

2320
public IRegularExpression Subexpression { get; }
@@ -26,10 +23,8 @@ public Group(IRegularExpression expression, string specifier, Quantifier quantif
2623

2724
public string Specifier { get; }
2825

29-
private readonly bool _spellOutWhiteSpace;
30-
31-
public string Description => string.Format(AssistantResources.AtomDescription_Group,
32-
_spellOutWhiteSpace && WhitespaceToString.IsFullySpellingOutApplicable(Specifier, out var spelledOutWhiteSpace)
26+
public string Description(bool spellOutWhitespace) => string.Format(AssistantResources.AtomDescription_Group,
27+
spellOutWhitespace && WhitespaceToString.IsFullySpellingOutApplicable(Specifier, out var spelledOutWhiteSpace)
3328
? spelledOutWhiteSpace
3429
: Specifier);
3530

Rubberduck.RegexAssistant/Atoms/Literal.cs

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ 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;
1514

1615
static Literal() {
1716
foreach (var escape in new[]{ '.', '+', '*', '?', '(', ')', '{', '}', '[', ']', '|', '\\' })
@@ -33,7 +32,7 @@ static Literal() {
3332
_escapeDescriptions.Add('t', AssistantResources.AtomDescription_HTab);
3433
}
3534

36-
public Literal(string specifier, Quantifier quantifier, bool spellOutWhitespace = false)
35+
public Literal(string specifier, Quantifier quantifier)
3736
{
3837
if (specifier == null || quantifier == null)
3938
{
@@ -47,7 +46,6 @@ public Literal(string specifier, Quantifier quantifier, bool spellOutWhitespace
4746
throw new ArgumentException("The given specifier does not denote a Literal");
4847
}
4948

50-
_surroundWhitespaceCharWithIdentifier = spellOutWhitespace;
5149
Specifier = specifier;
5250
}
5351

@@ -56,57 +54,54 @@ public Literal(string specifier, Quantifier quantifier, bool spellOutWhitespace
5654
public Quantifier Quantifier { get; }
5755

5856
private static readonly Dictionary<char, string> _escapeDescriptions = new Dictionary<char, string>();
59-
public string Description
57+
public string Description(bool spellOutWhitespace)
6058
{
61-
get
59+
// here be dragons!
60+
// keep track of:
61+
// - escaped chars
62+
// - escape sequences (each having a different description)
63+
// - codepoint escapes (belongs into above category but kept separate)
64+
// - and actually boring literal matches
65+
if (Specifier.Length > 1)
6266
{
63-
// here be dragons!
64-
// keep track of:
65-
// - escaped chars
66-
// - escape sequences (each having a different description)
67-
// - codepoint escapes (belongs into above category but kept separate)
68-
// - and actually boring literal matches
69-
if (Specifier.Length > 1)
67+
var relevant = Specifier.Substring(1); // skip the damn Backslash at the start
68+
if (relevant.Length > 1) // longer sequences
7069
{
71-
var relevant = Specifier.Substring(1); // skip the damn Backslash at the start
72-
if (relevant.Length > 1) // longer sequences
70+
if (relevant.StartsWith("u"))
7371
{
74-
if (relevant.StartsWith("u"))
75-
{
76-
return string.Format(AssistantResources.AtomDescription_Literal_UnicodePoint, relevant.Substring(1)); //skip u
77-
}
78-
79-
if (relevant.StartsWith("x"))
80-
{
81-
return string.Format(AssistantResources.AtomDescription_Literal_HexCodepoint, relevant.Substring(1)); // skip x
82-
}
83-
84-
return string.Format(AssistantResources.AtomDescription_Literal_OctalCodepoint, relevant); // no format specifier to skip
72+
return string.Format(AssistantResources.AtomDescription_Literal_UnicodePoint, relevant.Substring(1)); //skip u
8573
}
8674

87-
if (EscapeLiterals.Contains(relevant[0]))
75+
if (relevant.StartsWith("x"))
8876
{
89-
return string.Format(AssistantResources.AtomDescription_Literal_EscapedLiteral, relevant);
77+
return string.Format(AssistantResources.AtomDescription_Literal_HexCodepoint, relevant.Substring(1)); // skip x
9078
}
9179

92-
if (char.IsDigit(relevant[0]))
93-
{
94-
return string.Format(AssistantResources.AtomDescription_Literal_Backreference, relevant);
95-
}
80+
return string.Format(AssistantResources.AtomDescription_Literal_OctalCodepoint, relevant); // no format specifier to skip
81+
}
9682

97-
return _escapeDescriptions[relevant[0]];
83+
if (EscapeLiterals.Contains(relevant[0]))
84+
{
85+
return string.Format(AssistantResources.AtomDescription_Literal_EscapedLiteral, relevant);
9886
}
9987

100-
if (Specifier.Equals("."))
88+
if (char.IsDigit(relevant[0]))
10189
{
102-
return AssistantResources.AtomDescription_Dot;
90+
return string.Format(AssistantResources.AtomDescription_Literal_Backreference, relevant);
10391
}
10492

105-
return string.Format(AssistantResources.AtomDescription_Literal_ActualLiteral,
106-
_surroundWhitespaceCharWithIdentifier && WhitespaceToString.IsFullySpellingOutApplicable(Specifier, out var spelledOutWhiteSpace)
107-
? spelledOutWhiteSpace
108-
: Specifier);
93+
return _escapeDescriptions[relevant[0]];
10994
}
95+
96+
if (Specifier.Equals("."))
97+
{
98+
return AssistantResources.AtomDescription_Dot;
99+
}
100+
101+
return string.Format(AssistantResources.AtomDescription_Literal_ActualLiteral,
102+
spellOutWhitespace && WhitespaceToString.IsFullySpellingOutApplicable(Specifier, out var spelledOutWhiteSpace)
103+
? spelledOutWhiteSpace
104+
: Specifier);
110105
}
111106

112107
public override string ToString() => Specifier;

Rubberduck.RegexAssistant/Expressions/SingleAtomExpression.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ public SingleAtomExpression(IAtom atom)
1313
Atom = atom ?? throw new ArgumentNullException();
1414
}
1515

16-
public string Description => $"{Atom.Description} {Atom.Quantifier.HumanReadable()}.";
16+
public string Description(bool spellOutWhitespace) => $"{Atom.Description(spellOutWhitespace)} {Atom.Quantifier.HumanReadable()}.";
1717

1818
public IList<IRegularExpression> Subexpressions => new List<IRegularExpression>(Enumerable.Empty<IRegularExpression>());
1919

20-
21-
public override string ToString() => $"Atom: {Atom.ToString()}";
20+
public override string ToString() => $"Atom: {Atom}";
2221
public override bool Equals(object obj) => obj is SingleAtomExpression other && other.Atom.Equals(Atom);
2322
public override int GetHashCode() => Atom.GetHashCode();
2423
}

Rubberduck.RegexAssistant/IDescribable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
public interface IDescribable
44
{
5-
string Description { get; }
5+
string Description(bool spellOutWhiteSpace = false);
66
}
77
}

Rubberduck.RegexAssistant/VBRegexParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static IRegularExpression Parse(string specifier, bool spellOutWhiteSpace
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), spellOutWhiteSpace)));
37+
concatenation.Add(new SingleAtomExpression(new RdGroup(expression, expressionBody, new Quantifier(quantifier))));
3838
specifier = specifier.Substring(expressionBody.Length + quantifier.Length);
3939
continue;
4040
}
@@ -61,7 +61,7 @@ public static IRegularExpression Parse(string specifier, bool spellOutWhiteSpace
6161
continue;
6262
}
6363
var quantifier = GetQuantifier(specifier, expressionBody.Length);
64-
concatenation.Add(new SingleAtomExpression(new Literal(expressionBody, new Quantifier(quantifier), spellOutWhiteSpace)));
64+
concatenation.Add(new SingleAtomExpression(new Literal(expressionBody, new Quantifier(quantifier))));
6565
specifier = specifier.Substring(expressionBody.Length + quantifier.Length);
6666
continue;
6767
}

0 commit comments

Comments
 (0)