Skip to content

Commit 47a5f85

Browse files
author
Sébastien Geiser
committed
Refactoring and updating Plugin part
1 parent a270e87 commit 47a5f85

18 files changed

+5445
-57
lines changed

CSharpRegexTools4Npp/BNpp.cs

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace CSharpRegexTools4Npp
66
{
7-
public class BNpp
7+
public static class BNpp
88
{
9-
public static NotepadPPGateway NotepadPP { get; private set; } = new NotepadPPGateway();
9+
public static NotepadPPGateway NotepadPP { get; } = new NotepadPPGateway();
1010

1111
public static ScintillaGateway Scintilla => new ScintillaGateway(PluginBase.GetCurrentScintilla());
1212

@@ -29,8 +29,6 @@ public static string CurrentEOL
2929
case SciMsg.SC_EOL_CR:
3030
eol = "\r";
3131
break;
32-
default:
33-
break;
3432
}
3533

3634
return eol;
@@ -53,7 +51,7 @@ public static string Text
5351
set
5452
{
5553
IScintillaGateway scintilla = new ScintillaGateway(PluginBase.GetCurrentScintilla());
56-
string text = BEncoding.GetScintillaTextFromUtf8Text(value, out int length);
54+
string text = BEncoding.GetScintillaTextFromUtf8Text(value, out _);
5755
scintilla.SetText(text);
5856
}
5957
}
@@ -87,7 +85,7 @@ public static int SelectionEnd
8785
int curPos = (int)Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETSELECTIONEND, 0, 0);
8886
IScintillaGateway scintilla = new ScintillaGateway(PluginBase.GetCurrentScintilla());
8987
string beginingText = scintilla.GetText(curPos);
90-
string text = BEncoding.GetScintillaTextFromUtf8Text(beginingText, out int length);
88+
BEncoding.GetScintillaTextFromUtf8Text(beginingText, out int length);
9189
return length;
9290
}
9391

@@ -106,7 +104,7 @@ public static int SelectionEnd
106104
}
107105

108106
string afterText = allText.Substring(0, endToUse);
109-
string afterTextInDefaultEncoding = BEncoding.GetScintillaTextFromUtf8Text(afterText, out int defaultEnd);
107+
BEncoding.GetScintillaTextFromUtf8Text(afterText, out int defaultEnd);
110108

111109
Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_SETSELECTIONEND, defaultEnd, 0);
112110
}
@@ -138,17 +136,15 @@ public static string SelectedText
138136
{
139137
get
140138
{
141-
IScintillaGateway scintillaGateway = new ScintillaGateway(PluginBase.GetCurrentScintilla());
142-
int start = scintillaGateway.GetSelectionStart().Value;
143-
int end = scintillaGateway.GetSelectionEnd().Value;
144-
145-
return end - start == 0 ? "" : Text.Substring(start, end - start);
139+
IScintillaGateway scintilla = new ScintillaGateway(PluginBase.GetCurrentScintilla());
140+
return BEncoding.GetUtf8TextFromScintillaText(scintilla.GetSelText());
146141
}
147142

148143
set
149144
{
145+
IScintillaGateway scintilla = new ScintillaGateway(PluginBase.GetCurrentScintilla());
150146
string defaultNewText = BEncoding.GetScintillaTextFromUtf8Text(value);
151-
Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_REPLACESEL, 0, defaultNewText);
147+
scintilla.ReplaceSel(defaultNewText);
152148
}
153149
}
154150

@@ -188,9 +184,9 @@ public static void SelectTextAndShow(int start, int end)
188184
}
189185

190186
string beforeText = allText.Substring(0, startToUse);
191-
string beforeTextInDefaultEncoding = BEncoding.GetScintillaTextFromUtf8Text(beforeText, out int defaultStart);
187+
BEncoding.GetScintillaTextFromUtf8Text(beforeText, out int defaultStart);
192188
string endText = allText.Substring(0, endToUse);
193-
string endTextInDefaultEncoding = BEncoding.GetScintillaTextFromUtf8Text(endText, out int defaultEnd);
189+
BEncoding.GetScintillaTextFromUtf8Text(endText, out int defaultEnd);
194190

195191
Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GOTOPOS, defaultStart, 0);
196192
Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_SETSELECTIONEND, defaultEnd, 0);
@@ -232,14 +228,13 @@ public static void AddSelection(int start, int end)
232228
}
233229

234230
string beforeText = allText.Substring(0, startToUse);
235-
string beforeTextInDefaultEncoding = BEncoding.GetScintillaTextFromUtf8Text(beforeText, out int defaultStart);
231+
BEncoding.GetScintillaTextFromUtf8Text(beforeText, out int defaultStart);
236232
string endText = allText.Substring(0, endToUse);
237-
string endTextInDefaultEncoding = BEncoding.GetScintillaTextFromUtf8Text(endText, out int defaultEnd);
233+
BEncoding.GetScintillaTextFromUtf8Text(endText, out int defaultEnd);
238234

239235
Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_ADDSELECTION, defaultStart, defaultEnd);
240236
}
241237

