Search in sources :

Example 11 with XmlDocument

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

the class XmlNSDescriptorImpl method doFindIn.

private TypeDescriptor doFindIn(final XmlTag[] tags, final String name, final String namespace, final Pair<QNameKey, XmlTag> pair, final XmlTag rootTag) {
    for (final XmlTag tag : tags) {
        if (equalsToSchemaName(tag, "complexType")) {
            if (name == null) {
                CachedValue<TypeDescriptor> value = createAndPutTypesCachedValue(tag, pair);
                return value.getValue();
            }
            String nameAttribute = tag.getAttributeValue("name");
            if (isSameName(name, namespace, nameAttribute)) {
                CachedValue<TypeDescriptor> cachedValue = createAndPutTypesCachedValue(tag, pair);
                return cachedValue.getValue();
            }
        } else if (equalsToSchemaName(tag, "simpleType")) {
            if (name == null) {
                CachedValue<TypeDescriptor> value = createAndPutTypesCachedValueSimpleType(tag, pair);
                return value.getValue();
            }
            String nameAttribute = tag.getAttributeValue("name");
            if (isSameName(name, namespace, nameAttribute)) {
                CachedValue<TypeDescriptor> cachedValue = createAndPutTypesCachedValue(tag, pair);
                return cachedValue.getValue();
            }
        } else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (equalsToSchemaName(tag, IMPORT_TAG_NAME) && (namespace == null || !namespace.equals(getDefaultNamespace())))) {
            final String schemaLocation = tag.getAttributeValue("schemaLocation");
            if (schemaLocation != null) {
                final XmlFile xmlFile = XmlUtil.findNamespace(rootTag.getContainingFile(), schemaLocation);
                if (xmlFile != null) {
                    final XmlDocument document = xmlFile.getDocument();
                    if (document != null) {
                        final CachedValue<TypeDescriptor> value = CachedValuesManager.getManager(tag.getProject()).createCachedValue(() -> {
                            final String currentName = tag.getAttributeValue("name");
                            if ((currentName != null && !currentName.equals(XmlUtil.findLocalNameByQualifiedName(name))) || !xmlFile.isValid() || xmlFile.getDocument() == null) {
                                myTypesMap.remove(pair);
                                return new CachedValueProvider.Result<>(null, PsiModificationTracker.MODIFICATION_COUNT);
                            }
                            final XmlDocument document1 = xmlFile.getDocument();
                            final XmlNSDescriptorImpl nsDescriptor = findNSDescriptor(tag, document1);
                            if (nsDescriptor == null) {
                                myTypesMap.remove(pair);
                                return new CachedValueProvider.Result<>(null, PsiModificationTracker.MODIFICATION_COUNT);
                            }
                            final XmlTag rTag = document1.getRootTag();
                            final TypeDescriptor complexTypeDescriptor = nsDescriptor.findTypeDescriptorImpl(rTag, name, namespace);
                            return new CachedValueProvider.Result<>(complexTypeDescriptor, rTag);
                        }, false);
                        if (value.getValue() != null) {
                            myTypesMap.put(pair, value);
                            return value.getValue();
                        }
                    }
                }
            }
        } else if (equalsToSchemaName(tag, REDEFINE_TAG_NAME)) {
            final XmlTag[] subTags = tag.getSubTags();
            TypeDescriptor descriptor = doFindIn(subTags, name, namespace, pair, rootTag);
            if (descriptor != null)
                return descriptor;
            final XmlNSDescriptorImpl nsDescriptor = getRedefinedElementDescriptor(tag);
            if (nsDescriptor != null) {
                final XmlTag redefinedRootTag = ((XmlDocument) nsDescriptor.getDeclaration()).getRootTag();
                descriptor = doFindIn(redefinedRootTag.getSubTags(), name, namespace, pair, redefinedRootTag);
                if (descriptor != null)
                    return descriptor;
            }
        }
    }
    return null;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument) CachedValue(com.intellij.psi.util.CachedValue) XmlTag(com.intellij.psi.xml.XmlTag)

Example 12 with XmlDocument

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

the class XmlNSDescriptorImpl method getAttributeImpl.

