@Override
            @Nullable
            public ExpectedTypeInfo[] fun(final CompletionLocation location) {
              if (PsiJavaPatterns.psiElement()
                  .beforeLeaf(PsiJavaPatterns.psiElement().withText("."))
                  .accepts(location.getCompletionParameters().getPosition())) {
                return ExpectedTypeInfo.EMPTY_ARRAY;
              }

              return JavaSmartCompletionContributor.getExpectedTypes(
                  location.getCompletionParameters());
            }
 public StatisticsInfo serialize(final LookupElement element, final CompletionLocation location) {
   return new StatisticsInfo(
       "completion#" + location.getCompletionParameters().getOriginalFile().getLanguage(),
       element.getLookupString());
 }
Пример #3
0
  public Result weigh(
      @NotNull final LookupElement element, @NotNull final CompletionLocation location) {
    if (location == null) {
      return null;
    }
    if (location.getCompletionType() != CompletionType.BASIC
        && location.getCompletionType() != CompletionType.SMART) return Result.normal;

    final Object object = element.getObject();
    if (!(object instanceof PsiModifierListOwner) && !(object instanceof PsiExpression))
      return Result.normal;

    final PsiMethod positionMethod = JavaCompletionUtil.POSITION_METHOD.getValue(location);
    if (positionMethod == null) return Result.normal;

    final PsiElement position = location.getCompletionParameters().getPosition();
    final ElementFilter filter = JavaCompletionUtil.recursionFilter(position);
    if (filter != null && !filter.isAcceptable(object, position)) {
      return Result.recursive;
    }

    final PsiMethodCallExpression expression =
        PsiTreeUtil.getParentOfType(position, PsiMethodCallExpression.class, true, PsiClass.class);
    final PsiReferenceExpression reference =
        expression != null
            ? expression.getMethodExpression()
            : PsiTreeUtil.getParentOfType(position, PsiReferenceExpression.class);
    if (reference == null) return Result.normal;

    final PsiExpression qualifier = reference.getQualifierExpression();
    boolean isDelegate = qualifier != null && !(qualifier instanceof PsiThisExpression);

    if (isPassingObjectToItself(object, qualifier, isDelegate)) {
      return Result.passingObjectToItself;
    }

    if (expression != null) {
      final ExpectedTypeInfo[] expectedInfos = JavaCompletionUtil.EXPECTED_TYPES.getValue(location);
      if (expectedInfos != null) {
        final PsiType itemType = JavaCompletionUtil.getLookupElementType(element);
        if (itemType != null) {
          for (final ExpectedTypeInfo expectedInfo : expectedInfos) {
            if (positionMethod.equals(expectedInfo.getCalledMethod())
                && expectedInfo.getType().isAssignableFrom(itemType)) {
              return isDelegate ? Result.delegation : Result.recursive;
            }
          }
        }
      }
      return Result.normal;
    }

    if (object instanceof PsiMethod) {
      final PsiMethod method = (PsiMethod) object;
      if (PsiTreeUtil.isAncestor(reference, position, false)
          && Comparing.equal(method.getName(), positionMethod.getName())
          && method.getParameterList().getParametersCount()
              == positionMethod.getParameterList().getParametersCount()) {
        if (findDeepestSuper(method).equals(findDeepestSuper(positionMethod))) {
          return isDelegate ? Result.delegation : Result.recursive;
        }
      }
    }

    return Result.normal;
  }