diff --git a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java index 94c8cf98a71b..e960e0246f20 100644 --- a/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java +++ b/java/java-impl/src/com/intellij/refactoring/introduceVariable/IntroduceVariableBase.java @@ -329,9 +329,18 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase { if (statements.length == 1) { if (statements[0] instanceof PsiExpressionStatement) { tempExpr = ((PsiExpressionStatement) statements[0]).getExpression(); - } else if (statements[0] instanceof PsiReturnStatement) { + } + else if (statements[0] instanceof PsiReturnStatement) { tempExpr = ((PsiReturnStatement)statements[0]).getReturnValue(); } + else if (statements[0] instanceof PsiSwitchStatement) { + PsiExpression expr = JavaPsiFacade.getElementFactory(project).createExpressionFromText(statements[0].getText(), statements[0]); + TextRange range = statements[0].getTextRange(); + final RangeMarker rangeMarker = FileDocumentManager.getInstance().getDocument(file.getVirtualFile()).createRangeMarker(range); + expr.putUserData(ElementToWorkOn.TEXT_RANGE, rangeMarker); + expr.putUserData(ElementToWorkOn.PARENT, statements[0]); + return expr; + } } } diff --git a/java/java-tests/testData/refactoring/introduceVariable/FromSwitchStatementJava12Preview.after.java b/java/java-tests/testData/refactoring/introduceVariable/FromSwitchStatementJava12Preview.after.java new file mode 100644 index 000000000000..30c3a423b77b --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceVariable/FromSwitchStatementJava12Preview.after.java @@ -0,0 +1,8 @@ +class MyTest { + { + String temp = switch (0) { + default: + break ""; + }; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/introduceVariable/FromSwitchStatementJava12Preview.java b/java/java-tests/testData/refactoring/introduceVariable/FromSwitchStatementJava12Preview.java new file mode 100644 index 000000000000..a6b62ae1ee19 --- /dev/null +++ b/java/java-tests/testData/refactoring/introduceVariable/FromSwitchStatementJava12Preview.java @@ -0,0 +1,7 @@ +class MyTest { + { + switch (0) { + default: break ""; + } + } +} \ No newline at end of file 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 73b2c338bea0..a5eee78cba49 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/IntroduceVariableTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/IntroduceVariableTest.java @@ -259,6 +259,10 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase { doTest(new MockIntroduceVariableHandler("temp", true, false, false, CommonClassNames.JAVA_LANG_STRING)); } + public void testFromSwitchStatementJava12Preview() { + doTest(new MockIntroduceVariableHandler("temp", true, false, false, CommonClassNames.JAVA_LANG_STRING)); + } + public void testDeclareTernary() { doTest(new MockIntroduceVariableHandler("temp", true, false, false, CommonClassNames.JAVA_LANG_STRING)); }