Skip to content

Commit 549579e

Browse files
author
Sébastien Geiser
committed
Some Refactoring
1 parent c362e8f commit 549579e

File tree

8 files changed

+90
-71
lines changed

8 files changed

+90
-71
lines changed

CSharpRegexTools4Npp/BNpp.cs

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ public static void SelectTextAndShow(int start, int end)
188188
}
189189

190190
string beforeText = allText.Substring(0, startToUse);
191-
string beforeTextInDefaultEncoding = BEncoding.GetScintillaTextFromUtf8Text(beforeText, out int defaultStart);
191+
BEncoding.GetScintillaTextFromUtf8Text(beforeText, out int defaultStart);
192192
string endText = allText.Substring(0, endToUse);
193-
string endTextInDefaultEncoding = BEncoding.GetScintillaTextFromUtf8Text(endText, out int defaultEnd);
193+
BEncoding.GetScintillaTextFromUtf8Text(endText, out int defaultEnd);
194194

195195
Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GOTOPOS, defaultStart, 0);
196196
Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_SETSELECTIONEND, defaultEnd, 0);
@@ -232,9 +232,9 @@ public static void AddSelection(int start, int end)
232232
}
233233

234234
string beforeText = allText.Substring(0, startToUse);
235-
string beforeTextInDefaultEncoding = BEncoding.GetScintillaTextFromUtf8Text(beforeText, out int defaultStart);
235+
BEncoding.GetScintillaTextFromUtf8Text(beforeText, out int defaultStart);
236236
string endText = allText.Substring(0, endToUse);
237-
string endTextInDefaultEncoding = BEncoding.GetScintillaTextFromUtf8Text(endText, out int defaultEnd);
237+
BEncoding.GetScintillaTextFromUtf8Text(endText, out int defaultEnd);
238238

239239
Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_ADDSELECTION, defaultStart, defaultEnd);
240240
}
@@ -272,77 +272,55 @@ internal static class BEncoding
272272
/// </summary>
273273
public static string GetUtf8TextFromScintillaText(string scText)
274274
{
275-
string result = "";
276-
int iEncoding = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0);
277-
278-
switch (iEncoding)
275+
switch ((int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0))
279276
{
280277
case 65001: // UTF8
281-
result = utf8.GetString(Encoding.Default.GetBytes(scText));
282-
break;
278+
return utf8.GetString(Encoding.Default.GetBytes(scText));
283279
default:
284280
Encoding ANSI = Encoding.GetEncoding(1252);
285281

286282
byte[] ansiBytes = ANSI.GetBytes(scText);
287283
byte[] utf8Bytes = Encoding.Convert(ANSI, Encoding.UTF8, ansiBytes);
288284

289-
result = Encoding.UTF8.GetString(utf8Bytes);
290-
break;
285+
return Encoding.UTF8.GetString(utf8Bytes);
291286
}
292-
293-
return result;
294287
}
295288

296289
/// <summary>
297290
/// Convertit le texte spécifier de l'encodage C# (UTF8) à l'encodage document Notepad++ courant
298291
/// </summary>
299292
public static string GetScintillaTextFromUtf8Text(string utf8Text)
300293
{
301-
string result = "";
302-
int iEncoding = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0);
303-
304-
switch (iEncoding)
294+
switch ((int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0))
305295
{
306296
case 65001: // UTF8
307-
result = Encoding.Default.GetString(utf8.GetBytes(utf8Text));
308-
break;
297+
return Encoding.Default.GetString(utf8.GetBytes(utf8Text));
309298
default:
310299
Encoding ANSI = Encoding.GetEncoding(1252);
311300

312301
byte[] utf8Bytes = utf8.GetBytes(utf8Text);
313302
byte[] ansiBytes = Encoding.Convert(Encoding.UTF8, ANSI, utf8Bytes);
314303

315-
result = ANSI.GetString(ansiBytes);
316-
break;
304+
return ANSI.GetString(ansiBytes);
317305
}
318-
319-
return result;
320306
}
321307

322308
/// <summary>
323309
/// Convertit le texte spécifier de l'encodage C# (UTF8) à l'encodage document Notepad++ courant
324310
/// </summary>
325311
public static string GetScintillaTextFromUtf8Text(string utf8Text, out int length)
326312
{
327-
string result = "";
328-
int iEncoding = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0);
329-
330313
byte[] utf8Bytes = utf8.GetBytes(utf8Text);
331314
length = utf8Bytes.Length;
332-
333-
switch (iEncoding)
315+
switch ((int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0))
334316
{
335317
case 65001: // UTF8
336-
result = Encoding.Default.GetString(utf8.GetBytes(utf8Text));
337-
break;
318+
return Encoding.Default.GetString(utf8.GetBytes(utf8Text));
338319
default:
339320
Encoding ANSI = Encoding.GetEncoding(1252);
340321
byte[] ansiBytes = Encoding.Convert(Encoding.UTF8, ANSI, utf8Bytes);
341-
result = ANSI.GetString(ansiBytes);
342-
break;
322+
return ANSI.GetString(ansiBytes);
343323
}
344-
345-
return result;
346324
}
347325
}
348326
}

