Skip to content

Commit 7c66fef

Browse files
author
Sébastien Geiser
committed
CSharp As text source testable and savable
1 parent d6d432a commit 7c66fef

File tree

8 files changed

+124
-104
lines changed

8 files changed

+124
-104
lines changed

CSharpRegexTools4Npp/BNpp.cs

Lines changed: 35 additions & 13 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-
BEncoding.GetScintillaTextFromUtf8Text(beforeText, out int defaultStart);
191+
string beforeTextInDefaultEncoding = BEncoding.GetScintillaTextFromUtf8Text(beforeText, out int defaultStart);
192192
string endText = allText.Substring(0, endToUse);
193-
BEncoding.GetScintillaTextFromUtf8Text(endText, out int defaultEnd);
193+
string endTextInDefaultEncoding = 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-
BEncoding.GetScintillaTextFromUtf8Text(beforeText, out int defaultStart);
235+
string beforeTextInDefaultEncoding = BEncoding.GetScintillaTextFromUtf8Text(beforeText, out int defaultStart);
236236
string endText = allText.Substring(0, endToUse);
237-
BEncoding.GetScintillaTextFromUtf8Text(endText, out int defaultEnd);
237+
string endTextInDefaultEncoding = BEncoding.GetScintillaTextFromUtf8Text(endText, out int defaultEnd);
238238

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

282286
byte[] ansiBytes = ANSI.GetBytes(scText);
283287
byte[] utf8Bytes = Encoding.Convert(ANSI, Encoding.UTF8, ansiBytes);
284288

285-
return Encoding.UTF8.GetString(utf8Bytes);
289+
result = Encoding.UTF8.GetString(utf8Bytes);
290+
break;
286291
}
292+
293+
return result;
287294
}
288295

289296
/// <summary>
290297
/// Convertit le texte spécifier de l'encodage C# (UTF8) à l'encodage document Notepad++ courant
291298
/// </summary>
292299
public static string GetScintillaTextFromUtf8Text(string utf8Text)
293300
{
294-
switch ((int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0))
301+
string result = "";
302+
int iEncoding = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0);
303+
304+
switch (iEncoding)
295305
{
296306
case 65001: // UTF8
297-
return Encoding.Default.GetString(utf8.GetBytes(utf8Text));
307+
result = Encoding.Default.GetString(utf8.GetBytes(utf8Text));
308+
break;
298309
default:
299310
Encoding ANSI = Encoding.GetEncoding(1252);
300311

301312
byte[] utf8Bytes = utf8.GetBytes(utf8Text);
302313
byte[] ansiBytes = Encoding.Convert(Encoding.UTF8, ANSI, utf8Bytes);
303314

304-
return ANSI.GetString(ansiBytes);
315+
result = ANSI.GetString(ansiBytes);
316+
break;
305317
}
318+
319+
return result;
306320
}
307321

308322
/// <summary>
309323
/// Convertit le texte spécifier de l'encodage C# (UTF8) à l'encodage document Notepad++ courant
310324
/// </summary>
311325
public static string GetScintillaTextFromUtf8Text(string utf8Text, out int length)
312326
{
327+
string result = "";
328+
int iEncoding = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0);
329+
313330
byte[] utf8Bytes = utf8.GetBytes(utf8Text);
314331
length = utf8Bytes.Length;
315-
switch ((int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0))
332+
333+
switch (iEncoding)
316334
{
317335
case 65001: // UTF8
318-
return Encoding.Default.GetString(utf8.GetBytes(utf8Text));
336+
result = Encoding.Default.GetString(utf8.GetBytes(utf8Text));
337+
break;
319338
default:
320339
Encoding ANSI = Encoding.GetEncoding(1252);
321340
byte[] ansiBytes = Encoding.Convert(Encoding.UTF8, ANSI, utf8Bytes);
322-
return ANSI.GetString(ansiBytes);
341+
result = ANSI.GetString(ansiBytes);
342+
break;
323343
}
344+
345+
return result;
324346
}
325347
}
326348
}

