package jetbrains.mps.lang.structure.plugin;
/*Generated by MPS */
import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.mps.openapi.language.SProperty;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
import jetbrains.mps.lang.core.behavior.PropertyAttribute__BehaviorDescriptor;
import jetbrains.mps.internal.collections.runtime.IVisitor;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import jetbrains.mps.lang.core.behavior.ChildAttribute__BehaviorDescriptor;
import java.util.List;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import org.jetbrains.mps.openapi.language.SReferenceLink;
import jetbrains.mps.lang.core.behavior.LinkAttribute__BehaviorDescriptor;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import org.jetbrains.mps.openapi.model.SReference;
import jetbrains.mps.smodel.SModelInternal;
import jetbrains.mps.internal.collections.runtime.ITranslator2;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
public class RefactoringRuntime {
public static void changePropertyInstance(SNode node, final SProperty oldProp, final SProperty newProp) {
if (node.hasProperty(newProp) && neq_7voxfg_a0a0a0b(node.getProperty(oldProp), node.getProperty(newProp)) || node.getProperty(oldProp) == null) {
// merge case
return;
}
Iterable<SNode> attributes = (Iterable<SNode>) node.getChildren(MetaAdapterFactory.getContainmentLink(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x10802efe25aL, 0x47bf8397520e5942L, "smodelAttribute"));
Sequence.fromIterable(SNodeOperations.ofConcept(attributes, MetaAdapterFactory.getConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x2eb1ad060897da56L, "jetbrains.mps.lang.core.structure.PropertyAttribute"))).where(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
return PropertyAttribute__BehaviorDescriptor.getProperty_id1avfQ4BBzOo.invoke(it).equals(oldProp);
}
}).visitAll(new IVisitor<SNode>() {
public void visit(SNode it) {
PropertyAttribute__BehaviorDescriptor.setProperty_id6Gg5Klvu8CV.invoke(it, newProp);
}
});
String value = node.getProperty(oldProp);
node.setProperty(newProp, value);
node.setProperty(oldProp, null);
}
public static void changeContainmentLinkInstance(SNode node, final SContainmentLink oldLink, final SContainmentLink newLink) {
if (node.getChildren(newLink).iterator().hasNext()) {
// merge case
return;
}
Iterable<SNode> attributes = (Iterable<SNode>) node.getChildren(MetaAdapterFactory.getContainmentLink(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x10802efe25aL, 0x47bf8397520e5942L, "smodelAttribute"));
Sequence.fromIterable(SNodeOperations.ofConcept(attributes, MetaAdapterFactory.getConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x9d98713f247885aL, "jetbrains.mps.lang.core.structure.ChildAttribute"))).where(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
return ChildAttribute__BehaviorDescriptor.getLink_idBpxLfMirzf.invoke(it).equals(oldLink);
}
}).visitAll(new IVisitor<SNode>() {
public void visit(SNode it) {
ChildAttribute__BehaviorDescriptor.setLink_idBpxLfMirzM.invoke(it, newLink);
}
});
List<SNode> children = ListSequence.fromListWithValues(new ArrayList<SNode>(), (Iterable<SNode>) node.getChildren(oldLink));
for (SNode child : ListSequence.fromList(children)) {
node.removeChild(child);
node.addChild(newLink, child);
}
}
public static void changeReferenceLinkInstances(SNode node, final SReferenceLink oldLink, final SReferenceLink newLink) {
if (node.getReference(newLink) != null) {
// merge case
return;
}
Iterable<SNode> attributes = (Iterable<SNode>) node.getChildren(MetaAdapterFactory.getContainmentLink(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x10802efe25aL, 0x47bf8397520e5942L, "smodelAttribute"));
Sequence.fromIterable(SNodeOperations.ofConcept(attributes, MetaAdapterFactory.getConcept(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x2eb1ad060897da51L, "jetbrains.mps.lang.core.structure.LinkAttribute"))).where(new IWhereFilter<SNode>() {
public boolean accept(SNode it) {
return LinkAttribute__BehaviorDescriptor.getLink_id1avfQ4BEFo6.invoke(it).equals(oldLink);
}
}).visitAll(new IVisitor<SNode>() {
public void visit(SNode it) {
LinkAttribute__BehaviorDescriptor.setLink_id6Gg5KlvuxxF.invoke(it, newLink);
}
});
SNode referenceTarget = node.getReferenceTarget(oldLink);
node.setReferenceTarget(newLink, referenceTarget);
node.setReferenceTarget(oldLink, null);
}
public static SNode replaceWithNewConcept(final SNode node, SAbstractConcept newConcept) {
jetbrains.mps.smodel.SNode newInstance = (jetbrains.mps.smodel.SNode) SConceptOperations.createNewNode(SNodeOperations.asInstanceConcept(newConcept));
newInstance.setId(((jetbrains.mps.smodel.SNode) node).getNodeId());
for (SProperty prop : Sequence.fromIterable(node.getProperties())) {
newInstance.setProperty(prop, node.getProperty(prop));
}
for (SReference ref : Sequence.fromIterable(node.getReferences())) {
newInstance.setReferenceTarget(ref.getLink(), ref.getTargetNode());
}
for (SNode child : Sequence.fromIterable(node.getChildren())) {
SContainmentLink containmentLink = child.getContainmentLink();
node.removeChild(child);
newInstance.addChild(containmentLink, child);
}
if (SNodeOperations.getModel(node) instanceof SModelInternal) {
if (!(as_7voxfg_a0a0a0a6a7_0(SNodeOperations.getModel(node), SModelInternal.class).importedLanguageIds().contains(newConcept.getLanguage()))) {
as_7voxfg_a0a0a0a6a7(SNodeOperations.getModel(node), SModelInternal.class).addLanguage(newConcept.getLanguage());
}
}
final SNode result = SNodeOperations.replaceWithAnother(node, newInstance);
ListSequence.fromList(SNodeOperations.getNodeDescendants(result, null, true, new SAbstractConcept[]{})).translate(new ITranslator2<SNode, SReference>() {
public Iterable<SReference> translate(SNode it) {
return SNodeOperations.getReferences(it);
}
}).where(new IWhereFilter<SReference>() {
public boolean accept(SReference it) {
return SLinkOperations.getTargetNode(it) == node;
}
}).visitAll(new IVisitor<SReference>() {
public void visit(SReference it) {
it.getSourceNode().setReferenceTarget(it.getLink(), result);
}
});
return result;
}
private static boolean neq_7voxfg_a0a0a0b(Object a, Object b) {
return !(((a != null ? a.equals(b) : a == b)));
}
private static <T> T as_7voxfg_a0a0a0a6a7(Object o, Class<T> type) {
return (type.isInstance(o) ? (T) o : null);
}
private static <T> T as_7voxfg_a0a0a0a6a7_0(Object o, Class<T> type) {
return (type.isInstance(o) ? (T) o : null);
}
}