Search in sources :

Example 1 with XmlElementDescriptorImpl

use of com.intellij.xml.impl.schema.XmlElementDescriptorImpl in project intellij-community by JetBrains.

the class XmlUtil method getSchemaSimpleContent.

@Nullable
public static XmlTag getSchemaSimpleContent(@NotNull XmlTag tag) {
    XmlElementDescriptor descriptor = tag.getDescriptor();
    if (descriptor instanceof XmlElementDescriptorImpl) {
        final TypeDescriptor type = ((XmlElementDescriptorImpl) descriptor).getType(tag);
        if (type instanceof ComplexTypeDescriptor) {
            final XmlTag[] simpleContent = new XmlTag[1];
            processXmlElements(((ComplexTypeDescriptor) type).getDeclaration(), new PsiElementProcessor() {

                @Override
                public boolean execute(@NotNull final PsiElement element) {
                    if (element instanceof XmlTag) {
                        final XmlTag tag = (XmlTag) element;
                        @NonNls final String s = ((XmlTag) element).getLocalName();
                        if ((s.equals(XSD_SIMPLE_CONTENT_TAG) || s.equals("restriction") && "string".equals(findLocalNameByQualifiedName(tag.getAttributeValue("base")))) && tag.getNamespace().equals(XML_SCHEMA_URI)) {
                            simpleContent[0] = tag;
                            return false;
                        }
                    }
                    return true;
                }
            }, true);
            return simpleContent[0];
        }
    }
    return null;
}
Also used : ComplexTypeDescriptor(com.intellij.xml.impl.schema.ComplexTypeDescriptor) TypeDescriptor(com.intellij.xml.impl.schema.TypeDescriptor) ComplexTypeDescriptor(com.intellij.xml.impl.schema.ComplexTypeDescriptor) XmlElementDescriptorImpl(com.intellij.xml.impl.schema.XmlElementDescriptorImpl) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with XmlElementDescriptorImpl

use of com.intellij.xml.impl.schema.XmlElementDescriptorImpl in project intellij-community by JetBrains.

the class TagNameVariantCollector method getTagDescriptors.

public static List<XmlElementDescriptor> getTagDescriptors(final XmlTag element, final Collection<String> namespaces, @Nullable List<String> nsInfo) {
    XmlElementDescriptor elementDescriptor = null;
    String elementNamespace = element.getNamespacePrefix().isEmpty() ? null : element.getNamespace();
    final Map<String, XmlElementDescriptor> descriptorsMap = new HashMap<>();
    PsiElement context = element.getParent();
    PsiElement curElement = element.getParent();
    while (curElement instanceof XmlTag) {
        final XmlTag declarationTag = (XmlTag) curElement;
        final String namespace = declarationTag.getNamespace();
        if (!descriptorsMap.containsKey(namespace)) {
            final XmlElementDescriptor descriptor = declarationTag.getDescriptor();
            if (descriptor != null) {
                descriptorsMap.put(namespace, descriptor);
                if (elementDescriptor == null) {
                    elementDescriptor = descriptor;
                    if (elementNamespace == null) {
                        elementNamespace = namespace;
                    }
                }
            }
        }
        curElement = curElement.getContext();
    }
    final Set<XmlNSDescriptor> visited = new HashSet<>();
    final XmlExtension extension = XmlExtension.getExtension(element.getContainingFile());
    final ArrayList<XmlElementDescriptor> variants = new ArrayList<>();
    for (final String namespace : namespaces) {
        final int initialSize = variants.size();
        processVariantsInNamespace(namespace, element, variants, elementDescriptor, elementNamespace, descriptorsMap, visited, context instanceof XmlTag ? (XmlTag) context : element, extension);
        if (nsInfo != null) {
            for (int i = initialSize; i < variants.size(); i++) {
                XmlElementDescriptor descriptor = variants.get(i);
                nsInfo.add(descriptor instanceof XmlElementDescriptorImpl && !(descriptor instanceof RelaxedHtmlFromSchemaElementDescriptor) ? ((XmlElementDescriptorImpl) descriptor).getNamespaceByContext(element) : namespace);
            }
        }
    }
    final boolean hasPrefix = StringUtil.isNotEmpty(element.getNamespacePrefix());
    return ContainerUtil.filter(variants, descriptor -> {
        if (descriptor instanceof AnyXmlElementDescriptor) {
            return false;
        } else if (hasPrefix && descriptor instanceof XmlElementDescriptorImpl && !namespaces.contains(((XmlElementDescriptorImpl) descriptor).getNamespace())) {
            return false;
        }
        return true;
    });
}
Also used : AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) XmlExtension(com.intellij.xml.XmlExtension) RelaxedHtmlFromSchemaElementDescriptor(com.intellij.html.impl.RelaxedHtmlFromSchemaElementDescriptor) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) XmlElementDescriptorImpl(com.intellij.xml.impl.schema.XmlElementDescriptorImpl) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 3 with XmlElementDescriptorImpl

use of com.intellij.xml.impl.schema.XmlElementDescriptorImpl in project intellij-community by JetBrains.

the class XsltContextProviderBase method processElementDescriptors.

