1 | // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | using System.IO; |
---|
21 | using ICSharpCode.AvalonEdit.Utils; |
---|
22 | #if NREFACTORY |
---|
23 | using ICSharpCode.NRefactory.Editor; |
---|
24 | #endif |
---|
25 | |
---|
26 | namespace ICSharpCode.AvalonEdit.Document |
---|
27 | { |
---|
28 | /// <summary> |
---|
29 | /// Implements the ITextSource interface using a rope. |
---|
30 | /// </summary> |
---|
31 | [Serializable] |
---|
32 | public sealed class RopeTextSource : ITextSource |
---|
33 | { |
---|
34 | readonly Rope<char> rope; |
---|
35 | readonly ITextSourceVersion version; |
---|
36 | |
---|
37 | /// <summary> |
---|
38 | /// Creates a new RopeTextSource. |
---|
39 | /// </summary> |
---|
40 | public RopeTextSource(Rope<char> rope) |
---|
41 | { |
---|
42 | if (rope == null) |
---|
43 | throw new ArgumentNullException("rope"); |
---|
44 | this.rope = rope.Clone(); |
---|
45 | } |
---|
46 | |
---|
47 | /// <summary> |
---|
48 | /// Creates a new RopeTextSource. |
---|
49 | /// </summary> |
---|
50 | public RopeTextSource(Rope<char> rope, ITextSourceVersion version) |
---|
51 | { |
---|
52 | if (rope == null) |
---|
53 | throw new ArgumentNullException("rope"); |
---|
54 | this.rope = rope.Clone(); |
---|
55 | this.version = version; |
---|
56 | } |
---|
57 | |
---|
58 | /// <summary> |
---|
59 | /// Returns a clone of the rope used for this text source. |
---|
60 | /// </summary> |
---|
61 | /// <remarks> |
---|
62 | /// RopeTextSource only publishes a copy of the contained rope to ensure that the underlying rope cannot be modified. |
---|
63 | /// </remarks> |
---|
64 | [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification="Not a property because it creates a clone")] |
---|
65 | public Rope<char> GetRope() |
---|
66 | { |
---|
67 | return rope.Clone(); |
---|
68 | } |
---|
69 | |
---|
70 | /// <inheritdoc/> |
---|
71 | public string Text { |
---|
72 | get { return rope.ToString(); } |
---|
73 | } |
---|
74 | |
---|
75 | /// <inheritdoc/> |
---|
76 | public int TextLength { |
---|
77 | get { return rope.Length; } |
---|
78 | } |
---|
79 | |
---|
80 | /// <inheritdoc/> |
---|
81 | public char GetCharAt(int offset) |
---|
82 | { |
---|
83 | return rope[offset]; |
---|
84 | } |
---|
85 | |
---|
86 | /// <inheritdoc/> |
---|
87 | public string GetText(int offset, int length) |
---|
88 | { |
---|
89 | return rope.ToString(offset, length); |
---|
90 | } |
---|
91 | |
---|
92 | /// <inheritdoc/> |
---|
93 | public string GetText(ISegment segment) |
---|
94 | { |
---|
95 | return rope.ToString(segment.Offset, segment.Length); |
---|
96 | } |
---|
97 | |
---|
98 | /// <inheritdoc/> |
---|
99 | public TextReader CreateReader() |
---|
100 | { |
---|
101 | return new RopeTextReader(rope); |
---|
102 | } |
---|
103 | |
---|
104 | /// <inheritdoc/> |
---|
105 | public TextReader CreateReader(int offset, int length) |
---|
106 | { |
---|
107 | return new RopeTextReader(rope.GetRange(offset, length)); |
---|
108 | } |
---|
109 | |
---|
110 | /// <inheritdoc/> |
---|
111 | public ITextSource CreateSnapshot() |
---|
112 | { |
---|
113 | return this; |
---|
114 | } |
---|
115 | |
---|
116 | /// <inheritdoc/> |
---|
117 | public ITextSource CreateSnapshot(int offset, int length) |
---|
118 | { |
---|
119 | return new RopeTextSource(rope.GetRange(offset, length)); |
---|
120 | } |
---|
121 | |
---|
122 | /// <inheritdoc/> |
---|
123 | public int IndexOf(char c, int startIndex, int count) |
---|
124 | { |
---|
125 | return rope.IndexOf(c, startIndex, count); |
---|
126 | } |
---|
127 | |
---|
128 | /// <inheritdoc/> |
---|
129 | public int IndexOfAny(char[] anyOf, int startIndex, int count) |
---|
130 | { |
---|
131 | return rope.IndexOfAny(anyOf, startIndex, count); |
---|
132 | } |
---|
133 | |
---|
134 | /// <inheritdoc/> |
---|
135 | public int LastIndexOf(char c, int startIndex, int count) |
---|
136 | { |
---|
137 | return rope.LastIndexOf(c, startIndex, count); |
---|
138 | } |
---|
139 | |
---|
140 | /// <inheritdoc/> |
---|
141 | public ITextSourceVersion Version { |
---|
142 | get { return version; } |
---|
143 | } |
---|
144 | |
---|
145 | /// <inheritdoc/> |
---|
146 | public int IndexOf(string searchText, int startIndex, int count, StringComparison comparisonType) |
---|
147 | { |
---|
148 | return rope.IndexOf(searchText, startIndex, count, comparisonType); |
---|
149 | } |
---|
150 | |
---|
151 | /// <inheritdoc/> |
---|
152 | public int LastIndexOf(string searchText, int startIndex, int count, StringComparison comparisonType) |
---|
153 | { |
---|
154 | return rope.LastIndexOf(searchText, startIndex, count, comparisonType); |
---|
155 | } |
---|
156 | |
---|
157 | /// <inheritdoc/> |
---|
158 | public void WriteTextTo(TextWriter writer) |
---|
159 | { |
---|
160 | rope.WriteTo(writer, 0, rope.Length); |
---|
161 | } |
---|
162 | |
---|
163 | /// <inheritdoc/> |
---|
164 | public void WriteTextTo(TextWriter writer, int offset, int length) |
---|
165 | { |
---|
166 | rope.WriteTo(writer, offset, length); |
---|
167 | } |
---|
168 | } |
---|
169 | } |
---|