Search in sources :

Example 51 with XmlAttributeDescriptor

use of com.intellij.xml.XmlAttributeDescriptor 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 52 with XmlAttributeDescriptor

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

the class FlexMxmlColorAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (!(element instanceof XmlAttribute) || !JavaScriptSupportLoader.isFlexMxmFile(element.getContainingFile())) {
        return;
    }
    if (!LineMarkerSettings.getSettings().isEnabled(new ColorLineMarkerProvider())) {
        return;
    }
    XmlAttribute attribute = (XmlAttribute) element;
    XmlAttributeDescriptor descriptor = attribute.getDescriptor();
    if (!(descriptor instanceof AnnotationBackedDescriptorImpl)) {
        return;
    }
    AnnotationBackedDescriptorImpl annotationBackedDescriptor = (AnnotationBackedDescriptorImpl) descriptor;
    String format = annotationBackedDescriptor.getFormat();
    if (!FlexCssPropertyDescriptor.COLOR_FORMAT.equals(format)) {
        return;
    }
    final String value = attribute.getValue();
    if (value == null || value.length() == 0) {
        return;
    }
    if (!JSCommonTypeNames.ARRAY_CLASS_NAME.equals(annotationBackedDescriptor.getType())) {
        XmlAttributeValue valueElement = attribute.getValueElement();
        if (valueElement != null) {
            Annotation annotation = holder.createInfoAnnotation(valueElement, null);
            annotation.setGutterIconRenderer(new MyRenderer(value, attribute));
        }
    }
}
Also used : ColorLineMarkerProvider(com.intellij.ui.ColorLineMarkerProvider) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AnnotationBackedDescriptorImpl(com.intellij.javascript.flex.mxml.schema.AnnotationBackedDescriptorImpl) Annotation(com.intellij.lang.annotation.Annotation)

Example 53 with XmlAttributeDescriptor

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

the class ResolveExternalInlineStyleSourceAction method findTargetIfStyleDeclarationOwner.

private Navigatable findTargetIfStyleDeclarationOwner(XmlTag parent) {
    int foundCount = 0;
    Navigatable target = null;
    for (XmlAttribute attribute : parent.getAttributes()) {
        XmlAttributeDescriptor descriptor = attribute.getDescriptor();
        // 8
        if (descriptor instanceof AnnotationBackedDescriptor) {
            String ourValue = properties.get(descriptor.getName());
            if (ourValue != null) {
                if (attribute.getDisplayValue().equals(ourValue)) {
                    foundCount++;
                    if (descriptor.getName().equals(targetStyleName)) {
                        target = (Navigatable) attribute;
                    }
                    if (foundCount == properties.size()) {
                        return target;
                    }
                }
            }
        }
    }
    for (XmlTag tag : parent.getSubTags()) {
        XmlElementDescriptor descriptor = tag.getDescriptor();
        if (descriptor instanceof AnnotationBackedDescriptor && ((PsiPresentableMetaData) descriptor).getTypeName().equals(FlexAnnotationNames.STYLE)) {
            String ourValue = properties.get(descriptor.getName());
            if (ourValue != null) {
                if (tag.getSubTags().length == 0 && tag.getValue().getTrimmedText().equals(ourValue)) {
                    foundCount++;
                    if (descriptor.getName().equals(targetStyleName)) {
                        target = (Navigatable) tag;
                    }
                    if (foundCount == properties.size()) {
                        return target;
                    }
                }
            }
        }
    }
    return null;
}
Also used : PsiPresentableMetaData(com.intellij.psi.meta.PsiPresentableMetaData) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) AnnotationBackedDescriptor(com.intellij.lang.javascript.flex.AnnotationBackedDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) Navigatable(com.intellij.pom.Navigatable) XmlTag(com.intellij.psi.xml.XmlTag)

Example 54 with XmlAttributeDescriptor

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

the class AngularJSAttributeDescriptorsProvider method getAttributeDescriptors.

