PsiExpression (@NotNull Project project, PsiExpression subExpression) { if (!mySubExpressionValue) { // Prevent extracting while condition to internal 'if' PsiElement parent = PsiUtil.skipParenthesizedExprUp(subExpression.getParent()); PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); if (parent instanceof PsiWhileStatement whileStatement) { if (whileStatement.getCondition() != null) { PsiStatement replacement = factory.createStatementFromText("if(" + whileStatement.getCondition().getText() + ");", whileStatement); PsiIfStatement ifStatement = (PsiIfStatement)whileStatement.replace(replacement); subExpression = Objects.requireNonNull(ifStatement.getCondition()); } } else if (parent instanceof PsiPolyadicExpression polyadicExpression && JavaTokenType.OROR.equals(polyadicExpression.getOperationTokenType())) { PsiExpression expression = expandLastIfDisjunct(polyadicExpression, subExpression, factory); if (expression != null) { return expression; } } } CodeBlockSurrounder surrounder = CodeBlockSurrounder.forExpression(subExpression); return surrounder == null ? null : surrounder.surround().getExpression(); }