Search in sources :

Example 1 with EvaluatedXmlName

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

the class AbstractDomChildrenDescriptor method getAttributesDescriptors.

@Override
public XmlAttributeDescriptor[] getAttributesDescriptors(@Nullable final XmlTag context) {
    if (context == null)
        return XmlAttributeDescriptor.EMPTY;
    DomElement domElement = myManager.getDomElement(context);
    if (domElement == null)
        return XmlAttributeDescriptor.EMPTY;
    final List<? extends DomAttributeChildDescription> descriptions = domElement.getGenericInfo().getAttributeChildrenDescriptions();
    List<XmlAttributeDescriptor> descriptors = new ArrayList<>();
    for (DomAttributeChildDescription description : descriptions) {
        descriptors.add(new DomAttributeXmlDescriptor(description, myManager.getProject()));
    }
    List<? extends CustomDomChildrenDescription> customs = domElement.getGenericInfo().getCustomNameChildrenDescription();
    for (CustomDomChildrenDescription custom : customs) {
        CustomDomChildrenDescription.AttributeDescriptor descriptor = custom.getCustomAttributeDescriptor();
        if (descriptor != null) {
            for (EvaluatedXmlName variant : descriptor.getCompletionVariants(domElement)) {
                AttributeChildDescriptionImpl childDescription = new AttributeChildDescriptionImpl(variant.getXmlName(), String.class);
                descriptors.add(new DomAttributeXmlDescriptor(childDescription, myManager.getProject()));
            }
        }
    }
    return descriptors.toArray(new XmlAttributeDescriptor[descriptors.size()]);
}
Also used : DomElement(com.intellij.util.xml.DomElement) EvaluatedXmlName(com.intellij.util.xml.EvaluatedXmlName) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) ArrayList(java.util.ArrayList) AttributeChildDescriptionImpl(com.intellij.util.xml.impl.AttributeChildDescriptionImpl)

Example 2 with EvaluatedXmlName

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

the class AbstractDomChildrenDescriptor method getElementsDescriptors.

@Override
public XmlElementDescriptor[] getElementsDescriptors(final XmlTag context) {
    final DomElement domElement = myManager.getDomElement(context);
    if (domElement == null)
        return EMPTY_ARRAY;
    List<XmlElementDescriptor> xmlElementDescriptors = new ArrayList<>();
    for (DomCollectionChildDescription childrenDescription : domElement.getGenericInfo().getCollectionChildrenDescriptions()) {
        xmlElementDescriptors.add(new DomElementXmlDescriptor(childrenDescription, myManager));
    }
    for (DomFixedChildDescription childrenDescription : domElement.getGenericInfo().getFixedChildrenDescriptions()) {
        xmlElementDescriptors.add(new DomElementXmlDescriptor(childrenDescription, myManager));
    }
    final List<? extends CustomDomChildrenDescription> customs = domElement.getGenericInfo().getCustomNameChildrenDescription();
    for (final CustomDomChildrenDescription custom : customs) {
        final CustomDomChildrenDescription.TagNameDescriptor tagNameDescriptor = custom.getTagNameDescriptor();
        if (tagNameDescriptor == null)
            continue;
        final XmlTag xmlTag = domElement.getXmlTag();
        for (final EvaluatedXmlName name : tagNameDescriptor.getCompletionVariants(domElement)) {
            AbstractDomChildrenDescriptor descriptor = new AbstractDomChildrenDescriptor(myManager) {

                @Override
                public String getDefaultName() {
                    final String ns = xmlTag != null ? name.getNamespace(xmlTag, (XmlFile) xmlTag.getContainingFile()) : null;
                    if (ns != null) {
                        final String prefix = xmlTag.getPrefixByNamespace(ns);
                        if (prefix != null) {
                            return prefix + ":" + name.getXmlName().getLocalName();
                        }
                    }
                    return name.getXmlName().getLocalName();
                }

                @Override
                @Nullable
                public PsiElement getDeclaration() {
                    final PomTarget target = tagNameDescriptor.findDeclaration(domElement, name);
                    return target == null ? null : PomService.convertToPsi(context.getProject(), target);
                }
            };
            xmlElementDescriptors.add(descriptor);
        }
        xmlElementDescriptors.add(new AnyXmlElementDescriptor(this, getNSDescriptor()));
    }
    return xmlElementDescriptors.toArray(new XmlElementDescriptor[xmlElementDescriptors.size()]);
}
Also used : EvaluatedXmlName(com.intellij.util.xml.EvaluatedXmlName) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) XmlFile(com.intellij.psi.xml.XmlFile) ArrayList(java.util.ArrayList) PomTarget(com.intellij.pom.PomTarget) DomElement(com.intellij.util.xml.DomElement) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag)

