Search in sources :

Example 1 with PsiPresentableMetaData

use of com.intellij.psi.meta.PsiPresentableMetaData in project intellij-community by JetBrains.

the class PsiElement2UsageTargetAdapter method update.

private void update(PsiElement element) {
    if (element != null && element.isValid()) {
        final ItemPresentation presentation = ((NavigationItem) element).getPresentation();
        myIconOpen = presentation == null ? null : ComputableIcon.create(presentation, true);
        myIconClosed = presentation == null ? null : ComputableIcon.create(presentation, false);
        myPresentableText = presentation == null ? UsageViewUtil.createNodeText(element) : presentation.getPresentableText();
        if (myIconOpen == null || myIconClosed == null) {
            if (element instanceof PsiMetaOwner) {
                final PsiMetaOwner psiMetaOwner = (PsiMetaOwner) element;
                final PsiMetaData metaData = psiMetaOwner.getMetaData();
                if (metaData instanceof PsiPresentableMetaData) {
                    final PsiPresentableMetaData psiPresentableMetaData = (PsiPresentableMetaData) metaData;
                    if (myIconOpen == null)
                        myIconOpen = ComputableIcon.create(psiPresentableMetaData);
                    if (myIconClosed == null)
                        myIconClosed = ComputableIcon.create(psiPresentableMetaData);
                }
            } else if (element instanceof PsiFile) {
                final PsiFile psiFile = (PsiFile) element;
                final VirtualFile virtualFile = psiFile.getVirtualFile();
                if (virtualFile != null) {
                    myIconOpen = ComputableIcon.create(virtualFile);
                    myIconClosed = ComputableIcon.create(virtualFile);
                }
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiPresentableMetaData(com.intellij.psi.meta.PsiPresentableMetaData) NavigationItem(com.intellij.navigation.NavigationItem) PsiElementNavigationItem(com.intellij.navigation.PsiElementNavigationItem) PsiMetaData(com.intellij.psi.meta.PsiMetaData) ItemPresentation(com.intellij.navigation.ItemPresentation) PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner)

Example 2 with PsiPresentableMetaData

use of com.intellij.psi.meta.PsiPresentableMetaData in project android by JetBrains.

the class AndroidLayoutXmlTagNameProvider method addTagNameVariants.

@Override
public void addTagNameVariants(List<LookupElement> elements, @NotNull XmlTag tag, String prefix) {
    final PsiFile file = tag.getContainingFile();
    if (!(file instanceof XmlFile && LayoutDomFileDescription.isLayoutFile((XmlFile) file))) {
        // Only use this provider for Android layout files
        return;
    }
    XmlExtension xmlExtension = XmlExtension.getExtension(file);
    List<XmlElementDescriptor> variants = TagNameVariantCollector.getTagDescriptors(tag, NAMESPACES, null);
    // Find the framework widgets that have a support library alternative
    Set<String> supportAlternatives = Sets.newHashSet();
    for (XmlElementDescriptor descriptor : variants) {
        String qualifiedName = descriptor.getName(tag);
        if (qualifiedName.startsWith(ANDROID_SUPPORT_PKG_PREFIX)) {
            supportAlternatives.add(AndroidUtils.getUnqualifiedName(qualifiedName));
        }
    }
    final Set<String> addedNames = Sets.newHashSet();
    for (XmlElementDescriptor descriptor : variants) {
        String qualifiedName = descriptor.getName(tag);
        if (!addedNames.add(qualifiedName)) {
            continue;
        }
        final String simpleName = AndroidUtils.getUnqualifiedName(qualifiedName);
        // Creating LookupElementBuilder with PsiElement gives an ability to show documentation during completion time
        PsiElement declaration = descriptor.getDeclaration();
        LookupElementBuilder lookupElement = declaration == null ? LookupElementBuilder.create(qualifiedName) : LookupElementBuilder.create(declaration, qualifiedName);
        final boolean isDeprecated = isDeclarationDeprecated(declaration);
        if (isDeprecated) {
            lookupElement = lookupElement.withStrikeoutness(true);
        }
        if (simpleName != null) {
            lookupElement = lookupElement.withLookupString(simpleName);
        }
        // This statement preserves them in autocompletion.
        if (descriptor instanceof PsiPresentableMetaData) {
            lookupElement = lookupElement.withIcon(((PsiPresentableMetaData) descriptor).getIcon());
        }
        // Using insert handler is required for, e.g. automatic insertion of required fields in Android layout XMLs
        if (xmlExtension.useXmlTagInsertHandler()) {
            lookupElement = lookupElement.withInsertHandler(XmlTagInsertHandler.INSTANCE);
        }
        // Deprecated tag names are supposed to be shown below non-deprecated tags
        int priority = isDeprecated ? 10 : 100;
        if (simpleName == null) {
            if (supportAlternatives.contains(qualifiedName)) {
                // This component has a support library alternative so lower the priority so the support component is shown at the top.
                priority -= 1;
            } else {
                // The component doesn't have an alternative in the support library so push it to the top.
                priority += 10;
            }
        }
        elements.add(PrioritizedLookupElement.withPriority(lookupElement, priority));
    }
}
Also used : PsiPresentableMetaData(com.intellij.psi.meta.PsiPresentableMetaData) XmlExtension(com.intellij.xml.XmlExtension) XmlFile(com.intellij.psi.xml.XmlFile) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiFile(com.intellij.psi.PsiFile) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PsiPresentableMetaData

use of com.intellij.psi.meta.PsiPresentableMetaData in project intellij-plugins by JetBrains.

the class ResolveExternalInlineStyleSourceAction method findTargetIfStyleDeclarationOwner.

private Navigatable findTargetIfStyleDeclarationOwner(XmlTag parent) {
    int foundCount = 0;
    Navigatable target = null;
    for (XmlAttribute attribute : parent.getAttributes()) {
        XmlAttributeDescriptor descriptor = attribute.getDescriptor();
        // 8
        if (descriptor instanceof AnnotationBackedDescriptor) {
            String ourValue = properties.get(descriptor.getName());
            if (ourValue != null) {
                if (attribute.getDisplayValue().equals(ourValue)) {
                    foundCount++;
                    if (descriptor.getName().equals(targetStyleName)) {
                        target = (Navigatable) attribute;
                    }
                    if (foundCount == properties.size()) {
                        return target;
                    }
                }
            }
        }
    }
    for (XmlTag tag : parent.getSubTags()) {
        XmlElementDescriptor descriptor = tag.getDescriptor();
        if (descriptor instanceof AnnotationBackedDescriptor && ((PsiPresentableMetaData) descriptor).getTypeName().equals(FlexAnnotationNames.STYLE)) {
            String ourValue = properties.get(descriptor.getName());
            if (ourValue != null) {
                if (tag.getSubTags().length == 0 && tag.getValue().getTrimmedText().equals(ourValue)) {
                    foundCount++;
                    if (descriptor.getName().equals(targetStyleName)) {
                        target = (Navigatable) tag;
                    }
                    if (foundCount == properties.size()) {
                        return target;
                    }
                }
            }
        }
    }
    return null;
}
Also used : PsiPresentableMetaData(com.intellij.psi.meta.PsiPresentableMetaData) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) AnnotationBackedDescriptor(com.intellij.lang.javascript.flex.AnnotationBackedDescriptor) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) Navigatable(com.intellij.pom.Navigatable) XmlTag(com.intellij.psi.xml.XmlTag)

Example 4 with PsiPresentableMetaData

use of com.intellij.psi.meta.PsiPresentableMetaData in project intellij-community by JetBrains.

the class DefinitionReference method getVariants.

@Override
@NotNull
public Object[] getVariants() {
    final RngGrammar scope = getScope();
    if (scope == null) {
        return ResolveResult.EMPTY_ARRAY;
    }
    final Map<String, Set<Define>> map = DefinitionResolver.getAllVariants(scope);
    if (map == null || map.size() == 0)
        return ArrayUtil.EMPTY_OBJECT_ARRAY;
    return ContainerUtil.mapNotNull(map.values(), defines -> {
        final Define define = defines.iterator().next();
        if (defines.size() == 0) {
            return null;
        } else {
            final PsiElement element = define.getPsiElement();
            if (element != null) {
                final PsiPresentableMetaData data = (PsiPresentableMetaData) ((PsiMetaOwner) element).getMetaData();
                if (data != null) {
                    return LookupValueFactory.createLookupValue(data.getName(), data.getIcon());
                } else {
                    return define.getName();
                }
            } else {
                return define.getName();
            }
        }
    }).toArray();
}
Also used : PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner) PsiPresentableMetaData(com.intellij.psi.meta.PsiPresentableMetaData) ArrayUtil(com.intellij.util.ArrayUtil) ContainerUtil(com.intellij.util.containers.ContainerUtil) LookupValueFactory(com.intellij.codeInsight.lookup.LookupValueFactory) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) LocalQuickFixProvider(com.intellij.codeInspection.LocalQuickFixProvider) PsiReferenceBase(com.intellij.psi.PsiReferenceBase) RngParentRef(org.intellij.plugins.relaxNG.xml.dom.RngParentRef) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) Define(org.intellij.plugins.relaxNG.model.Define) XmlTag(com.intellij.psi.xml.XmlTag) GenericAttributeValue(com.intellij.util.xml.GenericAttributeValue) Set(java.util.Set) DefinitionResolver(org.intellij.plugins.relaxNG.model.resolve.DefinitionResolver) Nullable(org.jetbrains.annotations.Nullable) PsiElementResolveResult(com.intellij.psi.PsiElementResolveResult) ResolveResult(com.intellij.psi.ResolveResult) RngGrammar(org.intellij.plugins.relaxNG.xml.dom.RngGrammar) Function(com.intellij.util.Function) XmlElement(com.intellij.psi.xml.XmlElement) EmptyResolveMessageProvider(com.intellij.codeInsight.daemon.EmptyResolveMessageProvider) NotNull(org.jetbrains.annotations.NotNull) PsiPresentableMetaData(com.intellij.psi.meta.PsiPresentableMetaData) Set(java.util.Set) Define(org.intellij.plugins.relaxNG.model.Define) PsiMetaOwner(com.intellij.psi.meta.PsiMetaOwner) PsiElement(com.intellij.psi.PsiElement) RngGrammar(org.intellij.plugins.relaxNG.xml.dom.RngGrammar) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with PsiPresentableMetaData

