From 5cadf03d88bb6b4113d969864a546ba85a54f4e2 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 27 Apr 2023 10:30:24 +0200 Subject: [PATCH] Pass the old document and the new document on DocumentChanged event --- .../Document/DocumentChangedEventArgs.cs | 25 +++++++++++++++++++ src/AvaloniaEdit/Editing/TextArea.cs | 4 +-- src/AvaloniaEdit/Rendering/TextView.cs | 4 +-- src/AvaloniaEdit/TextEditor.cs | 6 ++--- src/AvaloniaEdit/TextEditorComponent.cs | 2 +- 5 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 src/AvaloniaEdit/Document/DocumentChangedEventArgs.cs diff --git a/src/AvaloniaEdit/Document/DocumentChangedEventArgs.cs b/src/AvaloniaEdit/Document/DocumentChangedEventArgs.cs new file mode 100644 index 00000000..1913d2cc --- /dev/null +++ b/src/AvaloniaEdit/Document/DocumentChangedEventArgs.cs @@ -0,0 +1,25 @@ +using System; + +namespace AvaloniaEdit.Document +{ + public class DocumentChangedEventArgs : EventArgs + { + /// + /// Gets the old TextDocument. + /// + public TextDocument OldDocument { get; private set; } + /// + /// Gets the new TextDocument. + /// + public TextDocument NewDocument { get; private set; } + + /// + /// Provides data for the event. + /// + public DocumentChangedEventArgs(TextDocument oldDocument, TextDocument newDocument) + { + OldDocument = oldDocument; + NewDocument = newDocument; + } + } +} diff --git a/src/AvaloniaEdit/Editing/TextArea.cs b/src/AvaloniaEdit/Editing/TextArea.cs index 3f68dc27..ade1cb34 100644 --- a/src/AvaloniaEdit/Editing/TextArea.cs +++ b/src/AvaloniaEdit/Editing/TextArea.cs @@ -258,7 +258,7 @@ public TextDocument Document } /// - public event EventHandler DocumentChanged; + public event EventHandler DocumentChanged; /// /// Gets if the the document displayed by the text editor is readonly @@ -296,7 +296,7 @@ private void OnDocumentChanged(TextDocument oldValue, TextDocument newValue) // in the new document (e.g. if new document is shorter than the old document). Caret.Location = new TextLocation(1, 1); ClearSelection(); - DocumentChanged?.Invoke(this, EventArgs.Empty); + DocumentChanged?.Invoke(this, new DocumentChangedEventArgs(oldValue, newValue)); //CommandManager.InvalidateRequerySuggested(); } #endregion diff --git a/src/AvaloniaEdit/Rendering/TextView.cs b/src/AvaloniaEdit/Rendering/TextView.cs index bf781661..932750fa 100644 --- a/src/AvaloniaEdit/Rendering/TextView.cs +++ b/src/AvaloniaEdit/Rendering/TextView.cs @@ -130,7 +130,7 @@ internal FontFamily FontFamily /// /// Occurs when the document property has changed. /// - public event EventHandler DocumentChanged; + public event EventHandler DocumentChanged; private static void OnDocumentChanged(AvaloniaPropertyChangedEventArgs e) { @@ -159,7 +159,7 @@ private void OnDocumentChanged(TextDocument oldValue, TextDocument newValue) CachedElements = new TextViewCachedElements(); } InvalidateMeasure(); - DocumentChanged?.Invoke(this, EventArgs.Empty); + DocumentChanged?.Invoke(this, new DocumentChangedEventArgs(oldValue, newValue)); } private void RecreateCachedElements() diff --git a/src/AvaloniaEdit/TextEditor.cs b/src/AvaloniaEdit/TextEditor.cs index e1c44f82..1f7bba8e 100644 --- a/src/AvaloniaEdit/TextEditor.cs +++ b/src/AvaloniaEdit/TextEditor.cs @@ -120,12 +120,12 @@ public TextDocument Document /// /// Occurs when the document property has changed. /// - public event EventHandler DocumentChanged; + public event EventHandler DocumentChanged; /// /// Raises the event. /// - protected virtual void OnDocumentChanged(EventArgs e) + protected virtual void OnDocumentChanged(DocumentChangedEventArgs e) { DocumentChanged?.Invoke(this, e); } @@ -148,7 +148,7 @@ private void OnDocumentChanged(TextDocument oldValue, TextDocument newValue) TextDocumentWeakEventManager.TextChanged.AddHandler(newValue, OnTextChanged); PropertyChangedWeakEventManager.AddHandler(newValue.UndoStack, OnUndoStackPropertyChangedHandler); } - OnDocumentChanged(EventArgs.Empty); + OnDocumentChanged(new DocumentChangedEventArgs(oldValue, newValue)); OnTextChanged(EventArgs.Empty); } #endregion diff --git a/src/AvaloniaEdit/TextEditorComponent.cs b/src/AvaloniaEdit/TextEditorComponent.cs index a5c6a981..e404abf3 100644 --- a/src/AvaloniaEdit/TextEditorComponent.cs +++ b/src/AvaloniaEdit/TextEditorComponent.cs @@ -39,7 +39,7 @@ public interface ITextEditorComponent : IServiceProvider /// Occurs when the Document property changes (when the text editor is connected to another /// document - not when the document content changes). /// - event EventHandler DocumentChanged; + event EventHandler DocumentChanged; /// /// Gets the options of the text editor.