Mercurial > projects > dwt-addons
annotate dwtx/jface/text/DocumentCommand.d @ 151:eb21d3dfc767
fix module statement
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 24 Aug 2008 23:55:45 +0200 |
parents | 75302ef3f92f |
children | f70d9508c95c |
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; |
159 | |
160 | |
161 import java.util.ArrayList; | |
162 import java.util.Collections; | |
163 import java.util.Iterator; | |
164 import java.util.List; | |
165 import java.util.ListIterator; | |
166 import java.util.NoSuchElementException; | |
167 | |
168 import dwt.events.VerifyEvent; | |
169 import dwtx.core.runtime.Assert; | |
170 | |
171 | |
172 /** | |
173 * Represents a text modification as a document replace command. The text | |
174 * modification is given as a {@link dwt.events.VerifyEvent} and | |
175 * translated into a document replace command relative to a given offset. A | |
176 * document command can also be used to initialize a given | |
177 * <code>VerifyEvent</code>. | |
178 * <p> | |
179 * A document command can also represent a list of related changes.</p> | |
180 */ | |
181 public class DocumentCommand { | |
182 | |
183 /** | |
184 * A command which is added to document commands. | |
185 * @since 2.1 | |
186 */ | |
187 private static class Command : Comparable { | |
188 /** The offset of the range to be replaced */ | |
146 | 189 private const int fOffset; |
129 | 190 /** The length of the range to be replaced. */ |
146 | 191 private const int fLength; |
129 | 192 /** The replacement text */ |
146 | 193 private const String fText; |
129 | 194 /** The listener who owns this command */ |
146 | 195 private const IDocumentListener fOwner; |
129 | 196 |
197 /** | |
198 * Creates a new command with the given specification. | |
199 * | |
200 * @param offset the offset of the replace command | |
201 * @param length the length of the replace command | |
202 * @param text the text to replace with, may be <code>null</code> | |
203 * @param owner the document command owner, may be <code>null</code> | |
204 * @since 3.0 | |
205 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
206 public this(int offset, int length, String text, IDocumentListener owner) { |
129 | 207 if (offset < 0 || length < 0) |
208 throw new IllegalArgumentException(); | |
209 fOffset= offset; | |
210 fLength= length; | |
211 fText= text; | |
212 fOwner= owner; | |
213 } | |
214 | |
215 /** | |
216 * Returns the length delta for this command. | |
217 * | |
218 * @return the length delta for this command | |
219 */ | |
220 public int getDeltaLength() { | |
221 return (fText is null ? 0 : fText.length()) - fLength; | |
222 } | |
223 | |
224 /** | |
225 * Executes the document command on the specified document. | |
226 * | |
227 * @param document the document on which to execute the command. | |
228 * @throws BadLocationException in case this commands cannot be executed | |
229 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
230 public void execute(IDocument document) { |
129 | 231 |
232 if (fLength is 0 && fText is null) | |
233 return; | |
234 | |
235 if (fOwner !is null) | |
236 document.removeDocumentListener(fOwner); | |
237 | |
238 document.replace(fOffset, fLength, fText); | |
239 | |
240 if (fOwner !is null) | |
241 document.addDocumentListener(fOwner); | |
242 } | |
243 | |
244 /* | |
245 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) | |
246 */ | |
247 public int compareTo(final Object object) { | |
248 if (isEqual(object)) | |
249 return 0; | |
250 | |
134 | 251 final Command command= cast(Command) object; |
129 | 252 |
253 // diff middle points if not intersecting | |
254 if (fOffset + fLength <= command.fOffset || command.fOffset + command.fLength <= fOffset) { | |
255 int value= (2 * fOffset + fLength) - (2 * command.fOffset + command.fLength); | |
256 if (value !is 0) | |
257 return value; | |
258 } | |
259 // the answer | |
260 return 42; | |
261 } | |
262 | |
263 private bool isEqual(Object object) { | |
264 if (object is this) | |
265 return true; | |
138 | 266 if (!( cast(Command)object )) |
129 | 267 return false; |
134 | 268 final Command command= cast(Command) object; |
129 | 269 return command.fOffset is fOffset && command.fLength is fLength; |
270 } | |
271 } | |
272 | |
273 /** | |
274 * An iterator, which iterates in reverse over a list. | |
275 */ | |
276 private static class ReverseListIterator : Iterator { | |
277 | |
278 /** The list iterator. */ | |
146 | 279 private const ListIterator fListIterator; |
129 | 280 |
281 /** | |
282 * Creates a reverse list iterator. | |
283 * @param listIterator the iterator that this reverse iterator is based upon | |
284 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
285 public this(ListIterator listIterator) { |
129 | 286 if (listIterator is null) |
287 throw new IllegalArgumentException(); | |
288 fListIterator= listIterator; | |
289 } | |
290 | |
291 /* | |
292 * @see java.util.Iterator#hasNext() | |
293 */ | |
294 public bool hasNext() { | |
295 return fListIterator.hasPrevious(); | |
296 } | |
297 | |
298 /* | |
299 * @see java.util.Iterator#next() | |
300 */ | |
301 public Object next() { | |
302 return fListIterator.previous(); | |
303 } | |
304 | |
305 /* | |
306 * @see java.util.Iterator#remove() | |
307 */ | |
308 public void remove() { | |
309 throw new UnsupportedOperationException(); | |
310 } | |
311 } | |
312 | |
313 /** | |
314 * A command iterator. | |
315 */ | |
316 private static class CommandIterator : Iterator { | |
317 | |
318 /** The command iterator. */ | |
146 | 319 private const Iterator fIterator; |
129 | 320 |
321 /** The original command. */ | |
322 private Command fCommand; | |
323 | |
324 /** A flag indicating the direction of iteration. */ | |
325 private bool fForward; | |
326 | |
327 /** | |
328 * Creates a command iterator. | |
329 * | |
330 * @param commands an ascending ordered list of commands | |
331 * @param command the original command | |
332 * @param forward the direction | |
333 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
334 public this(final List commands, final Command command, final bool forward) { |
129 | 335 if (commands is null || command is null) |
336 throw new IllegalArgumentException(); | |
337 fIterator= forward ? commands.iterator() : new ReverseListIterator(commands.listIterator(commands.size())); | |
338 fCommand= command; | |
339 fForward= forward; | |
340 } | |
341 | |
342 /* | |
343 * @see java.util.Iterator#hasNext() | |
344 */ | |
345 public bool hasNext() { | |
346 return fCommand !is null || fIterator.hasNext(); | |
347 } | |
348 | |
349 /* | |
350 * @see java.util.Iterator#next() | |
351 */ | |
352 public Object next() { | |
353 | |
354 if (!hasNext()) | |
355 throw new NoSuchElementException(); | |
356 | |
357 if (fCommand is null) | |
358 return fIterator.next(); | |
359 | |
360 if (!fIterator.hasNext()) { | |
361 final Command tempCommand= fCommand; | |
362 fCommand= null; | |
363 return tempCommand; | |
364 } | |
365 | |
134 | 366 final Command command= cast(Command) fIterator.next(); |
129 | 367 final int compareValue= command.compareTo(fCommand); |
368 | |
369 if ((compareValue < 0) ^ !fForward) { | |
370 return command; | |
371 | |
372 } else if ((compareValue > 0) ^ !fForward) { | |
373 final Command tempCommand= fCommand; | |
374 fCommand= command; | |
375 return tempCommand; | |
376 | |
377 } else { | |
378 throw new IllegalArgumentException(); | |
379 } | |
380 } | |
381 | |
382 /* | |
383 * @see java.util.Iterator#remove() | |
384 */ | |
385 public void remove() { | |
386 throw new UnsupportedOperationException(); | |
387 } | |
388 } | |
389 | |
390 /** Must the command be updated */ | |
391 public bool doit= false; | |
392 /** The offset of the command. */ | |
393 public int offset; | |
394 /** The length of the command */ | |
395 public int length; | |
396 /** The text to be inserted */ | |
397 public String text; | |
398 /** | |
399 * The owner of the document command which will not be notified. | |
400 * @since 2.1 | |
401 */ | |
402 public IDocumentListener owner; | |
403 /** | |
404 * The caret offset with respect to the document before the document command is executed. | |
405 * @since 2.1 | |
406 */ | |
407 public int caretOffset; | |
408 /** | |
409 * Additional document commands. | |
410 * @since 2.1 | |
411 */ | |
146 | 412 private const List fCommands= new ArrayList(); |
129 | 413 /** |
414 * Indicates whether the caret should be shifted by this command. | |
415 * @since 3.0 | |
416 */ | |
417 public bool shiftsCaret; | |
418 | |
419 | |
420 /** | |
421 * Creates a new document command. | |
422 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
423 protected this() { |
129 | 424 } |
425 | |
426 /** | |
427 * Translates a verify event into a document replace command using the given offset. | |
428 * | |
429 * @param event the event to be translated | |
430 * @param modelRange the event range as model range | |
431 */ | |
432 void setEvent(VerifyEvent event, IRegion modelRange) { | |
433 | |
434 doit= true; | |
435 text= event.text; | |
436 | |
437 offset= modelRange.getOffset(); | |
438 length= modelRange.getLength(); | |
439 | |
440 owner= null; | |
441 caretOffset= -1; | |
442 shiftsCaret= true; | |
443 fCommands.clear(); | |
444 } | |
445 | |
446 /** | |
447 * Fills the given verify event with the replace text and the <code>doit</code> | |
448 * flag of this document command. Returns whether the document command | |
449 * covers the same range as the verify event considering the given offset. | |
450 * | |
451 * @param event the event to be changed | |
452 * @param modelRange to be considered for range comparison | |
453 * @return <code>true</code> if this command and the event cover the same range | |
454 */ | |
455 bool fillEvent(VerifyEvent event, IRegion modelRange) { | |
456 event.text= text; | |
457 event.doit= (offset is modelRange.getOffset() && length is modelRange.getLength() && doit && caretOffset is -1); | |
458 return event.doit; | |
459 } | |
460 | |
461 /** | |
462 * Adds an additional replace command. The added replace command must not overlap | |
463 * with existing ones. If the document command owner is not <code>null</code>, it will not | |
464 * get document change notifications for the particular command. | |
465 * | |
466 * @param commandOffset the offset of the region to replace | |
467 * @param commandLength the length of the region to replace | |
468 * @param commandText the text to replace with, may be <code>null</code> | |
469 * @param commandOwner the command owner, may be <code>null</code> | |
470 * @throws BadLocationException if the added command intersects with an existing one | |
471 * @since 2.1 | |
472 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
473 public void addCommand(int commandOffset, int commandLength, String commandText, IDocumentListener commandOwner) { |
129 | 474 final Command command= new Command(commandOffset, commandLength, commandText, commandOwner); |
475 | |
476 if (intersects(command)) | |
477 throw new BadLocationException(); | |
478 | |
479 final int index= Collections.binarySearch(fCommands, command); | |
480 | |
481 // a command with exactly the same ranges exists already | |
482 if (index >= 0) | |
483 throw new BadLocationException(); | |
484 | |
485 // binary search result is defined as (-(insertionIndex) - 1) | |
486 final int insertionIndex= -(index + 1); | |
487 | |
488 // overlaps to the right? | |
134 | 489 if (insertionIndex !is fCommands.size() && intersects(cast(Command) fCommands.get(insertionIndex), command)) |
129 | 490 throw new BadLocationException(); |
491 | |
492 // overlaps to the left? | |
134 | 493 if (insertionIndex !is 0 && intersects(cast(Command) fCommands.get(insertionIndex - 1), command)) |
129 | 494 throw new BadLocationException(); |
495 | |
496 fCommands.add(insertionIndex, command); | |
497 } | |
498 | |
499 /** | |
500 * Returns an iterator over the commands in ascending position order. | |
501 * The iterator includes the original document command. | |
502 * Commands cannot be removed. | |
503 * | |
504 * @return returns the command iterator | |
505 */ | |
506 public Iterator getCommandIterator() { | |
507 Command command= new Command(offset, length, text, owner); | |
508 return new CommandIterator(fCommands, command, true); | |
509 } | |
510 | |
511 /** | |
512 * Returns the number of commands including the original document command. | |
513 * | |
514 * @return returns the number of commands | |
515 * @since 2.1 | |
516 */ | |
517 public int getCommandCount() { | |
518 return 1 + fCommands.size(); | |
519 } | |
520 | |
521 /** | |
522 * Returns whether the two given commands intersect. | |
523 * | |
524 * @param command0 the first command | |
525 * @param command1 the second command | |
526 * @return <code>true</code> if the commands intersect | |
527 * @since 2.1 | |
528 */ | |
529 private bool intersects(Command command0, Command command1) { | |
530 // diff middle points if not intersecting | |
531 if (command0.fOffset + command0.fLength <= command1.fOffset || command1.fOffset + command1.fLength <= command0.fOffset) | |
532 return (2 * command0.fOffset + command0.fLength) - (2 * command1.fOffset + command1.fLength) is 0; | |
533 return true; | |
534 } | |
535 | |
536 /** | |
537 * Returns whether the given command intersects with this command. | |
538 * | |
539 * @param command the command | |
540 * @return <code>true</code> if the command intersects with this command | |
541 * @since 2.1 | |
542 */ | |
543 private bool intersects(Command command) { | |
544 // diff middle points if not intersecting | |
545 if (offset + length <= command.fOffset || command.fOffset + command.fLength <= offset) | |
546 return (2 * offset + length) - (2 * command.fOffset + command.fLength) is 0; | |
547 return true; | |
548 } | |
549 | |
550 /** | |
551 * Executes the document commands on a document. | |
552 * | |
553 * @param document the document on which to execute the commands | |
554 * @throws BadLocationException in case access to the given document fails | |
555 * @since 2.1 | |
556 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
557 void execute(IDocument document) { |
129 | 558 |
559 if (length is 0 && text is null && fCommands.size() is 0) | |
560 return; | |
561 | |
562 DefaultPositionUpdater updater= new DefaultPositionUpdater(getCategory()); | |
563 Position caretPosition= null; | |
564 try { | |
565 if (updateCaret()) { | |
566 document.addPositionCategory(getCategory()); | |
567 document.addPositionUpdater(updater); | |
568 caretPosition= new Position(caretOffset); | |
569 document.addPosition(getCategory(), caretPosition); | |
570 } | |
571 | |
572 final Command originalCommand= new Command(offset, length, text, owner); | |
573 for (final Iterator iterator= new CommandIterator(fCommands, originalCommand, false); iterator.hasNext(); ) | |
134 | 574 (cast(Command) iterator.next()).execute(document); |
129 | 575 |
576 } catch (BadLocationException e) { | |
577 // ignore | |
578 } catch (BadPositionCategoryException e) { | |
579 // ignore | |
580 } finally { | |
581 if (updateCaret()) { | |
582 document.removePositionUpdater(updater); | |
583 try { | |
584 document.removePositionCategory(getCategory()); | |
585 } catch (BadPositionCategoryException e) { | |
586 Assert.isTrue(false); | |
587 } | |
588 caretOffset= caretPosition.getOffset(); | |
589 } | |
590 } | |
591 } | |
592 | |
593 /** | |
594 * Returns <code>true</code> if the caret offset should be updated, <code>false</code> otherwise. | |
595 * | |
596 * @return <code>true</code> if the caret offset should be updated, <code>false</code> otherwise | |
597 * @since 3.0 | |
598 */ | |
599 private bool updateCaret() { | |
600 return shiftsCaret && caretOffset !is -1; | |
601 } | |
602 | |
603 /** | |
604 * Returns the position category for the caret offset position. | |
605 * | |
606 * @return the position category for the caret offset position | |
607 * @since 3.0 | |
608 */ | |
609 private String getCategory() { | |
610 return toString(); | |
611 } | |
612 | |
613 } |