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 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);
 }
 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;
 }
  static boolean isAfterTypeDot(PsiElement position) {
    if (isInsideParameterList(position)
        || position.getContainingFile() instanceof PsiJavaCodeReferenceCodeFragment) {
      return false;
    }

    return psiElement()
            .afterLeaf(psiElement().withText(".").afterLeaf(CLASS_REFERENCE))
            .accepts(position)
        || isAfterPrimitiveOrArrayType(position);
  }
  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 void addPrimitiveTypes(final Consumer<LookupElement> result, PsiElement position) {
    if (AFTER_DOT.accepts(position)) {
      return;
    }

    boolean afterNew =
        psiElement()
            .afterLeaf(
                psiElement()
                    .withText(PsiKeyword.NEW)
                    .andNot(psiElement().afterLeaf(PsiKeyword.THROW, ".")))
            .accepts(position);
    if (afterNew) {
      PsiElementFactory factory = JavaPsiFacade.getElementFactory(position.getProject());
      for (String primitiveType : PRIMITIVE_TYPES) {
        result.consume(
            PsiTypeLookupItem.createLookupItem(
                factory.createTypeFromText(primitiveType + "[]", null), null));
      }
      result.consume(
          PsiTypeLookupItem.createLookupItem(factory.createTypeFromText("void[]", null), null));
      return;
    }

    boolean inCast =
        psiElement()
            .afterLeaf(
                psiElement()
                    .withText("(")
                    .withParent(
                        psiElement(PsiParenthesizedExpression.class, PsiTypeCastExpression.class)))
            .accepts(position);

    boolean typeFragment =
        position.getContainingFile() instanceof PsiTypeCodeFragment
            && PsiTreeUtil.prevVisibleLeaf(position) == null;
    boolean declaration = DECLARATION_START.getValue().accepts(position);
    boolean expressionPosition = isExpressionPosition(position);
    boolean inGenerics =
        PsiTreeUtil.getParentOfType(position, PsiReferenceParameterList.class) != null;
    if (START_FOR.accepts(position)
        || isInsideParameterList(position)
        || inGenerics
        || VARIABLE_AFTER_FINAL.accepts(position)
        || inCast
        || declaration
        || typeFragment
        || expressionPosition
        || isStatementPosition(position)) {
      for (String primitiveType : PRIMITIVE_TYPES) {
        result.consume(createKeyword(position, primitiveType));
      }
    }
    if (declaration) {
      result.consume(
          new OverrideableSpace(
              createKeyword(position, PsiKeyword.VOID), TailType.HUMBLE_SPACE_BEFORE_WORD));
    } else if (typeFragment && ((PsiTypeCodeFragment) position.getContainingFile()).isVoidValid()) {
      result.consume(createKeyword(position, PsiKeyword.VOID));
    }
  }
  public void fillCompletions(
      CompletionParameters parameters, final Consumer<LookupElement> result) {
    final PsiElement position = parameters.getPosition();
    if (PsiTreeUtil.getParentOfType(position, PsiComment.class, false) != null) {
      return;
    }

    PsiStatement statement = PsiTreeUtil.getParentOfType(position, PsiExpressionStatement.class);
    if (statement == null) {
      statement = PsiTreeUtil.getParentOfType(position, PsiDeclarationStatement.class);
    }
    PsiElement prevLeaf = PsiTreeUtil.prevVisibleLeaf(position);
    if (statement != null
        && statement.getTextRange().getStartOffset() == position.getTextRange().getStartOffset()) {
      if (!psiElement()
          .withSuperParent(2, PsiSwitchStatement.class)
          .afterLeaf("{")
          .accepts(statement)) {
        PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(prevLeaf, PsiTryStatement.class);
        if (tryStatement == null
            || tryStatement.getCatchSections().length > 0
            || tryStatement.getFinallyBlock() != null) {
          result.consume(
              new OverrideableSpace(
                  createKeyword(position, PsiKeyword.FINAL), TailType.HUMBLE_SPACE_BEFORE_WORD));
        }
      }
    }

    if (isStatementPosition(position)) {
      if (PsiTreeUtil.getParentOfType(position, PsiSwitchStatement.class, false, PsiMember.class)
          != null) {
        result.consume(
            new OverrideableSpace(createKeyword(position, PsiKeyword.CASE), TailType.INSERT_SPACE));
        result.consume(
            new OverrideableSpace(
                createKeyword(position, PsiKeyword.DEFAULT), TailType.CASE_COLON));
        if (START_SWITCH.accepts(position)) {
          return;
        }
      }

      addBreakContinue(result, position);
      addStatementKeywords(result, position);
    }

    if (SUPER_OR_THIS_PATTERN.accepts(position)) {
      final boolean afterDot = AFTER_DOT.accepts(position);
      final boolean insideQualifierClass = isInsideQualifierClass(position);
      final boolean insideInheritorClass =
          PsiUtil.isLanguageLevel8OrHigher(position) && isInsideInheritorClass(position);
      if (!afterDot || insideQualifierClass || insideInheritorClass) {
        if (!afterDot || insideQualifierClass) {
          result.consume(createKeyword(position, PsiKeyword.THIS));
        }

        final LookupItem superItem = (LookupItem) createKeyword(position, PsiKeyword.SUPER);
        if (psiElement()
            .afterLeaf(psiElement().withText("{").withSuperParent(2, psiMethod().constructor(true)))
            .accepts(position)) {
          final PsiMethod method =
              PsiTreeUtil.getParentOfType(position, PsiMethod.class, false, PsiClass.class);
          assert method != null;
          final boolean hasParams = superConstructorHasParameters(method);
          superItem.setInsertHandler(
              new ParenthesesInsertHandler<LookupElement>() {
                @Override
                protected boolean placeCaretInsideParentheses(
                    InsertionContext context, LookupElement item) {
                  return hasParams;
                }

                @Override
                public void handleInsert(InsertionContext context, LookupElement item) {
                  super.handleInsert(context, item);
                  TailType.insertChar(context.getEditor(), context.getTailOffset(), ';');
                }
              });
        }

        result.consume(superItem);
      }
    }

    if (isExpressionPosition(position)) {
      if (PsiTreeUtil.getParentOfType(position, PsiAnnotation.class) == null) {
        result.consume(
            TailTypeDecorator.withTail(
                createKeyword(position, PsiKeyword.NEW), TailType.INSERT_SPACE));
        result.consume(createKeyword(position, PsiKeyword.NULL));
      }
      if (mayExpectBoolean(parameters)) {
        result.consume(createKeyword(position, PsiKeyword.TRUE));
        result.consume(createKeyword(position, PsiKeyword.FALSE));
      }
    }

    PsiFile file = position.getContainingFile();
    if (!(file instanceof PsiExpressionCodeFragment)
        && !(file instanceof PsiJavaCodeReferenceCodeFragment)
        && !(file instanceof PsiTypeCodeFragment)) {
      if (prevLeaf == null) {
        result.consume(
            new OverrideableSpace(
                createKeyword(position, PsiKeyword.PACKAGE), TailType.HUMBLE_SPACE_BEFORE_WORD));
        result.consume(
            new OverrideableSpace(
                createKeyword(position, PsiKeyword.IMPORT), TailType.HUMBLE_SPACE_BEFORE_WORD));
      } else if (END_OF_BLOCK.getValue().isAcceptable(position, position)
          && PsiTreeUtil.getParentOfType(position, PsiMember.class) == null) {
        result.consume(
            new OverrideableSpace(
                createKeyword(position, PsiKeyword.IMPORT), TailType.HUMBLE_SPACE_BEFORE_WORD));
      }
    }

    if ((isInsideParameterList(position)
            || isAtResourceVariableStart(position)
            || isAtCatchVariableStart(position))
        && !psiElement().afterLeaf(PsiKeyword.FINAL).accepts(position)
        && !AFTER_DOT.accepts(position)) {
      result.consume(
          TailTypeDecorator.withTail(
              createKeyword(position, PsiKeyword.FINAL), TailType.HUMBLE_SPACE_BEFORE_WORD));
    }

    if (isInstanceofPlace(position)) {
      result.consume(
          LookupElementDecorator.withInsertHandler(
              createKeyword(position, PsiKeyword.INSTANCEOF),
              new InsertHandler<LookupElementDecorator<LookupElement>>() {
                @Override
                public void handleInsert(
                    InsertionContext context, LookupElementDecorator<LookupElement> item) {
                  TailType tailType = TailType.HUMBLE_SPACE_BEFORE_WORD;
                  if (tailType.isApplicable(context)) {
                    tailType.processTail(context.getEditor(), context.getTailOffset());
                  }

                  if ('!' == context.getCompletionChar()) {
                    context.setAddCompletionChar(false);
                    context.commitDocument();
                    PsiInstanceOfExpression expr =
                        PsiTreeUtil.findElementOfClassAtOffset(
                            context.getFile(),
                            context.getStartOffset(),
                            PsiInstanceOfExpression.class,
                            false);
                    if (expr != null) {
                      String space =
                          context.getCodeStyleSettings().SPACE_WITHIN_PARENTHESES ? " " : "";
                      context
                          .getDocument()
                          .insertString(expr.getTextRange().getStartOffset(), "!(" + space);
                      context.getDocument().insertString(context.getTailOffset(), space + ")");
                    }
                  }
                }
              }));
    }

    if (isSuitableForClass(position)) {
      for (String s : ModifierChooser.getKeywords(position)) {
        result.consume(
            new OverrideableSpace(createKeyword(position, s), TailType.HUMBLE_SPACE_BEFORE_WORD));
      }
      result.consume(
          new OverrideableSpace(
              createKeyword(position, PsiKeyword.CLASS), TailType.HUMBLE_SPACE_BEFORE_WORD));
      if (PsiTreeUtil.getParentOfType(position, PsiCodeBlock.class, true, PsiMember.class)
          == null) {
        result.consume(
            new OverrideableSpace(
                createKeyword(position, PsiKeyword.INTERFACE), TailType.HUMBLE_SPACE_BEFORE_WORD));
        if (PsiUtil.getLanguageLevel(position).isAtLeast(LanguageLevel.JDK_1_5)) {
          result.consume(
              new OverrideableSpace(
                  createKeyword(position, PsiKeyword.ENUM), TailType.INSERT_SPACE));
        }
      }
    }

    addPrimitiveTypes(result, position);

    if (isAfterTypeDot(position)) {
      result.consume(createKeyword(position, PsiKeyword.CLASS));
    }

    addUnfinishedMethodTypeParameters(position, result);

    if (JavaMemberNameCompletionContributor.INSIDE_TYPE_PARAMS_PATTERN.accepts(position)) {
      result.consume(
          new OverrideableSpace(
              createKeyword(position, PsiKeyword.EXTENDS), TailType.HUMBLE_SPACE_BEFORE_WORD));
      result.consume(
          new OverrideableSpace(
              createKeyword(position, PsiKeyword.SUPER), TailType.HUMBLE_SPACE_BEFORE_WORD));
    }
  }