Mercurial > projects > dwt-addons
annotate dwtx/jface/text/DefaultPositionUpdater.d @ 161:f8d52b926852
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 27 Aug 2008 14:49:30 +0200 |
parents | 26688fec6d23 |
children |
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 */ | |
161 | 197 protected Position fOriginalPosition; |
129 | 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) { |
161 | 214 fOriginalPosition= new Position(0, 0); |
129 | 215 fCategory= category; |
216 } | |
217 | |
218 /** | |
219 * Returns the category this updater is responsible for. | |
220 * | |
221 * @return the category this updater is responsible for | |
222 */ | |
223 protected String getCategory() { | |
224 return fCategory; | |
225 } | |
226 | |
227 /** | |
228 * Returns whether the current event describes a well formed replace | |
229 * by which the current position is directly affected. | |
230 * | |
231 * @return <code>true</code> the current position is directly affected | |
232 * @since 3.0 | |
233 */ | |
234 protected bool isAffectingReplace() { | |
235 return fLength > 0 && fReplaceLength > 0 && fPosition.length < fOriginalPosition.length; | |
236 } | |
237 | |
238 /** | |
239 * Adapts the currently investigated position to an insertion. | |
240 */ | |
241 protected void adaptToInsert() { | |
242 | |
243 int myStart= fPosition.offset; | |
244 int myEnd= fPosition.offset + fPosition.length - 1; | |
245 myEnd= Math.max(myStart, myEnd); | |
246 | |
247 int yoursStart= fOffset; | |
248 int yoursEnd= fOffset + fReplaceLength -1; | |
249 yoursEnd= Math.max(yoursStart, yoursEnd); | |
250 | |
251 if (myEnd < yoursStart) | |
252 return; | |
253 | |
254 if (fLength <= 0) { | |
255 | |
256 if (myStart < yoursStart) | |
257 fPosition.length += fReplaceLength; | |
258 else | |
259 fPosition.offset += fReplaceLength; | |
260 | |
261 } else { | |
262 | |
263 if (myStart <= yoursStart && fOriginalPosition.offset <= yoursStart) | |
264 fPosition.length += fReplaceLength; | |
265 else | |
266 fPosition.offset += fReplaceLength; | |
267 } | |
268 } | |
269 | |
270 /** | |
271 * Adapts the currently investigated position to a deletion. | |
272 */ | |
273 protected void adaptToRemove() { | |
274 | |
275 int myStart= fPosition.offset; | |
276 int myEnd= fPosition.offset + fPosition.length -1; | |
277 myEnd= Math.max(myStart, myEnd); | |
278 | |
279 int yoursStart= fOffset; | |
280 int yoursEnd= fOffset + fLength -1; | |
281 yoursEnd= Math.max(yoursStart, yoursEnd); | |
282 | |
283 if (myEnd < yoursStart) | |
284 return; | |
285 | |
286 if (myStart <= yoursStart) { | |
287 | |
288 if (yoursEnd <= myEnd) | |
289 fPosition.length -= fLength; | |
290 else | |
291 fPosition.length -= (myEnd - yoursStart +1); | |
292 | |
293 } else if (yoursStart < myStart) { | |
294 | |
295 if (yoursEnd < myStart) | |
296 fPosition.offset -= fLength; | |
297 else { | |
298 fPosition.offset -= (myStart - yoursStart); | |
299 fPosition.length -= (yoursEnd - myStart +1); | |
300 } | |
301 | |
302 } | |
303 | |
304 // validate position to allowed values | |
305 if (fPosition.offset < 0) | |
306 fPosition.offset= 0; | |
307 | |
308 if (fPosition.length < 0) | |
309 fPosition.length= 0; | |
310 } | |
311 | |
312 /** | |
313 * Adapts the currently investigated position to the replace operation. | |
314 * First it checks whether the change replaces the whole range of the position. | |
315 * If not, it performs first the deletion of the previous text and afterwards | |
316 * the insertion of the new text. | |
317 */ | |
318 protected void adaptToReplace() { | |
319 | |
320 if (fPosition.offset is fOffset && fPosition.length is fLength && fPosition.length > 0) { | |
321 | |
322 // replace the whole range of the position | |
323 fPosition.length += (fReplaceLength - fLength); | |
324 if (fPosition.length < 0) { | |
325 fPosition.offset += fPosition.length; | |
326 fPosition.length= 0; | |
327 } | |
328 | |
329 } else { | |
330 | |
331 if (fLength > 0) | |
332 adaptToRemove(); | |
333 | |
334 if (fReplaceLength > 0) | |
335 adaptToInsert(); | |
336 } | |
337 } | |
338 | |
339 /** | |
340 * Determines whether the currently investigated position has been deleted by | |
341 * the replace operation specified in the current event. If so, it deletes | |
342 * the position and removes it from the document's position category. | |
343 * | |
344 * @return <code>true</code> if position has not been deleted | |
345 */ | |
346 protected bool notDeleted() { | |
347 | |
348 if (fOffset < fPosition.offset && (fPosition.offset + fPosition.length < fOffset + fLength)) { | |
349 | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
350 fPosition.delete_(); |
129 | 351 |
352 try { | |
353 fDocument.removePosition(fCategory, fPosition); | |
354 } catch (BadPositionCategoryException x) { | |
355 } | |
356 | |
357 return false; | |
358 } | |
359 | |
360 return true; | |
361 } | |
362 | |
363 /* | |
364 * @see dwtx.jface.text.IPositionUpdater#update(dwtx.jface.text.DocumentEvent) | |
365 */ | |
366 public void update(DocumentEvent event) { | |
367 | |
368 try { | |
369 | |
370 | |
371 fOffset= event.getOffset(); | |
372 fLength= event.getLength(); | |
373 fReplaceLength= (event.getText() is null ? 0 : event.getText().length()); | |
374 fDocument= event.getDocument(); | |
375 | |
376 Position[] category= fDocument.getPositions(fCategory); | |
377 for (int i= 0; i < category.length; i++) { | |
378 | |
379 fPosition= category[i]; | |
380 fOriginalPosition.offset= fPosition.offset; | |
381 fOriginalPosition.length= fPosition.length; | |
382 | |
383 if (notDeleted()) | |
384 adaptToReplace(); | |
385 } | |
386 | |
387 } catch (BadPositionCategoryException x) { | |
388 // do nothing | |
389 } finally { | |
390 fDocument= null; | |
391 } | |
392 } | |
393 } |