44
55namespace 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 ( ) ;
10+
11+ public static ScintillaGateway Scintilla => new ScintillaGateway ( PluginBase . GetCurrentScintilla ( ) ) ;
1012
1113 /// <summary>
1214 /// Récupère les caractères de fin de lignes courant
@@ -17,18 +19,16 @@ public static string CurrentEOL
1719 get
1820 {
1921 string eol = "\n " ;
20- int value = Win32 . SendMessage ( PluginBase . nppData . _nppHandle , SciMsg . SCI_GETEOLMODE , 0 , 0 ) . ToInt32 ( ) ;
22+ int value = Scintilla . GetEOLMode ( ) ;
2123
22- switch ( value )
24+ switch ( ( SciMsg ) value )
2325 {
24- case 0 :
26+ case SciMsg . SC_EOL_CRLF :
2527 eol = "\r \n " ;
2628 break ;
27- case 1 :
29+ case SciMsg . SC_EOL_CR :
2830 eol = "\r " ;
2931 break ;
30- default :
31- break ;
3232 }
3333
3434 return eol ;
@@ -51,7 +51,7 @@ public static string Text
5151 set
5252 {
5353 IScintillaGateway scintilla = new ScintillaGateway ( PluginBase . GetCurrentScintilla ( ) ) ;
54- string text = BEncoding . GetScintillaTextFromUtf8Text ( value , out int length ) ;
54+ string text = BEncoding . GetScintillaTextFromUtf8Text ( value , out _ ) ;
5555 scintilla . SetText ( text ) ;
5656 }
5757 }
@@ -85,7 +85,7 @@ public static int SelectionEnd
8585 int curPos = ( int ) Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_GETSELECTIONEND , 0 , 0 ) ;
8686 IScintillaGateway scintilla = new ScintillaGateway ( PluginBase . GetCurrentScintilla ( ) ) ;
8787 string beginingText = scintilla . GetText ( curPos ) ;
88- string text = BEncoding . GetScintillaTextFromUtf8Text ( beginingText , out int length ) ;
88+ BEncoding . GetScintillaTextFromUtf8Text ( beginingText , out int length ) ;
8989 return length ;
9090 }
9191
@@ -104,7 +104,7 @@ public static int SelectionEnd
104104 }
105105
106106 string afterText = allText . Substring ( 0 , endToUse ) ;
107- string afterTextInDefaultEncoding = BEncoding . GetScintillaTextFromUtf8Text ( afterText , out int defaultEnd ) ;
107+ BEncoding . GetScintillaTextFromUtf8Text ( afterText , out int defaultEnd ) ;
108108
109109 Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_SETSELECTIONEND , defaultEnd , 0 ) ;
110110 }
@@ -136,17 +136,15 @@ public static string SelectedText
136136 {
137137 get
138138 {
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 ( ) ) ;
144141 }
145142
146143 set
147144 {
145+ IScintillaGateway scintilla = new ScintillaGateway ( PluginBase . GetCurrentScintilla ( ) ) ;
148146 string defaultNewText = BEncoding . GetScintillaTextFromUtf8Text ( value ) ;
149- Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_REPLACESEL , 0 , defaultNewText ) ;
147+ scintilla . ReplaceSel ( defaultNewText ) ;
150148 }
151149 }
152150
@@ -186,9 +184,9 @@ public static void SelectTextAndShow(int start, int end)
186184 }
187185
188186 string beforeText = allText . Substring ( 0 , startToUse ) ;
189- string beforeTextInDefaultEncoding = BEncoding . GetScintillaTextFromUtf8Text ( beforeText , out int defaultStart ) ;
187+ BEncoding . GetScintillaTextFromUtf8Text ( beforeText , out int defaultStart ) ;
190188 string endText = allText . Substring ( 0 , endToUse ) ;
191- string endTextInDefaultEncoding = BEncoding . GetScintillaTextFromUtf8Text ( endText , out int defaultEnd ) ;
189+ BEncoding . GetScintillaTextFromUtf8Text ( endText , out int defaultEnd ) ;
192190
193191 Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_GOTOPOS , defaultStart , 0 ) ;
194192 Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_SETSELECTIONEND , defaultEnd , 0 ) ;
@@ -230,14 +228,13 @@ public static void AddSelection(int start, int end)
230228 }
231229
232230 string beforeText = allText . Substring ( 0 , startToUse ) ;
233- string beforeTextInDefaultEncoding = BEncoding . GetScintillaTextFromUtf8Text ( beforeText , out int defaultStart ) ;
231+ BEncoding . GetScintillaTextFromUtf8Text ( beforeText , out int defaultStart ) ;
234232 string endText = allText . Substring ( 0 , endToUse ) ;
235- string endTextInDefaultEncoding = BEncoding . GetScintillaTextFromUtf8Text ( endText , out int defaultEnd ) ;
233+ BEncoding . GetScintillaTextFromUtf8Text ( endText , out int defaultEnd ) ;
236234
237235 Win32 . SendMessage ( PluginBase . GetCurrentScintilla ( ) , SciMsg . SCI_ADDSELECTION , defaultStart , defaultEnd ) ;
238236 }
239237
240-
241238 /// <summary>
242239 /// Récupère le texte de la ligne spécifiée
243240 /// </summary>
@@ -263,84 +260,63 @@ public static string GetLineText(int lineNb)
263260 /// </summary>
264261 internal static class BEncoding
265262 {
266- private static Encoding utf8 = Encoding . UTF8 ;
263+ private static readonly Encoding utf8 = Encoding . UTF8 ;
267264
268265 /// <summary>
269266 /// Convertit le texte spécifier de l'encodage du document Notepad++ courant à l'encodage C# (UTF8)
270267 /// </summary>
271268 public static string GetUtf8TextFromScintillaText ( string scText )
272269 {
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 ) )
277271 {
278272 case 65001 : // UTF8
279- result = utf8 . GetString ( Encoding . Default . GetBytes ( scText ) ) ;
280- break ;
273+ return utf8 . GetString ( Encoding . Default . GetBytes ( scText ) ) ;
281274 default :
282275 Encoding ANSI = Encoding . GetEncoding ( 1252 ) ;
283276
284277 byte [ ] ansiBytes = ANSI . GetBytes ( scText ) ;
285278 byte [ ] utf8Bytes = Encoding . Convert ( ANSI , Encoding . UTF8 , ansiBytes ) ;
286279
287- result = Encoding . UTF8 . GetString ( utf8Bytes ) ;
288- break ;
280+ return Encoding . UTF8 . GetString ( utf8Bytes ) ;
289281 }
290-
291- return result ;
292282 }
293283
294284 /// <summary>
295285 /// Convertit le texte spécifier de l'encodage C# (UTF8) à l'encodage document Notepad++ courant
296286 /// </summary>
297287 public static string GetScintillaTextFromUtf8Text ( string utf8Text )
298288 {
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 ) )
303290 {
304291 case 65001 : // UTF8
305- result = Encoding . Default . GetString ( utf8 . GetBytes ( utf8Text ) ) ;
306- break ;
292+ return Encoding . Default . GetString ( utf8 . GetBytes ( utf8Text ) ) ;
307293 default :
308294 Encoding ANSI = Encoding . GetEncoding ( 1252 ) ;
309295
310296 byte [ ] utf8Bytes = utf8 . GetBytes ( utf8Text ) ;
311297 byte [ ] ansiBytes = Encoding . Convert ( Encoding . UTF8 , ANSI , utf8Bytes ) ;
312298
313- result = ANSI . GetString ( ansiBytes ) ;
314- break ;
299+ return ANSI . GetString ( ansiBytes ) ;
315300 }
316-
317- return result ;
318301 }
319302
320303 /// <summary>
321304 /// Convertit le texte spécifier de l'encodage C# (UTF8) à l'encodage document Notepad++ courant
322305 /// </summary>
323306 public static string GetScintillaTextFromUtf8Text ( string utf8Text , out int length )
324307 {
325- string result = "" ;
326- int iEncoding = ( int ) Win32 . SendMessage ( PluginBase . nppData . _nppHandle , SciMsg . SCI_GETCODEPAGE , 0 , 0 ) ;
327-
328308 byte [ ] utf8Bytes = utf8 . GetBytes ( utf8Text ) ;
329309 length = utf8Bytes . Length ;
330310
331- switch ( iEncoding )
311+ switch ( ( int ) Win32 . SendMessage ( PluginBase . nppData . _nppHandle , SciMsg . SCI_GETCODEPAGE , 0 , 0 ) )
332312 {
333313 case 65001 : // UTF8
334- result = Encoding . Default . GetString ( utf8 . GetBytes ( utf8Text ) ) ;
335- break ;
314+ return Encoding . Default . GetString ( utf8 . GetBytes ( utf8Text ) ) ;
336315 default :
337316 Encoding ANSI = Encoding . GetEncoding ( 1252 ) ;
338317 byte [ ] ansiBytes = Encoding . Convert ( Encoding . UTF8 , ANSI , utf8Bytes ) ;
339- result = ANSI . GetString ( ansiBytes ) ;
340- break ;
318+ return ANSI . GetString ( ansiBytes ) ;
341319 }
342-
343- return result ;
344320 }
345321 }
346322}
0 commit comments