[java-refactoring] CodeBlockSurrounder: do not fail when parentheses are removed by nested surrounder

Found by property tests

GitOrigin-RevId: 244eea39dbb352bb1486a99d2aab9c5e636693b1
This commit is contained in:
Tagir Valeev
2020-12-21 10:44:41 +00:00
committed by intellij-monorepo-bot
parent b03f6c2569
commit 9276b73b49
4 changed files with 24 additions and 2 deletions
@@ -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;
}
}
@@ -0,0 +1,7 @@
class X{
boolean test(String s1, String s2) {
return s1 == null || (s2.equals(s1.trim()) && <selection>s1.isEmpty()</selection>);
}
}
@@ -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[]") {
@@ -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();