diff --git a/java/java-impl/src/com/intellij/refactoring/extractMethod/newImpl/ExtractMethodHelper.kt b/java/java-impl/src/com/intellij/refactoring/extractMethod/newImpl/ExtractMethodHelper.kt index 91915abde842..f67f4d0bcacd 100644 --- a/java/java-impl/src/com/intellij/refactoring/extractMethod/newImpl/ExtractMethodHelper.kt +++ b/java/java-impl/src/com/intellij/refactoring/extractMethod/newImpl/ExtractMethodHelper.kt @@ -10,6 +10,7 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import com.intellij.psi.* import com.intellij.psi.codeStyle.JavaCodeStyleManager +import com.intellij.psi.formatter.java.MultipleFieldDeclarationHelper import com.intellij.psi.impl.source.DummyHolder import com.intellij.psi.impl.source.codeStyle.JavaCodeStyleManagerImpl import com.intellij.psi.search.GlobalSearchScope @@ -60,21 +61,12 @@ object ExtractMethodHelper { fun normalizedAnchor(anchor: PsiMember): PsiMember { return if (anchor is PsiField) { - findLastFieldInDeclaration(anchor) + MultipleFieldDeclarationHelper.findLastFieldInGroup(anchor.node).psi as? PsiField ?: anchor } else { anchor } } - private fun findLastFieldInDeclaration(field: PsiField): PsiField { - val nextSibling = PsiTreeUtil.skipWhitespacesForward(field) - return if (PsiUtil.getElementType(nextSibling) == JavaTokenType.COMMA) { - PsiTreeUtil.skipWhitespacesForward(nextSibling) as PsiField - } else { - field - } - } - fun addNullabilityAnnotation(owner: PsiModifierListOwner, nullability: Nullability) { val nullabilityManager = NullableNotNullManager.getInstance(owner.project) val annotation = when (nullability) { diff --git a/java/java-tests/testData/refactoring/extractMethodNew/FieldGroupAnchor.java b/java/java-tests/testData/refactoring/extractMethodNew/FieldGroupAnchor.java new file mode 100644 index 000000000000..b2dd0951d79e --- /dev/null +++ b/java/java-tests/testData/refactoring/extractMethodNew/FieldGroupAnchor.java @@ -0,0 +1,3 @@ +public class Test { + int x = 2 + 2, y, /*comment*/ z = 0; +} \ No newline at end of file diff --git a/java/java-tests/testData/refactoring/extractMethodNew/FieldGroupAnchor_after.java b/java/java-tests/testData/refactoring/extractMethodNew/FieldGroupAnchor_after.java new file mode 100644 index 000000000000..66ee5b8910ff --- /dev/null +++ b/java/java-tests/testData/refactoring/extractMethodNew/FieldGroupAnchor_after.java @@ -0,0 +1,7 @@ +public class Test { + int x = newMethod(), y, /*comment*/ z = 0; + + private int newMethod() { + return 2 + 2; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/refactoring/ExtractMethodNewTest.java b/java/java-tests/testSrc/com/intellij/java/refactoring/ExtractMethodNewTest.java index c221399723a6..282e6c013fba 100644 --- a/java/java-tests/testSrc/com/intellij/java/refactoring/ExtractMethodNewTest.java +++ b/java/java-tests/testSrc/com/intellij/java/refactoring/ExtractMethodNewTest.java @@ -213,6 +213,10 @@ public class ExtractMethodNewTest extends LightJavaCodeInsightTestCase { doTest(); } + public void testFieldGroupAnchor() throws Exception { + doTest(); + } + public void testSCR27887() throws Exception { doTest(); }