Search in sources :

Example 41 with XmlAttribute

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

the class LinkedToHtmlFilesContributor method fillRelatedFiles.

@Override
public void fillRelatedFiles(@NotNull final XmlFile xmlFile, @NotNull final Set<PsiFile> resultSet) {
    HtmlLinkUtil.processLinks(xmlFile, tag -> {
        final XmlAttribute attribute = tag.getAttribute("href");
        if (attribute == null) {
            return true;
        }
        final XmlAttributeValue link = attribute.getValueElement();
        if (link == null) {
            return true;
        }
        for (PsiReference reference : link.getReferences()) {
            if (reference instanceof PsiPolyVariantReference) {
                final ResolveResult[] results = ((PsiPolyVariantReference) reference).multiResolve(false);
                for (ResolveResult result : results) {
                    final PsiElement resolvedElement = result.getElement();
                    if (resolvedElement instanceof PsiFile) {
                        resultSet.add((PsiFile) resolvedElement);
                    }
                }
            } else {
                final PsiElement resolvedElement = reference.resolve();
                if (resolvedElement instanceof PsiFile) {
                    resultSet.add((PsiFile) resolvedElement);
                }
            }
        }
        return true;
    });
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue)

Example 42 with XmlAttribute

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

the class XmlLanguageBreadcrumbsInfoProvider method getElementTooltip.

