Skip to main content
summaryrefslogtreecommitdiffstats
blob: fcd83ac311713aefaf1f73a2be84f6bf6ff75717 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.text.edits;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.Assert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;

/**
 * A copy source edit denotes the source of a copy operation. Copy
 * source edits are only valid inside an edit tree if they have a
 * corresponding traget edit. Furthermore the corresponding
 * target edit can't be a direct or indirect child of the source 
 * edit. Violating one of two requirements will result in a <code>
 * MalformedTreeException</code> when executing the edit tree.
 * <p>
 * A copy source edit can manange an optional source modifier. A
 * source modifier can provide a set of replace edits which will
 * to applied to the source before it gets inserted at the target
 * position.
 * 
 * @see org.eclipse.text.edits.CopyTargetEdit
 *  
 * @since 3.0 
 */
public final class CopySourceEdit extends TextEdit {

	private CopyTargetEdit fTarget;
	private ISourceModifier fModifier;
	
	private String fSourceContent;
	private TextEdit fSourceRoot;
	
	private static class PartialCopier extends TextEditVisitor {
		TextEdit fResult;
		List fParents= new ArrayList();
		TextEdit fCurrentParent;

		public static TextEdit perform(TextEdit source) {
			PartialCopier copier= new PartialCopier();
			source.accept(copier);
			return copier.fResult;
		}
		private void manageCopy(TextEdit copy) {
			if (fResult == null)
				fResult= copy;
			if (fCurrentParent != null) {
				fCurrentParent.addChild(copy);
			}
			fParents.add(fCurrentParent);
			fCurrentParent= copy;
		}
		public void postVisit(TextEdit edit) {
			fCurrentParent= (TextEdit)fParents.remove(fParents.size() - 1);
		}
		public boolean visitNode(TextEdit edit) {
			manageCopy(edit.doCopy());
			return true;
		}
		public boolean visit(CopySourceEdit edit) {
			manageCopy(new RangeMarker(edit.getOffset(), edit.getLength()));
			return true;
		}
		public boolean visit(CopyTargetEdit edit) {
			manageCopy(new InsertEdit(edit.getOffset(), edit.getSourceEdit().getContent()));
			return true;
		}
		public boolean visit(MoveSourceEdit edit) {
			manageCopy(new DeleteEdit(edit.getOffset(), edit.getLength()));
			return true;
		}
		public boolean visit(MoveTargetEdit edit) {
			manageCopy(new InsertEdit(edit.getOffset(), edit.getSourceEdit().getContent()));
			return true;
		}
	}
	
	/**
	 * Constructs a new copy source edit.
	 * 
	 * @param offset the edit's offset
	 * @param length the edit's length
	 */
	public CopySourceEdit(int offset, int length) {
		super(offset, length);
	}

	/**
	 * Constructs a new copy source edit.
	 * 
	 * @param offset the edit's offset
	 * @param length the edit's length
	 * @param target the edit's target
	 */
	public CopySourceEdit(int offset, int length, CopyTargetEdit target) {
		this(offset, length);
		setTargetEdit(target);
	}

	/*
	 * Copy Constructor
	 */
	private CopySourceEdit(CopySourceEdit other) {
		super(other);
		if (other.fModifier != null)
			fModifier= other.fModifier.copy();
	}

	/**
	 * Returns the associated traget edit or <code>null</code>
	 * if no target edit is associated yet.
	 * 
	 * @return the target edit or <code>null</code>
	 */
	public CopyTargetEdit getTargetEdit() {
		return fTarget;
	}
	
	/**
	 * Sets the target edit.
	 * 
	 * @param edit the new target edit.
	 * 
	 * @exception MalformedTreeException is thrown if the target edit
	 *  is a direct or indirect child of the source edit
	 */
	public void setTargetEdit(CopyTargetEdit edit) throws MalformedTreeException {
		Assert.isNotNull(edit);
		if (fTarget != edit) {
			fTarget= edit;
			fTarget.setSourceEdit(this);
		}
	}
	
	/**
	 * Returns the current source modifier or <code>null</code>
	 * if no source modifier is set.
	 * 
	 * @return the source modifier
	 */
	public ISourceModifier getSourceModifier() {
		return fModifier;
	}
	
	/**
	 * Sets the optional source modifier.
	 * 
	 * @param modifier the source modifier or <code>null</code>
	 *  if no source modification is need. 
	 */
	public void setSourceModifier(ISourceModifier modifier) {
		fModifier= modifier;
	}
	
	/* non Java-doc
	 * @see TextEdit#doCopy
	 */	
	protected TextEdit doCopy() {
		return new CopySourceEdit(this);
	}
	
