From e51edaedf726168073dc8edd8e3b14f58ab377ab Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 8 Mar 2012 15:17:42 +0400 Subject: [PATCH] IDEA-50592 'Surround with cast' for binary expressions (e.g. a+b) should parenthesize those expressions --- .../surroundWith/JavaWithCastSurrounder.java | 4 +++- .../surroundWith/java/SurroundBinaryWithCast.java | 7 +++++++ .../java/SurroundBinaryWithCast_after.java | 7 +++++++ .../java/SurroundNonExpressionWithCast_after.java | 2 +- .../surroundWith/JavaSurroundWithTest.java | 13 ++++++++++++- .../lang/surroundWith/SurroundExpressionTest.java | 1 + .../groovy/surround/expr/binaryWithCast.test | 3 +++ .../testdata/groovy/surround/expr/type_cast1.test | 2 +- 8 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast.java create mode 100644 java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast_after.java create mode 100644 plugins/groovy/testdata/groovy/surround/expr/binaryWithCast.test 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 7311aa4dac03..84803828a7ef 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,6 +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.refactoring.introduceField.ElementToWorkOn; @@ -44,7 +45,8 @@ 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); - final Template template = generateTemplate(project, expr.getText(), types); + String exprText = expr instanceof PsiBinaryExpression ? "(" + expr.getText() + ")" : expr.getText(); + final Template template = generateTemplate(project, exprText, types); TextRange range; if (expr.isPhysical()) { range = expr.getTextRange(); diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast.java new file mode 100644 index 000000000000..bd4c9bf756ef --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast.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_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast_after.java new file mode 100644 index 000000000000..d72d903be7a6 --- /dev/null +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast_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/SurroundNonExpressionWithCast_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundNonExpressionWithCast_after.java index 26f446823122..659e44293c6e 100644 --- a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundNonExpressionWithCast_after.java +++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundNonExpressionWithCast_after.java @@ -1,7 +1,7 @@ class Test { void foo() { String a = "", b = "", c = ""; - if (a + (() b + c)) { + if (a + (() (b + c))) { } } 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 9094d244ae80..8356309aba85 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 @@ -106,7 +106,18 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase { doTest(getTestName(false), new JavaWithNotSurrounder()); } - private void doTest(@NotNull String fileName, final Surrounder surrounder) throws Exception { + public void testSurroundBinaryWithCast() { + 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); checkResultByFile(BASE_PATH + fileName + "_after.java"); diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundExpressionTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundExpressionTest.java index 7eb43b286ac3..d615404d4ec8 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundExpressionTest.java +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundExpressionTest.java @@ -15,6 +15,7 @@ public class SurroundExpressionTest extends SurroundTestCase { public void testType_cast2() throws Exception { doTest(new TypeCastSurrounder()); } public void testWhile1() throws Exception { doTest(new WhileExprSurrounder()); } public void testWith2() throws Exception { doTest(new WithExprSurrounder()); } + public void testBinaryWithCast() throws Exception { doTest(new TypeCastSurrounder()); } @Override protected String getBasePath() { diff --git a/plugins/groovy/testdata/groovy/surround/expr/binaryWithCast.test b/plugins/groovy/testdata/groovy/surround/expr/binaryWithCast.test new file mode 100644 index 000000000000..cf498cd0427e --- /dev/null +++ b/plugins/groovy/testdata/groovy/surround/expr/binaryWithCast.test @@ -0,0 +1,3 @@ +a + b +----- +(() (a + b)) \ No newline at end of file diff --git a/plugins/groovy/testdata/groovy/surround/expr/type_cast1.test b/plugins/groovy/testdata/groovy/surround/expr/type_cast1.test index fc381f74e4f6..d40773e67492 100644 --- a/plugins/groovy/testdata/groovy/surround/expr/type_cast1.test +++ b/plugins/groovy/testdata/groovy/surround/expr/type_cast1.test @@ -1,3 +1,3 @@ expr ----- -(() expr) \ No newline at end of file +(() expr) \ No newline at end of file