Search in sources :

Example 6 with XmlBackedJSClassImpl

use of com.intellij.lang.javascript.flex.XmlBackedJSClassImpl in project intellij-plugins by JetBrains.

the class FlexXmlBackedSymbolContributor method getItemsByName.

@NotNull
public NavigationItem[] getItemsByName(String name, final String pattern, Project project, boolean includeNonProjectItems) {
    Collection<NavigationItem> result = new THashSet<>();
    result.addAll(FlexXmlBackedMembersIndex.getItemsByName(name, project));
    for (NavigationItem item : JavaScriptClassContributor.getItemsByNameStatic(name, project, includeNonProjectItems)) {
        if (item instanceof XmlBackedJSClassImpl) {
            result.add(item);
        }
    }
    return result.toArray(new NavigationItem[result.size()]);
}
Also used : NavigationItem(com.intellij.navigation.NavigationItem) XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) THashSet(gnu.trove.THashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with XmlBackedJSClassImpl

use of com.intellij.lang.javascript.flex.XmlBackedJSClassImpl in project intellij-plugins by JetBrains.

the class FlashUmlDependencyProvider method computeUsedClasses.

public Collection<Pair<JSClass, FlashUmlRelationship>> computeUsedClasses() {
    final Collection<Pair<JSClass, FlashUmlRelationship>> result = new ArrayList<>();
    final JSElementVisitor visitor = new JSElementVisitor() {

        JSVariable myVariable;

        JSNewExpression myNewExpression;

        boolean myInField;

        boolean myInParameter;

        @Override
        public void visitJSReferenceExpression(final JSReferenceExpression node) {
            if (PsiTreeUtil.getParentOfType(node, JSImportStatement.class) != null) {
                return;
            }
            if (myVariable == null && myNewExpression == null && !myInParameter && isReturnTypeReference(node)) {
                return;
            }
            PsiElement resolved = node.resolve();
            if (myNewExpression != null && resolved instanceof JSFunction) {
                if (((JSFunction) resolved).isConstructor()) {
                    resolved = JSResolveUtil.findParent(resolved);
                }
            }
            if (resolved instanceof JSClass) {
                FlashUmlRelationship relType;
                if (node.getParent() instanceof JSReferenceExpression) {
                    relType = Factory.dependency(myInField ? myVariable.getName() : null, myVariable != null ? myVariable : node);
                } else if (myNewExpression != null) {
                    if (node.getParent() instanceof JSGenericSignature) {
                        relType = Factory.dependency(myInField ? myVariable.getName() : null, myVariable != null ? myVariable : node);
                    } else {
                        relType = Factory.create(myNewExpression);
                    }
                } else if (myInField && node.getParent() instanceof JSGenericSignature) {
                    assert myVariable != null;
                    String qName = ((JSClass) resolved).getQualifiedName();
                    if (FlashUmlVfsResolver.isVectorType(qName)) {
                        relType = Factory.dependency(myVariable.getName(), myVariable);
                    } else {
                        relType = Factory.oneToMany(myVariable.getName(), myVariable);
                    }
                } else if (myInField) {
                    assert myVariable != null;
                    String qName = ((JSClass) resolved).getQualifiedName();
                    if (FlashUmlVfsResolver.isVectorType(qName)) {
                        relType = Factory.dependency(myVariable.getName(), myVariable);
                    } else {
                        relType = Factory.oneToOne(myVariable.getName(), myVariable);
                    }
                } else {
                    relType = Factory.dependency(null, myVariable != null ? myVariable : node);
                }
                result.add(Pair.create((JSClass) resolved, relType));
            }
            super.visitJSReferenceExpression(node);
        }

        @Override
        public void visitJSVariable(final JSVariable node) {
            if (node instanceof JSParameter) {
                myInParameter = true;
            } else {
                myVariable = node;
            }
            myInField = JSResolveUtil.findParent(node) instanceof JSClass;
            try {
                super.visitJSVariable(node);
            } finally {
                myVariable = null;
                myInField = false;
                myInParameter = false;
            }
        }

        @Override
        public void visitJSNewExpression(final JSNewExpression node) {
            myNewExpression = node;
            try {
                super.visitJSNewExpression(node);
            } finally {
                myNewExpression = null;
            }
        }

        @Override
        public void visitElement(final PsiElement element) {
            super.visitElement(element);
            element.acceptChildren(this);
        }
    };
    if (myClazz instanceof XmlBackedJSClassImpl) {
        // TODO process attributes
        ((XmlBackedJSClassImpl) myClazz).processInjectedFiles(jsFile -> {
            jsFile.accept(visitor);
            return true;
        });
        myClazz.getParent().acceptChildren(new // don't visit parent tag
        XmlElementVisitor() {

            // also to prevent extra references resolve
            private String myInClassAttributeName;

            @Override
            public void visitXmlTag(final XmlTag tag) {
                XmlElementDescriptor descriptor = tag.getDescriptor();
                if (descriptor != null) {
                    PsiElement declaration = descriptor.getDeclaration();
                    if (declaration instanceof XmlFile && JavaScriptSupportLoader.isFlexMxmFile((PsiFile) declaration)) {
                        declaration = XmlBackedJSClassFactory.getXmlBackedClass((XmlFile) declaration);
                    }
                    if (declaration instanceof JSClass) {
                        XmlAttribute id = tag.getAttribute("id");
                        FlashUmlRelationship type = id != null && StringUtil.isNotEmpty(id.getValue()) ? Factory.oneToOne(id.getValue(), id) : Factory.dependency(null, tag);
                        result.add(Pair.create((JSClass) declaration, type));
                    }
                }
                super.visitXmlTag(tag);
            }

            @Override
            public void visitXmlAttribute(final XmlAttribute attribute) {
                XmlAttributeDescriptor descriptor = attribute.getDescriptor();
                if (descriptor instanceof AnnotationBackedDescriptor) {
                    if (FlexReferenceContributor.isClassReferenceType(((AnnotationBackedDescriptor) descriptor).getType())) {
                        myInClassAttributeName = StringUtil.notNullize(attribute.getName());
                        try {
                            super.visitXmlAttribute(attribute);
                        } finally {
                            myInClassAttributeName = null;
                        }
                    }
                }
            }

            @Override
            public void visitXmlAttributeValue(final XmlAttributeValue value) {
                if (myInClassAttributeName != null) {
                    processReferenceSet(value.getReferences(), result, Factory.dependency(myInClassAttributeName, value.getParent()));
                }
            }

            @Override
            public void visitXmlText(final XmlText text) {
                List<Pair<PsiElement, TextRange>> injectedFiles = InjectedLanguageManager.getInstance(text.getProject()).getInjectedPsiFiles(text);
                if (injectedFiles != null) {
                    for (Pair<PsiElement, TextRange> pair : injectedFiles) {
                        if (CSS.is(pair.first.getLanguage())) {
                            pair.first.accept(new CssElementVisitor() {

                                // to prevent extra references resolve
                                private boolean myInClassReference;

                                @Override
                                public void visitElement(final PsiElement element) {
                                    super.visitElement(element);
                                    element.acceptChildren(this);
                                }

                                @Override
                                public void visitCssFunction(final CssFunction _function) {
                                    if (FlexReferenceContributor.CLASS_REFERENCE.equals(_function.getName())) {
                                        myInClassReference = true;
                                        try {
                                            super.visitCssFunction(_function);
                                        } finally {
                                            myInClassReference = false;
                                        }
                                    }
                                }

                                @Override
                                public void visitCssString(final CssString _string) {
                                    if (myInClassReference) {
                                        CssDeclaration declaration = PsiTreeUtil.getParentOfType(_string, CssDeclaration.class);
                                        if (declaration != null) {
                                            processReferenceSet(_string.getReferences(), result, Factory.dependency(declaration.getPropertyName(), declaration));
                                        }
                                    }
                                }
                            });
                        }
                    }
                }
                super.visitXmlText(text);
            }

            @Override
            public void visitElement(final PsiElement element) {
                super.visitElement(element);
                element.acceptChildren(this);
            }
        });
    }
    myClazz.processDeclarations(new BaseScopeProcessor() {

        @Override
        public boolean execute(@NotNull final PsiElement element, @NotNull final ResolveState state) {
            element.accept(visitor);
            return true;
        }
    }, ResolveState.initial(), myClazz, myClazz);
    return result;
}
Also used : XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) JSImportStatement(com.intellij.lang.javascript.psi.ecmal4.JSImportStatement) ArrayList(java.util.ArrayList) JSGenericSignature(com.intellij.lang.javascript.psi.ecmal4.JSGenericSignature) CssString(com.intellij.psi.css.CssString) BaseScopeProcessor(com.intellij.psi.scope.BaseScopeProcessor) AnnotationBackedDescriptor(com.intellij.lang.javascript.flex.AnnotationBackedDescriptor) CssElementVisitor(com.intellij.psi.css.CssElementVisitor) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) CssFunction(com.intellij.psi.css.CssFunction) Pair(com.intellij.openapi.util.Pair) CssDeclaration(com.intellij.psi.css.CssDeclaration) CssString(com.intellij.psi.css.CssString) TextRange(com.intellij.openapi.util.TextRange) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor)

