2 * (c) Copyright IBM Corp. 2000, 2001.
5 package net.sourceforge.phpdt.internal.corext.textmanipulation;
7 import org.eclipse.core.runtime.CoreException;
10 //import org.eclipse.jdt.internal.corext.Assert;
13 * A text edit that moves text inside a text buffer.
15 public final class MoveTextEdit extends TextEdit {
17 /* package */ static class TargetMark extends NopTextEdit {
18 private MoveTextEdit fMoveTextEdit;
19 public TargetMark(TextRange range, MoveTextEdit edit) {
23 /* package */ MoveTextEdit getMoveTextEdit() {
26 public TextEdit perform(TextBuffer buffer) throws CoreException {
27 fMoveTextEdit.internalPerform(buffer);
28 return super.perform(buffer);
30 public TextEdit copy() {
31 // Assert.isTrue(false, "This should never happen"); //$NON-NLS-1$
36 private TextRange fTarget;
37 private TextRange fSource;
38 private int fPerformCounter;
41 * Creates a new <code>MoveTextEdit</code>. The text edit doesn't support
42 * overlapping moves. So for a <code>MoveTextEdit</code> <code>destination <= offset &&
43 * offset + length - 1 <= destination</code> must be <code>true</code>.
45 * @param offset the offset of the text to be moved
46 * @param length the text length to be moved
47 * @param destination the destination offset
49 public MoveTextEdit(int offset, int length, int destination) {
50 // Assert.isTrue(destination <= offset || offset + length <= destination);
51 fSource= new TextRange(offset, length);
52 fTarget= new TextRange(destination);
56 * Creates a new <code>MoveTextEdit</code> with the given source and target range.
58 * @param source the source
59 * @param target the target
61 private MoveTextEdit(TextRange source,TextRange target) {
67 * Returns the move text edit's source range. This method returns the same range
68 * as <code>TextEdit#getTextRange()</code>
70 * @return the edit's source range
72 public TextRange getSourceRange() {
77 * Returns the move text edit's target range.
79 * @return the edit's target range
81 public TextRange getTargetRange() {
86 * @see TextEdit#getTextRange()
88 public TextRange getTextRange() {
93 * @see TextEdit#connect(TextBufferEditor)
95 public void connect(TextBufferEditor editor) throws CoreException {
96 editor.add(new TargetMark(fTarget, this));
100 * @see TextEdit#perform(TextBuffer)
102 public TextEdit perform(TextBuffer buffer) throws CoreException {
103 internalPerform(buffer);
104 return new MoveTextEdit(fTarget, fSource);
108 * @see TextEdit#copy()
110 public TextEdit copy() {
111 TextRange source= getSourceRange();
112 TextRange target= getTargetRange();
113 return new MoveTextEdit(source.fOffset, source.fLength, target.fOffset);
116 //---- Helper method ---------------------------------------------------------------------------------
118 private void internalPerform(TextBuffer buffer) throws CoreException {
119 // Assert.isTrue(fPerformCounter < 2);
120 if (++fPerformCounter == 2) {
121 TextRange source= getSourceRange();
122 TextRange target= getTargetRange();
123 String current= buffer.getContent(source.fOffset, source.fLength);
124 buffer.replace(source, ""); //$NON-NLS-1$
125 buffer.replace(target, current);
129 /* package */ boolean isUpMove() {
130 return fSource.fOffset < fTarget.fOffset;
133 /* package */ boolean isDownMove() {
134 return fSource.fOffset > fTarget.fOffset;
137 /* package */ TextRange getChildRange() {
138 int offset= fSource.fOffset;
139 int length= fSource.fLength;
140 int destination= fTarget.fOffset;
141 if (destination <= offset)
142 return new TextRange(destination, offset + length - destination);
144 return new TextRange(offset, destination - offset);