diff --git a/ICSharpCode.NRefactory/Editor/IDocument.cs b/ICSharpCode.NRefactory/Editor/IDocument.cs index 4201ce4484..f9a793b911 100644 --- a/ICSharpCode.NRefactory/Editor/IDocument.cs +++ b/ICSharpCode.NRefactory/Editor/IDocument.cs @@ -109,6 +109,18 @@ namespace ICSharpCode.NRefactory.Editor /// void Insert(int offset, string text); + /// + /// Inserts text. + /// + /// The offset at which the text is inserted. + /// The new text. + /// + /// Anchors positioned exactly at the insertion offset will move according to their movement type. + /// For AnchorMovementType.Default, they will move behind the inserted text. + /// The caret will also move behind the inserted text. + /// + void Insert(int offset, ITextSource text); + /// /// Inserts text. /// @@ -121,6 +133,18 @@ namespace ICSharpCode.NRefactory.Editor /// void Insert(int offset, string text, AnchorMovementType defaultAnchorMovementType); + /// + /// Inserts text. + /// + /// The offset at which the text is inserted. + /// The new text. + /// + /// Anchors positioned exactly at the insertion offset will move according to the anchor's movement type. + /// For AnchorMovementType.Default, they will move according to the movement type specified by this parameter. + /// The caret will also move according to the parameter. + /// + void Insert(int offset, ITextSource text, AnchorMovementType defaultAnchorMovementType); + /// /// Removes text. /// @@ -136,6 +160,14 @@ namespace ICSharpCode.NRefactory.Editor /// The new text. void Replace(int offset, int length, string newText); + /// + /// Replaces text. + /// + /// The starting offset of the text to be replaced. + /// The length of the text to be replaced. + /// The new text. + void Replace(int offset, int length, ITextSource newText); + /// /// Make the document combine the following actions into a single /// action for undo purposes. diff --git a/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs b/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs index a4d76a8394..2a36ccda33 100644 --- a/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs +++ b/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs @@ -257,6 +257,21 @@ namespace ICSharpCode.NRefactory.Editor throw new NotSupportedException(); } + void IDocument.Insert(int offset, ITextSource text) + { + throw new NotImplementedException(); + } + + void IDocument.Insert(int offset, ITextSource text, AnchorMovementType defaultAnchorMovementType) + { + throw new NotImplementedException(); + } + + void IDocument.Replace(int offset, int length, ITextSource newText) + { + throw new NotImplementedException(); + } + void IDocument.StartUndoableAction() { } diff --git a/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs b/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs index 79553a4136..f69070a196 100644 --- a/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs +++ b/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs @@ -104,17 +104,35 @@ namespace ICSharpCode.NRefactory.Editor Replace(offset, 0, text); } + /// + public void Insert(int offset, ITextSource text) + { + if (text == null) + throw new ArgumentNullException("text"); + Replace(offset, 0, text.Text); + } + /// public void Insert(int offset, string text, AnchorMovementType defaultAnchorMovementType) { if (offset < 0 || offset > this.TextLength) throw new ArgumentOutOfRangeException("offset"); + if (text == null) + throw new ArgumentNullException("text"); if (defaultAnchorMovementType == AnchorMovementType.BeforeInsertion) PerformChange(new InsertionWithMovementBefore(offset, text)); else Replace(offset, 0, text); } + /// + public void Insert(int offset, ITextSource text, AnchorMovementType defaultAnchorMovementType) + { + if (text == null) + throw new ArgumentNullException("text"); + Insert(offset, text.Text, defaultAnchorMovementType); + } + [Serializable] sealed class InsertionWithMovementBefore : TextChangeEventArgs { @@ -149,6 +167,14 @@ namespace ICSharpCode.NRefactory.Editor PerformChange(new TextChangeEventArgs(offset, b.ToString(offset, length), newText)); } + /// + public void Replace(int offset, int length, ITextSource newText) + { + if (newText == null) + throw new ArgumentNullException("newText"); + Replace(offset, length, newText.Text); + } + bool isInChange; void PerformChange(TextChangeEventArgs change) @@ -166,7 +192,7 @@ namespace ICSharpCode.NRefactory.Editor documentSnapshot = null; cachedText = null; b.Remove(change.Offset, change.RemovalLength); - b.Insert(change.Offset, change.InsertedText); + b.Insert(change.Offset, change.InsertedText.Text); versionProvider.AppendChange(change); // Update anchors and fire Deleted events diff --git a/ICSharpCode.NRefactory/Editor/StringTextSource.cs b/ICSharpCode.NRefactory/Editor/StringTextSource.cs index 3a24db3646..36780954bd 100644 --- a/ICSharpCode.NRefactory/Editor/StringTextSource.cs +++ b/ICSharpCode.NRefactory/Editor/StringTextSource.cs @@ -27,6 +27,11 @@ namespace ICSharpCode.NRefactory.Editor [Serializable] public class StringTextSource : ITextSource { + /// + /// Gets a text source containing the empty string. + /// + public static readonly StringTextSource Empty = new StringTextSource(string.Empty); + readonly string text; readonly ITextSourceVersion version; @@ -69,7 +74,7 @@ namespace ICSharpCode.NRefactory.Editor /// public ITextSource CreateSnapshot() { - return this; // StringTextBuffer is immutable + return this; // StringTextSource is immutable } /// diff --git a/ICSharpCode.NRefactory/Editor/TextChangeEventArgs.cs b/ICSharpCode.NRefactory/Editor/TextChangeEventArgs.cs index 936de37aa4..f1eeb4d27f 100644 --- a/ICSharpCode.NRefactory/Editor/TextChangeEventArgs.cs +++ b/ICSharpCode.NRefactory/Editor/TextChangeEventArgs.cs @@ -28,8 +28,8 @@ namespace ICSharpCode.NRefactory.Editor public class TextChangeEventArgs : EventArgs { readonly int offset; - readonly string removedText; - readonly string insertedText; + readonly ITextSource removedText; + readonly ITextSource insertedText; /// /// The offset at which the change occurs. @@ -41,7 +41,7 @@ namespace ICSharpCode.NRefactory.Editor /// /// The text that was removed. /// - public string RemovedText { + public ITextSource RemovedText { get { return removedText; } } @@ -49,13 +49,13 @@ namespace ICSharpCode.NRefactory.Editor /// The number of characters removed. /// public int RemovalLength { - get { return removedText.Length; } + get { return removedText.TextLength; } } /// /// The text that was inserted. /// - public string InsertedText { + public ITextSource InsertedText { get { return insertedText; } } @@ -63,7 +63,7 @@ namespace ICSharpCode.NRefactory.Editor /// The number of characters inserted. /// public int InsertionLength { - get { return insertedText.Length; } + get { return insertedText.TextLength; } } /// @@ -71,9 +71,23 @@ namespace ICSharpCode.NRefactory.Editor /// public TextChangeEventArgs(int offset, string removedText, string insertedText) { + if (offset < 0) + throw new ArgumentOutOfRangeException("offset", offset, "offset must not be negative"); this.offset = offset; - this.removedText = removedText ?? string.Empty; - this.insertedText = insertedText ?? string.Empty; + this.removedText = removedText != null ? new StringTextSource(removedText) : StringTextSource.Empty; + this.insertedText = insertedText != null ? new StringTextSource(insertedText) : StringTextSource.Empty; + } + + /// + /// Creates a new TextChangeEventArgs object. + /// + public TextChangeEventArgs(int offset, ITextSource removedText, ITextSource insertedText) + { + if (offset < 0) + throw new ArgumentOutOfRangeException("offset", offset, "offset must not be negative"); + this.offset = offset; + this.removedText = removedText ?? StringTextSource.Empty; + this.insertedText = insertedText ?? StringTextSource.Empty; } ///