public static PsiElement expandDiamondsAndReplaceExplicitTypeWithVar(PsiTypeElement typeElement, PsiElement context) {
    PsiElement parent = typeElement.getParent();
    if (parent instanceof PsiVariable) {
      PsiExpression copyVariableInitializer = ((PsiVariable)parent).getInitializer();
      if (copyVariableInitializer instanceof PsiNewExpression) {
        final PsiDiamondType.DiamondInferenceResult diamondResolveResult =
          PsiDiamondTypeImpl.resolveInferredTypesNoCheck((PsiNewExpression)copyVariableInitializer, copyVariableInitializer);
        if (!diamondResolveResult.getInferredTypes().isEmpty()) {
          PsiDiamondTypeUtil.expandTopLevelDiamondsInside(copyVariableInitializer);
        }
      }
      else if (copyVariableInitializer instanceof PsiArrayInitializerExpression initializer) {
        AddNewArrayExpressionFix.doFix(initializer);
      }
      else if (copyVariableInitializer instanceof PsiFunctionalExpression) {
        PsiTypeCastExpression castExpression =
          (PsiTypeCastExpression)JavaPsiFacade.getElementFactory(copyVariableInitializer.getProject())
            .createExpressionFromText("(" + typeElement.getText() + ")a", copyVariableInitializer);
        Objects.requireNonNull(castExpression.getOperand()).replace(copyVariableInitializer);
        copyVariableInitializer.replace(castExpression);
      }
    }

    return new CommentTracker().replaceAndRestoreComments(typeElement, JavaPsiFacade.getElementFactory(context.getProject()).createTypeElementFromText("var", context));
  }
public static PsiElement expandDiamondsAndReplaceExplicitTypeWithVar(PsiTypeElement typeElement, PsiElement context)