Search in sources :

Example 61 with LookupElement

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

the class JavaTemplateUtil method addTypeLookupItem.

public static LookupElement addTypeLookupItem(Set<LookupElement> items, PsiType type) {
    final LookupElement item = PsiTypeLookupItem.createLookupItem(type, null);
    items.add(item);
    item.putUserData(TemplateLookupSelectionHandler.KEY_IN_LOOKUP_ITEM, new JavaTemplateLookupSelectionHandler());
    return item;
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 62 with LookupElement

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

the class JavaReflectionCompletionContributor method addAnnotationClasses.

private static void addAnnotationClasses(@NotNull PsiClass psiClass, boolean isDeclared, @NotNull CompletionResultSet result) {
    Set<PsiAnnotation> declaredAnnotations = isDeclared ? ContainerUtil.set(AnnotationUtil.getAllAnnotations(psiClass, false, null, false)) : null;
    PsiAnnotation[] annotations = AnnotationUtil.getAllAnnotations(psiClass, true, null, false);
    for (PsiAnnotation annotation : annotations) {
        PsiJavaCodeReferenceElement referenceElement = annotation.getNameReferenceElement();
        if (referenceElement != null) {
            PsiElement resolved = referenceElement.resolve();
            if (resolved instanceof PsiClass) {
                PsiClass annotationClass = (PsiClass) resolved;
                String className = annotationClass.getName();
                if (className != null) {
                    LookupElement lookupElement = LookupElementBuilder.createWithIcon(annotationClass).withPresentableText(className + ".class").withInsertHandler(JavaReflectionCompletionContributor::handleAnnotationClassInsertion);
                    if (isDeclared) {
                        lookupElement = withPriority(lookupElement, declaredAnnotations.contains(annotation));
                    }
                    result.addElement(lookupElement);
                }
            }
        }
    }
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 63 with LookupElement

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

the class JavaClassReference method processPackage.

@NotNull
private LookupElement[] processPackage(@NotNull PsiPackage aPackage) {
    final ArrayList<LookupElement> list = ContainerUtil.newArrayList();
    final int startOffset = StringUtil.isEmpty(aPackage.getName()) ? 0 : aPackage.getQualifiedName().length() + 1;
    final GlobalSearchScope scope = getScope(getJavaContextFile());
    for (final PsiPackage subPackage : aPackage.getSubPackages(scope)) {
        final String shortName = subPackage.getQualifiedName().substring(startOffset);
        if (PsiNameHelper.getInstance(subPackage.getProject()).isIdentifier(shortName)) {
            list.add(LookupElementBuilder.create(subPackage).withIcon(subPackage.getIcon(Iconable.ICON_FLAG_VISIBILITY)));
        }
    }
    final List<PsiClass> classes = ContainerUtil.filter(aPackage.getClasses(scope), psiClass -> StringUtil.isNotEmpty(psiClass.getName()));
    final Map<CustomizableReferenceProvider.CustomizationKey, Object> options = getOptions();
    if (options != null) {
        final boolean instantiatable = JavaClassReferenceProvider.INSTANTIATABLE.getBooleanValue(options);
        final boolean concrete = JavaClassReferenceProvider.CONCRETE.getBooleanValue(options);
        final boolean notInterface = JavaClassReferenceProvider.NOT_INTERFACE.getBooleanValue(options);
        final boolean notEnum = JavaClassReferenceProvider.NOT_ENUM.getBooleanValue(options);
        final ClassKind classKind = getClassKind();
        for (PsiClass clazz : classes) {
            if (isClassAccepted(clazz, classKind, instantiatable, concrete, notInterface, notEnum)) {
                list.add(JavaClassNameCompletionContributor.createClassLookupItem(clazz, false));
            }
        }
    } else {
        for (PsiClass clazz : classes) {
            list.add(JavaClassNameCompletionContributor.createClassLookupItem(clazz, false));
        }
    }
    return list.toArray(new LookupElement[list.size()]);
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ClassKind(com.intellij.psi.util.ClassKind) NotNull(org.jetbrains.annotations.NotNull)

Example 64 with LookupElement

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

the class FileReferenceCompletionImpl method getFileReferenceCompletionVariants.

@NotNull
@Override
public Object[] getFileReferenceCompletionVariants(final FileReference reference) {
    final String s = reference.getText();
    if (s != null && s.equals("/")) {
        return ArrayUtil.EMPTY_OBJECT_ARRAY;
    }
    final CommonProcessors.CollectUniquesProcessor<PsiFileSystemItem> collector = new CommonProcessors.CollectUniquesProcessor<>();
    final PsiElementProcessor<PsiFileSystemItem> processor = new PsiElementProcessor<PsiFileSystemItem>() {

        @Override
        public boolean execute(@NotNull PsiFileSystemItem fileSystemItem) {
            return new FilteringProcessor<>(reference.getFileReferenceSet().getReferenceCompletionFilter(), collector).process(FileReference.getOriginalFile(fileSystemItem));
        }
    };
    List<Object> additionalItems = ContainerUtil.newArrayList();
    for (PsiFileSystemItem context : reference.getContexts()) {
        for (final PsiElement child : context.getChildren()) {
            if (child instanceof PsiFileSystemItem) {
                processor.execute((PsiFileSystemItem) child);
            }
        }
        if (context instanceof FileReferenceResolver) {
            additionalItems.addAll(((FileReferenceResolver) context).getVariants(reference));
        }
    }
    final FileType[] types = reference.getFileReferenceSet().getSuitableFileTypes();
    final THashSet<PsiElement> set = new THashSet<>(collector.getResults(), VARIANTS_HASHING_STRATEGY);
    final PsiElement[] candidates = PsiUtilCore.toPsiElementArray(set);
    final Object[] variants = new Object[candidates.length + additionalItems.size()];
    for (int i = 0; i < candidates.length; i++) {
        PsiElement candidate = candidates[i];
        Object item = reference.createLookupItem(candidate);
        if (item == null) {
            item = FileInfoManager.getFileLookupItem(candidate);
        }
        if (candidate instanceof PsiFile && item instanceof LookupElement && types.length > 0 && ArrayUtil.contains(((PsiFile) candidate).getFileType(), types)) {
            item = PrioritizedLookupElement.withPriority((LookupElement) item, Double.MAX_VALUE);
        }
        variants[i] = item;
    }
    for (int i = 0; i < additionalItems.size(); i++) {
        variants[i + candidates.length] = additionalItems.get(i);
    }
    if (!reference.getFileReferenceSet().isUrlEncoded()) {
        return variants;
    }
    List<Object> encodedVariants = new ArrayList<>(variants.length + additionalItems.size());
    for (int i = 0; i < candidates.length; i++) {
        final PsiElement element = candidates[i];
        if (element instanceof PsiNamedElement) {
            final PsiNamedElement psiElement = (PsiNamedElement) element;
            String name = psiElement.getName();
            final String encoded = reference.encode(name, psiElement);
            if (encoded == null)
                continue;
            if (!encoded.equals(name)) {
                final Icon icon = psiElement.getIcon(Iconable.ICON_FLAG_READ_STATUS | Iconable.ICON_FLAG_VISIBILITY);
                LookupElementBuilder item = FileInfoManager.getFileLookupItem(candidates[i], encoded, icon);
                encodedVariants.add(item.withTailText(" (" + name + ")"));
            } else {
                encodedVariants.add(variants[i]);
            }
        }
    }
    encodedVariants.addAll(additionalItems);
    return ArrayUtil.toObjectArray(encodedVariants);
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiFile(com.intellij.psi.PsiFile) CommonProcessors(com.intellij.util.CommonProcessors) PsiElement(com.intellij.psi.PsiElement) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) THashSet(gnu.trove.THashSet) FileType(com.intellij.openapi.fileTypes.FileType) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 65 with LookupElement

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

the class DefaultTextCompletionValueDescriptor method createLookupBuilder.

@NotNull
@Override
public LookupElementBuilder createLookupBuilder(@NotNull T item) {
    LookupElementBuilder builder = LookupElementBuilder.create(item, getLookupString(item)).withIcon(getIcon(item));
    InsertHandler<LookupElement> handler = createInsertHandler(item);
    if (handler != null) {
        builder = builder.withInsertHandler(handler);
    }
    String tailText = getTailText(item);
    if (tailText != null) {
        builder = builder.withTailText(tailText, true);
    }
    String typeText = getTypeText(item);
    if (typeText != null) {
        builder = builder.withTypeText(typeText);
    }
    return builder;
}
Also used : LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

LookupElement (com.intellij.codeInsight.lookup.LookupElement)183 PsiElement (com.intellij.psi.PsiElement)33 NotNull (org.jetbrains.annotations.NotNull)32 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)20 Nullable (org.jetbrains.annotations.Nullable)17 ArrayList (java.util.ArrayList)14 Project (com.intellij.openapi.project.Project)12 LookupElementPresentation (com.intellij.codeInsight.lookup.LookupElementPresentation)10 Document (com.intellij.openapi.editor.Document)10 PsiFile (com.intellij.psi.PsiFile)10 Editor (com.intellij.openapi.editor.Editor)9 THashSet (gnu.trove.THashSet)8 LinkedHashSet (java.util.LinkedHashSet)8 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)7 XmlTag (com.intellij.psi.xml.XmlTag)7 Consumer (com.intellij.util.Consumer)7 HashSet (java.util.HashSet)7 InsertionContext (com.intellij.codeInsight.completion.InsertionContext)6 PsiTypeLookupItem (com.intellij.codeInsight.lookup.PsiTypeLookupItem)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6