use of com.intellij.psi.meta.PsiPresentableMetaData in project intellij-community by JetBrains.

the class DefaultXmlTagNameProvider method addTagNameVariants.

@Override
public void addTagNameVariants(List<LookupElement> elements, @NotNull XmlTag tag, String prefix) {
    final List<String> namespaces;
    if (prefix.isEmpty()) {
        namespaces = new ArrayList<>(Arrays.asList(tag.knownNamespaces()));
        // empty namespace
        namespaces.add(XmlUtil.EMPTY_URI);
    } else {
        namespaces = new ArrayList<>(Collections.singletonList(tag.getNamespace()));
    }
    PsiFile psiFile = tag.getContainingFile();
    XmlExtension xmlExtension = XmlExtension.getExtension(psiFile);
    List<String> nsInfo = new ArrayList<>();
    List<XmlElementDescriptor> variants = TagNameVariantCollector.getTagDescriptors(tag, namespaces, nsInfo);
    if (variants.isEmpty() && psiFile instanceof XmlFile && ((XmlFile) psiFile).getRootTag() == tag) {
        getRootTagsVariants(tag, elements);
        return;
    }
    final Set<String> visited = new HashSet<>();
    for (int i = 0; i < variants.size(); i++) {
        XmlElementDescriptor descriptor = variants.get(i);
        String qname = descriptor.getName(tag);
        if (!visited.add(qname))
            continue;
        if (!prefix.isEmpty() && qname.startsWith(prefix + ":")) {
            qname = qname.substring(prefix.length() + 1);
        }
        PsiElement declaration = descriptor.getDeclaration();
        if (declaration != null && !declaration.isValid()) {
            LOG.error(descriptor + " contains invalid declaration: " + declaration);
        }
        LookupElementBuilder lookupElement = declaration == null ? LookupElementBuilder.create(qname) : LookupElementBuilder.create(declaration, qname);
        final int separator = qname.indexOf(':');
        if (separator > 0) {
            lookupElement = lookupElement.withLookupString(qname.substring(separator + 1));
        }
        String ns = nsInfo.get(i);
        if (StringUtil.isNotEmpty(ns)) {
            lookupElement = lookupElement.withTypeText(ns, true);
        }
        if (descriptor instanceof PsiPresentableMetaData) {
            lookupElement = lookupElement.withIcon(((PsiPresentableMetaData) descriptor).getIcon());
        }
        if (xmlExtension.useXmlTagInsertHandler()) {
            lookupElement = lookupElement.withInsertHandler(XmlTagInsertHandler.INSTANCE);
        }
        lookupElement = lookupElement.withCaseSensitivity(!(descriptor instanceof HtmlElementDescriptorImpl));
        elements.add(PrioritizedLookupElement.withPriority(lookupElement, separator > 0 ? 0 : 1));
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) PsiPresentableMetaData(com.intellij.psi.meta.PsiPresentableMetaData) HtmlElementDescriptorImpl(com.intellij.psi.impl.source.html.dtd.HtmlElementDescriptorImpl) XmlExtension(com.intellij.xml.XmlExtension) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiFile(com.intellij.psi.PsiFile) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement)

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