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

Commit

Permalink
Fix Caret Offset Crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexer10 committed Apr 1, 2020
1 parent e933254 commit eeb4398
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 41 deletions.
2 changes: 1 addition & 1 deletion App/AssemblyInfo1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
#if (DEBUG)
[assembly: AssemblyVersion("1.13.*")]
#else
[assembly: AssemblyVersion("1.3.4.2")]
[assembly: AssemblyVersion("1.3.4.3")]
#endif
84 changes: 44 additions & 40 deletions UI/Components/EditorElement.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,35 @@

namespace Spedit.UI.Components
{
/// <summary>
/// Interaction logic for EditorElement.xaml
/// </summary>
public partial class EditorElement : UserControl
/// <summary>
/// Interaction logic for EditorElement.xaml
/// </summary>
public partial class EditorElement : UserControl
{
private string _FullFilePath = "";

private bool _NeedsSave;
public Timer AutoSaveTimer;
private readonly BracketHighlightRenderer bracketHighlightRenderer;
private readonly SPBracketSearcher bracketSearcher;
private readonly ColorizeSelection colorizeSelection;

private readonly Storyboard FadeJumpGridIn;
private readonly Storyboard FadeJumpGridOut;
private readonly SPFoldingStrategy foldingStrategy;

private readonly Timer regularyTimer;
private string _FullFilePath = "";

private bool _NeedsSave;
public Timer AutoSaveTimer;

private FileSystemWatcher fileWatcher;

public FoldingManager foldingManager;
private readonly SPFoldingStrategy foldingStrategy;

private bool isBlock;

private bool JumpGridIsOpen;

private double LineHeight;
public new LayoutDocument Parent;

private readonly Timer regularyTimer;
private bool SelectionIsHighlited;
private bool WantFoldingUpdate;

Expand Down Expand Up @@ -80,9 +80,12 @@ public EditorElement(string filePath)
var fInfo = new FileInfo(filePath);
if (fInfo.Exists)
{
fileWatcher = new FileSystemWatcher(fInfo.DirectoryName) {IncludeSubdirectories = false};
fileWatcher.NotifyFilter = NotifyFilters.Size | NotifyFilters.LastWrite;
fileWatcher.Filter = "*" + fInfo.Extension;
fileWatcher = new FileSystemWatcher(fInfo.DirectoryName ?? throw new NullReferenceException())
{
IncludeSubdirectories = false,
NotifyFilter = NotifyFilters.Size | NotifyFilters.LastWrite,
Filter = "*" + fInfo.Extension
};
fileWatcher.Changed += fileWatcher_Changed;
fileWatcher.EnableRaisingEvents = true;
}
Expand Down Expand Up @@ -238,10 +241,9 @@ private void JumpNumberKeyDown(object sender, KeyEventArgs e)

private void JumpToNumber(object sender, RoutedEventArgs e)
{
int num;
if (int.TryParse(JumpNumber.Text, out num))
if (int.TryParse(JumpNumber.Text, out var num))
{
if (LineJump.IsChecked.Value)
if (LineJump.IsChecked != null && LineJump.IsChecked.Value)
{
num = Math.Max(1, Math.Min(num, editor.LineCount));
var line = editor.Document.GetLineByNumber(num);
Expand Down Expand Up @@ -273,22 +275,22 @@ private void fileWatcher_Changed(object sender, FileSystemEventArgs e)
if (e == null) return;
if (e.FullPath == _FullFilePath)
{
var ReloadFile = false;
bool reloadFile;
if (_NeedsSave)
{
var result = MessageBox.Show(
string.Format(Program.Translations.GetLanguage("DFileChanged"), _FullFilePath) +
Environment.NewLine + Program.Translations.GetLanguage("FileTryReload"),
Program.Translations.GetLanguage("FileChanged"), MessageBoxButton.YesNo,
MessageBoxImage.Asterisk);
ReloadFile = result == MessageBoxResult.Yes;
reloadFile = result == MessageBoxResult.Yes;
}
else //when the user didnt changed anything, we just reload the file since we are intelligent...
{
ReloadFile = true;
reloadFile = true;
}

if (ReloadFile)
if (reloadFile)
Dispatcher.Invoke(() =>
{
FileStream stream;
Expand All @@ -306,6 +308,7 @@ private void fileWatcher_Changed(object sender, FileSystemEventArgs e)
}
catch (Exception)
{
// ignored
}
Thread.Sleep(
Expand Down Expand Up @@ -360,13 +363,14 @@ private void regularyTimer_Elapsed(object sender, ElapsedEventArgs e)
}
catch (Exception)
{
// ignored
}
}
}

public void Save(bool Force = false)
public void Save(bool force = false)
{
if (_NeedsSave || Force)
if (_NeedsSave || force)
{
if (fileWatcher != null) fileWatcher.EnableRaisingEvents = false;
try
Expand All @@ -390,50 +394,51 @@ public void Save(bool Force = false)
}
}

public void UpdateFontSize(double size, bool UpdateLineHeight = true)
public void UpdateFontSize(double size, bool updateLineHeight = true)
{
if (size > 2 && size < 31)
{
editor.FontSize = size;
StatusLine_FontSize.Text = size.ToString("n0") + $" {Program.Translations.GetLanguage("PtAbb")}";
}

if (UpdateLineHeight) LineHeight = editor.TextArea.TextView.DefaultLineHeight;
if (updateLineHeight) LineHeight = editor.TextArea.TextView.DefaultLineHeight;
}

public void ToggleCommentOnLine()
{
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
var lineText = editor.Document.GetText(line.Offset, line.Length);
var leadinggWhiteSpaces = 0;
for (var i = 0; i < lineText.Length; ++i)
if (char.IsWhiteSpace(lineText[i]))
leadinggWhiteSpaces++;
var leadingWhiteSpaces = 0;
foreach (var l in lineText)
if (char.IsWhiteSpace(l))
leadingWhiteSpaces++;
else
break;

lineText = lineText.Trim();
if (lineText.Length > 1)
{
if (lineText[0] == '/' && lineText[1] == '/')
editor.Document.Remove(line.Offset + leadinggWhiteSpaces, 2);
editor.Document.Remove(line.Offset + leadingWhiteSpaces, 2);
else
editor.Document.Insert(line.Offset + leadinggWhiteSpaces, "//");
editor.Document.Insert(line.Offset + leadingWhiteSpaces, "//");
}
else
{
editor.Document.Insert(line.Offset + leadinggWhiteSpaces, "//");
editor.Document.Insert(line.Offset + leadingWhiteSpaces, "//");
}
}

public void DuplicateLine(bool down)
private void DuplicateLine(bool down)
{
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
var lineText = editor.Document.GetText(line.Offset, line.Length);
editor.Document.Insert(line.Offset, lineText + Environment.NewLine);
if (down) editor.CaretOffset -= line.Length + 1;
}

public void MoveLine(bool down)
private void MoveLine(bool down)
{
var line = editor.Document.GetLineByOffset(editor.CaretOffset);
if (down)
Expand Down Expand Up @@ -498,9 +503,8 @@ public async void Close(bool ForcedToSave = false, bool CheckSavings = true)

Program.MainWindow.EditorsReferences.Remove(this);
var childs = Program.MainWindow.DockingPaneGroup.Children;
foreach (var c in childs)
if (c is LayoutDocumentPane)
((LayoutDocumentPane) c).Children.Remove(Parent);
foreach (var c in childs) (c as LayoutDocumentPane)?.Children.Remove(Parent);

Parent = null; //to prevent a ring depency which disables the GC from work
Program.MainWindow.UpdateWindowTitle();
}
Expand Down Expand Up @@ -610,9 +614,9 @@ private void TextArea_TextEntering(object sender, TextCompositionEventArgs e)
{
if (e.Text == "\n")
{
if (editor.Document.TextLength < editor.CaretOffset+1)
if (editor.Document.TextLength < editor.CaretOffset + 1 || editor.CaretOffset < 3)
return;

var segment = new AnchorSegment(editor.Document, editor.CaretOffset - 1, 2);
var text = editor.Document.GetText(segment);
if (text == "{}")
Expand Down Expand Up @@ -779,7 +783,7 @@ protected override void ColorizeLine(DocumentLine line)
var text = CurrentContext.Document.GetText(line);
var start = 0;
int index;
while ((index = text.IndexOf(SelectionString, start)) >= 0)
while ((index = text.IndexOf(SelectionString, start, StringComparison.Ordinal)) >= 0)
{
ChangeLinePart(
lineStartOffset + index,
Expand Down

0 comments on commit eeb4398

Please sign in to comment.