~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.NRefactory/TextLocation.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright (c) 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
 
 
19
 
using System;
20
 
using System.Globalization;
21
 
 
22
 
namespace ICSharpCode.NRefactory
23
 
{
24
 
        /// <summary>
25
 
        /// A line/column position.
26
 
        /// Text editor lines/columns are counted started from one.
27
 
        /// </summary>
28
 
        /// <remarks>
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.
31
 
        /// </remarks>
32
 
        [Serializable]
33
 
        public struct TextLocation : IComparable<TextLocation>, IEquatable<TextLocation>
34
 
        {
35
 
                /// <summary>
36
 
                /// Represents no text location (0, 0).
37
 
                /// </summary>
38
 
                public static readonly TextLocation Empty = new TextLocation(0, 0);
39
 
                
40
 
                /// <summary>
41
 
                /// Constant of the minimum line.
42
 
                /// </summary>
43
 
                public const int MinLine   = 1;
44
 
                
45
 
                /// <summary>
46
 
                /// Constant of the minimum column.
47
 
                /// </summary>
48
 
                public const int MinColumn = 1;
49
 
                
50
 
                /// <summary>
51
 
                /// Creates a TextLocation instance.
52
 
                /// </summary>
53
 
                public TextLocation(int line, int column)
54
 
                {
55
 
                        this.line = line;
56
 
                        this.column = column;
57
 
                }
58
 
                
59
 
                int column, line;
60
 
                
61
 
                /// <summary>
62
 
                /// Gets the line number.
63
 
                /// </summary>
64
 
                public int Line {
65
 
                        get { return line; }
66
 
                }
67
 
                
68
 
                /// <summary>
69
 
                /// Gets the column number.
70
 
                /// </summary>
71
 
                public int Column {
72
 
                        get { return column; }
73
 
                }
74
 
                
75
 
                /// <summary>
76
 
                /// Gets whether the TextLocation instance is empty.
77
 
                /// </summary>
78
 
                public bool IsEmpty {
79
 
                        get {
80
 
                                return column < MinLine && line < MinColumn;
81
 
                        }
82
 
                }
83
 
                
84
 
                /// <summary>
85
 
                /// Gets a string representation for debugging purposes.
86
 
                /// </summary>
87
 
                public override string ToString()
88
 
                {
89
 
                        return string.Format(CultureInfo.InvariantCulture, "(Line {1}, Col {0})", this.column, this.line);
90
 
                }
91
 
                
92
 
                /// <summary>
93
 
                /// Gets a hash code.
94
 
                /// </summary>
95
 
                public override int GetHashCode()
96
 
                {
97
 
                        return unchecked (191 * column.GetHashCode() ^ line.GetHashCode());
98
 
                }
99
 
                
100
 
                /// <summary>
101
 
                /// Equality test.
102
 
                /// </summary>
103
 
                public override bool Equals(object obj)
104
 
                {
105
 
                        if (!(obj is TextLocation)) return false;
106
 
                        return (TextLocation)obj == this;
107
 
                }
108
 
                
109
 
                /// <summary>
110
 
                /// Equality test.
111
 
                /// </summary>
112
 
                public bool Equals(TextLocation other)
113
 
                {
114
 
                        return this == other;
115
 
                }
116
 
                
117
 
                /// <summary>
118
 
                /// Equality test.
119
 
                /// </summary>
120
 
                public static bool operator ==(TextLocation left, TextLocation right)
121
 
                {
122
 
                        return left.column == right.column && left.line == right.line;
123
 
                }
124
 
                
125
 
                /// <summary>
126
 
                /// Inequality test.
127
 
                /// </summary>
128
 
                public static bool operator !=(TextLocation left, TextLocation right)
129
 
                {
130
 
                        return left.column != right.column || left.line != right.line;
131
 
                }
132
 
                
133
 
                /// <summary>
134
 
                /// Compares two text locations.
135
 
                /// </summary>
136
 
                public static bool operator <(TextLocation left, TextLocation right)
137
 
                {
138
 
                        if (left.line < right.line)
139
 
                                return true;
140
 
                        else if (left.line == right.line)
141
 
                                return left.column < right.column;
142
 
                        else
143
 
                                return false;
144
 
                }
145
 
                
146
 
                /// <summary>
147
 
                /// Compares two text locations.
148
 
                /// </summary>
149
 
                public static bool operator >(TextLocation left, TextLocation right)
150
 
                {
151
 
                        if (left.line > right.line)
152
 
                                return true;
153
 
                        else if (left.line == right.line)
154
 
                                return left.column > right.column;
155
 
                        else
156
 
                                return false;
157
 
                }
158
 
                
159
 
                /// <summary>
160
 
                /// Compares two text locations.
161
 
                /// </summary>
162
 
                public static bool operator <=(TextLocation left, TextLocation right)
163
 
                {
164
 
                        return !(left > right);
165
 
                }
166
 
                
167
 
                /// <summary>
168
 
                /// Compares two text locations.
169
 
                /// </summary>
170
 
                public static bool operator >=(TextLocation left, TextLocation right)
171
 
                {
172
 
                        return !(left < right);
173
 
                }
174
 
                
175
 
                /// <summary>
176
 
                /// Compares two text locations.
177
 
                /// </summary>
178
 
                public int CompareTo(TextLocation other)
179
 
                {
180
 
                        if (this == other)
181
 
                                return 0;
182
 
                        if (this < other)
183
 
                                return -1;
184
 
                        else
185
 
                                return 1;
186
 
                }
187
 
        }
188
 
}