static void insertParentheses(@NotNull InsertionContext context,
                                @NotNull LookupElement item,
                                boolean overloadsMatter,
                                ThreeState hasParams, // UNSURE if providing no arguments is a valid situation
                                boolean forceClosingParenthesis) {
    Editor editor = context.getEditor();
    char completionChar = context.getCompletionChar();
    PsiFile file = context.getFile();

    TailType tailType = completionChar == '(' ? TailTypes.noneType() :
                        completionChar == ':' ? TailTypes.conditionalExpressionColonType() :
                        LookupItem.handleCompletionChar(context.getEditor(), item, completionChar);
    boolean hasTail = tailType != TailTypes.noneType() && tailType != TailTypes.unknownType();
    boolean smart = completionChar == Lookup.COMPLETE_STATEMENT_SELECT_CHAR;

    if (completionChar == '(' || completionChar == '.' || completionChar == ',' || completionChar == ';' || completionChar == ':' || completionChar == ' ') {
      context.setAddCompletionChar(false);
    }

    if (hasTail) {
      hasParams = ThreeState.NO;
    }
    boolean needRightParenth = forceClosingParenthesis ||
                               !smart && (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET ||
                                          hasParams == ThreeState.NO && completionChar != '(');

    context.commitDocument();

    CommonCodeStyleSettings styleSettings = CompletionStyleUtil.getCodeStyleSettings(context);
    PsiElement elementAt = file.findElementAt(context.getStartOffset());
    if (elementAt == null || !(elementAt.getParent() instanceof PsiMethodReferenceExpression)) {
      ThreeState hasParameters = hasParams;
      boolean spaceBetweenParentheses = hasParams == ThreeState.YES && styleSettings.SPACE_WITHIN_METHOD_CALL_PARENTHESES ||
                                        hasParams == ThreeState.UNSURE && styleSettings.SPACE_WITHIN_EMPTY_METHOD_CALL_PARENTHESES;
      new ParenthesesInsertHandler<>(styleSettings.SPACE_BEFORE_METHOD_CALL_PARENTHESES, spaceBetweenParentheses,
                                     needRightParenth, styleSettings.METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE) {
        @Override
        protected boolean placeCaretInsideParentheses(InsertionContext context1, LookupElement item1) {
          return hasParameters != ThreeState.NO;
        }

        @Override
        protected PsiElement findExistingLeftParenthesis(@NotNull InsertionContext context) {
          PsiElement token = super.findExistingLeftParenthesis(context);
          return isPartOfLambda(token) ? null : token;
        }

        private static boolean isPartOfLambda(PsiElement token) {
          return token != null && token.getParent() instanceof PsiExpressionList &&
                 PsiUtilCore.getElementType(PsiTreeUtil.nextVisibleLeaf(token.getParent())) == JavaTokenType.ARROW;
        }
      }.handleInsert(context, item);
    }

    if (hasParams != ThreeState.NO) {
      // Invoke parameters popup
      AutoPopupController.getInstance(file.getProject()).autoPopupParameterInfo(editor, overloadsMatter ? null : (PsiElement)item.getObject());
    }

    if (smart || !needRightParenth || !EditorSettingsExternalizable.getInstance().isInsertParenthesesAutomatically() ||
        !insertTail(context, item, tailType, hasTail)) {
      return;
    }

    if (completionChar == '.') {
      AutoPopupController.getInstance(file.getProject()).autoPopupMemberLookup(context.getEditor(), null);
    } else if (completionChar == ',') {
      AutoPopupController.getInstance(file.getProject()).autoPopupParameterInfo(context.getEditor(), null);
    }
  }
static void insertParentheses(@NotNull InsertionContext context,
                                @NotNull LookupElement item,
                                boolean overloadsMatter,
                                ThreeState hasParams, // UNSURE if providing no arguments is a valid situation
                                boolean forceClosingParenthesis)