RegexDialog/RegExToolDialog.xaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,12 @@
340340
<TextBlock>You must use at least one <Bold>return</Bold> keyword to return a string</TextBlock>
341341
<TextBlock>You can use the following variables :</TextBlock>
342342
<TextBlock>- <Bold>match</Bold> : Contains the current Regex Match</TextBlock>
343-
<TextBlock>- <Bold>index</Bold> : The index of the current match in the current file</TextBlock>
343+
<TextBlock>- <Bold>matchIndex</Bold> : The index of the current match in the current file</TextBlock>
344344
<TextBlock>- <Bold>fileName</Bold> : (If in directory search) the fileName where the match was found</TextBlock>
345345
<TextBlock>- <Bold>globalIndex</Bold> : (If in directory search) the index of the match in all files</TextBlock>
346346
<TextBlock>- <Bold>fileIndex</Bold> : (If in directory search) the index of the file</TextBlock>
347-
<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>
347+
<TextBlock Margin="0,5,0,0">You can add specific usings between <Bold>#usings</Bold> and <Bold>#usings</Bold></TextBlock>
348+
<TextBlock>You can declare shared variables, properties or methods putting code between <Bold>#global</Bold> and <Bold>#endglobal</Bold></TextBlock>
348349
<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>
349350
<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>
350351
</StackPanel>
@@ -356,10 +357,11 @@
356357
Name="ReplaceEditor"
357358
FontFamily="Consolas"
358359
FontSize="12"
359-
ScrollViewer.HorizontalScrollBarVisibility="Auto"
360-
TextChanged="ReplaceEditor_TextChanged" Margin="-1,-1,1,1"
360+
ScrollViewer.HorizontalScrollBarVisibility="Auto"
361+
Margin="-1,-1,1,1"
361362
ShowLineNumbers="{Binding ShowLinesNumbersReplaceEditorOption}">
362363
<i:Interaction.Behaviors>
364+
<behaviors:SimplePropertyBindingBehavior PropertyName="Text" Value="{Binding ReplaceEditorText}" PropertyChangedTriggerEventName="TextChanged" />
363365
<behaviors:SimplePropertyBindingBehavior PropertyName="Options.ShowSpaces" Value="{Binding ShowSpaceCharsReplaceEditorOption}" />
364366
<behaviors:SimplePropertyBindingBehavior PropertyName="Options.ShowTabs" Value="{Binding ShowSpaceCharsReplaceEditorOption}" />
365367
<behaviors:SimplePropertyBindingBehavior PropertyName="Options.ShowEndOfLine" Value="{Binding ShowEndOfLinesReplaceEditorOption}" />

RegexDialog/RegExToolDialog.xaml.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Collections.ObjectModel;
1010
using System.IO;
1111
using System.Linq;
12+
using System.Reflection;
1213
using System.Text;
1314
using System.Text.RegularExpressions;
1415
using System.Windows;
@@ -46,10 +47,13 @@ public partial class RegExToolDialog : Window
4647

4748
private bool mustSelectEditor;
4849

50+
private readonly IEvaluator csEval = CSScript.Evaluator;
51+
4952
private readonly BracketColorizer currentBracketColorizer = new BracketColorizer();
5053
private readonly BracketColorizer matchingBracketColorizer = new BracketColorizer();
5154

5255
private static readonly Regex cSharpReplaceSpecialZoneCleaningRegex = new Regex(@"(?<=^|\s)\#(?<name>\w+)(?=\s).*(?<=\s)\#end\k<name>(?=\s|$)\s*", RegexOptions.Singleline | RegexOptions.Compiled);
56+
private static readonly Regex cSharpReplaceUsingsPartRegex = new Regex(@"(?<=^|\s)\#usings(?=\s)(?<usings>.*)(?<=\s)\#endusings(?=\s|$)", RegexOptions.Singleline | RegexOptions.Compiled);
5357
private static readonly Regex cSharpReplaceGlobalPartRegex = new Regex(@"(?<=^|\s)\#global(?=\s)(?<global>.*)(?<=\s)\#endglobal(?=\s|$)", RegexOptions.Singleline | RegexOptions.Compiled);
5458
private static readonly Regex cSharpReplaceBeforePartRegex = new Regex(@"(?<=^|\s)\#before(?=\s)(?<before>.*)(?<=\s)\#endbefore(?=\s|$)", RegexOptions.Singleline | RegexOptions.Compiled);
5559
private static readonly Regex cSharpReplaceAfterPartRegex = new Regex(@"(?<=^|\s)\#after(?=\s)(?<after>.*)(?<=\s)\#endafter(?=\s|$)", RegexOptions.Singleline | RegexOptions.Compiled);
@@ -61,8 +65,9 @@ public object ReplaceScript
6165
Match beforeMatch = cSharpReplaceBeforePartRegex.Match(ReplaceEditor.Text);
6266
Match afterMatch = cSharpReplaceAfterPartRegex.Match(ReplaceEditor.Text);
6367

