Mercurial > projects > dwt-addons
annotate dwtx/jface/internal/text/revisions/Range.d @ 192:c3583c6ec027
Added missing default cases for switch statements
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 03 Nov 2008 22:52:26 +0100 |
parents | 1a5b8f8129df |
children |
rev | line source |
---|---|
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 | |
129 | 23 import dwt.dwthelper.utils; |
162 | 24 import tango.text.convert.Format; |
129 | 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> | |
162 | 39 * |
129 | 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. | |
162 | 45 * |
129 | 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 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
51 public static Range copy(ILineRange range) { |
129 | 52 return createRelative(range.getStartLine(), range.getNumberOfLines()); |
53 } | |
162 | 54 |
129 | 55 /** |
56 * Creates a new range equal to the passed line range. | |
162 | 57 * |
129 | 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 } | |
162 | 64 |
129 | 65 /** |
66 * Creates a new range with the given start offset and length. | |
162 | 67 * |
129 | 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 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
74 public static Range createRelative(int start, int length) { |
129 | 75 return new Range(start, length); |
76 } | |
77 | |
78 /** | |
79 * Creates a new range with the given start and end offsets. | |
162 | 80 * |
129 | 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 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
97 private this(int start, int length) { |
129 | 98 moveTo(start); |
99 setLength(length); | |
100 } | |
162 | 101 |
129 | 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 } | |
162 | 115 |
129 | 116 /** |
117 * Returns the first line contained in this range. Short equivalent of {@link #getStartLine()}. | |
162 | 118 * |
129 | 119 * @return the first line contained in this range |
120 */ | |
121 public int start() { | |
122 return fStart; | |
123 } | |
162 | 124 |
129 | 125 /** |
126 * Returns the number of lines contained in this range. Short equivalent of {@link #getNumberOfLines()}. | |
162 | 127 * |
129 | 128 * @return the number of lines contained in this range |
129 */ | |
130 public int length() { | |
131 return fLength; | |
132 } | |
162 | 133 |
129 | 134 /** |
135 * Returns the first line after this range. Equivalent to {@linkplain #start() start} + {@linkplain #length() length}. | |
162 | 136 * |
129 | 137 * @return the first line after this range |
138 */ | |
139 public int end() { | |
140 return start() + length(); | |
141 } | |
162 | 142 |
129 | 143 /** |
144 * Moves the receiver to <code>start</code>, keeping {@link #length()} constant. | |
162 | 145 * |
129 | 146 * @param start the new start, must be >= 0 |
147 * @throws LineIndexOutOfBoundsException if <code>start</code> < 0 | |
148 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
149 public void moveTo(int start) { |
129 | 150 if (!(start >= 0)) |
162 | 151 throw new LineIndexOutOfBoundsException(Format("Cannot set a negative start: {}", start)); //$NON-NLS-1$ |
129 | 152 fStart= start; |
153 } | |
162 | 154 |
129 | 155 /** |
156 * Moves this range such that the {@link #end()} is at <code>end</code>, keeping | |
157 * {@link #length()} constant. | |
162 | 158 * |
129 | 159 * @param end the new end |
160 * @throws LineIndexOutOfBoundsException if <code>end</code> <= {@link #start()} | |
161 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
162 public void moveEndTo(int end) { |
129 | 163 moveTo(end - length()); |
164 } | |
162 | 165 |
129 | 166 /** |
167 * Moves the range by <code>delta</code> lines, keeping {@link #length()} constant. The | |
168 * resulting start line must be >= 0. | |
162 | 169 * |
129 | 170 * @param delta the number of lines to shift the range |
171 * @throws LineIndexOutOfBoundsException if <code>-delta</code> > {@link #start()} | |
172 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
173 public void moveBy(int delta) { |
129 | 174 moveTo(start() + delta); |
175 } | |
162 | 176 |
129 | 177 /** |
178 * Moves the start offset to <code>start</code>, keeping {@link #end()} constant. | |
162 | 179 * |
129 | 180 * @param start the new start, must be >= 0 and < {@link #end()} |
181 * @throws LineIndexOutOfBoundsException if <code>start</code> < 0 or >= {@link #end()} | |
182 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
183 public void setStart(int start) { |
129 | 184 int end= end(); |
185 if (!(start >= 0 && start < end)) | |
162 | 186 throw new LineIndexOutOfBoundsException(Format("Cannot set a negative start: {}", start)); //$NON-NLS-1$ |
129 | 187 moveTo(start); |
188 setEnd(end); | |
189 } | |
162 | 190 |
129 | 191 /** |
192 * Sets the end of this range, keeping {@link #start()} constant. | |
162 | 193 * |
129 | 194 * @param end the new end, must be > {@link #start()} |
195 * @throws LineIndexOutOfBoundsException if <code>end</code> <= {@link #start()} | |
196 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
197 public void setEnd(int end) { |
129 | 198 setLength(end - start()); |
199 } | |
162 | 200 |
129 | 201 /** |
202 * Sets the length of this range, keeping {@link #start()} constant. | |
162 | 203 * |
129 | 204 * @param length the new length, must be > 0 |
205 * @throws LineIndexOutOfBoundsException if <code>length</code> <= 0 | |
206 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
207 public void setLength(int length) { |
129 | 208 if (!(length > 0)) |
162 | 209 throw new LineIndexOutOfBoundsException(Format("Cannot set length <= 0: {}", length)); //$NON-NLS-1$ |
129 | 210 fLength= length; |
211 } | |
162 | 212 |
129 | 213 /** |
214 * Sets the length of this range, keeping {@link #end()} constant. | |
162 | 215 * |
129 | 216 * @param length the new length, must be > 0 and <= {@link #end()} |
217 * @throws LineIndexOutOfBoundsException if <code>length</code> <= 0 | |
218 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
219 public void setLengthAndMove(int length) { |
129 | 220 setStart(end() - length); |
221 } | |
162 | 222 |
129 | 223 /** |
224 * Resizes the range by <code>delta</code> lines, keeping {@link #start()} constant. | |
162 | 225 * |
129 | 226 * @param delta the number of lines to resize the range |
162 | 227 * @throws LineIndexOutOfBoundsException if <code>-delta</code> >= {@link #length()} |
129 | 228 */ |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
229 public void resizeBy(int delta) { |
129 | 230 setLength(length() + delta); |
231 } | |
162 | 232 |
129 | 233 /** |
234 * Resizes the range by <code>delta</code> lines by moving the start offset, {@link #end()} remains unchanged. | |
162 | 235 * |
129 | 236 * @param delta the number of lines to resize the range |
162 | 237 * @throws LineIndexOutOfBoundsException if <code>-delta</code> >= {@link #length()} |
129 | 238 */ |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
239 public void resizeAndMoveBy(int delta) { |
129 | 240 setStart(start() + delta); |
241 } | |
162 | 242 |
129 | 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. | |
162 | 246 * |
129 | 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 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
251 public Range split(int remaining) { |
129 | 252 if (!(remaining < length())) // assert before modification |
162 | 253 throw new LineIndexOutOfBoundsException(Format("Remaining must be less than length: {}", length())); //$NON-NLS-1$ |
129 | 254 |
255 int splitLength= length() - remaining; | |
256 setLength(remaining); | |
257 return new Range(end(), splitLength); | |
258 } | |
162 | 259 |
129 | 260 /** |
261 * Returns <code>true</code> if the passed range has the same offset and length as the receiver. | |
162 | 262 * |
129 | 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 } | |
162 | 273 |
129 | 274 /* |
275 * @see java.lang.Object#clone() | |
276 */ | |
277 public Object clone() { | |
278 return Range.copy(this); | |
279 } | |
280 } |