OLD | NEW |
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 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 relative.startColumn -= column; | 218 relative.startColumn -= column; |
219 if (this.endLine == line) | 219 if (this.endLine == line) |
220 relative.endColumn -= column; | 220 relative.endColumn -= column; |
221 | 221 |
222 relative.startLine -= line; | 222 relative.startLine -= line; |
223 relative.endLine -= line; | 223 relative.endLine -= line; |
224 return relative; | 224 return relative; |
225 }, | 225 }, |
226 | 226 |
227 /** | 227 /** |
228 * @param {string} text | |
229 * @return {!WebInspector.SourceRange} | |
230 */ | |
231 toSourceRange: function(text) | |
232 { | |
233 var start = text.offsetFromPosition(this.startLine, this.startColumn); | |
234 var end = text.offsetFromPosition(this.endLine, this.endColumn); | |
235 return new WebInspector.SourceRange(start, end - start); | |
236 }, | |
237 | |
238 /** | |
239 * @param {!WebInspector.TextRange} originalRange | 228 * @param {!WebInspector.TextRange} originalRange |
240 * @param {!WebInspector.TextRange} editedRange | 229 * @param {!WebInspector.TextRange} editedRange |
241 * @return {!WebInspector.TextRange} | 230 * @return {!WebInspector.TextRange} |
242 */ | 231 */ |
243 rebaseAfterTextEdit: function(originalRange, editedRange) | 232 rebaseAfterTextEdit: function(originalRange, editedRange) |
244 { | 233 { |
245 console.assert(originalRange.startLine === editedRange.startLine); | 234 console.assert(originalRange.startLine === editedRange.startLine); |
246 console.assert(originalRange.startColumn === editedRange.startColumn); | 235 console.assert(originalRange.startColumn === editedRange.startColumn); |
247 var rebase = this.clone(); | 236 var rebase = this.clone(); |
248 if (!this.follows(originalRange)) | 237 if (!this.follows(originalRange)) |
(...skipping 12 matching lines...) Expand all Loading... |
261 /** | 250 /** |
262 * @override | 251 * @override |
263 * @return {string} | 252 * @return {string} |
264 */ | 253 */ |
265 toString: function() | 254 toString: function() |
266 { | 255 { |
267 return JSON.stringify(this); | 256 return JSON.stringify(this); |
268 }, | 257 }, |
269 | 258 |
270 /** | 259 /** |
271 * @param {string} text | |
272 * @param {string} replacement | |
273 * @return {string} | |
274 */ | |
275 replaceInText: function(text, replacement) | |
276 { | |
277 var sourceRange = this.toSourceRange(text); | |
278 return text.substring(0, sourceRange.offset) + replacement + text.substr
ing(sourceRange.offset + sourceRange.length); | |
279 }, | |
280 | |
281 /** | |
282 * @param {string} text | |
283 * @return {string} | |
284 */ | |
285 extract: function(text) | |
286 { | |
287 var sourceRange = this.toSourceRange(text); | |
288 return text.substr(sourceRange.offset, sourceRange.length); | |
289 }, | |
290 | |
291 /** | |
292 * @param {number} lineNumber | 260 * @param {number} lineNumber |
293 * @param {number} columnNumber | 261 * @param {number} columnNumber |
294 * @return {boolean} | 262 * @return {boolean} |
295 */ | 263 */ |
296 containsLocation: function(lineNumber, columnNumber) | 264 containsLocation: function(lineNumber, columnNumber) |
297 { | 265 { |
298 if (this.startLine === this.endLine) | 266 if (this.startLine === this.endLine) |
299 return this.startLine === lineNumber && this.startColumn <= columnNu
mber && columnNumber <= this.endColumn; | 267 return this.startLine === lineNumber && this.startColumn <= columnNu
mber && columnNumber <= this.endColumn; |
300 if (this.startLine === lineNumber) | 268 if (this.startLine === lineNumber) |
301 return this.startColumn <= columnNumber; | 269 return this.startColumn <= columnNumber; |
302 if (this.endLine === lineNumber) | 270 if (this.endLine === lineNumber) |
303 return columnNumber <= this.endColumn; | 271 return columnNumber <= this.endColumn; |
304 return this.startLine < lineNumber && lineNumber < this.endLine; | 272 return this.startLine < lineNumber && lineNumber < this.endLine; |
305 } | 273 } |
306 } | 274 } |
307 | 275 |
308 /** | 276 /** |
309 * @param {!WebInspector.TextRange} oldRange | 277 * @param {!WebInspector.TextRange} oldRange |
310 * @param {string} newText | 278 * @param {string} newText |
311 * @return {!WebInspector.TextRange} | 279 * @return {!WebInspector.TextRange} |
312 */ | 280 */ |
313 WebInspector.TextRange.fromEdit = function(oldRange, newText) | 281 WebInspector.TextRange.fromEdit = function(oldRange, newText) |
314 { | 282 { |
315 var endLine = oldRange.startLine; | 283 var endLine = oldRange.startLine; |
316 var endColumn = oldRange.startColumn + newText.length; | 284 var endColumn = oldRange.startColumn + newText.length; |
317 var lineEndings = newText.lineEndings(); | 285 var lineEndings = newText.computeLineEndings(); |
318 if (lineEndings.length > 1) { | 286 if (lineEndings.length > 1) { |
319 endLine = oldRange.startLine + lineEndings.length - 1; | 287 endLine = oldRange.startLine + lineEndings.length - 1; |
320 var len = lineEndings.length; | 288 var len = lineEndings.length; |
321 endColumn = lineEndings[len - 1] - lineEndings[len - 2] - 1; | 289 endColumn = lineEndings[len - 1] - lineEndings[len - 2] - 1; |
322 } | 290 } |
323 return new WebInspector.TextRange( | 291 return new WebInspector.TextRange( |
324 oldRange.startLine, | 292 oldRange.startLine, |
325 oldRange.startColumn, | 293 oldRange.startColumn, |
326 endLine, | 294 endLine, |
327 endColumn); | 295 endColumn); |
328 } | 296 } |
329 | 297 |
330 /** | 298 /** |
331 * @constructor | 299 * @constructor |
332 * @param {number} offset | 300 * @param {number} offset |
333 * @param {number} length | 301 * @param {number} length |
334 */ | 302 */ |
335 WebInspector.SourceRange = function(offset, length) | 303 WebInspector.SourceRange = function(offset, length) |
336 { | 304 { |
337 this.offset = offset; | 305 this.offset = offset; |
338 this.length = length; | 306 this.length = length; |
339 } | 307 } |
340 | 308 |
341 WebInspector.SourceRange.prototype = { | 309 WebInspector.SourceRange.prototype = { |
342 /** | 310 /** |
343 * @param {string} text | 311 * @param {!WebInspector.Text} text |
344 * @return {!WebInspector.TextRange} | 312 * @return {!WebInspector.TextRange} |
345 */ | 313 */ |
346 toTextRange: function(text) | 314 toTextRange: function(text) |
347 { | 315 { |
348 var p1 = fromOffset(text, this.offset); | 316 var p1 = fromOffset(text, this.offset); |
349 var p2 = fromOffset(text, this.offset + this.length); | 317 var p2 = fromOffset(text, this.offset + this.length); |
350 return new WebInspector.TextRange(p1.lineNumber, p1.columnNumber, p2.lin
eNumber, p2.columnNumber); | 318 return new WebInspector.TextRange(p1.lineNumber, p1.columnNumber, p2.lin
eNumber, p2.columnNumber); |
351 | 319 |
352 /** | 320 /** |
353 * @param {string} text | 321 * @param {!WebInspector.Text} text |
354 * @param {number} offset | 322 * @param {number} offset |
355 * @return {!{lineNumber: number, columnNumber: number}} | 323 * @return {!{lineNumber: number, columnNumber: number}} |
356 */ | 324 */ |
357 function fromOffset(text, offset) | 325 function fromOffset(text, offset) |
358 { | 326 { |
359 var lineEndings = text.lineEndings(); | 327 var lineEndings = text.lineEndings(); |
360 var lineNumber = lineEndings.lowerBound(offset); | 328 var lineNumber = lineEndings.lowerBound(offset); |
361 var columnNumber = lineNumber === 0 ? offset : offset - lineEndings[
lineNumber - 1] - 1; | 329 var columnNumber = lineNumber === 0 ? offset : offset - lineEndings[
lineNumber - 1] - 1; |
362 return {lineNumber: lineNumber, columnNumber: columnNumber}; | 330 return {lineNumber: lineNumber, columnNumber: columnNumber}; |
363 } | 331 } |
(...skipping 14 matching lines...) Expand all Loading... |
378 } | 346 } |
379 | 347 |
380 WebInspector.SourceEdit.prototype = { | 348 WebInspector.SourceEdit.prototype = { |
381 /** | 349 /** |
382 * @return {!WebInspector.TextRange} | 350 * @return {!WebInspector.TextRange} |
383 */ | 351 */ |
384 newRange: function() | 352 newRange: function() |
385 { | 353 { |
386 return WebInspector.TextRange.fromEdit(this.oldRange, this.newText); | 354 return WebInspector.TextRange.fromEdit(this.oldRange, this.newText); |
387 }, | 355 }, |
| 356 } |
388 | 357 |
389 /** | |
390 * @param {string} text | |
391 * @return {string} | |
392 */ | |
393 applyToText: function(text) | |
394 { | |
395 return this.oldRange.replaceInText(text, this.newText); | |
396 }, | |
397 } | |
OLD | NEW |