Skip to content

Commit 67fa2d5

Browse files
author
Sébastien Geiser
committed
To C# in progress
1 parent 1d4a1ad commit 67fa2d5

File tree

8 files changed

+135
-51
lines changed

8 files changed

+135
-51
lines changed

RegexDialog/RegExToolDialog.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@
295295
<MenuItem Header="Paste" Command="ApplicationCommands.Paste"/>
296296
<Separator/>
297297
<MenuItem Name="cmiRegexCopyForOnOneLine" Header="Copy on one line" Click="CmiRegexCopyForOnOneLine_Click"/>
298+
<MenuItem Name="CmiRegexCopyForCSharpString" Header="Copy for C# string" Click="CmiRegexCopyForCSharpString_Click" />
298299
<MenuItem Name="cmiRegexCopyForXml" Header="Copy for XML" Click="CmiRegexCopyForXml_Click"/>
299300
<MenuItem Name="cmiRegexPasteFromXml" Header="Paste from XML" Click="CmiRegexPasteFromXml_Click"/>
300301
<Separator />
@@ -397,6 +398,8 @@
397398
<MenuItem Header="Copy" Command="ApplicationCommands.Copy"/>
398399
<MenuItem Header="Paste" Command="ApplicationCommands.Paste"/>
399400
<Separator />
401+
<MenuItem Name="CmiReplaceCopyForCSharpString" Header="Copy for C# string" Click="CmiReplaceCopyForCSharpString_Click" />
402+
<Separator />
400403
<MenuItem Header="Select All" Command="ApplicationCommands.SelectAll"/>
401404
</ContextMenu>
402405
</avalonEdit:TextEditor.ContextMenu>

RegexDialog/RegExToolDialog.xaml.cs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
using Newtonsoft.Json;
66
using Ookii.Dialogs.Wpf;
77
using System;
8+
using System.CodeDom;
9+
using System.CodeDom.Compiler;
810
using System.Collections.Generic;
911
using System.Collections.ObjectModel;
12+
using System.Diagnostics;
1013
using System.IO;
1114
using System.Linq;
1215
using System.Text;
@@ -85,7 +88,7 @@ private string InjectInReplaceScript(string replaceScript)
8588
.RegexReplace(@"\s*//(?<type>match|group).*//end\k<type>", string.Empty, RegexOptions.Singleline)
8689
.RegexReplace("//capture(?<keep>.*)//endcapture", "${keep}", RegexOptions.Singleline));
8790

88-
public string CSharpTextSourceScript => Res.CSharpTextSourceContainer
91+
public string CSharpTextSourceScript => Res.TextSourceContainer
8992
.Replace("//code", cSharpReplaceSpecialZoneCleaningRegex.Replace(TextSourceEditor.Text, string.Empty))
9093
.Replace("//usings", cSharpReplaceUsingsPartRegex.Match(TextSourceEditor.Text).Groups["usings"].Value);
9194

@@ -575,7 +578,7 @@ List<RegexResult> GetMatchesFor(string text, string fileName = "", int selection
575578
}
576579
else if (Config.Instance.TextSourceOn == RegexTextSource.CSharpScript)
577580
{
578-
dynamic sourceScript = CSharpTextSourceScript;
581+
dynamic sourceScript = csEval.LoadCode(CSharpTextSourceScript);
579582

580583
MatchResultsTreeView.ItemsSource = GetMatchesFor(sourceScript.Get().ToString(), "script");
581584

@@ -902,7 +905,7 @@ void Extract(string text, string fileName = "")
902905
}
903906
else if (Config.Instance.TextSourceOn == RegexTextSource.CSharpScript)
904907
{
905-
dynamic sourceScript = CSharpTextSourceScript;
908+
dynamic sourceScript = csEval.LoadCode(CSharpTextSourceScript);
906909
Extract(sourceScript.Get().ToString(), "script");
907910
}
908911
else
@@ -1616,6 +1619,15 @@ private void InsertInReplaceFromContextMenu_Click(object sender, RoutedEventArgs
16161619
catch { }
16171620
}
16181621

