129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.internal.text.revisions.Range;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import dwtx.jface.text.source.ILineRange;
|
|
18
|
|
19 /**
|
|
20 * A variable {@link dwtx.jface.text.source.ILineRange} with the following invariant:
|
|
21 * <ul>
|
|
22 * <li>{@link #start() start} >= 0
|
|
23 * <li>{@link #length() length} > 0, i.e. a range cannot be empty
|
|
24 * </ul>
|
|
25 * <p>
|
|
26 * Attempts to create or modify a <code>Range</code> such that this invariant would be violated
|
|
27 * result in a {@link LineIndexOutOfBoundsException} being
|
|
28 * thrown.
|
|
29 * </p>
|
|
30 *
|
|
31 * @since 3.2
|
|
32 */
|
|
33 public final class Range : ILineRange, Cloneable {
|
|
34 /**
|
|
35 * Creates a new range with the same start and length as the passed line range.
|
|
36 *
|
|
37 * @param range the range to copy
|
|
38 * @return a <code>Range</code> with the same start and length as <code>range</code>
|
|
39 * @throws LineIndexOutOfBoundsException if the passed {@link ILineRange} does not adhere to the
|
|
40 * contract of {@link Range}
|
|
41 */
|
|
42 public static Range copy(ILineRange range) throws LineIndexOutOfBoundsException {
|
|
43 return createRelative(range.getStartLine(), range.getNumberOfLines());
|
|
44 }
|
|
45
|
|
46 /**
|
|
47 * Creates a new range equal to the passed line range.
|
|
48 *
|
|
49 * @param range the range to copy
|
|
50 * @return a <code>Range</code> equal to <code>range</code>
|
|
51 */
|
|
52 public static Range copy(Range range) {
|
|
53 return createRelative(range.start(), range.length());
|
|
54 }
|
|
55
|
|
56 /**
|
|
57 * Creates a new range with the given start offset and length.
|
|
58 *
|
|
59 * @param start the first line of the new range, must be >= 0
|
|
60 * @param length the number of lines included in the new range, must be > 0
|
|
61 * @return a <code>Range</code> with the given start and length
|
|
62 * @throws LineIndexOutOfBoundsException if the parameters violate the invariant of
|
|
63 * {@link Range}
|
|
64 */
|
|
65 public static Range createRelative(int start, int length) throws LineIndexOutOfBoundsException {
|
|
66 return new Range(start, length);
|
|
67 }
|
|
68
|
|
69 /**
|
|
70 * Creates a new range with the given start and end offsets.
|
|
71 *
|
|
72 * @param start the first line of the new range, must be >= 0
|
|
73 * @param end the first line not in the range any more (exclusive), must be > <code>start</code>
|
|
74 * @return a <code>Range</code> with the given start and end offsets
|
|
75 * @throws LineIndexOutOfBoundsException if the parameters violate the invariant of
|
|
76 * {@link Range}
|
|
77 */
|
|
78 public static Range createAbsolute(int start, int end) {
|
|
79 return new Range(start, end - start);
|
|
80 }
|
|
81
|
|
82 private int fStart;
|
|
83 private int fLength;
|
|
84
|
|
85 /*
|
|
86 * Private constructor.
|
|
87 */
|
|
88 private Range(int start, int length) {
|
|
89 moveTo(start);
|
|
90 setLength(length);
|
|
91 }
|
|
92
|
|
93 /*
|
|
94 * @see dwtx.jface.text.source.ILineRange#getStartLine()
|
|
95 */
|
|
96 public int getStartLine() {
|
|
97 return start();
|
|
98 }
|
|
99
|
|
100 /*
|
|
101 * @see dwtx.jface.text.source.ILineRange#getNumberOfLines()
|
|
102 */
|
|
103 public int getNumberOfLines() {
|
|
104 return length();
|
|
105 }
|
|
106
|
|
107 /**
|
|
108 * Returns the first line contained in this range. Short equivalent of {@link #getStartLine()}.
|
|
109 *
|
|
110 * @return the first line contained in this range
|
|
111 */
|
|
112 public int start() {
|
|
113 return fStart;
|
|
114 }
|
|
115
|
|
116 /**
|
|
117 * Returns the number of lines contained in this range. Short equivalent of {@link #getNumberOfLines()}.
|
|
118 *
|
|
119 * @return the number of lines contained in this range
|
|
120 */
|
|
121 public int length() {
|
|
122 return fLength;
|
|
123 }
|
|
124
|
|
125 /**
|
|
126 * Returns the first line after this range. Equivalent to {@linkplain #start() start} + {@linkplain #length() length}.
|
|
127 *
|
|
128 * @return the first line after this range
|
|
129 */
|
|
130 public int end() {
|
|
131 return start() + length();
|
|
132 }
|
|
133
|
|
134 /**
|
|
135 * Moves the receiver to <code>start</code>, keeping {@link #length()} constant.
|
|
136 *
|
|
137 * @param start the new start, must be >= 0
|
|
138 * @throws LineIndexOutOfBoundsException if <code>start</code> < 0
|
|
139 */
|
|
140 public void moveTo(int start) throws LineIndexOutOfBoundsException {
|
|
141 if (!(start >= 0))
|
|
142 throw new LineIndexOutOfBoundsException("Cannot set a negative start: " + start); //$NON-NLS-1$
|
|
143 fStart= start;
|
|
144 }
|
|
145
|
|
146 /**
|
|
147 * Moves this range such that the {@link #end()} is at <code>end</code>, keeping
|
|
148 * {@link #length()} constant.
|
|
149 *
|
|
150 * @param end the new end
|
|
151 * @throws LineIndexOutOfBoundsException if <code>end</code> <= {@link #start()}
|
|
152 */
|
|
153 public void moveEndTo(int end) throws LineIndexOutOfBoundsException {
|
|
154 moveTo(end - length());
|
|
155 }
|
|
156
|
|
157 /**
|
|
158 * Moves the range by <code>delta</code> lines, keeping {@link #length()} constant. The
|
|
159 * resulting start line must be >= 0.
|
|
160 *
|
|
161 * @param delta the number of lines to shift the range
|
|
162 * @throws LineIndexOutOfBoundsException if <code>-delta</code> > {@link #start()}
|
|
163 */
|
|
164 public void moveBy(int delta) throws LineIndexOutOfBoundsException {
|
|
165 moveTo(start() + delta);
|
|
166 }
|
|
167
|
|
168 /**
|
|
169 * Moves the start offset to <code>start</code>, keeping {@link #end()} constant.
|
|
170 *
|
|
171 * @param start the new start, must be >= 0 and < {@link #end()}
|
|
172 * @throws LineIndexOutOfBoundsException if <code>start</code> < 0 or >= {@link #end()}
|
|
173 */
|
|
174 public void setStart(int start) throws LineIndexOutOfBoundsException {
|
|
175 int end= end();
|
|
176 if (!(start >= 0 && start < end))
|
|
177 throw new LineIndexOutOfBoundsException("Cannot set a negative start: " + start); //$NON-NLS-1$
|
|
178 moveTo(start);
|
|
179 setEnd(end);
|
|
180 }
|
|
181
|
|
182 /**
|
|
183 * Sets the end of this range, keeping {@link #start()} constant.
|
|
184 *
|
|
185 * @param end the new end, must be > {@link #start()}
|
|
186 * @throws LineIndexOutOfBoundsException if <code>end</code> <= {@link #start()}
|
|
187 */
|
|
188 public void setEnd(int end) throws LineIndexOutOfBoundsException {
|
|
189 setLength(end - start());
|
|
190 }
|
|
191
|
|
192 /**
|
|
193 * Sets the length of this range, keeping {@link #start()} constant.
|
|
194 *
|
|
195 * @param length the new length, must be > 0
|
|
196 * @throws LineIndexOutOfBoundsException if <code>length</code> <= 0
|
|
197 */
|
|
198 public void setLength(int length) throws LineIndexOutOfBoundsException {
|
|
199 if (!(length > 0))
|
|
200 throw new LineIndexOutOfBoundsException("Cannot set length <= 0: " + length); //$NON-NLS-1$
|
|
201 fLength= length;
|
|
202 }
|
|
203
|
|
204 /**
|
|
205 * Sets the length of this range, keeping {@link #end()} constant.
|
|
206 *
|
|
207 * @param length the new length, must be > 0 and <= {@link #end()}
|
|
208 * @throws LineIndexOutOfBoundsException if <code>length</code> <= 0
|
|
209 */
|
|
210 public void setLengthAndMove(int length) throws LineIndexOutOfBoundsException {
|
|
211 setStart(end() - length);
|
|
212 }
|
|
213
|
|
214 /**
|
|
215 * Resizes the range by <code>delta</code> lines, keeping {@link #start()} constant.
|
|
216 *
|
|
217 * @param delta the number of lines to resize the range
|
|
218 * @throws LineIndexOutOfBoundsException if <code>-delta</code> >= {@link #length()}
|
|
219 */
|
|
220 public void resizeBy(int delta) throws LineIndexOutOfBoundsException {
|
|
221 setLength(length() + delta);
|
|
222 }
|
|
223
|
|
224 /**
|
|
225 * Resizes the range by <code>delta</code> lines by moving the start offset, {@link #end()} remains unchanged.
|
|
226 *
|
|
227 * @param delta the number of lines to resize the range
|
|
228 * @throws LineIndexOutOfBoundsException if <code>-delta</code> >= {@link #length()}
|
|
229 */
|
|
230 public void resizeAndMoveBy(int delta) throws LineIndexOutOfBoundsException {
|
|
231 setStart(start() + delta);
|
|
232 }
|
|
233
|
|
234 /**
|
|
235 * Splits a range off the end of the receiver. The receiver is shortened to only include
|
|
236 * <code>remaining</code> lines after the split.
|
|
237 *
|
|
238 * @param remaining the number of lines to remain in the receiver, must be in [1, {@link #length() length})
|
|
239 * @return the split off range
|
|
240 * @throws LineIndexOutOfBoundsException if <code>remaining</code>>= {@link #length()} or <code>remaining</code><t;= 0
|
|
241 */
|
|
242 public Range split(int remaining) throws LineIndexOutOfBoundsException {
|
|
243 if (!(remaining < length())) // assert before modification
|
|
244 throw new LineIndexOutOfBoundsException("Remaining must be less than length: " + length()); //$NON-NLS-1$
|
|
245
|
|
246 int splitLength= length() - remaining;
|
|
247 setLength(remaining);
|
|
248 return new Range(end(), splitLength);
|
|
249 }
|
|
250
|
|
251 /**
|
|
252 * Returns <code>true</code> if the passed range has the same offset and length as the receiver.
|
|
253 *
|
|
254 * @param range another line range to compare the receiver to
|
|
255 * @return <code>true</code> if <code>range</code> has the same offset and length as the receiver
|
|
256 */
|
|
257 public bool equalRange(ILineRange range) {
|
|
258 if (range is this)
|
|
259 return true;
|
|
260 if (range is null)
|
|
261 return false;
|
|
262 return range.getStartLine() is start() && range.getNumberOfLines() is length();
|
|
263 }
|
|
264
|
|
265 /*
|
|
266 * @see java.lang.Object#clone()
|
|
267 */
|
|
268 public Object clone() {
|
|
269 return Range.copy(this);
|
|
270 }
|
|
271 }
|