Search in sources :

Example 1 with VariableLookupItem

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

the class MethodsChainLookupRangingHelper method processMethod.

@Nullable
private static MethodProcResult processMethod(@NotNull final PsiMethod method, @Nullable final PsiClass qualifierClass, final boolean isHeadMethod, final int weight, final ChainCompletionContext context, final CachedRelevantStaticMethodSearcher staticMethodSearcher, final NullableNotNullManager nullableNotNullManager) {
    int unreachableParametersCount = 0;
    int notMatchedStringVars = 0;
    int matchedParametersInContext = 0;
    boolean hasCallingVariableInContext = false;
    boolean introduceNewVariable = false;
    final PsiParameterList parameterList = method.getParameterList();
    final TIntObjectHashMap<SubLookupElement> parametersMap = new TIntObjectHashMap<>(parameterList.getParametersCount());
    final PsiParameter[] parameters = parameterList.getParameters();
    for (int i = 0; i < parameters.length; i++) {
        final PsiParameter parameter = parameters[i];
        final String typeQName = parameter.getType().getCanonicalText();
        if (JAVA_LANG_STRING.equals(typeQName)) {
            final PsiVariable relevantStringVar = context.findRelevantStringInContext(parameter.getName());
            if (relevantStringVar == null) {
                notMatchedStringVars++;
            } else {
                parametersMap.put(i, new VariableSubLookupElement(relevantStringVar));
            }
        } else if (!ChainCompletionStringUtil.isPrimitiveOrArrayOfPrimitives(typeQName)) {
            final Collection<PsiVariable> contextVariables = context.getVariables(typeQName);
            final PsiVariable contextVariable = ContainerUtil.getFirstItem(contextVariables, null);
            if (contextVariable != null) {
                if (contextVariables.size() == 1)
                    parametersMap.put(i, new VariableSubLookupElement(contextVariable));
                matchedParametersInContext++;
                continue;
            }
            final Collection<ContextRelevantVariableGetter> relevantVariablesGetters = context.getRelevantVariablesGetters(typeQName);
            final ContextRelevantVariableGetter contextVariableGetter = ContainerUtil.getFirstItem(relevantVariablesGetters, null);
            if (contextVariableGetter != null) {
                if (relevantVariablesGetters.size() == 1)
                    parametersMap.put(i, contextVariableGetter.createSubLookupElement());
                matchedParametersInContext++;
                continue;
            }
            final Collection<PsiMethod> containingClassMethods = context.getContainingClassMethods(typeQName);
            final PsiMethod contextRelevantGetter = ContainerUtil.getFirstItem(containingClassMethods, null);
            if (contextRelevantGetter != null) {
                if (containingClassMethods.size() == 1)
                    parametersMap.put(i, new GetterLookupSubLookupElement(method.getName()));
                matchedParametersInContext++;
                continue;
            }
            final ContextRelevantStaticMethod contextRelevantStaticMethod = ContainerUtil.getFirstItem(staticMethodSearcher.getRelevantStaticMethods(typeQName, weight), null);
            if (contextRelevantStaticMethod != null) {
                //
                // In most cases it is not really relevant
                //
                //parametersMap.put(i, contextRelevantStaticMethod.createLookupElement());
                matchedParametersInContext++;
                continue;
            }
            if (!nullableNotNullManager.isNullable(parameter, true)) {
                unreachableParametersCount++;
            }
        }
    }
    final LookupElement lookupElement;
    if (isHeadMethod) {
        if (method.hasModifierProperty(PsiModifier.STATIC)) {
            hasCallingVariableInContext = true;
            lookupElement = createLookupElement(method, parametersMap);
        } else if (method.isConstructor()) {
            return null;
        } else {
            @SuppressWarnings("ConstantConditions") final String classQName = qualifierClass.getQualifiedName();
            if (classQName == null)
                return null;
            final Object e = ContainerUtil.getFirstItem(context.getContextRefElements(classQName), null);
            if (e != null) {
                final LookupElement firstChainElement;
                if (e instanceof PsiVariable) {
                    firstChainElement = new VariableLookupItem((PsiVariable) e);
                } else if (e instanceof PsiMethod) {
                    firstChainElement = createLookupElement((PsiMethod) e, null);
                } else if (e instanceof LookupElement) {
                    firstChainElement = (LookupElement) e;
                } else {
                    throw new AssertionError();
                }
                hasCallingVariableInContext = true;
                lookupElement = new JavaChainLookupElement(firstChainElement, createLookupElement(method, parametersMap));
            } else {
                lookupElement = createLookupElement(method, parametersMap);
                if (!context.getContainingClassQNames().contains(classQName)) {
                    introduceNewVariable = true;
                }
            }
        }
    } else {
        lookupElement = createLookupElement(method, parametersMap);
    }
    return new MethodProcResult(lookupElement, unreachableParametersCount, notMatchedStringVars, hasCallingVariableInContext, introduceNewVariable, matchedParametersInContext);
}
Also used : VariableSubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.VariableSubLookupElement) ContextRelevantVariableGetter(com.intellij.compiler.classFilesIndex.chainsSearch.context.ContextRelevantVariableGetter) SubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.SubLookupElement) GetterLookupSubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.GetterLookupSubLookupElement) VariableSubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.VariableSubLookupElement) JavaChainLookupElement(com.intellij.codeInsight.completion.JavaChainLookupElement) SubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.SubLookupElement) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ChainCompletionLookupElementUtil.createLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.ChainCompletionLookupElementUtil.createLookupElement) WeightableChainLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.WeightableChainLookupElement) ChainCompletionNewVariableLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.ChainCompletionNewVariableLookupElement) GetterLookupSubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.GetterLookupSubLookupElement) VariableSubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.VariableSubLookupElement) JavaChainLookupElement(com.intellij.codeInsight.completion.JavaChainLookupElement) VariableLookupItem(com.intellij.codeInsight.lookup.VariableLookupItem) GetterLookupSubLookupElement(com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.GetterLookupSubLookupElement) TIntObjectHashMap(gnu.trove.TIntObjectHashMap) Collection(java.util.Collection) ContextRelevantStaticMethod(com.intellij.compiler.classFilesIndex.chainsSearch.context.ContextRelevantStaticMethod) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with VariableLookupItem

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

