Search in sources :

Example 96 with LookupElement

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

the class ReferenceExpressionCompletionContributor method completeFinalReference.

static Set<LookupElement> completeFinalReference(final PsiElement element, PsiJavaCodeReferenceElement reference, ElementFilter filter, final JavaSmartCompletionParameters parameters) {
    final Set<PsiField> used = parameters.getParameters().getInvocationCount() < 2 ? findConstantsUsedInSwitch(element) : Collections.<PsiField>emptySet();
    final Set<LookupElement> elements = JavaSmartCompletionContributor.completeReference(element, reference, new AndFilter(filter, new ElementFilter() {

        @Override
        public boolean isAcceptable(Object o, PsiElement context) {
            if (o instanceof CandidateInfo) {
                final CandidateInfo info = (CandidateInfo) o;
                final PsiElement member = info.getElement();
                final PsiType expectedType = parameters.getExpectedType();
                if (expectedType.equals(PsiType.VOID)) {
                    return member instanceof PsiMethod;
                }
                //noinspection SuspiciousMethodCalls
                if (member instanceof PsiEnumConstant && used.contains(CompletionUtil.getOriginalOrSelf(member))) {
                    return false;
                }
                return AssignableFromFilter.isAcceptable(member, element, expectedType, info.getSubstitutor());
            }
            return false;
        }

        @Override
        public boolean isClassAcceptable(Class hintClass) {
            return true;
        }
    }), false, true, parameters.getParameters(), PrefixMatcher.ALWAYS_TRUE);
    for (LookupElement lookupElement : elements) {
        if (lookupElement.getObject() instanceof PsiMethod) {
            final JavaMethodCallElement item = lookupElement.as(JavaMethodCallElement.CLASS_CONDITION_KEY);
            if (item != null) {
                final PsiMethod method = (PsiMethod) lookupElement.getObject();
                if (SmartCompletionDecorator.hasUnboundTypeParams(method, parameters.getExpectedType())) {
                    item.setInferenceSubstitutor(SmartCompletionDecorator.calculateMethodReturnTypeSubstitutor(method, parameters.getExpectedType()), element);
                }
            }
        }
    }
    return elements;
}
Also used : CandidateInfo(com.intellij.psi.infos.CandidateInfo) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 97 with LookupElement

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

the class SlowerTypeConversions method run.

@Override
public void run() {
    final Set<Pair<LookupElement, String>> processedChains = ContainerUtil.newHashSet();
    for (final LookupElement item : myBase) {
        addSecondCompletionVariants(myElement, myReference, item, myParameters, lookupElement -> {
            ContainerUtil.addIfNotNull(processedChains, chainInfo(lookupElement));
            myResult.consume(lookupElement);
        });
    }
    if (!psiElement().afterLeaf(".").accepts(myElement)) {
        BasicExpressionCompletionContributor.processDataflowExpressionTypes(myElement, null, TRUE_MATCHER, baseItem -> addSecondCompletionVariants(myElement, myReference, baseItem, myParameters, lookupElement -> {
            if (!processedChains.contains(chainInfo(lookupElement))) {
                myResult.consume(lookupElement);
            }
        }));
    }
}
Also used : Nullable(org.jetbrains.annotations.Nullable) ReferenceExpressionCompletionContributor.getSpace(com.intellij.codeInsight.completion.ReferenceExpressionCompletionContributor.getSpace) IncorrectOperationException(com.intellij.util.IncorrectOperationException) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Pair(com.intellij.openapi.util.Pair) Set(java.util.Set) PsiJavaPatterns.psiElement(com.intellij.patterns.PsiJavaPatterns.psiElement) com.intellij.psi(com.intellij.psi) CodeStyleSettingsManager(com.intellij.psi.codeStyle.CodeStyleSettingsManager) ContainerUtil(com.intellij.util.containers.ContainerUtil) NotNull(org.jetbrains.annotations.NotNull) Consumer(com.intellij.util.Consumer) LookupElement(com.intellij.codeInsight.lookup.LookupElement) Pair(com.intellij.openapi.util.Pair)

Example 98 with LookupElement

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

the class JavaCharFilter method acceptChar.

