// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software // without restriction, including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons // to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. using System; using System.Diagnostics.CodeAnalysis; using System.IO; using Unity.CodeEditor.Utils; namespace Unity.CodeEditor.Document { /// /// Implements the ITextSource interface using a rope. /// internal sealed class RopeTextSource : ITextSource { private readonly Rope _rope; /// /// Creates a new RopeTextSource. /// internal RopeTextSource(Rope rope) { _rope = rope?.Clone() ?? throw new ArgumentNullException(nameof(rope)); } /// /// Creates a new RopeTextSource. /// internal RopeTextSource(Rope rope, ITextSourceVersion version) { _rope = rope?.Clone() ?? throw new ArgumentNullException(nameof(rope)); Version = version; } /// /// Returns a clone of the rope used for this text source. /// /// /// RopeTextSource only publishes a copy of the contained rope to ensure that the underlying rope cannot be modified. /// [SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification="Not a property because it creates a clone")] internal Rope GetRope() { return _rope.Clone(); } /// public string Text => _rope.ToString(); /// public int TextLength => _rope.Length; /// public char GetCharAt(int offset) { return _rope[offset]; } /// public string GetText(int offset, int length) { return _rope.ToString(offset, length); } /// public ReadOnlyMemory GetTextAsMemory(int offset, int length) { return _rope.GetMemory(offset, length); } /// public string GetText(ISegment segment) { return _rope.ToString(segment.Offset, segment.Length); } /// public TextReader CreateReader() { return new RopeTextReader(_rope); } /// public TextReader CreateReader(int offset, int length) { return new RopeTextReader(_rope.GetRange(offset, length)); } /// public ITextSource CreateSnapshot() { return this; } /// public ITextSource CreateSnapshot(int offset, int length) { return new RopeTextSource(_rope.GetRange(offset, length)); } /// public int IndexOf(char c, int startIndex, int count) { return _rope.IndexOf(c, startIndex, count); } /// public int IndexOfAny(char[] anyOf, int startIndex, int count) { return _rope.IndexOfAny(anyOf, startIndex, count); } /// public int LastIndexOf(char c, int startIndex, int count) { return _rope.LastIndexOf(c, startIndex, count); } /// public ITextSourceVersion Version { get; } /// public int IndexOf(string searchText, int startIndex, int count, StringComparison comparisonType) { return _rope.IndexOf(searchText, startIndex, count, comparisonType); } /// public int LastIndexOf(string searchText, int startIndex, int count, StringComparison comparisonType) { return _rope.LastIndexOf(searchText, startIndex, count, comparisonType); } /// public void WriteTextTo(TextWriter writer) { _rope.WriteTo(writer, 0, _rope.Length); } /// public void WriteTextTo(TextWriter writer, int offset, int length) { _rope.WriteTo(writer, offset, length); } } }