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