Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Editing/TextSegmentReadOnlySectionProvider.cs @ 17328

Last change on this file since 17328 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 3.6 KB
Line 
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
19using System;
20using System.Collections.Generic;
21using ICSharpCode.AvalonEdit.Document;
22#if NREFACTORY
23using ICSharpCode.NRefactory.Editor;
24#endif
25
26namespace ICSharpCode.AvalonEdit.Editing
27{
28  /// <summary>
29  /// Implementation for <see cref="IReadOnlySectionProvider"/> that stores the segments
30  /// in a <see cref="TextSegmentCollection{T}"/>.
31  /// </summary>
32  public class TextSegmentReadOnlySectionProvider<T> : IReadOnlySectionProvider where T : TextSegment
33  {
34    readonly TextSegmentCollection<T> segments;
35   
36    /// <summary>
37    /// Gets the collection storing the read-only segments.
38    /// </summary>
39    public TextSegmentCollection<T> Segments {
40      get { return segments; }
41    }
42   
43    /// <summary>
44    /// Creates a new TextSegmentReadOnlySectionProvider instance for the specified document.
45    /// </summary>
46    public TextSegmentReadOnlySectionProvider(TextDocument textDocument)
47    {
48      segments = new TextSegmentCollection<T>(textDocument);
49    }
50   
51    /// <summary>
52    /// Creates a new TextSegmentReadOnlySectionProvider instance using the specified TextSegmentCollection.
53    /// </summary>
54    public TextSegmentReadOnlySectionProvider(TextSegmentCollection<T> segments)
55    {
56      if (segments == null)
57        throw new ArgumentNullException("segments");
58      this.segments = segments;
59    }
60   
61    /// <summary>
62    /// Gets whether insertion is possible at the specified offset.
63    /// </summary>
64    public virtual bool CanInsert(int offset)
65    {
66      foreach (TextSegment segment in segments.FindSegmentsContaining(offset)) {
67        if (segment.StartOffset < offset && offset < segment.EndOffset)
68          return false;
69      }
70      return true;
71    }
72   
73    /// <summary>
74    /// Gets the deletable segments inside the given segment.
75    /// </summary>
76    public virtual IEnumerable<ISegment> GetDeletableSegments(ISegment segment)
77    {
78      if (segment == null)
79        throw new ArgumentNullException("segment");
80     
81      if (segment.Length == 0 && CanInsert(segment.Offset)) {
82        yield return segment;
83        yield break;
84      }
85     
86      int readonlyUntil = segment.Offset;
87      foreach (TextSegment ts in segments.FindOverlappingSegments(segment)) {
88        int start = ts.StartOffset;
89        int end = start + ts.Length;
90        if (start > readonlyUntil) {
91          yield return new SimpleSegment(readonlyUntil, start - readonlyUntil);
92        }
93        if (end > readonlyUntil) {
94          readonlyUntil = end;
95        }
96      }
97      int endOffset = segment.EndOffset;
98      if (readonlyUntil < endOffset) {
99        yield return new SimpleSegment(readonlyUntil, endOffset - readonlyUntil);
100      }
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.