TextRange.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (C) 2013 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @constructor
  32. * @param {number} startLine
  33. * @param {number} startColumn
  34. * @param {number} endLine
  35. * @param {number} endColumn
  36. */
  37. WebInspector.TextRange = function(startLine, startColumn, endLine, endColumn)
  38. {
  39. this.startLine = startLine;
  40. this.startColumn = startColumn;
  41. this.endLine = endLine;
  42. this.endColumn = endColumn;
  43. }
  44. WebInspector.TextRange.createFromLocation = function(line, column)
  45. {
  46. return new WebInspector.TextRange(line, column, line, column);
  47. }
  48. /**
  49. * @param {Object} serializedTextRange
  50. * @return {WebInspector.TextRange}
  51. */
  52. WebInspector.TextRange.fromObject = function (serializedTextRange)
  53. {
  54. return new WebInspector.TextRange(serializedTextRange.startLine, serializedTextRange.startColumn, serializedTextRange.endLine, serializedTextRange.endColumn);
  55. }
  56. WebInspector.TextRange.prototype = {
  57. /**
  58. * @return {boolean}
  59. */
  60. isEmpty: function()
  61. {
  62. return this.startLine === this.endLine && this.startColumn === this.endColumn;
  63. },
  64. /**
  65. * @param {WebInspector.TextRange} range
  66. * @return {boolean}
  67. */
  68. immediatelyPrecedes: function(range)
  69. {
  70. if (!range)
  71. return false;
  72. return this.endLine === range.startLine && this.endColumn === range.startColumn;
  73. },
  74. /**
  75. * @param {WebInspector.TextRange} range
  76. * @return {boolean}
  77. */
  78. immediatelyFollows: function(range)
  79. {
  80. if (!range)
  81. return false;
  82. return range.immediatelyPrecedes(this);
  83. },
  84. /**
  85. * @return {number}
  86. */
  87. get linesCount()
  88. {
  89. return this.endLine - this.startLine;
  90. },
  91. collapseToEnd: function()
  92. {
  93. return new WebInspector.TextRange(this.endLine, this.endColumn, this.endLine, this.endColumn);
  94. },
  95. /**
  96. * @return {WebInspector.TextRange}
  97. */
  98. normalize: function()
  99. {
  100. if (this.startLine > this.endLine || (this.startLine === this.endLine && this.startColumn > this.endColumn))
  101. return new WebInspector.TextRange(this.endLine, this.endColumn, this.startLine, this.startColumn);
  102. else
  103. return this.clone();
  104. },
  105. /**
  106. * @return {WebInspector.TextRange}
  107. */
  108. clone: function()
  109. {
  110. return new WebInspector.TextRange(this.startLine, this.startColumn, this.endLine, this.endColumn);
  111. },
  112. /**
  113. * @return {Object}
  114. */
  115. serializeToObject: function()
  116. {
  117. var serializedTextRange = {};
  118. serializedTextRange.startLine = this.startLine;
  119. serializedTextRange.startColumn = this.startColumn;
  120. serializedTextRange.endLine = this.endLine;
  121. serializedTextRange.endColumn = this.endColumn;
  122. return serializedTextRange;
  123. },
  124. /**
  125. * @param {WebInspector.TextRange} other
  126. * @return {number}
  127. */
  128. compareTo: function(other)
  129. {
  130. if (this.startLine > other.startLine)
  131. return 1;
  132. if (this.startLine < other.startLine)
  133. return -1;
  134. if (this.startColumn > other.startColumn)
  135. return 1;
  136. if (this.startColumn < other.startColumn)
  137. return -1;
  138. return 0;
  139. },
  140. /**
  141. * @param {number} lineOffset
  142. * @return {WebInspector.TextRange}
  143. */
  144. shift: function(lineOffset)
  145. {
  146. return new WebInspector.TextRange(this.startLine + lineOffset, this.startColumn, this.endLine + lineOffset, this.endColumn);
  147. },
  148. toString: function()
  149. {
  150. return JSON.stringify(this);
  151. }
  152. }