Search in sources :

Example 1 with JSAttributeImpl

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

the class FlexAttributeReferenceProvider method valueRefs.

public static PsiReference[] valueRefs(JSAttributeNameValuePairImpl element, String name) {
    final JSAttributeImpl jsAttribute = (JSAttributeImpl) element.getParent();
    final XmlElementDescriptor descriptor = jsAttribute.getBackedDescriptor();
    final XmlAttributeDescriptor attributeDescriptor = descriptor == null ? null : descriptor.getAttributeDescriptor(StringUtil.notNullize(name, JSAttributeNameValuePair.DEFAULT), null);
    if (name == null) {
        return getDefaultPropertyRefs(element, attributeDescriptor);
    }
    final String baseClassFqns = attributeDescriptor == null ? null : attributeDescriptor.getDefaultValue();
    if (baseClassFqns != null) {
        return getClassRefs(element, baseClassFqns);
    } else if ("source".equals(name)) {
        return getPathRefsCheckingParent(element);
    } else if ("key".equals(name)) {
        return getPropertyRefsCheckingParent(element);
    } else if (BUNDLE_ATTR_NAME.equals(name)) {
        return getBundleRefsCheckingParent(element);
    } else if (attributeDescriptor != null && attributeDescriptor.isEnumerated()) {
        final String[] enumeratedValues = attributeDescriptor.getEnumeratedValues();
        final TextRange range = getValueRange(element);
        if (enumeratedValues != null && enumeratedValues.length > 0 && range != null) {
            return new PsiReference[] { new EnumeratedAttributeValueReference(element, range, enumeratedValues) };
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : JSAttributeImpl(com.intellij.lang.javascript.psi.ecmal4.impl.JSAttributeImpl) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor)

Example 2 with JSAttributeImpl

use of com.intellij.lang.javascript.psi.ecmal4.impl.JSAttributeImpl 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)

Aggregations

JSAttributeImpl (com.intellij.lang.javascript.psi.ecmal4.impl.JSAttributeImpl)2 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)2 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)2 TextRange (com.intellij.openapi.util.TextRange)1 PsiReference (com.intellij.psi.PsiReference)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1