19
19
20
20
namespace Spedit . UI . Components
21
21
{
22
- /// <summary>
23
- /// Interaction logic for EditorElement.xaml
24
- /// </summary>
25
- public partial class EditorElement : UserControl
22
+ /// <summary>
23
+ /// Interaction logic for EditorElement.xaml
24
+ /// </summary>
25
+ public partial class EditorElement : UserControl
26
26
{
27
- private string _FullFilePath = "" ;
28
-
29
- private bool _NeedsSave ;
30
- public Timer AutoSaveTimer ;
31
27
private readonly BracketHighlightRenderer bracketHighlightRenderer ;
32
28
private readonly SPBracketSearcher bracketSearcher ;
33
29
private readonly ColorizeSelection colorizeSelection ;
34
30
35
31
private readonly Storyboard FadeJumpGridIn ;
36
32
private readonly Storyboard FadeJumpGridOut ;
33
+ private readonly SPFoldingStrategy foldingStrategy ;
34
+
35
+ private readonly Timer regularyTimer ;
36
+ private string _FullFilePath = "" ;
37
+
38
+ private bool _NeedsSave ;
39
+ public Timer AutoSaveTimer ;
37
40
38
41
private FileSystemWatcher fileWatcher ;
39
42
40
43
public FoldingManager foldingManager ;
41
- private readonly SPFoldingStrategy foldingStrategy ;
42
44
43
45
private bool isBlock ;
44
46
45
47
private bool JumpGridIsOpen ;
46
48
47
49
private double LineHeight ;
48
50
public new LayoutDocument Parent ;
49
-
50
- private readonly Timer regularyTimer ;
51
51
private bool SelectionIsHighlited ;
52
52
private bool WantFoldingUpdate ;
53
53
@@ -80,9 +80,12 @@ public EditorElement(string filePath)
80
80
var fInfo = new FileInfo ( filePath ) ;
81
81
if ( fInfo . Exists )
82
82
{
83
- fileWatcher = new FileSystemWatcher ( fInfo . DirectoryName ) { IncludeSubdirectories = false } ;
84
- fileWatcher . NotifyFilter = NotifyFilters . Size | NotifyFilters . LastWrite ;
85
- fileWatcher . Filter = "*" + fInfo . Extension ;
83
+ fileWatcher = new FileSystemWatcher ( fInfo . DirectoryName ?? throw new NullReferenceException ( ) )
84
+ {
85
+ IncludeSubdirectories = false ,
86
+ NotifyFilter = NotifyFilters . Size | NotifyFilters . LastWrite ,
87
+ Filter = "*" + fInfo . Extension
88
+ } ;
86
89
fileWatcher . Changed += fileWatcher_Changed ;
87
90
fileWatcher . EnableRaisingEvents = true ;
88
91
}
@@ -238,10 +241,9 @@ private void JumpNumberKeyDown(object sender, KeyEventArgs e)
238
241
239
242
private void JumpToNumber ( object sender , RoutedEventArgs e )
240
243
{
241
- int num ;
242
- if ( int . TryParse ( JumpNumber . Text , out num ) )
244
+ if ( int . TryParse ( JumpNumber . Text , out var num ) )
243
245
{
244
- if ( LineJump . IsChecked . Value )
246
+ if ( LineJump . IsChecked != null && LineJump . IsChecked . Value )
245
247
{
246
248
num = Math . Max ( 1 , Math . Min ( num , editor . LineCount ) ) ;
247
249
var line = editor . Document . GetLineByNumber ( num ) ;
@@ -273,22 +275,22 @@ private void fileWatcher_Changed(object sender, FileSystemEventArgs e)
273
275
if ( e == null ) return ;
274
276
if ( e . FullPath == _FullFilePath )
275
277
{
276
- var ReloadFile = false ;
278
+ bool reloadFile ;
277
279
if ( _NeedsSave )
278
280
{
279
281
var result = MessageBox . Show (
280
282
string . Format ( Program . Translations . GetLanguage ( "DFileChanged" ) , _FullFilePath ) +
281
283
Environment . NewLine + Program . Translations . GetLanguage ( "FileTryReload" ) ,
282
284
Program . Translations . GetLanguage ( "FileChanged" ) , MessageBoxButton . YesNo ,
283
285
MessageBoxImage . Asterisk ) ;
284
- ReloadFile = result == MessageBoxResult . Yes ;
286
+ reloadFile = result == MessageBoxResult . Yes ;
285
287
}
286
288
else //when the user didnt changed anything, we just reload the file since we are intelligent...
287
289
{
288
- ReloadFile = true ;
290
+ reloadFile = true ;
289
291
}
290
292
291
- if ( ReloadFile )
293
+ if ( reloadFile )
292
294
Dispatcher . Invoke ( ( ) =>
293
295
{
294
296
FileStream stream ;
@@ -306,6 +308,7 @@ private void fileWatcher_Changed(object sender, FileSystemEventArgs e)
306
308
}
307
309
catch ( Exception )
308
310
{
311
+ // ignored
309
312
}
310
313
311
314
Thread . Sleep (
@@ -360,13 +363,14 @@ private void regularyTimer_Elapsed(object sender, ElapsedEventArgs e)
360
363
}
361
364
catch ( Exception )
362
365
{
366
+ // ignored
363
367
}
364
368
}
365
369
}
366
370
367
- public void Save ( bool Force = false )
371
+ public void Save ( bool force = false )
368
372
{
369
- if ( _NeedsSave || Force )
373
+ if ( _NeedsSave || force )
370
374
{
371
375
if ( fileWatcher != null ) fileWatcher . EnableRaisingEvents = false ;
372
376
try
@@ -390,50 +394,51 @@ public void Save(bool Force = false)
390
394
}
391
395
}
392
396
393
- public void UpdateFontSize ( double size , bool UpdateLineHeight = true )
397
+ public void UpdateFontSize ( double size , bool updateLineHeight = true )
394
398
{
395
399
if ( size > 2 && size < 31 )
396
400
{
397
401
editor . FontSize = size ;
398
402
StatusLine_FontSize . Text = size . ToString ( "n0" ) + $ " { Program . Translations . GetLanguage ( "PtAbb" ) } ";
399
403
}
400
404
401
- if ( UpdateLineHeight ) LineHeight = editor . TextArea . TextView . DefaultLineHeight ;
405
+ if ( updateLineHeight ) LineHeight = editor . TextArea . TextView . DefaultLineHeight ;
402
406
}
403
407
404
408
public void ToggleCommentOnLine ( )
405
409
{
406
410
var line = editor . Document . GetLineByOffset ( editor . CaretOffset ) ;
407
411
var lineText = editor . Document . GetText ( line . Offset , line . Length ) ;
408
- var leadinggWhiteSpaces = 0 ;
409
- for ( var i = 0 ; i < lineText . Length ; ++ i )
410
- if ( char . IsWhiteSpace ( lineText [ i ] ) )
411
- leadinggWhiteSpaces ++ ;
412
+ var leadingWhiteSpaces = 0 ;
413
+ foreach ( var l in lineText )
414
+ if ( char . IsWhiteSpace ( l ) )
415
+ leadingWhiteSpaces ++ ;
412
416
else
413
417
break ;
418
+
414
419
lineText = lineText . Trim ( ) ;
415
420
if ( lineText . Length > 1 )
416
421
{
417
422
if ( lineText [ 0 ] == '/' && lineText [ 1 ] == '/' )
418
- editor . Document . Remove ( line . Offset + leadinggWhiteSpaces , 2 ) ;
423
+ editor . Document . Remove ( line . Offset + leadingWhiteSpaces , 2 ) ;
419
424
else
420
- editor . Document . Insert ( line . Offset + leadinggWhiteSpaces , "//" ) ;
425
+ editor . Document . Insert ( line . Offset + leadingWhiteSpaces , "//" ) ;
421
426
}
422
427
else
423
428
{
424
- editor . Document . Insert ( line . Offset + leadinggWhiteSpaces , "//" ) ;
429
+ editor . Document . Insert ( line . Offset + leadingWhiteSpaces , "//" ) ;
425
430
}
426
431
}
427
432
428
- public void DuplicateLine ( bool down )
433
+ private void DuplicateLine ( bool down )
429
434
{
430
435
var line = editor . Document . GetLineByOffset ( editor . CaretOffset ) ;
431
436
var lineText = editor . Document . GetText ( line . Offset , line . Length ) ;
432
437
editor . Document . Insert ( line . Offset , lineText + Environment . NewLine ) ;
433
438
if ( down ) editor . CaretOffset -= line . Length + 1 ;
434
439
}
435
440
436
- public void MoveLine ( bool down )
441
+ private void MoveLine ( bool down )
437
442
{
438
443
var line = editor . Document . GetLineByOffset ( editor . CaretOffset ) ;
439
444
if ( down )
@@ -498,9 +503,8 @@ public async void Close(bool ForcedToSave = false, bool CheckSavings = true)
498
503
499
504
Program . MainWindow . EditorsReferences . Remove ( this ) ;
500
505
var childs = Program . MainWindow . DockingPaneGroup . Children ;
501
- foreach ( var c in childs )
502
- if ( c is LayoutDocumentPane )
503
- ( ( LayoutDocumentPane ) c ) . Children . Remove ( Parent ) ;
506
+ foreach ( var c in childs ) ( c as LayoutDocumentPane ) ? . Children . Remove ( Parent ) ;
507
+
504
508
Parent = null ; //to prevent a ring depency which disables the GC from work
505
509
Program . MainWindow . UpdateWindowTitle ( ) ;
506
510
}
@@ -610,9 +614,9 @@ private void TextArea_TextEntering(object sender, TextCompositionEventArgs e)
610
614
{
611
615
if ( e . Text == "\n " )
612
616
{
613
- if ( editor . Document . TextLength < editor . CaretOffset + 1 )
617
+ if ( editor . Document . TextLength < editor . CaretOffset + 1 || editor . CaretOffset < 3 )
614
618
return ;
615
-
619
+
616
620
var segment = new AnchorSegment ( editor . Document , editor . CaretOffset - 1 , 2 ) ;
617
621
var text = editor . Document . GetText ( segment ) ;
618
622
if ( text == "{}" )
@@ -779,7 +783,7 @@ protected override void ColorizeLine(DocumentLine line)
779
783
var text = CurrentContext . Document . GetText ( line ) ;
780
784
var start = 0 ;
781
785
int index ;
782
- while ( ( index = text . IndexOf ( SelectionString , start ) ) >= 0 )
786
+ while ( ( index = text . IndexOf ( SelectionString , start , StringComparison . Ordinal ) ) >= 0 )
783
787
{
784
788
ChangeLinePart (
785
789
lineStartOffset + index ,
0 commit comments