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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/common/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
(Empty)
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 * @unrestricted
33 */
34 Common.TextRange = class {
35 /**
36 * @param {number} startLine
37 * @param {number} startColumn
38 * @param {number} endLine
39 * @param {number} endColumn
40 */
41 constructor(startLine, startColumn, endLine, endColumn) {
42 this.startLine = startLine;
43 this.startColumn = startColumn;
44 this.endLine = endLine;
45 this.endColumn = endColumn;
46 }
47
48 /**
49 * @param {number} line
50 * @param {number} column
51 * @return {!Common.TextRange}
52 */
53 static createFromLocation(line, column) {
54 return new Common.TextRange(line, column, line, column);
55 }
56
57 /**
58 * @param {!Object} serializedTextRange
59 * @return {!Common.TextRange}
60 */
61 static fromObject(serializedTextRange) {
62 return new Common.TextRange(
63 serializedTextRange.startLine, serializedTextRange.startColumn, serializ edTextRange.endLine,
64 serializedTextRange.endColumn);
65 }
66
67 /**
68 * @param {!Common.TextRange} range1
69 * @param {!Common.TextRange} range2
70 * @return {number}
71 */
72 static comparator(range1, range2) {
73 return range1.compareTo(range2);
74 }
75
76 /**
77 * @param {!Common.TextRange} oldRange
78 * @param {string} newText
79 * @return {!Common.TextRange}
80 */
81 static fromEdit(oldRange, newText) {
82 var endLine = oldRange.startLine;
83 var endColumn = oldRange.startColumn + newText.length;
84 var lineEndings = newText.computeLineEndings();
85 if (lineEndings.length > 1) {
86 endLine = oldRange.startLine + lineEndings.length - 1;
87 var len = lineEndings.length;
88 endColumn = lineEndings[len - 1] - lineEndings[len - 2] - 1;
89 }
90 return new Common.TextRange(oldRange.startLine, oldRange.startColumn, endLin e, endColumn);
91 }
92
93 /**
94 * @return {boolean}
95 */
96 isEmpty() {
97 return this.startLine === this.endLine && this.startColumn === this.endColum n;
98 }
99
100 /**
101 * @param {!Common.TextRange} range
102 * @return {boolean}
103 */
104 immediatelyPrecedes(range) {
105 if (!range)
106 return false;
107 return this.endLine === range.startLine && this.endColumn === range.startCol umn;
108 }
109
110 /**
111 * @param {!Common.TextRange} range
112 * @return {boolean}
113 */
114 immediatelyFollows(range) {
115 if (!range)
116 return false;
117 return range.immediatelyPrecedes(this);
118 }
119
120 /**
121 * @param {!Common.TextRange} range
122 * @return {boolean}
123 */
124 follows(range) {
125 return (range.endLine === this.startLine && range.endColumn <= this.startCol umn) || range.endLine < this.startLine;
126 }
127
128 /**
129 * @return {number}
130 */
131 get linesCount() {
132 return this.endLine - this.startLine;
133 }
134
135 /**
136 * @return {!Common.TextRange}
137 */
138 collapseToEnd() {
139 return new Common.TextRange(this.endLine, this.endColumn, this.endLine, this .endColumn);
140 }
141
142 /**
143 * @return {!Common.TextRange}
144 */
145 collapseToStart() {
146 return new Common.TextRange(this.startLine, this.startColumn, this.startLine , this.startColumn);
147 }
148
149 /**
150 * @return {!Common.TextRange}
151 */
152 normalize() {
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);
155 else
156 return this.clone();
157 }
158
159 /**
160 * @return {!Common.TextRange}
161 */
162 clone() {
163 return new Common.TextRange(this.startLine, this.startColumn, this.endLine, this.endColumn);
164 }
165
166 /**
167 * @return {!{startLine: number, startColumn: number, endLine: number, endColu mn: number}}
168 */
169 serializeToObject() {
170 var serializedTextRange = {};
171 serializedTextRange.startLine = this.startLine;
172 serializedTextRange.startColumn = this.startColumn;
173 serializedTextRange.endLine = this.endLine;
174 serializedTextRange.endColumn = this.endColumn;
175 return serializedTextRange;
176 }
177
178 /**
179 * @param {!Common.TextRange} other
180 * @return {number}
181 */
182 compareTo(other) {
183 if (this.startLine > other.startLine)
184 return 1;
185 if (this.startLine < other.startLine)
186 return -1;
187 if (this.startColumn > other.startColumn)
188 return 1;
189 if (this.startColumn < other.startColumn)
190 return -1;
191 return 0;
192 }
193
194 /**
195 * @param {number} lineNumber
196 * @param {number} columnNumber
197 * @return {number}
198 */
199 compareToPosition(lineNumber, columnNumber) {
200 if (lineNumber < this.startLine || (lineNumber === this.startLine && columnN umber < this.startColumn))
201 return -1;
202 if (lineNumber > this.endLine || (lineNumber === this.endLine && columnNumbe r > this.endColumn))
203 return 1;
204 return 0;
205 }
206
207 /**
208 * @param {!Common.TextRange} other
209 * @return {boolean}
210 */
211 equal(other) {
212 return this.startLine === other.startLine && this.endLine === other.endLine &&
213 this.startColumn === other.startColumn && this.endColumn === other.endCo lumn;
214 }
215
216 /**
217 * @param {number} line
218 * @param {number} column
219 * @return {!Common.TextRange}
220 */
221 relativeTo(line, column) {
222 var relative = this.clone();
223
224 if (this.startLine === line)
225 relative.startColumn -= column;
226 if (this.endLine === line)
227 relative.endColumn -= column;
228
229 relative.startLine -= line;
230 relative.endLine -= line;
231 return relative;
232 }
233
234 /**
235 * @param {!Common.TextRange} originalRange
236 * @param {!Common.TextRange} editedRange
237 * @return {!Common.TextRange}
238 */
239 rebaseAfterTextEdit(originalRange, editedRange) {
240 console.assert(originalRange.startLine === editedRange.startLine);
241 console.assert(originalRange.startColumn === editedRange.startColumn);
242 var rebase = this.clone();
243 if (!this.follows(originalRange))
244 return rebase;
245 var lineDelta = editedRange.endLine - originalRange.endLine;
246 var columnDelta = editedRange.endColumn - originalRange.endColumn;
247 rebase.startLine += lineDelta;
248 rebase.endLine += lineDelta;
249 if (rebase.startLine === editedRange.endLine)
250 rebase.startColumn += columnDelta;
251 if (rebase.endLine === editedRange.endLine)
252 rebase.endColumn += columnDelta;
253 return rebase;
254 }
255
256 /**
257 * @override
258 * @return {string}
259 */
260 toString() {
261 return JSON.stringify(this);
262 }
263
264 /**
265 * @param {number} lineNumber
266 * @param {number} columnNumber
267 * @return {boolean}
268 */
269 containsLocation(lineNumber, columnNumber) {
270 if (this.startLine === this.endLine)
271 return this.startLine === lineNumber && this.startColumn <= columnNumber & & columnNumber <= this.endColumn;
272 if (this.startLine === lineNumber)
273 return this.startColumn <= columnNumber;
274 if (this.endLine === lineNumber)
275 return columnNumber <= this.endColumn;
276 return this.startLine < lineNumber && lineNumber < this.endLine;
277 }
278 };
279
280
281 /**
282 * @unrestricted
283 */
284 Common.SourceRange = class {
285 /**
286 * @param {number} offset
287 * @param {number} length
288 */
289 constructor(offset, length) {
290 this.offset = offset;
291 this.length = length;
292 }
293 };
294
295 /**
296 * @unrestricted
297 */
298 Common.SourceEdit = class {
299 /**
300 * @param {string} sourceURL
301 * @param {!Common.TextRange} oldRange
302 * @param {string} newText
303 */
304 constructor(sourceURL, oldRange, newText) {
305 this.sourceURL = sourceURL;
306 this.oldRange = oldRange;
307 this.newText = newText;
308 }
309
310 /**
311 * @param {!Common.SourceEdit} edit1
312 * @param {!Common.SourceEdit} edit2
313 * @return {number}
314 */
315 static comparator(edit1, edit2) {
316 return Common.TextRange.comparator(edit1.oldRange, edit2.oldRange);
317 }
318
319 /**
320 * @return {!Common.TextRange}
321 */
322 newRange() {
323 return Common.TextRange.fromEdit(this.oldRange, this.newText);
324 }
325 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698