the class JavaStaticMemberProcessor method createLookupElement.

@NotNull
@Override
protected LookupElement createLookupElement(@NotNull PsiMember member, @NotNull final PsiClass containingClass, boolean shouldImport) {
    shouldImport |= myOriginalPosition != null && PsiTreeUtil.isAncestor(containingClass, myOriginalPosition, false);
    String exprText = member.getName() + (member instanceof PsiMethod ? "()" : "");
    PsiReference ref = JavaPsiFacade.getElementFactory(member.getProject()).createExpressionFromText(exprText, myOriginalPosition).findReferenceAt(0);
    if (ref instanceof PsiReferenceExpression && ((PsiReferenceExpression) ref).multiResolve(true).length > 0) {
        shouldImport = false;
    }
    if (member instanceof PsiMethod) {
        return AutoCompletionPolicy.NEVER_AUTOCOMPLETE.applyPolicy(new GlobalMethodCallElement((PsiMethod) member, shouldImport, false));
    }
    return AutoCompletionPolicy.NEVER_AUTOCOMPLETE.applyPolicy(new VariableLookupItem((PsiField) member, shouldImport) {

        @Override
        public void handleInsert(InsertionContext context) {
            FeatureUsageTracker.getInstance().triggerFeatureUsed(JavaCompletionFeatures.GLOBAL_MEMBER_NAME);
            super.handleInsert(context);
        }
    });
}
Also used : VariableLookupItem(com.intellij.codeInsight.lookup.VariableLookupItem) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with VariableLookupItem

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

the class BasicExpressionCompletionContributor method expressionToLookupElement.

@NotNull
private static LookupElement expressionToLookupElement(@NotNull PsiExpression expression) {
    if (expression instanceof PsiReferenceExpression) {
        final PsiReferenceExpression refExpr = (PsiReferenceExpression) expression;
        if (!refExpr.isQualified()) {
            final PsiElement target = refExpr.resolve();
            if (target instanceof PsiVariable) {
                final VariableLookupItem item = new VariableLookupItem((PsiVariable) target);
                item.setSubstitutor(PsiSubstitutor.EMPTY);
                return item;
            }
        }
    }
    if (expression instanceof PsiMethodCallExpression) {
        final PsiMethodCallExpression call = (PsiMethodCallExpression) expression;
        if (!call.getMethodExpression().isQualified()) {
            final PsiMethod method = call.resolveMethod();
            if (method != null) {
                return new JavaMethodCallElement(method);
            }
        }
    }
    return new ExpressionLookupItem(expression);
}
Also used : VariableLookupItem(com.intellij.codeInsight.lookup.VariableLookupItem) ExpressionLookupItem(com.intellij.codeInsight.lookup.ExpressionLookupItem) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with VariableLookupItem

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

