private static boolean isStatementPosition(PsiElement position) {
    if (PsiTreeUtil.getNonStrictParentOfType(position, PsiLiteralExpression.class, PsiComment.class)
        != null) {
      return false;
    }

    if (psiElement()
        .withSuperParent(2, PsiConditionalExpression.class)
        .andNot(psiElement().insideStarting(psiElement(PsiConditionalExpression.class)))
        .accepts(position)) {
      return false;
    }

    if (END_OF_BLOCK.getValue().isAcceptable(position, position)
        && PsiTreeUtil.getParentOfType(position, PsiCodeBlock.class, true, PsiMember.class)
            != null) {
      return true;
    }

    if (psiElement()
        .withParents(
            PsiReferenceExpression.class, PsiExpressionStatement.class, PsiIfStatement.class)
        .andNot(psiElement().afterLeaf("."))
        .accepts(position)) {
      PsiElement stmt = position.getParent().getParent();
      PsiIfStatement ifStatement = (PsiIfStatement) stmt.getParent();
      if (ifStatement.getElseBranch() == stmt || ifStatement.getThenBranch() == stmt) {
        return true;
      }
    }

    return false;
  }
 private static void addUnfinishedMethodTypeParameters(
     PsiElement position, final Consumer<LookupElement> result) {
   final ProcessingContext context = new ProcessingContext();
   if (psiElement()
       .inside(
           psiElement(PsiTypeElement.class)
               .afterLeaf(
                   psiElement()
                       .withText(">")
                       .withParent(
                           psiElement(PsiTypeParameterList.class)
                               .withParent(PsiErrorElement.class)
                               .save("typeParameterList"))))
       .accepts(position, context)) {
     final PsiTypeParameterList list = (PsiTypeParameterList) context.get("typeParameterList");
     PsiElement current = list.getParent().getParent();
     if (current instanceof PsiField) {
       current = current.getParent();
     }
     if (current instanceof PsiClass) {
       for (PsiTypeParameter typeParameter : list.getTypeParameters()) {
         result.consume(new JavaPsiClassReferenceElement(typeParameter));
       }
     }
   }
 }
  private static TailType getReturnTail(PsiElement position) {
    PsiElement scope = position;
    while (true) {
      if (scope instanceof PsiFile || scope instanceof PsiClassInitializer) {
        return TailType.NONE;
      }

      if (scope instanceof PsiMethod) {
        final PsiMethod method = (PsiMethod) scope;
        if (method.isConstructor() || PsiType.VOID.equals(method.getReturnType())) {
          return TailType.SEMICOLON;
        }

        return TailType.HUMBLE_SPACE_BEFORE_WORD;
      }
      if (scope instanceof PsiLambdaExpression) {
        final PsiType returnType =
            LambdaUtil.getFunctionalInterfaceReturnType(((PsiLambdaExpression) scope));
        if (PsiType.VOID.equals(returnType)) {
          return TailType.SEMICOLON;
        }
        return TailType.HUMBLE_SPACE_BEFORE_WORD;
      }
      scope = scope.getParent();
    }
  }
  public static boolean isInstanceofPlace(PsiElement position) {
    PsiElement prev = PsiTreeUtil.prevVisibleLeaf(position);
    if (prev == null) return false;

    PsiElement expr = PsiTreeUtil.getParentOfType(prev, PsiExpression.class);
    if (expr != null && expr.getTextRange().getEndOffset() == prev.getTextRange().getEndOffset()) {
      return true;
    }

    if (position instanceof PsiIdentifier && position.getParent() instanceof PsiLocalVariable) {
      PsiType type = ((PsiLocalVariable) position.getParent()).getType();
      if (type instanceof PsiClassType && ((PsiClassType) type).resolve() == null) {
        return true;
      }
    }

    return false;
  }
 private static boolean isInsideInheritorClass(PsiElement position) {
   if (position.getParent() instanceof PsiJavaCodeReferenceElement) {
     final PsiElement qualifier =
         ((PsiJavaCodeReferenceElement) position.getParent()).getQualifier();
     if (qualifier instanceof PsiJavaCodeReferenceElement) {
       final PsiElement qualifierClass = ((PsiJavaCodeReferenceElement) qualifier).resolve();
       if (qualifierClass instanceof PsiClass && ((PsiClass) qualifierClass).isInterface()) {
         PsiElement parent = position;
         while ((parent = PsiTreeUtil.getParentOfType(parent, PsiClass.class, true)) != null) {
           if (PsiUtil.getEnclosingStaticElement(position, (PsiClass) parent) == null
               && ((PsiClass) parent).isInheritor((PsiClass) qualifierClass, true)) {
             return true;
           }
         }
       }
     }
   }
   return false;
 }
 private static boolean isInsideQualifierClass(PsiElement position) {
   if (position.getParent() instanceof PsiJavaCodeReferenceElement) {
     final PsiElement qualifier =
         ((PsiJavaCodeReferenceElement) position.getParent()).getQualifier();
     if (qualifier instanceof PsiJavaCodeReferenceElement) {
       final PsiElement qualifierClass = ((PsiJavaCodeReferenceElement) qualifier).resolve();
       if (qualifierClass instanceof PsiClass) {
         PsiElement parent = position;
         final PsiManager psiManager = position.getManager();
         while ((parent = PsiTreeUtil.getParentOfType(parent, PsiClass.class, true)) != null) {
           if (psiManager.areElementsEquivalent(parent, qualifierClass)) {
             return true;
           }
         }
       }
     }
   }
   return false;
 }
 public static boolean isInsideParameterList(PsiElement position) {
   PsiElement prev = PsiTreeUtil.prevVisibleLeaf(position);
   PsiModifierList modifierList = PsiTreeUtil.getParentOfType(prev, PsiModifierList.class);
   if (modifierList != null) {
     if (PsiTreeUtil.isAncestor(modifierList, position, false)) {
       return false;
     }
     PsiElement parent = modifierList.getParent();
     return parent instanceof PsiParameterList
         || parent instanceof PsiParameter && parent.getParent() instanceof PsiParameterList;
   }
   return INSIDE_PARAMETER_LIST.accepts(position);
 }