Search in sources :

Example 1 with MxmlJSClass

use of com.intellij.javascript.flex.mxml.MxmlJSClass in project intellij-plugins by JetBrains.

the class FlashUmlDataModel method createEdge.

@Override
@Nullable
public DiagramEdge<Object> createEdge(@NotNull final DiagramNode<Object> from, @NotNull final DiagramNode<Object> to) {
    final JSClass fromClass = (JSClass) from.getIdentifyingElement();
    final JSClass toClass = (JSClass) to.getIdentifyingElement();
    if (fromClass.isEquivalentTo(toClass)) {
        return null;
    }
    if (toClass.isInterface()) {
        if (JSPsiImplUtils.containsEquivalent(fromClass.isInterface() ? fromClass.getSuperClasses() : fromClass.getImplementedInterfaces(), toClass)) {
            return null;
        }
        Callable<DiagramEdge<Object>> callable = () -> {
            String targetQName = toClass.getQualifiedName();
            JSRefactoringUtil.addToSupersList(fromClass, targetQName, true);
            if (targetQName.contains(".") && !(fromClass instanceof XmlBackedJSClassImpl)) {
                List<FormatFixer> formatters = new ArrayList<>();
                formatters.add(ImportUtils.insertImportStatements(fromClass, Collections.singletonList(targetQName)));
                formatters.addAll(ECMAScriptImportOptimizer.executeNoFormat(fromClass.getContainingFile()));
                FormatFixer.fixAll(formatters);
            }
            return addEdgeAndRefresh(from, to, fromClass.isInterface() ? FlashUmlRelationship.GENERALIZATION : FlashUmlRelationship.INTERFACE_GENERALIZATION);
        };
        String commandName = FlexBundle.message(fromClass.isInterface() ? "create.extends.relationship.command.name" : "create.implements.relationship.command.name", fromClass.getQualifiedName(), toClass.getQualifiedName());
        return DiagramAction.performCommand(getBuilder(), callable, commandName, null, fromClass.getContainingFile());
    } else {
        if (fromClass.isInterface()) {
            return null;
        } else if (fromClass instanceof XmlBackedJSClassImpl) {
            JSClass[] superClasses = fromClass.getSuperClasses();
            if (JSPsiImplUtils.containsEquivalent(superClasses, toClass)) {
                return null;
            }
            if (superClasses.length > 0) {
                // if base component is not resolved, replace it silently
                final JSClass currentParent = superClasses[0];
                if (Messages.showYesNoDialog(FlexBundle.message("replace.base.component.prompt", currentParent.getQualifiedName(), toClass.getQualifiedName()), FlexBundle.message("create.edge.title"), Messages.getQuestionIcon()) == Messages.NO) {
                    return null;
                }
            }
            Callable<DiagramEdge<Object>> callable = () -> {
                NewFlexComponentAction.setParentComponent((MxmlJSClass) fromClass, toClass.getQualifiedName());
                return addEdgeAndRefresh(from, to, DiagramRelationships.GENERALIZATION);
            };
            String commandName = FlexBundle.message("create.extends.relationship.command.name", fromClass.getQualifiedName(), toClass.getQualifiedName());
            return DiagramAction.performCommand(getBuilder(), callable, commandName, null, fromClass.getContainingFile());
        } else {
            final JSClass[] superClasses = fromClass.getSuperClasses();
            if (JSPsiImplUtils.containsEquivalent(superClasses, toClass)) {
                return null;
            }
            if (superClasses.length > 0 && !JSResolveUtil.isObjectClass(superClasses[0])) {
                // if base class is not resolved, replace it silently
                final JSClass currentParent = superClasses[0];
                if (Messages.showYesNoDialog(FlexBundle.message("replace.base.class.prompt", currentParent.getQualifiedName(), toClass.getQualifiedName()), FlexBundle.message("create.edge.title"), Messages.getQuestionIcon()) == Messages.NO) {
                    return null;
                }
            }
            Callable<DiagramEdge<Object>> callable = () -> {
                List<FormatFixer> formatters = new ArrayList<>();
                boolean optimize = false;
                if (superClasses.length > 0 && !JSResolveUtil.isObjectClass(superClasses[0])) {
                    JSRefactoringUtil.removeFromReferenceList(fromClass.getExtendsList(), superClasses[0], formatters);
                    optimize = needsImport(fromClass, superClasses[0]);
                }
                JSRefactoringUtil.addToSupersList(fromClass, toClass.getQualifiedName(), false);
                if (needsImport(fromClass, toClass)) {
                    formatters.add(ImportUtils.insertImportStatements(fromClass, Collections.singletonList(toClass.getQualifiedName())));
                    optimize = true;
                }
                if (optimize) {
                    formatters.addAll(ECMAScriptImportOptimizer.executeNoFormat(fromClass.getContainingFile()));
                }
                FormatFixer.fixAll(formatters);
                return addEdgeAndRefresh(from, to, DiagramRelationships.GENERALIZATION);
            };
            String commandName = FlexBundle.message("create.extends.relationship.command.name", fromClass.getQualifiedName(), toClass.getQualifiedName());
            return DiagramAction.performCommand(getBuilder(), callable, commandName, null, fromClass.getContainingFile());
        }
    }
}
Also used : XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) Callable(java.util.concurrent.Callable) JSReferenceList(com.intellij.lang.javascript.psi.ecmal4.JSReferenceList) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) FormatFixer(com.intellij.lang.javascript.refactoring.FormatFixer) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with MxmlJSClass

