Search in sources :

Example 1 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class XmlUnusedNamespaceInspection method getLocationReferences.

private static PsiReference[] getLocationReferences(String namespace, XmlTag tag) {
    XmlAttribute locationAttr = tag.getAttribute(XmlUtil.SCHEMA_LOCATION_ATT, XmlUtil.XML_SCHEMA_INSTANCE_URI);
    if (locationAttr == null) {
        return PsiReference.EMPTY_ARRAY;
    }
    XmlAttributeValue value = locationAttr.getValueElement();
    return value == null ? PsiReference.EMPTY_ARRAY : getLocationReferences(namespace, value);
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue)

Example 2 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class XmlUnusedNamespaceInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new XmlElementVisitor() {

        @Override
        public void visitXmlAttribute(XmlAttribute attribute) {
            PsiFile file = holder.getFile();
            if (!(file instanceof XmlFile))
                return;
            XmlRefCountHolder refCountHolder = XmlRefCountHolder.getRefCountHolder((XmlFile) file);
            if (refCountHolder == null)
                return;
            if (!attribute.isNamespaceDeclaration()) {
                checkUnusedLocations(attribute, holder, refCountHolder);
                return;
            }
            String namespace = attribute.getValue();
            String declaredPrefix = getDeclaredPrefix(attribute);
            if (namespace != null && !refCountHolder.isInUse(declaredPrefix)) {
                ImplicitUsageProvider[] implicitUsageProviders = Extensions.getExtensions(ImplicitUsageProvider.EP_NAME);
                for (ImplicitUsageProvider provider : implicitUsageProviders) {
                    if (provider.isImplicitUsage(attribute))
                        return;
                }
                XmlAttributeValue value = attribute.getValueElement();
                assert value != null;
                holder.registerProblem(attribute, XmlBundle.message("xml.inspections.unused.schema.declaration"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceDeclarationFix(declaredPrefix, false, !refCountHolder.isUsedNamespace(namespace)));
                XmlTag parent = attribute.getParent();
                if (declaredPrefix.isEmpty()) {
                    XmlAttribute location = getDefaultLocation(parent);
                    if (location != null) {
                        holder.registerProblem(location, XmlBundle.message("xml.inspections.unused.schema.location"), ProblemHighlightType.LIKE_UNUSED_SYMBOL, new RemoveNamespaceDeclarationFix(declaredPrefix, true, true));
                    }
                } else if (!refCountHolder.isUsedNamespace(namespace)) {
                    for (PsiReference reference : getLocationReferences(namespace, parent)) {
                        if (!XmlHighlightVisitor.hasBadResolve(reference, false))
                            holder.registerProblemForReference(reference, ProblemHighlightType.LIKE_UNUSED_SYMBOL, XmlBundle.message("xml.inspections.unused.schema.location"), new RemoveNamespaceDeclarationFix(declaredPrefix, true, true));
                    }
                }
            }
        }
    };
}
Also used : XmlRefCountHolder(com.intellij.xml.util.XmlRefCountHolder) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class IdReferenceProvider method getReferencesByElement.

