Skip to content

Commit d6d432a

Browse files
author
Sébastien Geiser
committed
fileNames in Before and after
1 parent dd1b62a commit d6d432a

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

RegexDialog/RegExToolDialog.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@
346346
<TextBlock>- <Bold>fileIndex</Bold> : (If in directory search) the index of the file</TextBlock>
347347
<TextBlock Margin="0,5,0,0">You can add specific usings between <Bold>#usings</Bold> and <Bold>#usings</Bold></TextBlock>
348348
<TextBlock>You can declare shared variables, properties or methods putting code between <Bold>#global</Bold> and <Bold>#endglobal</Bold></TextBlock>
349-
<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>
350-
<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>
349+
<TextBlock>Between <Bold>#before</Bold> and <Bold>#endbefore</Bold> you get the <Bold>text</Bold> and the<Bold>fileName</Bold> before it is used as input of the regex by returning a string you can redefine it.</TextBlock>
350+
<TextBlock>Between <Bold>#after</Bold> and <Bold>#endafter</Bold> you get the <Bold>text</Bold> and the<Bold>fileName</Bold> after it is processed by the regex by returning a string you can modify it a last time.</TextBlock>
351351
</StackPanel>
352352
</CheckBox.ToolTip>
353353
</CheckBox>

RegexDialog/RegExToolDialog.xaml.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
616616
{
617617
if (TryOpen?.Invoke(fileName, false) ?? false)
618618
{
619-
text = script.Before(GetText());
619+
text = script.Before(GetText(), fileName);
620620
int matchesCount = regex.Matches(text).Count;
621621

622622
if (matchesCount > 0)
@@ -628,7 +628,7 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
628628
index++;
629629
nbrOfElementToReplace++;
630630
return script.Replace(match, index, fileName, nbrOfElementToReplace, files);
631-
})));
631+
}), fileName, null));
632632

633633
try
634634
{
@@ -641,7 +641,7 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
641641
}
642642
else
643643
{
644-
text = script.Before(File.ReadAllText(fileName));
644+
text = script.Before(File.ReadAllText(fileName), fileName);
645645
int matchesCount = regex.Matches(text).Count;
646646

647647
if (matchesCount > 0)
@@ -653,7 +653,7 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
653653
index++;
654654
nbrOfElementToReplace++;
655655
return script.Replace(match, index, fileName, nbrOfElementToReplace, files);
656-
})));
656+
}), fileName, null));
657657

658658
files++;
659659
}
@@ -662,24 +662,27 @@ private void ReplaceAllButton_Click(object sender, RoutedEventArgs e)
662662
});
663663
break;
664664
case RegexTextSource.CurrentSelection:
665-
text = script.Before(GetCurrentText());
665+
string currentFileName = GetCurrentFileName?.Invoke() ?? string.Empty;
666+
text = script.Before(GetCurrentText(), currentFileName);
666667
nbrOfElementToReplace = regex.Matches(text).Count;
667668
lastSelectionStart = GetSelectionStartIndex?.Invoke() ?? 0;
668669
lastSelectionLength = GetSelectionLength?.Invoke() ?? 0;
670+
669671
SetSelectedText(script.After(regex.Replace(text, match =>
670672
{
671673
index++;
672-
return script.Replace(match, index, GetCurrentFileName?.Invoke() ?? string.Empty, index, 0);
673-
})));
674+
return script.Replace(match, index, currentFileName, index, 0);
675+
}), currentFileName, null));
674676
break;
675677
default:
676-
text = script.Before(GetCurrentText());
678+
currentFileName = GetCurrentFileName?.Invoke() ?? string.Empty;
679+
text = script.Before(GetCurrentText(), currentFileName);
677680
nbrOfElementToReplace = regex.Matches(text).Count;
678681
SetText(script.After(regex.Replace(text, match =>
679682
{
680683
index++;
681-
return script.Replace(match, index, GetCurrentFileName?.Invoke() ?? string.Empty, index, 0);
682-
})));
684+
return script.Replace(match, index, currentFileName, index, 0);
685+
}), currentFileName, null));
683686
break;
684687
}
685688
}
@@ -845,19 +848,24 @@ void Extract(string text, string fileName = "")
845848
}
846849
}
847850

851+
string currentFileName = null;
852+
List<string> fileNames = null;
853+
848854
if (Config.Instance.TextSourceOn == RegexTextSource.Directory)
849855
{
850-
GetFiles().ForEach(fileName => Extract(File.ReadAllText(fileName), fileName));
856+
fileNames = GetFiles();
857+
fileNames.ForEach(fileName => Extract(File.ReadAllText(fileName), fileName));
851858
}
852859
else
853860
{
854-
Extract(GetCurrentText(), GetCurrentFileName?.Invoke() ?? string.Empty);
861+
currentFileName = GetCurrentFileName?.Invoke() ?? string.Empty;
862+
Extract(GetCurrentText(), currentFileName);
855863
}
856864

857865
try
858866
{
859867
string result = sb.ToString();
860-
SetTextInNew(script?.After(result) ?? result);
868+
SetTextInNew(script?.After(result, currentFileName, fileNames) ?? result);
861869
}
862870
catch { }
863871
}

RegexDialog/Resources/CSharpReplaceContainer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public string Replace(Match match, Group group, Capture capture, int matchIndex,
2828
//code
2929
}
3030

31-
public string Before(string text)
31+
public string Before(string text, string fileName)
3232
{
3333
//before
3434
return text;
3535
//end
3636
}
3737

38-
public string After(string text)
38+
public string After(string text, string fileName, List<string> fileNames = null)
3939
{
4040
//after
4141
return text;

0 commit comments

Comments
 (0)