From 68603732dcb66448e30ca522c33004866b79eec1 Mon Sep 17 00:00:00 2001 From: Alexandr Suhinin Date: Tue, 7 Jul 2020 10:54:19 +0300 Subject: [PATCH] EA-232230: extract method: choose correct anchor inside of field group GitOrigin-RevId: dfcb53fe75f0f0bc4dd73e2167c5f07a321f095b --- .../extractMethod/newImpl/ExtractMethodHelper.kt | 12 ++---------- .../extractMethodNew/FieldGroupAnchor.java | 3 +++ .../extractMethodNew/FieldGroupAnchor_after.java | 7 +++++++ .../java/refactoring/ExtractMethodNewTest.java | 4 ++++ 4 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 java/java-tests/testData/refactoring/extractMethodNew/FieldGroupAnchor.java create mode 100644 java/java-tests/testData/refactoring/extractMethodNew/FieldGroupAnchor_after.java 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(); }