@Override
@NotNull
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) {
    if (element instanceof XmlAttributeValue) {
        final XmlExtension extension = XmlExtension.getExtensionByElement(element);
        if (extension != null && extension.hasDynamicComponents(element)) {
            return PsiReference.EMPTY_ARRAY;
        }
        final PsiElement parentElement = element.getParent();
        if (!(parentElement instanceof XmlAttribute))
            return PsiReference.EMPTY_ARRAY;
        final String name = ((XmlAttribute) parentElement).getName();
        final String ns = ((XmlAttribute) parentElement).getParent().getNamespace();
        final boolean jsfNs = Arrays.asList(XmlUtil.JSF_CORE_URIS).contains(ns) || Arrays.asList(XmlUtil.JSF_HTML_URIS).contains(ns);
        if (FOR_ATTR_NAME.equals(name)) {
            return new PsiReference[] { jsfNs && element.getText().indexOf(':') == -1 ? new IdRefReference(element) : new IdRefReference(element) {

                @Override
                public boolean isSoft() {
                    final XmlAttributeDescriptor descriptor = ((XmlAttribute) parentElement).getDescriptor();
                    return descriptor != null && !descriptor.hasIdRefType();
                }
            } };
        } else {
            final boolean allowReferences = !ourNamespacesWithoutNameReference.contains(ns);
            if (ID_ATTR_NAME.equals(name) && allowReferences || STYLE_ID_ATTR_NAME.equals(name) || NAME_ATTR_NAME.equals(name) && allowReferences) {
                final AttributeValueSelfReference attributeValueSelfReference;
                if (jsfNs) {
                    attributeValueSelfReference = new AttributeValueSelfReference(element);
                } else {
                    if (hasOuterLanguageElement(element))
                        return PsiReference.EMPTY_ARRAY;
                    attributeValueSelfReference = new GlobalAttributeValueSelfReference(element, true);
                }
                return new PsiReference[] { attributeValueSelfReference };
            }
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : XmlExtension(com.intellij.xml.XmlExtension) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class DomCompletionContributor method domKnowsBetter.

private boolean domKnowsBetter(final CompletionParameters parameters, final CompletionResultSet result) {
    final XmlAttributeValue element = PsiTreeUtil.getParentOfType(parameters.getPosition(), XmlAttributeValue.class);
    if (element == null) {
        return false;
    }
    if (isSchemaEnumerated(element)) {
        return false;
    }
    final PsiElement parent = element.getParent();
    if (parent instanceof XmlAttribute) {
        XmlAttributeDescriptor descriptor = ((XmlAttribute) parent).getDescriptor();
        if (descriptor != null && descriptor.getDefaultValue() != null) {
            final PsiReference[] references = myProvider.getReferencesByElement(element, new ProcessingContext());
            if (references.length > 0) {
                return LegacyCompletionContributor.completeReference(parameters, result);
            }
        }
    }
    return false;
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement)

Example 5 with XmlAttributeValue

use of com.intellij.psi.xml.XmlAttributeValue in project intellij-community by JetBrains.

the class GenericDomValueReference method createTextRange.

protected TextRange createTextRange() {
    if (myGenericValue instanceof GenericAttributeValue) {
        final GenericAttributeValue genericAttributeValue = (GenericAttributeValue) myGenericValue;
        final XmlAttributeValue attributeValue = genericAttributeValue.getXmlAttributeValue();
        if (attributeValue == null) {
            return TextRange.from(0, genericAttributeValue.getXmlAttribute().getTextLength());
        }
        final int length = attributeValue.getTextLength();
        return length < 2 ? TextRange.from(0, length) : new TextRange(1, length - 1);
    }
    final XmlTag tag = myGenericValue.getXmlTag();
    assert tag != null;
    return XmlTagUtil.getTrimmedValueRange(tag);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) ResolvingHint(com.intellij.psi.ResolvingHint) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)127 XmlAttribute (com.intellij.psi.xml.XmlAttribute)57 XmlTag (com.intellij.psi.xml.XmlTag)50 NotNull (org.jetbrains.annotations.NotNull)38 PsiElement (com.intellij.psi.PsiElement)31 Nullable (org.jetbrains.annotations.Nullable)24 PsiReference (com.intellij.psi.PsiReference)20 XmlFile (com.intellij.psi.xml.XmlFile)19 TextRange (com.intellij.openapi.util.TextRange)18 VirtualFile (com.intellij.openapi.vfs.VirtualFile)15 ArrayList (java.util.ArrayList)13 Project (com.intellij.openapi.project.Project)12 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)10 DomElement (com.intellij.util.xml.DomElement)9 PsiFile (com.intellij.psi.PsiFile)8 LazyValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.LazyValueResourceElementWrapper)7 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)5 Module (com.intellij.openapi.module.Module)5 ValueResourceElementWrapper (org.jetbrains.android.dom.wrappers.ValueResourceElementWrapper)5