@Nullable
private XmlAttributeDescriptor getAttributeImpl(String localName, String namespace, @Nullable Set<XmlTag> visited) {
    if (myTag == null)
        return null;
    XmlNSDescriptor nsDescriptor = myTag.getNSDescriptor(namespace, true);
    if (nsDescriptor != this && nsDescriptor instanceof XmlNSDescriptorImpl) {
        return ((XmlNSDescriptorImpl) nsDescriptor).getAttributeImpl(localName, namespace, visited);
    }
    if (visited == null)
        visited = new HashSet<>(1);
    else if (visited.contains(myTag))
        return null;
    visited.add(myTag);
    XmlTag[] tags = myTag.getSubTags();
    for (XmlTag tag : tags) {
        if (equalsToSchemaName(tag, ATTRIBUTE_TAG_NAME)) {
            String name = tag.getAttributeValue("name");
            if (name != null) {
                if (checkElementNameEquivalence(localName, namespace, name, tag)) {
                    return createAttributeDescriptor(tag);
                }
            }
        } else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (equalsToSchemaName(tag, IMPORT_TAG_NAME) && namespace.equals(tag.getAttributeValue("namespace")))) {
            final String schemaLocation = tag.getAttributeValue("schemaLocation");
            if (schemaLocation != null) {
                final XmlFile xmlFile = XmlUtil.findNamespace(myTag.getContainingFile(), schemaLocation);
                if (xmlFile != null) {
                    final XmlDocument includedDocument = xmlFile.getDocument();
                    if (includedDocument != null) {
                        final PsiMetaData data = includedDocument.getMetaData();
                        if (data instanceof XmlNSDescriptorImpl) {
                            final XmlAttributeDescriptor attributeDescriptor = ((XmlNSDescriptorImpl) data).getAttributeImpl(localName, namespace, visited);
                            if (attributeDescriptor != null) {
                                final CachedValue<XmlAttributeDescriptor> value = CachedValuesManager.getManager(includedDocument.getProject()).createCachedValue(() -> {
                                    Object[] deps = attributeDescriptor.getDependences();
                                    if (deps.length == 0) {
                                        LOG.error(attributeDescriptor + " (" + attributeDescriptor.getClass() + ") returned no dependencies");
                                    }
                                    return new CachedValueProvider.Result<>(attributeDescriptor, deps);
                                }, false);
                                return value.getValue();
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : CachedValueProvider(com.intellij.psi.util.CachedValueProvider) XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument) CachedValue(com.intellij.psi.util.CachedValue) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) PsiMetaData(com.intellij.psi.meta.PsiMetaData) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) THashSet(gnu.trove.THashSet) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with XmlDocument

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

the class HtmlTextCompletionConfidence method shouldSkipAutopopupInHtml.

public static boolean shouldSkipAutopopupInHtml(@NotNull PsiElement contextElement, int offset) {
    ASTNode node = contextElement.getNode();
    if (node != null && node.getElementType() == XmlTokenType.XML_DATA_CHARACTERS) {
        PsiElement parent = contextElement.getParent();
        if (parent instanceof XmlText || parent instanceof XmlDocument) {
            String contextElementText = contextElement.getText();
            int endOffset = offset - contextElement.getTextRange().getStartOffset();
            String prefix = contextElementText.substring(0, Math.min(contextElementText.length(), endOffset));
            return !StringUtil.startsWithChar(prefix, '<') && !StringUtil.startsWithChar(prefix, '&');
        }
    }
    return false;
}
Also used : ASTNode(com.intellij.lang.ASTNode) XmlText(com.intellij.psi.xml.XmlText) XmlDocument(com.intellij.psi.xml.XmlDocument) PsiElement(com.intellij.psi.PsiElement)

Example 14 with XmlDocument

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

the class XmlCharFilter method isWithinTag.

public static boolean isWithinTag(Lookup lookup) {
    if (isInXmlContext(lookup)) {
        PsiElement psiElement = lookup.getPsiElement();
        final PsiElement parentElement = psiElement != null ? psiElement.getParent() : null;
        if (parentElement instanceof XmlTag)
            return true;
        if (parentElement instanceof PsiErrorElement && parentElement.getParent() instanceof XmlDocument)
            return true;
        return (parentElement instanceof XmlDocument || parentElement instanceof XmlText) && (psiElement.textMatches("<") || psiElement.textMatches("\""));
    }
    return false;
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlText(com.intellij.psi.xml.XmlText) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 15 with XmlDocument

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

the class DtdResolveUtil method getNsDescriptor.

@Nullable
static XmlNSDescriptor getNsDescriptor(XmlElement element) {
    final XmlElement parentThatProvidesMetaData = PsiTreeUtil.getParentOfType(CompletionUtilCoreImpl.getOriginalElement(element), XmlDocument.class, XmlMarkupDecl.class);
    if (parentThatProvidesMetaData instanceof XmlDocument) {
        final XmlDocument document = (XmlDocument) parentThatProvidesMetaData;
        XmlNSDescriptor rootTagNSDescriptor = document.getRootTagNSDescriptor();
        if (rootTagNSDescriptor == null)
            rootTagNSDescriptor = (XmlNSDescriptor) document.getMetaData();
        return rootTagNSDescriptor;
    } else if (parentThatProvidesMetaData instanceof XmlMarkupDecl) {
        final XmlMarkupDecl markupDecl = (XmlMarkupDecl) parentThatProvidesMetaData;
        final PsiMetaData psiMetaData = markupDecl.getMetaData();
        if (psiMetaData instanceof XmlNSDescriptor) {
            return (XmlNSDescriptor) psiMetaData;
        }
    }
    return null;
}
Also used : XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) PsiMetaData(com.intellij.psi.meta.PsiMetaData) XmlElement(com.intellij.psi.xml.XmlElement) XmlMarkupDecl(com.intellij.psi.xml.XmlMarkupDecl) XmlDocument(com.intellij.psi.xml.XmlDocument) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlDocument (com.intellij.psi.xml.XmlDocument)57 XmlTag (com.intellij.psi.xml.XmlTag)39 XmlFile (com.intellij.psi.xml.XmlFile)32 PsiElement (com.intellij.psi.PsiElement)13 Nullable (org.jetbrains.annotations.Nullable)11 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)9 PsiFile (com.intellij.psi.PsiFile)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 ArrayList (java.util.ArrayList)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)5 PsiMetaData (com.intellij.psi.meta.PsiMetaData)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 NotNull (org.jetbrains.annotations.NotNull)4 Project (com.intellij.openapi.project.Project)3 XmlText (com.intellij.psi.xml.XmlText)3 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)3 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 Editor (com.intellij.openapi.editor.Editor)2 PsiErrorElement (com.intellij.psi.PsiErrorElement)2 FilterElementProcessor (com.intellij.psi.scope.processor.FilterElementProcessor)2