Skip to content

Commit 822d849

Browse files
author
Sébastien Geiser
committed
#before and #after in script
1 parent 58549a5 commit 822d849

File tree

2 files changed

+38
-46
lines changed

2 files changed

+38
-46
lines changed

RegexDialog/RegExToolDialog.xaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@
269269
FontSize="12"
270270
ScrollViewer.HorizontalScrollBarVisibility="Auto"
271271
TextChanged="RegexEditor_TextChanged"
272-
SizeChanged="RegexEditor_SizeChanged"
273272
ShowLineNumbers="{Binding ShowLinesNumbersRegexEditorOption}">
274273
<i:Interaction.Behaviors>
275274
<behaviors:SimplePropertyBindingBehavior PropertyName="Options.ShowSpaces" Value="{Binding ShowSpaceCharsRegexEditorOption}" />
@@ -332,7 +331,8 @@
332331
Margin="5,0"
333332
IsChecked="{Binding CSharpReplaceMode}"
334333
VerticalAlignment="Center"
335-
Content="_C#">
334+
Content="_C#"
335+
ToolTipService.ShowDuration="600000">
336336
<CheckBox.ToolTip>
337337
<StackPanel>
338338
<TextBlock>You must use at least one <Bold>return</Bold> keyword to return a string</TextBlock>
@@ -342,7 +342,9 @@
342342
<TextBlock>- <Bold>fileName</Bold> : (If in directory search) the fileName where the match was found</TextBlock>
343343
<TextBlock>- <Bold>globalIndex</Bold> : (If in directory search) the index of the match in all files</TextBlock>
344344
<TextBlock>- <Bold>fileIndex</Bold> : (If in directory search) the index of the file</TextBlock>
345-
<TextBlock Margin="0,5">You can declare shared variables, properties or methods putting code between <Bold>#global</Bold> and <Bold>#endglobal</Bold></TextBlock>
345+
<TextBlock Margin="0,5,0,0">You can declare shared variables, properties or methods putting code between <Bold>#global</Bold> and <Bold>#endglobal</Bold></TextBlock>
346+
<TextBlock>Between <Bold>#before</Bold> and <Bold>#endbefore</Bold> you get the <Bold>text</Bold> before it is used as input of the regex by returning a string you can redefine it.</TextBlock>
347+
<TextBlock>Between <Bold>#after</Bold> and <Bold>#endafter</Bold> you get the <Bold>text</Bold> after it is processed by the regex by returning a string you can modify it a last time.</TextBlock>
346348
</StackPanel>
347349
</CheckBox.ToolTip>
348350
</CheckBox>

RegexDialog/RegExToolDialog.xaml.cs

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,28 @@ namespace RegexDialog
2525
/// </summary>
2626
public partial class RegExToolDialog : Window
2727
{
28-
private List<RegExOptionViewModel> regExOptionViewModelsList = new List<RegExOptionViewModel>();
29-
private List<Regex> bracketsRegexList = (new Regex[]
28+
private readonly List<RegExOptionViewModel> regExOptionViewModelsList = new List<RegExOptionViewModel>();
29+
private readonly List<Regex> bracketsRegexList = (new Regex[]
3030
{
3131
new Regex(@"(?<!(?<![\\])([\\]{2})*[\\])[\(\)]", RegexOptions.Compiled),
3232
new Regex(@"(?<!(?<![\\])([\\]{2})*[\\])[\[\]]", RegexOptions.Compiled),
3333
new Regex(@"(?<!(?<![\\])([\\]{2})*[\\])[{}]", RegexOptions.Compiled),
3434
new Regex(@"(?<!(?<![\\])([\\]{2})*[\\])[<>]", RegexOptions.Compiled)
3535
}).ToList();
3636

37-
private ObservableCollection<string> regexHistory = new ObservableCollection<string>();
38-
private ObservableCollection<string> replaceHistory = new ObservableCollection<string>();
37+
private readonly ObservableCollection<string> regexHistory = new ObservableCollection<string>();
38+
private readonly ObservableCollection<string> replaceHistory = new ObservableCollection<string>();
3939

40-
private string[] openingBrackets = new string[] { "(", "[", "{", "<" };
40+
private readonly string[] openingBrackets = new string[] { "(", "[", "{", "<" };
4141

4242
private string lastMatchesText = "";
43-
private int lastSelectionStart = 0;
44-
private int lastSelectionLength = 0;
43+
private int lastSelectionStart;
44+
private int lastSelectionLength;
4545

46-
private bool mustSelectEditor = false;
46+
private bool mustSelectEditor;
4747

48-
private BracketColorizer currentBracketColorizer = new BracketColorizer();
49-
private BracketColorizer matchingBracketColorizer = new BracketColorizer();
48+
private readonly BracketColorizer currentBracketColorizer = new BracketColorizer();
49+
private readonly BracketColorizer matchingBracketColorizer = new BracketColorizer();
5050

5151
private static readonly Regex cSharpReplaceSpecialZoneCleaningRegex = new Regex(@"(?<=^|\s)\#(?<name>\w+)(?=\s).*(?<=\s)\#end\k<name>(?=\s|$)\s*", RegexOptions.Singleline | RegexOptions.Compiled);
5252
private static readonly Regex cSharpReplaceGlobalPartRegex = new Regex(@"(?<=^|\s)\#global(?=\s)(?<global>.*)(?<=\s)\#endglobal(?=\s|$)", RegexOptions.Singleline | RegexOptions.Compiled);
@@ -560,11 +560,11 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
560560
SetToHistory();
561561

562562
int files = 0;
563-
string text = GetCurrentText();
563+
string text;
564564

565565
Regex regex = new Regex(RegexEditor.Text, GetRegexOptions());
566566

567-
int nbrOfElementToReplace = Config.Instance.TextSourceOn == RegexTextSource.Directory ? 0 : regex.Matches(text).Count;
567+
int nbrOfElementToReplace = 0;
568568

569569
if (CSharpReplaceCheckbox.IsChecked.GetValueOrDefault())
570570
{
@@ -584,19 +584,19 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
584584
{
585585
if (TryOpen?.Invoke(fileName, false) ?? false)
586586
{
587-
text = GetText();
587+
text = script.Before(GetText());
588588
int matchesCount = regex.Matches(text).Count;
589589

590590
if (matchesCount > 0)
591591
{
592592
index = 0;
593593

594-
SetText(regex.Replace(text, match =>
594+
SetText(script.After(regex.Replace(text, match =>
595595
{
596596
index++;
597597
nbrOfElementToReplace++;
598598
return script.Replace(match, index, fileName, nbrOfElementToReplace, files);
599-
}));
599+
})));
600600

601601
try
602602
{
@@ -609,19 +609,19 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
609609
}
610610
else
611611
{
612-
text = File.ReadAllText(fileName);
612+
text = script.Before(File.ReadAllText(fileName));
613613
int matchesCount = regex.Matches(text).Count;
614614

615615
if (matchesCount > 0)
616616
{
617617
index = 0;
618618

619-
File.WriteAllText(fileName, regex.Replace(text, match =>
619+
File.WriteAllText(fileName, script.After(regex.Replace(text, match =>
620620
{
621621
index++;
622622
nbrOfElementToReplace++;
623623
return script.Replace(match, index, fileName, nbrOfElementToReplace, files);
624-
}));
624+
})));
625625

626626
files++;
627627
}
@@ -631,20 +631,24 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
631631
});
632632
break;
633633
case RegexTextSource.CurrentSelection:
634+
text = script.Before(GetCurrentText());
635+
nbrOfElementToReplace = regex.Matches(text).Count;
634636
lastSelectionStart = GetSelectionStartIndex?.Invoke() ?? 0;
635637
lastSelectionLength = GetSelectionLength?.Invoke() ?? 0;
636-
SetSelectedText(regex.Replace(text, match =>
638+
SetSelectedText(script.After(regex.Replace(text, match =>
637639
{
638640
index++;
639641
return script.Replace(match, index, GetCurrentFileName?.Invoke() ?? string.Empty, index, 0);
640-
}));
642+
})));
641643
break;
642644
default:
643-
SetText(regex.Replace(text, match =>
645+
text = script.Before(GetCurrentText());
646+
nbrOfElementToReplace = regex.Matches(text).Count;
647+
SetText(script.After(regex.Replace(text, match =>
644648
{
645649
index++;
646650
return script.Replace(match, index, GetCurrentFileName?.Invoke() ?? string.Empty, index, 0);
647-
}));
651+
})));
648652
break;
649653
}
650654
}
@@ -698,9 +702,13 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
698702

699703
break;
700704
case RegexTextSource.CurrentSelection:
705+
text = GetCurrentText();
706+
nbrOfElementToReplace = regex.Matches(text).Count;
701707
SetSelectedText(regex.Replace(text, ReplaceEditor.Text));
702708
break;
703709
default:
710+
text = GetCurrentText();
711+
nbrOfElementToReplace = regex.Matches(text).Count;
704712
SetText(regex.Replace(text, ReplaceEditor.Text));
705713
break;
706714
}
@@ -779,7 +787,7 @@ private void ExtractMatchesButton_Click(object sender, RoutedEventArgs e)
779787

780788
void Extract(string text, string fileName = "")
781789
{
782-
List<Match> matches = regex.Matches(text)
790+
List<Match> matches = regex.Matches((string)script?.Before(text) ?? text)
783791
.Cast<Match>()
784792
.ToList();
785793

@@ -819,7 +827,8 @@ void Extract(string text, string fileName = "")
819827

820828
try
821829
{
822-
SetTextInNew(sb.ToString());
830+
string result = sb.ToString();
831+
SetTextInNew(script?.After(result) ?? result);
823832
}
824833
catch { }
825834
}
@@ -1125,7 +1134,6 @@ private void Root_SizeChanged(object sender, SizeChangedEventArgs e)
11251134

11261135
RegexHistoryPopup.IsOpen = false;
11271136
ReplaceHistoryPopup.IsOpen = false;
1128-
SetMaxSizes();
11291137
}
11301138
catch { }
11311139
}
@@ -1154,24 +1162,6 @@ private void SaveWindowPosition()
11541162
catch { }
11551163
}
11561164

1157-
private void SetMaxSizes()
1158-
{
1159-
//try
1160-
//{
1161-
// RegexEditorRow.MaxHeight = Root.ActualHeight - RegexEditor.TransformToAncestor(Root).Transform(new Point(0, 0)).Y - 5 - 10;
1162-
//}
1163-
//catch { }
1164-
}
1165-
1166-
private void RegexEditor_SizeChanged(object sender, SizeChangedEventArgs e)
1167-
{
1168-
try
1169-
{
1170-
SetMaxSizes();
1171-
}
1172-
catch { }
1173-
}
1174-
11751165
private void Root_Loaded(object sender, RoutedEventArgs e)
11761166
{
11771167
try

0 commit comments

Comments
 (0)