introduce variable from yet standalone switch expression (IDEA-206523)

This commit is contained in:
Anna.Kozlova
2019-02-04 10:59:17 +01:00
parent 1fd8513825
commit 4a114d92f6
4 changed files with 29 additions and 1 deletions
@@ -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;
}
}
}
@@ -0,0 +1,8 @@
class MyTest {
{
String temp = switch (0) {
default:
break "";
};
}
}
@@ -0,0 +1,7 @@
class MyTest {
{
<selection>switch (0) {
default: break "";
}</selection>
}
}
@@ -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));
}