@Override
public Result acceptChar(char c, final int prefixLength, final Lookup lookup) {
    if (!lookup.getPsiFile().getLanguage().isKindOf(JavaLanguage.INSTANCE)) {
        return null;
    }
    LookupElement item = lookup.getCurrentItem();
    if (item == null || !item.isValid())
        return null;
    final Object o = item.getObject();
    if (c == '!') {
        if (o instanceof PsiVariable) {
            if (PsiType.BOOLEAN.isAssignableFrom(((PsiVariable) o).getType()))
                return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
        }
        if (o instanceof PsiMethod) {
            final PsiType type = ((PsiMethod) o).getReturnType();
            if (type != null && PsiType.BOOLEAN.isAssignableFrom(type))
                return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
        }
        if (o instanceof PsiKeyword && ((PsiKeyword) o).textMatches(PsiKeyword.INSTANCEOF)) {
            return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
        }
        return null;
    }
    if (c == '.' && isWithinLiteral(lookup))
        return Result.ADD_TO_PREFIX;
    if (c == ':') {
        PsiFile file = lookup.getPsiFile();
        PsiDocumentManager.getInstance(file.getProject()).commitDocument(lookup.getEditor().getDocument());
        PsiElement leaf = file.findElementAt(lookup.getEditor().getCaretModel().getOffset() - 1);
        if (PsiUtil.getLanguageLevel(file).isAtLeast(LanguageLevel.JDK_1_8)) {
            PsiStatement statement = PsiTreeUtil.getParentOfType(leaf, PsiStatement.class);
            if (statement == null || statement.getTextRange().getStartOffset() != leaf.getTextRange().getStartOffset()) {
                // not typing a statement label
                return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
            }
        }
        if (PsiTreeUtil.getParentOfType(leaf, PsiSwitchLabelStatement.class) != null || PsiTreeUtil.getParentOfType(leaf, PsiConditionalExpression.class) != null) {
            return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
        }
        return Result.HIDE_LOOKUP;
    }
    if (c == '[' || c == ']' || c == ')' || c == '>')
        return CharFilter.Result.SELECT_ITEM_AND_FINISH_LOOKUP;
    if (c == '<' && o instanceof PsiClass)
        return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
    if (c == '(') {
        if (o instanceof PsiClass) {
            if (PsiJavaPatterns.psiElement().afterLeaf(PsiKeyword.NEW).accepts(lookup.getPsiElement())) {
                return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
            }
            return Result.HIDE_LOOKUP;
        }
        if (o instanceof PsiType) {
            return Result.HIDE_LOOKUP;
        }
    }
    if ((c == ',' || c == '=') && o instanceof PsiVariable) {
        int lookupStart = lookup.getLookupStart();
        String name = ((PsiVariable) o).getName();
        if (lookupStart >= 0 && name != null && name.equals(lookup.itemPattern(item))) {
            return Result.HIDE_LOOKUP;
        }
    }
    if (c == '#' && PsiTreeUtil.getParentOfType(lookup.getPsiElement(), PsiDocComment.class) != null) {
        if (o instanceof PsiClass) {
            return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
        }
    }
    if (c == '(' && PsiKeyword.RETURN.equals(item.getLookupString())) {
        return Result.HIDE_LOOKUP;
    }
    return null;
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 99 with LookupElement

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

the class JavaMethodMergingContributor method handleAutoCompletionPossibility.

@Override
public AutoCompletionDecision handleAutoCompletionPossibility(@NotNull AutoCompletionContext context) {
    final CompletionParameters parameters = context.getParameters();
    if (parameters.getCompletionType() != CompletionType.SMART && parameters.getCompletionType() != CompletionType.BASIC) {
        return null;
    }
    if (Registry.is("java.completion.argument.live.template")) {
        return null;
    }
    final LookupElement[] items = context.getItems();
    if (items.length > 1) {
        String commonName = null;
        final ArrayList<PsiMethod> allMethods = new ArrayList<>();
        for (LookupElement item : items) {
            Object o = item.getPsiElement();
            if (item.getUserData(JavaCompletionUtil.FORCE_SHOW_SIGNATURE_ATTR) != null || !(o instanceof PsiMethod)) {
                return AutoCompletionDecision.SHOW_LOOKUP;
            }
            final PsiMethod method = (PsiMethod) o;
            final JavaChainLookupElement chain = item.as(JavaChainLookupElement.CLASS_CONDITION_KEY);
            final String name = method.getName() + "#" + (chain == null ? "" : chain.getQualifier().getLookupString());
            if (commonName != null && !commonName.equals(name)) {
                return AutoCompletionDecision.SHOW_LOOKUP;
            }
            commonName = name;
            allMethods.add(method);
        }
        for (LookupElement item : items) {
            JavaCompletionUtil.putAllMethods(item, allMethods);
        }
        return AutoCompletionDecision.insertItem(findBestOverload(items));
    }
    return super.handleAutoCompletionPossibility(context);
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) ArrayList(java.util.ArrayList) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 100 with LookupElement

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

the class JavaNoVariantsDelegator method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull final CompletionParameters parameters, @NotNull final CompletionResultSet result) {
    if (JavaModuleCompletion.isModuleFile(parameters.getOriginalFile())) {
        return;
    }
    final JavaCompletionSession session = new JavaCompletionSession(result);
    ResultTracker tracker = new ResultTracker(result) {

        @Override
        public void consume(CompletionResult plainResult) {
            super.consume(plainResult);
            LookupElement element = plainResult.getLookupElement();
            Object o = element.getObject();
            if (o instanceof PsiClass) {
                session.registerClass((PsiClass) o);
            }
            if (element instanceof TypeArgumentCompletionProvider.TypeArgsLookupElement) {
                ((TypeArgumentCompletionProvider.TypeArgsLookupElement) element).registerSingleClass(session);
            }
        }
    };
    result.runRemainingContributors(parameters, tracker);
    final boolean empty = tracker.containsOnlyPackages || suggestAllAnnotations(parameters);
    if (JavaCompletionContributor.isClassNamePossible(parameters) && !JavaCompletionContributor.mayStartClassName(result)) {
        result.restartCompletionOnAnyPrefixChange();
    }
    if (empty) {
        delegate(parameters, JavaCompletionSorting.addJavaSorting(parameters, result), session);
    } else if (Registry.is("ide.completion.show.better.matching.classes")) {
        if (parameters.getCompletionType() == CompletionType.BASIC && parameters.getInvocationCount() <= 1 && JavaCompletionContributor.mayStartClassName(result) && JavaCompletionContributor.isClassNamePossible(parameters) && !areNonImportedInheritorsAlreadySuggested(parameters)) {
            suggestNonImportedClasses(parameters, JavaCompletionSorting.addJavaSorting(parameters, result.withPrefixMatcher(tracker.betterMatcher)), session);
        }
    }
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Aggregations

LookupElement (com.intellij.codeInsight.lookup.LookupElement)182 PsiElement (com.intellij.psi.PsiElement)32 NotNull (org.jetbrains.annotations.NotNull)32 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)19 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 PsiTypeLookupItem (com.intellij.codeInsight.lookup.PsiTypeLookupItem)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 com.intellij.psi (com.intellij.psi)6