1622+
private void CmiReplaceCopyForCSharpString_Click(object sender, RoutedEventArgs e)
1623+
{
1624+
try
1625+
{
1626+
Clipboard.SetText((ReplaceEditor.SelectionLength > 0 ? ReplaceEditor.SelectedText : ReplaceEditor.Text).ToLiteral());
1627+
}
1628+
catch { }
1629+
}
1630+
16191631
private void PutInRegexHistory_Click(object sender, RoutedEventArgs e)
16201632
{
16211633
try
@@ -1805,6 +1817,15 @@ private void CmiRegexCopyForOnOneLine_Click(object sender, RoutedEventArgs e)
18051817
catch { }
18061818
}
18071819

1820+
private void CmiRegexCopyForCSharpString_Click(object sender, RoutedEventArgs e)
1821+
{
1822+
try
1823+
{
1824+
Clipboard.SetText((RegexEditor.SelectionLength > 0 ? RegexEditor.SelectedText : RegexEditor.Text).ToLiteral());
1825+
}
1826+
catch { }
1827+
}
1828+
18081829
private void CmiRegexCopyForXml_Click(object sender, RoutedEventArgs e)
18091830
{
18101831
try
@@ -2035,12 +2056,13 @@ private void ExportToVisualStudio_Click(object sender, RoutedEventArgs e)
20352056
VistaFolderBrowserDialog folderBrowserDialog = new VistaFolderBrowserDialog()
20362057
{
20372058
ShowNewFolderButton = true,
2059+
SelectedPath = @"C:\Projets"
20382060
};
20392061

20402062
Ookii.Dialogs.WinForms.InputDialog inputDialog = new Ookii.Dialogs.WinForms.InputDialog()
20412063
{
20422064
Content = "give a name for your project/solution :",
2043-
Input = "MySolution"
2065+
Input = "TestRegexSol"
20442066
};
20452067

20462068
if (folderBrowserDialog.ShowDialog(this) == true && inputDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
@@ -2058,8 +2080,15 @@ private void ExportToVisualStudio_Click(object sender, RoutedEventArgs e)
20582080
string projectDirectory = Path.Combine(solutionDirectory, projectName);
20592081
string projectFile = Path.Combine(projectDirectory, $"{projectName}.csproj");
20602082
string entryFile = Path.Combine(projectDirectory, "Program.cs");
2083+
string replaceFile = Path.Combine(projectDirectory, "CSharpReplaceContainer.cs");
2084+
string textSourceFile = Path.Combine(projectDirectory, "TextSourceContainer.cs");
20612085
string projectGuid = Guid.NewGuid().ToString();
20622086

2087+
string programCode = Res.VSProgram
2088+
.Replace("projectname", projectName)
2089+
.Replace("$pattern$", Config.Instance.RegexEditorText.ToLiteral())
2090+
.Replace("$replacement$", Config.Instance.ReplaceEditorText.ToLiteral());
2091+
20632092
Directory.CreateDirectory(projectDirectory);
20642093

20652094
// Write solution file
@@ -2084,14 +2113,30 @@ private void ExportToVisualStudio_Click(object sender, RoutedEventArgs e)
20842113
MessageBoxButton.YesNo,
20852114
MessageBoxImage.Question) == MessageBoxResult.Yes)
20862115
{
2087-
File.WriteAllText(projectFile,
2088-
Res.VSProject);
2116+
File.WriteAllText(projectFile, Res.VSProject);
2117+
}
2118+
2119+
if (Config.Instance.CSharpReplaceMode)
2120+
{
2121+
File.WriteAllText(replaceFile, ReplaceScriptForMatch);
2122+
}
2123+
2124+
if (Config.Instance.TextSourceOn == RegexTextSource.CSharpScript)
2125+
{
2126+
File.WriteAllText(textSourceFile, CSharpTextSourceScript);
20892127
}
20902128

20912129
// Write Entry file
2092-
File.WriteAllText(entryFile,
2093-
Res.VSProgram
2094-
.Replace("projectname", projectName));
2130+
if (!File.Exists(entryFile)
2131+
|| MessageBox.Show($"The entry file \"{entryFile}\" already exists.\r\nDo you want to override it ?",
2132+
"Entry file override",
2133+
MessageBoxButton.YesNo,
2134+
MessageBoxImage.Question) == MessageBoxResult.Yes)
2135+
{
2136+
File.WriteAllText(entryFile, programCode);
2137+
}
2138+
2139+
Process.Start($"\"{solutionFile}\"");
20952140
}
20962141
}
20972142
}

