Skip to content

Commit fbe3855

Browse files
CommonLoon102mkmm
andauthored
Highlight invalid regexes (#23)
* added TextBox write out catch ed ArgumentException from Regex * added simple regex validation * code cleanup * perform validation of both regex txt boxes in OnTextBoxChangedRegex * code formatting * Some refactoring and disabling the regex inputs while searching so an invalid regex can't disable the stop button * highlight workaround * added some paddings * Do not use short-circuit and (&&) as it skips the second validation * Use code instead of comment * obeying MS capitalization conventions https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/capitalization-conventions Co-authored-by: MartMet <MartMet> Co-authored-by: MartMet <[email protected]> Co-authored-by: CommonLoon102 <[email protected]>
1 parent 9cf38e3 commit fbe3855

File tree

5 files changed

+98
-39
lines changed

5 files changed

+98
-39
lines changed

RegexFileSearcher/RegexFileSearcher/MainForm.init.xeto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace RegexFileSearcher
55
{
66
public partial class MainForm : Form
77
{
8-
private readonly TextBox txtFilenameRegex;
8+
private readonly TextBox txtFileNameRegex;
99
private readonly CheckBox chkCompiled;
1010
private readonly CheckBox chkCultureInvariant;
1111
private readonly CheckBox chkEcmaScript;
@@ -44,7 +44,7 @@ public partial class MainForm : Form
4444
private MainForm(bool initializeControls)
4545
{
4646
XamlReader.Load(this);
47-
txtFilenameRegex = FindChild<TextBox>("txtFilenameRegex");
47+
txtFileNameRegex = FindChild<TextBox>("txtFileNameRegex");
4848
chkCompiled = FindChild<CheckBox>("chkCompiled");
4949
chkCultureInvariant = FindChild<CheckBox>("chkCultureInvariant");
5050
chkEcmaScript = FindChild<CheckBox>("chkEcmaScript");

RegexFileSearcher/RegexFileSearcher/MainForm.xeto

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
<TableLayout>
1212
<TableRow>
1313
<TableCell>
14-
<Label>Filename Regex:</Label>
15-
</TableCell>
16-
</TableRow>
17-
<TableRow>
18-
<TableCell ScaleWidth="True">
19-
<TextBox ID="txtFilenameRegex"></TextBox>
14+
<Panel Padding="5">
15+
<StackLayout Orientation="Vertical" HorizontalContentAlignment="Stretch">
16+
<Label>Filename Regex:</Label>
17+
<TextBox ID="txtFileNameRegex" />
18+
</StackLayout>
19+
</Panel>
2020
</TableCell>
2121
</TableRow>
2222
<TableRow>
@@ -52,7 +52,7 @@
5252
MaximumDecimalPlaces="0"
5353
Value="2"
5454
Increment="1"
55-
Width="90"/>
55+
Width="90" />
5656
</Panel>
5757
<Label>s</Label>
5858
</StackLayout>
@@ -64,12 +64,12 @@
6464
</TableRow>
6565
<TableRow>
6666
<TableCell>
67-
<Label>Content Regex:</Label>
68-
</TableCell>
69-
</TableRow>
70-
<TableRow>
71-
<TableCell ScaleWidth="True">
72-
<TextBox ID="txtContentRegex"></TextBox>
67+
<Panel Padding="5">
68+
<StackLayout Orientation="Vertical" HorizontalContentAlignment="Stretch">
69+
<Label>Content Regex:</Label>
70+
<TextBox ID="txtContentRegex" />
71+
</StackLayout>
72+
</Panel>
7373
</TableCell>
7474
</TableRow>
7575
<TableRow>
@@ -104,7 +104,7 @@
104104
MaximumDecimalPlaces="0"
105105
Value="2"
106106
Increment="1"
107-
Width="90"/>
107+
Width="90" />
108108
</Panel>
109109
<Label>s</Label>
110110
</StackLayout>
@@ -116,17 +116,21 @@
116116
</TableRow>
117117
<TableRow>
118118
<TableCell>
119-
<Label>Search Directory:</Label>
119+
<Panel Padding="5,2,0,2">
120+
<Label>Search Directory:</Label>
121+
</Panel>
120122
</TableCell>
121123
</TableRow>
122124
<TableRow>
123125
<TableCell>
124-
<FilePicker ID="fpSearchPath" FileAction="SelectFolder"></FilePicker>
126+
<Panel Padding="5,2,5,2">
127+
<FilePicker ID="fpSearchPath" FileAction="SelectFolder" />
128+
</Panel>
125129
</TableCell>
126130
</TableRow>
127131
<TableRow>
128132
<TableCell>
129-
<StackLayout Padding="0,5,0,5" Orientation="Horizontal">
133+
<StackLayout Padding="5,5,0,5" Orientation="Horizontal">
130134
<Label>Search in Subdirectories:</Label>
131135
<Panel Padding="5,0,5,0">
132136
<ComboBox ID="cboSubdirectories" Width="200" />
@@ -136,22 +140,28 @@
136140
</TableRow>
137141
<TableRow>
138142
<TableCell>
139-
<Button ID="btnStartSearch" Click="HandleSearch">Start Search</Button>
143+
<Panel Padding="5,5,5,20">
144+
<Button ID="btnStartSearch" Click="HandleSearch">Start Search</Button>
145+
</Panel>
140146
</TableCell>
141147
</TableRow>
142148
<TableRow>
143149
<TableCell>
144-
<Label>Open With Editor:</Label>
150+
<Panel Padding="5,2,5,2">
151+
<Label>Open With Editor:</Label>
152+
</Panel>
145153
</TableCell>
146154
</TableRow>
147155
<TableRow>
148156
<TableCell ScaleWidth="True">
157+
<Panel Padding="5,2,5,5">
149158
<FilePicker ID="fpOpenWith" FileAction="OpenFile" />
159+
</Panel>
150160
</TableCell>
151161
</TableRow>
152162
<TableRow>
153163
<TableCell>
154-
<StackLayout Orientation="Vertical">
164+
<StackLayout Orientation="Vertical" Padding="5,2,2,5">
155165
<StackLayout Orientation="Horizontal">
156166
<Button ID="btnSelectAll" Click="HandleSelectAll">Select All</Button>
157167
<Button ID="btnSelectNone" Click="HandleSelectNone">Select None</Button>
@@ -164,17 +174,19 @@
164174
</TableRow>
165175
<TableRow ScaleHeight="True">
166176
<TableCell>
167-
<TreeGridView ID="tvwResultExplorer" SelectedItemChanged="HandleResultExplorerSelectedItemChanged" />
177+
<Panel Padding="5,2,5,2">
178+
<TreeGridView ID="tvwResultExplorer" SelectedItemChanged="HandleResultExplorerSelectedItemChanged" />
179+
</Panel>
168180
</TableCell>
169181
</TableRow>
170182
<TableRow>
171183
<TableCell>
172-
<TextBox ID="txtPath" ReadOnly="True" />
173-
</TableCell>
174-
</TableRow>
175-
<TableRow>
176-
<TableCell>
177-
<Label ID="lblStatus" />
184+
<Panel Padding="5,0,5,0">
185+
<StackLayout Orientation="Vertical" HorizontalContentAlignment="Stretch">
186+
<TextBox ID="txtPath" ReadOnly="True" />
187+
<Label ID="lblStatus" />
188+
</StackLayout>
189+
</Panel>
178190
</TableCell>
179191
</TableRow>
180192
</TableLayout>

RegexFileSearcher/RegexFileSearcher/MainForm.xeto.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Eto.Drawing;
12
using Eto.Forms;
23
using System;
34
using System.Collections.Generic;
@@ -23,15 +24,33 @@ public MainForm() : this(initializeControls: true)
2324
{
2425
InitializeSubdirectoryPicker();
2526
InitializeResultExplorer();
27+
28+
txtFileNameRegex.TextChanged += OnTextBoxChangedRegex;
29+
txtContentRegex.TextChanged += OnTextBoxChangedRegex;
30+
}
31+
32+
private void OnTextBoxChangedRegex(object sender, EventArgs e)
33+
{
34+
bool isFileNameRegexValid = ValidateRegex(txtFileNameRegex);
35+
bool isContentRegexValid = ValidateRegex(txtContentRegex);
36+
btnStartSearch.Enabled = isFileNameRegexValid && isContentRegexValid;
37+
38+
static bool ValidateRegex(TextBox textBox)
39+
{
40+
bool isRegexValid = RegexValidator.IsRegexValid(textBox.Text, out string errorMessage);
41+
textBox.ToolTip = isRegexValid ? null : errorMessage;
42+
textBox.Parent.Parent.BackgroundColor = isRegexValid ? Colors.White : Colors.LightSalmon;
43+
return isRegexValid;
44+
}
2645
}
2746

2847
private int SearchDepth => int.Parse(cboSubdirectories.SelectedKey);
2948

30-
private Regex FilenameRegex =>
31-
string.IsNullOrEmpty(txtFilenameRegex.Text)
49+
private Regex FileNameRegex =>
50+
string.IsNullOrEmpty(txtFileNameRegex.Text)
3251
? null : new RegexPattern
3352
{
34-
Pattern = txtFilenameRegex.Text,
53+
Pattern = txtFileNameRegex.Text,
3554
IsCompiled = chkCompiled.Checked ?? false,
3655
IsCultureInvariant = chkCultureInvariant.Checked ?? false,
3756
IsEcmaScript = chkEcmaScript.Checked ?? false,
@@ -66,7 +85,7 @@ private RegexSearcher CreateNewSearcher()
6685
return new RegexSearcher(fpSearchPath.FilePath,
6786
SearchDepth,
6887
chkSearchInZipFiles.Checked ?? false,
69-
FilenameRegex,
88+
FileNameRegex,
7089
ContentRegex,
7190
_itemCollection,
7291
_cancellationTokenSource.Token);
@@ -174,6 +193,8 @@ private void StartSearch()
174193
lblStatus.Text = string.Empty;
175194
txtPath.Text = string.Empty;
176195
btnOrderByMatches.Enabled = false;
196+
txtFileNameRegex.Enabled = false;
197+
txtContentRegex.Enabled = false;
177198

178199
_cancellationTokenSource = new CancellationTokenSource();
179200
RegexSearcher searcher = CreateNewSearcher();
@@ -205,6 +226,8 @@ private void EndSearch(bool isUserRequested = true)
205226
{
206227
btnStartSearch.Text = "Start Search";
207228
btnOrderByMatches.Enabled = true;
229+
txtFileNameRegex.Enabled = true;
230+
txtContentRegex.Enabled = true;
208231
});
209232

210233
_searchEnded = true;

RegexFileSearcher/RegexFileSearcher/RegexSearcher.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal class RegexSearcher
1919
private readonly string _searchDirectory;
2020
private readonly bool _recurseSubdirectories;
2121
private readonly bool _searchInZipFiles;
22-
private readonly Regex _filenameRegex;
22+
private readonly Regex _fileNameRegex;
2323
private readonly Regex _contentRegex;
2424
private readonly CancellationToken _cancellationToken;
2525
private readonly TreeGridItemCollection _itemCollection;
@@ -39,7 +39,7 @@ public RegexSearcher(string searchDir,
3939
_depth = depth;
4040
_recurseSubdirectories = depth < 0;
4141
_searchInZipFiles = searchInZipFiles;
42-
_filenameRegex = fileRegex;
42+
_fileNameRegex = fileRegex;
4343
_contentRegex = contentRegex;
4444
_itemCollection = itemCollection;
4545
_cancellationToken = token;
@@ -71,12 +71,12 @@ public void StartSearch()
7171
return;
7272
}
7373

74-
switch (_filenameRegex, _contentRegex)
74+
switch (_fileNameRegex, _contentRegex)
7575
{
7676
case (null, null):
7777
break;
7878
case (_, null):
79-
MatchWith(MatchOnFilename);
79+
MatchWith(MatchOnFileName);
8080
break;
8181
case (null, _):
8282
MatchWith(MatchOnContent);
@@ -172,7 +172,7 @@ private void MatchWith(Action<FilePath> matcher)
172172
}
173173
}
174174

175-
private void MatchOnFilename(FilePath filePath)
175+
private void MatchOnFileName(FilePath filePath)
176176
{
177177
if (IsFileNameMatches(filePath))
178178
{
@@ -211,7 +211,7 @@ private void MatchAll(FilePath filePath)
211211
private bool IsFileNameMatches(FilePath filePath)
212212
{
213213
string fileName = Path.GetFileName(filePath.Path);
214-
return _filenameRegex.IsMatch(fileName);
214+
return _fileNameRegex.IsMatch(fileName);
215215
}
216216

217217
private void Add(FilePath filePath, int count = 0)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Text.RegularExpressions;
3+
4+
namespace RegexFileSearcher
5+
{
6+
public class RegexValidator
7+
{
8+
public static bool IsRegexValid(string pattern, out string errorMessage)
9+
{
10+
errorMessage = "";
11+
try
12+
{
13+
Regex.IsMatch("", pattern);
14+
}
15+
catch (ArgumentException e)
16+
{
17+
errorMessage = e.Message;
18+
return false;
19+
}
20+
21+
return true;
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)