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