OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013, the Dart project authors. | 2 * Copyright (c) 2013, the Dart project authors. |
3 * | 3 * |
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except | 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except |
5 * in compliance with the License. You may obtain a copy of the License at | 5 * in compliance with the License. You may obtain a copy of the License at |
6 * | 6 * |
7 * http://www.eclipse.org/legal/epl-v10.html | 7 * http://www.eclipse.org/legal/epl-v10.html |
8 * | 8 * |
9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License | 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License |
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express | 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express |
11 * or implied. See the License for the specific language governing permissions a
nd limitations under | 11 * or implied. See the License for the specific language governing permissions a
nd limitations under |
12 * the License. | 12 * the License. |
13 */ | 13 */ |
14 package com.google.dart.tools.ui.internal.text.dart; | 14 package com.google.dart.tools.ui.internal.text.dart; |
15 | 15 |
16 import static java.lang.Math.max; | |
17 import static java.lang.Math.min; | |
18 | |
19 /** | 16 /** |
20 * Instances of {@code DartReconcilingRegion} represent a source region that has
changed. | 17 * Instances of {@code DartReconcilingRegion} represent a source region that has
changed. |
21 */ | 18 */ |
22 public class DartReconcilingRegion { | 19 public class DartReconcilingRegion { |
23 | 20 |
24 public static final DartReconcilingRegion EMPTY = new DartReconcilingRegion(0,
0, 0); | 21 public static final DartReconcilingRegion EMPTY = new DartReconcilingRegion(0,
0, 0); |
25 | 22 |
26 private final int offset; | 23 private final int offset; |
27 private final int oldLength; | 24 private final int oldLength; |
28 private final int newLength; | 25 private final int newLength; |
29 | 26 |
30 /** | 27 /** |
31 * Construct a new instance representing a region of source. | 28 * Construct a new instance representing a region of source. |
32 * | 29 * |
33 * @param offset the offset of the first character that changed | 30 * @param offset the offset of the first character that changed |
34 * @param oldLength the number of characters that were replaced | 31 * @param oldLength the number of characters that were replaced |
35 * @param newLength the number of characters in the replacement text | 32 * @param newLength the number of characters in the replacement text |
36 */ | 33 */ |
37 public DartReconcilingRegion(int offset, int oldLength, int newLength) { | 34 public DartReconcilingRegion(int offset, int oldLength, int newLength) { |
38 this.offset = offset; | 35 this.offset = offset; |
39 this.oldLength = oldLength; | 36 this.oldLength = oldLength; |
40 this.newLength = newLength; | 37 this.newLength = newLength; |
41 } | 38 } |
42 | 39 |
43 /** | 40 /** |
44 * Return a new region representing the union of the receiver with the specifi
ed region. | 41 * Return a new region representing the union of the receiver with the specifi
ed region or |
| 42 * {@code null} if the resulting region is disjoint and cannot be represented
by this class. |
45 * | 43 * |
46 * @param offset the offset of the first character that changed | 44 * @param offset the offset of the first character that changed |
47 * @param oldLength the number of characters that were replaced | 45 * @param oldLength the number of characters that were replaced |
48 * @param newLength the number of characters in the replacement text | 46 * @param newLength the number of characters in the replacement text |
| 47 * @return the region or {@code null} if disjoint |
49 */ | 48 */ |
50 public DartReconcilingRegion add(int offset, int oldLength, int newLength) { | 49 public DartReconcilingRegion add(int offset, int oldLength, int newLength) { |
51 if (oldLength == 0 && newLength == 0) { | 50 if (oldLength == 0 && newLength == 0) { |
52 return this; | 51 return this; |
53 } | 52 } |
54 if (isEmpty()) { | 53 if (isEmpty()) { |
55 return new DartReconcilingRegion(offset, oldLength, newLength); | 54 return new DartReconcilingRegion(offset, oldLength, newLength); |
56 } | 55 } |
57 int offset2 = min(this.offset, offset); | 56 if (offset + oldLength < this.offset) { |
58 int oldLength2 = max(this.offset + this.oldLength, offset + oldLength) - off
set2; | 57 return null; |
59 int newLength2 = max(this.offset + this.newLength, offset + newLength) - off
set2; | 58 } |
| 59 if (offset + oldLength > this.offset + this.newLength) { |
| 60 return null; |
| 61 } |
| 62 int offset2 = Math.min(this.offset, offset); |
| 63 int oldLength2 = this.oldLength + (this.offset - offset2); |
| 64 int newLength2 = this.newLength + (newLength - oldLength) + (this.offset - o
ffset2); |
60 return new DartReconcilingRegion(offset2, oldLength2, newLength2); | 65 return new DartReconcilingRegion(offset2, oldLength2, newLength2); |
61 } | 66 } |
62 | 67 |
63 /** | 68 /** |
64 * Return the number of characters in the replacement text. | 69 * Return the number of characters in the replacement text. |
65 * | 70 * |
66 * @return the offset (greater than or equal to zero) | 71 * @return the offset (greater than or equal to zero) |
67 */ | 72 */ |
68 public int getNewLength() { | 73 public int getNewLength() { |
69 return newLength; | 74 return newLength; |
(...skipping 19 matching lines...) Expand all Loading... |
89 | 94 |
90 /** | 95 /** |
91 * Return {@code true} if the range represent by the receiver is empty. | 96 * Return {@code true} if the range represent by the receiver is empty. |
92 * | 97 * |
93 * @return {@code true} if empty | 98 * @return {@code true} if empty |
94 */ | 99 */ |
95 public boolean isEmpty() { | 100 public boolean isEmpty() { |
96 return oldLength == 0 && newLength == 0; | 101 return oldLength == 0 && newLength == 0; |
97 } | 102 } |
98 } | 103 } |
OLD | NEW |