package org.eclipse.text.edits;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
public final class CopySourceEdit extends TextEdit {
private CopyTargetEdit fTarget;
private ISourceModifier fModifier;
private String fSourceContent;
private TextEdit fSourceRoot;
private static class PartialCopier extends TextEditVisitor {
TextEdit fResult;
List<TextEdit> fParents= new ArrayList<>();
TextEdit fCurrentParent;
public static TextEdit perform(TextEdit source) {
PartialCopier copier= new PartialCopier();
source.accept(copier);
return copier.fResult;
}
private void manageCopy(TextEdit copy) {
if (fResult == null)
fResult= copy;
if (fCurrentParent != null) {
fCurrentParent.addChild(copy);
}
fParents.add(fCurrentParent);
fCurrentParent= copy;
}
@Override
public void postVisit(TextEdit edit) {
fCurrentParent= fParents.remove(fParents.size() - 1);
}
@Override
public boolean visitNode(TextEdit edit) {
manageCopy(edit.doCopy());
return true;
}
@Override
public boolean visit(CopySourceEdit edit) {
manageCopy(new RangeMarker(edit.getOffset(), edit.getLength()));
return true;
}
@Override
public boolean visit(CopyTargetEdit edit) {
manageCopy(new InsertEdit(edit.getOffset(), edit.getSourceEdit().getContent()));
return true;
}
@Override
public boolean visit(MoveSourceEdit edit) {
manageCopy(new DeleteEdit(edit.getOffset(), edit.getLength()));
return true;
}
@Override
public boolean visit(MoveTargetEdit edit) {
manageCopy(new InsertEdit(edit.getOffset(), edit.getSourceEdit().getContent()));
return true;
}
}
public CopySourceEdit(int offset, int length) {
super(offset, length);
}
public CopySourceEdit(int offset, int length, CopyTargetEdit target) {
this(offset, length);
setTargetEdit(target);
}
private CopySourceEdit(CopySourceEdit other) {
super(other);
if (other.fModifier != null)
fModifier= other.fModifier.copy();
}
public CopyTargetEdit getTargetEdit() {
return fTarget;
}
public void setTargetEdit(CopyTargetEdit edit) throws MalformedTreeException {
Assert.isNotNull(edit);
if (fTarget != edit) {
fTarget= edit;
fTarget.setSourceEdit(this);
}
}
public ISourceModifier getSourceModifier() {
return fModifier;
}
public void setSourceModifier(ISourceModifier modifier) {
fModifier= modifier;
}
@Override
protected TextEdit doCopy() {
return new CopySourceEdit(this);
}
@Override
protected void accept0(TextEditVisitor visitor) {
boolean visitChildren= visitor.visit(this);
if (visitChildren) {
acceptChildren(visitor);
}
}
String getContent() {
if (fSourceContent == null)
return "";
return fSourceContent;
}
void clearContent() {
fSourceContent= null;
}
@Override
protected void postProcessCopy(TextEditCopier copier) {
if (fTarget != null) {
CopySourceEdit source= (CopySourceEdit)copier.getCopy(this);
CopyTargetEdit target= (CopyTargetEdit)copier.getCopy(fTarget);
if (source != null && target != null)
source.setTargetEdit(target);
}
}
@Override
int traverseConsistencyCheck(TextEditProcessor processor, IDocument document, List<List<TextEdit>> sourceEdits) {
int result= super.traverseConsistencyCheck(processor, document, sourceEdits);
if (fSourceContent == null) {
if (sourceEdits.size() <= result) {
List<TextEdit> list= new ArrayList<>();
list.add(this);
for (int i= sourceEdits.size(); i < result; i++)
sourceEdits.add(null);
sourceEdits.add(list);
} else {
List<TextEdit> list= sourceEdits.get(result);
if (list == null) {
list= new ArrayList<>();
sourceEdits.add(result, list);
}
list.add(this);
}
}
return result;
}
@Override
void performConsistencyCheck(TextEditProcessor processor, IDocument document) throws MalformedTreeException {
if (fTarget == null)
throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("CopySourceEdit.no_target"));
if (fTarget.getSourceEdit() != this)
throw new MalformedTreeException(getParent(), this, TextEditMessages.getString("CopySourceEdit.different_source"));
}
@Override
void traverseSourceComputation(TextEditProcessor processor, IDocument document) {
performSourceComputation(processor, document);
}
@Override
void performSourceComputation(TextEditProcessor processor, IDocument document) {
try {
MultiTextEdit root= new MultiTextEdit(getOffset(), getLength());
root.internalSetChildren(internalGetChildren());
fSourceContent= document.get(getOffset(), getLength());
fSourceRoot= PartialCopier.perform(root);
fSourceRoot.internalMoveTree(-getOffset());
if (fSourceRoot.hasChildren()) {
EditDocument subDocument= new EditDocument(fSourceContent);
TextEditProcessor subProcessor= TextEditProcessor.createSourceComputationProcessor(subDocument, fSourceRoot, TextEdit.NONE);
subProcessor.performEdits();
if (needsTransformation())
applyTransformation(subDocument);
fSourceContent= subDocument.get();
fSourceRoot= null;
} else {
if (needsTransformation()) {
EditDocument subDocument= new EditDocument(fSourceContent);
applyTransformation(subDocument);
fSourceContent= subDocument.get();
}
}
} catch (BadLocationException cannotHappen) {
Assert.isTrue(false);
}
}
private boolean needsTransformation() {
return fModifier != null;
}
private void applyTransformation(IDocument document) throws MalformedTreeException {
TextEdit newEdit= new MultiTextEdit(0, document.getLength());
ReplaceEdit[] replaces= fModifier.getModifications(document.get());
for (ReplaceEdit replace : replaces) {
newEdit.addChild(replace);
}
try {
newEdit.apply(document, TextEdit.NONE);
} catch (BadLocationException cannotHappen) {
Assert.isTrue(false);
}
}
@Override
int performDocumentUpdating(IDocument document) throws BadLocationException {
fDelta= 0;
return fDelta;
}
@Override
boolean deleteChildren() {
return false;
}
}