Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Document/SimpleSegment.cs @ 17399

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

#2077: created branch and added first version

File size: 6.0 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.Diagnostics;
21using System.Globalization;
22using ICSharpCode.AvalonEdit.Utils;
23#if NREFACTORY
24using ICSharpCode.NRefactory.Editor;
25#endif
26
27namespace ICSharpCode.AvalonEdit.Document
28{
29  /// <summary>
30  /// Represents a simple segment (Offset,Length pair) that is not automatically updated
31  /// on document changes.
32  /// </summary>
33  struct SimpleSegment : IEquatable<SimpleSegment>, ISegment
34  {
35    public static readonly SimpleSegment Invalid = new SimpleSegment(-1, -1);
36   
37    /// <summary>
38    /// Gets the overlapping portion of the segments.
39    /// Returns SimpleSegment.Invalid if the segments don't overlap.
40    /// </summary>
41    public static SimpleSegment GetOverlap(ISegment segment1, ISegment segment2)
42    {
43      int start = Math.Max(segment1.Offset, segment2.Offset);
44      int end = Math.Min(segment1.EndOffset, segment2.EndOffset);
45      if (end < start)
46        return SimpleSegment.Invalid;
47      else
48        return new SimpleSegment(start, end - start);
49    }
50   
51    public readonly int Offset, Length;
52   
53    int ISegment.Offset {
54      get { return Offset; }
55    }
56   
57    int ISegment.Length {
58      get { return Length; }
59    }
60   
61    public int EndOffset {
62      get {
63        return Offset + Length;
64      }
65    }
66   
67    public SimpleSegment(int offset, int length)
68    {
69      this.Offset = offset;
70      this.Length = length;
71    }
72   
73    public SimpleSegment(ISegment segment)
74    {
75      Debug.Assert(segment != null);
76      this.Offset = segment.Offset;
77      this.Length = segment.Length;
78    }
79   
80    public override int GetHashCode()
81    {
82      unchecked {
83        return Offset + 10301 * Length;
84      }
85    }
86   
87    public override bool Equals(object obj)
88    {
89      return (obj is SimpleSegment) && Equals((SimpleSegment)obj);
90    }
91   
92    public bool Equals(SimpleSegment other)
93    {
94      return this.Offset == other.Offset && this.Length == other.Length;
95    }
96   
97    public static bool operator ==(SimpleSegment left, SimpleSegment right)
98    {
99      return left.Equals(right);
100    }
101   
102    public static bool operator !=(SimpleSegment left, SimpleSegment right)
103    {
104      return !left.Equals(right);
105    }
106   
107    /// <inheritdoc/>
108    public override string ToString()
109    {
110      return "[Offset=" + Offset.ToString(CultureInfo.InvariantCulture) + ", Length=" + Length.ToString(CultureInfo.InvariantCulture) + "]";
111    }
112  }
113 
114  /// <summary>
115  /// A segment using <see cref="TextAnchor"/>s as start and end positions.
116  /// </summary>
117  /// <remarks>
118  /// <para>
119  /// For the constructors creating new anchors, the start position will be AfterInsertion and the end position will be BeforeInsertion.
120  /// Should the end position move before the start position, the segment will have length 0.
121  /// </para>
122  /// </remarks>
123  /// <seealso cref="ISegment"/>
124  /// <seealso cref="TextSegment"/>
125  public sealed class AnchorSegment : ISegment
126  {
127    readonly TextAnchor start, end;
128   
129    /// <inheritdoc/>
130    public int Offset {
131      get { return start.Offset; }
132    }
133   
134    /// <inheritdoc/>
135    public int Length {
136      get {
137        // Math.Max takes care of the fact that end.Offset might move before start.Offset.
138        return Math.Max(0, end.Offset - start.Offset);
139      }
140    }
141   
142    /// <inheritdoc/>
143    public int EndOffset {
144      get {
145        // Math.Max takes care of the fact that end.Offset might move before start.Offset.
146        return Math.Max(start.Offset, end.Offset);
147      }
148    }
149   
150    /// <summary>
151    /// Creates a new AnchorSegment using the specified anchors.
152    /// The anchors must have <see cref="TextAnchor.SurviveDeletion"/> set to true.
153    /// </summary>
154    public AnchorSegment(TextAnchor start, TextAnchor end)
155    {
156      if (start == null)
157        throw new ArgumentNullException("start");
158      if (end == null)
159        throw new ArgumentNullException("end");
160      if (!start.SurviveDeletion)
161        throw new ArgumentException("Anchors for AnchorSegment must use SurviveDeletion", "start");
162      if (!end.SurviveDeletion)
163        throw new ArgumentException("Anchors for AnchorSegment must use SurviveDeletion", "end");
164      this.start = start;
165      this.end = end;
166    }
167   
168    /// <summary>
169    /// Creates a new AnchorSegment that creates new anchors.
170    /// </summary>
171    public AnchorSegment(TextDocument document, ISegment segment)
172      : this(document, ThrowUtil.CheckNotNull(segment, "segment").Offset, segment.Length)
173    {
174    }
175   
176    /// <summary>
177    /// Creates a new AnchorSegment that creates new anchors.
178    /// </summary>
179    public AnchorSegment(TextDocument document, int offset, int length)
180    {
181      if (document == null)
182        throw new ArgumentNullException("document");
183      this.start = document.CreateAnchor(offset);
184      this.start.SurviveDeletion = true;
185      this.start.MovementType = AnchorMovementType.AfterInsertion;
186      this.end = document.CreateAnchor(offset + length);
187      this.end.SurviveDeletion = true;
188      this.end.MovementType = AnchorMovementType.BeforeInsertion;
189    }
190   
191    /// <inheritdoc/>
192    public override string ToString()
193    {
194      return "[Offset=" + Offset.ToString(CultureInfo.InvariantCulture) + ", EndOffset=" + EndOffset.ToString(CultureInfo.InvariantCulture) + "]";
195    }
196  }
197}
Note: See TracBrowser for help on using the repository browser.