Search in sources :

Example 51 with XmlElement

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

the class DomHighlightingHelperImpl method checkRequired.

@Override
@NotNull
public List<DomElementProblemDescriptor> checkRequired(final DomElement element, final DomElementAnnotationHolder holder) {
    final Required required = element.getAnnotation(Required.class);
    if (required != null) {
        final XmlElement xmlElement = element.getXmlElement();
        if (xmlElement == null) {
            if (required.value()) {
                final String xmlElementName = element.getXmlElementName();
                String namespace = element.getXmlElementNamespace();
                if (element instanceof GenericAttributeValue) {
                    return Collections.singletonList(holder.createProblem(element, IdeBundle.message("attribute.0.should.be.defined", xmlElementName), new DefineAttributeQuickFix(xmlElementName, namespace)));
                }
                return Collections.singletonList(holder.createProblem(element, HighlightSeverity.ERROR, IdeBundle.message("child.tag.0.should.be.defined", xmlElementName), new AddRequiredSubtagFix(xmlElementName, namespace)));
            }
        } else if (element instanceof GenericDomValue) {
            return ContainerUtil.createMaybeSingletonList(checkRequiredGenericValue((GenericDomValue) element, required, holder));
        }
    }
    if (DomUtil.hasXml(element)) {
        final SmartList<DomElementProblemDescriptor> list = new SmartList<>();
        final DomGenericInfo info = element.getGenericInfo();
        for (final AbstractDomChildrenDescription description : info.getChildrenDescriptions()) {
            if (description instanceof DomCollectionChildDescription && description.getValues(element).isEmpty()) {
                final DomCollectionChildDescription childDescription = (DomCollectionChildDescription) description;
                final Required annotation = description.getAnnotation(Required.class);
                if (annotation != null && annotation.value()) {
                    list.add(holder.createProblem(element, childDescription, IdeBundle.message("child.tag.0.should.be.defined", ((DomCollectionChildDescription) description).getXmlElementName())));
                }
            }
        }
        return list;
    }
    return Collections.emptyList();
}
Also used : DomGenericInfo(com.intellij.util.xml.reflect.DomGenericInfo) DomCollectionChildDescription(com.intellij.util.xml.reflect.DomCollectionChildDescription) XmlElement(com.intellij.psi.xml.XmlElement) AbstractDomChildrenDescription(com.intellij.util.xml.reflect.AbstractDomChildrenDescription) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 52 with XmlElement

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

the class DomHighlightingHelperImpl method checkResolveProblems.

@Override
@NotNull
public List<DomElementProblemDescriptor> checkResolveProblems(GenericDomValue element, final DomElementAnnotationHolder holder) {
    if (StringUtil.isEmpty(element.getStringValue())) {
        final Required required = element.getAnnotation(Required.class);
        if (required != null && !required.nonEmpty())
            return Collections.emptyList();
    }
    final XmlElement valueElement = DomUtil.getValueElement(element);
    if (valueElement != null && !isSoftReference(element)) {
        final SmartList<DomElementProblemDescriptor> list = new SmartList<>();
        final PsiReference[] psiReferences = myProvider.getReferencesByElement(valueElement, new ProcessingContext());
        GenericDomValueReference domReference = ContainerUtil.findInstance(psiReferences, GenericDomValueReference.class);
        final Converter converter = WrappingConverter.getDeepestConverter(element.getConverter(), element);
        boolean hasBadResolve = false;
        if (domReference == null || !isDomResolveOK(element, domReference, converter)) {
            for (final PsiReference reference : psiReferences) {
                if (reference != domReference && hasBadResolve(reference)) {
                    hasBadResolve = true;
                    list.add(holder.createResolveProblem(element, reference));
                }
            }
            final boolean isResolvingConverter = converter instanceof ResolvingConverter;
            //noinspection unchecked
            if (!hasBadResolve && (domReference != null || isResolvingConverter && hasBadResolve(domReference = new GenericDomValueReference(element)))) {
                hasBadResolve = true;
                final String errorMessage = converter.getErrorMessage(element.getStringValue(), ConvertContextFactory.createConvertContext(DomManagerImpl.getDomInvocationHandler(element)));
                if (errorMessage != null) {
                    list.add(holder.createResolveProblem(element, domReference));
                }
            }
        }
        if (!hasBadResolve && psiReferences.length == 0 && element.getValue() == null && !PsiTreeUtil.hasErrorElements(valueElement)) {
            final String errorMessage = converter.getErrorMessage(element.getStringValue(), ConvertContextFactory.createConvertContext(DomManagerImpl.getDomInvocationHandler(element)));
            if (errorMessage != null) {
                list.add(holder.createProblem(element, errorMessage));
            }
        }
        return list;
    }
    return Collections.emptyList();
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) PsiReference(com.intellij.psi.PsiReference) XmlElement(com.intellij.psi.xml.XmlElement) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 53 with XmlElement

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

