129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 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.CopySourceEdit;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import java.util.ArrayList;
|
|
18 import java.util.List;
|
|
19
|
|
20 import dwtx.core.runtime.Assert;
|
|
21 import dwtx.jface.text.BadLocationException;
|
|
22 import dwtx.jface.text.IDocument;
|
|
23
|
|
24 /**
|
|
25 * A copy source edit denotes the source of a copy operation. Copy
|
|
26 * source edits are only valid inside an edit tree if they have a
|
|
27 * corresponding target edit. Furthermore the corresponding
|
|
28 * target edit can't be a direct or indirect child of the source
|
|
29 * edit. Violating one of two requirements will result in a <code>
|
|
30 * MalformedTreeException</code> when executing the edit tree.
|
|
31 * <p>
|
|
32 * A copy source edit can manage an optional source modifier. A
|
|
33 * source modifier can provide a set of replace edits which will
|
|
34 * to applied to the source before it gets inserted at the target
|
|
35 * position.
|
|
36 *
|
|
37 * @see dwtx.text.edits.CopyTargetEdit
|
|
38 *
|
|
39 * @since 3.0
|
|
40 */
|
|
41 public final class CopySourceEdit : TextEdit {
|
|
42
|
|
43 private CopyTargetEdit fTarget;
|
|
44 private ISourceModifier fModifier;
|
|
45
|
|
46 private String fSourceContent;
|
|
47 private TextEdit fSourceRoot;
|
|
48
|
|
49 private static class PartialCopier : TextEditVisitor {
|
|
50 TextEdit fResult;
|
|
51 List fParents= new ArrayList();
|
|
52 TextEdit fCurrentParent;
|
|
53
|
|
54 public static TextEdit perform(TextEdit source) {
|
|
55 PartialCopier copier= new PartialCopier();
|
|
56 source.accept(copier);
|
|
57 return copier.fResult;
|
|
58 }
|
|
59 private void manageCopy(TextEdit copy) {
|
|
60 if (fResult is null)
|
|
61 fResult= copy;
|
|
62 if (fCurrentParent !is null) {
|
|
63 fCurrentParent.addChild(copy);
|
|
64 }
|
|
65 fParents.add(fCurrentParent);
|
|
66 fCurrentParent= copy;
|
|
67 }
|
|
68 public void postVisit(TextEdit edit) {
|
|
69 fCurrentParent= (TextEdit)fParents.remove(fParents.size() - 1);
|
|
70 }
|
|
71 public bool visitNode(TextEdit edit) {
|
|
72 manageCopy(edit.doCopy());
|
|
73 return true;
|
|
74 }
|
|
75 public bool visit(CopySourceEdit edit) {
|
|
76 manageCopy(new RangeMarker(edit.getOffset(), edit.getLength()));
|
|
77 return true;
|
|
78 }
|
|
79 public bool visit(CopyTargetEdit edit) {
|
|
80 manageCopy(new InsertEdit(edit.getOffset(), edit.getSourceEdit().getContent()));
|
|
81 return true;
|
|
82 }
|
|
83 public bool visit(MoveSourceEdit edit) {
|
|
84 manageCopy(new DeleteEdit(edit.getOffset(), edit.getLength()));
|
|
85 return true;
|
|
86 }
|
|
87 public bool visit(MoveTargetEdit edit) {
|
|
88 manageCopy(new InsertEdit(edit.getOffset(), edit.getSourceEdit().getContent()));
|
|
89 return true;
|
|
90 }
|
|
91 }
|
|
92
|
|
93 /**
|
|
94 * Constructs a new copy source edit.
|
|
95 *
|
|
96 * @param offset the edit's offset
|
|
97 * @param length the edit's length
|
|
98 */
|
130
|
99 public this(int offset, int length) {
|
129
|
100 super(offset, length);
|
|
101 }
|
|
102
|
|
103 /**
|
|
104 * Constructs a new copy source edit.
|
|
105 *
|
|
106 * @param offset the edit's offset
|
|
107 * @param length the edit's length
|
|
108 * @param target the edit's target
|
|
109 */
|
130
|
110 public this(int offset, int length, CopyTargetEdit target) {
|
129
|
111 this(offset, length);
|
|
112 setTargetEdit(target);
|
|
113 }
|
|
114
|
|
115 /*
|
|
116 * Copy Constructor
|
|
117 */
|
130
|
118 private this(CopySourceEdit other) {
|
129
|
119 super(other);
|
|
120 if (other.fModifier !is null)
|
|
121 fModifier= other.fModifier.copy();
|
|
122 }
|
|
123
|
|
124 /**
|
|
125 * Returns the associated target edit or <code>null</code>
|
|
126 * if no target edit is associated yet.
|
|
127 *
|
|
128 * @return the target edit or <code>null</code>
|
|
129 */
|
|
130 public CopyTargetEdit getTargetEdit() {
|
|
131 return fTarget;
|
|
132 }
|
|
133
|
|
134 /**
|
|
135 * Sets the target edit.
|
|
136 *
|
|
137 * @param edit the new target edit.
|
|
138 *
|
|
139 * @exception MalformedTreeException is thrown if the target edit
|
|
140 * is a direct or indirect child of the source edit
|
|
141 */
|
|
142 public void setTargetEdit(CopyTargetEdit edit) throws MalformedTreeException {
|
|
143 Assert.isNotNull(edit);
|
|
144 if (fTarget !is edit) {
|
|
145 fTarget= edit;
|
|
146 fTarget.setSourceEdit(this);
|
|
147 }
|
|
148 }
|
|
149
|
|
150 /**
|
|
151 * Returns the current source modifier or <code>null</code>
|
|
152 * if no source modifier is set.
|
|
153 *
|
|
154 * @return the source modifier
|
|
155 */
|
|
156 public ISourceModifier getSourceModifier() {
|
|
157 return fModifier;
|
|
158 }
|
|
159
|
|
160 /**
|
|
161 * Sets the optional source modifier.
|
|
162 *
|
|
163 * @param modifier the source modifier or <code>null</code>
|
|
164 * if no source modification is need.
|
|
165 */
|
|
166 public void setSourceModifier(ISourceModifier modifier) {
|
|
167 fModifier= modifier;
|
|
168 }
|
|
169
|
|
170 /*
|
|
171 * @see TextEdit#doCopy
|
|
172 */
|
|
173 protected TextEdit doCopy() {
|
|
174 return new CopySourceEdit(this);
|
|
175 }
|
|
176
|
|
177 /*
|
|
178 * @see TextEdit#accept0
|
|
179 */
|
|
180 protected void accept0(TextEditVisitor visitor) {
|
|
181 bool visitChildren= visitor.visit(this);
|
|
182 if (visitChildren) {
|
|
183 acceptChildren(visitor);
|
|
184 }
|
|
185 }
|
|
186
|
|
187 //---- API for CopyTargetEdit ------------------------------------------------
|
|
188
|
|
189 String getContent() {
|
|
190 // The source content can be null if the edit wasn't executed
|
|
191 // due to an exclusion list of the text edit processor. Return
|
|
192 // the empty string which can be moved without any harm.
|
|
193 if (fSourceContent is null)
|
|
194 return ""; //$NON-NLS-1$
|
|
195 return fSourceContent;
|
|
196 }
|
|
197
|
|
198 void clearContent() {
|
|
199 fSourceContent= null;
|
|
200 }
|
|
201
|
|
202 /*
|
|
203 * @see TextEdit#postProcessCopy
|
|
204 */
|
|
205 protected void postProcessCopy(TextEditCopier copier) {
|
|
206 if (fTarget !is null) {
|
|
207 CopySourceEdit source= (CopySourceEdit)copier.getCopy(this);
|
|
208 CopyTargetEdit target= (CopyTargetEdit)copier.getCopy(fTarget);
|
|
209 if (source !is null && target !is null)
|
|
210 source.setTargetEdit(target);
|
|
211 }
|
|
212 }
|
|
213
|
|
214 //---- consistency check ----------------------------------------------------
|
|
215
|
|
216 int traverseConsistencyCheck(TextEditProcessor processor, IDocument document, List sourceEdits) {
|
|
217 int result= super.traverseConsistencyCheck(processor, document, sourceEdits);
|
|
218 // Since source computation takes place in a recursive fashion (see
|
|
219 // performSourceComputation) we only do something if we don't have a
|
|
220 // computed source already.
|
|
221 if (fSourceContent is null) {
|
|
222 if (sourceEdits.size() <= result) {
|
|
223 List list= new ArrayList();
|
|
224 list.add(this);
|
|
225 for (int i= sourceEdits.size(); i < result; i++)
|
|
226 sourceEdits.add(null);
|
|
227 sourceEdits.add(list);
|
|
228 } else {
|
|
229 List list= (List)sourceEdits.get(result);
|
|
230 if (list is null) {
|
|
231 list= new ArrayList();
|
|
232 sourceEdits.add(result, list);
|
|
233 }
|
|
234 list.add(this);
|
|
235 }
|
|
236 }
|
|
237 return result;
|
|
238 }
|
|
239
|
|
240 void performConsistencyCheck(TextEditProcessor processor, IDocument document) throws MalformedTreeException {
|
|
241 if (fTarget is null)
|
|
242 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("CopySourceEdit.no_target")); //$NON-NLS-1$
|
|
243 if (fTarget.getSourceEdit() !is this)
|
|
244 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("CopySourceEdit.different_source")); //$NON-NLS-1$
|
|
245 /* causes ASTRewrite to fail
|
|
246 if (getRoot() !is fTarget.getRoot())
|
|
247 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("CopySourceEdit.different_tree")); //$NON-NLS-1$
|
|
248 */
|
|
249 }
|
|
250
|
|
251 //---- source computation -------------------------------------------------------
|
|
252
|
|
253 void traverseSourceComputation(TextEditProcessor processor, IDocument document) {
|
|
254 // always perform source computation independent of processor.considerEdit
|
|
255 // The target might need the source and the source is computed in a
|
|
256 // temporary buffer.
|
|
257 performSourceComputation(processor, document);
|
|
258 }
|
|
259
|
|
260 void performSourceComputation(TextEditProcessor processor, IDocument document) {
|
|
261 try {
|
|
262 MultiTextEdit root= new MultiTextEdit(getOffset(), getLength());
|
|
263 root.internalSetChildren(internalGetChildren());
|
|
264 fSourceContent= document.get(getOffset(), getLength());
|
|
265 fSourceRoot= PartialCopier.perform(root);
|
|
266 fSourceRoot.internalMoveTree(-getOffset());
|
|
267 if (fSourceRoot.hasChildren()) {
|
|
268 EditDocument subDocument= new EditDocument(fSourceContent);
|
|
269 TextEditProcessor subProcessor= TextEditProcessor.createSourceComputationProcessor(subDocument, fSourceRoot, TextEdit.NONE);
|
|
270 subProcessor.performEdits();
|
|
271 if (needsTransformation())
|
|
272 applyTransformation(subDocument);
|
|
273 fSourceContent= subDocument.get();
|
|
274 fSourceRoot= null;
|
|
275 } else {
|
|
276 if (needsTransformation()) {
|
|
277 EditDocument subDocument= new EditDocument(fSourceContent);
|
|
278 applyTransformation(subDocument);
|
|
279 fSourceContent= subDocument.get();
|
|
280 }
|
|
281 }
|
|
282 } catch (BadLocationException cannotHappen) {
|
|
283 Assert.isTrue(false);
|
|
284 }
|
|
285 }
|
|
286
|
|
287 private bool needsTransformation() {
|
|
288 return fModifier !is null;
|
|
289 }
|
|
290
|
|
291 private void applyTransformation(IDocument document) throws MalformedTreeException {
|
|
292 TextEdit newEdit= new MultiTextEdit(0, document.getLength());
|
|
293 ReplaceEdit[] replaces= fModifier.getModifications(document.get());
|
|
294 for (int i= 0; i < replaces.length; i++) {
|
|
295 newEdit.addChild(replaces[i]);
|
|
296 }
|
|
297 try {
|
|
298 newEdit.apply(document, TextEdit.NONE);
|
|
299 } catch (BadLocationException cannotHappen) {
|
|
300 Assert.isTrue(false);
|
|
301 }
|
|
302 }
|
|
303
|
|
304 //---- document updating ----------------------------------------------------------------
|
|
305
|
|
306 int performDocumentUpdating(IDocument document) throws BadLocationException {
|
|
307 fDelta= 0;
|
|
308 return fDelta;
|
|
309 }
|
|
310
|
|
311 //---- region updating ----------------------------------------------------------------
|
|
312
|
|
313 /*
|
|
314 * @see TextEdit#deleteChildren
|
|
315 */
|
|
316 bool deleteChildren() {
|
|
317 return false;
|
|
318 }
|
|
319 }
|