mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
IDEA-275892 [extract method]: align expression statements inside enhanced switch
GitOrigin-RevId: d2908c8bd460b4488f2c2a7c86c8f80899e2667c
This commit is contained in:
committed by
intellij-monorepo-bot
parent
3d9770213b
commit
fae79f2222
@@ -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
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -146,6 +146,14 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
|
||||
doTest()
|
||||
}
|
||||
|
||||
fun testExpressionStatementInSwitchExpression(){
|
||||
doTest()
|
||||
}
|
||||
|
||||
fun testExpressionStatementInSwitchStatement(){
|
||||
doTest()
|
||||
}
|
||||
|
||||
fun testRefactoringListener(){
|
||||
templateTest {
|
||||
configureByFile("$BASE_PATH/${getTestName(false)}.java")
|
||||
|
||||
Reference in New Issue
Block a user