Mercurial > projects > dwt-addons
annotate dwtx/text/edits/TextEditProcessor.d @ 161:f8d52b926852
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 27 Aug 2008 14:49:30 +0200 |
parents | 7926b636c282 |
children |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 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.text.edits.TextEditProcessor; | |
14 | |
131 | 15 import dwtx.text.edits.MultiTextEdit; // packageimport |
16 import dwtx.text.edits.CopySourceEdit; // packageimport | |
17 import dwtx.text.edits.MoveSourceEdit; // packageimport | |
18 import dwtx.text.edits.CopyingRangeMarker; // packageimport | |
19 import dwtx.text.edits.ReplaceEdit; // packageimport | |
20 import dwtx.text.edits.EditDocument; // packageimport | |
21 import dwtx.text.edits.UndoCollector; // packageimport | |
22 import dwtx.text.edits.DeleteEdit; // packageimport | |
23 import dwtx.text.edits.MoveTargetEdit; // packageimport | |
24 import dwtx.text.edits.CopyTargetEdit; // packageimport | |
25 import dwtx.text.edits.TextEditCopier; // packageimport | |
26 import dwtx.text.edits.ISourceModifier; // packageimport | |
27 import dwtx.text.edits.TextEditMessages; // packageimport | |
28 import dwtx.text.edits.MalformedTreeException; // packageimport | |
29 import dwtx.text.edits.TreeIterationInfo; // packageimport | |
30 import dwtx.text.edits.TextEditVisitor; // packageimport | |
31 import dwtx.text.edits.TextEditGroup; // packageimport | |
32 import dwtx.text.edits.TextEdit; // packageimport | |
33 import dwtx.text.edits.RangeMarker; // packageimport | |
34 import dwtx.text.edits.UndoEdit; // packageimport | |
35 import dwtx.text.edits.InsertEdit; // packageimport | |
36 | |
37 | |
129 | 38 import dwt.dwthelper.utils; |
39 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
40 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
41 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
140
diff
changeset
|
42 |
129 | 43 |
44 import dwtx.core.runtime.Assert; | |
45 import dwtx.jface.text.BadLocationException; | |
46 import dwtx.jface.text.IDocument; | |
47 | |
48 | |
49 /** | |
50 * A <code>TextEditProcessor</code> manages a set of edits and applies | |
51 * them as a whole to an <code>IDocument</code>. | |
52 * <p> | |
53 * This class isn't intended to be subclassed.</p> | |
54 * | |
55 * @see dwtx.text.edits.TextEdit#apply(IDocument) | |
56 * | |
57 * @since 3.0 | |
58 * @noextend This class is not intended to be subclassed by clients. | |
59 */ | |
60 public class TextEditProcessor { | |
61 | |
62 private IDocument fDocument; | |
63 private TextEdit fRoot; | |
64 private int fStyle; | |
65 | |
66 private bool fChecked; | |
67 private MalformedTreeException fException; | |
68 | |
69 private List fSourceEdits; | |
70 | |
71 /** | |
72 * Constructs a new edit processor for the given | |
73 * document. | |
74 * | |
75 * @param document the document to manipulate | |
76 * @param root the root of the text edit tree describing | |
77 * the modifications. By passing a text edit a a text edit | |
78 * processor the ownership of the edit is transfered to the | |
79 * text edit processors. Clients must not modify the edit | |
80 * (e.g adding new children) any longer. | |
81 * | |
82 * @param style {@link TextEdit#NONE}, {@link TextEdit#CREATE_UNDO} or {@link TextEdit#UPDATE_REGIONS}) | |
83 */ | |
130 | 84 public this(IDocument document, TextEdit root, int style) { |
129 | 85 this(document, root, style, false); |
86 } | |
87 | |
130 | 88 private this(IDocument document, TextEdit root, int style, bool secondary) { |
159 | 89 Assert.isNotNull(cast(Object)document); |
129 | 90 Assert.isNotNull(root); |
91 fDocument= document; | |
92 fRoot= root; | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
93 if ( auto mte = cast(MultiTextEdit)fRoot ) |
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
94 mte.defineRegion(0); |
129 | 95 fStyle= style; |
96 if (secondary) { | |
97 fChecked= true; | |
98 fSourceEdits= new ArrayList(); | |
99 } | |
100 } | |
101 | |
102 /** | |
103 * Creates a special internal processor used to during source computation inside | |
104 * move source and copy source edits | |
105 * | |
106 * @param document the document to be manipulated | |
107 * @param root the edit tree | |
108 * @param style {@link TextEdit#NONE}, {@link TextEdit#CREATE_UNDO} or {@link TextEdit#UPDATE_REGIONS}) | |
109 * @return a secondary text edit processor | |
110 * @since 3.1 | |
111 */ | |
112 static TextEditProcessor createSourceComputationProcessor(IDocument document, TextEdit root, int style) { | |
113 return new TextEditProcessor(document, root, style, true); | |
114 } | |
115 | |
116 /** | |
117 * Returns the document to be manipulated. | |
118 * | |
119 * @return the document | |
120 */ | |
121 public IDocument getDocument() { | |
122 return fDocument; | |
123 } | |
124 | |
125 /** | |
126 * Returns the edit processor's root edit. | |
127 * | |
128 * @return the processor's root edit | |
129 */ | |
130 public TextEdit getRoot() { | |
131 return fRoot; | |
132 } | |
133 | |
134 /** | |
135 * Returns the style bits of the text edit processor | |
136 * | |
137 * @return the style bits | |
138 * @see TextEdit#CREATE_UNDO | |
139 * @see TextEdit#UPDATE_REGIONS | |
140 */ | |
141 public int getStyle() { | |
142 return fStyle; | |
143 } | |
144 | |
145 /** | |
146 * Checks if the processor can execute all its edits. | |
147 * | |
148 * @return <code>true</code> if the edits can be executed. Return <code>false | |
149 * </code>otherwise. One major reason why edits cannot be executed are wrong | |
150 * offset or length values of edits. Calling perform in this case will very | |
151 * likely end in a <code>BadLocationException</code>. | |
152 */ | |
153 public bool canPerformEdits() { | |
154 try { | |
155 fRoot.dispatchCheckIntegrity(this); | |
156 fChecked= true; | |
157 } catch (MalformedTreeException e) { | |
158 fException= e; | |
159 return false; | |
160 } | |
161 return true; | |
162 } | |
163 | |
164 /** | |
165 * Executes the text edits. | |
166 * | |
167 * @return an object representing the undo of the executed edits | |
168 * @exception MalformedTreeException is thrown if the edit tree isn't | |
169 * in a valid state. This exception is thrown before any edit is executed. | |
170 * So the document is still in its original state. | |
171 * @exception BadLocationException is thrown if one of the edits in the | |
172 * tree can't be executed. The state of the document is undefined if this | |
173 * exception is thrown. | |
174 */ | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
136
diff
changeset
|
175 public UndoEdit performEdits() { |
129 | 176 if (!fChecked) { |
177 fRoot.dispatchCheckIntegrity(this); | |
178 } else { | |
179 if (fException !is null) | |
180 throw fException; | |
181 } | |
182 return fRoot.dispatchPerformEdits(this); | |
183 } | |
184 | |
185 /* | |
186 * Class isn't intended to be sub-lcassed | |
187 */ | |
188 protected bool considerEdit(TextEdit edit) { | |
189 return true; | |
190 } | |
161 | 191 package bool considerEdit_package(TextEdit edit) { |
192 return considerEdit(edit); | |
193 } | |
194 | |
129 | 195 |
196 //---- checking -------------------------------------------------------------------- | |
197 | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
198 void checkIntegrityDo() { |
129 | 199 fSourceEdits= new ArrayList(); |
200 fRoot.traverseConsistencyCheck(this, fDocument, fSourceEdits); | |
201 if (fRoot.getExclusiveEnd() > fDocument.getLength()) | |
202 throw new MalformedTreeException(null, fRoot, TextEditMessages.getString("TextEditProcessor.invalid_length")); //$NON-NLS-1$ | |
203 } | |
204 | |
205 void checkIntegrityUndo() { | |
206 if (fRoot.getExclusiveEnd() > fDocument.getLength()) | |
207 throw new MalformedTreeException(null, fRoot, TextEditMessages.getString("TextEditProcessor.invalid_length")); //$NON-NLS-1$ | |
208 } | |
209 | |
210 //---- execution -------------------------------------------------------------------- | |
211 | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
212 UndoEdit executeDo() { |
129 | 213 UndoCollector collector= new UndoCollector(fRoot); |
214 try { | |
215 if (createUndo()) | |
216 collector.connect(fDocument); | |
217 computeSources(); | |
218 fRoot.traverseDocumentUpdating(this, fDocument); | |
219 if (updateRegions()) { | |
220 fRoot.traverseRegionUpdating(this, fDocument, 0, false); | |
221 } | |
222 } finally { | |
223 collector.disconnect(fDocument); | |
224 } | |
159 | 225 return collector.undo_package; |
129 | 226 } |
227 | |
228 private void computeSources() { | |
229 for (Iterator iter= fSourceEdits.iterator(); iter.hasNext();) { | |
134 | 230 List list= cast(List)iter.next(); |
129 | 231 if (list !is null) { |
232 for (Iterator edits= list.iterator(); edits.hasNext();) { | |
134 | 233 TextEdit edit= cast(TextEdit)edits.next(); |
129 | 234 edit.traverseSourceComputation(this, fDocument); |
235 } | |
236 } | |
237 } | |
238 } | |
239 | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
240 UndoEdit executeUndo() { |
129 | 241 UndoCollector collector= new UndoCollector(fRoot); |
242 try { | |
243 if (createUndo()) | |
244 collector.connect(fDocument); | |
245 TextEdit[] edits= fRoot.getChildren(); | |
246 for (int i= edits.length - 1; i >= 0; i--) { | |
247 edits[i].performDocumentUpdating(fDocument); | |
248 } | |
249 } finally { | |
250 collector.disconnect(fDocument); | |
251 } | |
159 | 252 return collector.undo_package; |
129 | 253 } |
254 | |
255 private bool createUndo() { | |
256 return (fStyle & TextEdit.CREATE_UNDO) !is 0; | |
257 } | |
258 | |
259 private bool updateRegions() { | |
260 return (fStyle & TextEdit.UPDATE_REGIONS) !is 0; | |
261 } | |
262 } |