Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit eeb4398

Browse files
committed
Fix Caret Offset Crash
1 parent e933254 commit eeb4398

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

App/AssemblyInfo1.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@
3333
#if (DEBUG)
3434
[assembly: AssemblyVersion("1.13.*")]
3535
#else
36-
[assembly: AssemblyVersion("1.3.4.2")]
36+
[assembly: AssemblyVersion("1.3.4.3")]
3737
#endif

UI/Components/EditorElement.xaml.cs

+44-40
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,35 @@
1919

2020
namespace Spedit.UI.Components
2121
{
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
2626
{
27-
private string _FullFilePath = "";
28-
29-
private bool _NeedsSave;
30-
public Timer AutoSaveTimer;
3127
private readonly BracketHighlightRenderer bracketHighlightRenderer;
3228
private readonly SPBracketSearcher bracketSearcher;
3329
private readonly ColorizeSelection colorizeSelection;
3430

3531
private readonly Storyboard FadeJumpGridIn;
3632
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;
3740

3841
private FileSystemWatcher fileWatcher;
3942

4043
public FoldingManager foldingManager;
41-
private readonly SPFoldingStrategy foldingStrategy;
4244

4345
private bool isBlock;
4446

4547
private bool JumpGridIsOpen;
4648

4749
private double LineHeight;
4850
public new LayoutDocument Parent;
49-
50-
private readonly Timer regularyTimer;
5151
private bool SelectionIsHighlited;
5252
private bool WantFoldingUpdate;
5353

@@ -80,9 +80,12 @@ public EditorElement(string filePath)
8080
var fInfo = new FileInfo(filePath);
8181
if (fInfo.Exists)
8282
{
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+
};
8689
fileWatcher.Changed += fileWatcher_Changed;
8790
fileWatcher.EnableRaisingEvents = true;
8891
}
@@ -238,10 +241,9 @@ private void JumpNumberKeyDown(object sender, KeyEventArgs e)
238241

239242
private void JumpToNumber(object sender, RoutedEventArgs e)
240243
{
241-
int num;
242-
if (int.TryParse(JumpNumber.Text, out num))
244+
if (int.TryParse(JumpNumber.Text, out var num))
243245
{
244-
if (LineJump.IsChecked.Value)
246+
if (LineJump.IsChecked != null && LineJump.IsChecked.Value)
245247
{
246248
num = Math.Max(1, Math.Min(num, editor.LineCount));
247249
var line = editor.Document.GetLineByNumber(num);
@@ -273,22 +275,22 @@ private void fileWatcher_Changed(object sender, FileSystemEventArgs e)
273275
if (e == null) return;
274276
if (e.FullPath == _FullFilePath)
275277
{
276-
var ReloadFile = false;
278+
bool reloadFile;
277279
if (_NeedsSave)
278280
{
279281
var result = MessageBox.Show(
280282
string.Format(Program.Translations.GetLanguage("DFileChanged"), _FullFilePath) +
281283
Environment.NewLine + Program.Translations.GetLanguage("FileTryReload"),
282284
Program.Translations.GetLanguage("FileChanged"), MessageBoxButton.YesNo,
283285
MessageBoxImage.Asterisk);
284-
ReloadFile = result == MessageBoxResult.Yes;
286+
reloadFile = result == MessageBoxResult.Yes;
285287
}
286288
else //when the user didnt changed anything, we just reload the file since we are intelligent...
287289
{
288-
ReloadFile = true;
290+
reloadFile = true;
289291
}
290292

291-
if (ReloadFile)
293+
if (reloadFile)
292294
Dispatcher.Invoke(() =>
293295
{
294296
FileStream stream;
@@ -306,6 +308,7 @@ private void fileWatcher_Changed(object sender, FileSystemEventArgs e)
306308
}
307309
catch (Exception)
308310
{
311+
// ignored
309312
}
310313

311314
Thread.Sleep(
@@ -360,13 +363,14 @@ private void regularyTimer_Elapsed(object sender, ElapsedEventArgs e)
360363
}
361364
catch (Exception)
362365
{
366+
// ignored
363367
}
364368
}
365369
}
366370

367-
public void Save(bool Force = false)
371+
public void Save(bool force = false)
368372
{
369-
if (_NeedsSave || Force)
373+
if (_NeedsSave || force)
370374
{
371375
if (fileWatcher != null) fileWatcher.EnableRaisingEvents = false;
372376
try
@@ -390,50 +394,51 @@ public void Save(bool Force = false)
390394
}
391395
}
392396

393-
public void UpdateFontSize(double size, bool UpdateLineHeight = true)
397+
public void UpdateFontSize(double size, bool updateLineHeight = true)
394398
{
395399
if (size > 2 && size < 31)
396400
{
397401
editor.FontSize = size;
398402
StatusLine_FontSize.Text = size.ToString("n0") + $" {Program.Translations.GetLanguage("PtAbb")}";
399403
}
400404

401-
if (UpdateLineHeight) LineHeight = editor.TextArea.TextView.DefaultLineHeight;
405+
if (updateLineHeight) LineHeight = editor.TextArea.TextView.DefaultLineHeight;
402406
}
403407

404408
public void ToggleCommentOnLine()
405409
{
406410
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
407411
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++;
412416
else
413417
break;
418+
414419
lineText = lineText.Trim();
415420
if (lineText.Length > 1)
416421
{
417422
if (lineText[0] == '/' && lineText[1] == '/')
418-
editor.Document.Remove(line.Offset + leadinggWhiteSpaces, 2);
423+
editor.Document.Remove(line.Offset + leadingWhiteSpaces, 2);
419424
else
420-
editor.Document.Insert(line.Offset + leadinggWhiteSpaces, "//");
425+
editor.Document.Insert(line.Offset + leadingWhiteSpaces, "//");
421426
}
422427
else
423428
{
424-
editor.Document.Insert(line.Offset + leadinggWhiteSpaces, "//");
429+
editor.Document.Insert(line.Offset + leadingWhiteSpaces, "//");
425430
}
426431
}
427432

428-
public void DuplicateLine(bool down)
433+
private void DuplicateLine(bool down)
429434
{
430435
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
431436
var lineText = editor.Document.GetText(line.Offset, line.Length);
432437
editor.Document.Insert(line.Offset, lineText + Environment.NewLine);
433438
if (down) editor.CaretOffset -= line.Length + 1;
434439
}
435440

436-
public void MoveLine(bool down)
441+
private void MoveLine(bool down)
437442
{
438443
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
439444
if (down)
@@ -498,9 +503,8 @@ public async void Close(bool ForcedToSave = false, bool CheckSavings = true)
498503

499504
Program.MainWindow.EditorsReferences.Remove(this);
500505
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+
504508
Parent = null; //to prevent a ring depency which disables the GC from work
505509
Program.MainWindow.UpdateWindowTitle();
506510
}
@@ -610,9 +614,9 @@ private void TextArea_TextEntering(object sender, TextCompositionEventArgs e)
610614
{
611615
if (e.Text == "\n")
612616
{
613-
if (editor.Document.TextLength < editor.CaretOffset+1)
617+
if (editor.Document.TextLength < editor.CaretOffset + 1 || editor.CaretOffset < 3)
614618
return;
615-
619+
616620
var segment = new AnchorSegment(editor.Document, editor.CaretOffset - 1, 2);
617621
var text = editor.Document.GetText(segment);
618622
if (text == "{}")
@@ -779,7 +783,7 @@ protected override void ColorizeLine(DocumentLine line)
779783
var text = CurrentContext.Document.GetText(line);
780784
var start = 0;
781785
int index;
782-
while ((index = text.IndexOf(SelectionString, start)) >= 0)
786+
while ((index = text.IndexOf(SelectionString, start, StringComparison.Ordinal)) >= 0)
783787
{
784788
ChangeLinePart(
785789
lineStartOffset + index,

0 commit comments

Comments
 (0)