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