~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/TextSegmentReadOnlySectionProvider.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using ICSharpCode.AvalonEdit.Document;
 
7
 
 
8
namespace ICSharpCode.AvalonEdit.Editing
 
9
{
 
10
        /// <summary>
 
11
        /// Implementation for <see cref="IReadOnlySectionProvider"/> that stores the segments
 
12
        /// in a <see cref="TextSegmentCollection{T}"/>.
 
13
        /// </summary>
 
14
        public class TextSegmentReadOnlySectionProvider<T> : IReadOnlySectionProvider where T : TextSegment
 
15
        {
 
16
                readonly TextSegmentCollection<T> segments;
 
17
                
 
18
                /// <summary>
 
19
                /// Gets the collection storing the read-only segments.
 
20
                /// </summary>
 
21
                public TextSegmentCollection<T> Segments {
 
22
                        get { return segments; }
 
23
                }
 
24
                
 
25
                /// <summary>
 
26
                /// Creates a new TextSegmentReadOnlySectionProvider instance for the specified document.
 
27
                /// </summary>
 
28
                public TextSegmentReadOnlySectionProvider(TextDocument textDocument)
 
29
                {
 
30
                        segments = new TextSegmentCollection<T>(textDocument);
 
31
                }
 
32
                
 
33
                /// <summary>
 
34
                /// Creates a new TextSegmentReadOnlySectionProvider instance using the specified TextSegmentCollection.
 
35
                /// </summary>
 
36
                public TextSegmentReadOnlySectionProvider(TextSegmentCollection<T> segments)
 
37
                {
 
38
                        if (segments == null)
 
39
                                throw new ArgumentNullException("segments");
 
40
                        this.segments = segments;
 
41
                }
 
42
                
 
43
                /// <summary>
 
44
                /// Gets whether insertion is possible at the specified offset.
 
45
                /// </summary>
 
46
                public virtual bool CanInsert(int offset)
 
47
                {
 
48
                        foreach (TextSegment segment in segments.FindSegmentsContaining(offset)) {
 
49
                                if (segment.StartOffset < offset && offset < segment.EndOffset)
 
50
                                        return false;
 
51
                        }
 
52
                        return true;
 
53
                }
 
54
                
 
55
                /// <summary>
 
56
                /// Gets the deletable segments inside the given segment.
 
57
                /// </summary>
 
58
                public virtual IEnumerable<ISegment> GetDeletableSegments(ISegment segment)
 
59
                {
 
60
                        if (segment == null)
 
61
                                throw new ArgumentNullException("segment");
 
62
                        
 
63
                        int readonlyUntil = segment.Offset;
 
64
                        foreach (TextSegment ts in segments.FindOverlappingSegments(segment)) {
 
65
                                int start = ts.StartOffset;
 
66
                                int end = start + ts.Length;
 
67
                                if (start > readonlyUntil) {
 
68
                                        yield return new SimpleSegment(readonlyUntil, start - readonlyUntil);
 
69
                                }
 
70
                                if (end > readonlyUntil) {
 
71
                                        readonlyUntil = end;
 
72
                                }
 
73
                        }
 
74
                        int endOffset = segment.EndOffset;
 
75
                        if (readonlyUntil < endOffset) {
 
76
                                yield return new SimpleSegment(readonlyUntil, endOffset - readonlyUntil);
 
77
                        }
 
78
                }
 
79
        }
 
80
}