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 | |
---|
19 | using System; |
---|
20 | using System.Globalization; |
---|
21 | |
---|
22 | namespace ICSharpCode.NRefactory.TypeSystem |
---|
23 | { |
---|
24 | [Serializable] |
---|
25 | public struct DomRegion : IEquatable<DomRegion> |
---|
26 | { |
---|
27 | readonly string fileName; |
---|
28 | readonly int beginLine; |
---|
29 | readonly int endLine; |
---|
30 | readonly int beginColumn; |
---|
31 | readonly int endColumn; |
---|
32 | |
---|
33 | public readonly static DomRegion Empty = new DomRegion(); |
---|
34 | |
---|
35 | public bool IsEmpty { |
---|
36 | get { |
---|
37 | return BeginLine <= 0; |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | public string FileName { |
---|
42 | get { return fileName; } |
---|
43 | } |
---|
44 | |
---|
45 | public int BeginLine { |
---|
46 | get { |
---|
47 | return beginLine; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | /// <value> |
---|
52 | /// if the end line is == -1 the end column is -1 too |
---|
53 | /// this stands for an unknwon end |
---|
54 | /// </value> |
---|
55 | public int EndLine { |
---|
56 | get { |
---|
57 | return endLine; |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | public int BeginColumn { |
---|
62 | get { |
---|
63 | return beginColumn; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | /// <value> |
---|
68 | /// if the end column is == -1 the end line is -1 too |
---|
69 | /// this stands for an unknown end |
---|
70 | /// </value> |
---|
71 | public int EndColumn { |
---|
72 | get { |
---|
73 | return endColumn; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | public TextLocation Begin { |
---|
78 | get { |
---|
79 | return new TextLocation (beginLine, beginColumn); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | public TextLocation End { |
---|
84 | get { |
---|
85 | return new TextLocation (endLine, endColumn); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | public DomRegion (int beginLine, int beginColumn, int endLine, int endColumn) : this (null, beginLine, beginColumn, endLine, endColumn) |
---|
90 | { |
---|
91 | } |
---|
92 | |
---|
93 | public DomRegion(string fileName, int beginLine, int beginColumn, int endLine, int endColumn) |
---|
94 | { |
---|
95 | this.fileName = fileName; |
---|
96 | this.beginLine = beginLine; |
---|
97 | this.beginColumn = beginColumn; |
---|
98 | this.endLine = endLine; |
---|
99 | this.endColumn = endColumn; |
---|
100 | } |
---|
101 | |
---|
102 | public DomRegion (int beginLine, int beginColumn) : this (null, beginLine, beginColumn) |
---|
103 | { |
---|
104 | } |
---|
105 | |
---|
106 | public DomRegion (string fileName, int beginLine, int beginColumn) |
---|
107 | { |
---|
108 | this.fileName = fileName; |
---|
109 | this.beginLine = beginLine; |
---|
110 | this.beginColumn = beginColumn; |
---|
111 | this.endLine = -1; |
---|
112 | this.endColumn = -1; |
---|
113 | } |
---|
114 | |
---|
115 | public DomRegion (TextLocation begin, TextLocation end) : this (null, begin, end) |
---|
116 | { |
---|
117 | } |
---|
118 | |
---|
119 | public DomRegion (string fileName, TextLocation begin, TextLocation end) |
---|
120 | { |
---|
121 | this.fileName = fileName; |
---|
122 | this.beginLine = begin.Line; |
---|
123 | this.beginColumn = begin.Column; |
---|
124 | this.endLine = end.Line; |
---|
125 | this.endColumn = end.Column; |
---|
126 | } |
---|
127 | |
---|
128 | public DomRegion (TextLocation begin) : this (null, begin) |
---|
129 | { |
---|
130 | } |
---|
131 | |
---|
132 | public DomRegion (string fileName, TextLocation begin) |
---|
133 | { |
---|
134 | this.fileName = fileName; |
---|
135 | this.beginLine = begin.Line; |
---|
136 | this.beginColumn = begin.Column; |
---|
137 | this.endLine = -1; |
---|
138 | this.endColumn = -1; |
---|
139 | } |
---|
140 | |
---|
141 | /// <remarks> |
---|
142 | /// Returns true, if the given coordinates (line, column) are in the region. |
---|
143 | /// This method assumes that for an unknown end the end line is == -1 |
---|
144 | /// </remarks> |
---|
145 | public bool IsInside(int line, int column) |
---|
146 | { |
---|
147 | if (IsEmpty) |
---|
148 | return false; |
---|
149 | return line >= BeginLine && |
---|
150 | (line <= EndLine || EndLine == -1) && |
---|
151 | (line != BeginLine || column >= BeginColumn) && |
---|
152 | (line != EndLine || column <= EndColumn); |
---|
153 | } |
---|
154 | |
---|
155 | public bool IsInside(TextLocation location) |
---|
156 | { |
---|
157 | return IsInside(location.Line, location.Column); |
---|
158 | } |
---|
159 | |
---|
160 | /// <remarks> |
---|
161 | /// Returns true, if the given coordinates (line, column) are in the region. |
---|
162 | /// This method assumes that for an unknown end the end line is == -1 |
---|
163 | /// </remarks> |
---|
164 | public bool Contains(int line, int column) |
---|
165 | { |
---|
166 | if (IsEmpty) |
---|
167 | return false; |
---|
168 | return line >= BeginLine && |
---|
169 | (line <= EndLine || EndLine == -1) && |
---|
170 | (line != BeginLine || column >= BeginColumn) && |
---|
171 | (line != EndLine || column < EndColumn); |
---|
172 | } |
---|
173 | |
---|
174 | public bool Contains(TextLocation location) |
---|
175 | { |
---|
176 | return Contains(location.Line, location.Column); |
---|
177 | } |
---|
178 | |
---|
179 | public bool IntersectsWith (DomRegion region) |
---|
180 | { |
---|
181 | return region.Begin <= End && region.End >= Begin; |
---|
182 | } |
---|
183 | |
---|
184 | public bool OverlapsWith (DomRegion region) |
---|
185 | { |
---|
186 | var maxBegin = Begin > region.Begin ? Begin : region.Begin; |
---|
187 | var minEnd = End < region.End ? End : region.End; |
---|
188 | return maxBegin < minEnd; |
---|
189 | } |
---|
190 | |
---|
191 | public override string ToString() |
---|
192 | { |
---|
193 | return string.Format( |
---|
194 | CultureInfo.InvariantCulture, |
---|
195 | "[DomRegion FileName={0}, Begin=({1}, {2}), End=({3}, {4})]", |
---|
196 | fileName, beginLine, beginColumn, endLine, endColumn); |
---|
197 | } |
---|
198 | |
---|
199 | public override bool Equals(object obj) |
---|
200 | { |
---|
201 | return obj is DomRegion && Equals((DomRegion)obj); |
---|
202 | } |
---|
203 | |
---|
204 | public override int GetHashCode() |
---|
205 | { |
---|
206 | unchecked { |
---|
207 | int hashCode = fileName != null ? fileName.GetHashCode() : 0; |
---|
208 | hashCode ^= beginColumn + 1100009 * beginLine + 1200007 * endLine + 1300021 * endColumn; |
---|
209 | return hashCode; |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | public bool Equals(DomRegion other) |
---|
214 | { |
---|
215 | return beginLine == other.beginLine && beginColumn == other.beginColumn |
---|
216 | && endLine == other.endLine && endColumn == other.endColumn |
---|
217 | && fileName == other.fileName; |
---|
218 | } |
---|
219 | |
---|
220 | public static bool operator ==(DomRegion left, DomRegion right) |
---|
221 | { |
---|
222 | return left.Equals(right); |
---|
223 | } |
---|
224 | |
---|
225 | public static bool operator !=(DomRegion left, DomRegion right) |
---|
226 | { |
---|
227 | return !left.Equals(right); |
---|
228 | } |
---|
229 | } |
---|
230 | } |
---|