PsiStatement (@NotNull Project project, @NotNull PsiElementFactory factory) { PsiWhileStatement whileStatement = myStatement; PsiExpression oldCondition = Objects.requireNonNull(PsiUtil.skipParenthesizedExprDown(whileStatement.getCondition())); PsiStatement body = whileStatement.getBody(); PsiBlockStatement blockBody; int operandIndex = -1; if (oldCondition instanceof PsiPolyadicExpression) { PsiPolyadicExpression polyadic = (PsiPolyadicExpression)oldCondition; if (polyadic.getOperationTokenType().equals(JavaTokenType.ANDAND)) { PsiExpression[] operands = polyadic.getOperands(); operandIndex = ContainerUtil.indexOf(Arrays.asList(operands), o -> PsiTreeUtil.isAncestor(o, myExpression, false)); } } if (body == null) { PsiWhileStatement newWhileStatement = (PsiWhileStatement)factory.createStatementFromText("while(true) {}", whileStatement); Objects.requireNonNull(newWhileStatement.getCondition()).replace(oldCondition); whileStatement = (PsiWhileStatement)whileStatement.replace(newWhileStatement); blockBody = (PsiBlockStatement)Objects.requireNonNull(whileStatement.getBody()); oldCondition = Objects.requireNonNull(whileStatement.getCondition()); } else if (body instanceof PsiBlockStatement) { blockBody = (PsiBlockStatement)body; } else { PsiBlockStatement newBody = BlockUtils.createBlockStatement(project); newBody.getCodeBlock().add(body); blockBody = (PsiBlockStatement)body.replace(newBody); } PsiExpression lOperands; PsiExpression rOperands; if (operandIndex > 0) { PsiPolyadicExpression polyadic = (PsiPolyadicExpression)oldCondition; PsiExpression operand = polyadic.getOperands()[operandIndex]; PsiJavaToken token = Objects.requireNonNull(polyadic.getTokenBeforeOperand(operand)); lOperands = SplitConditionUtil.getLOperands(polyadic, token); rOperands = AndOrToIfSurrounder.getRightOperands(polyadic, operand); } else { lOperands = factory.createExpressionFromText("true", whileStatement); rOperands = oldCondition; } PsiCodeBlock codeBlock = blockBody.getCodeBlock(); PsiIfStatement ifStatement = (PsiIfStatement)factory.createStatementFromText("if(!true) break;", whileStatement); ifStatement = (PsiIfStatement)codeBlock.addAfter(ifStatement, codeBlock.getLBrace()); PsiPrefixExpression negation = (PsiPrefixExpression)Objects.requireNonNull(ifStatement.getCondition()); Objects.requireNonNull(negation.getOperand()).replace(rOperands); Objects.requireNonNull(whileStatement.getCondition()).replace(lOperands); return ifStatement; }