From c001357617c2b59d08534f9532e8bdb6bd780893 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Tue, 20 Sep 2011 02:46:32 +0200 Subject: [PATCH] Add GetNewOffset() method to TextChangeEventArgs. --- .../Editor/ITextAnchor.cs | 4 ++-- .../Editor/TextChangeEventArgs.cs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/ITextAnchor.cs b/src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/ITextAnchor.cs index 6020ddc8dd..be1f084eec 100644 --- a/src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/ITextAnchor.cs +++ b/src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/ITextAnchor.cs @@ -121,8 +121,8 @@ namespace ICSharpCode.NRefactory.Editor { /// /// When text is inserted at the anchor position, the type of the insertion - /// determines where the caret moves to. For normal insertions, the anchor will stay - /// behind the inserted text. + /// determines where the caret moves to. For normal insertions, the anchor will move + /// after the inserted text. /// Default, /// diff --git a/src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/TextChangeEventArgs.cs b/src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/TextChangeEventArgs.cs index a0c0f659ee..e97b12de66 100644 --- a/src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/TextChangeEventArgs.cs +++ b/src/Libraries/NRefactory/ICSharpCode.NRefactory/Editor/TextChangeEventArgs.cs @@ -75,5 +75,22 @@ namespace ICSharpCode.NRefactory.Editor this.removedText = removedText ?? string.Empty; this.insertedText = insertedText ?? string.Empty; } + + /// + /// Gets the new offset where the specified offset moves after this document change. + /// + public virtual int GetNewOffset(int offset, AnchorMovementType movementType = AnchorMovementType.Default) + { + if (offset >= this.Offset && offset <= this.Offset + this.RemovalLength) { + if (movementType == AnchorMovementType.BeforeInsertion) + return this.Offset; + else + return this.Offset + this.InsertionLength; + } else if (offset > this.Offset) { + return offset + this.InsertionLength - this.RemovalLength; + } else { + return offset; + } + } } }