Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(566)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/text_utils/TextRange.js

Issue 2769843003: DevTools: split text_utils out of common module (Closed)
Patch Set: rebaseline Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 /** 31 /**
32 * @unrestricted 32 * @unrestricted
33 */ 33 */
34 Common.TextRange = class { 34 TextUtils.TextRange = class {
35 /** 35 /**
36 * @param {number} startLine 36 * @param {number} startLine
37 * @param {number} startColumn 37 * @param {number} startColumn
38 * @param {number} endLine 38 * @param {number} endLine
39 * @param {number} endColumn 39 * @param {number} endColumn
40 */ 40 */
41 constructor(startLine, startColumn, endLine, endColumn) { 41 constructor(startLine, startColumn, endLine, endColumn) {
42 this.startLine = startLine; 42 this.startLine = startLine;
43 this.startColumn = startColumn; 43 this.startColumn = startColumn;
44 this.endLine = endLine; 44 this.endLine = endLine;
45 this.endColumn = endColumn; 45 this.endColumn = endColumn;
46 } 46 }
47 47
48 /** 48 /**
49 * @param {number} line 49 * @param {number} line
50 * @param {number} column 50 * @param {number} column
51 * @return {!Common.TextRange} 51 * @return {!TextUtils.TextRange}
52 */ 52 */
53 static createFromLocation(line, column) { 53 static createFromLocation(line, column) {
54 return new Common.TextRange(line, column, line, column); 54 return new TextUtils.TextRange(line, column, line, column);
55 } 55 }
56 56
57 /** 57 /**
58 * @param {!Object} serializedTextRange 58 * @param {!Object} serializedTextRange
59 * @return {!Common.TextRange} 59 * @return {!TextUtils.TextRange}
60 */ 60 */
61 static fromObject(serializedTextRange) { 61 static fromObject(serializedTextRange) {
62 return new Common.TextRange( 62 return new TextUtils.TextRange(
63 serializedTextRange.startLine, serializedTextRange.startColumn, serializ edTextRange.endLine, 63 serializedTextRange.startLine, serializedTextRange.startColumn, serializ edTextRange.endLine,
64 serializedTextRange.endColumn); 64 serializedTextRange.endColumn);
65 } 65 }
66 66
67 /** 67 /**
68 * @param {!Common.TextRange} range1 68 * @param {!TextUtils.TextRange} range1
69 * @param {!Common.TextRange} range2 69 * @param {!TextUtils.TextRange} range2
70 * @return {number} 70 * @return {number}
71 */ 71 */
72 static comparator(range1, range2) { 72 static comparator(range1, range2) {
73 return range1.compareTo(range2); 73 return range1.compareTo(range2);
74 } 74 }
75 75
76 /** 76 /**
77 * @param {!Common.TextRange} oldRange 77 * @param {!TextUtils.TextRange} oldRange
78 * @param {string} newText 78 * @param {string} newText
79 * @return {!Common.TextRange} 79 * @return {!TextUtils.TextRange}
80 */ 80 */
81 static fromEdit(oldRange, newText) { 81 static fromEdit(oldRange, newText) {
82 var endLine = oldRange.startLine; 82 var endLine = oldRange.startLine;
83 var endColumn = oldRange.startColumn + newText.length; 83 var endColumn = oldRange.startColumn + newText.length;
84 var lineEndings = newText.computeLineEndings(); 84 var lineEndings = newText.computeLineEndings();
85 if (lineEndings.length > 1) { 85 if (lineEndings.length > 1) {
86 endLine = oldRange.startLine + lineEndings.length - 1; 86 endLine = oldRange.startLine + lineEndings.length - 1;
87 var len = lineEndings.length; 87 var len = lineEndings.length;
88 endColumn = lineEndings[len - 1] - lineEndings[len - 2] - 1; 88 endColumn = lineEndings[len - 1] - lineEndings[len - 2] - 1;
89 } 89 }
90 return new Common.TextRange(oldRange.startLine, oldRange.startColumn, endLin e, endColumn); 90 return new TextUtils.TextRange(oldRange.startLine, oldRange.startColumn, end Line, endColumn);
91 } 91 }
92 92
93 /** 93 /**
94 * @return {boolean} 94 * @return {boolean}
95 */ 95 */
96 isEmpty() { 96 isEmpty() {
97 return this.startLine === this.endLine && this.startColumn === this.endColum n; 97 return this.startLine === this.endLine && this.startColumn === this.endColum n;
98 } 98 }
99 99
100 /** 100 /**
101 * @param {!Common.TextRange} range 101 * @param {!TextUtils.TextRange} range
102 * @return {boolean} 102 * @return {boolean}
103 */ 103 */
104 immediatelyPrecedes(range) { 104 immediatelyPrecedes(range) {
105 if (!range) 105 if (!range)
106 return false; 106 return false;
107 return this.endLine === range.startLine && this.endColumn === range.startCol umn; 107 return this.endLine === range.startLine && this.endColumn === range.startCol umn;
108 } 108 }
109 109
110 /** 110 /**
111 * @param {!Common.TextRange} range 111 * @param {!TextUtils.TextRange} range
112 * @return {boolean} 112 * @return {boolean}
113 */ 113 */
114 immediatelyFollows(range) { 114 immediatelyFollows(range) {
115 if (!range) 115 if (!range)
116 return false; 116 return false;
117 return range.immediatelyPrecedes(this); 117 return range.immediatelyPrecedes(this);
118 } 118 }
119 119
120 /** 120 /**
121 * @param {!Common.TextRange} range 121 * @param {!TextUtils.TextRange} range
122 * @return {boolean} 122 * @return {boolean}
123 */ 123 */
124 follows(range) { 124 follows(range) {
125 return (range.endLine === this.startLine && range.endColumn <= this.startCol umn) || range.endLine < this.startLine; 125 return (range.endLine === this.startLine && range.endColumn <= this.startCol umn) || range.endLine < this.startLine;
126 } 126 }
127 127
128 /** 128 /**
129 * @return {number} 129 * @return {number}
130 */ 130 */
131 get linesCount() { 131 get linesCount() {
132 return this.endLine - this.startLine; 132 return this.endLine - this.startLine;
133 } 133 }
134 134
135 /** 135 /**
136 * @return {!Common.TextRange} 136 * @return {!TextUtils.TextRange}
137 */ 137 */
138 collapseToEnd() { 138 collapseToEnd() {
139 return new Common.TextRange(this.endLine, this.endColumn, this.endLine, this .endColumn); 139 return new TextUtils.TextRange(this.endLine, this.endColumn, this.endLine, t his.endColumn);
140 } 140 }
141 141
142 /** 142 /**
143 * @return {!Common.TextRange} 143 * @return {!TextUtils.TextRange}
144 */ 144 */
145 collapseToStart() { 145 collapseToStart() {
146 return new Common.TextRange(this.startLine, this.startColumn, this.startLine , this.startColumn); 146 return new TextUtils.TextRange(this.startLine, this.startColumn, this.startL ine, this.startColumn);
147 } 147 }
148 148
149 /** 149 /**
150 * @return {!Common.TextRange} 150 * @return {!TextUtils.TextRange}
151 */ 151 */
152 normalize() { 152 normalize() {
153 if (this.startLine > this.endLine || (this.startLine === this.endLine && thi s.startColumn > this.endColumn)) 153 if (this.startLine > this.endLine || (this.startLine === this.endLine && thi s.startColumn > this.endColumn))
154 return new Common.TextRange(this.endLine, this.endColumn, this.startLine, this.startColumn); 154 return new TextUtils.TextRange(this.endLine, this.endColumn, this.startLin e, this.startColumn);
155 else 155 else
156 return this.clone(); 156 return this.clone();
157 } 157 }
158 158
159 /** 159 /**
160 * @return {!Common.TextRange} 160 * @return {!TextUtils.TextRange}
161 */ 161 */
162 clone() { 162 clone() {
163 return new Common.TextRange(this.startLine, this.startColumn, this.endLine, this.endColumn); 163 return new TextUtils.TextRange(this.startLine, this.startColumn, this.endLin e, this.endColumn);
164 } 164 }
165 165
166 /** 166 /**
167 * @return {!{startLine: number, startColumn: number, endLine: number, endColu mn: number}} 167 * @return {!{startLine: number, startColumn: number, endLine: number, endColu mn: number}}
168 */ 168 */
169 serializeToObject() { 169 serializeToObject() {
170 var serializedTextRange = {}; 170 var serializedTextRange = {};
171 serializedTextRange.startLine = this.startLine; 171 serializedTextRange.startLine = this.startLine;
172 serializedTextRange.startColumn = this.startColumn; 172 serializedTextRange.startColumn = this.startColumn;
173 serializedTextRange.endLine = this.endLine; 173 serializedTextRange.endLine = this.endLine;
174 serializedTextRange.endColumn = this.endColumn; 174 serializedTextRange.endColumn = this.endColumn;
175 return serializedTextRange; 175 return serializedTextRange;
176 } 176 }
177 177
178 /** 178 /**
179 * @param {!Common.TextRange} other 179 * @param {!TextUtils.TextRange} other
180 * @return {number} 180 * @return {number}
181 */ 181 */
182 compareTo(other) { 182 compareTo(other) {
183 if (this.startLine > other.startLine) 183 if (this.startLine > other.startLine)
184 return 1; 184 return 1;
185 if (this.startLine < other.startLine) 185 if (this.startLine < other.startLine)
186 return -1; 186 return -1;
187 if (this.startColumn > other.startColumn) 187 if (this.startColumn > other.startColumn)
188 return 1; 188 return 1;
189 if (this.startColumn < other.startColumn) 189 if (this.startColumn < other.startColumn)
190 return -1; 190 return -1;
191 return 0; 191 return 0;
192 } 192 }
193 193
194 /** 194 /**
195 * @param {number} lineNumber 195 * @param {number} lineNumber
196 * @param {number} columnNumber 196 * @param {number} columnNumber
197 * @return {number} 197 * @return {number}
198 */ 198 */
199 compareToPosition(lineNumber, columnNumber) { 199 compareToPosition(lineNumber, columnNumber) {
200 if (lineNumber < this.startLine || (lineNumber === this.startLine && columnN umber < this.startColumn)) 200 if (lineNumber < this.startLine || (lineNumber === this.startLine && columnN umber < this.startColumn))
201 return -1; 201 return -1;
202 if (lineNumber > this.endLine || (lineNumber === this.endLine && columnNumbe r > this.endColumn)) 202 if (lineNumber > this.endLine || (lineNumber === this.endLine && columnNumbe r > this.endColumn))
203 return 1; 203 return 1;
204 return 0; 204 return 0;
205 } 205 }
206 206
207 /** 207 /**
208 * @param {!Common.TextRange} other 208 * @param {!TextUtils.TextRange} other
209 * @return {boolean} 209 * @return {boolean}
210 */ 210 */
211 equal(other) { 211 equal(other) {
212 return this.startLine === other.startLine && this.endLine === other.endLine && 212 return this.startLine === other.startLine && this.endLine === other.endLine &&
213 this.startColumn === other.startColumn && this.endColumn === other.endCo lumn; 213 this.startColumn === other.startColumn && this.endColumn === other.endCo lumn;
214 } 214 }
215 215
216 /** 216 /**
217 * @param {number} line 217 * @param {number} line
218 * @param {number} column 218 * @param {number} column
219 * @return {!Common.TextRange} 219 * @return {!TextUtils.TextRange}
220 */ 220 */
221 relativeTo(line, column) { 221 relativeTo(line, column) {
222 var relative = this.clone(); 222 var relative = this.clone();
223 223
224 if (this.startLine === line) 224 if (this.startLine === line)
225 relative.startColumn -= column; 225 relative.startColumn -= column;
226 if (this.endLine === line) 226 if (this.endLine === line)
227 relative.endColumn -= column; 227 relative.endColumn -= column;
228 228
229 relative.startLine -= line; 229 relative.startLine -= line;
230 relative.endLine -= line; 230 relative.endLine -= line;
231 return relative; 231 return relative;
232 } 232 }
233 233
234 /** 234 /**
235 * @param {!Common.TextRange} originalRange 235 * @param {!TextUtils.TextRange} originalRange
236 * @param {!Common.TextRange} editedRange 236 * @param {!TextUtils.TextRange} editedRange
237 * @return {!Common.TextRange} 237 * @return {!TextUtils.TextRange}
238 */ 238 */
239 rebaseAfterTextEdit(originalRange, editedRange) { 239 rebaseAfterTextEdit(originalRange, editedRange) {
240 console.assert(originalRange.startLine === editedRange.startLine); 240 console.assert(originalRange.startLine === editedRange.startLine);
241 console.assert(originalRange.startColumn === editedRange.startColumn); 241 console.assert(originalRange.startColumn === editedRange.startColumn);
242 var rebase = this.clone(); 242 var rebase = this.clone();
243 if (!this.follows(originalRange)) 243 if (!this.follows(originalRange))
244 return rebase; 244 return rebase;
245 var lineDelta = editedRange.endLine - originalRange.endLine; 245 var lineDelta = editedRange.endLine - originalRange.endLine;
246 var columnDelta = editedRange.endColumn - originalRange.endColumn; 246 var columnDelta = editedRange.endColumn - originalRange.endColumn;
247 rebase.startLine += lineDelta; 247 rebase.startLine += lineDelta;
(...skipping 26 matching lines...) Expand all
274 if (this.endLine === lineNumber) 274 if (this.endLine === lineNumber)
275 return columnNumber <= this.endColumn; 275 return columnNumber <= this.endColumn;
276 return this.startLine < lineNumber && lineNumber < this.endLine; 276 return this.startLine < lineNumber && lineNumber < this.endLine;
277 } 277 }
278 }; 278 };
279 279
280 280
281 /** 281 /**
282 * @unrestricted 282 * @unrestricted
283 */ 283 */
284 Common.SourceRange = class { 284 TextUtils.SourceRange = class {
285 /** 285 /**
286 * @param {number} offset 286 * @param {number} offset
287 * @param {number} length 287 * @param {number} length
288 */ 288 */
289 constructor(offset, length) { 289 constructor(offset, length) {
290 this.offset = offset; 290 this.offset = offset;
291 this.length = length; 291 this.length = length;
292 } 292 }
293 }; 293 };
294 294
295 /** 295 /**
296 * @unrestricted 296 * @unrestricted
297 */ 297 */
298 Common.SourceEdit = class { 298 TextUtils.SourceEdit = class {
299 /** 299 /**
300 * @param {string} sourceURL 300 * @param {string} sourceURL
301 * @param {!Common.TextRange} oldRange 301 * @param {!TextUtils.TextRange} oldRange
302 * @param {string} newText 302 * @param {string} newText
303 */ 303 */
304 constructor(sourceURL, oldRange, newText) { 304 constructor(sourceURL, oldRange, newText) {
305 this.sourceURL = sourceURL; 305 this.sourceURL = sourceURL;
306 this.oldRange = oldRange; 306 this.oldRange = oldRange;
307 this.newText = newText; 307 this.newText = newText;
308 } 308 }
309 309
310 /** 310 /**
311 * @param {!Common.SourceEdit} edit1 311 * @param {!TextUtils.SourceEdit} edit1
312 * @param {!Common.SourceEdit} edit2 312 * @param {!TextUtils.SourceEdit} edit2
313 * @return {number} 313 * @return {number}
314 */ 314 */
315 static comparator(edit1, edit2) { 315 static comparator(edit1, edit2) {
316 return Common.TextRange.comparator(edit1.oldRange, edit2.oldRange); 316 return TextUtils.TextRange.comparator(edit1.oldRange, edit2.oldRange);
317 } 317 }
318 318
319 /** 319 /**
320 * @return {!Common.TextRange} 320 * @return {!TextUtils.TextRange}
321 */ 321 */
322 newRange() { 322 newRange() {
323 return Common.TextRange.fromEdit(this.oldRange, this.newText); 323 return TextUtils.TextRange.fromEdit(this.oldRange, this.newText);
324 } 324 }
325 }; 325 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698