4
4
5
5
namespace CSharpRegexTools4Npp
6
6
{
7
- public class BNpp
7
+ public static class BNpp
8
8
{
9
- public static NotepadPPGateway NotepadPP { get ; private set ; } = new NotepadPPGateway ( ) ;
9
+ public static NotepadPPGateway NotepadPP { get ; } = new NotepadPPGateway ( ) ;
10
10
11
11
public static ScintillaGateway Scintilla => new ScintillaGateway ( PluginBase . GetCurrentScintilla ( ) ) ;
12
12
@@ -29,8 +29,6 @@ public static string CurrentEOL
29
29
case SciMsg . SC_EOL_CR :
30
30
eol = "\r " ;
31
31
break ;
32
- default :
33
- break ;
34
32
}
35
33
36
34
return eol ;
@@ -53,7 +51,7 @@ public static string Text
53
51
set
54
52
{
55
53
IScintillaGateway scintilla = new ScintillaGateway ( PluginBase . GetCurrentScintilla ( ) ) ;
56
- string text = BEncoding . GetScintillaTextFromUtf8Text ( value , out int length ) ;
54
+ string text = BEncoding . GetScintillaTextFromUtf8Text ( value , out _ ) ;
57
55
scintilla . SetText ( text ) ;
58
56
}
59
57
}
@@ -87,7 +85,7 @@ public static int SelectionEnd
87
85
int curPos = ( int ) Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_GETSELECTIONEND , 0 , 0 ) ;
88
86
IScintillaGateway scintilla = new ScintillaGateway ( PluginBase . GetCurrentScintilla ( ) ) ;
89
87
string beginingText = scintilla . GetText ( curPos ) ;
90
- string text = BEncoding . GetScintillaTextFromUtf8Text ( beginingText , out int length ) ;
88
+ BEncoding . GetScintillaTextFromUtf8Text ( beginingText , out int length ) ;
91
89
return length ;
92
90
}
93
91
@@ -106,7 +104,7 @@ public static int SelectionEnd
106
104
}
107
105
108
106
string afterText = allText . Substring ( 0 , endToUse ) ;
109
- string afterTextInDefaultEncoding = BEncoding . GetScintillaTextFromUtf8Text ( afterText , out int defaultEnd ) ;
107
+ BEncoding . GetScintillaTextFromUtf8Text ( afterText , out int defaultEnd ) ;
110
108
111
109
Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_SETSELECTIONEND , defaultEnd , 0 ) ;
112
110
}
@@ -138,17 +136,15 @@ public static string SelectedText
138
136
{
139
137
get
140
138
{
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 ( ) ) ;
146
141
}
147
142
148
143
set
149
144
{
145
+ IScintillaGateway scintilla = new ScintillaGateway ( PluginBase . GetCurrentScintilla ( ) ) ;
150
146
string defaultNewText = BEncoding . GetScintillaTextFromUtf8Text ( value ) ;
151
- Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_REPLACESEL , 0 , defaultNewText ) ;
147
+ scintilla . ReplaceSel ( defaultNewText ) ;
152
148
}
153
149
}
154
150
@@ -188,9 +184,9 @@ public static void SelectTextAndShow(int start, int end)
188
184
}
189
185
190
186
string beforeText = allText . Substring ( 0 , startToUse ) ;
191
- string beforeTextInDefaultEncoding = BEncoding . GetScintillaTextFromUtf8Text ( beforeText , out int defaultStart ) ;
187
+ BEncoding . GetScintillaTextFromUtf8Text ( beforeText , out int defaultStart ) ;
192
188
string endText = allText . Substring ( 0 , endToUse ) ;
193
- string endTextInDefaultEncoding = BEncoding . GetScintillaTextFromUtf8Text ( endText , out int defaultEnd ) ;
189
+ BEncoding . GetScintillaTextFromUtf8Text ( endText , out int defaultEnd ) ;
194
190
195
191
Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_GOTOPOS , defaultStart , 0 ) ;
196
192
Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_SETSELECTIONEND , defaultEnd , 0 ) ;
@@ -232,14 +228,13 @@ public static void AddSelection(int start, int end)
232
228
}
233
229
234
230
string beforeText = allText . Substring ( 0 , startToUse ) ;
235
- string beforeTextInDefaultEncoding = BEncoding . GetScintillaTextFromUtf8Text ( beforeText , out int defaultStart ) ;
231
+ BEncoding . GetScintillaTextFromUtf8Text ( beforeText , out int defaultStart ) ;
236
232
string endText = allText . Substring ( 0 , endToUse ) ;
237
- string endTextInDefaultEncoding = BEncoding . GetScintillaTextFromUtf8Text ( endText , out int defaultEnd ) ;
233
+ BEncoding . GetScintillaTextFromUtf8Text ( endText , out int defaultEnd ) ;
238
234
239
235
Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_ADDSELECTION , defaultStart , defaultEnd ) ;
240
236
}
241
237
242
-
243
238
/// <summary>
244
239
/// Récupère le texte de la ligne spécifiée
245
240
/// </summary>
@@ -265,84 +260,63 @@ public static string GetLineText(int lineNb)
265
260
/// </summary>
266
261
internal static class BEncoding
267
262
{
268
- private static Encoding utf8 = Encoding . UTF8 ;
263
+ private static readonly Encoding utf8 = Encoding . UTF8 ;
269
264
270
265
/// <summary>
271
266
/// Convertit le texte spécifier de l'encodage du document Notepad++ courant à l'encodage C# (UTF8)
272
267
/// </summary>
273
268
public static string GetUtf8TextFromScintillaText ( string scText )
274
269
{
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 ) )
279
271
{
280
272
case 65001 : // UTF8
281
- result = utf8 . GetString ( Encoding . Default . GetBytes ( scText ) ) ;
282
- break ;
273
+ return utf8 . GetString ( Encoding . Default . GetBytes ( scText ) ) ;
283
274
default :
284
275
Encoding ANSI = Encoding . GetEncoding ( 1252 ) ;
285
276
286
277
byte [ ] ansiBytes = ANSI . GetBytes ( scText ) ;
287
278
byte [ ] utf8Bytes = Encoding . Convert ( ANSI , Encoding . UTF8 , ansiBytes ) ;
288
279
289
- result = Encoding . UTF8 . GetString ( utf8Bytes ) ;
290
- break ;
280
+ return Encoding . UTF8 . GetString ( utf8Bytes ) ;
291
281
}
292
-
293
- return result ;
294
282
}
295
283
296
284
/// <summary>
297
285
/// Convertit le texte spécifier de l'encodage C# (UTF8) à l'encodage document Notepad++ courant
298
286
/// </summary>
299
287
public static string GetScintillaTextFromUtf8Text ( string utf8Text )
300
288
{
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 ) )
305
290
{
306
291
case 65001 : // UTF8
307
- result = Encoding . Default . GetString ( utf8 . GetBytes ( utf8Text ) ) ;
308
- break ;
292
+ return Encoding . Default . GetString ( utf8 . GetBytes ( utf8Text ) ) ;
309
293
default :
310
294
Encoding ANSI = Encoding . GetEncoding ( 1252 ) ;
311
295
312
296
byte [ ] utf8Bytes = utf8 . GetBytes ( utf8Text ) ;
313
297
byte [ ] ansiBytes = Encoding . Convert ( Encoding . UTF8 , ANSI , utf8Bytes ) ;
314
298
315
- result = ANSI . GetString ( ansiBytes ) ;
316
- break ;
299
+ return ANSI . GetString ( ansiBytes ) ;
317
300
}
318
-
319
- return result ;
320
301
}
321
302
322
303
/// <summary>
323
304
/// Convertit le texte spécifier de l'encodage C# (UTF8) à l'encodage document Notepad++ courant
324
305
/// </summary>
325
306
public static string GetScintillaTextFromUtf8Text ( string utf8Text , out int length )
326
307
{
327
- string result = "" ;
328
- int iEncoding = ( int ) Win32 . SendMessage ( PluginBase . nppData . _nppHandle , SciMsg . SCI_GETCODEPAGE , 0 , 0 ) ;
329
-
330
308
byte [ ] utf8Bytes = utf8 . GetBytes ( utf8Text ) ;
331
309
length = utf8Bytes . Length ;
332
310
333
- switch ( iEncoding )
311
+ switch ( ( int ) Win32 . SendMessage ( PluginBase . nppData . _nppHandle , SciMsg . SCI_GETCODEPAGE , 0 , 0 ) )
334
312
{
335
313
case 65001 : // UTF8
336
- result = Encoding . Default . GetString ( utf8 . GetBytes ( utf8Text ) ) ;
337
- break ;
314
+ return Encoding . Default . GetString ( utf8 . GetBytes ( utf8Text ) ) ;
338
315
default :
339
316
Encoding ANSI = Encoding . GetEncoding ( 1252 ) ;
340
317
byte [ ] ansiBytes = Encoding . Convert ( Encoding . UTF8 , ANSI , utf8Bytes ) ;
341
- result = ANSI . GetString ( ansiBytes ) ;
342
- break ;
318
+ return ANSI . GetString ( ansiBytes ) ;
343
319
}
344
-
345
- return result ;
346
320
}
347
321
}
348
322
}
0 commit comments