Search in sources :

Example 81 with XmlAttributeDescriptor

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

the class GenerateXmlTagAction method generateRaw.

private static void generateRaw(@NotNull final XmlTag newTag) {
    XmlElementDescriptor selected = newTag.getDescriptor();
    if (selected == null)
        return;
    switch(selected.getContentType()) {
        case XmlElementDescriptor.CONTENT_TYPE_EMPTY:
            newTag.collapseIfEmpty();
            ASTNode node = newTag.getNode();
            assert node != null;
            ASTNode elementEnd = node.findChildByType(XmlTokenType.XML_EMPTY_ELEMENT_END);
            if (elementEnd == null) {
                LeafElement emptyTagEnd = Factory.createSingleLeafElement(XmlTokenType.XML_EMPTY_ELEMENT_END, "/>", 0, 2, null, newTag.getManager());
                node.addChild(emptyTagEnd);
            }
            break;
        case XmlElementDescriptor.CONTENT_TYPE_MIXED:
            newTag.getValue().setText("");
    }
    for (XmlAttributeDescriptor descriptor : selected.getAttributesDescriptors(newTag)) {
        if (descriptor.isRequired()) {
            newTag.setAttribute(descriptor.getName(), "");
        }
    }
    List<XmlElementDescriptor> tags = getRequiredSubTags(selected);
    for (XmlElementDescriptor descriptor : tags) {
        if (descriptor == null) {
            XmlTag tag = XmlElementFactory.getInstance(newTag.getProject()).createTagFromText("<", newTag.getLanguage());
            newTag.addSubTag(tag, false);
        } else {
            XmlTag subTag = newTag.addSubTag(createTag(newTag, descriptor), false);
            generateRaw(subTag);
        }
    }
}
Also used : XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) ASTNode(com.intellij.lang.ASTNode) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) LeafElement(com.intellij.psi.impl.source.tree.LeafElement)

Example 82 with XmlAttributeDescriptor

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

the class XmlAttributeValueGetter method getEnumeratedValues.

