Mercurial > projects > dwt-addons
annotate dwtx/jface/text/DocumentCommand.d @ 158:25f1f92fa3df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 26 Aug 2008 02:46:34 +0200 |
parents | a9566845f1cb |
children | 7926b636c282 |
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 | |
131 | 14 |
151 | 15 module dwtx.jface.text.DocumentCommand; |
16 | |
131 | 17 import dwtx.jface.text.IDocumentPartitioningListener; // packageimport |
18 import dwtx.jface.text.DefaultTextHover; // packageimport | |
19 import dwtx.jface.text.AbstractInformationControl; // packageimport | |
20 import dwtx.jface.text.TextUtilities; // packageimport | |
21 import dwtx.jface.text.IInformationControlCreatorExtension; // packageimport | |
22 import dwtx.jface.text.AbstractInformationControlManager; // packageimport | |
23 import dwtx.jface.text.ITextViewerExtension2; // packageimport | |
24 import dwtx.jface.text.IDocumentPartitioner; // packageimport | |
25 import dwtx.jface.text.DefaultIndentLineAutoEditStrategy; // packageimport | |
26 import dwtx.jface.text.ITextSelection; // packageimport | |
27 import dwtx.jface.text.Document; // packageimport | |
28 import dwtx.jface.text.FindReplaceDocumentAdapterContentProposalProvider; // packageimport | |
29 import dwtx.jface.text.ITextListener; // packageimport | |
30 import dwtx.jface.text.BadPartitioningException; // packageimport | |
31 import dwtx.jface.text.ITextViewerExtension5; // packageimport | |
32 import dwtx.jface.text.IDocumentPartitionerExtension3; // packageimport | |
33 import dwtx.jface.text.IUndoManager; // packageimport | |
34 import dwtx.jface.text.ITextHoverExtension2; // packageimport | |
35 import dwtx.jface.text.IRepairableDocument; // packageimport | |
36 import dwtx.jface.text.IRewriteTarget; // packageimport | |
37 import dwtx.jface.text.DefaultPositionUpdater; // packageimport | |
38 import dwtx.jface.text.RewriteSessionEditProcessor; // packageimport | |
39 import dwtx.jface.text.TextViewerHoverManager; // packageimport | |
40 import dwtx.jface.text.DocumentRewriteSession; // packageimport | |
41 import dwtx.jface.text.TextViewer; // packageimport | |
42 import dwtx.jface.text.ITextViewerExtension8; // packageimport | |
43 import dwtx.jface.text.RegExMessages; // packageimport | |
44 import dwtx.jface.text.IDelayedInputChangeProvider; // packageimport | |
45 import dwtx.jface.text.ITextOperationTargetExtension; // packageimport | |
46 import dwtx.jface.text.IWidgetTokenOwner; // packageimport | |
47 import dwtx.jface.text.IViewportListener; // packageimport | |
48 import dwtx.jface.text.GapTextStore; // packageimport | |
49 import dwtx.jface.text.MarkSelection; // packageimport | |
50 import dwtx.jface.text.IDocumentPartitioningListenerExtension; // packageimport | |
51 import dwtx.jface.text.IDocumentAdapterExtension; // packageimport | |
52 import dwtx.jface.text.IInformationControlExtension; // packageimport | |
53 import dwtx.jface.text.IDocumentPartitioningListenerExtension2; // packageimport | |
54 import dwtx.jface.text.DefaultDocumentAdapter; // packageimport | |
55 import dwtx.jface.text.ITextViewerExtension3; // packageimport | |
56 import dwtx.jface.text.IInformationControlCreator; // packageimport | |
57 import dwtx.jface.text.TypedRegion; // packageimport | |
58 import dwtx.jface.text.ISynchronizable; // packageimport | |
59 import dwtx.jface.text.IMarkRegionTarget; // packageimport | |
60 import dwtx.jface.text.TextViewerUndoManager; // packageimport | |
61 import dwtx.jface.text.IRegion; // packageimport | |
62 import dwtx.jface.text.IInformationControlExtension2; // packageimport | |
63 import dwtx.jface.text.IDocumentExtension4; // packageimport | |
64 import dwtx.jface.text.IDocumentExtension2; // packageimport | |
65 import dwtx.jface.text.IDocumentPartitionerExtension2; // packageimport | |
66 import dwtx.jface.text.Assert; // packageimport | |
67 import dwtx.jface.text.DefaultInformationControl; // packageimport | |
68 import dwtx.jface.text.IWidgetTokenOwnerExtension; // packageimport | |
69 import dwtx.jface.text.DocumentClone; // packageimport | |
70 import dwtx.jface.text.DefaultUndoManager; // packageimport | |
71 import dwtx.jface.text.IFindReplaceTarget; // packageimport | |
72 import dwtx.jface.text.IAutoEditStrategy; // packageimport | |
73 import dwtx.jface.text.ILineTrackerExtension; // packageimport | |
74 import dwtx.jface.text.IUndoManagerExtension; // packageimport | |
75 import dwtx.jface.text.TextSelection; // packageimport | |
76 import dwtx.jface.text.DefaultAutoIndentStrategy; // packageimport | |
77 import dwtx.jface.text.IAutoIndentStrategy; // packageimport | |
78 import dwtx.jface.text.IPainter; // packageimport | |
79 import dwtx.jface.text.IInformationControl; // packageimport | |
80 import dwtx.jface.text.IInformationControlExtension3; // packageimport | |
81 import dwtx.jface.text.ITextViewerExtension6; // packageimport | |
82 import dwtx.jface.text.IInformationControlExtension4; // packageimport | |
83 import dwtx.jface.text.DefaultLineTracker; // packageimport | |
84 import dwtx.jface.text.IDocumentInformationMappingExtension; // packageimport | |
85 import dwtx.jface.text.IRepairableDocumentExtension; // packageimport | |
86 import dwtx.jface.text.ITextHover; // packageimport | |
87 import dwtx.jface.text.FindReplaceDocumentAdapter; // packageimport | |
88 import dwtx.jface.text.ILineTracker; // packageimport | |
89 import dwtx.jface.text.Line; // packageimport | |
90 import dwtx.jface.text.ITextViewerExtension; // packageimport | |
91 import dwtx.jface.text.IDocumentAdapter; // packageimport | |
92 import dwtx.jface.text.TextEvent; // packageimport | |
93 import dwtx.jface.text.BadLocationException; // packageimport | |
94 import dwtx.jface.text.AbstractDocument; // packageimport | |
95 import dwtx.jface.text.AbstractLineTracker; // packageimport | |
96 import dwtx.jface.text.TreeLineTracker; // packageimport | |
97 import dwtx.jface.text.ITextPresentationListener; // packageimport | |
98 import dwtx.jface.text.Region; // packageimport | |
99 import dwtx.jface.text.ITextViewer; // packageimport | |
100 import dwtx.jface.text.IDocumentInformationMapping; // packageimport | |
101 import dwtx.jface.text.MarginPainter; // packageimport | |
102 import dwtx.jface.text.IPaintPositionManager; // packageimport | |
103 import dwtx.jface.text.TextPresentation; // packageimport | |
104 import dwtx.jface.text.IFindReplaceTargetExtension; // packageimport | |
105 import dwtx.jface.text.ISlaveDocumentManagerExtension; // packageimport | |
106 import dwtx.jface.text.ISelectionValidator; // packageimport | |
107 import dwtx.jface.text.IDocumentExtension; // packageimport | |
108 import dwtx.jface.text.PropagatingFontFieldEditor; // packageimport | |
109 import dwtx.jface.text.ConfigurableLineTracker; // packageimport | |
110 import dwtx.jface.text.SlaveDocumentEvent; // packageimport | |
111 import dwtx.jface.text.IDocumentListener; // packageimport | |
112 import dwtx.jface.text.PaintManager; // packageimport | |
113 import dwtx.jface.text.IFindReplaceTargetExtension3; // packageimport | |
114 import dwtx.jface.text.ITextDoubleClickStrategy; // packageimport | |
115 import dwtx.jface.text.IDocumentExtension3; // packageimport | |
116 import dwtx.jface.text.Position; // packageimport | |
117 import dwtx.jface.text.TextMessages; // packageimport | |
118 import dwtx.jface.text.CopyOnWriteTextStore; // packageimport | |
119 import dwtx.jface.text.WhitespaceCharacterPainter; // packageimport | |
120 import dwtx.jface.text.IPositionUpdater; // packageimport | |
121 import dwtx.jface.text.DefaultTextDoubleClickStrategy; // packageimport | |
122 import dwtx.jface.text.ListLineTracker; // packageimport | |
123 import dwtx.jface.text.ITextInputListener; // packageimport | |
124 import dwtx.jface.text.BadPositionCategoryException; // packageimport | |
125 import dwtx.jface.text.IWidgetTokenKeeperExtension; // packageimport | |
126 import dwtx.jface.text.IInputChangedListener; // packageimport | |
127 import dwtx.jface.text.ITextOperationTarget; // packageimport | |
128 import dwtx.jface.text.IDocumentInformationMappingExtension2; // packageimport | |
129 import dwtx.jface.text.ITextViewerExtension7; // packageimport | |
130 import dwtx.jface.text.IInformationControlExtension5; // packageimport | |
131 import dwtx.jface.text.IDocumentRewriteSessionListener; // packageimport | |
132 import dwtx.jface.text.JFaceTextUtil; // packageimport | |
133 import dwtx.jface.text.AbstractReusableInformationControlCreator; // packageimport | |
134 import dwtx.jface.text.TabsToSpacesConverter; // packageimport | |
135 import dwtx.jface.text.CursorLinePainter; // packageimport | |
136 import dwtx.jface.text.ITextHoverExtension; // packageimport | |
137 import dwtx.jface.text.IEventConsumer; // packageimport | |
138 import dwtx.jface.text.IDocument; // packageimport | |
139 import dwtx.jface.text.IWidgetTokenKeeper; // packageimport | |
140 import dwtx.jface.text.TypedPosition; // packageimport | |
141 import dwtx.jface.text.IEditingSupportRegistry; // packageimport | |
142 import dwtx.jface.text.IDocumentPartitionerExtension; // packageimport | |
143 import dwtx.jface.text.AbstractHoverInformationControlManager; // packageimport | |
144 import dwtx.jface.text.IEditingSupport; // packageimport | |
145 import dwtx.jface.text.IMarkSelection; // packageimport | |
146 import dwtx.jface.text.ISlaveDocumentManager; // packageimport | |
147 import dwtx.jface.text.DocumentEvent; // packageimport | |
148 import dwtx.jface.text.DocumentPartitioningChangedEvent; // packageimport | |
149 import dwtx.jface.text.ITextStore; // packageimport | |
150 import dwtx.jface.text.JFaceTextMessages; // packageimport | |
151 import dwtx.jface.text.DocumentRewriteSessionEvent; // packageimport | |
152 import dwtx.jface.text.SequentialRewriteTextStore; // packageimport | |
153 import dwtx.jface.text.DocumentRewriteSessionType; // packageimport | |
154 import dwtx.jface.text.TextAttribute; // packageimport | |
155 import dwtx.jface.text.ITextViewerExtension4; // packageimport | |
156 import dwtx.jface.text.ITypedRegion; // packageimport | |
157 | |
129 | 158 import dwt.dwthelper.utils; |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
151
diff
changeset
|
159 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
151
diff
changeset
|
160 |
129 | 161 import dwt.events.VerifyEvent; |
162 import dwtx.core.runtime.Assert; | |
163 | |
164 | |
165 /** | |
166 * Represents a text modification as a document replace command. The text | |
167 * modification is given as a {@link dwt.events.VerifyEvent} and | |
168 * translated into a document replace command relative to a given offset. A | |
169 * document command can also be used to initialize a given | |
170 * <code>VerifyEvent</code>. | |
171 * <p> | |
172 * A document command can also represent a list of related changes.</p> | |
173 */ | |
174 public class DocumentCommand { | |
175 | |
176 /** | |
177 * A command which is added to document commands. | |
178 * @since 2.1 | |
179 */ | |
180 private static class Command : Comparable { | |
181 /** The offset of the range to be replaced */ | |
146 | 182 private const int fOffset; |
129 | 183 /** The length of the range to be replaced. */ |
146 | 184 private const int fLength; |
129 | 185 /** The replacement text */ |
146 | 186 private const String fText; |
129 | 187 /** The listener who owns this command */ |
146 | 188 private const IDocumentListener fOwner; |
129 | 189 |
190 /** | |
191 * Creates a new command with the given specification. | |
192 * | |
193 * @param offset the offset of the replace command | |
194 * @param length the length of the replace command | |
195 * @param text the text to replace with, may be <code>null</code> | |
196 * @param owner the document command owner, may be <code>null</code> | |
197 * @since 3.0 | |
198 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
199 public this(int offset, int length, String text, IDocumentListener owner) { |
129 | 200 if (offset < 0 || length < 0) |
201 throw new IllegalArgumentException(); | |
202 fOffset= offset; | |
203 fLength= length; | |
204 fText= text; | |
205 fOwner= owner; | |
206 } | |
207 | |
208 /** | |
209 * Returns the length delta for this command. | |
210 * | |
211 * @return the length delta for this command | |
212 */ | |
213 public int getDeltaLength() { | |
214 return (fText is null ? 0 : fText.length()) - fLength; | |
215 } | |
216 | |
217 /** | |
218 * Executes the document command on the specified document. | |
219 * | |
220 * @param document the document on which to execute the command. | |
221 * @throws BadLocationException in case this commands cannot be executed | |
222 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
223 public void execute(IDocument document) { |
129 | 224 |
225 if (fLength is 0 && fText is null) | |
226 return; | |
227 | |
228 if (fOwner !is null) | |
229 document.removeDocumentListener(fOwner); | |
230 | |
231 document.replace(fOffset, fLength, fText); | |
232 | |
233 if (fOwner !is null) | |
234 document.addDocumentListener(fOwner); | |
235 } | |
236 | |
237 /* | |
238 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) | |
239 */ | |
156 | 240 public int compareTo(Object object) { |
129 | 241 if (isEqual(object)) |
242 return 0; | |
243 | |
134 | 244 final Command command= cast(Command) object; |
129 | 245 |
246 // diff middle points if not intersecting | |
247 if (fOffset + fLength <= command.fOffset || command.fOffset + command.fLength <= fOffset) { | |
248 int value= (2 * fOffset + fLength) - (2 * command.fOffset + command.fLength); | |
249 if (value !is 0) | |
250 return value; | |
251 } | |
252 // the answer | |
253 return 42; | |
254 } | |
255 | |
256 private bool isEqual(Object object) { | |
257 if (object is this) | |
258 return true; | |
138 | 259 if (!( cast(Command)object )) |
129 | 260 return false; |
134 | 261 final Command command= cast(Command) object; |
129 | 262 return command.fOffset is fOffset && command.fLength is fLength; |
263 } | |
264 } | |
265 | |
266 /** | |
267 * An iterator, which iterates in reverse over a list. | |
268 */ | |
269 private static class ReverseListIterator : Iterator { | |
270 | |
271 /** The list iterator. */ | |
146 | 272 private const ListIterator fListIterator; |
129 | 273 |
274 /** | |
275 * Creates a reverse list iterator. | |
276 * @param listIterator the iterator that this reverse iterator is based upon | |
277 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
278 public this(ListIterator listIterator) { |
129 | 279 if (listIterator is null) |
280 throw new IllegalArgumentException(); | |
281 fListIterator= listIterator; | |
282 } | |
283 | |
284 /* | |
285 * @see java.util.Iterator#hasNext() | |
286 */ | |
287 public bool hasNext() { | |
288 return fListIterator.hasPrevious(); | |
289 } | |
290 | |
291 /* | |
292 * @see java.util.Iterator#next() | |
293 */ | |
294 public Object next() { | |
295 return fListIterator.previous(); | |
296 } | |
297 | |
298 /* | |
299 * @see java.util.Iterator#remove() | |
300 */ | |
301 public void remove() { | |
302 throw new UnsupportedOperationException(); | |
303 } | |
304 } | |
305 | |
306 /** | |
307 * A command iterator. | |
308 */ | |
309 private static class CommandIterator : Iterator { | |
310 | |
311 /** The command iterator. */ | |
146 | 312 private const Iterator fIterator; |
129 | 313 |
314 /** The original command. */ | |
315 private Command fCommand; | |
316 | |
317 /** A flag indicating the direction of iteration. */ | |
318 private bool fForward; | |
319 | |
320 /** | |
321 * Creates a command iterator. | |
322 * | |
323 * @param commands an ascending ordered list of commands | |
324 * @param command the original command | |
325 * @param forward the direction | |
326 */ | |
156 | 327 public this(List commands, Command command, bool forward) { |
129 | 328 if (commands is null || command is null) |
329 throw new IllegalArgumentException(); | |
330 fIterator= forward ? commands.iterator() : new ReverseListIterator(commands.listIterator(commands.size())); | |
331 fCommand= command; | |
332 fForward= forward; | |
333 } | |
334 | |
335 /* | |
336 * @see java.util.Iterator#hasNext() | |
337 */ | |
338 public bool hasNext() { | |
339 return fCommand !is null || fIterator.hasNext(); | |
340 } | |
341 | |
342 /* | |
343 * @see java.util.Iterator#next() | |
344 */ | |
345 public Object next() { | |
346 | |
347 if (!hasNext()) | |
348 throw new NoSuchElementException(); | |
349 | |
350 if (fCommand is null) | |
351 return fIterator.next(); | |
352 | |
353 if (!fIterator.hasNext()) { | |
354 final Command tempCommand= fCommand; | |
355 fCommand= null; | |
356 return tempCommand; | |
357 } | |
358 | |
134 | 359 final Command command= cast(Command) fIterator.next(); |
129 | 360 final int compareValue= command.compareTo(fCommand); |
361 | |
362 if ((compareValue < 0) ^ !fForward) { | |
363 return command; | |
364 | |
365 } else if ((compareValue > 0) ^ !fForward) { | |
366 final Command tempCommand= fCommand; | |
367 fCommand= command; | |
368 return tempCommand; | |
369 | |
370 } else { | |
371 throw new IllegalArgumentException(); | |
372 } | |
373 } | |
374 | |
375 /* | |
376 * @see java.util.Iterator#remove() | |
377 */ | |
378 public void remove() { | |
379 throw new UnsupportedOperationException(); | |
380 } | |
381 } | |
382 | |
383 /** Must the command be updated */ | |
384 public bool doit= false; | |
385 /** The offset of the command. */ | |
386 public int offset; | |
387 /** The length of the command */ | |
388 public int length; | |
389 /** The text to be inserted */ | |
390 public String text; | |
391 /** | |
392 * The owner of the document command which will not be notified. | |
393 * @since 2.1 | |
394 */ | |
395 public IDocumentListener owner; | |
396 /** | |
397 * The caret offset with respect to the document before the document command is executed. | |
398 * @since 2.1 | |
399 */ | |
400 public int caretOffset; | |
401 /** | |
402 * Additional document commands. | |
403 * @since 2.1 | |
404 */ | |
146 | 405 private const List fCommands= new ArrayList(); |
129 | 406 /** |
407 * Indicates whether the caret should be shifted by this command. | |
408 * @since 3.0 | |
409 */ | |
410 public bool shiftsCaret; | |
411 | |
412 | |
413 /** | |
414 * Creates a new document command. | |
415 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
416 protected this() { |
129 | 417 } |
418 | |
419 /** | |
420 * Translates a verify event into a document replace command using the given offset. | |
421 * | |
422 * @param event the event to be translated | |
423 * @param modelRange the event range as model range | |
424 */ | |
425 void setEvent(VerifyEvent event, IRegion modelRange) { | |
426 | |
427 doit= true; | |
428 text= event.text; | |
429 | |
430 offset= modelRange.getOffset(); | |
431 length= modelRange.getLength(); | |
432 | |
433 owner= null; | |
434 caretOffset= -1; | |
435 shiftsCaret= true; | |
436 fCommands.clear(); | |
437 } | |
438 | |
439 /** | |
440 * Fills the given verify event with the replace text and the <code>doit</code> | |
441 * flag of this document command. Returns whether the document command | |
442 * covers the same range as the verify event considering the given offset. | |
443 * | |
444 * @param event the event to be changed | |
445 * @param modelRange to be considered for range comparison | |
446 * @return <code>true</code> if this command and the event cover the same range | |
447 */ | |
448 bool fillEvent(VerifyEvent event, IRegion modelRange) { | |
449 event.text= text; | |
450 event.doit= (offset is modelRange.getOffset() && length is modelRange.getLength() && doit && caretOffset is -1); | |
451 return event.doit; | |
452 } | |
453 | |
454 /** | |
455 * Adds an additional replace command. The added replace command must not overlap | |
456 * with existing ones. If the document command owner is not <code>null</code>, it will not | |
457 * get document change notifications for the particular command. | |
458 * | |
459 * @param commandOffset the offset of the region to replace | |
460 * @param commandLength the length of the region to replace | |
461 * @param commandText the text to replace with, may be <code>null</code> | |
462 * @param commandOwner the command owner, may be <code>null</code> | |
463 * @throws BadLocationException if the added command intersects with an existing one | |
464 * @since 2.1 | |
465 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
466 public void addCommand(int commandOffset, int commandLength, String commandText, IDocumentListener commandOwner) { |
129 | 467 final Command command= new Command(commandOffset, commandLength, commandText, commandOwner); |
468 | |
469 if (intersects(command)) | |
470 throw new BadLocationException(); | |
471 | |
472 final int index= Collections.binarySearch(fCommands, command); | |
473 | |
474 // a command with exactly the same ranges exists already | |
475 if (index >= 0) | |
476 throw new BadLocationException(); | |
477 | |
478 // binary search result is defined as (-(insertionIndex) - 1) | |
479 final int insertionIndex= -(index + 1); | |
480 | |
481 // overlaps to the right? | |
134 | 482 if (insertionIndex !is fCommands.size() && intersects(cast(Command) fCommands.get(insertionIndex), command)) |
129 | 483 throw new BadLocationException(); |
484 | |
485 // overlaps to the left? | |
134 | 486 if (insertionIndex !is 0 && intersects(cast(Command) fCommands.get(insertionIndex - 1), command)) |
129 | 487 throw new BadLocationException(); |
488 | |
489 fCommands.add(insertionIndex, command); | |
490 } | |
491 | |
492 /** | |
493 * Returns an iterator over the commands in ascending position order. | |
494 * The iterator includes the original document command. | |
495 * Commands cannot be removed. | |
496 * | |
497 * @return returns the command iterator | |
498 */ | |
499 public Iterator getCommandIterator() { | |
500 Command command= new Command(offset, length, text, owner); | |
501 return new CommandIterator(fCommands, command, true); | |
502 } | |
503 | |
504 /** | |
505 * Returns the number of commands including the original document command. | |
506 * | |
507 * @return returns the number of commands | |
508 * @since 2.1 | |
509 */ | |
510 public int getCommandCount() { | |
511 return 1 + fCommands.size(); | |
512 } | |
513 | |
514 /** | |
515 * Returns whether the two given commands intersect. | |
516 * | |
517 * @param command0 the first command | |
518 * @param command1 the second command | |
519 * @return <code>true</code> if the commands intersect | |
520 * @since 2.1 | |
521 */ | |
522 private bool intersects(Command command0, Command command1) { | |
523 // diff middle points if not intersecting | |
524 if (command0.fOffset + command0.fLength <= command1.fOffset || command1.fOffset + command1.fLength <= command0.fOffset) | |
525 return (2 * command0.fOffset + command0.fLength) - (2 * command1.fOffset + command1.fLength) is 0; | |
526 return true; | |
527 } | |
528 | |
529 /** | |
530 * Returns whether the given command intersects with this command. | |
531 * | |
532 * @param command the command | |
533 * @return <code>true</code> if the command intersects with this command | |
534 * @since 2.1 | |
535 */ | |
536 private bool intersects(Command command) { | |
537 // diff middle points if not intersecting | |
538 if (offset + length <= command.fOffset || command.fOffset + command.fLength <= offset) | |
539 return (2 * offset + length) - (2 * command.fOffset + command.fLength) is 0; | |
540 return true; | |
541 } | |
542 | |
543 /** | |
544 * Executes the document commands on a document. | |
545 * | |
546 * @param document the document on which to execute the commands | |
547 * @throws BadLocationException in case access to the given document fails | |
548 * @since 2.1 | |
549 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
550 void execute(IDocument document) { |
129 | 551 |
552 if (length is 0 && text is null && fCommands.size() is 0) | |
553 return; | |
554 | |
555 DefaultPositionUpdater updater= new DefaultPositionUpdater(getCategory()); | |
556 Position caretPosition= null; | |
557 try { | |
558 if (updateCaret()) { | |
559 document.addPositionCategory(getCategory()); | |
560 document.addPositionUpdater(updater); | |
561 caretPosition= new Position(caretOffset); | |
562 document.addPosition(getCategory(), caretPosition); | |
563 } | |
564 | |
565 final Command originalCommand= new Command(offset, length, text, owner); | |
566 for (final Iterator iterator= new CommandIterator(fCommands, originalCommand, false); iterator.hasNext(); ) | |
134 | 567 (cast(Command) iterator.next()).execute(document); |
129 | 568 |
569 } catch (BadLocationException e) { | |
570 // ignore | |
571 } catch (BadPositionCategoryException e) { | |
572 // ignore | |
573 } finally { | |
574 if (updateCaret()) { | |
575 document.removePositionUpdater(updater); | |
576 try { | |
577 document.removePositionCategory(getCategory()); | |
578 } catch (BadPositionCategoryException e) { | |
579 Assert.isTrue(false); | |
580 } | |
581 caretOffset= caretPosition.getOffset(); | |
582 } | |
583 } | |
584 } | |
585 | |
586 /** | |
587 * Returns <code>true</code> if the caret offset should be updated, <code>false</code> otherwise. | |
588 * | |
589 * @return <code>true</code> if the caret offset should be updated, <code>false</code> otherwise | |
590 * @since 3.0 | |
591 */ | |
592 private bool updateCaret() { | |
593 return shiftsCaret && caretOffset !is -1; | |
594 } | |
595 | |
596 /** | |
597 * Returns the position category for the caret offset position. | |
598 * | |
599 * @return the position category for the caret offset position | |
600 * @since 3.0 | |
601 */ | |
602 private String getCategory() { | |
603 return toString(); | |
604 } | |
605 | |
606 } |