Search in sources :

Example 1 with DElementPattern

use of org.kohsuke.rngom.digested.DElementPattern in project intellij-community by JetBrains.

the class RngDocumentationProvider method generateDoc.

@Override
@Nullable
public String generateDoc(PsiElement element, @Nullable PsiElement originalElement) {
    final XmlElement c = PsiTreeUtil.getParentOfType(originalElement, XmlTag.class, XmlAttribute.class);
    if (c != null && c.getManager() == null) {
        LOG.warn("Invalid context element passed to generateDoc()", new Throwable("<stack trace>"));
        return null;
    }
    if (c instanceof XmlTag) {
        final XmlTag xmlElement = (XmlTag) c;
        final XmlElementDescriptor descriptor = xmlElement.getDescriptor();
        if (descriptor instanceof CompositeDescriptor) {
            final StringBuilder sb = new StringBuilder();
            final CompositeDescriptor d = (CompositeDescriptor) descriptor;
            final DElementPattern[] patterns = d.getElementPatterns();
            final THashSet<PsiElement> elements = ContainerUtil.newIdentityTroveSet();
            for (DElementPattern pattern : patterns) {
                final PsiElement psiElement = d.getDeclaration(pattern.getLocation());
                if (psiElement instanceof XmlTag && elements.add(psiElement)) {
                    if (sb.length() > 0) {
                        sb.append("<hr>");
                    }
                    sb.append(getDocumentationFromTag((XmlTag) psiElement, xmlElement.getLocalName(), "Element"));
                }
            }
            return makeDocumentation(sb);
        } else if (descriptor instanceof RngElementDescriptor) {
            final RngElementDescriptor d = (RngElementDescriptor) descriptor;
            final PsiElement declaration = d.getDeclaration();
            if (declaration instanceof XmlTag) {
                return makeDocumentation(getDocumentationFromTag((XmlTag) declaration, xmlElement.getLocalName(), "Element"));
            }
        }
    } else if (c instanceof XmlAttribute) {
        final XmlAttribute attribute = (XmlAttribute) c;
        final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
        if (descriptor instanceof RngXmlAttributeDescriptor) {
            final RngXmlAttributeDescriptor d = (RngXmlAttributeDescriptor) descriptor;
            final StringBuilder sb = new StringBuilder();
            final Collection<PsiElement> declaration = ContainerUtil.newIdentityTroveSet(d.getDeclarations());
            for (PsiElement psiElement : declaration) {
                if (psiElement instanceof XmlTag) {
                    if (sb.length() > 0) {
                        sb.append("<hr>");
                    }
                    sb.append(getDocumentationFromTag((XmlTag) psiElement, d.getName(), "Attribute"));
                }
            }
            return makeDocumentation(sb);
        }
    } else if (element instanceof XmlTag) {
        return makeDocumentation(getDocumentationFromTag((XmlTag) element, ((XmlTag) element).getLocalName(), "Element"));
    }
    return null;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) RngXmlAttributeDescriptor(org.intellij.plugins.relaxNG.model.descriptors.RngXmlAttributeDescriptor) RngElementDescriptor(org.intellij.plugins.relaxNG.model.descriptors.RngElementDescriptor) RngXmlAttributeDescriptor(org.intellij.plugins.relaxNG.model.descriptors.RngXmlAttributeDescriptor) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) XmlElement(com.intellij.psi.xml.XmlElement) CompositeDescriptor(org.intellij.plugins.relaxNG.model.descriptors.CompositeDescriptor) Collection(java.util.Collection) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) DElementPattern(org.kohsuke.rngom.digested.DElementPattern) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with DElementPattern

use of org.kohsuke.rngom.digested.DElementPattern in project intellij-community by JetBrains.

the class RngNsDescriptor method findDescriptor.

private XmlElementDescriptor findDescriptor(final QName qName, List<DElementPattern> list) {
    int max = -1;
    DElementPattern maxPattern = null;
    for (DElementPattern pattern : list) {
        final NameClass nameClass = pattern.getName();
        if (nameClass.contains(qName)) {
            final int spec = nameClass.containsSpecificity(qName);
            if (spec > max) {
                maxPattern = pattern;
                max = spec;
            }
        }
    }
    final List<DElementPattern> patterns = ContainerUtil.findAll(list, pattern -> {
        final NameClass nameClass = pattern.getName();
        return nameClass.contains(qName);
    });
    if (maxPattern != null) {
        if (patterns.size() > 1) {
            return initDescriptor(new CompositeDescriptor(this, maxPattern, patterns));
        } else {
            return initDescriptor(new RngElementDescriptor(this, maxPattern));
        }
    } else {
        return null;
    }
}
Also used : NameClass(org.kohsuke.rngom.nc.NameClass) DElementPattern(org.kohsuke.rngom.digested.DElementPattern)

