public static @Nullable CodeBlockSurrounder forExpression(@NotNull PsiExpression expression) {
    PsiElement cur = expression;
    PsiElement parent = cur.getParent();
    while (parent instanceof PsiExpression || parent instanceof PsiExpressionList || parent instanceof PsiTemplate) {
      if (parent instanceof PsiLambdaExpression lambda) {
        return new LambdaCodeBlockSurrounder(expression, lambda);
      }
      if (parent instanceof PsiPolyadicExpression polyadicExpression) {
        IElementType type = polyadicExpression.getOperationTokenType();
        if (type.equals(JavaTokenType.ANDAND) && polyadicExpression.getOperands()[0] != cur) {
          PsiElement conditionParent = PsiUtil.skipParenthesizedExprUp(polyadicExpression.getParent());
          if (conditionParent instanceof PsiWhileStatement whileStatement) {
            return new WhileConditionSurrounder(expression, whileStatement);
          }
          CodeBlockSurrounder parentSurrounder = forExpressionSkipParentheses(polyadicExpression);
          if (parentSurrounder == null) return null;
          ParentContext parentContext = parentSurrounder.getExpectedParentContext();
          if (parentContext != ParentContext.RETURN && parentContext != ParentContext.SIMPLE_IF_CONDITION) return null;
          return new AndOrToIfSurrounder(expression, polyadicExpression, parentSurrounder);
        }
        else if (type.equals(JavaTokenType.OROR) && polyadicExpression.getOperands()[0] != cur) {
          CodeBlockSurrounder parentSurrounder = forExpressionSkipParentheses(polyadicExpression);
          if (parentSurrounder == null) return null;
          ParentContext parentContext = parentSurrounder.getExpectedParentContext();
          if (parentContext != ParentContext.RETURN) return null;
          return new AndOrToIfSurrounder(expression, polyadicExpression, parentSurrounder);
        }
      }
      if (parent instanceof PsiConditionalExpression conditional && conditional.getCondition() != cur) {
        CodeBlockSurrounder parentSurrounder = forExpressionSkipParentheses(conditional);
        if (parentSurrounder == null) return null;
        ParentContext parentContext = parentSurrounder.getExpectedParentContext();
        if (parentContext != ParentContext.ASSIGNMENT && parentContext != ParentContext.RETURN) return null;
        return new TernaryToIfSurrounder(expression, conditional, parentSurrounder);
      }
      if (JavaPsiConstructorUtil.isConstructorCall(parent) && !PsiUtil.isAvailable(JavaFeature.STATEMENTS_BEFORE_SUPER, parent)) {
        return null;
      }
      cur = parent;
      parent = cur.getParent();
    }
    if (parent instanceof PsiEnumConstant) {
      return new AnonymousCallSurrounder(expression);
    }
    if (parent instanceof PsiStatement) {
      PsiElement grandParent = parent.getParent();
      if (grandParent instanceof PsiForStatement forStatement && forStatement.getUpdate() == parent) return null;
    }
    if (parent instanceof PsiWhileStatement whileStatement && whileStatement.getCondition() == cur) {
      return new WhileConditionSurrounder(expression, whileStatement);
    }
    if (parent instanceof PsiForeachStatement foreachStatement && foreachStatement.getIteratedValue() == cur ||
        parent instanceof PsiIfStatement ifStatement && ifStatement.getCondition() == cur ||
        parent instanceof PsiReturnStatement || parent instanceof PsiExpressionStatement ||
        parent instanceof PsiYieldStatement || parent instanceof PsiThrowStatement) {
      return forStatement((PsiStatement)parent, expression);
    }
    if (parent instanceof PsiLocalVariable variable) {
      PsiDeclarationStatement decl = tryCast(parent.getParent(), PsiDeclarationStatement.class);
      if (decl != null && ArrayUtil.getFirstElement(decl.getDeclaredElements()) == parent) {
        PsiTypeElement typeElement = variable.getTypeElement();
        if (!typeElement.isInferredType() ||
            PsiTypesUtil.replaceWithExplicitType(((PsiLocalVariable)parent.copy()).getTypeElement()) != null) {
          PsiElement declParent = decl.getParent();
          if (declParent instanceof PsiForStatement forStatement && forStatement.getInitialization() == decl) {
            if (hasNameCollision(decl, declParent.getParent())) {
              // There's another var with the same name as one declared in for initialization 
              return new SimpleSurrounder(expression, forStatement);
            }
            return forStatement((PsiStatement)declParent, expression);
          }
          return forStatement(decl, expression);
        }
      }
    }
    if (parent instanceof PsiResourceVariable) {
      PsiResourceList list = tryCast(parent.getParent(), PsiResourceList.class);
      if (list != null && list.getParent() instanceof PsiTryStatement tryStatement) {
        Iterator<PsiResourceListElement> iterator = list.iterator();
        if (iterator.hasNext() && iterator.next() == parent && tryStatement.getCatchBlocks().length == 0 
            && tryStatement.getFinallyBlock() == null) {
          return forStatement(tryStatement, expression);
        }
        return new SplitTrySurrounder(expression, (PsiResourceVariable)parent, tryStatement);
      }
      return null;
    }
    if (parent instanceof PsiField) {
      if (parent.getParent() instanceof PsiImplicitClass) {
        return null;
      }
      return new ExtractFieldInitializerSurrounder(expression, (PsiField)parent);
    }

    return null;
  }
public static @Nullable CodeBlockSurrounder forExpression(@NotNull PsiExpression expression)