Example 3 with EvaluatedXmlName

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

the class AddToCompositeCollectionInvocation method invoke.

@Override
public Object invoke(final DomInvocationHandler<?, ?> handler, final Object[] args) throws Throwable {
    final XmlTag tag = handler.ensureTagExists();
    Set<XmlTag> set = ContainerUtil.newTroveSet();
    for (final CollectionChildDescriptionImpl qname : myQnames) {
        set.addAll(qname.getCollectionSubTags(handler, tag));
    }
    int index = args != null && args.length == 1 ? (Integer) args[0] : Integer.MAX_VALUE;
    XmlTag lastTag = null;
    int i = 0;
    final XmlTag[] tags = tag.getSubTags();
    for (final XmlTag subTag : tags) {
        if (i == index)
            break;
        if (set.contains(subTag)) {
            lastTag = subTag;
            i++;
        }
    }
    final DomManagerImpl manager = handler.getManager();
    final boolean b = manager.setChanging(true);
    try {
        final EvaluatedXmlName evaluatedXmlName = handler.createEvaluatedXmlName(myMainDescription.getXmlName());
        final XmlTag emptyTag = handler.createChildTag(evaluatedXmlName);
        final XmlTag newTag;
        if (lastTag == null) {
            if (tags.length == 0) {
                newTag = (XmlTag) tag.add(emptyTag);
            } else {
                newTag = (XmlTag) tag.addBefore(emptyTag, tags[0]);
            }
        } else {
            newTag = (XmlTag) tag.addAfter(emptyTag, lastTag);
        }
        return new CollectionElementInvocationHandler(myType, newTag, myMainDescription, handler, null).getProxy();
    } finally {
        manager.setChanging(b);
    }
}
Also used : EvaluatedXmlName(com.intellij.util.xml.EvaluatedXmlName) XmlTag(com.intellij.psi.xml.XmlTag)

Example 4 with EvaluatedXmlName

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

the class DomSemContributor method registerSemProviders.

