private static void removeParensFromAssignmentExpression(
     @NotNull PsiAssignmentExpression assignment, boolean ignoreClarifyingParentheses) {
   final PsiExpression lhs = assignment.getLExpression();
   final PsiExpression rhs = assignment.getRExpression();
   removeParentheses(lhs, ignoreClarifyingParentheses);
   if (rhs != null) {
     removeParentheses(rhs, ignoreClarifyingParentheses);
   }
 }
 private static void removeParensFromMethodCallExpression(
     @NotNull PsiMethodCallExpression methodCallExpression, boolean ignoreClarifyingParentheses) {
   final PsiReferenceExpression target = methodCallExpression.getMethodExpression();
   final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
   final PsiExpression[] arguments = argumentList.getExpressions();
   removeParentheses(target, ignoreClarifyingParentheses);
   for (final PsiExpression argument : arguments) {
     removeParentheses(argument, ignoreClarifyingParentheses);
   }
 }
 private static void removeParensFromArrayAccessExpression(
     @NotNull PsiArrayAccessExpression arrayAccessExpression,
     boolean ignoreClarifyingParentheses) {
   final PsiExpression arrayExpression = arrayAccessExpression.getArrayExpression();
   removeParentheses(arrayExpression, ignoreClarifyingParentheses);
   final PsiExpression indexExpression = arrayAccessExpression.getIndexExpression();
   if (indexExpression != null) {
     removeParentheses(indexExpression, ignoreClarifyingParentheses);
   }
 }
 private static void removeParensFromConditionalExpression(
     @NotNull PsiConditionalExpression conditionalExpression,
     boolean ignoreClarifyingParentheses) {
   final PsiExpression condition = conditionalExpression.getCondition();
   removeParentheses(condition, ignoreClarifyingParentheses);
   final PsiExpression thenBranch = conditionalExpression.getThenExpression();
   if (thenBranch != null) {
     removeParentheses(thenBranch, ignoreClarifyingParentheses);
   }
   final PsiExpression elseBranch = conditionalExpression.getElseExpression();
   if (elseBranch != null) {
     removeParentheses(elseBranch, ignoreClarifyingParentheses);
   }
 }
 private static void removeParensFromTypeCastExpression(
     @NotNull PsiTypeCastExpression typeCastExpression, boolean ignoreClarifyingParentheses) {
   final PsiExpression operand = typeCastExpression.getOperand();
   if (operand != null) {
     removeParentheses(operand, ignoreClarifyingParentheses);
   }
 }
 private static void removeParensFromReferenceExpression(
     @NotNull PsiReferenceExpression referenceExpression, boolean ignoreClarifyingParentheses) {
   final PsiExpression qualifier = referenceExpression.getQualifierExpression();
   if (qualifier != null) {
     removeParentheses(qualifier, ignoreClarifyingParentheses);
   }
 }
 private static void removeParensFromArrayInitializerExpression(
     @NotNull PsiArrayInitializerExpression arrayInitializerExpression,
     boolean ignoreClarifyingParentheses) {
   final PsiExpression[] initializers = arrayInitializerExpression.getInitializers();
   for (final PsiExpression initializer : initializers) {
     removeParentheses(initializer, ignoreClarifyingParentheses);
   }
 }
 private static void removeParensFromNewExpression(
     @NotNull PsiNewExpression newExpression, boolean ignoreClarifyingParentheses) {
   final PsiExpression[] dimensions = newExpression.getArrayDimensions();
   for (PsiExpression dimension : dimensions) {
     removeParentheses(dimension, ignoreClarifyingParentheses);
   }
   final PsiExpression qualifier = newExpression.getQualifier();
   if (qualifier != null) {
     removeParentheses(qualifier, ignoreClarifyingParentheses);
   }
   final PsiExpression arrayInitializer = newExpression.getArrayInitializer();
   if (arrayInitializer != null) {
     removeParentheses(arrayInitializer, ignoreClarifyingParentheses);
   }
   final PsiExpressionList argumentList = newExpression.getArgumentList();
   if (argumentList != null) {
     final PsiExpression[] arguments = argumentList.getExpressions();
     for (PsiExpression argument : arguments) {
       removeParentheses(argument, ignoreClarifyingParentheses);
     }
   }
 }
 private static void removeParensFromPostfixExpression(
     @NotNull PsiPostfixExpression postfixExpression, boolean ignoreClarifyingParentheses) {
   final PsiExpression operand = postfixExpression.getOperand();
   removeParentheses(operand, ignoreClarifyingParentheses);
 }
 private static void removeParensFromPolyadicExpression(
     @NotNull PsiPolyadicExpression polyadicExpression, boolean ignoreClarifyingParentheses) {
   for (PsiExpression operand : polyadicExpression.getOperands()) {
     removeParentheses(operand, ignoreClarifyingParentheses);
   }
 }
 private static void removeParensFromInstanceOfExpression(
     @NotNull PsiInstanceOfExpression instanceofExpression, boolean ignoreClarifyingParentheses) {
   final PsiExpression operand = instanceofExpression.getOperand();
   removeParentheses(operand, ignoreClarifyingParentheses);
 }
 private static void removeParensFromParenthesizedExpression(
     @NotNull PsiParenthesizedExpression parenthesizedExpression,
     boolean ignoreClarifyingParentheses) {
   final PsiExpression body = parenthesizedExpression.getExpression();
   if (body == null) {
     parenthesizedExpression.delete();
     return;
   }
   final PsiElement parent = parenthesizedExpression.getParent();
   if (!(parent instanceof PsiExpression)
       || parent instanceof PsiParenthesizedExpression
       || parent instanceof PsiArrayInitializerExpression) {
     final PsiExpression newExpression = (PsiExpression) parenthesizedExpression.replace(body);
     removeParentheses(newExpression, ignoreClarifyingParentheses);
     return;
   } else if (parent instanceof PsiArrayAccessExpression) {
     final PsiArrayAccessExpression arrayAccessExpression = (PsiArrayAccessExpression) parent;
     if (parenthesizedExpression == arrayAccessExpression.getIndexExpression()) {
       // use addAfter() + delete() instead of replace() to
       // workaround automatic insertion of parentheses by psi
       final PsiExpression newExpression =
           (PsiExpression) parent.addAfter(body, parenthesizedExpression);
       parenthesizedExpression.delete();
       removeParentheses(newExpression, ignoreClarifyingParentheses);
       return;
     }
   }
   final PsiExpression parentExpression = (PsiExpression) parent;
   final int parentPrecedence = getPrecedence(parentExpression);
   final int childPrecedence = getPrecedence(body);
   if (parentPrecedence < childPrecedence) {
     final PsiElement bodyParent = body.getParent();
     final PsiParenthesizedExpression newParenthesizedExpression =
         (PsiParenthesizedExpression) parenthesizedExpression.replace(bodyParent);
     final PsiExpression expression = newParenthesizedExpression.getExpression();
     if (expression != null) {
       removeParentheses(expression, ignoreClarifyingParentheses);
     }
   } else if (parentPrecedence == childPrecedence) {
     if (parentExpression instanceof PsiPolyadicExpression
         && body instanceof PsiPolyadicExpression) {
       final PsiPolyadicExpression parentPolyadicExpression =
           (PsiPolyadicExpression) parentExpression;
       final IElementType parentOperator = parentPolyadicExpression.getOperationTokenType();
       final PsiPolyadicExpression bodyPolyadicExpression = (PsiPolyadicExpression) body;
       final IElementType bodyOperator = bodyPolyadicExpression.getOperationTokenType();
       final PsiType parentType = parentPolyadicExpression.getType();
       final PsiType bodyType = body.getType();
       if (parentType != null
           && parentType.equals(bodyType)
           && parentOperator.equals(bodyOperator)) {
         final PsiExpression[] parentOperands = parentPolyadicExpression.getOperands();
         if (PsiTreeUtil.isAncestor(parentOperands[0], body, true)
             || isCommutativeOperator(bodyOperator)) {
           // use addAfter() + delete() instead of replace() to
           // workaround automatic insertion of parentheses by psi
           final PsiExpression newExpression =
               (PsiExpression) parent.addAfter(body, parenthesizedExpression);
           parenthesizedExpression.delete();
           removeParentheses(newExpression, ignoreClarifyingParentheses);
           return;
         }
       }
       if (ignoreClarifyingParentheses) {
         if (parentOperator.equals(bodyOperator)) {
           removeParentheses(body, ignoreClarifyingParentheses);
         }
       } else {
         final PsiExpression newExpression = (PsiExpression) parenthesizedExpression.replace(body);
         removeParentheses(newExpression, ignoreClarifyingParentheses);
       }
     } else {
       final PsiExpression newExpression = (PsiExpression) parenthesizedExpression.replace(body);
       removeParentheses(newExpression, ignoreClarifyingParentheses);
     }
   } else {
     if (ignoreClarifyingParentheses
         && parent instanceof PsiPolyadicExpression
         && (body instanceof PsiPolyadicExpression || body instanceof PsiInstanceOfExpression)) {
       removeParentheses(body, ignoreClarifyingParentheses);
     } else {
       final PsiExpression newExpression = (PsiExpression) parenthesizedExpression.replace(body);
       removeParentheses(newExpression, ignoreClarifyingParentheses);
     }
   }
 }