From 56ce334d568a0601e060eb488fb0d5205a3b2ce3 Mon Sep 17 00:00:00 2001 From: "Anna.Kozlova" Date: Thu, 6 Jul 2017 11:11:44 +0200 Subject: [PATCH] disable surround with () for void expressions (IDEA-175462) --- .../surroundWith/JavaWithCastSurrounder.java | 2 +- .../JavaWithParenthesesSurrounder.java | 2 +- .../surroundWith/JavaSurroundWithTest.java | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) 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 010f8d3b0cdf..47f51bdcd5a5 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 @@ -38,7 +38,7 @@ public class JavaWithCastSurrounder extends JavaExpressionSurrounder { @Override public boolean isApplicable(PsiExpression expr) { - return true; + return !PsiType.VOID.equals(expr.getType()); } @Override diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithParenthesesSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithParenthesesSurrounder.java index b3ba90ea4531..dc15c7e68d6e 100644 --- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithParenthesesSurrounder.java +++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithParenthesesSurrounder.java @@ -28,7 +28,7 @@ import com.intellij.util.IncorrectOperationException; public class JavaWithParenthesesSurrounder extends JavaExpressionSurrounder{ @Override public boolean isApplicable(PsiExpression expr) { - return true; + return !PsiType.VOID.equals(expr.getType()); } @Override diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/generation/surroundWith/JavaSurroundWithTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/generation/surroundWith/JavaSurroundWithTest.java index 910e8f23acfc..22a0882fcba0 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/generation/surroundWith/JavaSurroundWithTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/generation/surroundWith/JavaSurroundWithTest.java @@ -31,7 +31,9 @@ import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.editor.SelectionModel; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiExpression; import com.intellij.psi.codeStyle.CommonCodeStyleSettings; +import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; import com.intellij.testFramework.LightCodeInsightTestCase; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; @@ -258,4 +260,19 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase { return text != null && text.contains("while"); })); } + + public void testExcludeVoidExpressions() { + configureFromFileText("a.java", + "class Foo {\n" + + " void bar() {\n" + + " System.out.println();\n" + + " }\n" + + "}"); + SelectionModel model = myEditor.getSelectionModel(); + PsiExpression expr = + IntroduceVariableBase.getSelectedExpression(myFile.getProject(), myFile, model.getSelectionStart(), model.getSelectionEnd()); + assertNotNull(expr); + assertFalse(new JavaWithParenthesesSurrounder().isApplicable(expr)); + assertFalse(new JavaWithCastSurrounder().isApplicable(expr)); + } }