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)
5
using System.Collections.Generic;
6
using ICSharpCode.AvalonEdit.Document;
8
namespace ICSharpCode.AvalonEdit.Editing
11
/// Implementation for <see cref="IReadOnlySectionProvider"/> that stores the segments
12
/// in a <see cref="TextSegmentCollection{T}"/>.
14
public class TextSegmentReadOnlySectionProvider<T> : IReadOnlySectionProvider where T : TextSegment
16
readonly TextSegmentCollection<T> segments;
19
/// Gets the collection storing the read-only segments.
21
public TextSegmentCollection<T> Segments {
22
get { return segments; }
26
/// Creates a new TextSegmentReadOnlySectionProvider instance for the specified document.
28
public TextSegmentReadOnlySectionProvider(TextDocument textDocument)
30
segments = new TextSegmentCollection<T>(textDocument);
34
/// Creates a new TextSegmentReadOnlySectionProvider instance using the specified TextSegmentCollection.
36
public TextSegmentReadOnlySectionProvider(TextSegmentCollection<T> segments)
39
throw new ArgumentNullException("segments");
40
this.segments = segments;
44
/// Gets whether insertion is possible at the specified offset.
46
public virtual bool CanInsert(int offset)
48
foreach (TextSegment segment in segments.FindSegmentsContaining(offset)) {
49
if (segment.StartOffset < offset && offset < segment.EndOffset)
56
/// Gets the deletable segments inside the given segment.
58
public virtual IEnumerable<ISegment> GetDeletableSegments(ISegment segment)
61
throw new ArgumentNullException("segment");
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);
70
if (end > readonlyUntil) {
74
int endOffset = segment.EndOffset;
75
if (readonlyUntil < endOffset) {
76
yield return new SimpleSegment(readonlyUntil, endOffset - readonlyUntil);