Search in sources :

Example 71 with XmlElementDescriptor

use of com.intellij.xml.XmlElementDescriptor in project intellij-plugins by JetBrains.

the class ActionScriptAnnotatingVisitor method visitJSAttributeNameValuePair.

@Override
public void visitJSAttributeNameValuePair(@NotNull final JSAttributeNameValuePair attributeNameValuePair) {
    final boolean ok = checkReferences(attributeNameValuePair);
    if (!ok)
        return;
    // check if attribute value must be FQN of a class class inherited from some other class
    if (attributeNameValuePair.getValueNode() == null)
        return;
    final PsiElement parent = attributeNameValuePair.getParent();
    final XmlElementDescriptor descriptor = parent instanceof JSAttributeImpl ? ((JSAttributeImpl) parent).getBackedDescriptor() : null;
    final String attributeName = StringUtil.notNullize(attributeNameValuePair.getName(), JSAttributeNameValuePair.DEFAULT);
    final XmlAttributeDescriptor attributeDescriptor = descriptor == null ? null : descriptor.getAttributeDescriptor(attributeName, null);
    final String baseClassFqns = attributeDescriptor == null ? null : attributeDescriptor.getDefaultValue();
    if (baseClassFqns != null && !StringUtil.isEmptyOrSpaces(baseClassFqns)) {
        final PsiReference[] references = attributeNameValuePair.getReferences();
        PsiReference lastReference = references.length > 0 ? references[0] : null;
        for (final PsiReference reference : references) {
            if (reference.getRangeInElement().getEndOffset() > lastReference.getRangeInElement().getEndOffset()) {
                lastReference = reference;
            }
        }
        final PsiElement resolved = lastReference != null ? lastReference.resolve() : null;
        if (resolved instanceof JSClass) {
            boolean correctClass = false;
            final Collection<String> resolvedBaseClasses = new ArrayList<>();
            final GlobalSearchScope scope = JSResolveUtil.getResolveScope(attributeNameValuePair);
            for (String baseClassFqn : StringUtil.split(baseClassFqns, ",")) {
                if ("Object".equals(baseClassFqn)) {
                    correctClass = true;
                    break;
                }
                final PsiElement baseClass = ActionScriptClassResolver.findClassByQNameStatic(baseClassFqn, attributeNameValuePair);
                if (baseClass instanceof JSClass) {
                    resolvedBaseClasses.add(baseClassFqn);
                    if (JSInheritanceUtil.isParentClass((JSClass) resolved, (JSClass) baseClass, false, scope)) {
                        correctClass = true;
                        break;
                    }
                }
            }
            if (!correctClass) {
                final String classesForMessage = resolvedBaseClasses.isEmpty() ? StringUtil.replace(baseClassFqns, ",", ", ") : StringUtil.join(resolvedBaseClasses, ", ");
                myHolder.createErrorAnnotation(calcRangeForReferences(lastReference), JSBundle.message("javascript.expected.class.or.descendant", classesForMessage));
            }
        } else if (resolved != attributeNameValuePair) {
            // for some reason int and uint are resolved to self-reference JSResolveUtil.MyResolveResult() instead of usual JSClass
            myHolder.createErrorAnnotation(attributeNameValuePair.getValueNode(), JSBundle.message("javascript.qualified.class.name.expected"));
        }
    }
}
Also used : JSAttributeImpl(com.intellij.lang.javascript.psi.ecmal4.impl.JSAttributeImpl) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor)

Example 72 with XmlElementDescriptor

use of com.intellij.xml.XmlElementDescriptor in project android by JetBrains.

the class NlProperties method getPropertiesWithReadLock.

