package jetbrains.mps.baseLanguage.intentions; /*Generated by MPS */ import org.jetbrains.mps.openapi.model.SNode; import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory; import jetbrains.mps.internal.collections.runtime.ListSequence; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations; import jetbrains.mps.baseLanguage.tuples.runtime.Tuples; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations; import jetbrains.mps.smodel.CopyUtil; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations; import org.jetbrains.annotations.Nullable; import jetbrains.mps.baseLanguage.tuples.runtime.MultiTuple; import org.jetbrains.mps.openapi.language.SContainmentLink; import org.jetbrains.mps.openapi.language.SAbstractConcept; import org.jetbrains.mps.openapi.language.SProperty; import jetbrains.mps.internal.collections.runtime.CollectionSequence; import org.jetbrains.mps.openapi.language.SReferenceLink; import java.util.Iterator; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations; import jetbrains.mps.smodel.action.SNodeFactoryOperations; import jetbrains.mps.baseLanguage.actions.PrecedenceUtil; public class IntentionUtils { private IntentionUtils() { } /*package*/ static SNode optimizeNode(SNode node) { { SNode matchedNode_k79hya_a0b = node; { boolean matches_k79hya_a0a1 = false; { SNode matchingNode_k79hya_a0a1 = node; if (matchingNode_k79hya_a0a1 != null) { matches_k79hya_a0a1 = matchingNode_k79hya_a0a1.getConcept().isSubConceptOf(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, "jetbrains.mps.baseLanguage.structure.StatementList")); } } if (matches_k79hya_a0a1) { return (ListSequence.fromList(SLinkOperations.getChildren(matchedNode_k79hya_a0b, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, 0xf8cc6bf961L, "statement"))).count() == 1 ? optimizeNode(ListSequence.fromList(SLinkOperations.getChildren(matchedNode_k79hya_a0b, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b200L, 0xf8cc6bf961L, "statement"))).getElement(0)) : null); } else { boolean matches_k79hya_b0a1 = false; { SNode matchingNode_k79hya_b0a1 = node; if (matchingNode_k79hya_b0a1 != null) { matches_k79hya_b0a1 = matchingNode_k79hya_b0a1.getConcept().isSubConceptOf(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfc092b6b77L, "jetbrains.mps.baseLanguage.structure.BlockStatement")); } } if (matches_k79hya_b0a1) { return optimizeNode(SLinkOperations.getTarget(matchedNode_k79hya_a0b, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfc092b6b77L, 0xfc092b6b78L, "statements"))); } else { boolean matches_k79hya_c0a1 = false; { SNode matchingNode_k79hya_c0a1 = node; if (matchingNode_k79hya_c0a1 != null) { matches_k79hya_c0a1 = matchingNode_k79hya_c0a1.getConcept().isSubConceptOf(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b215L, "jetbrains.mps.baseLanguage.structure.Statement")); } } if (matches_k79hya_c0a1) { return matchedNode_k79hya_a0b; } else return null; } } } } } /*package*/ static boolean canBeConvertedToTernary(SNode statement1, SNode statement2) { return getAppreciateDiffNodes(statement1, statement2) != null; } /*package*/ static SNode convertToTernary(SNode statement1, SNode statement2, SNode condition) { Tuples._2<SNode, SNode> diff = getAppreciateDiffNodes(statement1, statement2); if (diff == null) { return null; } SNode ternaryOperator = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10ef01239c9L, "jetbrains.mps.baseLanguage.structure.TernaryOperatorExpression")); SLinkOperations.setTarget(ternaryOperator, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10ef01239c9L, 0x10ef012826fL, "condition"), (SNode) CopyUtil.copy(condition)); SLinkOperations.setTarget(ternaryOperator, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10ef01239c9L, 0x10ef012a1c0L, "ifTrue"), (SNode) CopyUtil.copy(diff._0())); SLinkOperations.setTarget(ternaryOperator, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10ef01239c9L, 0x10ef012cedcL, "ifFalse"), (SNode) CopyUtil.copy(diff._1())); SNodeOperations.replaceWithAnother(diff._0(), ternaryOperator); return statement1; } @Nullable /*package*/ static Tuples._2<SNode, SNode> getAppreciateDiffNodes(SNode node1, SNode node2) { Tuples._2<SNode, SNode> currentDiff = getDiffNodes(node1, node2); if (currentDiff == null) { return null; } while (currentDiff._0() != node1) { if (isDiffCanBeConvertedToTernary(currentDiff)) { return currentDiff; } currentDiff = MultiTuple.<SNode,SNode>from(SNodeOperations.getParent(currentDiff._0()), SNodeOperations.getParent(currentDiff._1())); } return (isDiffCanBeConvertedToTernary(currentDiff) ? currentDiff : null); } /*package*/ static boolean isDiffCanBeConvertedToTernary(Tuples._2<SNode, SNode> diff) { if (SNodeOperations.isInstanceOf(SNodeOperations.getParent(diff._0()), MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b213L, "jetbrains.mps.baseLanguage.structure.ExpressionStatement"))) { return false; } // todo: i don't think that this code is true SContainmentLink l = diff._0().getContainmentLink(); return SConceptOperations.isSubConceptOf(SNodeOperations.asSConcept(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10ef01239c9L, "jetbrains.mps.baseLanguage.structure.TernaryOperatorExpression")), SNodeOperations.asSConcept(l.getTargetConcept())); } @Nullable /*package*/ static Tuples._2<SNode, SNode> getDiffNodes(SNode node1, SNode node2) { if (neq_k79hya_a0a0g(SNodeOperations.getConcept(node1), SNodeOperations.getConcept(node2))) { return MultiTuple.<SNode,SNode>from(node1, node2); } SAbstractConcept concept = SNodeOperations.getConcept(node1); // todo: use ConceptRegistry/SConcept when it will possible for (SProperty p : CollectionSequence.fromCollection(concept.getProperties())) { if (neq_k79hya_a0a0e0g(node1.getProperty(p), node2.getProperty(p))) { return MultiTuple.<SNode,SNode>from(node1, node2); } } for (SReferenceLink r : CollectionSequence.fromCollection(concept.getReferenceLinks())) { if (node1.getReference(r).getTargetNode() != node2.getReference(r).getTargetNode()) { return MultiTuple.<SNode,SNode>from(node1, node2); } } Tuples._2<SNode, SNode> currentResult = null; for (SContainmentLink l : CollectionSequence.fromCollection(concept.getContainmentLinks())) { Iterator<? extends SNode> c1 = node1.getChildren(l).iterator(); Iterator<? extends SNode> c2 = node2.getChildren(l).iterator(); while (c1.hasNext() && c2.hasNext()) { SNode child1 = c1.next(); SNode child2 = c2.next(); Tuples._2<SNode, SNode> currentChildDiff = getDiffNodes(child1, child2); if (currentChildDiff == null) { continue; } if (currentResult == null) { currentResult = currentChildDiff; } else { return MultiTuple.<SNode,SNode>from(node1, node2); } } if (c1.hasNext() || c2.hasNext()) { return MultiTuple.<SNode,SNode>from(node1, node2); } } return currentResult; } public static SNode negateBooleanNodes(SNode node) { SNode clone = SNodeOperations.copyNode(node); if (SNodeOperations.isInstanceOf(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b201L, "jetbrains.mps.baseLanguage.structure.BooleanConstant"))) { SPropertyOperations.set(SNodeOperations.cast(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b201L, "jetbrains.mps.baseLanguage.structure.BooleanConstant")), MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b201L, 0xf8cc56b202L, "value"), "" + (!(SPropertyOperations.getBoolean(SNodeOperations.cast(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b201L, "jetbrains.mps.baseLanguage.structure.BooleanConstant")), MetaAdapterFactory.getProperty(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b201L, 0xf8cc56b202L, "value"))))); return clone; } if (SNodeOperations.isInstanceOf(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbcf6bd10dL, "jetbrains.mps.baseLanguage.structure.NotExpression"))) { return SLinkOperations.getTarget(SNodeOperations.cast(node, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbcf6bd10dL, "jetbrains.mps.baseLanguage.structure.NotExpression")), MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbcf6bd10dL, 0xfbcf6c30a4L, "expression")); } if (SNodeOperations.isInstanceOf(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, "jetbrains.mps.baseLanguage.structure.BinaryOperation"))) { SNode replacement; if (SNodeOperations.isInstanceOf(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfb7c3070eeL, "jetbrains.mps.baseLanguage.structure.AndExpression"))) { replacement = SNodeFactoryOperations.createNewNode(SNodeFactoryOperations.asInstanceConcept(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfb8255689fL, "jetbrains.mps.baseLanguage.structure.OrExpression")), null); } else if (SNodeOperations.isInstanceOf(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfb8255689fL, "jetbrains.mps.baseLanguage.structure.OrExpression"))) { replacement = SNodeFactoryOperations.createNewNode(SNodeFactoryOperations.asInstanceConcept(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfb7c3070eeL, "jetbrains.mps.baseLanguage.structure.AndExpression")), null); } else if (SNodeOperations.isInstanceOf(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b210L, "jetbrains.mps.baseLanguage.structure.EqualsExpression"))) { replacement = SNodeFactoryOperations.createNewNode(SNodeFactoryOperations.asInstanceConcept(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf9e20e303fL, "jetbrains.mps.baseLanguage.structure.NotEqualsExpression")), null); } else if (SNodeOperations.isInstanceOf(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf9e20e303fL, "jetbrains.mps.baseLanguage.structure.NotEqualsExpression"))) { replacement = SNodeFactoryOperations.createNewNode(SNodeFactoryOperations.asInstanceConcept(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b210L, "jetbrains.mps.baseLanguage.structure.EqualsExpression")), null); } else if (SNodeOperations.isInstanceOf(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbced38fcfL, "jetbrains.mps.baseLanguage.structure.GreaterThanExpression"))) { replacement = SNodeFactoryOperations.createNewNode(SNodeFactoryOperations.asInstanceConcept(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10c8d53a915L, "jetbrains.mps.baseLanguage.structure.LessThanOrEqualsExpression")), null); } else if (SNodeOperations.isInstanceOf(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10c8d0fac2cL, "jetbrains.mps.baseLanguage.structure.GreaterThanOrEqualsExpression"))) { replacement = SNodeFactoryOperations.createNewNode(SNodeFactoryOperations.asInstanceConcept(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbced3b82aL, "jetbrains.mps.baseLanguage.structure.LessThanExpression")), null); } else if (SNodeOperations.isInstanceOf(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbced3b82aL, "jetbrains.mps.baseLanguage.structure.LessThanExpression"))) { replacement = SNodeFactoryOperations.createNewNode(SNodeFactoryOperations.asInstanceConcept(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10c8d0fac2cL, "jetbrains.mps.baseLanguage.structure.GreaterThanOrEqualsExpression")), null); } else if (SNodeOperations.isInstanceOf(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x10c8d53a915L, "jetbrains.mps.baseLanguage.structure.LessThanOrEqualsExpression"))) { replacement = SNodeFactoryOperations.createNewNode(SNodeFactoryOperations.asInstanceConcept(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbced38fcfL, "jetbrains.mps.baseLanguage.structure.GreaterThanExpression")), null); } else { SNode notNode = SNodeFactoryOperations.createNewNode(SNodeFactoryOperations.asInstanceConcept(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbcf6bd10dL, "jetbrains.mps.baseLanguage.structure.NotExpression")), null); SLinkOperations.setTarget(notNode, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbcf6bd10dL, 0xfbcf6c30a4L, "expression"), clone); IntentionUtils.addParensToNotIfNeeded(notNode); return notNode; } SLinkOperations.setTarget(replacement, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, 0xfbdeb7a11cL, "leftExpression"), SLinkOperations.getTarget(SNodeOperations.cast(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, "jetbrains.mps.baseLanguage.structure.BinaryOperation")), MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, 0xfbdeb7a11cL, "leftExpression"))); SLinkOperations.setTarget(replacement, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, 0xfbdeb7a11bL, "rightExpression"), SLinkOperations.getTarget(SNodeOperations.cast(clone, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, "jetbrains.mps.baseLanguage.structure.BinaryOperation")), MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbdeb6fecfL, 0xfbdeb7a11bL, "rightExpression"))); return replacement; } SNode notNode = SNodeFactoryOperations.createNewNode(SNodeFactoryOperations.asInstanceConcept(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbcf6bd10dL, "jetbrains.mps.baseLanguage.structure.NotExpression")), null); SLinkOperations.setTarget(notNode, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbcf6bd10dL, 0xfbcf6c30a4L, "expression"), clone); IntentionUtils.addParensToNotIfNeeded(notNode); return notNode; } public static void addParensToNotIfNeeded(SNode notExpr) { if (PrecedenceUtil.needsParensAroundNotExpression(notExpr)) { SNode childExpr = SLinkOperations.getTarget(notExpr, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfbcf6bd10dL, 0xfbcf6c30a4L, "expression")); SNode parens = SNodeFactoryOperations.replaceWithNewChild(childExpr, SNodeFactoryOperations.asInstanceConcept(MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfb4ed32b7fL, "jetbrains.mps.baseLanguage.structure.ParenthesizedExpression"))); SLinkOperations.setTarget(parens, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xfb4ed32b7fL, 0xfb4ed32b80L, "expression"), childExpr); } } private static boolean neq_k79hya_a0a0g(Object a, Object b) { return !(((a != null ? a.equals(b) : a == b))); } private static boolean neq_k79hya_a0a0e0g(Object a, Object b) { return !(((a != null ? a.equals(b) : a == b))); } }