comparison dwtx/jface/internal/text/revisions/Range.d @ 162:1a5b8f8129df

...
author Frank Benoit <benoit@tionex.de>
date Mon, 08 Sep 2008 00:51:37 +0200
parents 6dcb0baaa031
children
comparison
equal deleted inserted replaced
161:f8d52b926852 162:1a5b8f8129df
18 import dwtx.jface.internal.text.revisions.Colors; // packageimport 18 import dwtx.jface.internal.text.revisions.Colors; // packageimport
19 import dwtx.jface.internal.text.revisions.ChangeRegion; // packageimport 19 import dwtx.jface.internal.text.revisions.ChangeRegion; // packageimport
20 import dwtx.jface.internal.text.revisions.RevisionPainter; // packageimport 20 import dwtx.jface.internal.text.revisions.RevisionPainter; // packageimport
21 import dwtx.jface.internal.text.revisions.RevisionSelectionProvider; // packageimport 21 import dwtx.jface.internal.text.revisions.RevisionSelectionProvider; // packageimport
22 22
23
24 import dwt.dwthelper.utils; 23 import dwt.dwthelper.utils;
24 import tango.text.convert.Format;
25 25
26 import dwtx.jface.text.source.ILineRange; 26 import dwtx.jface.text.source.ILineRange;
27 27
28 /** 28 /**
29 * A variable {@link dwtx.jface.text.source.ILineRange} with the following invariant: 29 * A variable {@link dwtx.jface.text.source.ILineRange} with the following invariant:
34 * <p> 34 * <p>
35 * Attempts to create or modify a <code>Range</code> such that this invariant would be violated 35 * Attempts to create or modify a <code>Range</code> such that this invariant would be violated
36 * result in a {@link LineIndexOutOfBoundsException} being 36 * result in a {@link LineIndexOutOfBoundsException} being
37 * thrown. 37 * thrown.
38 * </p> 38 * </p>
39 * 39 *
40 * @since 3.2 40 * @since 3.2
41 */ 41 */
42 public final class Range : ILineRange, Cloneable { 42 public final class Range : ILineRange, Cloneable {
43 /** 43 /**
44 * Creates a new range with the same start and length as the passed line range. 44 * Creates a new range with the same start and length as the passed line range.
45 * 45 *
46 * @param range the range to copy 46 * @param range the range to copy
47 * @return a <code>Range</code> with the same start and length as <code>range</code> 47 * @return a <code>Range</code> with the same start and length as <code>range</code>
48 * @throws LineIndexOutOfBoundsException if the passed {@link ILineRange} does not adhere to the 48 * @throws LineIndexOutOfBoundsException if the passed {@link ILineRange} does not adhere to the
49 * contract of {@link Range} 49 * contract of {@link Range}
50 */ 50 */
51 public static Range copy(ILineRange range) { 51 public static Range copy(ILineRange range) {
52 return createRelative(range.getStartLine(), range.getNumberOfLines()); 52 return createRelative(range.getStartLine(), range.getNumberOfLines());
53 } 53 }
54 54
55 /** 55 /**
56 * Creates a new range equal to the passed line range. 56 * Creates a new range equal to the passed line range.
57 * 57 *
58 * @param range the range to copy 58 * @param range the range to copy
59 * @return a <code>Range</code> equal to <code>range</code> 59 * @return a <code>Range</code> equal to <code>range</code>
60 */ 60 */
61 public static Range copy(Range range) { 61 public static Range copy(Range range) {
62 return createRelative(range.start(), range.length()); 62 return createRelative(range.start(), range.length());
63 } 63 }
64 64
65 /** 65 /**
66 * Creates a new range with the given start offset and length. 66 * Creates a new range with the given start offset and length.
67 * 67 *
68 * @param start the first line of the new range, must be &gt;= 0 68 * @param start the first line of the new range, must be &gt;= 0
69 * @param length the number of lines included in the new range, must be &gt; 0 69 * @param length the number of lines included in the new range, must be &gt; 0
70 * @return a <code>Range</code> with the given start and length 70 * @return a <code>Range</code> with the given start and length
71 * @throws LineIndexOutOfBoundsException if the parameters violate the invariant of 71 * @throws LineIndexOutOfBoundsException if the parameters violate the invariant of
72 * {@link Range} 72 * {@link Range}
75 return new Range(start, length); 75 return new Range(start, length);
76 } 76 }
77 77
78 /** 78 /**
79 * Creates a new range with the given start and end offsets. 79 * Creates a new range with the given start and end offsets.
80 * 80 *
81 * @param start the first line of the new range, must be &gt;= 0 81 * @param start the first line of the new range, must be &gt;= 0
82 * @param end the first line not in the range any more (exclusive), must be &gt; <code>start</code> 82 * @param end the first line not in the range any more (exclusive), must be &gt; <code>start</code>
83 * @return a <code>Range</code> with the given start and end offsets 83 * @return a <code>Range</code> with the given start and end offsets
84 * @throws LineIndexOutOfBoundsException if the parameters violate the invariant of 84 * @throws LineIndexOutOfBoundsException if the parameters violate the invariant of
85 * {@link Range} 85 * {@link Range}
96 */ 96 */
97 private this(int start, int length) { 97 private this(int start, int length) {
98 moveTo(start); 98 moveTo(start);
99 setLength(length); 99 setLength(length);
100 } 100 }
101 101
102 /* 102 /*
103 * @see dwtx.jface.text.source.ILineRange#getStartLine() 103 * @see dwtx.jface.text.source.ILineRange#getStartLine()
104 */ 104 */
105 public int getStartLine() { 105 public int getStartLine() {
106 return start(); 106 return start();
110 * @see dwtx.jface.text.source.ILineRange#getNumberOfLines() 110 * @see dwtx.jface.text.source.ILineRange#getNumberOfLines()
111 */ 111 */
112 public int getNumberOfLines() { 112 public int getNumberOfLines() {
113 return length(); 113 return length();
114 } 114 }
115 115
116 /** 116 /**
117 * Returns the first line contained in this range. Short equivalent of {@link #getStartLine()}. 117 * Returns the first line contained in this range. Short equivalent of {@link #getStartLine()}.
118 * 118 *
119 * @return the first line contained in this range 119 * @return the first line contained in this range
120 */ 120 */
121 public int start() { 121 public int start() {
122 return fStart; 122 return fStart;
123 } 123 }
124 124
125 /** 125 /**
126 * Returns the number of lines contained in this range. Short equivalent of {@link #getNumberOfLines()}. 126 * Returns the number of lines contained in this range. Short equivalent of {@link #getNumberOfLines()}.
127 * 127 *
128 * @return the number of lines contained in this range 128 * @return the number of lines contained in this range
129 */ 129 */
130 public int length() { 130 public int length() {
131 return fLength; 131 return fLength;
132 } 132 }
133 133
134 /** 134 /**
135 * Returns the first line after this range. Equivalent to {@linkplain #start() start} + {@linkplain #length() length}. 135 * Returns the first line after this range. Equivalent to {@linkplain #start() start} + {@linkplain #length() length}.
136 * 136 *
137 * @return the first line after this range 137 * @return the first line after this range
138 */ 138 */
139 public int end() { 139 public int end() {
140 return start() + length(); 140 return start() + length();
141 } 141 }
142 142
143 /** 143 /**
144 * Moves the receiver to <code>start</code>, keeping {@link #length()} constant. 144 * Moves the receiver to <code>start</code>, keeping {@link #length()} constant.
145 * 145 *
146 * @param start the new start, must be &gt;= 0 146 * @param start the new start, must be &gt;= 0
147 * @throws LineIndexOutOfBoundsException if <code>start</code> &lt; 0 147 * @throws LineIndexOutOfBoundsException if <code>start</code> &lt; 0
148 */ 148 */
149 public void moveTo(int start) { 149 public void moveTo(int start) {
150 if (!(start >= 0)) 150 if (!(start >= 0))
151 throw new LineIndexOutOfBoundsException("Cannot set a negative start: " + start); //$NON-NLS-1$ 151 throw new LineIndexOutOfBoundsException(Format("Cannot set a negative start: {}", start)); //$NON-NLS-1$
152 fStart= start; 152 fStart= start;
153 } 153 }
154 154
155 /** 155 /**
156 * Moves this range such that the {@link #end()} is at <code>end</code>, keeping 156 * Moves this range such that the {@link #end()} is at <code>end</code>, keeping
157 * {@link #length()} constant. 157 * {@link #length()} constant.
158 * 158 *
159 * @param end the new end 159 * @param end the new end
160 * @throws LineIndexOutOfBoundsException if <code>end</code> &lt;= {@link #start()} 160 * @throws LineIndexOutOfBoundsException if <code>end</code> &lt;= {@link #start()}
161 */ 161 */
162 public void moveEndTo(int end) { 162 public void moveEndTo(int end) {
163 moveTo(end - length()); 163 moveTo(end - length());
164 } 164 }
165 165
166 /** 166 /**
167 * Moves the range by <code>delta</code> lines, keeping {@link #length()} constant. The 167 * Moves the range by <code>delta</code> lines, keeping {@link #length()} constant. The
168 * resulting start line must be &gt;= 0. 168 * resulting start line must be &gt;= 0.
169 * 169 *
170 * @param delta the number of lines to shift the range 170 * @param delta the number of lines to shift the range
171 * @throws LineIndexOutOfBoundsException if <code>-delta</code> &gt; {@link #start()} 171 * @throws LineIndexOutOfBoundsException if <code>-delta</code> &gt; {@link #start()}
172 */ 172 */
173 public void moveBy(int delta) { 173 public void moveBy(int delta) {
174 moveTo(start() + delta); 174 moveTo(start() + delta);
175 } 175 }
176 176
177 /** 177 /**
178 * Moves the start offset to <code>start</code>, keeping {@link #end()} constant. 178 * Moves the start offset to <code>start</code>, keeping {@link #end()} constant.
179 * 179 *
180 * @param start the new start, must be &gt;= 0 and &lt; {@link #end()} 180 * @param start the new start, must be &gt;= 0 and &lt; {@link #end()}
181 * @throws LineIndexOutOfBoundsException if <code>start</code> &lt; 0 or &gt;= {@link #end()} 181 * @throws LineIndexOutOfBoundsException if <code>start</code> &lt; 0 or &gt;= {@link #end()}
182 */ 182 */
183 public void setStart(int start) { 183 public void setStart(int start) {
184 int end= end(); 184 int end= end();
185 if (!(start >= 0 && start < end)) 185 if (!(start >= 0 && start < end))
186 throw new LineIndexOutOfBoundsException("Cannot set a negative start: " + start); //$NON-NLS-1$ 186 throw new LineIndexOutOfBoundsException(Format("Cannot set a negative start: {}", start)); //$NON-NLS-1$
187 moveTo(start); 187 moveTo(start);
188 setEnd(end); 188 setEnd(end);
189 } 189 }
190 190
191 /** 191 /**
192 * Sets the end of this range, keeping {@link #start()} constant. 192 * Sets the end of this range, keeping {@link #start()} constant.
193 * 193 *
194 * @param end the new end, must be &gt; {@link #start()} 194 * @param end the new end, must be &gt; {@link #start()}
195 * @throws LineIndexOutOfBoundsException if <code>end</code> &lt;= {@link #start()} 195 * @throws LineIndexOutOfBoundsException if <code>end</code> &lt;= {@link #start()}
196 */ 196 */
197 public void setEnd(int end) { 197 public void setEnd(int end) {
198 setLength(end - start()); 198 setLength(end - start());
199 } 199 }
200 200
201 /** 201 /**
202 * Sets the length of this range, keeping {@link #start()} constant. 202 * Sets the length of this range, keeping {@link #start()} constant.
203 * 203 *
204 * @param length the new length, must be &gt; 0 204 * @param length the new length, must be &gt; 0
205 * @throws LineIndexOutOfBoundsException if <code>length</code> &lt;= 0 205 * @throws LineIndexOutOfBoundsException if <code>length</code> &lt;= 0
206 */ 206 */
207 public void setLength(int length) { 207 public void setLength(int length) {
208 if (!(length > 0)) 208 if (!(length > 0))
209 throw new LineIndexOutOfBoundsException("Cannot set length <= 0: " + length); //$NON-NLS-1$ 209 throw new LineIndexOutOfBoundsException(Format("Cannot set length <= 0: {}", length)); //$NON-NLS-1$
210 fLength= length; 210 fLength= length;
211 } 211 }
212 212
213 /** 213 /**
214 * Sets the length of this range, keeping {@link #end()} constant. 214 * Sets the length of this range, keeping {@link #end()} constant.
215 * 215 *
216 * @param length the new length, must be &gt; 0 and &lt;= {@link #end()} 216 * @param length the new length, must be &gt; 0 and &lt;= {@link #end()}
217 * @throws LineIndexOutOfBoundsException if <code>length</code> &lt;= 0 217 * @throws LineIndexOutOfBoundsException if <code>length</code> &lt;= 0
218 */ 218 */
219 public void setLengthAndMove(int length) { 219 public void setLengthAndMove(int length) {
220 setStart(end() - length); 220 setStart(end() - length);
221 } 221 }
222 222
223 /** 223 /**
224 * Resizes the range by <code>delta</code> lines, keeping {@link #start()} constant. 224 * Resizes the range by <code>delta</code> lines, keeping {@link #start()} constant.
225 * 225 *
226 * @param delta the number of lines to resize the range 226 * @param delta the number of lines to resize the range
227 * @throws LineIndexOutOfBoundsException if <code>-delta</code> &gt;= {@link #length()} 227 * @throws LineIndexOutOfBoundsException if <code>-delta</code> &gt;= {@link #length()}
228 */ 228 */
229 public void resizeBy(int delta) { 229 public void resizeBy(int delta) {
230 setLength(length() + delta); 230 setLength(length() + delta);
231 } 231 }
232 232
233 /** 233 /**
234 * Resizes the range by <code>delta</code> lines by moving the start offset, {@link #end()} remains unchanged. 234 * Resizes the range by <code>delta</code> lines by moving the start offset, {@link #end()} remains unchanged.
235 * 235 *
236 * @param delta the number of lines to resize the range 236 * @param delta the number of lines to resize the range
237 * @throws LineIndexOutOfBoundsException if <code>-delta</code> &gt;= {@link #length()} 237 * @throws LineIndexOutOfBoundsException if <code>-delta</code> &gt;= {@link #length()}
238 */ 238 */
239 public void resizeAndMoveBy(int delta) { 239 public void resizeAndMoveBy(int delta) {
240 setStart(start() + delta); 240 setStart(start() + delta);
241 } 241 }
242 242
243 /** 243 /**
244 * Splits a range off the end of the receiver. The receiver is shortened to only include 244 * Splits a range off the end of the receiver. The receiver is shortened to only include
245 * <code>remaining</code> lines after the split. 245 * <code>remaining</code> lines after the split.
246 * 246 *
247 * @param remaining the number of lines to remain in the receiver, must be in [1, {@link #length() length}) 247 * @param remaining the number of lines to remain in the receiver, must be in [1, {@link #length() length})
248 * @return the split off range 248 * @return the split off range
249 * @throws LineIndexOutOfBoundsException if <code>remaining</code>&gt;= {@link #length()} or <code>remaining</code>&ltt;= 0 249 * @throws LineIndexOutOfBoundsException if <code>remaining</code>&gt;= {@link #length()} or <code>remaining</code>&ltt;= 0
250 */ 250 */
251 public Range split(int remaining) { 251 public Range split(int remaining) {
252 if (!(remaining < length())) // assert before modification 252 if (!(remaining < length())) // assert before modification
253 throw new LineIndexOutOfBoundsException("Remaining must be less than length: " + length()); //$NON-NLS-1$ 253 throw new LineIndexOutOfBoundsException(Format("Remaining must be less than length: {}", length())); //$NON-NLS-1$
254 254
255 int splitLength= length() - remaining; 255 int splitLength= length() - remaining;
256 setLength(remaining); 256 setLength(remaining);
257 return new Range(end(), splitLength); 257 return new Range(end(), splitLength);
258 } 258 }
259 259
260 /** 260 /**
261 * Returns <code>true</code> if the passed range has the same offset and length as the receiver. 261 * Returns <code>true</code> if the passed range has the same offset and length as the receiver.
262 * 262 *
263 * @param range another line range to compare the receiver to 263 * @param range another line range to compare the receiver to
264 * @return <code>true</code> if <code>range</code> has the same offset and length as the receiver 264 * @return <code>true</code> if <code>range</code> has the same offset and length as the receiver
265 */ 265 */
266 public bool equalRange(ILineRange range) { 266 public bool equalRange(ILineRange range) {
267 if (range is this) 267 if (range is this)
268 return true; 268 return true;
269 if (range is null) 269 if (range is null)
270 return false; 270 return false;
271 return range.getStartLine() is start() && range.getNumberOfLines() is length(); 271 return range.getStartLine() is start() && range.getNumberOfLines() is length();
272 } 272 }
273 273
274 /* 274 /*
275 * @see java.lang.Object#clone() 275 * @see java.lang.Object#clone()
276 */ 276 */
277 public Object clone() { 277 public Object clone() {
278 return Range.copy(this); 278 return Range.copy(this);