From 7a3b25b4dc85643ec1550e68afda45c864e370cf Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 3 Aug 2012 18:15:32 +0200 Subject: [PATCH] Add WriteTextTo() method to ITextSource. --- ICSharpCode.NRefactory/Editor/ITextSource.cs | 10 ++++++ .../Editor/ReadOnlyDocument.cs | 12 +++++++ .../Editor/StringBuilderDocument.cs | 34 +++++++++++++++++++ .../Editor/StringTextSource.cs | 12 +++++++ 4 files changed, 68 insertions(+) diff --git a/ICSharpCode.NRefactory/Editor/ITextSource.cs b/ICSharpCode.NRefactory/Editor/ITextSource.cs index c8810571de..148aee0d29 100644 --- a/ICSharpCode.NRefactory/Editor/ITextSource.cs +++ b/ICSharpCode.NRefactory/Editor/ITextSource.cs @@ -94,6 +94,16 @@ namespace ICSharpCode.NRefactory.Editor /// offset or length is outside the valid range. string GetText(ISegment segment); + /// + /// Writes the text from this document into the TextWriter. + /// + void WriteTextTo(TextWriter writer); + + /// + /// Writes the text from this document into the TextWriter. + /// + void WriteTextTo(TextWriter writer, int offset, int length); + /// /// Gets the index of the first occurrence of the character in the specified array. /// diff --git a/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs b/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs index 2a36ccda33..0cc4b68dc5 100644 --- a/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs +++ b/ICSharpCode.NRefactory/Editor/ReadOnlyDocument.cs @@ -359,6 +359,18 @@ namespace ICSharpCode.NRefactory.Editor return textSource.CreateReader(offset, length); } + /// + public void WriteTextTo(System.IO.TextWriter writer) + { + textSource.WriteTextTo(writer); + } + + /// + public void WriteTextTo(System.IO.TextWriter writer, int offset, int length) + { + textSource.WriteTextTo(writer, offset, length); + } + /// public char GetCharAt(int offset) { diff --git a/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs b/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs index f69070a196..d310f34c4a 100644 --- a/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs +++ b/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs @@ -41,11 +41,27 @@ namespace ICSharpCode.NRefactory.Editor b = new StringBuilder(); } + /// + /// Creates a new StringBuilderDocument with the specified initial text. + /// public StringBuilderDocument(string text) { + if (text == null) + throw new ArgumentNullException("text"); b = new StringBuilder(text); } + /// + /// Creates a new StringBuilderDocument with the initial text copied from the specified text source. + /// + public StringBuilderDocument(ITextSource textSource) + { + if (textSource == null) + throw new ArgumentNullException("textSource"); + b = new StringBuilder(textSource.TextLength); + textSource.WriteTextTo(new StringWriter(b)); + } + /// public event EventHandler TextChanging; @@ -273,6 +289,22 @@ namespace ICSharpCode.NRefactory.Editor { return new StringReader(GetText(offset, length)); } + + /// + public void WriteTextTo(TextWriter writer) + { + if (writer == null) + throw new ArgumentNullException("writer"); + writer.Write(this.Text); + } + + /// + public void WriteTextTo(TextWriter writer, int offset, int length) + { + if (writer == null) + throw new ArgumentNullException("writer"); + writer.Write(GetText(offset, length)); + } #endregion #region GetText / IndexOf @@ -310,6 +342,8 @@ namespace ICSharpCode.NRefactory.Editor /// public string GetText(ISegment segment) { + if (segment == null) + throw new ArgumentNullException("segment"); return b.ToString(segment.Offset, segment.Length); } diff --git a/ICSharpCode.NRefactory/Editor/StringTextSource.cs b/ICSharpCode.NRefactory/Editor/StringTextSource.cs index 36780954bd..99a81f02a7 100644 --- a/ICSharpCode.NRefactory/Editor/StringTextSource.cs +++ b/ICSharpCode.NRefactory/Editor/StringTextSource.cs @@ -95,6 +95,18 @@ namespace ICSharpCode.NRefactory.Editor return new StringReader(text.Substring(offset, length)); } + /// + public void WriteTextTo(TextWriter writer) + { + writer.Write(text); + } + + /// + public void WriteTextTo(TextWriter writer, int offset, int length) + { + writer.Write(text.Substring(offset, length)); + } + /// public char GetCharAt(int offset) {