From 819bab82874b8c9a3b967fe327ea3f242db42564 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Tue, 13 Oct 2009 16:42:49 +0000 Subject: [PATCH] Fixed change grouping when the document is changed inside event handlers for TextDocument.TextChanged. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@5067 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Document/TextDocument.cs | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs index 3f7ba59571..aa50c64a49 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs @@ -199,6 +199,7 @@ namespace ICSharpCode.AvalonEdit.Document /// /// BeginUpdate() /// + /// Start of change group (on undo stack) /// event is raised /// /// Insert() / Remove() / Replace() @@ -214,6 +215,7 @@ namespace ICSharpCode.AvalonEdit.Document /// event is raised /// event is raised /// event is raised + /// End of change group (on undo stack) /// event is raised /// /// @@ -355,11 +357,15 @@ namespace ICSharpCode.AvalonEdit.Document throw new InvalidOperationException("Cannot end update within document change."); if (beginUpdateCount == 0) throw new InvalidOperationException("No update is active."); - beginUpdateCount -= 1; - if (beginUpdateCount == 0) { + if (beginUpdateCount == 1) { + // fire change events inside the change group - event handlers might add additional + // document changes to the change group FireChangeEvents(); + beginUpdateCount = 0; if (UpdateFinished != null) UpdateFinished(this, EventArgs.Empty); + } else { + beginUpdateCount -= 1; } } @@ -382,30 +388,29 @@ namespace ICSharpCode.AvalonEdit.Document bool fireTextChanged; /// - /// Fires TextChanged, TextLengthChanged, TotalHeightChanged, LineCountChanged if required. + /// Fires TextChanged, TextLengthChanged, LineCountChanged if required. /// internal void FireChangeEvents() { - if (beginUpdateCount > 0) - return; - - if (fireTextChanged) { + // it may be necessary to fire the event multiple times if the document is changed + // from inside the event handlers + while (fireTextChanged) { fireTextChanged = false; if (TextChanged != null) TextChanged(this, EventArgs.Empty); - } - - int textLength = rope.Length; - if (textLength != oldTextLength) { - oldTextLength = textLength; - if (TextLengthChanged != null) - TextLengthChanged(this, EventArgs.Empty); - } - int lineCount = lineTree.LineCount; - if (lineCount != oldLineCount) { - oldLineCount = lineCount; - if (LineCountChanged != null) - LineCountChanged(this, EventArgs.Empty); + + int textLength = rope.Length; + if (textLength != oldTextLength) { + oldTextLength = textLength; + if (TextLengthChanged != null) + TextLengthChanged(this, EventArgs.Empty); + } + int lineCount = lineTree.LineCount; + if (lineCount != oldLineCount) { + oldLineCount = lineCount; + if (LineCountChanged != null) + LineCountChanged(this, EventArgs.Empty); + } } } #endregion