the class MagicCompletionContributor method addCompletionVariants.

private static void addCompletionVariants(@NotNull final CompletionParameters parameters, @NotNull final CompletionResultSet result, PsiElement pos, MagicConstantInspection.AllowedValues allowedValues) {
    final Set<PsiElement> allowed = new THashSet<>(new TObjectHashingStrategy<PsiElement>() {

        @Override
        public int computeHashCode(PsiElement object) {
            return 0;
        }

        @Override
        public boolean equals(PsiElement o1, PsiElement o2) {
            return parameters.getOriginalFile().getManager().areElementsEquivalent(o1, o2);
        }
    });
    if (allowedValues.canBeOred) {
        PsiElementFactory factory = JavaPsiFacade.getElementFactory(pos.getProject());
        PsiExpression zero = factory.createExpressionFromText("0", pos);
        result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(zero, "0"), PRIORITY - 1));
        PsiExpression minusOne = factory.createExpressionFromText("-1", pos);
        result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(minusOne, "-1"), PRIORITY - 1));
        allowed.add(zero);
        allowed.add(minusOne);
    }
    List<ExpectedTypeInfo> types = Arrays.asList(JavaSmartCompletionContributor.getExpectedTypes(parameters));
    for (PsiAnnotationMemberValue value : allowedValues.values) {
        if (value instanceof PsiReference) {
            PsiElement resolved = ((PsiReference) value).resolve();
            if (resolved instanceof PsiNamedElement) {
                LookupElement lookupElement = LookupItemUtil.objectToLookupItem(resolved);
                if (lookupElement instanceof VariableLookupItem) {
                    ((VariableLookupItem) lookupElement).setSubstitutor(PsiSubstitutor.EMPTY);
                }
                LookupElement element = PrioritizedLookupElement.withPriority(lookupElement, PRIORITY);
                element = decorate(parameters, types, element);
                result.addElement(element);
                allowed.add(resolved);
                continue;
            }
        }
        LookupElement element = LookupElementBuilder.create(value, value.getText());
        element = decorate(parameters, types, element);
        result.addElement(element);
        allowed.add(value);
    }
    result.runRemainingContributors(parameters, completionResult -> {
        LookupElement element = completionResult.getLookupElement();
        Object object = element.getObject();
        if (object instanceof PsiElement && allowed.contains(object)) {
            return;
        }
        result.passResult(completionResult);
    });
}
Also used : LookupElement(com.intellij.codeInsight.lookup.LookupElement) THashSet(gnu.trove.THashSet) ExpectedTypeInfo(com.intellij.codeInsight.ExpectedTypeInfo) VariableLookupItem(com.intellij.codeInsight.lookup.VariableLookupItem)

Example 5 with VariableLookupItem

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

the class ResourceTypeCompletionContributor method fillCompletionVariants.

