Skip to content

Commit

Permalink
Fix CTD when editing text by bypassing undostack of editor (we have o…
Browse files Browse the repository at this point in the history
…ur own)
  • Loading branch information
bcssov committed Mar 7, 2024
1 parent d4b13f1 commit 9c6b5a7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
25 changes: 24 additions & 1 deletion src/IronyModManager/Controls/TextEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 04-15-2020
//
// Last Modified By : Mario
// Last Modified On : 02-24-2024
// Last Modified On : 03-07-2024
// ***********************************************************************
// <copyright file="TextEditor.cs" company="Mario">
// Mario
Expand All @@ -21,6 +21,7 @@
using Avalonia.Controls.Primitives;
using Avalonia.Styling;
using AvaloniaEdit;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;
using IronyModManager.DI;
using IronyModManager.Implementation.Actions;
Expand Down Expand Up @@ -126,6 +127,17 @@ public int GetTopVisibleLine()
return firstLine;
}

/// <summary>
/// Saves a set text.
/// </summary>
/// <param name="value">The value.</param>
public void SafeSetText(string value)
{
var document = GetDocument();
document.Text = value ?? string.Empty;
CaretOffset = 0;
}

/// <summary>
/// Scroll to.
/// </summary>
Expand Down Expand Up @@ -253,6 +265,17 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
}
}

/// <summary>
/// Get document.
/// </summary>
/// <returns>A TextDocument.<see cref="TextDocument" /></returns>
/// <exception cref="System.NullReferenceException">No document</exception>
private TextDocument GetDocument()
{
var document = Document;
return document ?? throw new NullReferenceException("No document");
}

#endregion Methods
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Created : 03-20-2020
//
// Last Modified By : Mario
// Last Modified On : 02-24-2024
// Last Modified On : 03-07-2024
// ***********************************************************************
// <copyright file="MergeViewerControlView.xaml.cs" company="Mario">
// Mario
Expand Down Expand Up @@ -353,8 +353,8 @@ protected override void OnActivated(CompositeDisposable disposables)
diffLeft.TextArea.TextView.BackgroundRenderers.Add(diffLeftRenderer);
diffRight.TextArea.LeftMargins.Add(diffRightMargin);
diffRight.TextArea.TextView.BackgroundRenderers.Add(diffRightRenderer);
diffLeft.Text = string.Join(Environment.NewLine, ViewModel.LeftDiff);
diffRight.Text = string.Join(Environment.NewLine, ViewModel.RightDiff);
diffLeft.SafeSetText(string.Join(Environment.NewLine, ViewModel.LeftDiff));
diffRight.SafeSetText(string.Join(Environment.NewLine, ViewModel.RightDiff));
diffLeft.IsReadOnly = !ViewModel.LeftSidePatchMod;
diffRight.IsReadOnly = !ViewModel.RightSidePatchMod;

Expand Down Expand Up @@ -533,7 +533,9 @@ void evalKey()
return;
}
diffLeft.Text = newText;
var locLine = diffLeft.TextArea.Caret.Location.Line;
diffLeft.SafeSetText(newText);
diffLeft.TextArea.Caret.Location = new TextLocation(locLine, diffLeft.CaretOffset);
}).DisposeWith(disposables);

this.WhenAnyValue(v => v.ViewModel.RightDiff).Subscribe(s =>
Expand All @@ -548,7 +550,9 @@ void evalKey()
return;
}
diffRight.Text = newText;
var locLine = diffRight.TextArea.Caret.Location.Line;
diffRight.SafeSetText(newText);
diffRight.TextArea.Caret.Location = new TextLocation(locLine, diffRight.CaretOffset);
});

this.WhenAnyValue(v => v.ViewModel.LeftSidePatchMod).Subscribe(s =>
Expand Down

0 comments on commit 9c6b5a7

Please sign in to comment.