64-
return CSScript.Evaluator.LoadCode(Res.CSharpReplaceContainer
68+
return csEval.LoadCode(Res.CSharpReplaceContainer
6569
.Replace("//code", cSharpReplaceSpecialZoneCleaningRegex.Replace(ReplaceEditor.Text, string.Empty))
70+
.Replace("//usings", cSharpReplaceUsingsPartRegex.Match(ReplaceEditor.Text).Groups["usings"].Value)
6671
.Replace("//global", cSharpReplaceGlobalPartRegex.Match(ReplaceEditor.Text).Groups["global"].Value)
6772
.Replace("//before", beforeMatch.Success ? beforeMatch.Groups["before"].Value : "return text;")
6873
.Replace("//after", afterMatch.Success ? afterMatch.Groups["after"].Value : "return text;"));
@@ -213,9 +218,13 @@ private void Init()
213218
// Construit l'arbre des éléments de languages de replace.
214219
BuildReplaceLanguageElements();
215220

216-
// Rétablit le contenu des éditeur
221+
csEval
222+
.ReferenceAssemblyOf(this)
223+
.ReferenceDomainAssemblies()
224+
.ReferenceAssemblyByName("System.Xml")
225+
.ReferenceAssemblyByName("Newtonsoft.Json");
226+
217227
RegexEditor.Text = Config.Instance.RegexEditorText;
218-
ReplaceEditor.Text = Config.Instance.ReplaceEditorText;
219228

220229
Left = Config.Instance.DialogLeft ?? Left;
221230
Top = Config.Instance.DialogTop ?? Top;
@@ -935,11 +944,6 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs
935944
RegexEditor.TextArea.Caret.PositionChanged -= Caret_PositionChanged;
936945
}
937946

938-
private void ReplaceEditor_TextChanged(object sender, EventArgs e)
939-
{
940-
Config.Instance.ReplaceEditorText = ReplaceEditor.Text;
941-
}
942-
943947
private void MatchResultsTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
944948
{
945949
try

RegexDialog/RegexDialog.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@
251251
<Compile Include="Converters\ExpressionEvalConverter.cs" />
252252
<Compile Include="Model\RegexFileResult.cs" />
253253
<Compile Include="RegexPatternIndenter.cs" />
254+
<Resource Include="Resources\CSharpTextSourceContainer.cs" />
254255
<Compile Include="Utils\Config.cs" />
255256
<Compile Include="Properties\AssemblyInfo.cs" />
256257
<Compile Include="Model\RegexCaptureResult.cs" />

RegexDialog/Res.Designer.cs

Lines changed: 31 additions & 14 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@
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>
124127
<data name="RegexLanguageElements" type="System.Resources.ResXFileRef, System.Windows.Forms">
125128
<value>resources\regexlanguageelements.json;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;Windows-1252</value>
126129
</data>

RegexDialog/Resources/CSharpReplaceContainer.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
1-
//using Newtonsoft.Json;
21
using System.Collections;
32
using System.Collections.Generic;
43
using System.Collections.ObjectModel;
54
using System.ComponentModel;
6-
//using System.Data;
7-
using System.Diagnostics;
85
using System.Globalization;
9-
using System.IO;
106
using System.Linq;
11-
using System.Reflection;
127
using System.Text;
138
using System.Text.RegularExpressions;
149
using System.Windows;
15-
using System.Xml;
16-
//using RegexDialog;
10+
using RegexDialog;
11+
//usings
1712

1813
public class Script
1914
{
2015
//global
2116

22-
public string Replace(Match match, int index, string fileName, int globalIndex, int fileIndex)
17+
public string Replace(Match match, int matchIndex, string fileName, int globalIndex, int fileIndex)
2318
{
2419
//code
2520
}
2621

27-
public string Replace(Match match, Group group, int index, string fileName, int globalIndex, int fileIndex)
22+
public string Replace(Match match, Group group, int matchIndex, string fileName, int globalIndex, int fileIndex)
2823
{
2924
//code
3025
}
3126

32-
public string Replace(Match match, Group group, Capture capture, int index, string fileName, int globalIndex, int fileIndex)
27+
public string Replace(Match match, Group group, Capture capture, int matchIndex, string fileName, int globalIndex, int fileIndex)
3328
{
3429
//code
3530
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.ComponentModel;
5+
using System.Globalization;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Text.RegularExpressions;
9+
using System.Windows;
10+
using RegexDialog;
11+
//usings
12+
13+
public class CSharpTextSourceContainer
14+
{
15+
public object Get()
16+
{
17+
//code
18+
}
19+
}

0 commit comments

Comments
 (0)