@Override
public void registerSemProviders(SemRegistrar registrar) {
    registrar.registerSemElementProvider(DomManagerImpl.FILE_DESCRIPTION_KEY, xmlFile(), xmlFile -> {
        ApplicationManager.getApplication().assertReadAccessAllowed();
        return new FileDescriptionCachedValueProvider(DomManagerImpl.getDomManager(xmlFile.getProject()), xmlFile);
    });
    registrar.registerSemElementProvider(DomManagerImpl.DOM_HANDLER_KEY, xmlTag().withParent(psiElement(XmlElementType.XML_DOCUMENT).withParent(xmlFile())), xmlTag -> {
        final FileDescriptionCachedValueProvider provider = mySemService.getSemElement(DomManagerImpl.FILE_DESCRIPTION_KEY, xmlTag.getContainingFile());
        assert provider != null;
        final DomFileElementImpl element = provider.getFileElement();
        if (element != null) {
            final DomRootInvocationHandler handler = element.getRootHandler();
            if (handler.getXmlTag() == xmlTag) {
                return handler;
            }
        }
        return null;
    });
    final ElementPattern<XmlTag> nonRootTag = xmlTag().withParent(or(xmlTag(), xmlEntityRef().withParent(xmlTag())));
    registrar.registerSemElementProvider(DomManagerImpl.DOM_INDEXED_HANDLER_KEY, nonRootTag, tag -> {
        final XmlTag parentTag = PhysicalDomParentStrategy.getParentTag(tag);
        assert parentTag != null;
        DomInvocationHandler parent = getParentDom(parentTag);
        if (parent == null)
            return null;
        final String localName = tag.getLocalName();
        final String namespace = tag.getNamespace();
        final DomFixedChildDescription description = findChildrenDescription(parent.getGenericInfo().getFixedChildrenDescriptions(), tag, parent);
        if (description != null) {
            final int totalCount = description.getCount();
            int index = 0;
            PsiElement current = tag;
            while (true) {
                current = current.getPrevSibling();
                if (current == null) {
                    break;
                }
                if (current instanceof XmlTag) {
                    final XmlTag xmlTag = (XmlTag) current;
                    if (localName.equals(xmlTag.getLocalName()) && namespace.equals(xmlTag.getNamespace())) {
                        index++;
                        if (index >= totalCount) {
                            return null;
                        }
                    }
                }
            }
            final DomManagerImpl myDomManager = parent.getManager();
            return new IndexedElementInvocationHandler(parent.createEvaluatedXmlName(description.getXmlName()), (FixedChildDescriptionImpl) description, index, new PhysicalDomParentStrategy(tag, myDomManager), myDomManager, null);
        }
        return null;
    });
    registrar.registerSemElementProvider(DomManagerImpl.DOM_COLLECTION_HANDLER_KEY, nonRootTag, tag -> {
        final XmlTag parentTag = PhysicalDomParentStrategy.getParentTag(tag);
        assert parentTag != null;
        DomInvocationHandler parent = getParentDom(parentTag);
        if (parent == null)
            return null;
        final DomCollectionChildDescription description = findChildrenDescription(parent.getGenericInfo().getCollectionChildrenDescriptions(), tag, parent);
        if (description != null) {
            DomStub parentStub = parent.getStub();
            if (parentStub != null) {
                int index = ArrayUtil.indexOf(parentTag.findSubTags(tag.getName(), tag.getNamespace()), tag);
                ElementStub stub = parentStub.getElementStub(tag.getLocalName(), index);
                if (stub != null) {
                    XmlName name = description.getXmlName();
                    EvaluatedXmlNameImpl evaluatedXmlName = EvaluatedXmlNameImpl.createEvaluatedXmlName(name, name.getNamespaceKey(), true);
                    return new CollectionElementInvocationHandler(evaluatedXmlName, (AbstractDomChildDescriptionImpl) description, parent.getManager(), stub);
                }
            }
            return new CollectionElementInvocationHandler(description.getType(), tag, (AbstractCollectionChildDescription) description, parent, null);
        }
        return null;
    });
    registrar.registerSemElementProvider(DomManagerImpl.DOM_CUSTOM_HANDLER_KEY, nonRootTag, new NullableFunction<XmlTag, CollectionElementInvocationHandler>() {

        private final RecursionGuard myGuard = RecursionManager.createGuard("customDomParent");

        @Override
        public CollectionElementInvocationHandler fun(XmlTag tag) {
            if (StringUtil.isEmpty(tag.getName()))
                return null;
            final XmlTag parentTag = PhysicalDomParentStrategy.getParentTag(tag);
            assert parentTag != null;
            DomInvocationHandler parent = myGuard.doPreventingRecursion(tag, true, (NullableComputable<DomInvocationHandler>) () -> getParentDom(parentTag));
            if (parent == null)
                return null;
            DomGenericInfoEx info = parent.getGenericInfo();
            final List<? extends CustomDomChildrenDescription> customs = info.getCustomNameChildrenDescription();
            if (customs.isEmpty())
                return null;
            if (mySemService.getSemElement(DomManagerImpl.DOM_INDEXED_HANDLER_KEY, tag) == null && mySemService.getSemElement(DomManagerImpl.DOM_COLLECTION_HANDLER_KEY, tag) == null) {
                String localName = tag.getLocalName();
                XmlFile file = parent.getFile();
                for (final DomFixedChildDescription description : info.getFixedChildrenDescriptions()) {
                    XmlName xmlName = description.getXmlName();
                    if (localName.equals(xmlName.getLocalName()) && DomImplUtil.isNameSuitable(xmlName, tag, parent, file)) {
                        return null;
                    }
                }
                for (CustomDomChildrenDescription description : customs) {
                    if (description.getTagNameDescriptor() != null) {
                        AbstractCollectionChildDescription desc = (AbstractCollectionChildDescription) description;
                        Type type = description.getType();
                        return new CollectionElementInvocationHandler(type, tag, desc, parent, null);
                    }
                }
            }
            return null;
        }
    });
    registrar.registerSemElementProvider(DomManagerImpl.DOM_ATTRIBUTE_HANDLER_KEY, xmlAttribute(), attribute -> {
        final XmlTag tag = PhysicalDomParentStrategy.getParentTag(attribute);
        final DomInvocationHandler handler = tag == null ? null : getParentDom(tag);
        if (handler == null)
            return null;
        final String localName = attribute.getLocalName();
        final Ref<AttributeChildInvocationHandler> result = Ref.create(null);
        handler.getGenericInfo().processAttributeChildrenDescriptions(description -> {
            if (description.getXmlName().getLocalName().equals(localName)) {
                final EvaluatedXmlName evaluatedXmlName = handler.createEvaluatedXmlName(description.getXmlName());
                final String ns = evaluatedXmlName.getNamespace(tag, handler.getFile());
                if (ns.equals(tag.getNamespace()) && localName.equals(attribute.getName()) || ns.equals(attribute.getNamespace())) {
                    final DomManagerImpl myDomManager = handler.getManager();
                    final AttributeChildInvocationHandler attributeHandler = new AttributeChildInvocationHandler(evaluatedXmlName, description, myDomManager, new PhysicalDomParentStrategy(attribute, myDomManager), null);
                    result.set(attributeHandler);
                    return false;
                }
            }
            return true;
        });
        return result.get();
    });
}
Also used : DomStub(com.intellij.util.xml.stubs.DomStub) CustomDomChildrenDescription(com.intellij.util.xml.reflect.CustomDomChildrenDescription) EvaluatedXmlName(com.intellij.util.xml.EvaluatedXmlName) XmlName(com.intellij.util.xml.XmlName) PsiElement(com.intellij.psi.PsiElement) EvaluatedXmlName(com.intellij.util.xml.EvaluatedXmlName) XmlFile(com.intellij.psi.xml.XmlFile) RecursionGuard(com.intellij.openapi.util.RecursionGuard) NullableComputable(com.intellij.openapi.util.NullableComputable) EvaluatedXmlNameImpl(com.intellij.util.xml.EvaluatedXmlNameImpl) XmlElementType(com.intellij.psi.xml.XmlElementType) Type(java.lang.reflect.Type) DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) DomFixedChildDescription(com.intellij.util.xml.reflect.DomFixedChildDescription) ElementStub(com.intellij.util.xml.stubs.ElementStub) XmlTag(com.intellij.psi.xml.XmlTag)

