Search in sources :

Example 46 with LookupElementBuilder

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

the class XmlCompletionContributor method buildEntityLookupItem.

@Nullable
private static LookupElementBuilder buildEntityLookupItem(@NotNull final XmlEntityDecl decl) {
    final String name = decl.getName();
    if (name == null) {
        return null;
    }
    final LookupElementBuilder result = LookupElementBuilder.create(name).withInsertHandler(ENTITY_INSERT_HANDLER);
    final XmlAttributeValue value = decl.getValueElement();
    final ASTNode node = value.getNode();
    if (node != null) {
        final ASTNode[] nodes = node.getChildren(TokenSet.create(XmlTokenType.XML_CHAR_ENTITY_REF));
        if (nodes.length == 1) {
            final String valueText = nodes[0].getText();
            final int i = valueText.indexOf('#');
            if (i > 0) {
                String s = valueText.substring(i + 1);
                s = StringUtil.trimEnd(s, ";");
                try {
                    final int unicodeChar = Integer.valueOf(s).intValue();
                    return result.withTypeText(String.valueOf((char) unicodeChar));
                } catch (NumberFormatException e) {
                    return result;
                }
            }
        }
    }
    return result;
}
Also used : ASTNode(com.intellij.lang.ASTNode) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) Nullable(org.jetbrains.annotations.Nullable)

Example 47 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 48 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 49 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)

Example 50 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)

Aggregations

LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)53 LookupElement (com.intellij.codeInsight.lookup.LookupElement)19 NotNull (org.jetbrains.annotations.NotNull)16 PsiElement (com.intellij.psi.PsiElement)12 Nullable (org.jetbrains.annotations.Nullable)9 PsiFile (com.intellij.psi.PsiFile)6 Document (com.intellij.openapi.editor.Document)5 ArrayList (java.util.ArrayList)4 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 ParenthesesInsertHandler (com.intellij.codeInsight.completion.util.ParenthesesInsertHandler)2 AutoCompletionPolicy (com.intellij.codeInsight.lookup.AutoCompletionPolicy)2 AllIcons (com.intellij.icons.AllIcons)2 FileType (com.intellij.openapi.fileTypes.FileType)2