comparison org.eclipse.text/src/org/eclipse/text/edits/MoveTargetEdit.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children 5feec68b4556
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 org.eclipse.text.edits.MoveTargetEdit;
14
15 import org.eclipse.text.edits.MultiTextEdit; // packageimport
16 import org.eclipse.text.edits.CopySourceEdit; // packageimport
17 import org.eclipse.text.edits.MoveSourceEdit; // packageimport
18 import org.eclipse.text.edits.CopyingRangeMarker; // packageimport
19 import org.eclipse.text.edits.ReplaceEdit; // packageimport
20 import org.eclipse.text.edits.EditDocument; // packageimport
21 import org.eclipse.text.edits.UndoCollector; // packageimport
22 import org.eclipse.text.edits.DeleteEdit; // packageimport
23 import org.eclipse.text.edits.CopyTargetEdit; // packageimport
24 import org.eclipse.text.edits.TextEditCopier; // packageimport
25 import org.eclipse.text.edits.ISourceModifier; // packageimport
26 import org.eclipse.text.edits.TextEditMessages; // packageimport
27 import org.eclipse.text.edits.TextEditProcessor; // packageimport
28 import org.eclipse.text.edits.MalformedTreeException; // packageimport
29 import org.eclipse.text.edits.TreeIterationInfo; // packageimport
30 import org.eclipse.text.edits.TextEditVisitor; // packageimport
31 import org.eclipse.text.edits.TextEditGroup; // packageimport
32 import org.eclipse.text.edits.TextEdit; // packageimport
33 import org.eclipse.text.edits.RangeMarker; // packageimport
34 import org.eclipse.text.edits.UndoEdit; // packageimport
35 import org.eclipse.text.edits.InsertEdit; // packageimport
36
37
38 import java.lang.all;
39 import java.util.List;
40 import java.util.ArrayList;
41 import java.util.Set;
42
43
44
45 import org.eclipse.jface.text.BadLocationException;
46 import org.eclipse.jface.text.IDocument;
47
48 /**
49 * A move target edit denotes the target of a move operation. Move
50 * target edits are only valid inside an edit tree if they have a
51 * corresponding source edit. Furthermore a target edit can't
52 * can't be a direct or indirect child of its associated source edit.
53 * Violating one of two requirements will result in a <code>
54 * MalformedTreeException</code> when executing the edit tree.
55 * <p>
56 * Move target edits can't be used as a parent for other edits.
57 * Trying to add an edit to a move target edit results in a <code>
58 * MalformedTreeException</code> as well.
59 *
60 * @see org.eclipse.text.edits.MoveSourceEdit
61 * @see org.eclipse.text.edits.CopyTargetEdit
62 *
63 * @since 3.0
64 */
65 public final class MoveTargetEdit : TextEdit {
66
67 private MoveSourceEdit fSource;
68
69 /**
70 * Constructs a new move target edit
71 *
72 * @param offset the edit's offset
73 */
74 public this(int offset) {
75 super(offset, 0);
76 }
77
78 /**
79 * Constructs an new move target edit
80 *
81 * @param offset the edit's offset
82 * @param source the corresponding source edit
83 */
84 public this(int offset, MoveSourceEdit source) {
85 this(offset);
86 setSourceEdit(source);
87 }
88
89 /*
90 * Copy constructor
91 */
92 private this(MoveTargetEdit other) {
93 super(other);
94 }
95
96 /**
97 * Returns the associated source edit or <code>null</code>
98 * if no source edit is associated yet.
99 *
100 * @return the source edit or <code>null</code>
101 */
102 public MoveSourceEdit getSourceEdit() {
103 return fSource;
104 }
105
106 /**
107 * Sets the source edit.
108 *
109 * @param edit the source edit
110 *
111 * @exception MalformedTreeException is thrown if the target edit
112 * is a direct or indirect child of the source edit
113 */
114 public void setSourceEdit(MoveSourceEdit edit) {
115 if (fSource !is edit) {
116 fSource= edit;
117 fSource.setTargetEdit(this);
118 TextEdit parent= getParent();
119 while (parent !is null) {
120 if (parent is fSource)
121 throw new MalformedTreeException(parent, this, TextEditMessages.getString("MoveTargetEdit.wrong_parent")); //$NON-NLS-1$
122 parent= parent.getParent();
123 }
124 }
125 }
126
127 /*
128 * @see TextEdit#doCopy
129 */
130 protected TextEdit doCopy() {
131 return new MoveTargetEdit(this);
132 }
133
134 /*
135 * @see TextEdit#postProcessCopy
136 */
137 protected void postProcessCopy(TextEditCopier copier) {
138 if (fSource !is null) {
139 MoveTargetEdit target= cast(MoveTargetEdit)copier.getCopy(this);
140 MoveSourceEdit source= cast(MoveSourceEdit)copier.getCopy(fSource);
141 if (target !is null && source !is null)
142 target.setSourceEdit(source);
143 }
144 }
145
146 /*
147 * @see TextEdit#accept0
148 */
149 protected void accept0(TextEditVisitor visitor) {
150 bool visitChildren= visitor.visit(this);
151 if (visitChildren) {
152 acceptChildren(visitor);
153 }
154 }
155
156 //---- consistency check ----------------------------------------------------------
157
158 /*
159 * @see TextEdit#traverseConsistencyCheck
160 */
161 int traverseConsistencyCheck(TextEditProcessor processor, IDocument document, List sourceEdits) {
162 return super.traverseConsistencyCheck(processor, document, sourceEdits) + 1;
163 }
164
165 /*
166 * @see TextEdit#performConsistencyCheck
167 */
168 void performConsistencyCheck(TextEditProcessor processor, IDocument document) {
169 if (fSource is null)
170 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("MoveTargetEdit.no_source")); //$NON-NLS-1$
171 if (fSource.getTargetEdit() !is this)
172 throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("MoveTargetEdit.different_target")); //$NON-NLS-1$
173 }
174
175 //---- document updating ----------------------------------------------------------------
176
177 /*
178 * @see TextEdit#performDocumentUpdating
179 */
180 int performDocumentUpdating(IDocument document) {
181 String source= fSource.getContent();
182 document.replace(getOffset(), getLength(), source);
183 fDelta= source.length() - getLength();
184
185 MultiTextEdit sourceRoot= fSource.getSourceRoot();
186 if (sourceRoot !is null) {
187 sourceRoot.internalMoveTree(getOffset());
188 TextEdit[] sourceChildren= sourceRoot.removeChildren();
189 List children= new ArrayList(sourceChildren.length);
190 for (int i= 0; i < sourceChildren.length; i++) {
191 TextEdit child= sourceChildren[i];
192 child.internalSetParent(this);
193 children.add(child);
194 }
195 internalSetChildren(children);
196 }
197 fSource.clearContent();
198 return fDelta;
199 }
200
201 //---- region updating --------------------------------------------------------------
202
203 /*
204 * @see org.eclipse.text.edits.TextEdit#traversePassThree
205 */
206 int traverseRegionUpdating(TextEditProcessor processor, IDocument document, int accumulatedDelta, bool delete_) {
207 // the children got already updated / normalized while they got removed
208 // from the source edit. So we only have to adjust the offset computed to
209 // far.
210 if (delete_) {
211 deleteTree();
212 } else {
213 internalMoveTree(accumulatedDelta);
214 }
215 return accumulatedDelta + fDelta;
216 }
217
218 bool deleteChildren() {
219 return false;
220 }
221 }