private static void processElementDescriptors(XmlElementDescriptor descriptor, XmlTag tag, ElementNames names, Set<XmlElementDescriptor> history, int depth) throws StopProcessingException {
    if (!history.add(descriptor) || ++depth == 200) {
        if (depth == 200) {
            throw new StopProcessingException();
        }
        return;
    }
    final String namespace = descriptor instanceof XmlElementDescriptorImpl ? ((XmlElementDescriptorImpl) descriptor).getNamespace() : tag.getNamespace();
    names.elementNames.add(new QName(namespace, descriptor.getName()));
    final XmlAttributeDescriptor[] attributesDescriptors = descriptor.getAttributesDescriptors(null);
    for (XmlAttributeDescriptor attributesDescriptor : attributesDescriptors) {
        final String localPart = attributesDescriptor.getName();
        if (!"xmlns".equals(localPart))
            names.attributeNames.add(new QName(localPart));
    }
    final XmlElementDescriptor[] descriptors = descriptor.getElementsDescriptors(tag);
    for (XmlElementDescriptor elem : descriptors) {
        processElementDescriptors(elem, tag, names, history, depth);
    }
}
Also used : XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) QName(javax.xml.namespace.QName) XmlElementDescriptorImpl(com.intellij.xml.impl.schema.XmlElementDescriptorImpl) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor)

Example 4 with XmlElementDescriptorImpl

use of com.intellij.xml.impl.schema.XmlElementDescriptorImpl in project intellij-community by JetBrains.

the class XmlHighlightingTest method testAnyAttributeNavigation.

public void testAnyAttributeNavigation() throws Exception {
    configureByFiles(null, getVirtualFile(BASE_PATH + "AnyAttributeNavigation/test.xml"), getVirtualFile(BASE_PATH + "AnyAttributeNavigation/test.xsd"), getVirtualFile(BASE_PATH + "AnyAttributeNavigation/library.xsd"));
    PsiReference at = getFile().findReferenceAt(getEditor().getCaretModel().getOffset());
    XmlTag tag = PsiTreeUtil.getParentOfType(at.getElement(), XmlTag.class);
    XmlElementDescriptorImpl descriptor = (XmlElementDescriptorImpl) tag.getDescriptor();
    XmlAttributeDescriptor[] descriptors = descriptor.getAttributesDescriptors(tag);
    System.out.println(Arrays.asList(descriptors));
    doDoTest(true, false);
    PsiElement resolve = at.resolve();
    assertTrue(resolve instanceof XmlTag);
}
Also used : XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) XmlElementDescriptorImpl(com.intellij.xml.impl.schema.XmlElementDescriptorImpl)

Example 5 with XmlElementDescriptorImpl

use of com.intellij.xml.impl.schema.XmlElementDescriptorImpl in project intellij-community by JetBrains.

the class XmlTagInsertHandler method addRequiredSubTags.

private static boolean addRequiredSubTags(Template template, XmlElementDescriptor descriptor, PsiFile file, XmlTag context) {
    if (!WebEditorOptions.getInstance().isAutomaticallyInsertRequiredSubTags())
        return false;
    List<XmlElementDescriptor> requiredSubTags = GenerateXmlTagAction.getRequiredSubTags(descriptor);
    if (!requiredSubTags.isEmpty()) {
        template.addTextSegment(">");
        template.setToReformat(true);
    }
    for (XmlElementDescriptor subTag : requiredSubTags) {
        if (subTag == null) {
            // placeholder for smart completion
            template.addTextSegment("<");
            template.addVariable(new MacroCallNode(new CompleteSmartMacro()), true);
            continue;
        }
        String qname = subTag.getName();
        if (subTag instanceof XmlElementDescriptorImpl) {
            String prefixByNamespace = context.getPrefixByNamespace(((XmlElementDescriptorImpl) subTag).getNamespace());
            if (StringUtil.isNotEmpty(prefixByNamespace)) {
                qname = prefixByNamespace + ":" + subTag.getName();
            }
        }
        template.addTextSegment("<" + qname);
        addRequiredAttributes(subTag, null, template, file);
        completeTagTail(template, subTag, file, context, false);
    }
    if (!requiredSubTags.isEmpty()) {
        addTagEnd(template, descriptor, context);
    }
    return !requiredSubTags.isEmpty();
}
Also used : MacroCallNode(com.intellij.codeInsight.template.impl.MacroCallNode) XmlElementDescriptorImpl(com.intellij.xml.impl.schema.XmlElementDescriptorImpl) CompleteSmartMacro(com.intellij.codeInsight.template.macro.CompleteSmartMacro)

Aggregations

XmlElementDescriptorImpl (com.intellij.xml.impl.schema.XmlElementDescriptorImpl)6 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)3 XmlTag (com.intellij.psi.xml.XmlTag)2 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)2 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)2 MacroCallNode (com.intellij.codeInsight.template.impl.MacroCallNode)1 CompleteSmartMacro (com.intellij.codeInsight.template.macro.CompleteSmartMacro)1 RelaxedHtmlFromSchemaElementDescriptor (com.intellij.html.impl.RelaxedHtmlFromSchemaElementDescriptor)1 PsiElement (com.intellij.psi.PsiElement)1 PsiElementProcessor (com.intellij.psi.search.PsiElementProcessor)1 XmlExtension (com.intellij.xml.XmlExtension)1 AnyXmlElementDescriptor (com.intellij.xml.impl.schema.AnyXmlElementDescriptor)1 ComplexTypeDescriptor (com.intellij.xml.impl.schema.ComplexTypeDescriptor)1 TypeDescriptor (com.intellij.xml.impl.schema.TypeDescriptor)1 QName (javax.xml.namespace.QName)1 Nullable (org.jetbrains.annotations.Nullable)1