Search in sources :

Example 6 with PsiPresentableMetaData

use of com.intellij.psi.meta.PsiPresentableMetaData 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

PsiPresentableMetaData (com.intellij.psi.meta.PsiPresentableMetaData)6 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)3 PsiElement (com.intellij.psi.PsiElement)3 PsiFile (com.intellij.psi.PsiFile)3 XmlFile (com.intellij.psi.xml.XmlFile)3 XmlTag (com.intellij.psi.xml.XmlTag)3 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)3 XmlExtension (com.intellij.xml.XmlExtension)3 PsiMetaOwner (com.intellij.psi.meta.PsiMetaOwner)2 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)2 EmptyResolveMessageProvider (com.intellij.codeInsight.daemon.EmptyResolveMessageProvider)1 LookupElement (com.intellij.codeInsight.lookup.LookupElement)1 LookupValueFactory (com.intellij.codeInsight.lookup.LookupValueFactory)1 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 LocalQuickFixProvider (com.intellij.codeInspection.LocalQuickFixProvider)1 AnnotationBackedDescriptor (com.intellij.lang.javascript.flex.AnnotationBackedDescriptor)1 ItemPresentation (com.intellij.navigation.ItemPresentation)1 NavigationItem (com.intellij.navigation.NavigationItem)1 PsiElementNavigationItem (com.intellij.navigation.PsiElementNavigationItem)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1