Search in sources :

Example 31 with LookupElementBuilder

use of com.intellij.codeInsight.lookup.LookupElementBuilder in project android by JetBrains.

the class AndroidCompletionContributor method addDesignTimeAttributes.

/**
   * For every regular layout element attribute, add it with "tools:" prefix
   * (or whatever user uses for tools namespace)
   * <p/>
   * <a href="https://developer.android.com/studio/write/tool-attributes.html#design-time_view_attributes">Designtime attributes docs</a>
   */
private static void addDesignTimeAttributes(@NotNull final String namespacePrefix, @NotNull final PsiElement psiElement, @NotNull final AndroidFacet facet, @NotNull final XmlAttribute attribute, @NotNull final CompletionResultSet resultSet) {
    final XmlTag tag = attribute.getParent();
    final DomElement element = DomManager.getDomManager(tag.getProject()).getDomElement(tag);
    final Set<XmlName> registeredAttributes = new HashSet<>();
    if (element instanceof LayoutElement) {
        AttributeProcessingUtil.processLayoutAttributes(facet, tag, (LayoutElement) element, registeredAttributes, (xmlName, attrDef, parentStyleableName) -> {
            if (SdkConstants.ANDROID_URI.equals(xmlName.getNamespaceKey())) {
                final String localName = xmlName.getLocalName();
                // Lookup string is something that would be inserted when attribute is completed, so we want to use
                // local name as an argument of .create(), otherwise we'll end up with getting completions like
                // "tools:tools:src". However, we want to show "tools:" prefix in the completion list, and for that
                // .withPresentableText is used
                final LookupElementBuilder lookupElement = LookupElementBuilder.create(psiElement, localName).withInsertHandler(XmlAttributeInsertHandler.INSTANCE).withPresentableText(namespacePrefix + ":" + localName);
                resultSet.addElement(lookupElement);
            }
            return null;
        });
    }
}
Also used : LayoutElement(org.jetbrains.android.dom.layout.LayoutElement) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) HashSet(com.intellij.util.containers.HashSet)

Example 32 with LookupElementBuilder

use of com.intellij.codeInsight.lookup.LookupElementBuilder in project android by JetBrains.

the class AndroidCompletionContributor method addAndroidPrefixElement.

private static void addAndroidPrefixElement(PsiElement position, PsiElement parent, CompletionResultSet resultSet) {
    if (position.getText().startsWith(SdkConstants.ANDROID_NS_NAME_PREFIX)) {
        return;
    }
    final PsiElement grandparent = parent.getParent();
    if (!(grandparent instanceof XmlTag)) {
        return;
    }
    final DomElement element = DomManager.getDomManager(grandparent.getProject()).getDomElement((XmlTag) grandparent);
    if (!(element instanceof LayoutElement) && !(element instanceof PreferenceElement)) {
        return;
    }
    final String prefix = ((XmlTag) grandparent).getPrefixByNamespace(SdkConstants.NS_RESOURCES);
    if (prefix == null || prefix.length() < 3) {
        return;
    }
    final LookupElementBuilder e = LookupElementBuilder.create(prefix + ":").withTypeText("[Namespace Prefix]", true);
    resultSet.addElement(PrioritizedLookupElement.withPriority(e, Double.MAX_VALUE));
}
Also used : LayoutElement(org.jetbrains.android.dom.layout.LayoutElement) PreferenceElement(org.jetbrains.android.dom.xml.PreferenceElement) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiElement(com.intellij.psi.PsiElement)

Example 33 with LookupElementBuilder

use of com.intellij.codeInsight.lookup.LookupElementBuilder 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 34 with LookupElementBuilder

use of com.intellij.codeInsight.lookup.LookupElementBuilder in project android by JetBrains.

the class ResourceReferenceConverter method createLookupElement.

@Nullable
@Override
public LookupElement createLookupElement(ResourceValue resourceValue) {
    final String value = resourceValue.toString();
    boolean deprecated = false;
    String doc = null;
    if (myAttributeDefinition != null) {
        doc = myAttributeDefinition.getValueDoc(value);
        deprecated = myAttributeDefinition.isValueDeprecated(value);
    }
    LookupElementBuilder builder;
    if (doc == null) {
        builder = LookupElementBuilder.create(value);
    } else {
        builder = LookupElementBuilder.create(new DocumentationHolder(value, doc.trim()), value);
    }
    builder = builder.withCaseSensitivity(true).withStrikeoutness(deprecated);
    final String resourceName = resourceValue.getResourceName();
    if (resourceName != null) {
        builder = builder.withLookupString(resourceName);
    }
    final int priority;
    if (deprecated) {
        // Show deprecated values in the end of the autocompletion list
        priority = 0;
    } else if (TOP_PRIORITY_VALUES.contains(value)) {
        // http://b.android.com/189164
        // match_parent and wrap_content are shown higher in the list of autocompletions, if they're available
        priority = 2;
    } else {
        priority = 1;
    }
    return PrioritizedLookupElement.withPriority(builder, priority);
}
Also used : LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 35 with LookupElementBuilder

use of com.intellij.codeInsight.lookup.LookupElementBuilder in project android by JetBrains.

the class OnClickConverter method createLookupElement.

private static LookupElement createLookupElement(PsiMethod method) {
    final LookupElementBuilder builder = LookupElementBuilder.create(method, method.getName()).withIcon(method.getIcon(Iconable.ICON_FLAG_VISIBILITY)).withPresentableText(method.getName());
    final PsiClass containingClass = method.getContainingClass();
    return containingClass != null ? builder.withTailText(" (" + containingClass.getQualifiedName() + ')') : builder;
}
Also used : LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder)

Aggregations

LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)57 LookupElement (com.intellij.codeInsight.lookup.LookupElement)20 NotNull (org.jetbrains.annotations.NotNull)16 PsiElement (com.intellij.psi.PsiElement)13 Nullable (org.jetbrains.annotations.Nullable)9 PsiFile (com.intellij.psi.PsiFile)7 Document (com.intellij.openapi.editor.Document)5 ArrayList (java.util.ArrayList)5 PlainPrefixMatcher (com.intellij.codeInsight.completion.PlainPrefixMatcher)3 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)3 Editor (com.intellij.openapi.editor.Editor)3 TextRange (com.intellij.openapi.util.TextRange)3 PsiPresentableMetaData (com.intellij.psi.meta.PsiPresentableMetaData)3 XmlFile (com.intellij.psi.xml.XmlFile)3 XmlExtension (com.intellij.xml.XmlExtension)3 com.intellij.codeInsight.completion (com.intellij.codeInsight.completion)2 CompletionResultSet (com.intellij.codeInsight.completion.CompletionResultSet)2 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)2 ParenthesesInsertHandler (com.intellij.codeInsight.completion.util.ParenthesesInsertHandler)2 AutoCompletionPolicy (com.intellij.codeInsight.lookup.AutoCompletionPolicy)2