@Override
public void fillCompletionVariants(@NotNull final CompletionParameters parameters, @NotNull final CompletionResultSet result) {
    //if (parameters.getCompletionType() != CompletionType.SMART) return;
    PsiElement pos = parameters.getPosition();
    if (JavaKeywordCompletion.AFTER_DOT.accepts(pos)) {
        return;
    }
    AndroidFacet facet = AndroidFacet.getInstance(pos);
    if (facet == null) {
        return;
    }
    ResourceTypeInspection.Constraints allowedValues = getAllowedValues(pos);
    if (allowedValues == null)
        return;
    final Set<PsiElement> allowed = new THashSet<PsiElement>(new TObjectHashingStrategy<PsiElement>() {

        @Override
        public int computeHashCode(PsiElement object) {
            return 0;
        }

        @Override
        public boolean equals(PsiElement o1, PsiElement o2) {
            return parameters.getOriginalFile().getManager().areElementsEquivalent(o1, o2);
        }
    });
    // Suggest resource types
    if (allowedValues instanceof ResourceTypeInspection.ResourceTypeAllowedValues) {
        for (ResourceType resourceType : ((ResourceTypeInspection.ResourceTypeAllowedValues) allowedValues).types) {
            // are markers for @ColorInt and @Px
            if (resourceType == COLOR_INT_MARKER_TYPE || resourceType == DIMENSION_MARKER_TYPE) {
                continue;
            }
            PsiElementFactory factory = JavaPsiFacade.getElementFactory(pos.getProject());
            String code = "R." + resourceType.getName();
            // Look up the fully qualified name of the application package
            String fqcn = MergedManifest.get(facet).getPackage();
            String qualifiedCode = fqcn + "." + code;
            Project project = facet.getModule().getProject();
            PsiClass cls = JavaPsiFacade.getInstance(project).findClass(qualifiedCode, GlobalSearchScope.allScope(project));
            if (cls != null) {
                result.addElement(new JavaPsiClassReferenceElement(cls));
            } else {
                PsiExpression type = factory.createExpressionFromText(code, pos);
                result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(type, code), PRIORITY - 1));
                allowed.add(type);
            }
        }
    } else if (allowedValues instanceof ResourceTypeInspection.AllowedValues) {
        ResourceTypeInspection.AllowedValues a = (ResourceTypeInspection.AllowedValues) allowedValues;
        if (a.canBeOred) {
            PsiElementFactory factory = JavaPsiFacade.getElementFactory(pos.getProject());
            PsiExpression zero = factory.createExpressionFromText("0", pos);
            result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(zero, "0"), PRIORITY - 1));
            PsiExpression minusOne = factory.createExpressionFromText("-1", pos);
            result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(minusOne, "-1"), PRIORITY - 1));
            allowed.add(zero);
            allowed.add(minusOne);
        }
        List<ExpectedTypeInfo> types = Arrays.asList(JavaSmartCompletionContributor.getExpectedTypes(parameters));
        for (PsiAnnotationMemberValue value : a.values) {
            if (value instanceof PsiReference) {
                PsiElement resolved = ((PsiReference) value).resolve();
                if (resolved instanceof PsiNamedElement) {
                    LookupElement lookupElement = LookupItemUtil.objectToLookupItem(resolved);
                    if (lookupElement instanceof VariableLookupItem) {
                        ((VariableLookupItem) lookupElement).setSubstitutor(PsiSubstitutor.EMPTY);
                    }
                    LookupElement element = PrioritizedLookupElement.withPriority(lookupElement, PRIORITY);
                    element = decorate(parameters, types, element);
                    result.addElement(element);
                    allowed.add(resolved);
                    continue;
                }
            }
            LookupElement element = LookupElementBuilder.create(value, value.getText());
            element = decorate(parameters, types, element);
            result.addElement(element);
            allowed.add(value);
        }
    }
    result.runRemainingContributors(parameters, new Consumer<CompletionResult>() {

        @Override
        public void consume(CompletionResult completionResult) {
            LookupElement element = completionResult.getLookupElement();
            Object object = element.getObject();
            if (object instanceof PsiElement && allowed.contains(object)) {
                return;
            }
            result.passResult(completionResult);
        }
    });
}
Also used : VariableLookupItem(com.intellij.codeInsight.lookup.VariableLookupItem) List(java.util.List) ResourceType(com.android.resources.ResourceType) LookupElement(com.intellij.codeInsight.lookup.LookupElement) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) THashSet(gnu.trove.THashSet) Project(com.intellij.openapi.project.Project)

Aggregations

VariableLookupItem (com.intellij.codeInsight.lookup.VariableLookupItem)5 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 THashSet (gnu.trove.THashSet)2 NotNull (org.jetbrains.annotations.NotNull)2 ResourceType (com.android.resources.ResourceType)1 ExpectedTypeInfo (com.intellij.codeInsight.ExpectedTypeInfo)1 JavaChainLookupElement (com.intellij.codeInsight.completion.JavaChainLookupElement)1 ExpressionLookupItem (com.intellij.codeInsight.lookup.ExpressionLookupItem)1 ChainCompletionLookupElementUtil.createLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.ChainCompletionLookupElementUtil.createLookupElement)1 ChainCompletionNewVariableLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.ChainCompletionNewVariableLookupElement)1 WeightableChainLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.WeightableChainLookupElement)1 GetterLookupSubLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.GetterLookupSubLookupElement)1 SubLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.SubLookupElement)1 VariableSubLookupElement (com.intellij.compiler.classFilesIndex.chainsSearch.completion.lookup.sub.VariableSubLookupElement)1 ContextRelevantStaticMethod (com.intellij.compiler.classFilesIndex.chainsSearch.context.ContextRelevantStaticMethod)1 ContextRelevantVariableGetter (com.intellij.compiler.classFilesIndex.chainsSearch.context.ContextRelevantVariableGetter)1 Project (com.intellij.openapi.project.Project)1 TIntObjectHashMap (gnu.trove.TIntObjectHashMap)1 Collection (java.util.Collection)1 List (java.util.List)1