Example 5 with EvaluatedXmlName

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

the class DomGenericInfoEx method findChildrenDescription.

@Nullable
public final AbstractDomChildrenDescription findChildrenDescription(DomInvocationHandler handler, final String localName, String namespace, boolean attribute, final String qName) {
    for (final AbstractDomChildrenDescription description : getChildrenDescriptions()) {
        if (description instanceof DomChildDescriptionImpl && description instanceof AttributeChildDescriptionImpl == attribute) {
            final XmlName xmlName = ((DomChildDescriptionImpl) description).getXmlName();
            if (attribute && StringUtil.isEmpty(namespace) && xmlName.getLocalName().equals(localName))
                return description;
            final EvaluatedXmlName evaluatedXmlName = handler.createEvaluatedXmlName(xmlName);
            if (DomImplUtil.isNameSuitable(evaluatedXmlName, localName, qName, namespace, handler.getFile())) {
                return description;
            }
        }
    }
    List<? extends CustomDomChildrenDescription> list = getCustomNameChildrenDescription();
    for (CustomDomChildrenDescription description : list) {
        if (attribute) {
        // todo
        } else if (description.getTagNameDescriptor() != null) {
            return description;
        }
    }
    return null;
}
Also used : EvaluatedXmlName(com.intellij.util.xml.EvaluatedXmlName) EvaluatedXmlName(com.intellij.util.xml.EvaluatedXmlName) XmlName(com.intellij.util.xml.XmlName) AbstractDomChildrenDescription(com.intellij.util.xml.reflect.AbstractDomChildrenDescription) CustomDomChildrenDescription(com.intellij.util.xml.reflect.CustomDomChildrenDescription) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

EvaluatedXmlName (com.intellij.util.xml.EvaluatedXmlName)7 XmlTag (com.intellij.psi.xml.XmlTag)4 XmlFile (com.intellij.psi.xml.XmlFile)3 XmlName (com.intellij.util.xml.XmlName)3 DomElement (com.intellij.util.xml.DomElement)2 CustomDomChildrenDescription (com.intellij.util.xml.reflect.CustomDomChildrenDescription)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 NullableComputable (com.intellij.openapi.util.NullableComputable)1 RecursionGuard (com.intellij.openapi.util.RecursionGuard)1 PomTarget (com.intellij.pom.PomTarget)1 PsiElement (com.intellij.psi.PsiElement)1 XmlElementType (com.intellij.psi.xml.XmlElementType)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 EvaluatedXmlNameImpl (com.intellij.util.xml.EvaluatedXmlNameImpl)1 AttributeChildDescriptionImpl (com.intellij.util.xml.impl.AttributeChildDescriptionImpl)1 AbstractDomChildrenDescription (com.intellij.util.xml.reflect.AbstractDomChildrenDescription)1 DomCollectionChildDescription (com.intellij.util.xml.reflect.DomCollectionChildDescription)1 DomFixedChildDescription (com.intellij.util.xml.reflect.DomFixedChildDescription)1 DomStub (com.intellij.util.xml.stubs.DomStub)1