Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Document/TextLocation.cs @ 17507

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

#2077: created branch and added first version

File size: 7.6 KB
Line 
1// Copyright (c) 2010-2013 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.ComponentModel;
21using System.Globalization;
22
23namespace ICSharpCode.AvalonEdit.Document
24{
25  #if !NREFACTORY
26  /// <summary>
27  /// A line/column position.
28  /// Text editor lines/columns are counted started from one.
29  /// </summary>
30  /// <remarks>
31  /// The document provides the methods <see cref="IDocument.GetLocation"/> and
32  /// <see cref="IDocument.GetOffset(TextLocation)"/> to convert between offsets and TextLocations.
33  /// </remarks>
34  [Serializable]
35  [TypeConverter(typeof(TextLocationConverter))]
36  public struct TextLocation : IComparable<TextLocation>, IEquatable<TextLocation>
37  {
38    /// <summary>
39    /// Represents no text location (0, 0).
40    /// </summary>
41    public static readonly TextLocation Empty = new TextLocation(0, 0);
42   
43    /// <summary>
44    /// Creates a TextLocation instance.
45    /// </summary>
46    public TextLocation(int line, int column)
47    {
48      this.line = line;
49      this.column = column;
50    }
51   
52    readonly int column, line;
53   
54    /// <summary>
55    /// Gets the line number.
56    /// </summary>
57    public int Line {
58      get { return line; }
59    }
60   
61    /// <summary>
62    /// Gets the column number.
63    /// </summary>
64    public int Column {
65      get { return column; }
66    }
67   
68    /// <summary>
69    /// Gets whether the TextLocation instance is empty.
70    /// </summary>
71    public bool IsEmpty {
72      get {
73        return column <= 0 && line <= 0;
74      }
75    }
76   
77    /// <summary>
78    /// Gets a string representation for debugging purposes.
79    /// </summary>
80    public override string ToString()
81    {
82      return string.Format(CultureInfo.InvariantCulture, "(Line {1}, Col {0})", this.column, this.line);
83    }
84   
85    /// <summary>
86    /// Gets a hash code.
87    /// </summary>
88    public override int GetHashCode()
89    {
90      return unchecked (191 * column.GetHashCode() ^ line.GetHashCode());
91    }
92   
93    /// <summary>
94    /// Equality test.
95    /// </summary>
96    public override bool Equals(object obj)
97    {
98      if (!(obj is TextLocation)) return false;
99      return (TextLocation)obj == this;
100    }
101   
102    /// <summary>
103    /// Equality test.
104    /// </summary>
105    public bool Equals(TextLocation other)
106    {
107      return this == other;
108    }
109   
110    /// <summary>
111    /// Equality test.
112    /// </summary>
113    public static bool operator ==(TextLocation left, TextLocation right)
114    {
115      return left.column == right.column && left.line == right.line;
116    }
117   
118    /// <summary>
119    /// Inequality test.
120    /// </summary>
121    public static bool operator !=(TextLocation left, TextLocation right)
122    {
123      return left.column != right.column || left.line != right.line;
124    }
125   
126    /// <summary>
127    /// Compares two text locations.
128    /// </summary>
129    public static bool operator <(TextLocation left, TextLocation right)
130    {
131      if (left.line < right.line)
132        return true;
133      else if (left.line == right.line)
134        return left.column < right.column;
135      else
136        return false;
137    }
138   
139    /// <summary>
140    /// Compares two text locations.
141    /// </summary>
142    public static bool operator >(TextLocation left, TextLocation right)
143    {
144      if (left.line > right.line)
145        return true;
146      else if (left.line == right.line)
147        return left.column > right.column;
148      else
149        return false;
150    }
151   
152    /// <summary>
153    /// Compares two text locations.
154    /// </summary>
155    public static bool operator <=(TextLocation left, TextLocation right)
156    {
157      return !(left > right);
158    }
159   
160    /// <summary>
161    /// Compares two text locations.
162    /// </summary>
163    public static bool operator >=(TextLocation left, TextLocation right)
164    {
165      return !(left < right);
166    }
167   
168    /// <summary>
169    /// Compares two text locations.
170    /// </summary>
171    public int CompareTo(TextLocation other)
172    {
173      if (this == other)
174        return 0;
175      if (this < other)
176        return -1;
177      else
178        return 1;
179    }
180  }
181 
182  /// <summary>
183  /// Converts strings of the form '0+[;,]0+' to a <see cref="TextLocation"/>.
184  /// </summary>
185  public class TextLocationConverter : TypeConverter
186  {
187    /// <inheritdoc/>
188    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
189    {
190      return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
191    }
192   
193    /// <inheritdoc/>
194    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
195    {
196      return destinationType == typeof(TextLocation) || base.CanConvertTo(context, destinationType);
197    }
198   
199    /// <inheritdoc/>
200    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
201    {
202      if (value is string) {
203        string[] parts = ((string)value).Split(';', ',');
204        if (parts.Length == 2) {
205          return new TextLocation(int.Parse(parts[0], culture), int.Parse(parts[1], culture));
206        }
207      }
208      return base.ConvertFrom(context, culture, value);
209    }
210   
211    /// <inheritdoc/>
212    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
213    {
214      if (value is TextLocation && destinationType == typeof(string)) {
215        var loc = (TextLocation)value;
216        return loc.Line.ToString(culture) + ";" + loc.Column.ToString(culture);
217      }
218      return base.ConvertTo(context, culture, value, destinationType);
219    }
220  }
221 
222  /// <summary>
223  /// An (Offset,Length)-pair.
224  /// </summary>
225  public interface ISegment
226  {
227    /// <summary>
228    /// Gets the start offset of the segment.
229    /// </summary>
230    int Offset { get; }
231   
232    /// <summary>
233    /// Gets the length of the segment.
234    /// </summary>
235    /// <remarks>For line segments (IDocumentLine), the length does not include the line delimeter.</remarks>
236    int Length { get; }
237   
238    /// <summary>
239    /// Gets the end offset of the segment.
240    /// </summary>
241    /// <remarks>EndOffset = Offset + Length;</remarks>
242    int EndOffset { get; }
243  }
244 
245  /// <summary>
246  /// Extension methods for <see cref="ISegment"/>.
247  /// </summary>
248  public static class ISegmentExtensions
249  {
250    /// <summary>
251    /// Gets whether <paramref name="segment"/> fully contains the specified segment.
252    /// </summary>
253    /// <remarks>
254    /// Use <c>segment.Contains(offset, 0)</c> to detect whether a segment (end inclusive) contains offset;
255    /// use <c>segment.Contains(offset, 1)</c> to detect whether a segment (end exclusive) contains offset.
256    /// </remarks>
257    public static bool Contains (this ISegment segment, int offset, int length)
258    {
259      return segment.Offset <= offset && offset + length <= segment.EndOffset;
260    }
261   
262    /// <summary>
263    /// Gets whether <paramref name="thisSegment"/> fully contains the specified segment.
264    /// </summary>
265    public static bool Contains (this ISegment thisSegment, ISegment segment)
266    {
267      return segment != null && thisSegment.Offset <= segment.Offset && segment.EndOffset <= thisSegment.EndOffset;
268    }
269  }
270  #endif
271}
Note: See TracBrowser for help on using the repository browser.