package jetbrains.mps.baseLanguage.pluginSolution.plugin;
/*Generated by MPS */
import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.ide.actions.MPSCommonDataKeys;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.project.MPSProject;
import jetbrains.mps.openapi.editor.EditorContext;
import jetbrains.mps.ide.editor.MPSEditorDataKeys;
import jetbrains.mps.typesystem.inference.TypeChecker;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.openapi.editor.Editor;
import jetbrains.mps.openapi.navigation.NavigationSupport;
import jetbrains.mps.openapi.editor.cells.EditorCell;
import jetbrains.mps.nodeEditor.cells.EditorCell_Label;
import org.jetbrains.mps.openapi.module.SModule;
import jetbrains.mps.smodel.SModelStereotype;
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 CreateMethodDeclaration_Action extends BaseAction {
private static final Icon ICON = null;
public CreateMethodDeclaration_Action() {
super("Create Method Declaration", "", ICON);
this.setIsAlwaysVisible(false);
this.setExecuteOutsideCommand(false);
}
@Override
public boolean isDumbAware() {
return true;
}
@Override
public boolean isApplicable(AnActionEvent event, final Map<String, Object> _params) {
return CreateMethodDeclaration_Action.this.getMethodName(event) != null;
}
@Override
public void doUpdate(@NotNull AnActionEvent event, final Map<String, Object> _params) {
this.setEnabledState(event.getPresentation(), this.isApplicable(event, _params));
}
@Override
protected boolean collectActionData(AnActionEvent event, final Map<String, Object> _params) {
if (!(super.collectActionData(event, _params))) {
return false;
}
{
SNode node = event.getData(MPSCommonDataKeys.NODE);
if (node != null && !(SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getInterfaceConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x116b46ac030L, "jetbrains.mps.baseLanguage.structure.IOperation")))) {
node = null;
}
if (node == null) {
return false;
}
}
{
MPSProject p = event.getData(MPSCommonDataKeys.MPS_PROJECT);
if (p == null) {
return false;
}
}
{
EditorContext p = event.getData(MPSEditorDataKeys.EDITOR_CONTEXT);
if (p == null) {
return false;
}
}
return true;
}
@Override
public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
SNode classifier = CreateMethodDeclaration_Action.this.getClassifier(event);
boolean isSameClassifier = SNodeOperations.getNodeAncestor(event.getData(MPSCommonDataKeys.NODE), MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101d9d3ca30L, "jetbrains.mps.baseLanguage.structure.Classifier"), false, false) == classifier;
String name = CreateMethodDeclaration_Action.this.getMethodName(event);
SNode type;
SNode inferType = TypeChecker.getInstance().getInferredTypeOf(event.getData(MPSCommonDataKeys.NODE));
if (SNodeOperations.isInstanceOf(inferType, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37f506dL, "jetbrains.mps.baseLanguage.structure.Type"))) {
type = SNodeOperations.cast(inferType, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8c37f506dL, "jetbrains.mps.baseLanguage.structure.Type"));
} else {
type = _quotation_createNode_v1wtfy_a0a0a5a0();
}
SNode method = _quotation_createNode_v1wtfy_a0g0a(type, name);
if (!(isSameClassifier)) {
SLinkOperations.setTarget(method, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x112670d273fL, 0x112670d886aL, "visibility"), _quotation_createNode_v1wtfy_a0a0h0a());
}
ListSequence.fromList(SLinkOperations.getChildren(classifier, MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101d9d3ca30L, 0x4a9a46de59132803L, "member"))).addElement(method);
SNodeOperations.replaceWithAnother(event.getData(MPSCommonDataKeys.NODE), _quotation_createNode_v1wtfy_a0a9a0(method));
if (isSameClassifier) {
event.getData(MPSEditorDataKeys.EDITOR_CONTEXT).selectWRTFocusPolicy(method);
} else {
Editor editor = NavigationSupport.getInstance().openNode(event.getData(MPSCommonDataKeys.MPS_PROJECT), classifier, true, false);
editor.getEditorContext().selectWRTFocusPolicy(method);
}
}
/*package*/ String getMethodName(final AnActionEvent event) {
EditorCell selectedCell = event.getData(MPSEditorDataKeys.EDITOR_CONTEXT).getSelectedCell();
if (!(selectedCell instanceof EditorCell_Label)) {
return null;
}
EditorCell_Label editorCell = ((EditorCell_Label) selectedCell);
if (!(editorCell.isErrorState())) {
return null;
}
String name = editorCell.getRenderedText();
if (name.endsWith("()")) {
name = name.substring(0, name.length() - 2);
}
if (!(name.matches("[a-zA-Z0-9_]*"))) {
return null;
}
return name;
}
/*package*/ SNode getClassifier(final AnActionEvent event) {
SNode operandType = TypeChecker.getInstance().getTypeOf(SLinkOperations.getTarget(SNodeOperations.cast(SNodeOperations.getParent(event.getData(MPSCommonDataKeys.NODE)), MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x116b46a08c4L, "jetbrains.mps.baseLanguage.structure.DotExpression")), MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x116b46a08c4L, 0x116b46a4416L, "operand")));
if (!(SNodeOperations.isInstanceOf(operandType, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, "jetbrains.mps.baseLanguage.structure.ClassifierType")))) {
return null;
}
SNode classifier = SLinkOperations.getTarget(SNodeOperations.cast(operandType, MetaAdapterFactory.getConcept(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, "jetbrains.mps.baseLanguage.structure.ClassifierType")), MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x101de48bf9eL, 0x101de490babL, "classifier"));
SModule module = SNodeOperations.getModel(classifier).getModule();
if (!(SModelStereotype.isUserModel(SNodeOperations.getModel(classifier))) || module.isReadOnly()) {
return null;
}
return classifier;
}
private static SNode _quotation_createNode_v1wtfy_a0a0a5a0() {
PersistenceFacade facade = PersistenceFacade.getInstance();
SNode quotedNode_1 = null;
quotedNode_1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8cc6bf96dL, "VoidType"), null, null, false);
return quotedNode_1;
}
private static SNode _quotation_createNode_v1wtfy_a0g0a(Object parameter_1, Object parameter_2) {
PersistenceFacade facade = PersistenceFacade.getInstance();
SNode quotedNode_3 = null;
SNode quotedNode_4 = null;
SNode quotedNode_5 = null;
SNode quotedNode_6 = null;
quotedNode_3 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8cc56b21dL, "InstanceMethodDeclaration"), null, null, false);
SNodeAccessUtil.setProperty(quotedNode_3, MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name"), (String) parameter_2);
quotedNode_4 = (SNode) parameter_1;
if (quotedNode_4 != null) {
quotedNode_3.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b1fcL, 0xf8cc56b1fdL, "returnType"), HUtil.copyIfNecessary(quotedNode_4));
}
quotedNode_5 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0x10af9586f0cL, "PrivateVisibility"), null, null, false);
quotedNode_3.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x112670d273fL, 0x112670d886aL, "visibility"), quotedNode_5);
quotedNode_6 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0xf8cc56b200L, "StatementList"), null, null, false);
quotedNode_3.addChild(MetaAdapterFactory.getContainmentLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0xf8cc56b1fcL, 0xf8cc56b1ffL, "body"), quotedNode_6);
return quotedNode_3;
}
private static SNode _quotation_createNode_v1wtfy_a0a0h0a() {
PersistenceFacade facade = PersistenceFacade.getInstance();
SNode quotedNode_1 = null;
quotedNode_1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0x10af9581ff1L, "PublicVisibility"), null, null, false);
return quotedNode_1;
}
private static SNode _quotation_createNode_v1wtfy_a0a9a0(Object parameter_1) {
PersistenceFacade facade = PersistenceFacade.getInstance();
SNode quotedNode_2 = null;
quotedNode_2 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(MetaAdapterFactory.getLanguage(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, "jetbrains.mps.baseLanguage"), 0x118154a6332L, "InstanceMethodCallOperation"), null, null, false);
SNodeAccessUtil.setReferenceTarget(quotedNode_2, MetaAdapterFactory.getReferenceLink(0xf3061a5392264cc5L, 0xa443f952ceaf5816L, 0x11857355952L, 0xf8c78301adL, "baseMethodDeclaration"), (SNode) parameter_1);
return quotedNode_2;
}
}