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.MoveSourceEdit;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import java.util.ArrayList;
|
|
18 import java.util.Arrays;
|
|
19 import java.util.HashMap;
|
|
20 import java.util.Iterator;
|
|
21 import java.util.List;
|
|
22 import java.util.Map;
|
|
23
|
|
24 import dwtx.core.runtime.Assert;
|
|
25 import dwtx.jface.text.BadLocationException;
|
|
26 import dwtx.jface.text.IDocument;
|
|
27 import dwtx.jface.text.IRegion;
|
|
28 import dwtx.jface.text.Region;
|
|
29
|
|
30 /**
|
|
31 * A move source edit denotes the source of a move operation. Move
|
|
32 * source edits are only valid inside an edit tree if they have a
|
|
33 * corresponding target edit. Furthermore the corresponding target
|
|
34 * edit can't be a direct or indirect child of the source edit.
|
|
35 * Violating one of two requirements will result in a <code>
|
|
36 * MalformedTreeException</code> when executing the edit tree.
|
|
37 * <p>
|
|
38 * A move source edit can manage an optional source modifier. A
|
|
39 * source modifier can provide a set of replace edits which will
|
|
40 * to applied to the source before it gets inserted at the target
|
|
41 * position.
|
|
42 *
|
|
43 * @see dwtx.text.edits.MoveTargetEdit
|
|
44 * @see dwtx.text.edits.CopySourceEdit
|
|
45 *
|
|
46 * @since 3.0
|
|
47 */
|
|
48 public final class MoveSourceEdit : TextEdit {
|
|
49
|
|
50 private MoveTargetEdit fTarget;
|
|
51 private ISourceModifier fModifier;
|
|
52
|
|
53 private String fSourceContent;
|
|
54 private MultiTextEdit fSourceRoot;
|
|
55
|
|
56 /**
|
|
57 * Constructs a new move source edit.
|
|
58 *
|
|
59 * @param offset the edit's offset
|
|
60 * @param length the edit's length
|
|
61 */
|
130
|
62 public this(int offset, int length) {
|
129
|
63 super(offset, length);
|
|
64 }
|
|
65
|
|
66 /**
|
|
67 * Constructs a new copy source edit.
|
|
68 *
|
|
69 * @param offset the edit's offset
|
|
70 * @param length the edit's length
|
|
71 * @param target the edit's target
|
|
72 */
|
130
|
73 public this(int offset, int length, MoveTargetEdit target) {
|
129
|
74 this(offset, length);
|
|
75 setTargetEdit(target);
|
|
76 }
|
|
77
|
|
78 /*
|
|
79 * Copy constructor
|
|
80 */
|
130
|
81 private this(MoveSourceEdit other) {
|
129
|
82 super(other);
|
|
83 if (other.fModifier !is null)
|
|
84 fModifier= other.fModifier.copy();
|
|
85 }
|
|
86
|
|
87 /**
|
|
88 * Returns the associated target edit or <code>null</code>
|
|
89 * if no target edit is associated yet.
|
|
90 *
|
|
91 * @return the target edit or <code>null</code>
|
|
92 */
|
130
|
93 public this getTargetEdit() {
|
129
|
94 return fTarget;
|
|
95 }
|
|
96
|
|
97 /**
|
|
98 * Sets the target edit.
|
|
99 *
|
|
100 * @param edit the new target edit.
|
|
101 *
|
|
102 * @exception MalformedTreeException is thrown if the target edit
|
|
103 * is a direct or indirect child of the source edit
|
|
104 */
|
|
105 public void setTargetEdit(MoveTargetEdit edit) {
|
|
106 fTarget= edit;
|
|
107 fTarget.setSourceEdit(this);
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 * Returns the current source modifier or <code>null</code>
|
|
112 * if no source modifier is set.
|
|
113 *
|
|
114 * @return the source modifier
|
|
115 */
|
|
116 public ISourceModifier getSourceModifier() {
|
|
117 return fModifier;
|
|
118 }
|
|
119
|
|
120 /**
|
|
121 * Sets the optional source modifier.
|
|
122 *
|
|
123 * @param modifier the source modifier or <code>null</code>
|
|
124 * if no source modification is need.
|
|
125 */
|
|
126 public void setSourceModifier(ISourceModifier modifier) {
|
|
127 fModifier= modifier;
|
|
128 }
|
|
129
|
|
130 //---- API for MoveTargetEdit ---------------------------------------------
|
|
131
|
|
132 String getContent() {
|
|
133 // The source content can be null if the edit wasn't executed
|
|
134 // due to an exclusion list of the text edit processor. Return
|
|
135 // the empty string which can be moved without any harm.
|
|
136 if (fSourceContent is null)
|
|
137 return ""; //$NON-NLS-1$
|
|
138 return fSourceContent;
|
|
139 }
|
|
140
|
|
141 MultiTextEdit getSourceRoot() {
|
|
142 return fSourceRoot;
|
|
143 }
|
|
144
|
|
145 void clearContent() {
|
|
146 fSourceContent= null;
|
|
147 fSourceRoot= null;
|
|
148 }
|
|
149
|
|
150 //---- Copying -------------------------------------------------------------
|
|
151
|
|
152 /*
|
|
153 * @see TextEdit#doCopy
|
|
154 */
|
|
155 protected TextEdit doCopy() {
|
|
156 return new MoveSourceEdit(this);
|
|
157 }
|
|
158
|
|
159 /*
|
|
160 * @see TextEdit#postProcessCopy
|
|
161 */
|
|
162 protected void postProcessCopy(TextEditCopier copier) {
|
|
163 if (fTarget !is null) {
|
|
164 MoveSourceEdit source= (MoveSourceEdit)copier.getCopy(this);
|
|
165 MoveTargetEdit target= (MoveTargetEdit)copier.getCopy(fTarget);
|
|
166 if (source !is null && target !is null)
|
|
167 source.setTargetEdit(target);
|
|
168 }
|
|
169 }
|
|
170
|
|
171 //---- Visitor -------------------------------------------------------------
|
|
172
|
|
173 /*
|
|
174 * @see TextEdit#accept0
|
|
175 */
|
|
176 protected void accept0(TextEditVisitor visitor) {
|
|
177 bool visitChildren= visitor.visit(this);
|
|
178 if (visitChildren) {
|
|
179 acceptChildren(visitor);
|
|
180 }
|
|
181 }
|
|
182
|
|
183 //---- consistency check ----------------------------------------------------------------
|
|
184
|
|
185 int traverseConsistencyCheck(TextEditProcessor processor, IDocument document, List sourceEdits) {
|
|
186 int result= super.traverseConsistencyCheck(processor, document, sourceEdits);
|
|
187 // Since source computation takes place in a recursive fashion (see
|
|
188 // performSourceComputation) we only do something if we don't have a
|
|
189 // computed source already.
|
|
190 if (fSourceContent is null) {
|
|
191 if (sourceEdits.size() <= result) {
|
|
192 List list= new ArrayList();
|
|
193 list.add(this);
|
|
194 for (int i= sourceEdits.size(); i < result; i++)
|
|
195 sourceEdits.add(null);
|
|
196 sourceEdits.add(list);
|
|
197 } else {
|
|
198 List list= (List)sourceEdits.get(result);
|
|
199 if (list is null) {
|
|
200 list= new ArrayList();
|
|
201 sourceEdits.add(result, list);
|
|
202 }
|
|
203 list.add(this);
|
|
204 }
|
|
205 }
|
|
206 return result;
|
|
207 }
|
|
208
|
|
209 void performConsistencyCheck(TextEditProcessor processor, IDocument document) throws MalformedTreeException {
|
|
210 if (fTarget is null)
|
|
211 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("MoveSourceEdit.no_target")); //$NON-NLS-1$
|
|
212 if (fTarget.getSourceEdit() !is this)
|
|
213 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("MoveSourceEdit.different_source")); //$NON-NLS-1$
|
|
214 /* Causes AST rewrite to fail
|
|
215 if (getRoot() !is fTarget.getRoot())
|
|
216 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("MoveSourceEdit.different_tree")); //$NON-NLS-1$
|
|
217 */
|
|
218 }
|
|
219
|
|
220 //---- source computation --------------------------------------------------------------
|
|
221
|
|
222 void traverseSourceComputation(TextEditProcessor processor, IDocument document) {
|
|
223 // always perform source computation independent of processor.considerEdit
|
|
224 // The target might need the source and the source is computed in a
|
|
225 // temporary buffer.
|
|
226 performSourceComputation(processor, document);
|
|
227 }
|
|
228
|
|
229 void performSourceComputation(TextEditProcessor processor, IDocument document) {
|
|
230 try {
|
|
231 TextEdit[] children= removeChildren();
|
|
232 if (children.length > 0) {
|
|
233 String content= document.get(getOffset(), getLength());
|
|
234 EditDocument subDocument= new EditDocument(content);
|
|
235 fSourceRoot= new MultiTextEdit(getOffset(), getLength());
|
|
236 fSourceRoot.addChildren(children);
|
|
237 fSourceRoot.internalMoveTree(-getOffset());
|
|
238 int processingStyle= getStyle(processor);
|
|
239 TextEditProcessor subProcessor= TextEditProcessor.createSourceComputationProcessor(subDocument, fSourceRoot, processingStyle);
|
|
240 subProcessor.performEdits();
|
|
241 if (needsTransformation())
|
|
242 applyTransformation(subDocument, processingStyle);
|
|
243 fSourceContent= subDocument.get();
|
|
244 } else {
|
|
245 fSourceContent= document.get(getOffset(), getLength());
|
|
246 if (needsTransformation()) {
|
|
247 EditDocument subDocument= new EditDocument(fSourceContent);
|
|
248 applyTransformation(subDocument, getStyle(processor));
|
|
249 fSourceContent= subDocument.get();
|
|
250 }
|
|
251 }
|
|
252 } catch (BadLocationException cannotHappen) {
|
|
253 Assert.isTrue(false);
|
|
254 }
|
|
255 }
|
|
256
|
|
257 private int getStyle(TextEditProcessor processor) {
|
|
258 // we never need undo while performing local edits.
|
|
259 if ((processor.getStyle() & TextEdit.UPDATE_REGIONS) !is 0)
|
|
260 return TextEdit.UPDATE_REGIONS;
|
|
261 return TextEdit.NONE;
|
|
262 }
|
|
263
|
|
264 //---- document updating ----------------------------------------------------------------
|
|
265
|
|
266 int performDocumentUpdating(IDocument document) throws BadLocationException {
|
|
267 document.replace(getOffset(), getLength(), ""); //$NON-NLS-1$
|
|
268 fDelta= -getLength();
|
|
269 return fDelta;
|
|
270 }
|
|
271
|
|
272 //---- region updating --------------------------------------------------------------
|
|
273
|
|
274 /*
|
|
275 * @see TextEdit#deleteChildren
|
|
276 */
|
|
277 bool deleteChildren() {
|
|
278 return false;
|
|
279 }
|
|
280
|
|
281 //---- content transformation --------------------------------------------------
|
|
282
|
|
283 private bool needsTransformation() {
|
|
284 return fModifier !is null;
|
|
285 }
|
|
286
|
|
287 private void applyTransformation(IDocument document, int style) throws MalformedTreeException {
|
|
288 if ((style & TextEdit.UPDATE_REGIONS) !is 0 && fSourceRoot !is null) {
|
|
289 Map editMap= new HashMap();
|
|
290 TextEdit newEdit= createEdit(editMap);
|
|
291 List replaces= new ArrayList(Arrays.asList(fModifier.getModifications(document.get())));
|
|
292 insertEdits(newEdit, replaces);
|
|
293 try {
|
|
294 newEdit.apply(document, style);
|
|
295 } catch (BadLocationException cannotHappen) {
|
|
296 Assert.isTrue(false);
|
|
297 }
|
|
298 restorePositions(editMap);
|
|
299 } else {
|
|
300 MultiTextEdit newEdit= new MultiTextEdit(0, document.getLength());
|
|
301 TextEdit[] replaces= fModifier.getModifications(document.get());
|
|
302 for (int i= 0; i < replaces.length; i++) {
|
|
303 newEdit.addChild(replaces[i]);
|
|
304 }
|
|
305 try {
|
|
306 newEdit.apply(document, style);
|
|
307 } catch (BadLocationException cannotHappen) {
|
|
308 Assert.isTrue(false);
|
|
309 }
|
|
310 }
|
|
311 }
|
|
312
|
|
313 private TextEdit createEdit(Map editMap) {
|
|
314 MultiTextEdit result= new MultiTextEdit(0, fSourceRoot.getLength());
|
|
315 editMap.put(result, fSourceRoot);
|
|
316 createEdit(fSourceRoot, result, editMap);
|
|
317 return result;
|
|
318 }
|
|
319
|
|
320 private static void createEdit(TextEdit source, TextEdit target, Map editMap) {
|
|
321 TextEdit[] children= source.getChildren();
|
|
322 for (int i= 0; i < children.length; i++) {
|
|
323 TextEdit child= children[i];
|
|
324 // a deleted child remains deleted even if the temporary buffer
|
|
325 // gets modified.
|
|
326 if (child.isDeleted())
|
|
327 continue;
|
|
328 RangeMarker marker= new RangeMarker(child.getOffset(), child.getLength());
|
|
329 target.addChild(marker);
|
|
330 editMap.put(marker, child);
|
|
331 createEdit(child, marker, editMap);
|
|
332 }
|
|
333 }
|
|
334
|
|
335 private void insertEdits(TextEdit root, List edits) {
|
|
336 while(edits.size() > 0) {
|
|
337 ReplaceEdit edit= (ReplaceEdit)edits.remove(0);
|
|
338 insert(root, edit, edits);
|
|
339 }
|
|
340 }
|
|
341 private static void insert(TextEdit parent, ReplaceEdit edit, List edits) {
|
|
342 if (!parent.hasChildren()) {
|
|
343 parent.addChild(edit);
|
|
344 return;
|
|
345 }
|
|
346 TextEdit[] children= parent.getChildren();
|
|
347 // First dive down to find the right parent.
|
|
348 int removed= 0;
|
|
349 for (int i= 0; i < children.length; i++) {
|
|
350 TextEdit child= children[i];
|
|
351 if (child.covers(edit)) {
|
|
352 insert(child, edit, edits);
|
|
353 return;
|
|
354 } else if (edit.covers(child)) {
|
|
355 parent.removeChild(i - removed++);
|
|
356 edit.addChild(child);
|
|
357 } else {
|
|
358 IRegion intersect= intersect(edit, child);
|
|
359 if (intersect !is null) {
|
|
360 ReplaceEdit[] splits= splitEdit(edit, intersect);
|
|
361 insert(child, splits[0], edits);
|
|
362 edits.add(splits[1]);
|
|
363 return;
|
|
364 }
|
|
365 }
|
|
366 }
|
|
367 parent.addChild(edit);
|
|
368 }
|
|
369
|
|
370 public static IRegion intersect(TextEdit op1, TextEdit op2) {
|
|
371 int offset1= op1.getOffset();
|
|
372 int length1= op1.getLength();
|
|
373 int end1= offset1 + length1 - 1;
|
|
374 int offset2= op2.getOffset();
|
|
375 if (end1 < offset2)
|
|
376 return null;
|
|
377 int length2= op2.getLength();
|
|
378 int end2= offset2 + length2 - 1;
|
|
379 if (end2 < offset1)
|
|
380 return null;
|
130
|
381
|
129
|
382 int end= Math.min(end1, end2);
|
|
383 if (offset1 < offset2) {
|
|
384 return new Region(offset2, end - offset2 + 1);
|
|
385 }
|
|
386 return new Region(offset1, end - offset1 + 1);
|
|
387 }
|
|
388
|
|
389 private static ReplaceEdit[] splitEdit(ReplaceEdit edit, IRegion intersect) {
|
|
390 if (edit.getOffset() !is intersect.getOffset())
|
|
391 return splitIntersectRight(edit, intersect);
|
|
392 return splitIntersectLeft(edit, intersect);
|
|
393 }
|
|
394
|
|
395 private static ReplaceEdit[] splitIntersectRight(ReplaceEdit edit, IRegion intersect) {
|
|
396 ReplaceEdit[] result= new ReplaceEdit[2];
|
|
397 // this is the actual delete. We use replace to only deal with one type
|
|
398 result[0]= new ReplaceEdit(intersect.getOffset(), intersect.getLength(), ""); //$NON-NLS-1$
|
|
399 result[1]= new ReplaceEdit(
|
|
400 edit.getOffset(),
|
|
401 intersect.getOffset() - edit.getOffset(),
|
|
402 edit.getText());
|
|
403 return result;
|
|
404 }
|
|
405
|
|
406 private static ReplaceEdit[] splitIntersectLeft(ReplaceEdit edit, IRegion intersect) {
|
|
407 ReplaceEdit[] result= new ReplaceEdit[2];
|
|
408 result[0]= new ReplaceEdit(intersect.getOffset(), intersect.getLength(), edit.getText());
|
|
409 result[1]= new ReplaceEdit( // this is the actual delete. We use replace to only deal with one type
|
|
410 intersect.getOffset() + intersect.getLength(),
|
|
411 edit.getLength() - intersect.getLength(),
|
|
412 ""); //$NON-NLS-1$
|
|
413 return result;
|
|
414 }
|
|
415
|
|
416 private static void restorePositions(Map editMap) {
|
|
417 for (Iterator iter= editMap.keySet().iterator(); iter.hasNext();) {
|
|
418 TextEdit marker= (TextEdit)iter.next();
|
|
419 TextEdit edit= (TextEdit)editMap.get(marker);
|
|
420 if (marker.isDeleted()) {
|
|
421 edit.markAsDeleted();
|
|
422 } else {
|
|
423 edit.adjustOffset(marker.getOffset() - edit.getOffset());
|
|
424 edit.adjustLength(marker.getLength() - edit.getLength());
|
|
425 }
|
|
426 }
|
|
427 }
|
|
428 }
|