Search in sources :

Example 46 with XmlAttribute

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

the class AddSchemaPrefixIntention method getXmlnsDeclaration.

@Nullable
private static XmlAttribute getXmlnsDeclaration(PsiElement element) {
    final PsiElement parent = element.getParent();
    if (parent instanceof XmlTag) {
        XmlTag tag = (XmlTag) parent;
        if (tag.getNamespacePrefix().isEmpty()) {
            while (tag != null) {
                final XmlAttribute attr = tag.getAttribute("xmlns");
                if (attr != null)
                    return attr;
                tag = tag.getParentTag();
            }
        }
    } else if (parent instanceof XmlAttribute && ((XmlAttribute) parent).getName().equals("xmlns")) {
        return (XmlAttribute) parent;
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 47 with XmlAttribute

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

the class SchemaReferencesProvider method getReferencesByElement.

@Override
@NotNull
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull final ProcessingContext context) {
    final PsiElement parent = element.getParent();
    if (!(parent instanceof XmlAttribute))
        return PsiReference.EMPTY_ARRAY;
    final String attrName = ((XmlAttribute) parent).getName();
    if (VALUE_ATTR_NAME.equals(attrName)) {
        return PsiReference.EMPTY_ARRAY;
    } else if (NAME_ATTR_NAME.equals(attrName)) {
        return new PsiReference[] { new NameReference(element) };
    } else if (MEMBER_TYPES_ATTR_NAME.equals(attrName)) {
        final List<PsiReference> result = new ArrayList<>(1);
        final String text = element.getText();
        int lastIndex = 1;
        final int testLength = text.length();
        for (int i = 1; i < testLength; ++i) {
            if (Character.isWhitespace(text.charAt(i))) {
                if (lastIndex != i)
                    result.add(new TypeOrElementOrAttributeReference(element, new TextRange(lastIndex, i)));
                lastIndex = i + 1;
            }
        }
        if (lastIndex != testLength - 1)
            result.add(new TypeOrElementOrAttributeReference(element, new TextRange(lastIndex, testLength - 1)));
        return result.toArray(new PsiReference[result.size()]);
    } else {
        final PsiReference prefix = createSchemaPrefixReference(element);
        final PsiReference ref = createTypeOrElementOrAttributeReference(element, prefix == null ? null : prefix.getCanonicalText());
        return prefix == null ? new PsiReference[] { ref } : new PsiReference[] { ref, prefix };
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 48 with XmlAttribute

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

the class TypeOrElementOrAttributeReference method determineReferenceType.

@Nullable
private static ReferenceType determineReferenceType(PsiElement element) {
    final XmlAttribute attribute = PsiTreeUtil.getParentOfType(element, XmlAttribute.class);
    if (attribute == null) {
        return null;
    }
    final XmlTag tag = attribute.getParent();
    final String localName = tag.getLocalName();
    final String attributeLocalName = attribute.getLocalName();
    if (SchemaReferencesProvider.REF_ATTR_NAME.equals(attributeLocalName) || SchemaReferencesProvider.SUBSTITUTION_GROUP_ATTR_NAME.equals(attributeLocalName)) {
        if (localName.equals(SchemaReferencesProvider.GROUP_TAG_NAME)) {
            return ReferenceType.GroupReference;
        } else if (localName.equals(SchemaReferencesProvider.ATTRIBUTE_GROUP_TAG_NAME)) {
            return ReferenceType.AttributeGroupReference;
        } else if (SchemaReferencesProvider.ELEMENT_TAG_NAME.equals(localName)) {
            return ReferenceType.ElementReference;
        } else if (SchemaReferencesProvider.ATTRIBUTE_TAG_NAME.equals(localName)) {
            return ReferenceType.AttributeReference;
        }
    } else if (SchemaReferencesProvider.TYPE_ATTR_NAME.equals(attributeLocalName) || SchemaReferencesProvider.BASE_ATTR_NAME.equals(attributeLocalName) || SchemaReferencesProvider.MEMBER_TYPES_ATTR_NAME.equals(attributeLocalName) || SchemaReferencesProvider.ITEM_TYPE_ATTR_NAME.equals(attributeLocalName)) {
        return ReferenceType.TypeReference;
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 49 with XmlAttribute

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

the class XmlDuplicatedIdInspection method buildVisitor.

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

        @Override
        public void visitXmlAttributeValue(final XmlAttributeValue value) {
            if (value.getTextRange().isEmpty()) {
                return;
            }
            final PsiFile file = value.getContainingFile();
            if (!(file instanceof XmlFile)) {
                return;
            }
            PsiFile baseFile = PsiUtilCore.getTemplateLanguageFile(file);
            if (baseFile != file && !(baseFile instanceof XmlFile)) {
                return;
            }
            final XmlRefCountHolder refHolder = XmlRefCountHolder.getRefCountHolder((XmlFile) file);
            if (refHolder == null)
                return;
            final PsiElement parent = value.getParent();
            if (!(parent instanceof XmlAttribute))
                return;
            final XmlTag tag = (XmlTag) parent.getParent();
            if (tag == null)
                return;
            checkValue(value, (XmlFile) file, refHolder, tag, holder);
        }
    };
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 50 with XmlAttribute

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

the class MicrodataUtil method findScopesWithItemRef.

private static Map<String, XmlTag> findScopesWithItemRef(@Nullable final PsiFile file) {
    if (!(file instanceof XmlFile))
        return Collections.emptyMap();
    return CachedValuesManager.getCachedValue(file, new CachedValueProvider<Map<String, XmlTag>>() {

        @Nullable
        @Override
        public Result<Map<String, XmlTag>> compute() {
            final Map<String, XmlTag> result = new THashMap<>();
            file.accept(new XmlRecursiveElementVisitor() {

                @Override
                public void visitXmlTag(final XmlTag tag) {
                    super.visitXmlTag(tag);
                    XmlAttribute refAttr = tag.getAttribute(ITEM_REF);
                    if (refAttr != null && tag.getAttribute(ITEM_SCOPE) != null) {
                        getReferencesForAttributeValue(refAttr.getValueElement(), (t, v) -> {
                            result.put(t, tag);
                            return null;
                        });
                    }
                }
            });
            return Result.create(result, file);
        }
    });
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlRecursiveElementVisitor(com.intellij.psi.XmlRecursiveElementVisitor) THashMap(gnu.trove.THashMap) Nullable(org.jetbrains.annotations.Nullable) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlAttribute (com.intellij.psi.xml.XmlAttribute)220 XmlTag (com.intellij.psi.xml.XmlTag)116 PsiElement (com.intellij.psi.PsiElement)60 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)57 NotNull (org.jetbrains.annotations.NotNull)44 XmlFile (com.intellij.psi.xml.XmlFile)37 Nullable (org.jetbrains.annotations.Nullable)27 Project (com.intellij.openapi.project.Project)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)20 TextRange (com.intellij.openapi.util.TextRange)18 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)18 ArrayList (java.util.ArrayList)18 PsiFile (com.intellij.psi.PsiFile)17 PsiReference (com.intellij.psi.PsiReference)17 IncorrectOperationException (com.intellij.util.IncorrectOperationException)10 DomElement (com.intellij.util.xml.DomElement)10 Result (com.intellij.openapi.application.Result)9 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)9 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)8 XmlElement (com.intellij.psi.xml.XmlElement)6