242-
243238
/// <summary>
244239
/// Récupère le texte de la ligne spécifiée
245240
/// </summary>
@@ -265,84 +260,63 @@ public static string GetLineText(int lineNb)
265260
/// </summary>
266261
internal static class BEncoding
267262
{
268-
private static Encoding utf8 = Encoding.UTF8;
263+
private static readonly Encoding utf8 = Encoding.UTF8;
269264

270265
/// <summary>
271266
/// Convertit le texte spécifier de l'encodage du document Notepad++ courant à l'encodage C# (UTF8)
272267
/// </summary>
273268
public static string GetUtf8TextFromScintillaText(string scText)
274269
{
275-
string result = "";
276-
int iEncoding = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0);
277-
278-
switch (iEncoding)
270+
switch ((int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0))
279271
{
280272
case 65001: // UTF8
281-
result = utf8.GetString(Encoding.Default.GetBytes(scText));
282-
break;
273+
return utf8.GetString(Encoding.Default.GetBytes(scText));
283274
default:
284275
Encoding ANSI = Encoding.GetEncoding(1252);
285276

286277
byte[] ansiBytes = ANSI.GetBytes(scText);
287278
byte[] utf8Bytes = Encoding.Convert(ANSI, Encoding.UTF8, ansiBytes);
288279

289-
result = Encoding.UTF8.GetString(utf8Bytes);
290-
break;
280+
return Encoding.UTF8.GetString(utf8Bytes);
291281
}
292-
293-
return result;
294282
}
295283

296284
/// <summary>
297285
/// Convertit le texte spécifier de l'encodage C# (UTF8) à l'encodage document Notepad++ courant
298286
/// </summary>
299287
public static string GetScintillaTextFromUtf8Text(string utf8Text)
300288
{
301-
string result = "";
302-
int iEncoding = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0);
303-
304-
switch (iEncoding)
289+
switch ((int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0))
305290
{
306291
case 65001: // UTF8
307-
result = Encoding.Default.GetString(utf8.GetBytes(utf8Text));
308-
break;
292+
return Encoding.Default.GetString(utf8.GetBytes(utf8Text));
309293
default:
310294
Encoding ANSI = Encoding.GetEncoding(1252);
311295

312296
byte[] utf8Bytes = utf8.GetBytes(utf8Text);
313297
byte[] ansiBytes = Encoding.Convert(Encoding.UTF8, ANSI, utf8Bytes);
314298

315-
result = ANSI.GetString(ansiBytes);
316-
break;
299+
return ANSI.GetString(ansiBytes);
317300
}
318-
319-
return result;
320301
}
321302

322303
/// <summary>
323304
/// Convertit le texte spécifier de l'encodage C# (UTF8) à l'encodage document Notepad++ courant
324305
/// </summary>
325306
public static string GetScintillaTextFromUtf8Text(string utf8Text, out int length)
326307
{
327-
string result = "";
328-
int iEncoding = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0);
329-
330308
byte[] utf8Bytes = utf8.GetBytes(utf8Text);
331309
length = utf8Bytes.Length;
332310

333-
switch (iEncoding)
311+
switch ((int)Win32.SendMessage(PluginBase.nppData._nppHandle, SciMsg.SCI_GETCODEPAGE, 0, 0))
334312
{
335313
case 65001: // UTF8
336-
result = Encoding.Default.GetString(utf8.GetBytes(utf8Text));
337-
break;
314+
return Encoding.Default.GetString(utf8.GetBytes(utf8Text));
338315
default:
339316
Encoding ANSI = Encoding.GetEncoding(1252);
340317
byte[] ansiBytes = Encoding.Convert(Encoding.UTF8, ANSI, utf8Bytes);
341-
result = ANSI.GetString(ansiBytes);
342-
break;
318+
return ANSI.GetString(ansiBytes);
343319
}
344-
345-
return result;
346320
}
347321
}
348322
}

CSharpRegexTools4Npp/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public static void ShowTheDialog()
143143
}
144144
else if (BNpp.NotepadPP.GetAllOpenedDocuments.Any(s => s.Equals(fileName, StringComparison.OrdinalIgnoreCase)))
145145
{
146-
BNpp.NotepadPP.ShowOpenedDocument(fileName);
146+
BNpp.NotepadPP.ShowTab(fileName);
147147
result = true;
148148
}
149149
else if (!onlyIfAlreadyOpen)

CSharpRegexTools4Npp/PluginInfrastructure/GatewayDomain.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ public CharacterRange(int cpmin, int cpmax)
218218
{
219219
cpMin = cpmin; cpMax = cpmax;
220220
}
221-
222221
public int cpMin;
223222
public int cpMax;
224223
}
@@ -246,7 +245,6 @@ public TextRange(CharacterRange chrRange, int stringCapacity)
246245
_sciTextRange.chrg = chrRange;
247246
_sciTextRange.lpstrText = Marshal.AllocHGlobal(stringCapacity);
248247
}
249-
250248
public TextRange(int cpmin, int cpmax, int stringCapacity)
251249
{
252250
_sciTextRange.chrg.cpMin = cpmin;

0 commit comments

Comments
 (0)