Mercurial > projects > dwt-addons
annotate dwtx/text/edits/MultiTextEdit.d @ 150:5cf141e43417
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 24 Aug 2008 23:05:26 +0200 |
parents | 6dcb0baaa031 |
children | f70d9508c95c |
rev | line source |
---|---|
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.MultiTextEdit; | |
14 | |
131 | 15 import dwtx.text.edits.CopySourceEdit; // packageimport |
16 import dwtx.text.edits.MoveSourceEdit; // packageimport | |
17 import dwtx.text.edits.CopyingRangeMarker; // packageimport | |
18 import dwtx.text.edits.ReplaceEdit; // packageimport | |
19 import dwtx.text.edits.EditDocument; // packageimport | |
20 import dwtx.text.edits.UndoCollector; // packageimport | |
21 import dwtx.text.edits.DeleteEdit; // packageimport | |
22 import dwtx.text.edits.MoveTargetEdit; // packageimport | |
23 import dwtx.text.edits.CopyTargetEdit; // packageimport | |
24 import dwtx.text.edits.TextEditCopier; // packageimport | |
25 import dwtx.text.edits.ISourceModifier; // packageimport | |
26 import dwtx.text.edits.TextEditMessages; // packageimport | |
27 import dwtx.text.edits.TextEditProcessor; // 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 | |
40 import java.util.List; | |
41 | |
42 import dwtx.core.runtime.Assert; | |
43 import dwtx.jface.text.BadLocationException; | |
44 import dwtx.jface.text.IDocument; | |
45 import dwtx.jface.text.IRegion; | |
46 | |
47 /** | |
48 * A multi-text edit can be used to aggregate several edits into | |
49 * one edit. The edit itself doesn't modify a document. | |
50 * <p> | |
51 * Clients are allowed to implement subclasses of a multi-text | |
52 * edit.Subclasses must implement <code>doCopy()</code> to ensure | |
53 * the a copy of the right type is created. Not implementing | |
54 * <code>doCopy()</code> in subclasses will result in an assertion | |
55 * failure during copying. | |
56 * | |
57 * @since 3.0 | |
58 */ | |
59 public class MultiTextEdit : TextEdit { | |
60 | |
61 private bool fDefined; | |
62 | |
63 /** | |
64 * Creates a new <code>MultiTextEdit</code>. The range | |
65 * of the edit is determined by the range of its children. | |
66 * | |
67 * Adding this edit to a parent edit sets its range to the | |
68 * range covered by its children. If the edit doesn't have | |
69 * any children its offset is set to the parent's offset | |
70 * and its length is set to 0. | |
71 */ | |
130 | 72 public this() { |
129 | 73 super(0, Integer.MAX_VALUE); |
74 fDefined= false; | |
75 } | |
76 | |
77 /** | |
78 * Creates a new </code>MultiTextEdit</code> for the given | |
79 * range. Adding a child to this edit which isn't covered | |
80 * by the given range will result in an exception. | |
81 * | |
82 * @param offset the edit's offset | |
83 * @param length the edit's length. | |
84 * @see TextEdit#addChild(TextEdit) | |
85 * @see TextEdit#addChildren(TextEdit[]) | |
86 */ | |
130 | 87 public this(int offset, int length) { |
129 | 88 super(offset, length); |
89 fDefined= true; | |
90 } | |
91 | |
92 /* | |
93 * Copy constructor. | |
94 */ | |
130 | 95 protected this(MultiTextEdit other) { |
129 | 96 super(other); |
97 } | |
98 | |
99 /** | |
100 * Checks the edit's integrity. | |
101 * <p> | |
102 * Note that this method <b>should only be called</b> by the edit | |
103 * framework and not by normal clients.</p> | |
104 *<p> | |
105 * This default implementation does nothing. Subclasses may override | |
106 * if needed.</p> | |
107 * | |
108 * @exception MalformedTreeException if the edit isn't in a valid state | |
109 * and can therefore not be executed | |
110 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
111 protected void checkIntegrity() { |
129 | 112 // does nothing |
113 } | |
114 | |
115 /** | |
116 * {@inheritDoc} | |
117 */ | |
118 final bool isDefined() { | |
119 if (fDefined) | |
120 return true; | |
121 return hasChildren(); | |
122 } | |
123 | |
124 /** | |
125 * {@inheritDoc} | |
126 */ | |
127 public final int getOffset() { | |
128 if (fDefined) | |
129 return super.getOffset(); | |
130 | |
131 List/*<TextEdit>*/ children= internalGetChildren(); | |
132 if (children is null || children.size() is 0) | |
133 return 0; | |
134 // the children are already sorted | |
134 | 135 return (cast(TextEdit)children.get(0)).getOffset(); |
129 | 136 } |
137 | |
138 /** | |
139 * {@inheritDoc} | |
140 */ | |
141 public final int getLength() { | |
142 if (fDefined) | |
143 return super.getLength(); | |
144 | |
145 List/*<TextEdit>*/ children= internalGetChildren(); | |
146 if (children is null || children.size() is 0) | |
147 return 0; | |
148 // the children are already sorted | |
134 | 149 TextEdit first= cast(TextEdit)children.get(0); |
150 TextEdit last= cast(TextEdit)children.get(children.size() - 1); | |
129 | 151 return last.getOffset() - first.getOffset() + last.getLength(); |
152 } | |
153 | |
154 /** | |
155 * {@inheritDoc} | |
156 */ | |
157 public final bool covers(TextEdit other) { | |
158 if (fDefined) | |
159 return super.covers(other); | |
160 // an undefined multiple text edit covers everything | |
161 return true; | |
162 } | |
163 | |
164 /* | |
165 * @see dwtx.text.edits.TextEdit#canZeroLengthCover() | |
166 */ | |
167 protected bool canZeroLengthCover() { | |
168 return true; | |
169 } | |
170 | |
171 /* | |
172 * @see TextEdit#copy | |
173 */ | |
174 protected TextEdit doCopy() { | |
150 | 175 Assert.isTrue(MultiTextEdit.classinfo is getClass(), "Subclasses must reimplement copy0"); //$NON-NLS-1$ |
129 | 176 return new MultiTextEdit(this); |
177 } | |
178 | |
179 /* | |
180 * @see TextEdit#accept0 | |
181 */ | |
182 protected void accept0(TextEditVisitor visitor) { | |
183 bool visitChildren= visitor.visit(this); | |
184 if (visitChildren) { | |
185 acceptChildren(visitor); | |
186 } | |
187 } | |
188 | |
189 /* | |
190 * @see dwtx.text.edits.TextEdit#adjustOffset(int) | |
191 * @since 3.1 | |
192 */ | |
193 void adjustOffset(int delta) { | |
194 if (fDefined) | |
195 super.adjustOffset(delta); | |
196 } | |
197 | |
198 /* | |
199 * @see dwtx.text.edits.TextEdit#adjustLength(int) | |
200 * @since 3.1 | |
201 */ | |
202 void adjustLength(int delta) { | |
203 if (fDefined) | |
204 super.adjustLength(delta); | |
205 } | |
206 | |
207 /* | |
208 * @see TextEdit#performConsistencyCheck | |
209 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
210 void performConsistencyCheck(TextEditProcessor processor, IDocument document) { |
129 | 211 checkIntegrity(); |
212 } | |
213 | |
214 /* | |
215 * @see TextEdit#performDocumentUpdating | |
216 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
217 int performDocumentUpdating(IDocument document) { |
129 | 218 fDelta= 0; |
219 return fDelta; | |
220 } | |
221 | |
222 /* | |
223 * @see TextEdit#deleteChildren | |
224 */ | |
225 bool deleteChildren() { | |
226 return false; | |
227 } | |
228 | |
229 void aboutToBeAdded(TextEdit parent) { | |
230 defineRegion(parent.getOffset()); | |
231 } | |
232 | |
233 void defineRegion(int parentOffset) { | |
234 if (fDefined) | |
235 return; | |
236 if (hasChildren()) { | |
237 IRegion region= getCoverage(getChildren()); | |
238 internalSetOffset(region.getOffset()); | |
239 internalSetLength(region.getLength()); | |
240 } else { | |
241 internalSetOffset(parentOffset); | |
242 internalSetLength(0); | |
243 } | |
244 fDefined= true; | |
245 } | |
246 | |
247 /* | |
248 * @see dwtx.text.edits.TextEdit#internalToString(java.lang.StringBuffer, int) | |
249 * @since 3.3 | |
250 */ | |
251 void internalToString(StringBuffer buffer, int indent) { | |
252 super.internalToString(buffer, indent); | |
253 if (! fDefined) | |
254 buffer.append(" [undefined]"); //$NON-NLS-1$ | |
255 } | |
256 } |