the class DomInvocationHandler method createChildTag.

protected final XmlTag createChildTag(final EvaluatedXmlName tagName) {
    final String localName = tagName.getXmlName().getLocalName();
    if (localName.contains(":")) {
        try {
            return XmlElementFactory.getInstance(myManager.getProject()).createTagFromText("<" + localName + "/>");
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    }
    final XmlElement element = getXmlElement();
    assert element != null;
    return getXmlTag().createChildTag(localName, tagName.getNamespace(element, getFile()), null, false);
}
Also used : XmlElement(com.intellij.psi.xml.XmlElement)

Example 54 with XmlElement

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

the class PhysicalDomParentStrategy method strategyEquals.

public static boolean strategyEquals(DomParentStrategy strategy, final Object o) {
    if (strategy == o)
        return true;
    if (!(o instanceof DomParentStrategy))
        return false;
    final XmlElement thatElement = ((DomParentStrategy) o).getXmlElement();
    if (thatElement == null)
        return false;
    XmlElement element = strategy.getXmlElement();
    if (element == null)
        return false;
    if (xmlElementsEqual(element, thatElement)) {
        if (element != thatElement) {
            final PsiElement nav1 = element.getNavigationElement();
            final PsiElement nav2 = thatElement.getNavigationElement();
            if (nav1 != nav2) {
                PsiElement curContext = findIncluder(element);
                PsiElement navContext = findIncluder(nav1);
                LOG.error(LogMessageEx.createEvent("x:include processing error", "nav1,nav2=" + nav1 + ", " + nav2 + ";\n" + nav1.getContainingFile() + ":" + nav1.getTextRange().getStartOffset() + "!=" + nav2.getContainingFile() + ":" + nav2.getTextRange().getStartOffset() + ";\n" + (nav1 == element) + ";" + (nav2 == thatElement) + ";\n" + "contexts equal: " + (curContext == navContext) + ";\n" + "curContext?.physical=" + (curContext != null && curContext.isPhysical()) + ";\n" + "navContext?.physical=" + (navContext != null && navContext.isPhysical()) + ";\n" + "myElement.physical=" + element.isPhysical() + ";\n" + "thatElement.physical=" + thatElement.isPhysical() + "\n" + DebugUtil.currentStackTrace(), new Attachment("Including tag text 1.xml", curContext == null ? "null" : curContext.getText()), new Attachment("Including tag text 2.xml", navContext == null ? "null" : navContext.getText())));
                throw new AssertionError();
            }
        }
        return true;
    }
    return false;
}
Also used : XmlElement(com.intellij.psi.xml.XmlElement) Attachment(com.intellij.openapi.diagnostic.Attachment) PsiElement(com.intellij.psi.PsiElement)

Example 55 with XmlElement

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

the class XmlElementImpl method findElementByTokenType.

public XmlElement findElementByTokenType(final IElementType type) {
    final XmlElement[] result = new XmlElement[1];
    result[0] = null;
    processElements(new PsiElementProcessor() {

        @Override
        public boolean execute(@NotNull PsiElement element) {
            if (element instanceof TreeElement && ((ASTNode) element).getElementType() == type) {
                result[0] = (XmlElement) element;
                return false;
            }
            return true;
        }
    }, this);
    return result[0];
}
Also used : XmlElement(com.intellij.psi.xml.XmlElement) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiElement(com.intellij.psi.PsiElement) CompositePsiElement(com.intellij.psi.impl.source.tree.CompositePsiElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Aggregations

XmlElement (com.intellij.psi.xml.XmlElement)69 PsiElement (com.intellij.psi.PsiElement)16 NotNull (org.jetbrains.annotations.NotNull)16 Nullable (org.jetbrains.annotations.Nullable)11 XmlTag (com.intellij.psi.xml.XmlTag)10 PsiFile (com.intellij.psi.PsiFile)7 PsiReference (com.intellij.psi.PsiReference)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)6 XmlFile (com.intellij.psi.xml.XmlFile)6 Manifest (org.jetbrains.android.dom.manifest.Manifest)5 Module (com.intellij.openapi.module.Module)4 Project (com.intellij.openapi.project.Project)4 DomElement (com.intellij.util.xml.DomElement)4 ArrayList (java.util.ArrayList)4 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)3 CompositePsiElement (com.intellij.psi.impl.source.tree.CompositePsiElement)3 SmartList (com.intellij.util.SmartList)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 ResourceType (com.android.resources.ResourceType)2 Result (com.intellij.openapi.application.Result)2