RegexDialog/RegExToolDialog.xaml

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<BitmapImage x:Key="ShowLinesNumbers" UriSource="Resources/line_numbers.png"/>
3939
<BitmapImage x:Key="ShowSpaces" UriSource="Resources/space.png"/>
4040
<BitmapImage x:Key="ShowReturns" UriSource="Resources/return.png"/>
41+
<BitmapImage x:Key="Play" UriSource="Resources/control_play_blue.png"/>
4142

4243
<Style TargetType="Image">
4344
<Style.Triggers>
@@ -280,15 +281,15 @@
280281
<MenuItem Name="cmiRegexIndent" Header="Auto Indent and set IgnorePatternWhitespace" Click="CmiRegexIndent_Click"/>
281282
<MenuItem Name="cmiRegexSetOnOneLine" Header="Set on one line" Click="CmiRegexSetOnOneLine_Click"/>
282283
<Separator />
283-
<MenuItem Name="cmiRegexCut" Header="Cut" Click="CmiRegexCut_Click"/>
284-
<MenuItem Name="cmiRegexCopy" Header="Copy" Click="CmiRegexCopy_Click"/>
285-
<MenuItem Name="cmiRegexPaste" Header="Paste" Click="CmiRegexPaste_Click"/>
284+
<MenuItem Header="Cut" Command="ApplicationCommands.Cut"/>
285+
<MenuItem Header="Copy" Command="ApplicationCommands.Copy"/>
286+
<MenuItem Header="Paste" Command="ApplicationCommands.Paste"/>
286287
<Separator/>
287288
<MenuItem Name="cmiRegexCopyForOnOneLine" Header="Copy on one line" Click="CmiRegexCopyForOnOneLine_Click"/>
288289
<MenuItem Name="cmiRegexCopyForXml" Header="Copy for XML" Click="CmiRegexCopyForXml_Click"/>
289290
<MenuItem Name="cmiRegexPasteFromXml" Header="Paste from XML" Click="CmiRegexPasteFromXml_Click"/>
290291
<Separator />
291-
<MenuItem Name="cmiRegexSelectAll" Header="Select All" Click="CmiRegexSelectAll_Click"/>
292+
<MenuItem Header="Select All" Command="ApplicationCommands.SelectAll"/>
292293
</ContextMenu>
293294
</avalonEdit:TextEditor.ContextMenu>
294295
</avalonEdit:TextEditor>
@@ -346,8 +347,8 @@
346347
<TextBlock>- <Bold>fileIndex</Bold> : (If in directory search) the index of the file</TextBlock>
347348
<TextBlock Margin="0,5,0,0">You can add specific usings between <Bold>#usings</Bold> and <Bold>#usings</Bold></TextBlock>
348349
<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> 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>
350+
<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>
351+
<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>
351352
</StackPanel>
352353
</CheckBox.ToolTip>
353354
</CheckBox>
@@ -368,26 +369,26 @@
368369
</i:Interaction.Behaviors>
369370
<avalonEdit:TextEditor.ContextMenu>
370371
<ContextMenu Name="cmReplacefieldContextMenu">
371-
<MenuItem Name="cmiReplaceGroupByNumber" Header="Groups by number" >
372+
<MenuItem Name="cmiReplaceGroupByNumber" Header="Groups by number" Visibility="{Binding CSharpReplaceMode, Converter={converters:CustomBoolToVisibilityConverter TrueValue=Collapsed, FalseValue=Visible}}">
372373
<MenuItem.Style>
373374
<Style TargetType="{x:Type MenuItem}">
374375
<EventSetter Event="Click" Handler="InsertInReplaceFromContextMenu_Click" />
375376
</Style>
376377
</MenuItem.Style>
377378
</MenuItem>
378-
<MenuItem Name="cmiReplaceGroupByName" Header="Groups by name" >
379+
<MenuItem Name="cmiReplaceGroupByName" Header="Groups by name" Visibility="{Binding CSharpReplaceMode, Converter={converters:CustomBoolToVisibilityConverter TrueValue=Collapsed, FalseValue=Visible}}">
379380
<MenuItem.Style>
380381
<Style TargetType="{x:Type MenuItem}">
381382
<EventSetter Event="Click" Handler="InsertInReplaceFromContextMenu_Click" />
382383
</Style>
383384
</MenuItem.Style>
384385
</MenuItem>
385-
<Separator/>
386-
<MenuItem Name="cmiReplaceCut" Header="Cut" Click="CmiReplaceCut_Click"/>
387-
<MenuItem Name="cmiReplaceCopy" Header="Copy" Click="CmiReplaceCopy_Click"/>
388-
<MenuItem Name="cmiReplacePaste" Header="Paste" Click="CmiReplacePaste_Click"/>
386+
<Separator Visibility="{Binding CSharpReplaceMode, Converter={converters:CustomBoolToVisibilityConverter TrueValue=Collapsed, FalseValue=Visible}}"/>
387+
<MenuItem Header="Cut" Command="ApplicationCommands.Cut"/>
388+
<MenuItem Header="Copy" Command="ApplicationCommands.Copy"/>
389+
<MenuItem Header="Paste" Command="ApplicationCommands.Paste"/>
389390
<Separator />
390-
<MenuItem Name="cmiReplaceSelectAll" Header="Select All" Click="CmiReplaceSelectAll_Click"/>
391+
<MenuItem Header="Select All" Command="ApplicationCommands.SelectAll"/>
391392
</ContextMenu>
392393
</avalonEdit:TextEditor.ContextMenu>
393394
</avalonEdit:TextEditor>
@@ -700,7 +701,14 @@
700701
<Image Source="{StaticResource ShowReturns}" Width="16" Height="16" ToolTip="Show/Hide end of lines" />
701702
</StackPanel>
702703
</ToggleButton>
703-
704+
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
705+
<Button x:Name="TestCSharpTextSourceButton"
706+
Click="TestCSharpTextSourceButton_Click">
707+
<Image Source="{StaticResource Play}" Width="16" Height="16" ToolTip="Test and show result" />
708+
</Button>
709+
<CheckBox Content="In a new tab"
710+
VerticalAlignment="Center"
711+
IsChecked="{Binding ShowCSharpTextSourceTestInANewTab}"/>
704712
</WrapPanel>
705713
<Border BorderBrush="Gray"
706714
BorderThickness="1">
@@ -711,7 +719,17 @@
711719
SyntaxHighlighting="C#"
712720
ShowLineNumbers="{Binding ShowLinesNumbersCSharpTextSourceEditorOption}"
713721
ScrollViewer.HorizontalScrollBarVisibility="Auto" >
722+
<avalonEdit:TextEditor.ContextMenu>
723+
<ContextMenu>
724+
<MenuItem Header="Cut" Command="ApplicationCommands.Cut"/>
725+
<MenuItem Header="Copy" Command="ApplicationCommands.Copy"/>
726+
<MenuItem Header="Paste" Command="ApplicationCommands.Paste"/>
727+
<Separator />
728+
<MenuItem Header="Select All" Command="ApplicationCommands.SelectAll"/>
729+
</ContextMenu>
730+
</avalonEdit:TextEditor.ContextMenu>
714731
<i:Interaction.Behaviors>
732+
<behaviors:SimplePropertyBindingBehavior PropertyName="Text" Value="{Binding CSharpTextSourceEditorText}" PropertyChangedTriggerEventName="TextChanged" />
715733
<behaviors:SimplePropertyBindingBehavior PropertyName="Options.ShowSpaces" Value="{Binding ShowSpaceCharsCSharpTextSourceEditorOption}" />
716734
<behaviors:SimplePropertyBindingBehavior PropertyName="Options.ShowTabs" Value="{Binding ShowSpaceCharsCSharpTextSourceEditorOption}" />
717735
<behaviors:SimplePropertyBindingBehavior PropertyName="Options.ShowEndOfLine" Value="{Binding ShowEndOfLinesCSharpTextSourceEditorOption}" />

0 commit comments

Comments
 (0)