use of com.intellij.javascript.flex.mxml.MxmlJSClass in project intellij-plugins by JetBrains.

the class NodeClassInfo method fillMapsForClass.

private static void fillMapsForClass(final JSClass jsClass, final Map<String, Icon> staticFields, final Map<String, Icon> staticProperties, final Map<String, Icon> fields, final Map<String, Icon> properties) {
    for (final JSField variable : jsClass.getFields()) {
        final JSAttributeList varAttributes = variable.getAttributeList();
        if (varAttributes != null && varAttributes.hasModifier(JSAttributeList.ModifierType.STATIC)) {
            staticFields.put(variable.getName(), variable.getIcon(Iconable.ICON_FLAG_VISIBILITY));
        } else {
            fields.put(variable.getName(), variable.getIcon(Iconable.ICON_FLAG_VISIBILITY));
        }
    }
    for (final JSFunction function : jsClass.getFunctions()) {
        if (function.getKind() == JSFunction.FunctionKind.GETTER && function.getName() != null) {
            final JSAttributeList functionAttributes = function.getAttributeList();
            if (functionAttributes != null && functionAttributes.hasModifier(JSAttributeList.ModifierType.STATIC)) {
                staticProperties.put(function.getName(), function.getIcon(Iconable.ICON_FLAG_VISIBILITY));
            } else {
                properties.put(function.getName(), function.getIcon(Iconable.ICON_FLAG_VISIBILITY));
            }
        }
    }
    if (jsClass instanceof MxmlJSClass) {
        final PsiFile file = jsClass.getContainingFile();
        final XmlFile xmlFile = file instanceof XmlFile ? (XmlFile) file : null;
        final XmlTag rootTag = xmlFile == null ? null : xmlFile.getRootTag();
        if (rootTag != null) {
            processSubtagsRecursively(rootTag, tag -> {
                final String id = tag.getAttributeValue("id");
                if (id != null) {
                    fields.put(id, tag.getIcon(Iconable.ICON_FLAG_VISIBILITY));
                }
                return !MxmlJSClass.isTagThatAllowsAnyXmlContent(tag);
            });
        }
    }
}
Also used : JSAttributeList(com.intellij.lang.javascript.psi.ecmal4.JSAttributeList) XmlFile(com.intellij.psi.xml.XmlFile) JSFunction(com.intellij.lang.javascript.psi.JSFunction) JSField(com.intellij.lang.javascript.psi.JSField) PsiFile(com.intellij.psi.PsiFile) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) XmlTag(com.intellij.psi.xml.XmlTag)

Example 3 with MxmlJSClass

use of com.intellij.javascript.flex.mxml.MxmlJSClass in project intellij-plugins by JetBrains.

the class FlexValue method computeSourcePosition.

