mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-03-22 15:19:59 +07:00
[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:
committed by
intellij-monorepo-bot
parent
e11b6cc846
commit
93592a4c9e
@@ -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));
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// IDEA-360579
|
||||
class Hades {
|
||||
void persephone(boolean xyz, int param) {
|
||||
if(xyz && !in<caret>lineMe(param)){
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
boolean inlineMe(int param) {
|
||||
boolean result = false;
|
||||
if (param >= 10) {
|
||||
result = param != 20;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// IDEA-360579
|
||||
class Hades {
|
||||
void persephone(boolean xyz, int param) {
|
||||
if(xyz) {
|
||||
boolean result = false;
|
||||
if (param >= 10) {
|
||||
result = param != 20;
|
||||
}
|
||||
if (!result) {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -592,6 +592,8 @@ public class InlineMethodTest extends LightRefactoringTestCase {
|
||||
|
||||
public void testRenameLocalClassDoubleConflict() { doTest(); }
|
||||
|
||||
public void testBooleanResultInIfChain() { doTest(); }
|
||||
|
||||
public void testInlineSingleImplementation() {
|
||||
TestDialogManager.setTestDialog(TestDialog.YES, getTestRootDisposable());
|
||||
doTest();
|
||||
|
||||
Reference in New Issue
Block a user