diff --git a/java/java-tests/testData/refactoring/introduceVariable/NestedAndOrParentheses.after.java b/java/java-tests/testData/refactoring/introduceVariable/NestedAndOrParentheses.after.java new file mode 100644 index 000000000000..e5104fbae714 --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceVariable/NestedAndOrParentheses.after.java @@ -0,0 +1,10 @@ +class X{ + + boolean test(String s1, String s2) { + if (s1 == null) return true; + if (!s2.equals(s1.trim())) return false; + boolean foo = s1.isEmpty(); + return foo; + } + +} diff --git a/java/java-tests/testData/refactoring/introduceVariable/NestedAndOrParentheses.java b/java/java-tests/testData/refactoring/introduceVariable/NestedAndOrParentheses.java new file mode 100644 index 000000000000..c0b8ef20662d --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceVariable/NestedAndOrParentheses.java @@ -0,0 +1,7 @@ +class X{ + + boolean test(String s1, String s2) { + return s1 == null || (s2.equals(s1.trim()) && s1.isEmpty()); + } + +} diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/IntroduceVariableTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/IntroduceVariableTest.java index d6583222eca9..fbb1ad308700 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/IntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/IntroduceVariableTest.java @@ -347,6 +347,7 @@ public class IntroduceVariableTest extends LightJavaCodeInsightTestCase { public void testCapturedWildcardUpperBoundSuggestedAsType() { doTest("m", false, false, false, "I"); } public void testArrayOfCapturedWildcardUpperBoundSuggestedAsType() { doTest("m", false, false, false, "I[]"); } public void testFieldFromLambda() { doTest("foo", false, false, true, "int"); } + public void testNestedAndOrParentheses() { doTest("foo", false, false, false, "boolean"); } public void testReturnNonExportedArray() { doTest(new MockIntroduceVariableHandler("i", false, false, false, "java.io.File[]") { diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/CodeBlockSurrounder.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/CodeBlockSurrounder.java index 6ff9182be24d..8476b6b8516a 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/CodeBlockSurrounder.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/CodeBlockSurrounder.java @@ -132,8 +132,12 @@ public abstract class CodeBlockSurrounder { * @return the expression that replaced the original expression */ public @NotNull CodeBlockSurrounder.SurroundResult surround() { - Object marker = new Object(); - PsiTreeUtil.mark(myExpression, marker); + Object marker = ObjectUtils.sentinel("CodeBlockSurrounder.MARKER"); + PsiExpression expr = PsiUtil.skipParenthesizedExprDown(myExpression); + if (expr == null) { + expr = myExpression; + } + PsiTreeUtil.mark(expr, marker); Project project = myExpression.getProject(); PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); boolean physical = myExpression.isPhysical();