RegexDialog/RegexDialog.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@
262262
<Compile Include="Converters\ExpressionEvalConverter.cs" />
263263
<Compile Include="Model\RegexFileResult.cs" />
264264
<Compile Include="RegexPatternIndenter.cs" />
265-
<Resource Include="Resources\CSharpTextSourceContainer.cs" />
265+
<Resource Include="Resources\TextSourceContainer.cs" />
266266
<Compile Include="Res.Designer.cs">
267267
<AutoGen>True</AutoGen>
268268
<DesignTime>True</DesignTime>
@@ -317,6 +317,7 @@
317317
<EmbeddedResource Include="Res.resx">
318318
<Generator>ResXFileCodeGenerator</Generator>
319319
<LastGenOutput>Res.Designer.cs</LastGenOutput>
320+
<SubType>Designer</SubType>
320321
</EmbeddedResource>
321322
</ItemGroup>
322323
<ItemGroup>

RegexDialog/Res.Designer.cs

Lines changed: 43 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RegexDialog/Res.resx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,6 @@
121121
<data name="CSharpReplaceContainer" type="System.Resources.ResXFileRef, System.Windows.Forms">
122122
<value>resources\csharpreplacecontainer.cs;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
123123
</data>
124-
<data name="CSharpTextSourceContainer" type="System.Resources.ResXFileRef, System.Windows.Forms">
125-
<value>Resources\CSharpTextSourceContainer.cs;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
126-
</data>
127124
<data name="RegexLanguageElements" type="System.Resources.ResXFileRef, System.Windows.Forms">
128125
<value>resources\regexlanguageelements.json;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
129126
</data>
@@ -136,6 +133,9 @@
136133
<data name="Replace_syntax_color" type="System.Resources.ResXFileRef, System.Windows.Forms">
137134
<value>resources\replace_syntax_color.xshd;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;iso-8859-1</value>
138135
</data>
136+
<data name="TextSourceContainer" type="System.Resources.ResXFileRef, System.Windows.Forms">
137+
<value>resources\textsourcecontainer.cs;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
138+
</data>
139139
<data name="VSProgram" type="System.Resources.ResXFileRef, System.Windows.Forms">
140140
<value>resources\vsprogram.cs;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
141141
</data>

RegexDialog/Resources/CSharpTextSourceContainer.cs renamed to RegexDialog/Resources/TextSourceContainer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using RegexDialog;
1010
//usings
1111

12-
public class CSharpTextSourceContainer
12+
public class TextSourceContainer
1313
{
1414
public object Get()
1515
{

RegexDialog/Resources/VSProgram.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
//usings
1+
using System;
2+
using System.Text;
3+
using System.Text.RegularExpressions;
24

35
namespace projectname
46
{
57
class Program
68
{
9+
private static string pattern = "$pattern$";
10+
private static string replacement = "$replacement$";
11+
private static Regex regex = new Regex(pattern);
12+
13+
private static string input = (new TextSourceContainer()).Get().ToString();
14+
715
static void Main(string[] args)
816
{
9-
//code
17+
// To make a replace
18+
string replace = regex.Replace(input, replacement);
19+
Console.WriteLine(replace);
20+
1021
}
1122
}
1223
}

RegexDialog/Utils/Extensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using System;
2+
using System.CodeDom;
3+
using System.CodeDom.Compiler;
4+
using System.IO;
25
using System.Linq;
36
using System.Reflection;
47
using System.Security;
@@ -35,5 +38,18 @@ public static string RegexReplace(this string input, string pattern, string repl
3538
{
3639
return Regex.Replace(input, pattern, replacement, options);
3740
}
41+
42+
public static string ToLiteral(this string input)
43+
{
44+
using (var writer = new StringWriter())
45+
{
46+
using (var provider = CodeDomProvider.CreateProvider("CSharp"))
47+
{
48+
provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, new CodeGeneratorOptions { IndentString = "\t" });
49+
var literal = writer.ToString();
50+
return literal.Replace(string.Format("\" +{0}\t\"", Environment.NewLine), "").Trim('"');
51+
}
52+
}
53+
}
3854
}
3955
}

0 commit comments

Comments
 (0)