[groovy] hand qualification check logic over to extensions
authorDaniil Ovchinnikov <daniil.ovchinnikov@jetbrains.com>
Tue, 1 Nov 2016 10:15:02 +0000 (13:15 +0300)
committerDaniil Ovchinnikov <daniil.ovchinnikov@jetbrains.com>
Tue, 1 Nov 2016 10:32:33 +0000 (13:32 +0300)
plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/rename/GrRenameHelper.java
plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/rename/RenameGrFieldProcessor.java
plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/rename/renameHelper.kt
plugins/groovy/src/org/jetbrains/plugins/groovy/transformations/impl/DefaultRenameHelper.kt
plugins/groovy/src/org/jetbrains/plugins/groovy/transformations/impl/TraitRenameHelper.kt

index be7e1032bf779d54f0eb0972dc4764d409ed0668..fcd885bbc4387f6dcb12870855910f5e5e2faa9a 100644 (file)
@@ -16,6 +16,8 @@
 package org.jetbrains.plugins.groovy.refactoring.rename;
 
 import com.intellij.openapi.extensions.ExtensionPointName;
+import com.intellij.psi.PsiElement;
+import com.intellij.psi.PsiManager;
 import com.intellij.psi.PsiMember;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
@@ -47,5 +49,16 @@ public interface GrRenameHelper {
    * @see DefaultRenameHelper
    */
   @Nullable
-  String getNewMemberName(@NotNull PsiMember member, @NotNull String newOriginalName);
+  default String getNewMemberName(@NotNull PsiMember member, @NotNull String newOriginalName) {
+    return null;
+  }
+
+  /**
+   * @param manager
+   * @param before element to which the reference resolved before rename
+   * @param after  element to which the reference resolves after rename
+   */
+  default boolean isQualificationNeeded(@NotNull PsiManager manager, @NotNull PsiElement before, @NotNull PsiElement after) {
+    return false;
+  }
 }
index 5c6985af2cec4cd3e9e2979180990bd249ebf5dc..e26e37961fd64125b0de04061cb97f8e8ebced01 100644 (file)
@@ -51,6 +51,7 @@ import java.util.List;
 import java.util.Map;
 
 import static org.jetbrains.plugins.groovy.refactoring.rename.RenameHelperKt.getNewNameFromTransformations;
+import static org.jetbrains.plugins.groovy.refactoring.rename.RenameHelperKt.isQualificationNeeded;
 
 /**
  * @author ilyas
@@ -100,13 +101,16 @@ public class RenameGrFieldProcessor extends RenameJavaVariableProcessor {
 
     field.setName(newName);
 
+    PsiManager manager = psiElement.getManager();
     for (GrReferenceExpression expression : handled.keySet()) {
       PsiElement oldResolved = handled.get(expression);
       if (oldResolved == null) continue;
       PsiElement resolved = expression.resolve();
       if (resolved == null) continue;
-      if (expression.getManager().areElementsEquivalent(oldResolved, resolved)) continue;
-      qualify(field, expression);
+      if (manager.areElementsEquivalent(oldResolved, resolved)) continue;
+      if (oldResolved.equals(field) || isQualificationNeeded(manager, oldResolved, resolved)) {
+        qualify(field, expression);
+      }
     }
 
     if (listener != null) {
index 1b835d558838a85d2f2e53e19d90381b0038571d..8b420ab236648b15cb99429ded9b2de0e37efc1e 100644 (file)
@@ -16,6 +16,7 @@
 package org.jetbrains.plugins.groovy.refactoring.rename
 
 import com.intellij.psi.PsiElement
+import com.intellij.psi.PsiManager
 import com.intellij.psi.PsiMember
 
 fun PsiElement?.getNewNameFromTransformations(newName: String) = (this as? PsiMember)?.let {
@@ -29,4 +30,8 @@ private fun doGetNewNameFromTransformations(member: PsiMember, newName: String):
     if (newMemberName != null) return newMemberName
   }
   return null
+}
+
+fun isQualificationNeeded(manager: PsiManager, before: PsiElement, after: PsiElement) = GrRenameHelper.EP_NAME.extensions.any {
+  it.isQualificationNeeded(manager, before, after)
 }
\ No newline at end of file
index 19c9558957cf35bebecc9f65c4ac4a3d5ae72074..3d43309587e12ad0464fa71fbb20277358b47a01 100644 (file)
@@ -15,6 +15,8 @@
  */
 package org.jetbrains.plugins.groovy.transformations.impl
 
+import com.intellij.psi.PsiElement
+import com.intellij.psi.PsiManager
 import com.intellij.psi.PsiMember
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod
 import org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils.getAccessorName
@@ -32,4 +34,10 @@ class DefaultRenameHelper : GrRenameHelper {
       getAccessorName(if (member.name.startsWith("is")) "is" else "get", newOriginalName)
     }
   }
+
+  override fun isQualificationNeeded(manager: PsiManager,
+                                     before: PsiElement,
+                                     after: PsiElement): Boolean {
+    return before is GrAccessorMethod && (after !is GrAccessorMethod || !manager.areElementsEquivalent(before.property, after.property))
+  }
 }
\ No newline at end of file
index 90f0e0969bce2ba7ab0d420e12599f57633fe8e2..673c6dee342a937e5eb825286a3b56cfbbf62958 100644 (file)
@@ -15,6 +15,8 @@
  */
 package org.jetbrains.plugins.groovy.transformations.impl
 
+import com.intellij.psi.PsiElement
+import com.intellij.psi.PsiManager
 import com.intellij.psi.PsiMember
 import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrTraitField
 import org.jetbrains.plugins.groovy.lang.psi.util.GrTraitUtil
@@ -28,4 +30,10 @@ class TraitRenameHelper : GrRenameHelper {
     val containingClass = prototype.containingClass ?: return null
     return GrTraitUtil.getTraitFieldPrefix(containingClass) + newOriginalName
   }
+
+  override fun isQualificationNeeded(manager: PsiManager,
+                                     before: PsiElement,
+                                     after: PsiElement): Boolean {
+    return before is GrTraitField && (after !is GrTraitField || !manager.areElementsEquivalent(after.prototype, before.prototype))
+  }
 }
\ No newline at end of file