@Override
public XmlAttributeDescriptor[] getAttributeDescriptors(XmlTag xmlTag) {
    if (xmlTag != null) {
        final Map<String, XmlAttributeDescriptor> result = new LinkedHashMap<>();
        final Project project = xmlTag.getProject();
        final XmlElementDescriptor descriptor = xmlTag.getDescriptor();
        final Collection<String> directives = AngularIndexUtil.getAllKeys(AngularDirectivesIndex.KEY, project);
        if (AngularIndexUtil.hasAngularJS2(project)) {
            if (descriptor instanceof HtmlElementDescriptorImpl) {
                final XmlAttributeDescriptor[] descriptors = ((HtmlElementDescriptorImpl) descriptor).getDefaultAttributeDescriptors(xmlTag);
                for (XmlAttributeDescriptor attributeDescriptor : descriptors) {
                    final String name = attributeDescriptor.getName();
                    if (name.startsWith("on")) {
                        addAttributes(project, result, "(" + name.substring(2) + ")", null);
                    }
                }
            }
            for (XmlAttribute attribute : xmlTag.getAttributes()) {
                final String name = attribute.getName();
                if (isAngular2Attribute(name, project) || !directives.contains(name))
                    continue;
                final PsiElement declaration = applicableDirective(project, name, xmlTag, AngularDirectivesIndex.KEY);
                if (isApplicable(declaration)) {
                    for (XmlAttributeDescriptor binding : AngularAttributeDescriptor.getFieldBasedDescriptors((JSImplicitElement) declaration)) {
                        result.put(binding.getName(), binding);
                    }
                }
            }
        }
        final Collection<String> docDirectives = AngularIndexUtil.getAllKeys(AngularDirectivesDocIndex.KEY, project);
        for (String directiveName : docDirectives) {
            PsiElement declaration = applicableDirective(project, directiveName, xmlTag, AngularDirectivesDocIndex.KEY);
            if (isApplicable(declaration)) {
                addAttributes(project, result, directiveName, declaration);
            }
        }
        for (String directiveName : directives) {
            if (!docDirectives.contains(directiveName)) {
                PsiElement declaration = applicableDirective(project, directiveName, xmlTag, AngularDirectivesIndex.KEY);
                if (isApplicable(declaration)) {
                    addAttributes(project, result, directiveName, declaration);
                }
            }
        }
        return result.values().toArray(new XmlAttributeDescriptor[result.size()]);
    }
    return XmlAttributeDescriptor.EMPTY;
}
Also used : Project(com.intellij.openapi.project.Project) HtmlElementDescriptorImpl(com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement) LinkedHashMap(java.util.LinkedHashMap)

Example 55 with XmlAttributeDescriptor

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

the class NlPropertyItemTest method testCreateWithoutAttributeDefinition.

public void testCreateWithoutAttributeDefinition() {
    // It is an error not to specify an AttributeDefinition for normal attributes
    XmlAttributeDescriptor descriptor = getDescriptor(myTextView, ATTR_TEXT);
    assertThat(descriptor).isNotNull();
    try {
        NlPropertyItem.create(ImmutableList.of(myTextView), descriptor, null, null);
        fail("An AttributeDefinition should exist for ATTR_TEXT");
    } catch (IllegalArgumentException ex) {
        assertThat(ex.getMessage()).isEqualTo("Missing attribute definition for text");
    }
    // Style does not have an AttributeDefinition
    NlPropertyItem item = createFrom(myTextView, ATTR_STYLE);
    assertThat(item).isNotInstanceOf(NlFlagPropertyItem.class);
    assertThat(item).isNotInstanceOf(NlIdPropertyItem.class);
    assertThat(item.getName()).isEqualTo(ATTR_STYLE);
    assertThat(item.getDefinition()).isNull();
}
Also used : XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor)

Aggregations

XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)89 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)54 XmlTag (com.intellij.psi.xml.XmlTag)46 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)32 XmlAttribute (com.intellij.psi.xml.XmlAttribute)18 NotNull (org.jetbrains.annotations.NotNull)11 PsiElement (com.intellij.psi.PsiElement)10 ArrayList (java.util.ArrayList)8 XmlFile (com.intellij.psi.xml.XmlFile)7 AnnotationBackedDescriptor (com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)6 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)6 Nullable (org.jetbrains.annotations.Nullable)6 Project (com.intellij.openapi.project.Project)5 HtmlTag (com.intellij.psi.html.HtmlTag)5 NamespaceAwareXmlAttributeDescriptor (com.intellij.xml.NamespaceAwareXmlAttributeDescriptor)5 AnyXmlAttributeDescriptor (com.intellij.xml.impl.schema.AnyXmlAttributeDescriptor)5 JavaFxPropertyAttributeDescriptor (org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyAttributeDescriptor)5 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)3 PsiReference (com.intellij.psi.PsiReference)3 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)2