Mercurial > projects > dwt-addons
annotate dwtx/jface/text/DefaultPositionUpdater.d @ 151:eb21d3dfc767
fix module statement
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 24 Aug 2008 23:55:45 +0200 |
parents | 26688fec6d23 |
children | f8d52b926852 |
rev | line source |
---|---|
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.DefaultPositionUpdater; | |
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.RewriteSessionEditProcessor; // packageimport | |
36 import dwtx.jface.text.TextViewerHoverManager; // packageimport | |
37 import dwtx.jface.text.DocumentRewriteSession; // packageimport | |
38 import dwtx.jface.text.TextViewer; // packageimport | |
39 import dwtx.jface.text.ITextViewerExtension8; // packageimport | |
40 import dwtx.jface.text.RegExMessages; // packageimport | |
41 import dwtx.jface.text.IDelayedInputChangeProvider; // packageimport | |
42 import dwtx.jface.text.ITextOperationTargetExtension; // packageimport | |
43 import dwtx.jface.text.IWidgetTokenOwner; // packageimport | |
44 import dwtx.jface.text.IViewportListener; // packageimport | |
45 import dwtx.jface.text.GapTextStore; // packageimport | |
46 import dwtx.jface.text.MarkSelection; // packageimport | |
47 import dwtx.jface.text.IDocumentPartitioningListenerExtension; // packageimport | |
48 import dwtx.jface.text.IDocumentAdapterExtension; // packageimport | |
49 import dwtx.jface.text.IInformationControlExtension; // packageimport | |
50 import dwtx.jface.text.IDocumentPartitioningListenerExtension2; // packageimport | |
51 import dwtx.jface.text.DefaultDocumentAdapter; // packageimport | |
52 import dwtx.jface.text.ITextViewerExtension3; // packageimport | |
53 import dwtx.jface.text.IInformationControlCreator; // packageimport | |
54 import dwtx.jface.text.TypedRegion; // packageimport | |
55 import dwtx.jface.text.ISynchronizable; // packageimport | |
56 import dwtx.jface.text.IMarkRegionTarget; // packageimport | |
57 import dwtx.jface.text.TextViewerUndoManager; // packageimport | |
58 import dwtx.jface.text.IRegion; // packageimport | |
59 import dwtx.jface.text.IInformationControlExtension2; // packageimport | |
60 import dwtx.jface.text.IDocumentExtension4; // packageimport | |
61 import dwtx.jface.text.IDocumentExtension2; // packageimport | |
62 import dwtx.jface.text.IDocumentPartitionerExtension2; // packageimport | |
63 import dwtx.jface.text.Assert; // packageimport | |
64 import dwtx.jface.text.DefaultInformationControl; // packageimport | |
65 import dwtx.jface.text.IWidgetTokenOwnerExtension; // packageimport | |
66 import dwtx.jface.text.DocumentClone; // packageimport | |
67 import dwtx.jface.text.DefaultUndoManager; // packageimport | |
68 import dwtx.jface.text.IFindReplaceTarget; // packageimport | |
69 import dwtx.jface.text.IAutoEditStrategy; // packageimport | |
70 import dwtx.jface.text.ILineTrackerExtension; // packageimport | |
71 import dwtx.jface.text.IUndoManagerExtension; // packageimport | |
72 import dwtx.jface.text.TextSelection; // packageimport | |
73 import dwtx.jface.text.DefaultAutoIndentStrategy; // packageimport | |
74 import dwtx.jface.text.IAutoIndentStrategy; // packageimport | |
75 import dwtx.jface.text.IPainter; // packageimport | |
76 import dwtx.jface.text.IInformationControl; // packageimport | |
77 import dwtx.jface.text.IInformationControlExtension3; // packageimport | |
78 import dwtx.jface.text.ITextViewerExtension6; // packageimport | |
79 import dwtx.jface.text.IInformationControlExtension4; // packageimport | |
80 import dwtx.jface.text.DefaultLineTracker; // packageimport | |
81 import dwtx.jface.text.IDocumentInformationMappingExtension; // packageimport | |
82 import dwtx.jface.text.IRepairableDocumentExtension; // packageimport | |
83 import dwtx.jface.text.ITextHover; // packageimport | |
84 import dwtx.jface.text.FindReplaceDocumentAdapter; // packageimport | |
85 import dwtx.jface.text.ILineTracker; // packageimport | |
86 import dwtx.jface.text.Line; // packageimport | |
87 import dwtx.jface.text.ITextViewerExtension; // packageimport | |
88 import dwtx.jface.text.IDocumentAdapter; // packageimport | |
89 import dwtx.jface.text.TextEvent; // packageimport | |
90 import dwtx.jface.text.BadLocationException; // packageimport | |
91 import dwtx.jface.text.AbstractDocument; // packageimport | |
92 import dwtx.jface.text.AbstractLineTracker; // packageimport | |
93 import dwtx.jface.text.TreeLineTracker; // packageimport | |
94 import dwtx.jface.text.ITextPresentationListener; // packageimport | |
95 import dwtx.jface.text.Region; // packageimport | |
96 import dwtx.jface.text.ITextViewer; // packageimport | |
97 import dwtx.jface.text.IDocumentInformationMapping; // packageimport | |
98 import dwtx.jface.text.MarginPainter; // packageimport | |
99 import dwtx.jface.text.IPaintPositionManager; // packageimport | |
100 import dwtx.jface.text.TextPresentation; // packageimport | |
101 import dwtx.jface.text.IFindReplaceTargetExtension; // packageimport | |
102 import dwtx.jface.text.ISlaveDocumentManagerExtension; // packageimport | |
103 import dwtx.jface.text.ISelectionValidator; // packageimport | |
104 import dwtx.jface.text.IDocumentExtension; // packageimport | |
105 import dwtx.jface.text.PropagatingFontFieldEditor; // packageimport | |
106 import dwtx.jface.text.ConfigurableLineTracker; // packageimport | |
107 import dwtx.jface.text.SlaveDocumentEvent; // packageimport | |
108 import dwtx.jface.text.IDocumentListener; // packageimport | |
109 import dwtx.jface.text.PaintManager; // packageimport | |
110 import dwtx.jface.text.IFindReplaceTargetExtension3; // packageimport | |
111 import dwtx.jface.text.ITextDoubleClickStrategy; // packageimport | |
112 import dwtx.jface.text.IDocumentExtension3; // packageimport | |
113 import dwtx.jface.text.Position; // 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 | |
160 /** | |
161 * Default implementation of {@link dwtx.jface.text.IPositionUpdater}. | |
162 * <p> | |
163 * A default position updater must be configured with the position category whose positions it will | |
164 * update. Other position categories are not affected by this updater. | |
165 * </p> | |
166 * <p> | |
167 * This implementation follows the specification below: | |
168 * </p> | |
169 * <ul> | |
170 * <li>Inserting or deleting text before the position shifts the position accordingly.</li> | |
171 * <li>Inserting text at the position offset shifts the position accordingly.</li> | |
172 * <li>Inserting or deleting text strictly contained by the position shrinks or stretches the | |
173 * position.</li> | |
174 * <li>Inserting or deleting text after a position does not affect the position.</li> | |
175 * <li>Deleting text which strictly contains the position deletes the position. Note that the | |
176 * position is not deleted if its only shrunken to length zero. To delete a position, the | |
177 * modification must delete from <i>strictly before</i> to <i>strictly after</i> the position.</li> | |
178 * <li>Replacing text overlapping with the position is considered as a sequence of first deleting | |
179 * the replaced text and afterwards inserting the new text. Thus, a position might first be shifted | |
180 * and shrunken and then be stretched.</li> | |
181 * </ul> | |
182 * This class can be used as is or be adapted by subclasses. Fields are protected to allow | |
183 * subclasses direct access. Because of the frequency with which position updaters are used this is | |
184 * a performance decision. | |
185 */ | |
186 public class DefaultPositionUpdater : IPositionUpdater { | |
187 | |
188 /** The position category the updater draws responsible for */ | |
189 private String fCategory; | |
190 | |
191 /** Caches the currently investigated position */ | |
192 protected Position fPosition; | |
193 /** | |
194 * Remembers the original state of the investigated position | |
195 * @since 2.1 | |
196 */ | |
197 protected Position fOriginalPosition= new Position(0, 0); | |
198 /** Caches the offset of the replaced text */ | |
199 protected int fOffset; | |
200 /** Caches the length of the replaced text */ | |
201 protected int fLength; | |
202 /** Caches the length of the newly inserted text */ | |
203 protected int fReplaceLength; | |
204 /** Catches the document */ | |
205 protected IDocument fDocument; | |
206 | |
207 | |
208 /** | |
209 * Creates a new default position updater for the given category. | |
210 * | |
211 * @param category the category the updater is responsible for | |
212 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
213 public this(String category) { |
129 | 214 fCategory= category; |
215 } | |
216 | |
217 /** | |
218 * Returns the category this updater is responsible for. | |
219 * | |
220 * @return the category this updater is responsible for | |
221 */ | |
222 protected String getCategory() { | |
223 return fCategory; | |
224 } | |
225 | |
226 /** | |
227 * Returns whether the current event describes a well formed replace | |
228 * by which the current position is directly affected. | |
229 * | |
230 * @return <code>true</code> the current position is directly affected | |
231 * @since 3.0 | |
232 */ | |
233 protected bool isAffectingReplace() { | |
234 return fLength > 0 && fReplaceLength > 0 && fPosition.length < fOriginalPosition.length; | |
235 } | |
236 | |
237 /** | |
238 * Adapts the currently investigated position to an insertion. | |
239 */ | |
240 protected void adaptToInsert() { | |
241 | |
242 int myStart= fPosition.offset; | |
243 int myEnd= fPosition.offset + fPosition.length - 1; | |
244 myEnd= Math.max(myStart, myEnd); | |
245 | |
246 int yoursStart= fOffset; | |
247 int yoursEnd= fOffset + fReplaceLength -1; | |
248 yoursEnd= Math.max(yoursStart, yoursEnd); | |
249 | |
250 if (myEnd < yoursStart) | |
251 return; | |
252 | |
253 if (fLength <= 0) { | |
254 | |
255 if (myStart < yoursStart) | |
256 fPosition.length += fReplaceLength; | |
257 else | |
258 fPosition.offset += fReplaceLength; | |
259 | |
260 } else { | |
261 | |
262 if (myStart <= yoursStart && fOriginalPosition.offset <= yoursStart) | |
263 fPosition.length += fReplaceLength; | |
264 else | |
265 fPosition.offset += fReplaceLength; | |
266 } | |
267 } | |
268 | |
269 /** | |
270 * Adapts the currently investigated position to a deletion. | |
271 */ | |
272 protected void adaptToRemove() { | |
273 | |
274 int myStart= fPosition.offset; | |
275 int myEnd= fPosition.offset + fPosition.length -1; | |
276 myEnd= Math.max(myStart, myEnd); | |
277 | |
278 int yoursStart= fOffset; | |
279 int yoursEnd= fOffset + fLength -1; | |
280 yoursEnd= Math.max(yoursStart, yoursEnd); | |
281 | |
282 if (myEnd < yoursStart) | |
283 return; | |
284 | |
285 if (myStart <= yoursStart) { | |
286 | |
287 if (yoursEnd <= myEnd) | |
288 fPosition.length -= fLength; | |
289 else | |
290 fPosition.length -= (myEnd - yoursStart +1); | |
291 | |
292 } else if (yoursStart < myStart) { | |
293 | |
294 if (yoursEnd < myStart) | |
295 fPosition.offset -= fLength; | |
296 else { | |
297 fPosition.offset -= (myStart - yoursStart); | |
298 fPosition.length -= (yoursEnd - myStart +1); | |
299 } | |
300 | |
301 } | |
302 | |
303 // validate position to allowed values | |
304 if (fPosition.offset < 0) | |
305 fPosition.offset= 0; | |
306 | |
307 if (fPosition.length < 0) | |
308 fPosition.length= 0; | |
309 } | |
310 | |
311 /** | |
312 * Adapts the currently investigated position to the replace operation. | |
313 * First it checks whether the change replaces the whole range of the position. | |
314 * If not, it performs first the deletion of the previous text and afterwards | |
315 * the insertion of the new text. | |
316 */ | |
317 protected void adaptToReplace() { | |
318 | |
319 if (fPosition.offset is fOffset && fPosition.length is fLength && fPosition.length > 0) { | |
320 | |
321 // replace the whole range of the position | |
322 fPosition.length += (fReplaceLength - fLength); | |
323 if (fPosition.length < 0) { | |
324 fPosition.offset += fPosition.length; | |
325 fPosition.length= 0; | |
326 } | |
327 | |
328 } else { | |
329 | |
330 if (fLength > 0) | |
331 adaptToRemove(); | |
332 | |
333 if (fReplaceLength > 0) | |
334 adaptToInsert(); | |
335 } | |
336 } | |
337 | |
338 /** | |
339 * Determines whether the currently investigated position has been deleted by | |
340 * the replace operation specified in the current event. If so, it deletes | |
341 * the position and removes it from the document's position category. | |
342 * | |
343 * @return <code>true</code> if position has not been deleted | |
344 */ | |
345 protected bool notDeleted() { | |
346 | |
347 if (fOffset < fPosition.offset && (fPosition.offset + fPosition.length < fOffset + fLength)) { | |
348 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
349 fPosition.delete_(); |
129 | 350 |
351 try { | |
352 fDocument.removePosition(fCategory, fPosition); | |
353 } catch (BadPositionCategoryException x) { | |
354 } | |
355 | |
356 return false; | |
357 } | |
358 | |
359 return true; | |
360 } | |
361 | |
362 /* | |
363 * @see dwtx.jface.text.IPositionUpdater#update(dwtx.jface.text.DocumentEvent) | |
364 */ | |
365 public void update(DocumentEvent event) { | |
366 | |
367 try { | |
368 | |
369 | |
370 fOffset= event.getOffset(); | |
371 fLength= event.getLength(); | |
372 fReplaceLength= (event.getText() is null ? 0 : event.getText().length()); | |
373 fDocument= event.getDocument(); | |
374 | |
375 Position[] category= fDocument.getPositions(fCategory); | |
376 for (int i= 0; i < category.length; i++) { | |
377 | |
378 fPosition= category[i]; | |
379 fOriginalPosition.offset= fPosition.offset; | |
380 fOriginalPosition.length= fPosition.length; | |
381 | |
382 if (notDeleted()) | |
383 adaptToReplace(); | |
384 } | |
385 | |
386 } catch (BadPositionCategoryException x) { | |
387 // do nothing | |
388 } finally { | |
389 fDocument= null; | |
390 } | |
391 } | |
392 } |