Example 8 with XmlBackedJSClassImpl

use of com.intellij.lang.javascript.flex.XmlBackedJSClassImpl in project intellij-plugins by JetBrains.

the class FlashUmlDataModel method removeEdge.

@Override
public void removeEdge(DiagramEdge<Object> edge) {
    final Object source = edge.getSource().getIdentifyingElement();
    final Object target = edge.getTarget().getIdentifyingElement();
    final DiagramRelationshipInfo relationship = edge.getRelationship();
    if (!(source instanceof JSClass) || !(target instanceof JSClass) || relationship == DiagramRelationshipInfo.NO_RELATIONSHIP) {
        return;
    }
    final JSClass fromClass = (JSClass) source;
    final JSClass toClass = (JSClass) target;
    if (JSProjectUtil.isInLibrary(fromClass)) {
        return;
    }
    if (fromClass instanceof XmlBackedJSClassImpl && !toClass.isInterface()) {
        Messages.showErrorDialog(fromClass.getProject(), FlexBundle.message("base.component.needed.message"), FlexBundle.message("remove.edge.title"));
        return;
    }
    if (Messages.showYesNoDialog(fromClass.getProject(), FlexBundle.message("remove.inheritance.link.prompt", fromClass.getQualifiedName(), toClass.getQualifiedName()), FlexBundle.message("remove.edge.title"), Messages.getQuestionIcon()) != Messages.YES) {
        return;
    }
    final Runnable runnable = () -> {
        JSReferenceList refList = !fromClass.isInterface() && toClass.isInterface() ? fromClass.getImplementsList() : fromClass.getExtendsList();
        List<FormatFixer> formatters = new ArrayList<>();
        JSRefactoringUtil.removeFromReferenceList(refList, toClass, formatters);
        if (!(fromClass instanceof XmlBackedJSClassImpl) && needsImport(fromClass, toClass)) {
            formatters.addAll(ECMAScriptImportOptimizer.executeNoFormat(fromClass.getContainingFile()));
        }
        FormatFixer.fixAll(formatters);
    };
    DiagramAction.performCommand(getBuilder(), runnable, FlexBundle.message("remove.relationship.command.name"), null, fromClass.getContainingFile());
}
Also used : XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) JSReferenceList(com.intellij.lang.javascript.psi.ecmal4.JSReferenceList) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) JSReferenceList(com.intellij.lang.javascript.psi.ecmal4.JSReferenceList)

