PsiStatement (@NotNull PsiStatement returnOrYieldStatement, @NotNull PsiPolyadicExpression condition, @NotNull PsiExpression lOperands, @NotNull PsiExpression rOperands, @NotNull Project project, @NotNull PsiElementFactory factory) { CommentTracker ct = new CommentTracker(); boolean orChain = condition.getOperationTokenType().equals(JavaTokenType.OROR); String keyword = returnOrYieldStatement.getFirstChild().getText(); String extractedCondition = orChain ? ct.text(lOperands) : BoolUtils.getNegatedExpressionText(lOperands, ct); String ifText = "if(" + extractedCondition + ") " + keyword + " " + orChain + ";"; PsiStatement ifStatement = factory.createStatementFromText(ifText, returnOrYieldStatement); CodeStyleManager.getInstance(project).reformat(returnOrYieldStatement.getParent().addBefore(ifStatement, returnOrYieldStatement)); ct.replaceAndRestoreComments(Objects.requireNonNull(condition), rOperands); return returnOrYieldStatement; }