[java-refactoring] CodeBlockSurrounder: avoid collapsing with unrelated if-statement

Fixes IDEA-360579 Inline Method creates uncompilable code


(cherry picked from commit cd73e3e730b8cef37f4a26235a2ab3db812f02ff)

IJ-CR-147183

GitOrigin-RevId: 0258e753bfb5f7e66bc4a0526769fe8ba07aa02c
This commit is contained in:
Tagir Valeev
2024-10-18 10:02:32 +02:00
committed by intellij-monorepo-bot
parent e11b6cc846
commit 93592a4c9e
4 changed files with 38 additions and 3 deletions

View File

@@ -835,6 +835,7 @@ public abstract class CodeBlockSurrounder {
private static class AndOrToIfSurrounder extends CodeBlockSurrounder {
private final @NotNull PsiPolyadicExpression myPolyadicExpression;
private final @NotNull CodeBlockSurrounder myUpstream;
private PsiIfStatement myCreatedIf;
AndOrToIfSurrounder(@NotNull PsiExpression expression,
@NotNull PsiPolyadicExpression polyadicExpression,
@@ -894,7 +895,7 @@ public abstract class CodeBlockSurrounder {
}
@NotNull
private static PsiStatement splitReturn(@NotNull PsiStatement returnOrYieldStatement,
private PsiStatement splitReturn(@NotNull PsiStatement returnOrYieldStatement,
@NotNull PsiPolyadicExpression condition,
@NotNull PsiExpression lOperands,
@NotNull PsiExpression rOperands,
@@ -906,7 +907,8 @@ public abstract class CodeBlockSurrounder {
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));
myCreatedIf = (PsiIfStatement)CodeStyleManager.getInstance(project)
.reformat(returnOrYieldStatement.getParent().addBefore(ifStatement, returnOrYieldStatement));
ct.replaceAndRestoreComments(Objects.requireNonNull(condition), rOperands);
return returnOrYieldStatement;
}
@@ -936,7 +938,7 @@ public abstract class CodeBlockSurrounder {
}
}
PsiIfStatement ifStatement = tryCast(PsiTreeUtil.skipWhitespacesAndCommentsBackward(anchor), PsiIfStatement.class);
if (ifStatement == null) return;
if (ifStatement == null || ifStatement != myCreatedIf) return;
PsiStatement result = collapseIf(ifStatement, "&&", "||");
if (result == null) return;
myUpstream.collapse(myUpstream.anchor(result));