Example 9 with XmlBackedJSClassImpl

use of com.intellij.lang.javascript.flex.XmlBackedJSClassImpl in project intellij-plugins by JetBrains.

the class AnnotationBackedDescriptorImpl method getDeclaration.

@Override
@Nullable
public PsiElement getDeclaration() {
    final PsiElement[] result = new PsiElement[1];
    final PsiElement parentDescriptorDeclaration = parentDescriptor.getDeclaration();
    PsiElement element = parentDescriptorDeclaration;
    if (predefined)
        return element;
    if (myOriginatingElementType == OriginatingElementType.IdAttribute) {
        return findDeclarationByIdAttributeValue(parentDescriptorDeclaration, new THashSet<>());
    }
    while (element instanceof XmlFile) {
        XmlTag rootTag = ((XmlFile) element).getDocument().getRootTag();
        element = JSResolveUtil.getClassFromTagNameInMxml(rootTag.getFirstChild());
        if (element instanceof XmlBackedJSClassImpl) {
            element = element.getParent().getContainingFile();
        }
    }
    final JSNamedElement jsClass = (JSNamedElement) element;
    final ClassBackedElementDescriptor.AttributedItemsProcessor itemsProcessor = new ClassBackedElementDescriptor.AttributedItemsProcessor() {

        @Override
        public boolean process(final JSNamedElement jsNamedElement, final boolean isPackageLocalVisibility) {
            if (myOriginatingElementType != OriginatingElementType.Metadata && name.equals(jsNamedElement.getName())) {
                result[0] = jsNamedElement;
                return false;
            }
            return true;
        }

        @Override
        public boolean process(final JSAttributeNameValuePair pair, final String annotationName, final boolean included) {
            if (myOriginatingElementType == OriginatingElementType.Metadata && name.equals(pair.getSimpleValue()) && included) {
                result[0] = pair;
                return false;
            }
            return true;
        }
    };
    boolean b = jsClass == null || ClassBackedElementDescriptor.processAttributes(jsClass, itemsProcessor);
    if (b && jsClass instanceof JSClass) {
        final JSClass clazz = (JSClass) jsClass;
        final JSClass[] classes = clazz.getSuperClasses();
        if (classes.length > 0 && clazz.getName().equals(classes[0].getName()) && clazz != classes[0]) {
            b = ClassBackedElementDescriptor.processAttributes(classes[0], itemsProcessor);
        }
    }
    if (b && parentDescriptorDeclaration instanceof XmlFile) {
        final JSResolveUtil.JSInjectedFilesVisitor injectedFilesVisitor = new JSResolveUtil.JSInjectedFilesVisitor() {

            @Override
            protected void process(final JSFile file) {
                ClassBackedElementDescriptor.processAttributes(file, itemsProcessor);
            }
        };
        element = parentDescriptorDeclaration;
        while (element instanceof XmlFile) {
            XmlTag rootTag = ((XmlFile) element).getDocument().getRootTag();
            FlexUtils.processMxmlTags(rootTag, true, injectedFilesVisitor);
            if (result[0] != null)
                break;
            element = JSResolveUtil.getClassFromTagNameInMxml(rootTag.getFirstChild());
            if (element instanceof XmlBackedJSClassImpl) {
                element = element.getParent().getContainingFile();
            }
        }
    }
    return result[0];
}
Also used : XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with XmlBackedJSClassImpl

