Search in sources :

Example 51 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class FlashUmlDataModel method expandPackage.

public void expandPackage(final String psiPackage) {
    packages.remove(psiPackage);
    packagesRemovedByUser.add(psiPackage);
    final GlobalSearchScope searchScope = GlobalSearchScope.allScope(getProject());
    for (JSClass psiClass : getClasses(psiPackage, searchScope)) {
        addElement(psiClass);
    }
    for (String aPackage : getSubPackages(psiPackage, searchScope)) {
        addElement(aPackage);
    }
}
Also used : GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass)

Example 52 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass 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 53 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class FlashUmlDataModel method findNearestInterfaces.

private static void findNearestInterfaces(final JSClass psiClass, final Set<JSClass> result) {
    for (JSClass anInterface : psiClass.getSuperClasses()) {
        if (result.contains(anInterface)) {
            // don't check isEquivalent, equality check is enough for interfaces
            continue;
        }
        result.add(anInterface);
        findNearestInterfaces(anInterface, result);
    }
}
Also used : MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass)

Example 54 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class FlashUmlDataModel method addElement.

@Nullable
public DiagramNode<Object> addElement(Object element) {
    if (findNode(element) != null)
        return null;
    if (element instanceof JSClass) {
        if (!isAllowedToShow((JSClass) element)) {
            return null;
        }
        JSClass psiClass = (JSClass) element;
        if (psiClass.getQualifiedName() == null)
            return null;
        final SmartPsiElementPointer<JSClass> pointer = spManager.createSmartPsiElementPointer(psiClass);
        final String fqn = psiClass.getQualifiedName();
        classesAddedByUser.put(fqn, pointer);
        classesRemovedByUser.remove(fqn);
        setupScopeManager(psiClass, true);
        return new FlashUmlClassNode((JSClass) element, getProvider());
    } else if (element instanceof String) {
        String aPackage = (String) element;
        packages.add(aPackage);
        packagesRemovedByUser.remove(aPackage);
        return new FlashUmlPackageNode(aPackage, getProvider());
    }
    return null;
}
Also used : MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) Nullable(org.jetbrains.annotations.Nullable)

Example 55 with JSClass

use of com.intellij.lang.javascript.psi.ecmal4.JSClass in project intellij-plugins by JetBrains.

the class JSCreateMethodDialog method createFakeMethod.

static JSFunction createFakeMethod(JSClass clazz, String text, boolean inClass) {
    if (inClass) {
        JSFile file = JSElementFactory.createExpressionCodeFragment(clazz.getProject(), (clazz.isInterface() ? "interface" : "class") + " Dummy {" + text + "}", clazz, JavaScriptSupportLoader.ECMA_SCRIPT_L4, null, JSElementFactory.TopLevelCompletion.NO, null);
        JSClass aClass = PsiTreeUtil.findChildOfType(file, JSClass.class);
        return aClass.getFunctions()[0];
    } else {
        JSFile file = JSElementFactory.createExpressionCodeFragment(clazz.getProject(), text, clazz, JavaScriptSupportLoader.ECMA_SCRIPT_L4, null, JSElementFactory.TopLevelCompletion.NO, null);
        return PsiTreeUtil.findChildOfType(file, JSFunction.class);
    }
}
Also used : JSFile(com.intellij.lang.javascript.psi.JSFile) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass)

Aggregations

JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)141 PsiElement (com.intellij.psi.PsiElement)65 Nullable (org.jetbrains.annotations.Nullable)27 MxmlJSClass (com.intellij.javascript.flex.mxml.MxmlJSClass)23 NotNull (org.jetbrains.annotations.NotNull)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)22 JSFunction (com.intellij.lang.javascript.psi.JSFunction)18 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)18 Module (com.intellij.openapi.module.Module)17 XmlFile (com.intellij.psi.xml.XmlFile)17 Project (com.intellij.openapi.project.Project)16 PsiFile (com.intellij.psi.PsiFile)16 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)15 ArrayList (java.util.ArrayList)14 PsiDirectory (com.intellij.psi.PsiDirectory)11 XmlTag (com.intellij.psi.xml.XmlTag)11 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)8 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)8 XmlBackedJSClassImpl (com.intellij.lang.javascript.flex.XmlBackedJSClassImpl)7