Search in sources :

Example 1 with DomCollectionChildDescription

use of com.intellij.util.xml.reflect.DomCollectionChildDescription in project intellij-community by JetBrains.

the class DefaultGenerateElementProvider method generate.

@SuppressWarnings({ "unchecked" })
@Nullable
public T generate(@Nullable final DomElement parent, final Editor editor) {
    if (parent == null) {
        return null;
    }
    final List<? extends DomCollectionChildDescription> list = parent.getGenericInfo().getCollectionChildrenDescriptions();
    for (DomCollectionChildDescription childDescription : list) {
        if (ReflectionUtil.getRawType(childDescription.getType()).isAssignableFrom(myChildElementClass)) {
            XmlTag parentTag = parent.getXmlTag();
            if (editor != null && parentTag != null) {
                int offset = editor.getCaretModel().getOffset();
                PsiFile file = parentTag.getContainingFile();
                PsiElement psiElement = file.findElementAt(offset);
                if (psiElement instanceof PsiWhiteSpace && PsiTreeUtil.isAncestor(parentTag, psiElement, false)) {
                    Document document = editor.getDocument();
                    XmlTag childTag = parentTag.createChildTag(childDescription.getXmlElementName(), null, null, true);
                    String text = childTag.getText();
                    document.insertString(offset, text);
                    Project project = editor.getProject();
                    assert project != null;
                    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
                    documentManager.commitDocument(document);
                    PsiElement element = file.findElementAt(offset + 1);
                    T domElement = DomUtil.findDomElement(element, myChildElementClass);
                    if (domElement != null)
                        return domElement;
                    document.deleteString(offset, offset + text.length());
                    documentManager.commitDocument(document);
                }
            }
            int index = getCollectionIndex(parent, childDescription, editor);
            return index < 0 ? (T) childDescription.addValue(parent, myChildElementClass) : (T) childDescription.addValue(parent, myChildElementClass, index);
        }
    }
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with DomCollectionChildDescription

use of com.intellij.util.xml.reflect.DomCollectionChildDescription in project intellij-community by JetBrains.

the class BasicDomElementComponent method bindProperties.

protected final void bindProperties(final DomElement domElement) {
    if (domElement == null)
        return;
    DomElementAnnotationsManager.getInstance(domElement.getManager().getProject()).addHighlightingListener(new DomElementAnnotationsManager.DomHighlightingListener() {

        @Override
        public void highlightingFinished(@NotNull final DomFileElement element) {
            ApplicationManager.getApplication().invokeLater(() -> {
                if (getComponent().isShowing() && element.isValid()) {
                    updateHighlighting();
                }
            });
        }
    }, this);
    for (final AbstractDomChildrenDescription description : domElement.getGenericInfo().getChildrenDescriptions()) {
        final JComponent boundComponent = getBoundComponent(description);
        if (boundComponent != null) {
            if (description instanceof DomFixedChildDescription && DomUtil.isGenericValueType(description.getType())) {
                if ((description.getValues(domElement)).size() == 1) {
                    final GenericDomValue element = domElement.getManager().createStableValue(() -> domElement.isValid() ? (GenericDomValue) description.getValues(domElement).get(0) : null);
                    doBind(DomUIFactory.createControl(element, commitOnEveryChange(element)), boundComponent);
                } else {
                //todo not bound
                }
            } else if (description instanceof DomCollectionChildDescription) {
                doBind(DomUIFactory.getDomUIFactory().createCollectionControl(domElement, (DomCollectionChildDescription) description), boundComponent);
            }
        }
    }
    reset();
}
Also used : DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) DomFileElement(com.intellij.util.xml.DomFileElement) AbstractDomChildrenDescription(com.intellij.util.xml.reflect.AbstractDomChildrenDescription) DomFixedChildDescription(com.intellij.util.xml.reflect.DomFixedChildDescription) DomElementAnnotationsManager(com.intellij.util.xml.highlighting.DomElementAnnotationsManager) GenericDomValue(com.intellij.util.xml.GenericDomValue)

Example 3 with DomCollectionChildDescription

use of com.intellij.util.xml.reflect.DomCollectionChildDescription in project intellij-community by JetBrains.

the class DomBasicsTest method testChildrenReflection.

public void testChildrenReflection() throws Throwable {
    final MyElement element = createElement("<a><child/><child-element/><child-element/></a>");
    final DomGenericInfo info = element.getGenericInfo();
    final DomFixedChildDescription foo = info.getFixedChildDescription("foo");
    assertFixedChildDescription(foo, element.getFoo(), "foo");
    final DomFixedChildDescription child = info.getFixedChildDescription("child");
    assertFixedChildDescription(child, element.getChild(), "child");
    final DomFixedChildDescription genericChild = info.getFixedChildDescription("generic-value");
    assertGenericChildDescription(genericChild, element.getGenericValue(), "generic-value");
    final DomCollectionChildDescription collectionChild = info.getCollectionChildDescription("child-element");
    assertEquals(element.getChildElements(), collectionChild.getValues(element));
    assertEquals("child-element", collectionChild.getXmlElementName());
    assertEquals(MyElement.class, collectionChild.getType());
    assertEquals(MyElement.class.getMethod("getChildElements"), collectionChild.getGetterMethod().getMethod());
    assertEquals(new HashSet(Arrays.asList(foo, child, collectionChild, genericChild, info.getAttributeChildrenDescriptions().get(0))), new HashSet(info.getChildrenDescriptions()));
}
Also used : DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) DomGenericInfo(com.intellij.util.xml.reflect.DomGenericInfo) DomFixedChildDescription(com.intellij.util.xml.reflect.DomFixedChildDescription)

