package jetbrains.mps.lang.generator.intentions;
/*Generated by MPS */
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.generator.helper.QueriesUtil;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.typesystem.inference.TypeChecker;
import jetbrains.mps.lang.pattern.GeneratedMatchingPattern;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.AttributeOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.IAttributeDescriptor;
import java.util.List;
import java.util.ArrayList;
import jetbrains.mps.lang.structure.behavior.AbstractConceptDeclaration__BehaviorDescriptor;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.lang.core.behavior.BaseConcept__BehaviorDescriptor;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModelOperations;
import org.jetbrains.mps.openapi.persistence.PersistenceFacade;
import jetbrains.mps.smodel.SModelUtil_new;
public final class MacroIntentionsUtil {
public static SNode getContextNodeConcept(SNode contextNode) {
SNode enclosingMacro = findOuterMacro(contextNode);
if ((enclosingMacro == null)) {
return QueriesUtil.getApplicableConcept_fromEnvironment(contextNode);
}
if (SNodeOperations.isInstanceOf(enclosingMacro, MetaAdapterFactory.getConcept(0xb401a68083254110L, 0x8fd384331ff25befL, 0x10fef52f5efL, "jetbrains.mps.lang.generator.structure.SourceSubstituteMacro"))) {
return getConceptFrom(SNodeOperations.cast(enclosingMacro, MetaAdapterFactory.getConcept(0xb401a68083254110L, 0x8fd384331ff25befL, 0x10fef52f5efL, "jetbrains.mps.lang.generator.structure.SourceSubstituteMacro")));
}
return null;
}
private static SNode getConceptFrom(SNode macro) {
SNode query = QueriesUtil.getQueryFunction_fromSourceSubstituteMacro(macro);
SNode returnType = TypeChecker.getInstance().getTypeOf(query);
// ======
if (SNodeOperations.isInstanceOf(query, MetaAdapterFactory.getConcept(0xb401a68083254110L, 0x8fd384331ff25befL, 0x10ff3acfa74L, "jetbrains.mps.lang.generator.structure.SourceSubstituteMacro_SourceNodeQuery"))) {
{
GeneratedMatchingPattern pattern_iiuth6_a0d0b = new Pattern_m84bxgbllskx(_quotation_createNode_iiuth6_a0a0a0a0d0b());
SNode coercedNode_iiuth6_a0d0b = TypeChecker.getInstance().getRuntimeSupport().coerce_(returnType, pattern_iiuth6_a0d0b);
if (coercedNode_iiuth6_a0d0b != null) {
return pattern_iiuth6_a0d0b.getMatchedNode("concept");
} else {
return null;
}
}
} else if (SNodeOperations.isInstanceOf(query, MetaAdapterFactory.getConcept(0xb401a68083254110L, 0x8fd384331ff25befL, 0x10fef5bd603L, "jetbrains.mps.lang.generator.structure.SourceSubstituteMacro_SourceNodesQuery"))) {
{
GeneratedMatchingPattern pattern_iiuth6_a0a3a1 = new Pattern_m84bxgbllskj(_quotation_createNode_iiuth6_a0a0a0a0a3a1());
SNode coercedNode_iiuth6_a0a3a1 = TypeChecker.getInstance().getRuntimeSupport().coerce_(returnType, pattern_iiuth6_a0a3a1);
if (coercedNode_iiuth6_a0a3a1 != null) {
return pattern_iiuth6_a0a3a1.getMatchedNode("concept");
} else {
return null;
}
}
} else {
SNode macroOuterNode = SNodeOperations.getParent(SNodeOperations.getParent(macro));
return getContextNodeConcept(macroOuterNode);
}
}
private static SNode findOuterMacro(SNode contextNode) {
if (contextNode == null) {
return null;
}
if (ListSequence.fromList(AttributeOperations.getAttributeList(contextNode, new IAttributeDescriptor.NodeAttribute(MetaAdapterFactory.getConcept(0xb401a68083254110L, 0x8fd384331ff25befL, 0xfd47ed6742L, "jetbrains.mps.lang.generator.structure.NodeMacro")))).isNotEmpty()) {
return ListSequence.fromList(AttributeOperations.getAttributeList(contextNode, new IAttributeDescriptor.NodeAttribute(MetaAdapterFactory.getConcept(0xb401a68083254110L, 0x8fd384331ff25befL, 0xfd47ed6742L, "jetbrains.mps.lang.generator.structure.NodeMacro")))).last();
}
return findOuterMacro(SNodeOperations.getParent(contextNode));
}
public static List<SNode> getLinks(SNode sourceNode, boolean inCollection) {
if (sourceNode == null) {
return null;
}
List<SNode> result = ListSequence.fromList(new ArrayList<SNode>());
for (SNode child : AbstractConceptDeclaration__BehaviorDescriptor.getLinkDeclarations_idhEwILKK.invoke(sourceNode)) {
if (inCollection) {
if (SPropertyOperations.hasValue(child, MetaAdapterFactory.getProperty(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0xf979bd086aL, 0xf98054bb04L, "sourceCardinality"), "0..n", "0..1") || SPropertyOperations.hasValue(child, MetaAdapterFactory.getProperty(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0xf979bd086aL, 0xf98054bb04L, "sourceCardinality"), "1..n", "0..1")) {
ListSequence.fromList(result).addElement(child);
}
} else {
if (SPropertyOperations.hasValue(child, MetaAdapterFactory.getProperty(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0xf979bd086aL, 0xf98054bb04L, "sourceCardinality"), "0..1", "0..1") || SPropertyOperations.hasValue(child, MetaAdapterFactory.getProperty(0xc72da2b97cce4447L, 0x8389f407dc1158b7L, 0xf979bd086aL, 0xf98054bb04L, "sourceCardinality"), "1", "0..1")) {
ListSequence.fromList(result).addElement(child);
}
}
}
return result;
}
public static String getPresentaion(SNode intentionParam) {
// characters '_' and '&' are treated as mnemonics in AnAction that is created for each intention,
// however it's common to see '_' in link/property/conecept/template names, and removing this char
// (as mnemonics processing does) results in incorrect name shown to user, which is wrong.
// Here I escape only '_' as it's unlikely to see '&' in metamodel-level names,
// although correct (but impossible now) solution would be to change the way actions for intentions are created
// (i.e. without mnemonics processing).
return BaseConcept__BehaviorDescriptor.getPresentation_idhEwIMiw.invoke(intentionParam).replaceAll("_{1}", "__");
}
public static void copyVirtualPackage(SNode to, SNode from) {
SPropertyOperations.set(to, MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x10802efe25aL, 0x115eca8579fL, "virtualPackage"), SPropertyOperations.getString(SNodeOperations.getContainingRoot(from), MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x10802efe25aL, 0x115eca8579fL, "virtualPackage")));
}
public static boolean isInGeneratorModel(SNode node) {
return SNodeOperations.isInstanceOf(SModelOperations.getModuleStub(SNodeOperations.getModel(node)), MetaAdapterFactory.getConcept(0x86ef829012bb4ca7L, 0x947f093788f263a9L, 0x5869770da61dfe21L, "jetbrains.mps.lang.project.structure.Generator"));
}
private static SNode _quotation_createNode_iiuth6_a0a0a0a0d0b() {
PersistenceFacade facade = PersistenceFacade.getInstance();
SNode quotedNode_1 = null;
quotedNode_1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0x7866978ea0f04cc7L, 0x81bc4d213d9375e1L, "jetbrains.mps.lang.smodel"), 0x108f968b3caL, "SNodeType"), null, null, false);
return quotedNode_1;
}
private static SNode _quotation_createNode_iiuth6_a0a0a0a0a3a1() {
PersistenceFacade facade = PersistenceFacade.getInstance();
SNode quotedNode_1 = null;
SNode quotedNode_2 = null;
quotedNode_1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0x8388864671ce4f1cL, 0x9c53c54016f6ad4fL, "jetbrains.mps.baseLanguage.collections"), 0x10c260e9444L, "SequenceType"), null, null, false);
quotedNode_2 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0x7866978ea0f04cc7L, 0x81bc4d213d9375e1L, "jetbrains.mps.lang.smodel"), 0x108f968b3caL, "SNodeType"), null, null, false);
quotedNode_1.addChild(MetaAdapterFactory.getContainmentLink(0x8388864671ce4f1cL, 0x9c53c54016f6ad4fL, 0x10c260e9444L, 0x10c260ee40eL, "elementType"), quotedNode_2);
return quotedNode_1;
}
}