@NotNull
private Table<String, String, NlPropertyItem> getPropertiesWithReadLock(@NotNull AndroidFacet facet, @NotNull List<NlComponent> components, @NotNull GradleDependencyManager dependencyManager) {
    ResourceManager localResourceManager = facet.getLocalResourceManager();
    ResourceManager systemResourceManager = facet.getSystemResourceManager();
    if (systemResourceManager == null) {
        Logger.getInstance(NlProperties.class).error("No system resource manager for module: " + facet.getModule().getName());
        return ImmutableTable.of();
    }
    AttributeDefinitions localAttrDefs = localResourceManager.getAttributeDefinitions();
    AttributeDefinitions systemAttrDefs = systemResourceManager.getAttributeDefinitions();
    Table<String, String, NlPropertyItem> combinedProperties = null;
    for (NlComponent component : components) {
        XmlTag tag = component.getTag();
        if (!tag.isValid()) {
            return ImmutableTable.of();
        }
        XmlElementDescriptor elementDescriptor = myDescriptorProvider.getDescriptor(tag);
        if (elementDescriptor == null) {
            return ImmutableTable.of();
        }
        XmlAttributeDescriptor[] descriptors = elementDescriptor.getAttributesDescriptors(tag);
        Table<String, String, NlPropertyItem> properties = HashBasedTable.create(3, descriptors.length);
        for (XmlAttributeDescriptor desc : descriptors) {
            String namespace = getNamespace(desc, tag);
            AttributeDefinitions attrDefs = NS_RESOURCES.equals(namespace) ? systemAttrDefs : localAttrDefs;
            AttributeDefinition attrDef = attrDefs == null ? null : attrDefs.getAttrDefByName(desc.getName());
            NlPropertyItem property = NlPropertyItem.create(components, desc, namespace, attrDef);
            properties.put(StringUtil.notNullize(namespace), property.getName(), property);
        }
        // Exceptions:
        switch(tag.getName()) {
            case AUTO_COMPLETE_TEXT_VIEW:
                // An AutoCompleteTextView has a popup that is created at runtime.
                // Properties for this popup can be added to the AutoCompleteTextView tag.
                properties.put(ANDROID_URI, ATTR_POPUP_BACKGROUND, NlPropertyItem.create(components, new AndroidAnyAttributeDescriptor(ATTR_POPUP_BACKGROUND), ANDROID_URI, systemAttrDefs != null ? systemAttrDefs.getAttrDefByName(ATTR_POPUP_BACKGROUND) : null));
                break;
        }
        combinedProperties = combine(properties, combinedProperties);
    }
    // The following properties are deprecated in the support library and can be ignored by tools:
    assert combinedProperties != null;
    combinedProperties.remove(AUTO_URI, ATTR_PADDING_START);
    combinedProperties.remove(AUTO_URI, ATTR_PADDING_END);
    combinedProperties.remove(AUTO_URI, ATTR_THEME);
    setUpDesignProperties(combinedProperties);
    setUpSrcCompat(combinedProperties, facet, components, dependencyManager);
    initStarState(combinedProperties);
    //noinspection ConstantConditions
    return combinedProperties;
}
Also used : AndroidAnyAttributeDescriptor(org.jetbrains.android.dom.AndroidAnyAttributeDescriptor) AttributeDefinition(org.jetbrains.android.dom.attrs.AttributeDefinition) ResourceManager(org.jetbrains.android.resourceManagers.ResourceManager) AttributeDefinitions(org.jetbrains.android.dom.attrs.AttributeDefinitions) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) NamespaceAwareXmlAttributeDescriptor(com.intellij.xml.NamespaceAwareXmlAttributeDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 73 with XmlElementDescriptor

use of com.intellij.xml.XmlElementDescriptor 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 74 with XmlElementDescriptor

use of com.intellij.xml.XmlElementDescriptor in project intellij-plugins by JetBrains.

the class CreateEventMetadataByMxmlAttributeFix method applyFix.

@Override
protected void applyFix(final Project project, final PsiElement psiElement, PsiFile file, Editor editor) {
    assert psiElement instanceof XmlAttribute;
    final XmlTag tag = (XmlTag) psiElement.getParent();
    final XmlElementDescriptor descriptor = tag.getDescriptor();
    PsiElement type = descriptor == null ? null : descriptor.getDeclaration();
    if (type == null) {
        // can not resolve
        return;
    }
    if (!FileModificationService.getInstance().preparePsiElementForWrite(type)) {
        return;
    }
    file = type.getContainingFile();
    editor = getEditor(project, file);
    if (editor == null) {
        return;
    }
    final boolean addingToMxml = type instanceof XmlFile;
    final TemplateManager templateManager = TemplateManager.getInstance(project);
    final Template template = templateManager.createTemplate("", "");
    template.setToReformat(true);
    if (addingToMxml) {
        template.addTextSegment("\n");
    }
    template.addTextSegment("[Event(name=\"" + myEventName + "\", type=\"");
    template.addVariable(new MyExpression(FlexCommonTypeNames.FLASH_EVENT_FQN), true);
    template.addTextSegment("\")]");
    if (!addingToMxml) {
        template.addTextSegment("\n");
    }
    final int offset;
    if (addingToMxml) {
        XmlTag metadataTag = WriteAction.compute(() -> createOrGetMetadataTag((XmlFile) type));
        offset = metadataTag.getValue().getTextRange().getStartOffset();
    } else {
        offset = type.getTextRange().getStartOffset();
    }
    navigate(project, editor, offset, file.getVirtualFile());
    templateManager.startTemplate(editor, template);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) TemplateManager(com.intellij.codeInsight.template.TemplateManager) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) Template(com.intellij.codeInsight.template.Template)

