~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory/Editor/StringBuilderDocument.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
ļ»æ// Copyright (c) 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.Collections.Generic;
21
 
using System.IO;
22
 
using System.Text;
23
 
using ICSharpCode.NRefactory.Utils;
24
 
 
25
 
namespace ICSharpCode.NRefactory.Editor
26
 
{
27
 
        /// <summary>
28
 
        /// Document based on a string builder.
29
 
        /// This class serves as a reference implementation for the IDocument interface.
30
 
        /// </summary>
31
 
        public class StringBuilderDocument : IDocument
32
 
        {
33
 
                readonly StringBuilder b;
34
 
                readonly TextSourceVersionProvider versionProvider = new TextSourceVersionProvider();
35
 
                
36
 
                /// <summary>
37
 
                /// Creates a new StringBuilderDocument.
38
 
                /// </summary>
39
 
                public StringBuilderDocument()
40
 
                {
41
 
                        b = new StringBuilder();
42
 
                }
43
 
                
44
 
                public StringBuilderDocument(string text)
45
 
                {
46
 
                        b = new StringBuilder(text);
47
 
                }
48
 
                
49
 
                /// <inheritdoc/>
50
 
                public event EventHandler<TextChangeEventArgs> TextChanging;
51
 
                
52
 
                /// <inheritdoc/>
53
 
                public event EventHandler<TextChangeEventArgs> TextChanged;
54
 
                
55
 
                /// <inheritdoc/>
56
 
                public event EventHandler ChangeCompleted;
57
 
                
58
 
                /// <inheritdoc/>
59
 
                public ITextSourceVersion Version {
60
 
                        get { return versionProvider.CurrentVersion; }
61
 
                }
62
 
                
63
 
                #region Line<->Offset
64
 
                /// <inheritdoc/>
65
 
                public int LineCount {
66
 
                        get { return CreateDocumentSnapshot().LineCount; }
67
 
                }
68
 
                
69
 
                /// <inheritdoc/>
70
 
                public IDocumentLine GetLineByNumber(int lineNumber)
71
 
                {
72
 
                        return CreateDocumentSnapshot().GetLineByNumber(lineNumber);
73
 
                }
74
 
                
75
 
                /// <inheritdoc/>
76
 
                public IDocumentLine GetLineByOffset(int offset)
77
 
                {
78
 
                        return CreateDocumentSnapshot().GetLineByOffset(offset);
79
 
                }
80
 
                
81
 
                /// <inheritdoc/>
82
 
                public int GetOffset(int line, int column)
83
 
                {
84
 
                        return CreateDocumentSnapshot().GetOffset(line, column);
85
 
                }
86
 
                
87
 
                /// <inheritdoc/>
88
 
                public int GetOffset(TextLocation location)
89
 
                {
90
 
                        return CreateDocumentSnapshot().GetOffset(location);
91
 
                }
92
 
                
93
 
                /// <inheritdoc/>
94
 
                public TextLocation GetLocation(int offset)
95
 
                {
96
 
                        return CreateDocumentSnapshot().GetLocation(offset);
97
 
                }
98
 
                #endregion
99
 
                
100
 
                #region Insert/Remove/Replace
101
 
                /// <inheritdoc/>
102
 
                public void Insert(int offset, string text)
103
 
                {
104
 
                        Replace(offset, 0, text);
105
 
                }
106
 
                
107
 
                /// <inheritdoc/>
108
 
                public void Insert(int offset, string text, AnchorMovementType defaultAnchorMovementType)
109
 
                {
110
 
                        if (offset < 0 || offset > this.TextLength)
111
 
                                throw new ArgumentOutOfRangeException("offset");
112
 
                        if (defaultAnchorMovementType == AnchorMovementType.BeforeInsertion)
113
 
                                PerformChange(new InsertionWithMovementBefore(offset, text));
114
 
                        else
115
 
                                Replace(offset, 0, text);
116
 
                }
117
 
                
118
 
                [Serializable]
119
 
                sealed class InsertionWithMovementBefore : TextChangeEventArgs
120
 
                {
121
 
                        public InsertionWithMovementBefore(int offset, string newText) : base(offset, string.Empty, newText)
122
 
                        {
123
 
                        }
124
 
                        
125
 
                        public override int GetNewOffset(int offset, AnchorMovementType movementType)
126
 
                        {
127
 
                                if (offset == this.Offset && movementType == AnchorMovementType.Default)
128
 
                                        return offset;
129
 
                                else
130
 
                                        return base.GetNewOffset(offset, movementType);
131
 
                        }
132
 
                }
133
 
                
134
 
                /// <inheritdoc/>
135
 
                public void Remove(int offset, int length)
136
 
                {
137
 
                        Replace(offset, length, string.Empty);
138
 
                }
139
 
                
140
 
                /// <inheritdoc/>
141
 
                public void Replace(int offset, int length, string newText)
142
 
                {
143
 
                        if (offset < 0 || offset > this.TextLength)
144
 
                                throw new ArgumentOutOfRangeException("offset");
145
 
                        if (length < 0 || length > this.TextLength - offset)
146
 
                                throw new ArgumentOutOfRangeException("length");
147
 
                        if (newText == null)
148
 
                                throw new ArgumentNullException("newText");
149
 
                        PerformChange(new TextChangeEventArgs(offset, b.ToString(offset, length), newText));
150
 
                }
151
 
                
152
 
                bool isInChange;
153
 
                
154
 
                void PerformChange(TextChangeEventArgs change)
155
 
                {
156
 
                        // Ensure that all changes take place inside an update group.
157
 
                        // Will also take care of throwing an exception if isInChange is set.
158
 
                        StartUndoableAction();
159
 
                        try {
160
 
                                isInChange = true;
161
 
                                try {
162
 
                                        if (TextChanging != null)
163
 
                                                TextChanging(this, change);
164
 
                                        
165
 
                                        // Perform changes to document and Version property
166
 
                                        documentSnapshot = null;
167
 
                                        cachedText = null;
168
 
                                        b.Remove(change.Offset, change.RemovalLength);
169
 
                                        b.Insert(change.Offset, change.InsertedText);
170
 
                                        versionProvider.AppendChange(change);
171
 
                                        
172
 
                                        // Update anchors and fire Deleted events
173
 
                                        UpdateAnchors(change);
174
 
                                        
175
 
                                        if (TextChanged != null)
176
 
                                                TextChanged(this, change);
177
 
                                } finally {
178
 
                                        isInChange = false;
179
 
                                }
180
 
                        } finally {
181
 
                                EndUndoableAction();
182
 
                        }
183
 
                }
184
 
                #endregion
185
 
                
186
 
                #region Undo
187
 
                int undoGroupNesting = 0;
188
 
                
189
 
                /// <inheritdoc/>
190
 
                public void StartUndoableAction()
191
 
                {
192
 
                        // prevent changes from within the TextChanging/TextChanged event handlers
193
 
                        if (isInChange)
194
 
                                throw new InvalidOperationException();
195
 
                        undoGroupNesting++;
196
 
                }
197
 
                
198
 
                /// <inheritdoc/>
199
 
                public void EndUndoableAction()
200
 
                {
201
 
                        undoGroupNesting--;
202
 
                        if (undoGroupNesting == 0) {
203
 
                                if (ChangeCompleted != null)
204
 
                                        ChangeCompleted(this, EventArgs.Empty);
205
 
                        }
206
 
                }
207
 
                
208
 
                /// <inheritdoc/>
209
 
                public IDisposable OpenUndoGroup()
210
 
                {
211
 
                        StartUndoableAction();
212
 
                        return new CallbackOnDispose(EndUndoableAction);
213
 
                }
214
 
                #endregion
215
 
                
216
 
                #region CreateSnapshot/CreateReader
217
 
                ReadOnlyDocument documentSnapshot;
218
 
                
219
 
                /// <inheritdoc/>
220
 
                public IDocument CreateDocumentSnapshot()
221
 
                {
222
 
                        if (documentSnapshot == null)
223
 
                                documentSnapshot = new ReadOnlyDocument(this);
224
 
                        return documentSnapshot;
225
 
                }
226
 
                
227
 
                /// <inheritdoc/>
228
 
                public ITextSource CreateSnapshot()
229
 
                {
230
 
                        return new StringTextSource(this.Text, versionProvider.CurrentVersion);
231
 
                }
232
 
                
233
 
                /// <inheritdoc/>
234
 
                public ITextSource CreateSnapshot(int offset, int length)
235
 
                {
236
 
                        return new StringTextSource(GetText(offset, length));
237
 
                }
238
 
                
239
 
                /// <inheritdoc/>
240
 
                public TextReader CreateReader()
241
 
                {
242
 
                        return new StringReader(this.Text);
243
 
                }
244
 
                
245
 
                /// <inheritdoc/>
246
 
                public TextReader CreateReader(int offset, int length)
247
 
                {
248
 
                        return new StringReader(GetText(offset, length));
249
 
                }
250
 
                #endregion
251
 
                
252
 
                #region GetText / IndexOf
253
 
                string cachedText;
254
 
                
255
 
                /// <inheritdoc/>
256
 
                public string Text {
257
 
                        get { 
258
 
                                if (cachedText == null)
259
 
                                        cachedText = b.ToString();
260
 
                                return cachedText;
261
 
                        }
262
 
                        set {
263
 
                                Replace(0, b.Length, value); 
264
 
                        }
265
 
                }
266
 
                
267
 
                /// <inheritdoc/>
268
 
                public int TextLength {
269
 
                        get { return b.Length; }
270
 
                }
271
 
                
272
 
                /// <inheritdoc/>
273
 
                public char GetCharAt(int offset)
274
 
                {
275
 
                        return b[offset];
276
 
                }
277
 
                
278
 
                /// <inheritdoc/>
279
 
                public string GetText(int offset, int length)
280
 
                {
281
 
                        return b.ToString(offset, length);
282
 
                }
283
 
                
284
 
                /// <inheritdoc/>
285
 
                public string GetText(ISegment segment)
286
 
                {
287
 
                        return b.ToString(segment.Offset, segment.Length);
288
 
                }
289
 
                
290
 
                /// <inheritdoc/>
291
 
                public int IndexOf(char c, int startIndex, int count)
292
 
                {
293
 
                        return this.Text.IndexOf(c, startIndex, count);
294
 
                }
295
 
                
296
 
                /// <inheritdoc/>
297
 
                public int IndexOfAny(char[] anyOf, int startIndex, int count)
298
 
                {
299
 
                        return this.Text.IndexOfAny(anyOf, startIndex, count);
300
 
                }
301
 
                
302
 
                /// <inheritdoc/>
303
 
                public int IndexOf(string searchText, int startIndex, int count, StringComparison comparisonType)
304
 
                {
305
 
                        return this.Text.IndexOf(searchText, startIndex, count, comparisonType);
306
 
                }
307
 
                
308
 
                /// <inheritdoc/>
309
 
                public int LastIndexOf(char c, int startIndex, int count)
310
 
                {
311
 
                        return this.Text.LastIndexOf(c, startIndex + count - 1, count);
312
 
                }
313
 
                
314
 
                /// <inheritdoc/>
315
 
                public int LastIndexOf(string searchText, int startIndex, int count, StringComparison comparisonType)
316
 
                {
317
 
                        return this.Text.LastIndexOf(searchText, startIndex + count - 1, count, comparisonType);
318
 
                }
319
 
                #endregion
320
 
                
321
 
                #region CreateAnchor
322
 
                readonly List<WeakReference> anchors = new List<WeakReference>();
323
 
                
324
 
                /// <inheritdoc/>
325
 
                public ITextAnchor CreateAnchor(int offset)
326
 
                {
327
 
                        var newAnchor = new SimpleAnchor(this, offset);
328
 
                        for (int i = 0; i < anchors.Count; i++) {
329
 
                                if (!anchors[i].IsAlive)
330
 
                                        anchors[i] = new WeakReference(newAnchor);
331
 
                        }
332
 
                        anchors.Add(new WeakReference(newAnchor));
333
 
                        return newAnchor;
334
 
                }
335
 
                
336
 
                void UpdateAnchors(TextChangeEventArgs change)
337
 
                {
338
 
                        // First update all anchors, then fire the deleted events.
339
 
                        List<int> deletedAnchors = new List<int>();
340
 
                        for (int i = 0; i < anchors.Count; i++) {
341
 
                                var anchor = anchors[i].Target as SimpleAnchor;
342
 
                                if (anchor != null) {
343
 
                                        anchor.Update(change);
344
 
                                        if (anchor.IsDeleted)
345
 
                                                deletedAnchors.Add(i);
346
 
                                }
347
 
                        }
348
 
                        deletedAnchors.Reverse();
349
 
                        foreach (var index in deletedAnchors) {
350
 
                                var anchor = anchors[index].Target as SimpleAnchor;
351
 
                                if (anchor != null)
352
 
                                        anchor.RaiseDeletedEvent();
353
 
                                anchors.RemoveAt(index);
354
 
                        }
355
 
                }
356
 
                
357
 
                sealed class SimpleAnchor : ITextAnchor
358
 
                {
359
 
                        readonly StringBuilderDocument document;
360
 
                        int offset;
361
 
                        
362
 
                        public SimpleAnchor(StringBuilderDocument document, int offset)
363
 
                        {
364
 
                                this.document = document;
365
 
                                this.offset = offset;
366
 
                        }
367
 
                        
368
 
                        public event EventHandler Deleted;
369
 
                        
370
 
                        public TextLocation Location {
371
 
                                get {
372
 
                                        if (IsDeleted)
373
 
                                                throw new InvalidOperationException();
374
 
                                        return document.GetLocation(offset);
375
 
                                }
376
 
                        }
377
 
                        
378
 
                        public int Offset {
379
 
                                get {
380
 
                                        if (IsDeleted)
381
 
                                                throw new InvalidOperationException();
382
 
                                        return offset;
383
 
                                }
384
 
                        }
385
 
                        
386
 
                        public AnchorMovementType MovementType { get; set; }
387
 
                        
388
 
                        public bool SurviveDeletion { get; set; }
389
 
                        
390
 
                        public bool IsDeleted {
391
 
                                get { return offset < 0; }
392
 
                        }
393
 
                        
394
 
                        public void Update(TextChangeEventArgs change)
395
 
                        {
396
 
                                if (SurviveDeletion || offset <= change.Offset || offset >= change.Offset + change.RemovalLength) {
397
 
                                        offset = change.GetNewOffset(offset, MovementType);
398
 
                                } else {
399
 
                                        offset = -1;
400
 
                                }
401
 
                        }
402
 
                        
403
 
                        public void RaiseDeletedEvent()
404
 
                        {
405
 
                                if (Deleted != null)
406
 
                                        Deleted(this, EventArgs.Empty);
407
 
                        }
408
 
                        
409
 
                        public int Line {
410
 
                                get { return this.Location.Line; }
411
 
                        }
412
 
                        
413
 
                        public int Column {
414
 
                                get { return this.Location.Column; }
415
 
                        }
416
 
                }
417
 
                #endregion
418
 
                
419
 
                /// <inheritdoc/>
420
 
                public virtual object GetService(Type serviceType)
421
 
                {
422
 
                        return null;
423
 
                }
424
 
        }
425
 
}