@Override
@Nullable
public String getElementTooltip(@NotNull final PsiElement e) {
    final XmlTag tag = (XmlTag) e;
    final StringBuilder result = new StringBuilder("&lt;");
    result.append(tag.getName());
    final XmlAttribute[] attributes = tag.getAttributes();
    for (final XmlAttribute each : attributes) {
        result.append(" ").append(each.getText());
    }
    if (tag.isEmpty()) {
        result.append("/&gt;");
    } else {
        result.append("&gt;...&lt;/").append(tag.getName()).append("&gt;");
    }
    return result.toString();
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with XmlAttribute

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

the class SchemaDefinitionsSearch method getNameAttr.

public static XmlAttribute getNameAttr(XmlTagImpl xml) {
    XmlAttribute name = xml.getAttribute("name", XmlUtil.XML_SCHEMA_URI);
    name = name == null ? xml.getAttribute("name") : name;
    return name;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute)

Example 44 with XmlAttribute

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

the class SyntheticBlock method getSpacing.

@Override
public Spacing getSpacing(Block child1, @NotNull Block child2) {
    if (child1 instanceof ReadOnlyBlock || child2 instanceof ReadOnlyBlock) {
        return Spacing.getReadOnlySpacing();
    }
    if (!(child1 instanceof AbstractXmlBlock) || !(child2 instanceof AbstractXmlBlock)) {
        return null;
    }
    ASTNode node1 = ((AbstractBlock) child1).getNode();
    ASTNode node2 = ((AbstractBlock) child2).getNode();
    IElementType type1 = node1.getElementType();
    IElementType type2 = node2.getElementType();
    if (type2 == XmlElementType.XML_COMMENT) {
        // Do not remove any spaces except extra blank lines
        return Spacing.createSpacing(0, Integer.MAX_VALUE, 0, true, myXmlFormattingPolicy.getKeepBlankLines());
    }
    if (type1 == XmlElementType.XML_COMMENT) {
        ASTNode prev = node1.getTreePrev();
        if (prev != null) {
            node1 = prev;
            type1 = prev.getElementType();
        }
    }
    boolean firstIsText = isTextFragment(node1);
    boolean secondIsText = isTextFragment(node2);
    if (((AbstractXmlBlock) child1).isPreserveSpace() && ((AbstractXmlBlock) child2).isPreserveSpace()) {
        return Spacing.getReadOnlySpacing();
    }
    if (type1 == XmlTokenType.XML_CDATA_START || type2 == XmlTokenType.XML_CDATA_END) {
        if (myXmlFormattingPolicy.getKeepWhiteSpacesInsideCDATA()) {
            return Spacing.getReadOnlySpacing();
        }
        if (type1 == XmlTokenType.XML_CDATA_START && type2 == XmlTokenType.XML_CDATA_END) {
            return Spacing.createSpacing(0, 0, 0, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
        }
        if (type1 == XmlTokenType.XML_CDATA_START && child2 instanceof AnotherLanguageBlockWrapper || type2 == XmlTokenType.XML_CDATA_END && child1 instanceof AnotherLanguageBlockWrapper) {
            return Spacing.createSpacing(0, 0, 1, myXmlFormattingPolicy.getShouldKeepLineBreaks(), 0);
        }
    }
    boolean firstIsTag = node1.getPsi() instanceof XmlTag && !firstIsText;
    boolean secondIsTag = node2.getPsi() instanceof XmlTag && !secondIsText;
    boolean firstIsEntityRef = isEntityRef(node1);
    boolean secondIsEntityRef = isEntityRef(node2);
    if ((secondIsText && isInlineTag(node1) || firstIsText && isInlineTag(node2)) && myXmlFormattingPolicy.isKeepSpacesAroundInlineTags()) {
        return Spacing.getReadOnlySpacing();
    }
    if (isSpaceInText(firstIsTag, secondIsTag, firstIsText, secondIsText) && keepWhiteSpaces()) {
        return Spacing.getReadOnlySpacing();
    }
    if (firstIsEntityRef || secondIsEntityRef) {
        return Spacing.createSafeSpacing(myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
    }
    if (type1 == XmlElementType.XML_ATTRIBUTE && (type2 == XmlTokenType.XML_TAG_END || type2 == XmlTokenType.XML_EMPTY_ELEMENT_END)) {
        final PsiElement psi1 = node1.getPsi();
        if (psi1 instanceof XmlAttribute && myXmlFormattingPolicy.insertLineBreakAfterLastAttribute((XmlAttribute) psi1)) {
            return Spacing.createSpacing(0, 0, 1, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
        }
    }
    if (type2 == XmlTokenType.XML_EMPTY_ELEMENT_END && myXmlFormattingPolicy.addSpaceIntoEmptyTag()) {
        return Spacing.createSpacing(1, 1, 0, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
    }
    if (isXmlTagName(type1, type2)) {
        final int spaces = shouldAddSpaceAroundTagName(node1, node2) ? 1 : 0;
        return Spacing.createSpacing(spaces, spaces, 0, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
    }
    if (type2 == XmlElementType.XML_ATTRIBUTE) {
        int minLineFeeds = 0;
        if (type1 == XmlTokenType.XML_NAME) {
            final PsiElement psi2 = node2.getPsi();
            minLineFeeds = psi2 instanceof XmlAttribute && myXmlFormattingPolicy.insertLineBreakBeforeFirstAttribute((XmlAttribute) psi2) ? 1 : 0;
        }
        return Spacing.createSpacing(1, 1, minLineFeeds, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
    }
    if (((AbstractXmlBlock) child1).isTextElement() && ((AbstractXmlBlock) child2).isTextElement()) {
        return Spacing.createSafeSpacing(myXmlFormattingPolicy.getShouldKeepLineBreaksInText(), myXmlFormattingPolicy.getKeepBlankLines());
    }
    if (firstIsTag && insertLineFeedAfter((XmlTag) node1.getPsi())) {
        return Spacing.createSpacing(0, 0, 1, true, myXmlFormattingPolicy.getKeepBlankLines());
    }
    if ((firstIsText || firstIsTag) && secondIsTag) {
        //<tag/>text <tag/></tag>
        if (((AbstractXmlBlock) child2).insertLineBreakBeforeTag()) {
            return Spacing.createSpacing(0, Integer.MAX_VALUE, ((AbstractXmlBlock) child2).getBlankLinesBeforeTag() + 1, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
        } else if (((AbstractXmlBlock) child2).removeLineBreakBeforeTag()) {
            return Spacing.createSpacing(0, Integer.MAX_VALUE, 0, myXmlFormattingPolicy.getShouldKeepLineBreaks(), myXmlFormattingPolicy.getKeepBlankLines());
        }
    }
    final boolean saveSpacesBetweenTagAndText = myXmlFormattingPolicy.shouldSaveSpacesBetweenTagAndText() && child1.getTextRange().getEndOffset() < child2.getTextRange().getStartOffset();
    if (firstIsTag && secondIsText) {
        if (((AbstractXmlBlock) child1).isTextElement() || saveSpacesBetweenTagAndText) {
            return Spacing.createSafeSpacing(true, myXmlFormattingPolicy.getKeepBlankLines());
        } else {
            return Spacing.createSpacing(0, 0, 0, true, myXmlFormattingPolicy.getKeepBlankLines());
        }
    }
    if (firstIsText && secondIsTag) {
        //text-<tag/>
        if (((AbstractXmlBlock) child2).isTextElement() || saveSpacesBetweenTagAndText) {
            return Spacing.createSafeSpacing(true, myXmlFormattingPolicy.getKeepBlankLines());
        } else {
            return Spacing.createSpacing(0, 0, 0, true, myXmlFormattingPolicy.getKeepBlankLines());
        }
    }
    if (firstIsTag && secondIsTag) {
        //<tag/><tag/>
        return Spacing.createSpacing(0, Integer.MAX_VALUE, 0, true, myXmlFormattingPolicy.getKeepBlankLines());
    }
    return Spacing.createSpacing(0, Integer.MAX_VALUE, 0, myXmlFormattingPolicy.getShouldKeepLineBreaksInText(), myXmlFormattingPolicy.getKeepBlankLines());
}
Also used : IElementType(com.intellij.psi.tree.IElementType) XmlAttribute(com.intellij.psi.xml.XmlAttribute) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) AbstractBlock(com.intellij.psi.formatter.common.AbstractBlock) XmlTag(com.intellij.psi.xml.XmlTag)

Example 45 with XmlAttribute

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

the class XmlAttributeReferenceCompletionProvider method addAttributeReferenceCompletionVariants.

public static void addAttributeReferenceCompletionVariants(XmlAttributeReference reference, CompletionResultSet result, @Nullable InsertHandler<LookupElement> replacementInsertHandler) {
    final XmlTag declarationTag = reference.getElement().getParent();
    LOG.assertTrue(declarationTag.isValid());
    final XmlElementDescriptor parentDescriptor = declarationTag.getDescriptor();
    if (parentDescriptor != null) {
        final XmlAttribute[] attributes = declarationTag.getAttributes();
        XmlAttributeDescriptor[] descriptors = parentDescriptor.getAttributesDescriptors(declarationTag);
        descriptors = HtmlUtil.appendHtmlSpecificAttributeCompletions(declarationTag, descriptors, reference.getElement());
        addVariants(result, attributes, descriptors, reference.getElement(), replacementInsertHandler);
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) NamespaceAwareXmlAttributeDescriptor(com.intellij.xml.NamespaceAwareXmlAttributeDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlAttribute (com.intellij.psi.xml.XmlAttribute)220 XmlTag (com.intellij.psi.xml.XmlTag)116 PsiElement (com.intellij.psi.PsiElement)60 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)57 NotNull (org.jetbrains.annotations.NotNull)44 XmlFile (com.intellij.psi.xml.XmlFile)37 Nullable (org.jetbrains.annotations.Nullable)27 Project (com.intellij.openapi.project.Project)25 VirtualFile (com.intellij.openapi.vfs.VirtualFile)20 TextRange (com.intellij.openapi.util.TextRange)18 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)18 ArrayList (java.util.ArrayList)18 PsiFile (com.intellij.psi.PsiFile)17 PsiReference (com.intellij.psi.PsiReference)17 IncorrectOperationException (com.intellij.util.IncorrectOperationException)10 DomElement (com.intellij.util.xml.DomElement)10 Result (com.intellij.openapi.application.Result)9 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)9 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)8 XmlElement (com.intellij.psi.xml.XmlElement)6