Example 4 with DomCollectionChildDescription

use of com.intellij.util.xml.reflect.DomCollectionChildDescription in project intellij-community by JetBrains.

the class ResolvingElementQuickFix method getChildDescription.

@Nullable
public static <T extends DomElement> DomCollectionChildDescription getChildDescription(final List<DomElement> contexts, Class<T> clazz) {
    if (contexts.size() == 0) {
        return null;
    }
    final DomElement context = contexts.get(0);
    final DomGenericInfo genericInfo = context.getGenericInfo();
    final List<? extends DomCollectionChildDescription> descriptions = genericInfo.getCollectionChildrenDescriptions();
    for (DomCollectionChildDescription description : descriptions) {
        final Type type = description.getType();
        if (type.equals(clazz)) {
            return description;
        }
    }
    return null;
}
Also used : Type(java.lang.reflect.Type) DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) DomGenericInfo(com.intellij.util.xml.reflect.DomGenericInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with DomCollectionChildDescription

use of com.intellij.util.xml.reflect.DomCollectionChildDescription in project intellij-community by JetBrains.

the class BaseDomElementNode method doGetChildren.

protected final SimpleNode[] doGetChildren(final DomElement element) {
    if (!element.isValid())
        return NO_CHILDREN;
    List<SimpleNode> children = new ArrayList<>();
    final XmlTag tag = element.getXmlTag();
    if (tag != null && !(tag.getContainingFile() instanceof XmlFile))
        return NO_CHILDREN;
    final XmlElementDescriptor xmlElementDescriptor = tag == null ? null : tag.getDescriptor();
    final XmlElementDescriptor[] xmlDescriptors = xmlElementDescriptor == null ? null : xmlElementDescriptor.getElementsDescriptors(tag);
    for (DomFixedChildDescription description : element.getGenericInfo().getFixedChildrenDescriptions()) {
        String childName = description.getXmlElementName();
        if (xmlDescriptors != null) {
            if (findDescriptor(xmlDescriptors, childName) == -1)
                continue;
        }
        final List<? extends DomElement> values = description.getStableValues(element);
        if (shouldBeShown(description.getType())) {
            if (DomUtil.isGenericValueType(description.getType())) {
                for (DomElement value : values) {
                    children.add(new GenericValueNode((GenericDomValue) value, this));
                }
            } else {
                for (DomElement domElement : values) {
                    children.add(new BaseDomElementNode(domElement, myRootDomElement, this));
                }
            }
        }
    }
    for (DomCollectionChildDescription description : element.getGenericInfo().getCollectionChildrenDescriptions()) {
        if (shouldBeShown(description.getType())) {
            DomElementsGroupNode groupNode = new DomElementsGroupNode(element, description, this, myRootDomElement);
            if (isMarkedType(description.getType(), CONSOLIDATED_NODES_KEY)) {
                Collections.addAll(children, groupNode.getChildren());
            } else {
                children.add(groupNode);
            }
        }
    }
    AbstractDomElementNode[] childrenNodes = children.toArray(new AbstractDomElementNode[children.size()]);
    Comparator<AbstractDomElementNode> comparator = DomUtil.getFile(myDomElement).getUserData(COMPARATOR_KEY);
    if (comparator == null) {
        comparator = getDefaultComparator(element);
    }
    if (comparator != null) {
        Arrays.sort(childrenNodes, comparator);
    }
    return childrenNodes;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) SimpleNode(com.intellij.ui.treeStructure.SimpleNode) DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) DomFixedChildDescription(com.intellij.util.xml.reflect.DomFixedChildDescription) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

DomCollectionChildDescription (com.intellij.util.xml.reflect.DomCollectionChildDescription)11 DomFixedChildDescription (com.intellij.util.xml.reflect.DomFixedChildDescription)5 DomGenericInfo (com.intellij.util.xml.reflect.DomGenericInfo)4 XmlTag (com.intellij.psi.xml.XmlTag)3 Type (java.lang.reflect.Type)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 Project (com.intellij.openapi.project.Project)2 PsiElement (com.intellij.psi.PsiElement)2 XmlFile (com.intellij.psi.xml.XmlFile)2 SimpleNode (com.intellij.ui.treeStructure.SimpleNode)2 AbstractDomChildrenDescription (com.intellij.util.xml.reflect.AbstractDomChildrenDescription)2 Document (com.intellij.openapi.editor.Document)1 NullableComputable (com.intellij.openapi.util.NullableComputable)1 RecursionGuard (com.intellij.openapi.util.RecursionGuard)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 PsiFile (com.intellij.psi.PsiFile)1 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)1 XmlElement (com.intellij.psi.xml.XmlElement)1 XmlElementType (com.intellij.psi.xml.XmlElementType)1