use of com.intellij.lang.javascript.flex.XmlBackedJSClassImpl in project intellij-plugins by JetBrains.

the class ClassBackedElementDescriptor method ensureDescriptorsMapsInitialized.

private void ensureDescriptorsMapsInitialized(PsiElement element, @Nullable Set<JSClass> visited) {
    Map<String, AnnotationBackedDescriptor> map;
    Map<String, Map<String, AnnotationBackedDescriptor>> packageToInternalDescriptors;
    synchronized (CodeContext.class) {
        map = myDescriptors;
        packageToInternalDescriptors = myPackageToInternalDescriptors;
        if (map != null && packageToInternalDescriptors != null)
            return;
        map = new THashMap<>();
        packageToInternalDescriptors = new THashMap<>();
        Set<PsiElement> processedElements = null;
        if (element instanceof XmlBackedJSClassImpl) {
            // TODO: make this code and following loop better
            element = element.getParent().getContainingFile();
        }
        if (element instanceof XmlFile && MxmlJSClass.isFxgFile((PsiFile) element)) {
            element = XmlBackedJSClassFactory.getXmlBackedClass((XmlFile) element);
        }
        while (element instanceof XmlFile) {
            final XmlDocument document = ((XmlFile) element).getDocument();
            final XmlTag rootTag = document != null ? document.getRootTag() : null;
            final XmlElementDescriptor descriptor = rootTag != null ? rootTag.getDescriptor() : null;
            if (processedElements == null)
                processedElements = new THashSet<>();
            processedElements.add(element);
            element = descriptor != null ? descriptor.getDeclaration() : null;
            if (processedElements.contains(element))
                break;
            collectMxmlAttributes(map, packageToInternalDescriptors, rootTag);
        }
        if (element instanceof JSNamedElement) {
            JSNamedElement jsClass = (JSNamedElement) element;
            if (visited == null || !visited.contains(jsClass)) {
                if (!MxmlJSClass.XML_TAG_NAME.equals(jsClass.getName()) && !MxmlJSClass.XMLLIST_TAG_NAME.equals(jsClass.getName())) {
                    JSReferenceList extendsList = jsClass instanceof JSClass ? ((JSClass) jsClass).getExtendsList() : null;
                    if (extendsList != null) {
                        final JSClass clazz = (JSClass) jsClass;
                        if (visited == null) {
                            visited = new THashSet<>();
                        }
                        visited.add(clazz);
                        for (JSClass superClazz : clazz.getSuperClasses()) {
                            appendSuperClassDescriptors(map, packageToInternalDescriptors, superClazz, visited);
                        }
                    } else if (!OBJECT_CLASS_NAME.equals(jsClass.getName()) && CodeContext.isStdNamespace(context.namespace)) {
                        appendSuperClassDescriptors(map, packageToInternalDescriptors, ActionScriptClassResolver.findClassByQNameStatic(OBJECT_CLASS_NAME, jsClass), visited);
                    }
                }
                collectMyAttributes(jsClass, map, packageToInternalDescriptors);
            }
        }
        myDescriptors = map;
        myPackageToInternalDescriptors = packageToInternalDescriptors;
    }
}
Also used : XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) AnnotationBackedDescriptor(com.intellij.lang.javascript.flex.AnnotationBackedDescriptor) THashSet(gnu.trove.THashSet) PsiFile(com.intellij.psi.PsiFile) THashMap(gnu.trove.THashMap) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) PsiElement(com.intellij.psi.PsiElement)

Aggregations

XmlBackedJSClassImpl (com.intellij.lang.javascript.flex.XmlBackedJSClassImpl)13 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)6 PsiElement (com.intellij.psi.PsiElement)6 MxmlJSClass (com.intellij.javascript.flex.mxml.MxmlJSClass)5 Nullable (org.jetbrains.annotations.Nullable)5 XmlFile (com.intellij.psi.xml.XmlFile)4 AnnotationBackedDescriptor (com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)3 Ref (com.intellij.openapi.util.Ref)3 XmlTag (com.intellij.psi.xml.XmlTag)3 JSFile (com.intellij.lang.javascript.psi.JSFile)2 JSFunction (com.intellij.lang.javascript.psi.JSFunction)2 JSAttribute (com.intellij.lang.javascript.psi.ecmal4.JSAttribute)2 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)2 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)2 JSReferenceList (com.intellij.lang.javascript.psi.ecmal4.JSReferenceList)2 JSResolveUtil (com.intellij.lang.javascript.psi.resolve.JSResolveUtil)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)2 THashMap (gnu.trove.THashMap)2 THashSet (gnu.trove.THashSet)2