Skip to main content
summaryrefslogtreecommitdiffstats
blob: c1dcb81f555c2de1023ae1bae8c4a405219faf1d (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
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.text.edits;

import java.util.List;

import org.eclipse.core.runtime.Assert;

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

/**
 * A multi-text edit can be used to aggregate several edits into
 * one edit. The edit itself doesn't modify a document.
 * <p>
 * Clients are allowed to implement subclasses of a multi-text
 * edit.Subclasses must implement <code>doCopy()</code> to ensure
 * the a copy of the right type is created. Not implementing
 * <code>doCopy()</code> in subclasses will result in an assertion
 * failure during copying.
 *
 * @since 3.0
 */
public class MultiTextEdit extends TextEdit {

	private boolean fDefined;

	/**
	 * Creates a new <code>MultiTextEdit</code>. The range
	 * of the edit is determined by the range of its children.
	 *
	 * Adding this edit to a parent edit sets its range to the
	 * range covered by its children. If the edit doesn't have
	 * any children its offset is set to the parent's offset
	 * and its length is set to 0.
	 */
	public MultiTextEdit() {
		super(0, Integer.MAX_VALUE);
		fDefined= false;
	}

	/**
	 * Creates a new </code>MultiTextEdit</code> for the given
	 * range. Adding a child to this edit which isn't covered
	 * by the given range will result in an exception.
	 *
	 * @param offset the edit's offset
	 * @param length the edit's length.
	 * @see TextEdit#addChild(TextEdit)
	 * @see TextEdit#addChildren(TextEdit[])
	 */
	public MultiTextEdit(int offset, int length) {
		super(offset, length);
		fDefined= true;
	}

	/*
	 * Copy constructor.
	 */
	protected MultiTextEdit(MultiTextEdit other) {
		super(other);
	}

	/**
	 * Checks the edit's integrity.
	 * <p>
	 * Note that this method <b>should only be called</b> by the edit
	 * framework and not by normal clients.</p>
	 *<p>
	 * This default implementation does nothing. Subclasses may override
	 * if needed.</p>
	 *
	 * @exception MalformedTreeException if the edit isn't in a valid state
	 *  and can therefore not be executed
	 */
	protected void checkIntegrity() throws MalformedTreeException {
		// does nothing
	}

	@Override
	final boolean isDefined() {
		if (fDefined)
			return true;
		return hasChildren();
	}

	@Override
	public final int getOffset() {
		if (fDefined)
			return super.getOffset();

		List/*<TextEdit>*/ children= internalGetChildren();
		if (children == null || children.size() == 0)
			return 0;
		// the children are already sorted
		return ((TextEdit)children.get(0)).getOffset();
	}

	@Override
	public final int getLength() {
		if (fDefined)
			return super.getLength();

		List/*<TextEdit>*/ children= internalGetChildren();
		if (children == null || children.size() == 0)
			return 0;
		// the children are already sorted
		TextEdit first= (TextEdit)children.get(0);
		TextEdit last= (TextEdit)children.get(children.size() - 1);
		return last.getOffset() - first.getOffset() + last.getLength();
	}

	@Override
	public final boolean covers(TextEdit other) {
		if (fDefined)
			return super.covers(other);
		// an undefined multiple text edit covers everything
		return true;
	}

	@Override
	protected boolean canZeroLengthCover() {
		return true;
	}

	/*
	 * @see TextEdit#copy
	 */
	@Override
	protected TextEdit doCopy() {
		Assert.isTrue(MultiTextEdit.class == getClass(), "Subclasses must reimplement copy0"); //$NON-NLS-1$
		return new MultiTextEdit(this);
	}

	/*
	 * @see TextEdit#accept0
	 */
	@Override
	protected void accept0(TextEditVisitor visitor) {
		boolean visitChildren= visitor.visit(this);
		if (visitChildren) {
			acceptChildren(visitor);
		}
	}

	@Override
	void adjustOffset(int delta) {
		if (fDefined)
			super.adjustOffset(delta);
	}

	@Override
	void adjustLength(int delta) {
		if (fDefined)
			super.adjustLength(delta);
	}

	/*
	 * @see TextEdit#performConsistencyCheck
	 */
	@Override
	void performConsistencyCheck(TextEditProcessor processor, IDocument document) throws MalformedTreeException {
		checkIntegrity();
	}

	/*
	 * @see TextEdit#performDocumentUpdating
	 */
	@Override
	int performDocumentUpdating(IDocument document) throws BadLocationException {
		fDelta= 0;
		return fDelta;
	}

	/*
	 * @see TextEdit#deleteChildren
	 */
	@Override
	boolean deleteChildren() {
		return false;
	}

	@Override
	void aboutToBeAdded(TextEdit parent) {
		defineRegion(parent.getOffset());
	}

	void defineRegion(int parentOffset) {
		if (fDefined)
			return;
		if (hasChildren()) {
			IRegion region= getCoverage(getChildren());
			internalSetOffset(region.getOffset());
			internalSetLength(region.getLength());
		} else {
			internalSetOffset(parentOffset);
			internalSetLength(0);
		}
		fDefined= true;
	}

	@Override
	void internalToString(StringBuffer buffer, int indent) {
		super.internalToString(buffer, indent);
		if (! fDefined)
			buffer.append(" [undefined]"); //$NON-NLS-1$
	}
}

Back to the top