Java: Don't offer "surround with statement" actions for 'case' and 'default' clauses (IDEA-204012)

This commit is contained in:
Pavel Dolgov
2019-01-18 14:56:40 +03:00
parent aeb696fd59
commit 51dbcef667
5 changed files with 53 additions and 1 deletions
@@ -24,13 +24,14 @@ import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.TextRange;
import com.intellij.util.IncorrectOperationException;
import com.intellij.lang.surroundWith.Surrounder;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
abstract class JavaStatementsSurrounder implements Surrounder {
@Override
public boolean isApplicable(@NotNull PsiElement[] elements) {
return true;
return ContainerUtil.find(elements, PsiSwitchLabelStatementBase.class::isInstance) == null;
}
@Override
@@ -0,0 +1,8 @@
class C {
void foo(int n) {
switch (n) {
case 1:
<selection>break;</selection>
}
}
}
@@ -0,0 +1,10 @@
class C {
void foo(int n) {
switch (n) {
case 1:
if () {
break;
}
}
}
}
@@ -0,0 +1,8 @@
class C {
void foo(int n) {
switch (n) {
<selection>case 1:</selection>
break;
}
}
}
@@ -92,6 +92,14 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase {
doTest(new JavaWithIfSurrounder());
}
public void testSurroundSwitchCaseWithIf() {
doTestNotApplicable(new JavaWithIfSurrounder());
}
public void testSurroundSwitchBreakWithIf() {
doTest(getTestName(false), new JavaWithIfSurrounder());
}
public void testSurroundNonExpressionWithParenthesis() {
doTest(new JavaWithParenthesesSurrounder());
}
@@ -225,6 +233,23 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase {
checkResultByFile(BASE_PATH + fileName + "_after.java");
}
private void doTestNotApplicable(Surrounder surrounder) {
configureByFile(BASE_PATH + getTestName(false) + ".java");
SelectionModel selectionModel = getEditor().getSelectionModel();
List<SurroundDescriptor> descriptors = LanguageSurrounders.INSTANCE.allForLanguage(JavaLanguage.INSTANCE);
boolean atLeastOneFound = false;
for (SurroundDescriptor item : descriptors) {
PsiElement[] elements = item.getElementsToSurround(getFile(), selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
if (elements.length != 0) {
assertFalse("applicable " + item, surrounder.isApplicable(elements));
atLeastOneFound = true;
}
}
assertTrue("atLeastOneFound", atLeastOneFound);
}
private void doTestWithTemplateFinish(@NotNull String fileName, Surrounder surrounder, @Nullable String textToType) {
TemplateManagerImpl.setTemplateTesting(getTestRootDisposable());
configureByFile(BASE_PATH + fileName + ".java");