extract method: disable in sub-selection of annotation

e.g. in package-info.java there is no default class to choose as target

GitOrigin-RevId: 578998ac49f4da01a7365523938e5983d1a639f4
This commit is contained in:
Anna Kozlova
2020-03-23 08:37:47 +01:00
committed by intellij-monorepo-bot
parent 69801c215d
commit fa4d7ba5a7
3 changed files with 14 additions and 3 deletions

View File

@@ -235,9 +235,6 @@ public class ExtractMethodProcessor implements MatchProvider {
}
}
if (PsiTreeUtil.getParentOfType(myElements[0], PsiAnnotation.class) != null) {
throw new PrepareFailedException("Unable to extract method from annotation value", myElements[0]);
}
final PsiElement codeFragment = ControlFlowUtil.findCodeFragment(myElements[0]);
myCodeFragmentMember = codeFragment.getUserData(ElementToWorkOn.PARENT);
if (myCodeFragmentMember == null) {
@@ -249,6 +246,10 @@ public class ExtractMethodProcessor implements MatchProvider {
myCodeFragmentMember = ControlFlowUtil.findCodeFragment(context).getParent();
}
if (PsiTreeUtil.getParentOfType(myElements[0].isPhysical() ? myElements[0] : myCodeFragmentMember, PsiAnnotation.class) != null) {
throw new PrepareFailedException("Unable to extract method from annotation value", myElements[0]);
}
myControlFlowWrapper = new ControlFlowWrapper(myProject, codeFragment, myElements);
try {

View File

@@ -0,0 +1 @@
@SuppressWarnings("b<selection>a</selection>r")

View File

@@ -1421,6 +1421,15 @@ public class ExtractMethodTest extends LightJavaCodeInsightTestCase {
}
}
public void testExtractFromAnnotation1() throws Exception {
try {
doTest();
fail("Should not work for annotations");
}
catch (PrepareFailedException ignore) {
}
}
private void doTestDisabledParam() throws PrepareFailedException {
final CommonCodeStyleSettings settings = CodeStyleSettingsManager.getSettings(getProject()).getCommonSettings(JavaLanguage.INSTANCE);
settings.ELSE_ON_NEW_LINE = true;