IDEA-275893 [extract method]: align selection for switch statement rules

GitOrigin-RevId: 00c58a19d98bdc99b95cd228b1cc2fa746e9d301
This commit is contained in:
Alexandr Suhinin
2021-08-18 10:36:21 +00:00
committed by intellij-monorepo-bot
parent 6101375b0b
commit 3d9770213b
5 changed files with 58 additions and 5 deletions
@@ -41,6 +41,7 @@ class ExtractSelector {
singleElement is PsiBlockStatement -> if (singleElement.codeBlock.firstBodyElement != null) listOf(singleElement) else emptyList()
singleElement is PsiCodeBlock -> alignCodeBlock(singleElement)
singleElement is PsiExpression -> listOfNotNull(alignExpression(singleElement))
singleElement is PsiSwitchLabeledRuleStatement -> listOfNotNull(singleElement.body)
else -> elements
}
return when {
@@ -86,11 +87,8 @@ class ExtractSelector {
val filteredStatements = statements
.dropWhile { it is PsiSwitchLabelStatement || it is PsiWhiteSpace }
.dropLastWhile { it is PsiSwitchLabelStatement || it is PsiWhiteSpace }
return if (filteredStatements.any { it is PsiSwitchLabelStatement }) {
emptyList()
} else {
filteredStatements
}
if (filteredStatements.any { it is PsiSwitchLabelStatement || it is PsiSwitchLabeledRuleStatement }) return emptyList()
return filteredStatements
}
private fun isInsideAnnotation(expression: PsiExpression): Boolean {
@@ -0,0 +1,14 @@
package test;
public class Test1 {
void test(){
String s = "sample";
switch (s) {
<selection>case "one" -> {
System.out.println("one");
System.out.println("two");
}
default -> System.out.println();</selection>
}
}
}
@@ -0,0 +1,14 @@
package test;
public class Test1 {
void test(){
String s = "sample";
switch (s) {
<selection>case "one" -> {
System.out.println("one");
System.out.println("two");
}</selection>
default -> System.out.println();
}
}
}
@@ -0,0 +1,16 @@
package test;
public class Test1 {
void test(){
String s = "sample";
switch (s) {
case "one" -> extracted();
default -> System.out.println();
}
}
private void extracted() {
System.out.println("one");
System.out.println("two");
}
}
@@ -9,6 +9,7 @@ import com.intellij.openapi.util.Disposer
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.util.registry.Registry
import com.intellij.pom.java.LanguageLevel
import com.intellij.refactoring.RefactoringBundle
import com.intellij.refactoring.extractMethod.newImpl.MethodExtractor
import com.intellij.refactoring.listeners.RefactoringEventData
import com.intellij.refactoring.listeners.RefactoringEventListener
@@ -135,6 +136,16 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
doTest()
}
fun testDisabledOnSwitchRules(){
assertThrows(RefactoringErrorHintException::class.java, RefactoringBundle.message("selected.block.should.represent.a.set.of.statements.or.an.expression")) {
doTest()
}
}
fun testNormalizedOnSwitchRule(){
doTest()
}
fun testRefactoringListener(){
templateTest {
configureByFile("$BASE_PATH/${getTestName(false)}.java")