@@ -25,28 +25,28 @@ namespace RegexDialog
25
25
/// </summary>
26
26
public partial class RegExToolDialog : Window
27
27
{
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 [ ]
30
30
{
31
31
new Regex ( @"(?<!(?<![\\])([\\]{2})*[\\])[\(\)]" , RegexOptions . Compiled ) ,
32
32
new Regex ( @"(?<!(?<![\\])([\\]{2})*[\\])[\[\]]" , RegexOptions . Compiled ) ,
33
33
new Regex ( @"(?<!(?<![\\])([\\]{2})*[\\])[{}]" , RegexOptions . Compiled ) ,
34
34
new Regex ( @"(?<!(?<![\\])([\\]{2})*[\\])[<>]" , RegexOptions . Compiled )
35
35
} ) . ToList ( ) ;
36
36
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 > ( ) ;
39
39
40
- private string [ ] openingBrackets = new string [ ] { "(" , "[" , "{" , "<" } ;
40
+ private readonly string [ ] openingBrackets = new string [ ] { "(" , "[" , "{" , "<" } ;
41
41
42
42
private string lastMatchesText = "" ;
43
- private int lastSelectionStart = 0 ;
44
- private int lastSelectionLength = 0 ;
43
+ private int lastSelectionStart ;
44
+ private int lastSelectionLength ;
45
45
46
- private bool mustSelectEditor = false ;
46
+ private bool mustSelectEditor ;
47
47
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 ( ) ;
50
50
51
51
private static readonly Regex cSharpReplaceSpecialZoneCleaningRegex = new Regex ( @"(?<=^|\s)\#(?<name>\w+)(?=\s).*(?<=\s)\#end\k<name>(?=\s|$)\s*" , RegexOptions . Singleline | RegexOptions . Compiled ) ;
52
52
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)
560
560
SetToHistory ( ) ;
561
561
562
562
int files = 0 ;
563
- string text = GetCurrentText ( ) ;
563
+ string text ;
564
564
565
565
Regex regex = new Regex ( RegexEditor . Text , GetRegexOptions ( ) ) ;
566
566
567
- int nbrOfElementToReplace = Config . Instance . TextSourceOn == RegexTextSource . Directory ? 0 : regex . Matches ( text ) . Count ;
567
+ int nbrOfElementToReplace = 0 ;
568
568
569
569
if ( CSharpReplaceCheckbox . IsChecked . GetValueOrDefault ( ) )
570
570
{
@@ -584,19 +584,19 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
584
584
{
585
585
if ( TryOpen ? . Invoke ( fileName , false ) ?? false )
586
586
{
587
- text = GetText ( ) ;
587
+ text = script . Before ( GetText ( ) ) ;
588
588
int matchesCount = regex . Matches ( text ) . Count ;
589
589
590
590
if ( matchesCount > 0 )
591
591
{
592
592
index = 0 ;
593
593
594
- SetText ( regex . Replace ( text , match =>
594
+ SetText ( script . After ( regex . Replace ( text , match =>
595
595
{
596
596
index ++ ;
597
597
nbrOfElementToReplace ++ ;
598
598
return script . Replace ( match , index , fileName , nbrOfElementToReplace , files ) ;
599
- } ) ) ;
599
+ } ) ) ) ;
600
600
601
601
try
602
602
{
@@ -609,19 +609,19 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
609
609
}
610
610
else
611
611
{
612
- text = File . ReadAllText ( fileName ) ;
612
+ text = script . Before ( File . ReadAllText ( fileName ) ) ;
613
613
int matchesCount = regex . Matches ( text ) . Count ;
614
614
615
615
if ( matchesCount > 0 )
616
616
{
617
617
index = 0 ;
618
618
619
- File . WriteAllText ( fileName , regex . Replace ( text , match =>
619
+ File . WriteAllText ( fileName , script . After ( regex . Replace ( text , match =>
620
620
{
621
621
index ++ ;
622
622
nbrOfElementToReplace ++ ;
623
623
return script . Replace ( match , index , fileName , nbrOfElementToReplace , files ) ;
624
- } ) ) ;
624
+ } ) ) ) ;
625
625
626
626
files ++ ;
627
627
}
@@ -631,20 +631,24 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
631
631
} ) ;
632
632
break ;
633
633
case RegexTextSource . CurrentSelection :
634
+ text = script . Before ( GetCurrentText ( ) ) ;
635
+ nbrOfElementToReplace = regex . Matches ( text ) . Count ;
634
636
lastSelectionStart = GetSelectionStartIndex ? . Invoke ( ) ?? 0 ;
635
637
lastSelectionLength = GetSelectionLength ? . Invoke ( ) ?? 0 ;
636
- SetSelectedText ( regex . Replace ( text , match =>
638
+ SetSelectedText ( script . After ( regex . Replace ( text , match =>
637
639
{
638
640
index ++ ;
639
641
return script . Replace ( match , index , GetCurrentFileName ? . Invoke ( ) ?? string . Empty , index , 0 ) ;
640
- } ) ) ;
642
+ } ) ) ) ;
641
643
break ;
642
644
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 =>
644
648
{
645
649
index ++ ;
646
650
return script . Replace ( match , index , GetCurrentFileName ? . Invoke ( ) ?? string . Empty , index , 0 ) ;
647
- } ) ) ;
651
+ } ) ) ) ;
648
652
break ;
649
653
}
650
654
}
@@ -698,9 +702,13 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
698
702
699
703
break ;
700
704
case RegexTextSource . CurrentSelection :
705
+ text = GetCurrentText ( ) ;
706
+ nbrOfElementToReplace = regex . Matches ( text ) . Count ;
701
707
SetSelectedText ( regex . Replace ( text , ReplaceEditor . Text ) ) ;
702
708
break ;
703
709
default :
710
+ text = GetCurrentText ( ) ;
711
+ nbrOfElementToReplace = regex . Matches ( text ) . Count ;
704
712
SetText ( regex . Replace ( text , ReplaceEditor . Text ) ) ;
705
713
break ;
706
714
}
@@ -779,7 +787,7 @@ private void ExtractMatchesButton_Click(object sender, RoutedEventArgs e)
779
787
780
788
void Extract ( string text , string fileName = "" )
781
789
{
782
- List < Match > matches = regex . Matches ( text )
790
+ List < Match > matches = regex . Matches ( ( string ) script ? . Before ( text ) ?? text )
783
791
. Cast < Match > ( )
784
792
. ToList ( ) ;
785
793
@@ -819,7 +827,8 @@ void Extract(string text, string fileName = "")
819
827
820
828
try
821
829
{
822
- SetTextInNew ( sb . ToString ( ) ) ;
830
+ string result = sb . ToString ( ) ;
831
+ SetTextInNew ( script ? . After ( result ) ?? result ) ;
823
832
}
824
833
catch { }
825
834
}
@@ -1125,7 +1134,6 @@ private void Root_SizeChanged(object sender, SizeChangedEventArgs e)
1125
1134
1126
1135
RegexHistoryPopup . IsOpen = false ;
1127
1136
ReplaceHistoryPopup . IsOpen = false ;
1128
- SetMaxSizes ( ) ;
1129
1137
}
1130
1138
catch { }
1131
1139
}
@@ -1154,24 +1162,6 @@ private void SaveWindowPosition()
1154
1162
catch { }
1155
1163
}
1156
1164
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
-
1175
1165
private void Root_Loaded ( object sender , RoutedEventArgs e )
1176
1166
{
1177
1167
try
0 commit comments