From 7dd5d6710205de8c61ce521b9b98e49c43dd7e6f Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Thu, 27 Aug 2009 11:54:59 +0000 Subject: [PATCH] ITextBufferVersion: add MoveOffsetTo method git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@4782 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Document/ChangeTrackingCheckpoint.cs | 16 +++++++++++++++- .../AvalonEdit/AvalonEditDocumentAdapter.cs | 15 +++++++++++++++ src/Main/Base/Project/Src/Editor/ITextBuffer.cs | 6 ++++++ .../Src/ProjectContent/DefaultProjectContent.cs | 6 +++++- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/ChangeTrackingCheckpoint.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/ChangeTrackingCheckpoint.cs index 2f44ae50c0..25fd32be8e 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/ChangeTrackingCheckpoint.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/ChangeTrackingCheckpoint.cs @@ -72,7 +72,7 @@ namespace ICSharpCode.AvalonEdit.Document throw new ArgumentNullException("other"); return documentIdentifier == other.documentIdentifier; } - + /// /// Compares the age of this checkpoint to the other checkpoint. /// @@ -118,5 +118,19 @@ namespace ICSharpCode.AvalonEdit.Document yield return node.value; } while (node != other); } + + /// + /// Calculates where the offset has moved in the other buffer version. + /// + /// This method is thread-safe. + /// Raised if 'other' belongs to a different document than this checkpoint. + public int MoveOffsetTo(ChangeTrackingCheckpoint other, int oldOffset, AnchorMovementType movement) + { + int offset = oldOffset; + foreach (DocumentChangeEventArgs e in GetChangesTo(other)) { + offset = e.GetNewOffset(offset, movement); + } + return offset; + } } } diff --git a/src/Main/Base/Project/Src/Editor/AvalonEdit/AvalonEditDocumentAdapter.cs b/src/Main/Base/Project/Src/Editor/AvalonEdit/AvalonEditDocumentAdapter.cs index 03de7114c9..53ecc55212 100644 --- a/src/Main/Base/Project/Src/Editor/AvalonEdit/AvalonEditDocumentAdapter.cs +++ b/src/Main/Base/Project/Src/Editor/AvalonEdit/AvalonEditDocumentAdapter.cs @@ -237,6 +237,21 @@ namespace ICSharpCode.SharpDevelop.Editor.AvalonEdit throw new ArgumentException("Does not belong to same document"); return checkpoint.GetChangesTo(otherVersion.checkpoint).Select(c => new TextChangeEventArgs(c.Offset, c.RemovedText, c.InsertedText)); } + + public int MoveOffsetTo(ITextBufferVersion other, int oldOffset, AnchorMovementType movement) + { + SnapshotVersion otherVersion = other as SnapshotVersion; + if (otherVersion == null) + throw new ArgumentException("Does not belong to same document"); + switch (movement) { + case AnchorMovementType.AfterInsertion: + return checkpoint.MoveOffsetTo(otherVersion.checkpoint, oldOffset, ICSharpCode.AvalonEdit.Document.AnchorMovementType.AfterInsertion); + case AnchorMovementType.BeforeInsertion: + return checkpoint.MoveOffsetTo(otherVersion.checkpoint, oldOffset, ICSharpCode.AvalonEdit.Document.AnchorMovementType.BeforeInsertion); + default: + throw new NotSupportedException(); + } + } } #endregion diff --git a/src/Main/Base/Project/Src/Editor/ITextBuffer.cs b/src/Main/Base/Project/Src/Editor/ITextBuffer.cs index 9a460f3be4..25bd125f94 100644 --- a/src/Main/Base/Project/Src/Editor/ITextBuffer.cs +++ b/src/Main/Base/Project/Src/Editor/ITextBuffer.cs @@ -113,6 +113,12 @@ namespace ICSharpCode.SharpDevelop /// This method is thread-safe. /// Raised if 'other' belongs to a different document than this checkpoint. IEnumerable GetChangesTo(ITextBufferVersion other); + + /// + /// Calculates where the offset has moved in the other buffer version. + /// + /// Raised if 'other' belongs to a different document than this checkpoint. + int MoveOffsetTo(ITextBufferVersion other, int oldOffset, AnchorMovementType movement); } public sealed class StringTextBuffer : Editor.AvalonEdit.AvalonEditTextSourceAdapter diff --git a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/ProjectContent/DefaultProjectContent.cs b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/ProjectContent/DefaultProjectContent.cs index 02fc743235..4cb247f778 100644 --- a/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/ProjectContent/DefaultProjectContent.cs +++ b/src/Main/ICSharpCode.SharpDevelop.Dom/Project/Src/ProjectContent/DefaultProjectContent.cs @@ -249,7 +249,11 @@ namespace ICSharpCode.SharpDevelop.Dom [Conditional("DEBUG")] void CheckNotDisposed() { - Debug.Assert(!isDisposed); + // TODO: this is broken - we are accessing project contents even after + // they have been unloaded, e.g. on other threads + if (!isDisposed) { + // throw new ObjectDisposedException(); + } } public void AddClassToNamespaceList(IClass addClass)