IDEA-275892 [extract method]: align expression statements inside enhanced switch

GitOrigin-RevId: d2908c8bd460b4488f2c2a7c86c8f80899e2667c
This commit is contained in:
Alexandr Suhinin
2021-08-18 12:04:34 +03:00
committed by intellij-monorepo-bot
parent 3d9770213b
commit fae79f2222
6 changed files with 84 additions and 0 deletions

View File

@@ -42,6 +42,7 @@ class ExtractSelector {
singleElement is PsiCodeBlock -> alignCodeBlock(singleElement)
singleElement is PsiExpression -> listOfNotNull(alignExpression(singleElement))
singleElement is PsiSwitchLabeledRuleStatement -> listOfNotNull(singleElement.body)
singleElement is PsiExpressionStatement -> listOf(alignExpressionStatement(singleElement))
else -> elements
}
return when {
@@ -52,6 +53,13 @@ class ExtractSelector {
}
}
private fun alignExpressionStatement(statement: PsiExpressionStatement): PsiElement {
val switchRule = statement.parent as? PsiSwitchLabeledRuleStatement
val switchExpression = PsiTreeUtil.getParentOfType(switchRule, PsiSwitchExpression::class.java)
if (switchExpression != null) return statement.expression
return statement
}
private fun isControlFlowStatement(statement: PsiStatement?): Boolean {
return statement is PsiBreakStatement || statement is PsiContinueStatement || statement is PsiReturnStatement || statement is PsiYieldStatement
}

View File

@@ -0,0 +1,15 @@
package test;
public class Test1 {
void test(){
String s = "sample";
String result = switch (s) {
case "one" -> <selection>foo();</selection>
default -> "default";
};
}
String foo(){
return "42";
}
}

View File

@@ -0,0 +1,19 @@
package test;
public class Test1 {
void test(){
String s = "sample";
String result = switch (s) {
case "one" -> getFoo();
default -> "default";
};
}
private String getFoo() {
return foo();
}
String foo(){
return "42";
}
}

View File

@@ -0,0 +1,15 @@
package test;
public class Test1 {
void test(){
String s = "sample";
switch (s) {
case "one" -> <selection>foo();</selection>
default -> System.out.println();
};
}
String foo(){
return "42";
}
}

View File

@@ -0,0 +1,19 @@
package test;
public class Test1 {
void test(){
String s = "sample";
switch (s) {
case "one" -> extracted();
default -> System.out.println();
};
}
private void extracted() {
foo();
}
String foo(){
return "42";
}
}

View File

@@ -146,6 +146,14 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
doTest()
}
fun testExpressionStatementInSwitchExpression(){
doTest()
}
fun testExpressionStatementInSwitchStatement(){
doTest()
}
fun testRefactoringListener(){
templateTest {
configureByFile("$BASE_PATH/${getTestName(false)}.java")