package jetbrains.mps.baseLanguage.util.plugin.refactorings;
/*Generated by MPS */
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.List;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
import jetbrains.mps.smodel.behaviour.BHReflection;
import jetbrains.mps.core.aspects.behaviour.SMethodTrimmedId;
import jetbrains.mps.internal.collections.runtime.Sequence;
import org.jetbrains.mps.openapi.persistence.PersistenceFacade;
import jetbrains.mps.smodel.SModelUtil_new;
import org.jetbrains.mps.openapi.model.SNodeAccessUtil;
import jetbrains.mps.lang.typesystem.runtime.HUtil;
public class IntroduceLocalVariableRefactoring extends IntroduceVariableRefactoring {
public IntroduceLocalVariableRefactoring() {
}
@Override
public SNode doRefactoring() {
this.findDuplicates();
SNode var = _quotation_createNode_nngwe4_a0b0b(this.getName(), this.getExpressionType(), this.getExpression());
if (myIsFinal) {
SPropertyOperations.set(SLinkOperations.getTarget(var, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7f0L, 0xf8cc67c7f1L, "localVariableDeclaration")), MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0x111f9e9f00cL, "isFinal"), "" + (true));
}
SNode varDeclaration = SLinkOperations.getTarget(var, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7f0L, 0xf8cc67c7f1L, "localVariableDeclaration"));
SNode parentStatement = SNodeOperations.getNodeAncestor(this.getExpression(), MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b215L, "jetbrains.mps.baseLanguage.structure.Statement"), false, false);
while (!(SNodeOperations.isInstanceOf(SNodeOperations.getParent(parentStatement), MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, "jetbrains.mps.baseLanguage.structure.StatementList")))) {
parentStatement = SNodeOperations.getNodeAncestor(parentStatement, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b215L, "jetbrains.mps.baseLanguage.structure.Statement"), false, false);
}
SNodeOperations.insertPrevSiblingChild(parentStatement, var);
replaceNode(getExpression(), varDeclaration);
if (myIsReplacingAll) {
for (SNode duplicate : ListSequence.fromList(myDuplicates)) {
replaceNode(duplicate, varDeclaration);
}
}
return varDeclaration;
}
@Override
public SNode getRootToFindDuplicates(final SNode node) {
List<SNode> ancestors = SNodeOperations.getNodeAncestors(node, null, false);
SNode statementListContainer = SNodeOperations.getNodeAncestor(node, MetaAdapterFactory.getInterfaceConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11750ef8265L, "jetbrains.mps.baseLanguage.structure.IStatementListContainer"), false, false);
SNode metaContainer = ListSequence.fromList(ancestors).findFirst(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
return ((int) (Integer) BHReflection.invoke(it, SMethodTrimmedId.create("getMetaLevel", null, "3t0v3yFOD1A"))) != ((int) (Integer) BHReflection.invoke(node, SMethodTrimmedId.create("getMetaLevel", null, "3t0v3yFOD1A")));
}
});
int statementListContainerIndex = ListSequence.fromList(ancestors).indexOf(statementListContainer);
int metaContainerIndex = ListSequence.fromList(ancestors).indexOf(metaContainer);
if (statementListContainerIndex >= 0 && metaContainerIndex >= 0) {
ancestors = ListSequence.fromList(ancestors).take(Math.min(statementListContainerIndex, metaContainerIndex)).toListSequence();
} else if (statementListContainerIndex < 0 && metaContainerIndex >= 0) {
ancestors = ListSequence.fromList(ancestors).take(metaContainerIndex).toListSequence();
} else if (statementListContainerIndex >= 0 && metaContainerIndex < 0) {
ancestors = ListSequence.fromList(ancestors).take(statementListContainerIndex).toListSequence();
}
return Sequence.fromIterable(SNodeOperations.ofConcept(ancestors, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, "jetbrains.mps.baseLanguage.structure.StatementList"))).last();
}
@Override
public void replaceNode(SNode node, SNode declaration) {
SNode reference = ((SNode) BHReflection.invoke(declaration, SMethodTrimmedId.create("createReference", null, "hEwJfME")));
SNodeOperations.replaceWithAnother(node, reference);
this.moveDeclarationIfNeed(reference, SNodeOperations.getParent(declaration));
}
private void moveDeclarationIfNeed(SNode node, SNode declaration) {
List<SNode> declAncestors = SNodeOperations.getNodeAncestors(declaration, null, false);
ListSequence.fromList(declAncestors).addElement(declaration);
SNode commonList = SNodeOperations.getNodeAncestor(node, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, "jetbrains.mps.baseLanguage.structure.StatementList"), false, false);
while ((commonList != null) && !(ListSequence.fromList(declAncestors).contains(commonList))) {
commonList = SNodeOperations.getNodeAncestor(commonList, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, "jetbrains.mps.baseLanguage.structure.StatementList"), false, false);
}
if ((commonList != null)) {
SNode firstAncestor = null;
for (SNode statement : ListSequence.fromList(SLinkOperations.getChildren(commonList, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, 0xf8cc6bf961L, "statement")))) {
List<SNode> nodeAncestors = SNodeOperations.getNodeAncestors(node, null, false);
if (ListSequence.fromList(declAncestors).contains(statement) || ListSequence.fromList(nodeAncestors).contains(statement)) {
firstAncestor = statement;
break;
}
}
if (firstAncestor != declaration) {
SNodeOperations.insertPrevSiblingChild(firstAncestor, SNodeOperations.deleteNode(declaration));
}
}
}
public static boolean isApplicable(SNode expr) {
return SNodeOperations.isInstanceOf(expr, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37f506fL, "jetbrains.mps.baseLanguage.structure.Expression")) && SNodeOperations.getNodeAncestor(expr, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, "jetbrains.mps.baseLanguage.structure.StatementList"), false, false) != null;
}
private static SNode _quotation_createNode_nngwe4_a0b0b(Object parameter_1, Object parameter_2, Object parameter_3) {
PersistenceFacade facade = PersistenceFacade.getInstance();
SNode quotedNode_4 = null;
SNode quotedNode_5 = null;
SNode quotedNode_6 = null;
SNode quotedNode_7 = null;
quotedNode_4 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8cc67c7f0L, "LocalVariableDeclarationStatement"), null, null, false);
quotedNode_5 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8cc67c7efL, "LocalVariableDeclaration"), null, null, false);
SNodeAccessUtil.setProperty(quotedNode_5, MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name"), (String) parameter_1);
quotedNode_6 = (SNode) parameter_2;
if (quotedNode_6 != null) {
quotedNode_5.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x450368d90ce15bc3L, 0x4ed4d318133c80ceL, "type"), HUtil.copyIfNecessary(quotedNode_6));
}
quotedNode_7 = (SNode) parameter_3;
if (quotedNode_7 != null) {
quotedNode_5.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37a7f6eL, 0xf8c37f506eL, "initializer"), HUtil.copyIfNecessary(quotedNode_7));
}
quotedNode_4.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc67c7f0L, 0xf8cc67c7f1L, "localVariableDeclaration"), quotedNode_5);
return quotedNode_4;
}
}