From b51ae78b84f933abd4de03e65734403c2f7b72e3 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Thu, 9 Apr 2020 20:38:01 +0000 Subject: [PATCH] Do not resolve alias imported references on rename #KT-38096 Fixed --- .../KotlinReferencesSearcher.kt | 22 +++++++++++---- .../RenameKotlinPropertyProcessorCompat.kt | 12 ++------ .../rename/RenameKotlinPsiProcessor.kt | 5 +++- .../rename/RenameKotlinPsiProcessor.kt.191 | 5 +++- .../rename/RenameKotlinPsiProcessorCompat.kt | 28 ++++++++++++++----- .../propertyImportAliasByRef/after/test.kt | 12 ++++++++ .../propertyImportAliasByRef/before/test.kt | 12 ++++++++ .../propertyImportAliasByRef.test | 5 ++++ .../rename/RenameTestGenerated.java | 5 ++++ 9 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 idea/testData/refactoring/rename/propertyImportAliasByRef/after/test.kt create mode 100644 idea/testData/refactoring/rename/propertyImportAliasByRef/before/test.kt create mode 100644 idea/testData/refactoring/rename/propertyImportAliasByRef/propertyImportAliasByRef.test diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/ideaExtensions/KotlinReferencesSearcher.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/ideaExtensions/KotlinReferencesSearcher.kt index 74da14e67aa..170e2644d09 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/ideaExtensions/KotlinReferencesSearcher.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/ideaExtensions/KotlinReferencesSearcher.kt @@ -21,6 +21,7 @@ import com.intellij.openapi.util.text.StringUtil import com.intellij.psi.* import com.intellij.psi.impl.cache.CacheManager import com.intellij.psi.search.* +import com.intellij.psi.search.searches.MethodReferencesSearch import com.intellij.psi.search.searches.ReferencesSearch import com.intellij.util.Processor import com.intellij.util.containers.nullize @@ -73,7 +74,7 @@ data class KotlinReferencesSearchOptions( elementToSearch: PsiNamedElement, parameters: ReferencesSearch.SearchParameters ): SearchScope { - val kotlinOptions = (parameters as? KotlinReferencesSearchParameters)?.kotlinOptions ?: Empty + val kotlinOptions = (parameters as? KotlinAwareReferencesSearchParameters)?.kotlinOptions ?: Empty val elements = if (elementToSearch is KtDeclaration && !isOnlyKotlinSearch(parameters.scopeDeterminedByUser)) { elementToSearch.toLightElements().filterDataClassComponentsIfDisabled(kotlinOptions).nullize() } else { @@ -87,17 +88,28 @@ data class KotlinReferencesSearchOptions( } } +interface KotlinAwareReferencesSearchParameters { + val kotlinOptions: KotlinReferencesSearchOptions +} + class KotlinReferencesSearchParameters( elementToSearch: PsiElement, scope: SearchScope = runReadAction { elementToSearch.project.allScope() }, ignoreAccessScope: Boolean = false, optimizer: SearchRequestCollector? = null, - val kotlinOptions: KotlinReferencesSearchOptions = Empty -) : ReferencesSearch.SearchParameters(elementToSearch, scope, ignoreAccessScope, optimizer) + override val kotlinOptions: KotlinReferencesSearchOptions = Empty +) : ReferencesSearch.SearchParameters(elementToSearch, scope, ignoreAccessScope, optimizer), KotlinAwareReferencesSearchParameters + +class KotlinMethodReferencesSearchParameters( + elementToSearch: PsiMethod, + scope: SearchScope = runReadAction { elementToSearch.project.allScope() }, + strictSignatureSearch: Boolean = true, + override val kotlinOptions: KotlinReferencesSearchOptions = Empty +) : MethodReferencesSearch.SearchParameters(elementToSearch, scope, strictSignatureSearch), KotlinAwareReferencesSearchParameters class KotlinAliasedImportedElementSearcher : QueryExecutorBase(true) { override fun processQuery(parameters: ReferencesSearch.SearchParameters, consumer: Processor) { - val kotlinOptions = (parameters as? KotlinReferencesSearchParameters)?.kotlinOptions ?: Empty + val kotlinOptions = (parameters as? KotlinAwareReferencesSearchParameters)?.kotlinOptions ?: Empty if (!kotlinOptions.acceptImportAlias) return val element = parameters.elementToSearch if (!element.isValid) return @@ -142,7 +154,7 @@ class KotlinReferencesSearcher : QueryExecutorBase) { - private val kotlinOptions = (queryParameters as? KotlinReferencesSearchParameters)?.kotlinOptions ?: Empty + private val kotlinOptions = (queryParameters as? KotlinAwareReferencesSearchParameters)?.kotlinOptions ?: Empty private val longTasks = ArrayList<() -> Unit>() diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPropertyProcessorCompat.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPropertyProcessorCompat.kt index c3ae72e7167..3f562f53c9a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPropertyProcessorCompat.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPropertyProcessorCompat.kt @@ -15,9 +15,7 @@ import com.intellij.psi.search.SearchScope import com.intellij.psi.search.searches.DirectClassInheritorsSearch import com.intellij.psi.search.searches.OverridingMethodsSearch import com.intellij.refactoring.RefactoringBundle -import com.intellij.refactoring.listeners.RefactoringElementListener import com.intellij.refactoring.rename.RenameProcessor -import com.intellij.refactoring.util.MoveRenameUsageInfo import com.intellij.refactoring.util.RefactoringUtil import com.intellij.usageView.UsageInfo import com.intellij.usageView.UsageViewUtil @@ -37,15 +35,10 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.caches.resolve.unsafeResolveToDescriptor import org.jetbrains.kotlin.idea.core.getDeepestSuperDeclarations -import org.jetbrains.kotlin.idea.core.isEnumCompanionPropertyWithEntryConflict -import org.jetbrains.kotlin.idea.core.unquote import org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringSettings import org.jetbrains.kotlin.idea.refactoring.checkSuperMethodsWithPopup -import org.jetbrains.kotlin.idea.refactoring.dropOverrideKeywordIfNecessary import org.jetbrains.kotlin.idea.references.KtDestructuringDeclarationReference import org.jetbrains.kotlin.idea.references.KtReference -import org.jetbrains.kotlin.idea.references.KtSimpleNameReference -import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.util.application.runReadAction import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.load.java.JvmAbi @@ -53,9 +46,7 @@ import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject import org.jetbrains.kotlin.psi.psiUtil.findPropertyByName -import org.jetbrains.kotlin.psi.psiUtil.quoteIfNeeded import org.jetbrains.kotlin.resolve.BindingContext -import org.jetbrains.kotlin.resolve.DataClassDescriptorResolver import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.source.getPsi @@ -67,7 +58,8 @@ import org.jetbrains.kotlin.utils.SmartList abstract class RenameKotlinPropertyProcessorCompat : RenameKotlinPsiProcessor() { override fun canProcessElement(element: PsiElement): Boolean { val namedUnwrappedElement = element.namedUnwrappedElement - return namedUnwrappedElement is KtProperty || (namedUnwrappedElement is KtParameter && namedUnwrappedElement.hasValOrVar()) + return namedUnwrappedElement is KtProperty || namedUnwrappedElement is PropertyMethodWrapper || + (namedUnwrappedElement is KtParameter && namedUnwrappedElement.hasValOrVar()) } override fun isToSearchInComments(psiElement: PsiElement) = KotlinRefactoringSettings.instance.RENAME_SEARCH_IN_COMMENTS_FOR_FIELD diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPsiProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPsiProcessor.kt index 56cf72a2168..640a542a64a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPsiProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPsiProcessor.kt @@ -22,7 +22,10 @@ abstract class RenameKotlinPsiProcessor : RenameKotlinPsiProcessorCompat() { val searchParameters = KotlinReferencesSearchParameters( element, searchScope, - kotlinOptions = KotlinReferencesSearchOptions(searchForComponentConventions = false) + kotlinOptions = KotlinReferencesSearchOptions( + searchForComponentConventions = false, + acceptImportAlias = false + ) ) return findReferences(element, searchParameters) } diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPsiProcessor.kt.191 b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPsiProcessor.kt.191 index 2d2b8922bf6..d74c46d0eca 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPsiProcessor.kt.191 +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPsiProcessor.kt.191 @@ -19,7 +19,10 @@ abstract class RenameKotlinPsiProcessor : RenameKotlinPsiProcessorCompat() { val searchParameters = KotlinReferencesSearchParameters( element, element.project.projectScope() or element.useScope, - kotlinOptions = KotlinReferencesSearchOptions(searchForComponentConventions = false) + kotlinOptions = KotlinReferencesSearchOptions( + searchForComponentConventions = false, + acceptImportAlias = false + ) ) return findReferences(element, searchParameters) } diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPsiProcessorCompat.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPsiProcessorCompat.kt index c5ead349ef3..49ab5dd8cdc 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPsiProcessorCompat.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/rename/RenameKotlinPsiProcessorCompat.kt @@ -22,15 +22,15 @@ import org.jetbrains.kotlin.asJava.toLightMethods import org.jetbrains.kotlin.asJava.unwrapped import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper import org.jetbrains.kotlin.idea.references.KtReference +import org.jetbrains.kotlin.idea.references.getImportAlias +import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinMethodReferencesSearchParameters +import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchOptions import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchParameters import org.jetbrains.kotlin.idea.util.actualsForExpected import org.jetbrains.kotlin.idea.util.liftToExpected import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType -import org.jetbrains.kotlin.psi.psiUtil.isIdentifier -import org.jetbrains.kotlin.psi.psiUtil.parents -import org.jetbrains.kotlin.psi.psiUtil.quoteIfNeeded +import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.resolve.ImportPath import java.util.ArrayList import kotlin.collections.* @@ -56,12 +56,26 @@ abstract class RenameKotlinPsiProcessorCompat : RenamePsiElementProcessor() { protected fun findReferences( element: PsiElement, searchParameters: KotlinReferencesSearchParameters - ): MutableSet { + ): Collection { val references = ReferencesSearch.search(searchParameters).toMutableSet() if (element is KtNamedFunction || (element is KtProperty && !element.isLocal) || (element is KtParameter && element.hasValOrVar())) { - element.toLightMethods().flatMapTo(references) { MethodReferencesSearch.search(it) } + element.toLightMethods().flatMapTo(references) { method -> + MethodReferencesSearch.search( + KotlinMethodReferencesSearchParameters( + method, + kotlinOptions = KotlinReferencesSearchOptions( + acceptImportAlias = false + ) + ) + ) + } + } + return references.filter { + // have to filter so far as + // - text-matched reference could be named as imported alias and found in ReferencesSearch + // - MethodUsagesSearcher could create its own MethodReferencesSearchParameters regardless provided one + it.element.getNonStrictParentOfType() != null || it.getImportAlias() == null } - return references } override fun createUsageInfo(element: PsiElement, ref: PsiReference, referenceElement: PsiElement): UsageInfo { diff --git a/idea/testData/refactoring/rename/propertyImportAliasByRef/after/test.kt b/idea/testData/refactoring/rename/propertyImportAliasByRef/after/test.kt new file mode 100644 index 00000000000..aaebd6e62f1 --- /dev/null +++ b/idea/testData/refactoring/rename/propertyImportAliasByRef/after/test.kt @@ -0,0 +1,12 @@ +import Some.referWithoutAlias +import Some.referWithAli as referWithAlias + +fun referRenamed() { + println(referWithoutAlias) + println(referWithAlias) +} + +object Some { + var referWithoutAlias = 0 + var referWithAli = 0 +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/propertyImportAliasByRef/before/test.kt b/idea/testData/refactoring/rename/propertyImportAliasByRef/before/test.kt new file mode 100644 index 00000000000..379ea1a966d --- /dev/null +++ b/idea/testData/refactoring/rename/propertyImportAliasByRef/before/test.kt @@ -0,0 +1,12 @@ +import Some.referWithoutAlias +import Some.referWithAlias as referWithAlias + +fun referRenamed() { + println(referWithoutAlias) + println(referWithAlias) +} + +object Some { + var referWithoutAlias = 0 + var referWithAlias/*rename*/ = 0 +} \ No newline at end of file diff --git a/idea/testData/refactoring/rename/propertyImportAliasByRef/propertyImportAliasByRef.test b/idea/testData/refactoring/rename/propertyImportAliasByRef/propertyImportAliasByRef.test new file mode 100644 index 00000000000..7f96d932dc7 --- /dev/null +++ b/idea/testData/refactoring/rename/propertyImportAliasByRef/propertyImportAliasByRef.test @@ -0,0 +1,5 @@ +{ + "type": "AUTO_DETECT", + "mainFile": "test.kt", + "newName": "referWithAli" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java index 6015f17bdb7..08eb2f3497a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/rename/RenameTestGenerated.java @@ -483,6 +483,11 @@ public class RenameTestGenerated extends AbstractRenameTest { runTest("idea/testData/refactoring/rename/propertyAccidentalOverrideSuperclass/propertyAccidentalOverrideSuperclass.test"); } + @TestMetadata("propertyImportAliasByRef/propertyImportAliasByRef.test") + public void testPropertyImportAliasByRef_PropertyImportAliasByRef() throws Exception { + runTest("idea/testData/refactoring/rename/propertyImportAliasByRef/propertyImportAliasByRef.test"); + } + @TestMetadata("propertyParameterAccidentalOverrideSubclass/propertyParameterAccidentalOverrideSubclass.test") public void testPropertyParameterAccidentalOverrideSubclass_PropertyParameterAccidentalOverrideSubclass() throws Exception { runTest("idea/testData/refactoring/rename/propertyParameterAccidentalOverrideSubclass/propertyParameterAccidentalOverrideSubclass.test");