Example 75 with XmlElementDescriptor

use of com.intellij.xml.XmlElementDescriptor in project intellij-plugins by JetBrains.

the class FlexMxmlNSDescriptor method getElementDescriptor.

@Nullable
public XmlElementDescriptor getElementDescriptor(@NotNull final XmlTag tag) {
    if (MxmlJSClass.isInsideTagThatAllowsAnyXmlContent(tag)) {
        return new AnyXmlElementWithAnyChildrenDescriptor();
    }
    final String namespace = tag.getNamespace();
    final CodeContext context = CodeContext.getContext(namespace, module);
    final String localName = tag.getLocalName();
    XmlElementDescriptor descriptor = context.getElementDescriptor(localName, tag);
    if (descriptor == null) {
        final XmlTag parentTag = tag.getParentTag();
        if (parentTag != null && namespace.equals(parentTag.getNamespace())) {
            final XmlElementDescriptor parentDescriptor = parentTag.getDescriptor();
            if (parentDescriptor != null && // FIXME: prevent stackoverflow due to parent delegation due to different context namespace
            (!(parentDescriptor instanceof ClassBackedElementDescriptor) || ClassBackedElementDescriptor.sameNs(namespace, (((ClassBackedElementDescriptor) parentDescriptor).context.namespace)))) {
                descriptor = parentDescriptor.getElementDescriptor(tag, parentTag);
            } else if (parentDescriptor != null && !reportedAboutStackOverflow) {
                // TODO: remove diagnostic
                Logger.getInstance(getClass().getName()).error(new AssertionError("avoided SOE:\n" + tag.getContainingFile().getText()));
                reportedAboutStackOverflow = true;
            }
        }
    } else if (tag.getParent() instanceof XmlDocument && !isLegalRootElementDescriptor(descriptor)) {
        return null;
    }
    if (descriptor == null && JavaScriptSupportLoader.MXML_URI3.equals(tag.getNamespace())) {
        return FxDefinitionBackedDescriptor.getFxDefinitionBackedDescriptor(module, tag);
    }
    return descriptor;
}
Also used : XmlDocument(com.intellij.psi.xml.XmlDocument) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)159 XmlTag (com.intellij.psi.xml.XmlTag)88 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)60 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)54 PsiElement (com.intellij.psi.PsiElement)23 XmlFile (com.intellij.psi.xml.XmlFile)23 Nullable (org.jetbrains.annotations.Nullable)23 AnyXmlElementDescriptor (com.intellij.xml.impl.schema.AnyXmlElementDescriptor)22 PsiFile (com.intellij.psi.PsiFile)11 ArrayList (java.util.ArrayList)11 XmlAttribute (com.intellij.psi.xml.XmlAttribute)10 NotNull (org.jetbrains.annotations.NotNull)10 ClassBackedElementDescriptor (com.intellij.javascript.flex.mxml.schema.ClassBackedElementDescriptor)7 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)7 Project (com.intellij.openapi.project.Project)7 HtmlTag (com.intellij.psi.html.HtmlTag)7 AnnotationBackedDescriptor (com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)5 XmlDocument (com.intellij.psi.xml.XmlDocument)5 DElementPattern (org.kohsuke.rngom.digested.DElementPattern)5 InvalidPropertyException (com.intellij.flex.uiDesigner.InvalidPropertyException)4