1
ļ»æ// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
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:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
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.
20
using System.ComponentModel;
21
using System.Globalization;
23
namespace ICSharpCode.NRefactory
26
/// A line/column position.
27
/// Text editor lines/columns are counted started from one.
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.
34
[TypeConverter(typeof(TextLocationConverter))]
35
public struct TextLocation : IComparable<TextLocation>, IEquatable<TextLocation>
38
/// Represents no text location (0, 0).
40
public static readonly TextLocation Empty = new TextLocation(0, 0);
43
/// Constant of the minimum line.
45
public const int MinLine = 1;
48
/// Constant of the minimum column.
50
public const int MinColumn = 1;
53
/// Creates a TextLocation instance.
55
public TextLocation(int line, int column)
64
/// Gets the line number.
71
/// Gets the column number.
74
get { return column; }
78
/// Gets whether the TextLocation instance is empty.
82
return column < MinLine && line < MinColumn;
87
/// Gets a string representation for debugging purposes.
89
public override string ToString()
91
return string.Format(CultureInfo.InvariantCulture, "(Line {1}, Col {0})", this.column, this.line);
97
public override int GetHashCode()
99
return unchecked (191 * column.GetHashCode() ^ line.GetHashCode());
105
public override bool Equals(object obj)
107
if (!(obj is TextLocation)) return false;
108
return (TextLocation)obj == this;
114
public bool Equals(TextLocation other)
116
return this == other;
122
public static bool operator ==(TextLocation left, TextLocation right)
124
return left.column == right.column && left.line == right.line;
130
public static bool operator !=(TextLocation left, TextLocation right)
132
return left.column != right.column || left.line != right.line;
136
/// Compares two text locations.
138
public static bool operator <(TextLocation left, TextLocation right)
140
if (left.line < right.line)
142
else if (left.line == right.line)
143
return left.column < right.column;
149
/// Compares two text locations.
151
public static bool operator >(TextLocation left, TextLocation right)
153
if (left.line > right.line)
155
else if (left.line == right.line)
156
return left.column > right.column;
162
/// Compares two text locations.
164
public static bool operator <=(TextLocation left, TextLocation right)
166
return !(left > right);
170
/// Compares two text locations.
172
public static bool operator >=(TextLocation left, TextLocation right)
174
return !(left < right);
178
/// Compares two text locations.
180
public int CompareTo(TextLocation other)
191
public class TextLocationConverter : TypeConverter
193
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
195
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
198
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
200
return destinationType == typeof(TextLocation) || base.CanConvertTo(context, destinationType);
203
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
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]));
211
return base.ConvertFrom(context, culture, value);
214
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
216
if (value is TextLocation) {
217
var loc = (TextLocation)value;
218
return loc.Line + ";" + loc.Column;
220
return base.ConvertTo(context, culture, value, destinationType);