129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 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.text.Position;
|
|
14
|
131
|
15 import dwtx.jface.text.IDocumentPartitioningListener; // packageimport
|
|
16 import dwtx.jface.text.DefaultTextHover; // packageimport
|
|
17 import dwtx.jface.text.AbstractInformationControl; // packageimport
|
|
18 import dwtx.jface.text.TextUtilities; // packageimport
|
|
19 import dwtx.jface.text.IInformationControlCreatorExtension; // packageimport
|
|
20 import dwtx.jface.text.AbstractInformationControlManager; // packageimport
|
|
21 import dwtx.jface.text.ITextViewerExtension2; // packageimport
|
|
22 import dwtx.jface.text.IDocumentPartitioner; // packageimport
|
|
23 import dwtx.jface.text.DefaultIndentLineAutoEditStrategy; // packageimport
|
|
24 import dwtx.jface.text.ITextSelection; // packageimport
|
|
25 import dwtx.jface.text.Document; // packageimport
|
|
26 import dwtx.jface.text.FindReplaceDocumentAdapterContentProposalProvider; // packageimport
|
|
27 import dwtx.jface.text.ITextListener; // packageimport
|
|
28 import dwtx.jface.text.BadPartitioningException; // packageimport
|
|
29 import dwtx.jface.text.ITextViewerExtension5; // packageimport
|
|
30 import dwtx.jface.text.IDocumentPartitionerExtension3; // packageimport
|
|
31 import dwtx.jface.text.IUndoManager; // packageimport
|
|
32 import dwtx.jface.text.ITextHoverExtension2; // packageimport
|
|
33 import dwtx.jface.text.IRepairableDocument; // packageimport
|
|
34 import dwtx.jface.text.IRewriteTarget; // packageimport
|
|
35 import dwtx.jface.text.DefaultPositionUpdater; // packageimport
|
|
36 import dwtx.jface.text.RewriteSessionEditProcessor; // packageimport
|
|
37 import dwtx.jface.text.TextViewerHoverManager; // packageimport
|
|
38 import dwtx.jface.text.DocumentRewriteSession; // packageimport
|
|
39 import dwtx.jface.text.TextViewer; // packageimport
|
|
40 import dwtx.jface.text.ITextViewerExtension8; // packageimport
|
|
41 import dwtx.jface.text.RegExMessages; // packageimport
|
|
42 import dwtx.jface.text.IDelayedInputChangeProvider; // packageimport
|
|
43 import dwtx.jface.text.ITextOperationTargetExtension; // packageimport
|
|
44 import dwtx.jface.text.IWidgetTokenOwner; // packageimport
|
|
45 import dwtx.jface.text.IViewportListener; // packageimport
|
|
46 import dwtx.jface.text.GapTextStore; // packageimport
|
|
47 import dwtx.jface.text.MarkSelection; // packageimport
|
|
48 import dwtx.jface.text.IDocumentPartitioningListenerExtension; // packageimport
|
|
49 import dwtx.jface.text.IDocumentAdapterExtension; // packageimport
|
|
50 import dwtx.jface.text.IInformationControlExtension; // packageimport
|
|
51 import dwtx.jface.text.IDocumentPartitioningListenerExtension2; // packageimport
|
|
52 import dwtx.jface.text.DefaultDocumentAdapter; // packageimport
|
|
53 import dwtx.jface.text.ITextViewerExtension3; // packageimport
|
|
54 import dwtx.jface.text.IInformationControlCreator; // packageimport
|
|
55 import dwtx.jface.text.TypedRegion; // packageimport
|
|
56 import dwtx.jface.text.ISynchronizable; // packageimport
|
|
57 import dwtx.jface.text.IMarkRegionTarget; // packageimport
|
|
58 import dwtx.jface.text.TextViewerUndoManager; // packageimport
|
|
59 import dwtx.jface.text.IRegion; // packageimport
|
|
60 import dwtx.jface.text.IInformationControlExtension2; // packageimport
|
|
61 import dwtx.jface.text.IDocumentExtension4; // packageimport
|
|
62 import dwtx.jface.text.IDocumentExtension2; // packageimport
|
|
63 import dwtx.jface.text.IDocumentPartitionerExtension2; // packageimport
|
|
64 import dwtx.jface.text.Assert; // packageimport
|
|
65 import dwtx.jface.text.DefaultInformationControl; // packageimport
|
|
66 import dwtx.jface.text.IWidgetTokenOwnerExtension; // packageimport
|
|
67 import dwtx.jface.text.DocumentClone; // packageimport
|
|
68 import dwtx.jface.text.DefaultUndoManager; // packageimport
|
|
69 import dwtx.jface.text.IFindReplaceTarget; // packageimport
|
|
70 import dwtx.jface.text.IAutoEditStrategy; // packageimport
|
|
71 import dwtx.jface.text.ILineTrackerExtension; // packageimport
|
|
72 import dwtx.jface.text.IUndoManagerExtension; // packageimport
|
|
73 import dwtx.jface.text.TextSelection; // packageimport
|
|
74 import dwtx.jface.text.DefaultAutoIndentStrategy; // packageimport
|
|
75 import dwtx.jface.text.IAutoIndentStrategy; // packageimport
|
|
76 import dwtx.jface.text.IPainter; // packageimport
|
|
77 import dwtx.jface.text.IInformationControl; // packageimport
|
|
78 import dwtx.jface.text.IInformationControlExtension3; // packageimport
|
|
79 import dwtx.jface.text.ITextViewerExtension6; // packageimport
|
|
80 import dwtx.jface.text.IInformationControlExtension4; // packageimport
|
|
81 import dwtx.jface.text.DefaultLineTracker; // packageimport
|
|
82 import dwtx.jface.text.IDocumentInformationMappingExtension; // packageimport
|
|
83 import dwtx.jface.text.IRepairableDocumentExtension; // packageimport
|
|
84 import dwtx.jface.text.ITextHover; // packageimport
|
|
85 import dwtx.jface.text.FindReplaceDocumentAdapter; // packageimport
|
|
86 import dwtx.jface.text.ILineTracker; // packageimport
|
|
87 import dwtx.jface.text.Line; // packageimport
|
|
88 import dwtx.jface.text.ITextViewerExtension; // packageimport
|
|
89 import dwtx.jface.text.IDocumentAdapter; // packageimport
|
|
90 import dwtx.jface.text.TextEvent; // packageimport
|
|
91 import dwtx.jface.text.BadLocationException; // packageimport
|
|
92 import dwtx.jface.text.AbstractDocument; // packageimport
|
|
93 import dwtx.jface.text.AbstractLineTracker; // packageimport
|
|
94 import dwtx.jface.text.TreeLineTracker; // packageimport
|
|
95 import dwtx.jface.text.ITextPresentationListener; // packageimport
|
|
96 import dwtx.jface.text.Region; // packageimport
|
|
97 import dwtx.jface.text.ITextViewer; // packageimport
|
|
98 import dwtx.jface.text.IDocumentInformationMapping; // packageimport
|
|
99 import dwtx.jface.text.MarginPainter; // packageimport
|
|
100 import dwtx.jface.text.IPaintPositionManager; // packageimport
|
|
101 import dwtx.jface.text.TextPresentation; // packageimport
|
|
102 import dwtx.jface.text.IFindReplaceTargetExtension; // packageimport
|
|
103 import dwtx.jface.text.ISlaveDocumentManagerExtension; // packageimport
|
|
104 import dwtx.jface.text.ISelectionValidator; // packageimport
|
|
105 import dwtx.jface.text.IDocumentExtension; // packageimport
|
|
106 import dwtx.jface.text.PropagatingFontFieldEditor; // packageimport
|
|
107 import dwtx.jface.text.ConfigurableLineTracker; // packageimport
|
|
108 import dwtx.jface.text.SlaveDocumentEvent; // packageimport
|
|
109 import dwtx.jface.text.IDocumentListener; // packageimport
|
|
110 import dwtx.jface.text.PaintManager; // packageimport
|
|
111 import dwtx.jface.text.IFindReplaceTargetExtension3; // packageimport
|
|
112 import dwtx.jface.text.ITextDoubleClickStrategy; // packageimport
|
|
113 import dwtx.jface.text.IDocumentExtension3; // packageimport
|
|
114 import dwtx.jface.text.TextMessages; // packageimport
|
|
115 import dwtx.jface.text.CopyOnWriteTextStore; // packageimport
|
|
116 import dwtx.jface.text.WhitespaceCharacterPainter; // packageimport
|
|
117 import dwtx.jface.text.IPositionUpdater; // packageimport
|
|
118 import dwtx.jface.text.DefaultTextDoubleClickStrategy; // packageimport
|
|
119 import dwtx.jface.text.ListLineTracker; // packageimport
|
|
120 import dwtx.jface.text.ITextInputListener; // packageimport
|
|
121 import dwtx.jface.text.BadPositionCategoryException; // packageimport
|
|
122 import dwtx.jface.text.IWidgetTokenKeeperExtension; // packageimport
|
|
123 import dwtx.jface.text.IInputChangedListener; // packageimport
|
|
124 import dwtx.jface.text.ITextOperationTarget; // packageimport
|
|
125 import dwtx.jface.text.IDocumentInformationMappingExtension2; // packageimport
|
|
126 import dwtx.jface.text.ITextViewerExtension7; // packageimport
|
|
127 import dwtx.jface.text.IInformationControlExtension5; // packageimport
|
|
128 import dwtx.jface.text.IDocumentRewriteSessionListener; // packageimport
|
|
129 import dwtx.jface.text.JFaceTextUtil; // packageimport
|
|
130 import dwtx.jface.text.AbstractReusableInformationControlCreator; // packageimport
|
|
131 import dwtx.jface.text.TabsToSpacesConverter; // packageimport
|
|
132 import dwtx.jface.text.CursorLinePainter; // packageimport
|
|
133 import dwtx.jface.text.ITextHoverExtension; // packageimport
|
|
134 import dwtx.jface.text.IEventConsumer; // packageimport
|
|
135 import dwtx.jface.text.IDocument; // packageimport
|
|
136 import dwtx.jface.text.IWidgetTokenKeeper; // packageimport
|
|
137 import dwtx.jface.text.DocumentCommand; // packageimport
|
|
138 import dwtx.jface.text.TypedPosition; // packageimport
|
|
139 import dwtx.jface.text.IEditingSupportRegistry; // packageimport
|
|
140 import dwtx.jface.text.IDocumentPartitionerExtension; // packageimport
|
|
141 import dwtx.jface.text.AbstractHoverInformationControlManager; // packageimport
|
|
142 import dwtx.jface.text.IEditingSupport; // packageimport
|
|
143 import dwtx.jface.text.IMarkSelection; // packageimport
|
|
144 import dwtx.jface.text.ISlaveDocumentManager; // packageimport
|
|
145 import dwtx.jface.text.DocumentEvent; // packageimport
|
|
146 import dwtx.jface.text.DocumentPartitioningChangedEvent; // packageimport
|
|
147 import dwtx.jface.text.ITextStore; // packageimport
|
|
148 import dwtx.jface.text.JFaceTextMessages; // packageimport
|
|
149 import dwtx.jface.text.DocumentRewriteSessionEvent; // packageimport
|
|
150 import dwtx.jface.text.SequentialRewriteTextStore; // packageimport
|
|
151 import dwtx.jface.text.DocumentRewriteSessionType; // packageimport
|
|
152 import dwtx.jface.text.TextAttribute; // packageimport
|
|
153 import dwtx.jface.text.ITextViewerExtension4; // packageimport
|
|
154 import dwtx.jface.text.ITypedRegion; // packageimport
|
|
155
|
|
156
|
129
|
157 import dwt.dwthelper.utils;
|
|
158
|
|
159 import dwtx.core.runtime.Assert;
|
|
160
|
|
161
|
|
162 /**
|
|
163 * Positions describe text ranges of a document. Positions are adapted to
|
|
164 * changes applied to that document. The text range is specified by an offset
|
|
165 * and a length. Positions can be marked as deleted. Deleted positions are
|
|
166 * considered to no longer represent a valid text range in the managing
|
|
167 * document.
|
|
168 * <p>
|
|
169 * Positions attached to documents are usually updated by position updaters.
|
|
170 * Because position updaters are freely definable and because of the frequency
|
|
171 * in which they are used, the fields of a position are made publicly
|
|
172 * accessible. Clients other than position updaters are not allowed to access
|
|
173 * these public fields.
|
|
174 * </p>
|
|
175 * <p>
|
|
176 * Positions cannot be used as keys in hash tables as they override
|
|
177 * <code>equals</code> and <code>hashCode</code> as they would be value
|
|
178 * objects.
|
|
179 * </p>
|
|
180 *
|
|
181 * @see dwtx.jface.text.IDocument
|
|
182 */
|
|
183 public class Position {
|
|
184
|
|
185 /** The offset of the position */
|
|
186 public int offset;
|
|
187 /** The length of the position */
|
|
188 public int length;
|
|
189 /** Indicates whether the position has been deleted */
|
|
190 public bool isDeleted;
|
|
191
|
|
192 /**
|
|
193 * Creates a new position with the given offset and length 0.
|
|
194 *
|
|
195 * @param offset the position offset, must be >= 0
|
|
196 */
|
|
197 public Position(int offset) {
|
|
198 this(offset, 0);
|
|
199 }
|
|
200
|
|
201 /**
|
|
202 * Creates a new position with the given offset and length.
|
|
203 *
|
|
204 * @param offset the position offset, must be >= 0
|
|
205 * @param length the position length, must be >= 0
|
|
206 */
|
|
207 public Position(int offset, int length) {
|
|
208 Assert.isTrue(offset >= 0);
|
|
209 Assert.isTrue(length >= 0);
|
|
210 this.offset= offset;
|
|
211 this.length= length;
|
|
212 }
|
|
213
|
|
214 /**
|
|
215 * Creates a new, not initialized position.
|
|
216 */
|
|
217 protected Position() {
|
|
218 }
|
|
219
|
|
220 /*
|
|
221 * @see java.lang.Object#hashCode()
|
|
222 */
|
|
223 public int hashCode() {
|
|
224 int deleted= isDeleted ? 0 : 1;
|
|
225 return (offset << 24) | (length << 16) | deleted;
|
|
226 }
|
|
227
|
|
228 /**
|
|
229 * Marks this position as deleted.
|
|
230 */
|
|
231 public void delete() {
|
|
232 isDeleted= true;
|
|
233 }
|
|
234
|
|
235 /**
|
|
236 * Marks this position as not deleted.
|
|
237 *
|
|
238 * @since 2.0
|
|
239 */
|
|
240 public void undelete() {
|
|
241 isDeleted= false;
|
|
242 }
|
|
243
|
|
244 /*
|
|
245 * @see java.lang.Object#equals(java.lang.Object)
|
|
246 */
|
|
247 public bool equals(Object other) {
|
|
248 if (other instanceof Position) {
|
|
249 Position rp= (Position) other;
|
|
250 return (rp.offset is offset) && (rp.length is length);
|
|
251 }
|
|
252 return super.equals(other);
|
|
253 }
|
|
254
|
|
255 /**
|
|
256 * Returns the length of this position.
|
|
257 *
|
|
258 * @return the length of this position
|
|
259 */
|
|
260 public int getLength() {
|
|
261 return length;
|
|
262 }
|
|
263
|
|
264 /**
|
|
265 * Returns the offset of this position.
|
|
266 *
|
|
267 * @return the offset of this position
|
|
268 */
|
|
269 public int getOffset() {
|
|
270 return offset;
|
|
271 }
|
|
272
|
|
273 /**
|
|
274 * Checks whether the given index is inside
|
|
275 * of this position's text range.
|
|
276 *
|
|
277 * @param index the index to check
|
|
278 * @return <code>true</code> if <code>index</code> is inside of this position
|
|
279 */
|
|
280 public bool includes(int index) {
|
|
281
|
|
282 if (isDeleted)
|
|
283 return false;
|
|
284
|
|
285 return (this.offset <= index) && (index < this.offset + length);
|
|
286 }
|
|
287
|
|
288 /**
|
|
289 * Checks whether the intersection of the given text range
|
|
290 * and the text range represented by this position is empty
|
|
291 * or not.
|
|
292 *
|
|
293 * @param rangeOffset the offset of the range to check
|
|
294 * @param rangeLength the length of the range to check
|
|
295 * @return <code>true</code> if intersection is not empty
|
|
296 */
|
|
297 public bool overlapsWith(int rangeOffset, int rangeLength) {
|
|
298
|
|
299 if (isDeleted)
|
|
300 return false;
|
|
301
|
|
302 int end= rangeOffset + rangeLength;
|
|
303 int thisEnd= this.offset + this.length;
|
|
304
|
|
305 if (rangeLength > 0) {
|
|
306 if (this.length > 0)
|
|
307 return this.offset < end && rangeOffset < thisEnd;
|
|
308 return rangeOffset <= this.offset && this.offset < end;
|
|
309 }
|
|
310
|
|
311 if (this.length > 0)
|
|
312 return this.offset <= rangeOffset && rangeOffset < thisEnd;
|
|
313 return this.offset is rangeOffset;
|
|
314 }
|
|
315
|
|
316 /**
|
|
317 * Returns whether this position has been deleted or not.
|
|
318 *
|
|
319 * @return <code>true</code> if position has been deleted
|
|
320 */
|
|
321 public bool isDeleted() {
|
|
322 return isDeleted;
|
|
323 }
|
|
324
|
|
325 /**
|
|
326 * Changes the length of this position to the given length.
|
|
327 *
|
|
328 * @param length the new length of this position
|
|
329 */
|
|
330 public void setLength(int length) {
|
|
331 Assert.isTrue(length >= 0);
|
|
332 this.length= length;
|
|
333 }
|
|
334
|
|
335 /**
|
|
336 * Changes the offset of this position to the given offset.
|
|
337 *
|
|
338 * @param offset the new offset of this position
|
|
339 */
|
|
340 public void setOffset(int offset) {
|
|
341 Assert.isTrue(offset >= 0);
|
|
342 this.offset= offset;
|
|
343 }
|
|
344 }
|