Example 3 with DElementPattern

use of org.kohsuke.rngom.digested.DElementPattern in project intellij-community by JetBrains.

the class RngNsDescriptor method findRootDescriptorInner.

private XmlElementDescriptor findRootDescriptorInner(XmlTag tag) {
    final List<DElementPattern> allNamedPatterns = ContainerUtil.findAll(ChildElementFinder.find(-1, myPattern), NamedPatternFilter.INSTANCE);
    XmlElementDescriptor descriptor = findDescriptor(tag, allNamedPatterns);
    return descriptor != null ? descriptor : findDescriptor(tag, ChildElementFinder.find(myPattern));
}
Also used : XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) DElementPattern(org.kohsuke.rngom.digested.DElementPattern)

Example 4 with DElementPattern

use of org.kohsuke.rngom.digested.DElementPattern in project intellij-community by JetBrains.

the class CompositeDescriptor method collectAttributeDescriptors.

@Override
protected XmlAttributeDescriptor[] collectAttributeDescriptors(@Nullable XmlTag context) {
    final QName qName = null;
    final DPattern[] patterns;
    if (qName == null) {
        patterns = myPatterns;
    } else {
        final List<DElementPattern> p = ContainerUtil.findAll(myPatterns, pattern -> pattern.getName().contains(qName));
        patterns = p.toArray(new DPattern[p.size()]);
    }
    return computeAttributeDescriptors(AttributeFinder.find(patterns));
}
Also used : QName(javax.xml.namespace.QName) DPattern(org.kohsuke.rngom.digested.DPattern) DElementPattern(org.kohsuke.rngom.digested.DElementPattern)

Example 5 with DElementPattern

use of org.kohsuke.rngom.digested.DElementPattern in project intellij-community by JetBrains.

the class CompositeDescriptor method findElementDescriptor.

@Override
protected XmlElementDescriptor findElementDescriptor(XmlTag childTag) {
    final List<DElementPattern> patterns = new ArrayList<>();
    for (DElementPattern pattern : myPatterns) {
        patterns.addAll(ChildElementFinder.find(2, pattern));
    }
    // TODO: filter out impossible variants:
    /*
      while this needs both variants of <choice>-children
      <element><choice><caret>

      this does not, because <choice> inside <choice> is unambiguous:
      <element><choice><data type="string" /><choice><caret>
     */
    final XmlElementDescriptor d = myNsDescriptor.findDescriptor(childTag, patterns);
    if (d != null) {
        return d;
    }
    return NULL;
}
Also used : ArrayList(java.util.ArrayList) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) DElementPattern(org.kohsuke.rngom.digested.DElementPattern)

Aggregations

DElementPattern (org.kohsuke.rngom.digested.DElementPattern)7 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)5 AnyXmlElementDescriptor (com.intellij.xml.impl.schema.AnyXmlElementDescriptor)2 ArrayList (java.util.ArrayList)2 QName (javax.xml.namespace.QName)2 PsiElement (com.intellij.psi.PsiElement)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlElement (com.intellij.psi.xml.XmlElement)1 XmlTag (com.intellij.psi.xml.XmlTag)1 HashMap (com.intellij.util.containers.HashMap)1 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)1 Collection (java.util.Collection)1 CompositeDescriptor (org.intellij.plugins.relaxNG.model.descriptors.CompositeDescriptor)1 RngElementDescriptor (org.intellij.plugins.relaxNG.model.descriptors.RngElementDescriptor)1 RngXmlAttributeDescriptor (org.intellij.plugins.relaxNG.model.descriptors.RngXmlAttributeDescriptor)1 Nullable (org.jetbrains.annotations.Nullable)1 DPattern (org.kohsuke.rngom.digested.DPattern)1 NameClass (org.kohsuke.rngom.nc.NameClass)1