@NotNull
public static String[] getEnumeratedValues(XmlAttribute attribute) {
    final XmlAttributeDescriptor descriptor = attribute.getDescriptor();
    if (descriptor == null) {
        return ArrayUtil.EMPTY_STRING_ARRAY;
    }
    String[] result;
    if (descriptor instanceof BasicXmlAttributeDescriptor) {
        result = ((BasicXmlAttributeDescriptor) descriptor).getEnumeratedValues(attribute);
    } else if (descriptor instanceof XmlEnumerationDescriptor) {
        result = ((XmlEnumerationDescriptor) descriptor).getValuesForCompletion();
    } else {
        result = descriptor.getEnumeratedValues();
    }
    return result != null ? StringUtil.filterEmptyStrings(result) : ArrayUtil.EMPTY_STRING_ARRAY;
}
Also used : XmlEnumerationDescriptor(com.intellij.xml.impl.XmlEnumerationDescriptor) BasicXmlAttributeDescriptor(com.intellij.xml.impl.BasicXmlAttributeDescriptor) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) BasicXmlAttributeDescriptor(com.intellij.xml.impl.BasicXmlAttributeDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 83 with XmlAttributeDescriptor

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

the class XmlPrefixReferenceProvider method getReferencesByElement.

@NotNull
@Override
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
    XmlAttributeValue attributeValue = (XmlAttributeValue) element;
    String value = attributeValue.getValue();
    if (value == null)
        return PsiReference.EMPTY_ARRAY;
    int i = value.indexOf(':');
    if (i <= 0)
        return PsiReference.EMPTY_ARRAY;
    PsiElement parent = attributeValue.getParent();
    if (parent instanceof XmlAttribute) {
        XmlTag tag = ((XmlAttribute) parent).getParent();
        if (tag != null && !XmlNSDescriptorImpl.checkSchemaNamespace(tag)) {
            XmlAttributeDescriptor descriptor = ((XmlAttribute) parent).getDescriptor();
            if (descriptor instanceof XmlAttributeDescriptorImpl) {
                String type = ((XmlAttributeDescriptorImpl) descriptor).getType();
                if (type != null && type.endsWith(":QName")) {
                    String prefix = XmlUtil.findPrefixByQualifiedName(type);
                    String ns = ((XmlTag) descriptor.getDeclaration()).getNamespaceByPrefix(prefix);
                    if (XmlNSDescriptorImpl.checkSchemaNamespace(ns)) {
                        return new PsiReference[] { new SchemaPrefixReference(attributeValue, TextRange.from(1, i), value.substring(0, i), null) };
                    }
                }
            }
        }
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) PsiReference(com.intellij.psi.PsiReference) XmlAttributeDescriptorImpl(com.intellij.xml.impl.schema.XmlAttributeDescriptorImpl) SchemaPrefixReference(com.intellij.psi.impl.source.xml.SchemaPrefixReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 84 with XmlAttributeDescriptor

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

the class InsertRequiredAttributeFix method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") final Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    XmlTag myTag = (XmlTag) startElement;
    ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(myTag);
    final XmlElementDescriptor descriptor = myTag.getDescriptor();
    if (descriptor == null) {
        return;
    }
    final XmlAttributeDescriptor attrDescriptor = descriptor.getAttributeDescriptor(myAttrName, myTag);
    final boolean indirectSyntax = XmlExtension.getExtension(myTag.getContainingFile()).isIndirectSyntax(attrDescriptor);
    boolean insertShorthand = myTag instanceof HtmlTag && attrDescriptor != null && HtmlUtil.isBooleanAttribute(attrDescriptor, myTag);
    PsiElement anchor = SourceTreeToPsiMap.treeElementToPsi(XmlChildRole.EMPTY_TAG_END_FINDER.findChild(treeElement));
    final boolean anchorIsEmptyTag = anchor != null;
    if (anchor == null) {
        anchor = SourceTreeToPsiMap.treeElementToPsi(XmlChildRole.START_TAG_END_FINDER.findChild(treeElement));
    }
    if (anchor == null)
        return;
    final Template template = TemplateManager.getInstance(project).createTemplate("", "");
    if (indirectSyntax) {
        if (anchorIsEmptyTag)
            template.addTextSegment(">");
        template.addTextSegment("<jsp:attribute name=\"" + myAttrName + "\">");
    } else {
        template.addTextSegment(" " + myAttrName + (!insertShorthand ? "=\"" : ""));
    }
    Expression expression = new Expression() {

        final TextResult result = new TextResult("");

        @Override
        public Result calculateResult(ExpressionContext context) {
            return result;
        }

        @Override
        public Result calculateQuickResult(ExpressionContext context) {
            return null;
        }

        @Override
        public LookupElement[] calculateLookupItems(ExpressionContext context) {
            final LookupElement[] items = new LookupElement[myValues.length];
            for (int i = 0; i < items.length; i++) {
                items[i] = LookupElementBuilder.create(myValues[i]);
            }
            return items;
        }
    };
    if (!insertShorthand)
        template.addVariable(NAME_TEMPLATE_VARIABLE, expression, expression, true);
    if (indirectSyntax) {
        template.addTextSegment("</jsp:attribute>");
        template.addEndVariable();
        if (anchorIsEmptyTag)
            template.addTextSegment("</" + myTag.getName() + ">");
    } else if (!insertShorthand) {
        template.addTextSegment("\"");
    }
    final PsiElement anchor1 = anchor;
    final Runnable runnable = () -> ApplicationManager.getApplication().runWriteAction(() -> {
        int textOffset = anchor1.getTextOffset();
        if (!anchorIsEmptyTag && indirectSyntax)
            ++textOffset;
        editor.getCaretModel().moveToOffset(textOffset);
        if (anchorIsEmptyTag && indirectSyntax) {
            editor.getDocument().deleteString(textOffset, textOffset + 2);
        }
        TemplateManager.getInstance(project).startTemplate(editor, template);
    });
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
        Runnable commandRunnable = () -> CommandProcessor.getInstance().executeCommand(project, runnable, getText(), getFamilyName());
        ApplicationManager.getApplication().invokeLater(commandRunnable);
    } else {
        runnable.run();
    }
}
Also used : HtmlTag(com.intellij.psi.html.HtmlTag) LookupElement(com.intellij.codeInsight.lookup.LookupElement) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) ASTNode(com.intellij.lang.ASTNode) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 85 with XmlAttributeDescriptor

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

the class XmlAttributeReferenceCompletionProvider method addVariants.

