/NRefactory/ICSharpCode.NRefactory/TypeSystem/DomRegion.cs

http://github.com/icsharpcode/ILSpy · C# · 230 lines · 166 code · 31 blank · 33 comment · 26 complexity · c002877feeee5d93094316114e21ef8b MD5 · raw file

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