2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.textmanipulation;
7 import org.eclipse.core.runtime.CoreException;
9 // import net.sourceforge.phpdt.internal.corext.Assert;
12 * A text edit that moves text inside a text buffer.
14 public final class MoveTextEdit extends TextEdit {
16 /* package */static class TargetMark extends NopTextEdit {
17 private MoveTextEdit fMoveTextEdit;
19 public TargetMark(TextRange range, MoveTextEdit edit) {
24 /* package */MoveTextEdit getMoveTextEdit() {
28 public TextEdit perform(TextBuffer buffer) throws CoreException {
29 fMoveTextEdit.internalPerform(buffer);
30 return super.perform(buffer);
33 public TextEdit copy() {
34 // Assert.isTrue(false, "This should never happen"); //$NON-NLS-1$
39 private TextRange fTarget;
41 private TextRange fSource;
43 private int fPerformCounter;
46 * Creates a new <code>MoveTextEdit</code>. The text edit doesn't support
47 * overlapping moves. So for a
48 * <code>MoveTextEdit</code> <code>destination <= offset &&
49 * offset + length - 1 <= destination</code>
50 * must be <code>true</code>.
53 * the offset of the text to be moved
55 * the text length to be moved
57 * the destination offset
59 public MoveTextEdit(int offset, int length, int destination) {
60 // Assert.isTrue(destination <= offset || offset + length <=
62 fSource = new TextRange(offset, length);
63 fTarget = new TextRange(destination);
67 * Creates a new <code>MoveTextEdit</code> with the given source and
75 private MoveTextEdit(TextRange source, TextRange target) {
81 * Returns the move text edit's source range. This method returns the same
82 * range as <code>TextEdit#getTextRange()</code>
84 * @return the edit's source range
86 public TextRange getSourceRange() {
91 * Returns the move text edit's target range.
93 * @return the edit's target range
95 public TextRange getTargetRange() {
102 * @see TextEdit#getTextRange()
104 public TextRange getTextRange() {
111 * @see TextEdit#connect(TextBufferEditor)
113 public void connect(TextBufferEditor editor) throws CoreException {
114 editor.add(new TargetMark(fTarget, this));
120 * @see TextEdit#perform(TextBuffer)
122 public TextEdit perform(TextBuffer buffer) throws CoreException {
123 internalPerform(buffer);
124 return new MoveTextEdit(fTarget, fSource);
130 * @see TextEdit#copy()
132 public TextEdit copy() {
133 TextRange source = getSourceRange();
134 TextRange target = getTargetRange();
135 return new MoveTextEdit(source.fOffset, source.fLength, target.fOffset);
138 // ---- Helper method
139 // ---------------------------------------------------------------------------------
141 private void internalPerform(TextBuffer buffer) throws CoreException {
142 // Assert.isTrue(fPerformCounter < 2);
143 if (++fPerformCounter == 2) {
144 TextRange source = getSourceRange();
145 TextRange target = getTargetRange();
146 String current = buffer.getContent(source.fOffset, source.fLength);
147 buffer.replace(source, ""); //$NON-NLS-1$
148 buffer.replace(target, current);
152 /* package */boolean isUpMove() {
153 return fSource.fOffset < fTarget.fOffset;
156 /* package */boolean isDownMove() {
157 return fSource.fOffset > fTarget.fOffset;
160 /* package */TextRange getChildRange() {
161 int offset = fSource.fOffset;
162 int length = fSource.fLength;
163 int destination = fTarget.fOffset;
164 if (destination <= offset)
165 return new TextRange(destination, offset + length - destination);
167 return new TextRange(offset, destination - offset);