public static void extractPrefixPostfixExpressionToSeparateStatement(PsiElement element) {
    final PsiExpression operand;
    if (element instanceof PsiUnaryExpression) {
      operand = ((PsiUnaryExpression)element).getOperand();
      element = getSurroundPrefixPostfixExpression((PsiUnaryExpression)element);
    }
    else {
      assert false;
      return;
    }
    if (operand == null) {
      return;
    }
    final PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
    if (statement == null) {
      return;
    }
    final PsiElement parent = statement.getParent();
    if (parent == null) {
      return;
    }
    final Project project = element.getProject();
    final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
    final String newStatementText = element.getText() + ';';
    final String operandText = operand.getText();
    final PsiStatement newStatement = factory.createStatementFromText(newStatementText, element);
    if (statement instanceof PsiReturnStatement || statement instanceof PsiYieldStatement || statement instanceof PsiThrowStatement) {
      if (element instanceof PsiPostfixExpression) {
        // special handling of postfix expression in return/yield/throw statement
        final PsiExpression expression;
        if (statement instanceof PsiReturnStatement) {
          expression = ((PsiReturnStatement)statement).getReturnValue();
        } else if (statement instanceof PsiYieldStatement) {
          expression = ((PsiYieldStatement)statement).getExpression();
        } else {
          expression = ((PsiThrowStatement)statement).getException();
        }
        if (expression == null) {
          return;
        }
        final PsiType type = expression.getType();
        if (type == null) {
          return;
        }
        final String[] names = (statement instanceof PsiThrowStatement) ? new String[]{"e", "ex", "exc"} : new String[]{"result"};
        VariableNameGenerator generator = new VariableNameGenerator(expression, VariableKind.LOCAL_VARIABLE);
        if (statement instanceof PsiReturnStatement || statement instanceof PsiYieldStatement) {
          generator = generator.byType(type).byExpression(expression);
        }
        final String variableName = generator.byName(names).generate(true);
        final String newReturnValueText = PsiReplacementUtil.getElementText(expression, element, operandText);
        final String declarationStatementText = type.getCanonicalText() + ' ' + variableName + '=' + newReturnValueText + ';';
        final PsiStatement declarationStatement = factory.createStatementFromText(declarationStatementText, statement);
        parent.addBefore(declarationStatement, statement);
        parent.addBefore(newStatement, statement);
        final String keyword;
        if (statement instanceof PsiReturnStatement) {
          keyword = PsiKeyword.RETURN;
        } else if (statement instanceof PsiYieldStatement) {
          keyword = PsiKeyword.YIELD;
        } else {
          keyword = PsiKeyword.THROW;
        }
        final PsiStatement newReturnStatement = factory.createStatementFromText(keyword + " " + variableName + ';', statement);
        statement.replace(newReturnStatement);
        return;
      }
      else {
        parent.addBefore(newStatement, statement);
      }
    }
    else if (!(statement instanceof PsiForStatement)) {
      if (element instanceof PsiPostfixExpression) {
        parent.addAfter(newStatement, statement);
      }
      else {
        parent.addBefore(newStatement, statement);
      }
    }
    else if (operand instanceof PsiReferenceExpression referenceExpression) {
      final PsiElement target = referenceExpression.resolve();
      if (target != null) {
        final SearchScope useScope = target.getUseScope();
        if (!new LocalSearchScope(statement).equals(useScope)) {
          if (element instanceof PsiPostfixExpression) {
            parent.addAfter(newStatement, statement);
          }
          else {
            parent.addBefore(newStatement, statement);
          }
        }
      }
    }
    if (statement instanceof PsiLoopStatement loopStatement) {
      // in/decrement inside loop statement condition
      final PsiStatement body = loopStatement.getBody();
      if (body instanceof PsiBlockStatement blockStatement) {
        final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
        if (element instanceof PsiPostfixExpression) {
          final PsiElement firstElement = codeBlock.getFirstBodyElement();
          codeBlock.addBefore(newStatement, firstElement);
        }
        else {
          codeBlock.add(newStatement);
        }
      }
      else {
        final StringBuilder blockText = new StringBuilder();
        blockText.append('{');
        if (element instanceof PsiPostfixExpression) {
          blockText.append(newStatementText);
          if (body != null) {
            blockText.append(body.getText());
          }
        }
        else {
          if (body != null) {
            blockText.append(body.getText());
          }
          blockText.append(newStatementText);
        }
        blockText.append('}');
        final PsiStatement blockStatement = factory.createStatementFromText(blockText.toString(), statement);
        if (body == null) {
          loopStatement.add(blockStatement);
        }
        else {
          body.replace(blockStatement);
        }
      }
    }
    PsiReplacementUtil.replaceExpression((PsiExpression)element, operandText);
  }
public static void extractPrefixPostfixExpressionToSeparateStatement(PsiElement element)