@Override
public void computeSourcePosition(@NotNull final XNavigatable navigatable) {
    if (mySourcePosition == null) {
        navigatable.setSourcePosition(null);
        return;
    }
    XSourcePosition result = null;
    final Project project = myDebugProcess.getSession().getProject();
    if (myValueType == ValueType.Variable) {
        final PsiElement contextElement = XDebuggerUtil.getInstance().findContextElement(mySourcePosition.getFile(), mySourcePosition.getOffset(), project, true);
        final JSFunction jsFunction = PsiTreeUtil.getParentOfType(contextElement, JSFunction.class);
        if (jsFunction != null) {
            final Ref<JSVariable> varRef = new Ref<>();
            jsFunction.accept(new JSElementVisitor() {

                @Override
                public void visitJSElement(final JSElement node) {
                    if (varRef.isNull()) {
                        node.acceptChildren(this);
                    }
                }

                @Override
                public void visitJSVariable(final JSVariable node) {
                    if (myName.equals(node.getName())) {
                        varRef.set(node);
                    }
                    super.visitJSVariable(node);
                }
            });
            if (!varRef.isNull()) {
                result = DebuggerSupportUtils.calcSourcePosition(varRef.get());
            }
        }
    } else if (myValueType == ValueType.Parameter) {
        final PsiElement contextElement = XDebuggerUtil.getInstance().findContextElement(mySourcePosition.getFile(), mySourcePosition.getOffset(), project, true);
        final JSFunction jsFunction = PsiTreeUtil.getParentOfType(contextElement, JSFunction.class);
        final JSParameter[] parameters = jsFunction == null ? JSParameter.EMPTY_ARRAY : jsFunction.getParameterVariables();
        for (final JSParameter parameter : parameters) {
            if (myName.equals(parameter.getName())) {
                result = DebuggerSupportUtils.calcSourcePosition(parameter);
                break;
            }
        }
    } else if (myValueType == ValueType.Field && myParentResult != null) {
        final String typeFromFlexValueResult = getTypeAndAdditionalInfo(myParentResult).first;
        final JSClass jsClass = findJSClass(project, ModuleUtilCore.findModuleForFile(mySourcePosition.getFile(), project), typeFromFlexValueResult);
        if (jsClass != null) {
            final Ref<PsiElement> fieldRef = new Ref<>();
            fieldRef.set(JSInheritanceUtil.findMember(myName, jsClass, JSInheritanceUtil.SearchedMemberType.FieldsAndMethods, JSFunction.FunctionKind.GETTER, true));
            if (fieldRef.isNull() && jsClass instanceof MxmlJSClass) {
                final PsiFile file = jsClass.getContainingFile();
                final XmlFile xmlFile = file instanceof XmlFile ? (XmlFile) file : null;
                final XmlTag rootTag = xmlFile == null ? null : xmlFile.getRootTag();
                if (rootTag != null) {
                    NodeClassInfo.processSubtagsRecursively(rootTag, tag -> {
                        final XmlAttribute idAttr = tag.getAttribute("id");
                        final String id = idAttr == null ? null : idAttr.getValue();
                        if (id != null && id.equals(myName)) {
                            fieldRef.set(idAttr);
                        }
                        return !MxmlJSClass.isTagThatAllowsAnyXmlContent(tag);
                    });
                }
            }
            result = DebuggerSupportUtils.calcSourcePosition(fieldRef.get());
        }
    }
    navigatable.setSourcePosition(result);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) Project(com.intellij.openapi.project.Project) Ref(com.intellij.openapi.util.Ref) PsiFile(com.intellij.psi.PsiFile) XSourcePosition(com.intellij.xdebugger.XSourcePosition) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

MxmlJSClass (com.intellij.javascript.flex.mxml.MxmlJSClass)3 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 PsiFile (com.intellij.psi.PsiFile)2 XmlFile (com.intellij.psi.xml.XmlFile)2 XmlTag (com.intellij.psi.xml.XmlTag)2 XmlBackedJSClassImpl (com.intellij.lang.javascript.flex.XmlBackedJSClassImpl)1 JSField (com.intellij.lang.javascript.psi.JSField)1 JSFunction (com.intellij.lang.javascript.psi.JSFunction)1 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)1 JSReferenceList (com.intellij.lang.javascript.psi.ecmal4.JSReferenceList)1 FormatFixer (com.intellij.lang.javascript.refactoring.FormatFixer)1 Project (com.intellij.openapi.project.Project)1 Ref (com.intellij.openapi.util.Ref)1 PsiElement (com.intellij.psi.PsiElement)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XSourcePosition (com.intellij.xdebugger.XSourcePosition)1 Callable (java.util.concurrent.Callable)1 Nullable (org.jetbrains.annotations.Nullable)1