diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithCastSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithCastSurrounder.java index 84803828a7ef..61056086d01e 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithCastSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithCastSurrounder.java @@ -25,9 +25,7 @@ import com.intellij.openapi.editor.RangeMarker; import com.intellij.openapi.editor.ScrollType; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; -import com.intellij.psi.PsiBinaryExpression; -import com.intellij.psi.PsiExpression; -import com.intellij.psi.PsiType; +import com.intellij.psi.*; import com.intellij.refactoring.introduceField.ElementToWorkOn; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NonNls; @@ -45,7 +43,10 @@ class JavaWithCastSurrounder extends JavaExpressionSurrounder { public TextRange surroundExpression(final Project project, final Editor editor, PsiExpression expr) throws IncorrectOperationException { assert expr.isValid(); PsiType[] types = GuessManager.getInstance(project).guessTypeToCast(expr); - String exprText = expr instanceof PsiBinaryExpression ? "(" + expr.getText() + ")" : expr.getText(); + final boolean parenthesesNeeded = expr instanceof PsiPolyadicExpression || + expr instanceof PsiConditionalExpression || + expr instanceof PsiAssignmentExpression; + String exprText = parenthesesNeeded ? "(" + expr.getText() + ")" : expr.getText(); final Template template = generateTemplate(project, exprText, types); TextRange range; if (expr.isPhysical()) { diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundAssignmentWithCast.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundAssignmentWithCast.java new file mode 100644 index 000000000000..80227681479d --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundAssignmentWithCast.java @@ -0,0 +1,7 @@ +class Test { + void foo(int a, double b) { + if (a = b) { + + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundAssignmentWithCast_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundAssignmentWithCast_after.java new file mode 100644 index 000000000000..5233cdb555ee --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundAssignmentWithCast_after.java @@ -0,0 +1,7 @@ +class Test { + void foo(int a, double b) { + if ((() (a = b))) { + + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast.java index bd4c9bf756ef..bccc0460253a 100644 --- a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast.java +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast.java @@ -1,6 +1,6 @@ class Test { void foo(int a, double b) { - if (a + b) { + if (a + b + 1) { } } diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast_after.java index d72d903be7a6..1d908a694047 100644 --- a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast_after.java +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast_after.java @@ -1,6 +1,6 @@ class Test { void foo(int a, double b) { - if ((() (a + b))) { + if ((() (a + b + 1))) { } } diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundConditionalWithCast.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundConditionalWithCast.java new file mode 100644 index 000000000000..7b194592c5ce --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundConditionalWithCast.java @@ -0,0 +1,7 @@ +class Test { + void foo(int a, double b) { + if (true ? a : b) { + + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundConditionalWithCast_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundConditionalWithCast_after.java new file mode 100644 index 000000000000..16d9d00334ad --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundConditionalWithCast_after.java @@ -0,0 +1,7 @@ +class Test { + void foo(int a, double b) { + if ((() (true ? a : b))) { + + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java index 8356309aba85..78b7ca516c25 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java @@ -117,6 +117,28 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase { } } + public void testSurroundConditionalWithCast() { + final TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(getProject()); + templateManager.setTemplateTesting(true); + try { + doTest(getTestName(false), new JavaWithCastSurrounder()); + } + finally { + templateManager.setTemplateTesting(false); + } + } + + public void testSurroundAssignmentWithCast() { + final TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(getProject()); + templateManager.setTemplateTesting(true); + try { + doTest(getTestName(false), new JavaWithCastSurrounder()); + } + finally { + templateManager.setTemplateTesting(false); + } + } + private void doTest(@NotNull String fileName, final Surrounder surrounder) { configureByFile(BASE_PATH + fileName + ".java"); SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder);