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/** 32 * @constructor 33 * @param {number} startLine 34 * @param {number} startColumn 35 * @param {number} endLine 36 * @param {number} endColumn 37 */ 38WebInspector.TextRange = function(startLine, startColumn, endLine, endColumn) 39{ 40 this.startLine = startLine; 41 this.startColumn = startColumn; 42 this.endLine = endLine; 43 this.endColumn = endColumn; 44} 45 46WebInspector.TextRange.createFromLocation = function(line, column) 47{ 48 return new WebInspector.TextRange(line, column, line, column); 49} 50 51/** 52 * @param {!Object} serializedTextRange 53 * @return {!WebInspector.TextRange} 54 */ 55WebInspector.TextRange.fromObject = function(serializedTextRange) 56{ 57 return new WebInspector.TextRange(serializedTextRange.startLine, serializedTextRange.startColumn, serializedTextRange.endLine, serializedTextRange.endColumn); 58} 59 60WebInspector.TextRange.prototype = { 61 /** 62 * @return {boolean} 63 */ 64 isEmpty: function() 65 { 66 return this.startLine === this.endLine && this.startColumn === this.endColumn; 67 }, 68 69 /** 70 * @param {!WebInspector.TextRange} range 71 * @return {boolean} 72 */ 73 immediatelyPrecedes: function(range) 74 { 75 if (!range) 76 return false; 77 return this.endLine === range.startLine && this.endColumn === range.startColumn; 78 }, 79 80 /** 81 * @param {!WebInspector.TextRange} range 82 * @return {boolean} 83 */ 84 immediatelyFollows: function(range) 85 { 86 if (!range) 87 return false; 88 return range.immediatelyPrecedes(this); 89 }, 90 91 /** 92 * @return {number} 93 */ 94 get linesCount() 95 { 96 return this.endLine - this.startLine; 97 }, 98 99 collapseToEnd: function() 100 { 101 return new WebInspector.TextRange(this.endLine, this.endColumn, this.endLine, this.endColumn); 102 }, 103 104 /** 105 * @return {!WebInspector.TextRange} 106 */ 107 normalize: function() 108 { 109 if (this.startLine > this.endLine || (this.startLine === this.endLine && this.startColumn > this.endColumn)) 110 return new WebInspector.TextRange(this.endLine, this.endColumn, this.startLine, this.startColumn); 111 else 112 return this.clone(); 113 }, 114 115 /** 116 * @return {!WebInspector.TextRange} 117 */ 118 clone: function() 119 { 120 return new WebInspector.TextRange(this.startLine, this.startColumn, this.endLine, this.endColumn); 121 }, 122 123 /** 124 * @return {!Object} 125 */ 126 serializeToObject: function() 127 { 128 var serializedTextRange = {}; 129 serializedTextRange.startLine = this.startLine; 130 serializedTextRange.startColumn = this.startColumn; 131 serializedTextRange.endLine = this.endLine; 132 serializedTextRange.endColumn = this.endColumn; 133 return serializedTextRange; 134 }, 135 136 /** 137 * @param {!WebInspector.TextRange} other 138 * @return {number} 139 */ 140 compareTo: function(other) 141 { 142 if (this.startLine > other.startLine) 143 return 1; 144 if (this.startLine < other.startLine) 145 return -1; 146 if (this.startColumn > other.startColumn) 147 return 1; 148 if (this.startColumn < other.startColumn) 149 return -1; 150 return 0; 151 }, 152 153 /** 154 * @param {number} lineOffset 155 * @return {!WebInspector.TextRange} 156 */ 157 shift: function(lineOffset) 158 { 159 return new WebInspector.TextRange(this.startLine + lineOffset, this.startColumn, this.endLine + lineOffset, this.endColumn); 160 }, 161 162 toString: function() 163 { 164 return JSON.stringify(this); 165 } 166} 167 168/** 169 * @constructor 170 * @param {number} offset 171 * @param {number} length 172 */ 173WebInspector.SourceRange = function(offset, length) 174{ 175 this.offset = offset; 176 this.length = length; 177} 178