Skip to content

Commit 336c261

Browse files
author
Sébastien Geiser
committed
Export in VS in progress
1 parent 67fa2d5 commit 336c261

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

RegexDialog/RegExToolDialog.xaml.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,20 @@ public partial class RegExToolDialog : Window
5959
private static readonly Regex cSharpReplaceGlobalPartRegex = new Regex(@"(?<=^|\s)\#global(?=\s)(?<global>.*)(?<=\s)\#endglobal(?=\s|$)", RegexOptions.Singleline | RegexOptions.Compiled);
6060
private static readonly Regex cSharpReplaceBeforePartRegex = new Regex(@"(?<=^|\s)\#before(?=\s)(?<before>.*)(?<=\s)\#endbefore(?=\s|$)", RegexOptions.Singleline | RegexOptions.Compiled);
6161
private static readonly Regex cSharpReplaceAfterPartRegex = new Regex(@"(?<=^|\s)\#after(?=\s)(?<after>.*)(?<=\s)\#endafter(?=\s|$)", RegexOptions.Singleline | RegexOptions.Compiled);
62+
private static readonly Regex cSharpScriptsStartOfLinesForAddingTabs = new Regex(@"(?<start>^)(?<notend>[^\r\n])", RegexOptions.Multiline | RegexOptions.Compiled);
6263

6364
private string InjectInReplaceScript(string replaceScript)
6465
{
6566
Match beforeMatch = cSharpReplaceBeforePartRegex.Match(ReplaceEditor.Text);
6667
Match afterMatch = cSharpReplaceAfterPartRegex.Match(ReplaceEditor.Text);
6768

6869
return replaceScript
69-
.Replace("//code", cSharpReplaceSpecialZoneCleaningRegex.Replace(ReplaceEditor.Text, string.Empty))
70-
.Replace("//usings", cSharpReplaceUsingsPartRegex.Match(ReplaceEditor.Text).Groups["usings"].Value)
70+
.Replace("//code",
71+
cSharpScriptsStartOfLinesForAddingTabs.Replace(
72+
cSharpReplaceSpecialZoneCleaningRegex.Replace(ReplaceEditor.Text, string.Empty)
73+
, match => match.Groups["start"].Value + "\t\t" + match.Groups["notend"].Value)
74+
.TrimStart())
75+
.Replace("//usings", cSharpReplaceUsingsPartRegex.Match(ReplaceEditor.Text).Groups["usings"].Value.Trim())
7176
.Replace("//global", cSharpReplaceGlobalPartRegex.Match(ReplaceEditor.Text).Groups["global"].Value)
7277
.Replace("//before", beforeMatch.Success ? beforeMatch.Groups["before"].Value : "return text;")
7378
.Replace("//after", afterMatch.Success ? afterMatch.Groups["after"].Value : "return text;");
@@ -89,7 +94,11 @@ private string InjectInReplaceScript(string replaceScript)
8994
.RegexReplace("//capture(?<keep>.*)//endcapture", "${keep}", RegexOptions.Singleline));
9095

9196
public string CSharpTextSourceScript => Res.TextSourceContainer
92-
.Replace("//code", cSharpReplaceSpecialZoneCleaningRegex.Replace(TextSourceEditor.Text, string.Empty))
97+
.Replace("//code",
98+
cSharpScriptsStartOfLinesForAddingTabs.Replace(
99+
cSharpReplaceSpecialZoneCleaningRegex.Replace(TextSourceEditor.Text, string.Empty)
100+
, match => match.Groups["start"].Value + "\t\t" + match.Groups["notend"].Value)
101+
.TrimStart())
93102
.Replace("//usings", cSharpReplaceUsingsPartRegex.Match(TextSourceEditor.Text).Groups["usings"].Value);
94103

95104
public delegate bool TryOpenDelegate(string fileName, bool onlyIfAlreadyOpen);
@@ -1654,6 +1663,7 @@ private void New_MenuItem_Click(object sender, RoutedEventArgs e)
16541663

16551664
RegexEditor.Text = string.Empty;
16561665
ReplaceEditor.Text = string.Empty;
1666+
TextSourceEditor.Text = string.Empty;
16571667

16581668
regExOptionViewModelsList.ForEach(optionModel => optionModel.Selected = false);
16591669
}
@@ -2083,11 +2093,18 @@ private void ExportToVisualStudio_Click(object sender, RoutedEventArgs e)
20832093
string replaceFile = Path.Combine(projectDirectory, "CSharpReplaceContainer.cs");
20842094
string textSourceFile = Path.Combine(projectDirectory, "TextSourceContainer.cs");
20852095
string projectGuid = Guid.NewGuid().ToString();
2096+
string options = regExOptionViewModelsList.Count(option => option.Selected) == 0
2097+
? "RegexOptions.None"
2098+
: string.Join(" | ",
2099+
regExOptionViewModelsList
2100+
.FindAll(option => option.Selected)
2101+
.Select(option => "RegexOptions." + option.RegexOptions.ToString()));
20862102

20872103
string programCode = Res.VSProgram
20882104
.Replace("projectname", projectName)
20892105
.Replace("$pattern$", Config.Instance.RegexEditorText.ToLiteral())
2090-
.Replace("$replacement$", Config.Instance.ReplaceEditorText.ToLiteral());
2106+
.Replace("$replacement$", Config.Instance.ReplaceEditorText.ToLiteral())
2107+
.Replace("_options_", options);
20912108

20922109
Directory.CreateDirectory(projectDirectory);
20932110

RegexDialog/Resources/VSProgram.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using System.Text;
34
using System.Text.RegularExpressions;
45

@@ -8,7 +9,8 @@ class Program
89
{
910
private static string pattern = "$pattern$";
1011
private static string replacement = "$replacement$";
11-
private static Regex regex = new Regex(pattern);
12+
private static RegexOptions options = _options_;
13+
private static Regex regex = new Regex(pattern, options);
1214

1315
private static string input = (new TextSourceContainer()).Get().ToString();
1416

@@ -18,6 +20,9 @@ static void Main(string[] args)
1820
string replace = regex.Replace(input, replacement);
1921
Console.WriteLine(replace);
2022

23+
//To get all matches
24+
MatchCollection matches = regex.Matches(input);
25+
Console.WriteLine(string.Join("\r\n", matches.Cast<Match>().Select(match => match.Value)));
2126
}
2227
}
2328
}

0 commit comments

Comments
 (0)