Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory-5.5.0/TextLocation.cs

Last change on this file 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) 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.NRefactory
24{
25  /// <summary>
26  /// A line/column position.
27  /// Text editor lines/columns are counted started from one.
28  /// </summary>
29  /// <remarks>
30  /// The document provides the methods <see cref="Editor.IDocument.GetLocation"/> and
31  /// <see cref="Editor.IDocument.GetOffset(TextLocation)"/> to convert between offsets and TextLocations.
32  /// </remarks>
33  [Serializable]
34  [TypeConverter(typeof(TextLocationConverter))]
35  public struct TextLocation : IComparable<TextLocation>, IEquatable<TextLocation>
36  {
37    /// <summary>
38    /// Represents no text location (0, 0).
39    /// </summary>
40    public static readonly TextLocation Empty = new TextLocation(0, 0);
41   
42    /// <summary>
43    /// Constant of the minimum line.
44    /// </summary>
45    public const int MinLine   = 1;
46   
47    /// <summary>
48    /// Constant of the minimum column.
49    /// </summary>
50    public const int MinColumn = 1;
51   
52    /// <summary>
53    /// Creates a TextLocation instance.
54    /// </summary>
55    public TextLocation(int line, int column)
56    {
57      this.line = line;
58      this.column = column;
59    }
60   
61    int column, line;
62   
63    /// <summary>
64    /// Gets the line number.
65    /// </summary>
66    public int Line {
67      get { return line; }
68    }
69   
70    /// <summary>
71    /// Gets the column number.
72    /// </summary>
73    public int Column {
74      get { return column; }
75    }
76   
77    /// <summary>
78    /// Gets whether the TextLocation instance is empty.
79    /// </summary>
80    public bool IsEmpty {
81      get {
82        return column < MinLine && line < MinColumn;
83      }
84    }
85   
86    /// <summary>
87    /// Gets a string representation for debugging purposes.
88    /// </summary>
89    public override string ToString()
90    {
91      return string.Format(CultureInfo.InvariantCulture, "(Line {1}, Col {0})", this.column, this.line);
92    }
93   
94    /// <summary>
95    /// Gets a hash code.
96    /// </summary>
97    public override int GetHashCode()
98    {
99      return unchecked (191 * column.GetHashCode() ^ line.GetHashCode());
100    }
101   
102    /// <summary>
103    /// Equality test.
104    /// </summary>
105    public override bool Equals(object obj)
106    {
107      if (!(obj is TextLocation)) return false;
108      return (TextLocation)obj == this;
109    }
110   
111    /// <summary>
112    /// Equality test.
113    /// </summary>
114    public bool Equals(TextLocation other)
115    {
116      return this == other;
117    }
118   
119    /// <summary>
120    /// Equality test.
121    /// </summary>
122    public static bool operator ==(TextLocation left, TextLocation right)
123    {
124      return left.column == right.column && left.line == right.line;
125    }
126   
127    /// <summary>
128    /// Inequality test.
129    /// </summary>
130    public static bool operator !=(TextLocation left, TextLocation right)
131    {
132      return left.column != right.column || left.line != right.line;
133    }
134   
135    /// <summary>
136    /// Compares two text locations.
137    /// </summary>
138    public static bool operator <(TextLocation left, TextLocation right)
139    {
140      if (left.line < right.line)
141        return true;
142      else if (left.line == right.line)
143        return left.column < right.column;
144      else
145        return false;
146    }
147   
148    /// <summary>
149    /// Compares two text locations.
150    /// </summary>
151    public static bool operator >(TextLocation left, TextLocation right)
152    {
153      if (left.line > right.line)
154        return true;
155      else if (left.line == right.line)
156        return left.column > right.column;
157      else
158        return false;
159    }
160   
161    /// <summary>
162    /// Compares two text locations.
163    /// </summary>
164    public static bool operator <=(TextLocation left, TextLocation right)
165    {
166      return !(left > right);
167    }
168   
169    /// <summary>
170    /// Compares two text locations.
171    /// </summary>
172    public static bool operator >=(TextLocation left, TextLocation right)
173    {
174      return !(left < right);
175    }
176   
177    /// <summary>
178    /// Compares two text locations.
179    /// </summary>
180    public int CompareTo(TextLocation other)
181    {
182      if (this == other)
183        return 0;
184      if (this < other)
185        return -1;
186      else
187        return 1;
188    }
189  }
190 
191  public class TextLocationConverter : TypeConverter
192  {
193    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
194    {
195      return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
196    }
197   
198    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
199    {
200      return destinationType == typeof(TextLocation) || base.CanConvertTo(context, destinationType);
201    }
202   
203    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
204    {
205      if (value is string) {
206        string[] parts = ((string)value).Split(';', ',');
207        if (parts.Length == 2) {
208          return new TextLocation(int.Parse(parts[0]), int.Parse(parts[1]));
209        }
210      }
211      return base.ConvertFrom(context, culture, value);
212    }
213   
214    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
215    {
216      if (value is TextLocation) {
217        var loc = (TextLocation)value;
218        return loc.Line + ";" + loc.Column;
219      }
220      return base.ConvertTo(context, culture, value, destinationType);
221    }
222  }
223}
Note: See TracBrowser for help on using the repository browser.