1
// Copyright (c) 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.Globalization;
22
namespace ICSharpCode.NRefactory
25
/// A line/column position.
26
/// Text editor lines/columns are counted started from one.
29
/// The document provides the methods <see cref="Editor.IDocument.GetLocation"/> and
30
/// <see cref="Editor.IDocument.GetOffset(TextLocation)"/> to convert between offsets and TextLocations.
33
public struct TextLocation : IComparable<TextLocation>, IEquatable<TextLocation>
36
/// Represents no text location (0, 0).
38
public static readonly TextLocation Empty = new TextLocation(0, 0);
41
/// Constant of the minimum line.
43
public const int MinLine = 1;
46
/// Constant of the minimum column.
48
public const int MinColumn = 1;
51
/// Creates a TextLocation instance.
53
public TextLocation(int line, int column)
62
/// Gets the line number.
69
/// Gets the column number.
72
get { return column; }
76
/// Gets whether the TextLocation instance is empty.
80
return column < MinLine && line < MinColumn;
85
/// Gets a string representation for debugging purposes.
87
public override string ToString()
89
return string.Format(CultureInfo.InvariantCulture, "(Line {1}, Col {0})", this.column, this.line);
95
public override int GetHashCode()
97
return unchecked (191 * column.GetHashCode() ^ line.GetHashCode());
103
public override bool Equals(object obj)
105
if (!(obj is TextLocation)) return false;
106
return (TextLocation)obj == this;
112
public bool Equals(TextLocation other)
114
return this == other;
120
public static bool operator ==(TextLocation left, TextLocation right)
122
return left.column == right.column && left.line == right.line;
128
public static bool operator !=(TextLocation left, TextLocation right)
130
return left.column != right.column || left.line != right.line;
134
/// Compares two text locations.
136
public static bool operator <(TextLocation left, TextLocation right)
138
if (left.line < right.line)
140
else if (left.line == right.line)
141
return left.column < right.column;
147
/// Compares two text locations.
149
public static bool operator >(TextLocation left, TextLocation right)
151
if (left.line > right.line)
153
else if (left.line == right.line)
154
return left.column > right.column;
160
/// Compares two text locations.
162
public static bool operator <=(TextLocation left, TextLocation right)
164
return !(left > right);
168
/// Compares two text locations.
170
public static bool operator >=(TextLocation left, TextLocation right)
172
return !(left < right);
176
/// Compares two text locations.
178
public int CompareTo(TextLocation other)