private static void addVariants(final CompletionResultSet result, final XmlAttribute[] attributes, final XmlAttributeDescriptor[] descriptors, XmlAttribute attribute, @Nullable InsertHandler<LookupElement> replacementInsertHandler) {
    final XmlTag tag = attribute.getParent();
    final PsiFile file = tag.getContainingFile();
    final XmlExtension extension = XmlExtension.getExtension(file);
    final String prefix = attribute.getName().contains(":") && ((XmlAttributeImpl) attribute).getRealLocalName().length() > 0 ? attribute.getNamespacePrefix() + ":" : null;
    for (XmlAttributeDescriptor descriptor : descriptors) {
        if (isValidVariant(attribute, descriptor, attributes, extension)) {
            String name = descriptor.getName(tag);
            InsertHandler<LookupElement> insertHandler = XmlAttributeInsertHandler.INSTANCE;
            if (tag instanceof HtmlTag && HtmlUtil.isShortNotationOfBooleanAttributePreferred() && HtmlUtil.isBooleanAttribute(descriptor, tag)) {
                insertHandler = null;
            }
            if (replacementInsertHandler != null) {
                insertHandler = replacementInsertHandler;
            } else if (descriptor instanceof NamespaceAwareXmlAttributeDescriptor) {
                final String namespace = ((NamespaceAwareXmlAttributeDescriptor) descriptor).getNamespace(tag);
                if (file instanceof XmlFile && namespace != null && namespace.length() > 0 && !name.contains(":") && tag.getPrefixByNamespace(namespace) == null) {
                    insertHandler = new XmlAttributeInsertHandler(namespace);
                }
            }
            if (prefix == null || name.startsWith(prefix)) {
                if (prefix != null && name.length() > prefix.length()) {
                    name = descriptor.getName(tag).substring(prefix.length());
                }
                LookupElementBuilder element = LookupElementBuilder.create(name);
                if (descriptor instanceof PsiPresentableMetaData) {
                    element = element.withIcon(((PsiPresentableMetaData) descriptor).getIcon());
                }
                final int separator = name.indexOf(':');
                if (separator > 0) {
                    element = element.withLookupString(name.substring(separator + 1));
                }
                element = element.withCaseSensitivity(!(descriptor instanceof HtmlAttributeDescriptorImpl)).withInsertHandler(insertHandler);
                result.addElement(descriptor.isRequired() ? PrioritizedLookupElement.withPriority(element.appendTailText("(required)", true), 100) : HtmlUtil.isOwnHtmlAttribute(descriptor) ? PrioritizedLookupElement.withPriority(element, 50) : element);
            }
        }
    }
}
Also used : HtmlAttributeDescriptorImpl(com.intellij.psi.impl.source.html.dtd.HtmlAttributeDescriptorImpl) XmlFile(com.intellij.psi.xml.XmlFile) HtmlTag(com.intellij.psi.html.HtmlTag) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiPresentableMetaData(com.intellij.psi.meta.PsiPresentableMetaData) XmlExtension(com.intellij.xml.XmlExtension) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) NamespaceAwareXmlAttributeDescriptor(com.intellij.xml.NamespaceAwareXmlAttributeDescriptor) NamespaceAwareXmlAttributeDescriptor(com.intellij.xml.NamespaceAwareXmlAttributeDescriptor) XmlAttributeImpl(com.intellij.psi.impl.source.xml.XmlAttributeImpl) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiFile(com.intellij.psi.PsiFile) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)89 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)54 XmlTag (com.intellij.psi.xml.XmlTag)46 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)32 XmlAttribute (com.intellij.psi.xml.XmlAttribute)18 NotNull (org.jetbrains.annotations.NotNull)11 PsiElement (com.intellij.psi.PsiElement)10 ArrayList (java.util.ArrayList)8 XmlFile (com.intellij.psi.xml.XmlFile)7 AnnotationBackedDescriptor (com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)6 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)6 Nullable (org.jetbrains.annotations.Nullable)6 Project (com.intellij.openapi.project.Project)5 HtmlTag (com.intellij.psi.html.HtmlTag)5 NamespaceAwareXmlAttributeDescriptor (com.intellij.xml.NamespaceAwareXmlAttributeDescriptor)5 AnyXmlAttributeDescriptor (com.intellij.xml.impl.schema.AnyXmlAttributeDescriptor)5 JavaFxPropertyAttributeDescriptor (org.jetbrains.plugins.javaFX.fxml.descriptors.JavaFxPropertyAttributeDescriptor)5 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)3 PsiReference (com.intellij.psi.PsiReference)3 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)2