	/* (non-Javadoc)
	 * @see TextEdit#accept0
	 */
	protected void accept0(TextEditVisitor visitor) {
		boolean visitChildren = visitor.visit(this);
		if (visitChildren) {
			acceptChildren(visitor);
		}
	}

	//---- API for CopyTargetEdit ------------------------------------------------

	/* package */ String getContent() {
		// The source content can be null if the edit wasn't executed
		// due to an exclusion list of the text edit processor. Return
		// the empty string which can be moved without any harm.
		if (fSourceContent == null)
			return ""; //$NON-NLS-1$
		return fSourceContent;
	}
	
	/* package */ void clearContent() {
		fSourceContent= null;
	}
	
	/* non Java-doc
	 * @see TextEdit#postProcessCopy
	 */	
	protected void postProcessCopy(TextEditCopier copier) {
		if (fTarget != null) {
			CopySourceEdit source= (CopySourceEdit)copier.getCopy(this);
			CopyTargetEdit target= (CopyTargetEdit)copier.getCopy(fTarget);
			if (source != null && target != null)
				source.setTargetEdit(target);
		}
	}
	
	//---- consistency check ----------------------------------------------------
	
	/* package */ int traverseConsistencyCheck(TextEditProcessor processor, IDocument document, List sourceEdits) {
		int result= super.traverseConsistencyCheck(processor, document, sourceEdits);
		// Since source computation takes place in a recursive fashion (see
		// performSourceComputation) we only do something if we don't have a 
		// computated source already.
		if (fSourceContent == null) {
			if (sourceEdits.size() <= result) {
				List list= new ArrayList();
				list.add(this);
				for (int i= sourceEdits.size(); i < result; i++)
					sourceEdits.add(null);
				sourceEdits.add(list);
			} else {
				List list= (List)sourceEdits.get(result);
				if (list == null) {
					list= new ArrayList();
					sourceEdits.add(result, list);
				}
				list.add(this);
			}
		}
		return result;
	}
	
	/* package */ void performConsistencyCheck(TextEditProcessor processor, IDocument document) throws MalformedTreeException {
		if (fTarget == null)
			throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("CopySourceEdit.no_target")); //$NON-NLS-1$
		if (fTarget.getSourceEdit() != this)
			throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("CopySourceEdit.different_source")); //$NON-NLS-1$
	}
	
	//---- source computation -------------------------------------------------------
	
	/* package */ void traverseSourceComputation(TextEditProcessor processor, IDocument document) {
		if (processor.considerEdit(this)) {
			performSourceComputation(processor, document);
		}
	}
	
	/* package */ void performSourceComputation(TextEditProcessor processor, IDocument document) {
		try {
			MultiTextEdit root= new MultiTextEdit(getOffset(), getLength());
			root.internalSetChildren(internalGetChildren());
			fSourceContent= document.get(getOffset(), getLength());
			fSourceRoot= PartialCopier.perform(root);
			fSourceRoot.moveTree(-getOffset());
			if (fSourceRoot.hasChildren()) {
				EditDocument subDocument= new EditDocument(fSourceContent);
				fSourceRoot.apply(subDocument, TextEdit.NONE);
				if (needsTransformation())
					applyTransformation(subDocument);
				fSourceContent= subDocument.get();
				fSourceRoot= null;
			} else {
				if (needsTransformation()) {
					EditDocument subDocument= new EditDocument(fSourceContent);
					applyTransformation(subDocument);
					fSourceContent= subDocument.get();
				}
			}
		} catch (BadLocationException cannotHappen) {
			Assert.isTrue(false);
		}		
	}
	
	private boolean needsTransformation() {
		return fModifier != null;
	}
	
	private void applyTransformation(IDocument document) throws MalformedTreeException {
		TextEdit newEdit= new MultiTextEdit(0, document.getLength());
		ReplaceEdit[] replaces= fModifier.getModifications(document.get());
		for (int i= 0; i < replaces.length; i++) {
			newEdit.addChild(replaces[i]);
		}
		try {
			newEdit.apply(document, TextEdit.NONE);
		} catch (BadLocationException cannotHappen) {
			Assert.isTrue(false);
		}
	}
	
	//---- document updating ----------------------------------------------------------------
	
	/* package */ int performDocumentUpdating(IDocument document) throws BadLocationException {
		fDelta= 0;
		return fDelta;
	}

	//---- region updating ----------------------------------------------------------------
	
	/* non Java-doc
	 * @see TextEdit#deleteChildren
	 */	
	/* package */ boolean deleteChildren() {
		return false;
	}	
}

Back to the top