From ccb1ae13ea6b47fa850e7d00e78167140493f021 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Tue, 19 Mar 2019 00:31:22 +0700 Subject: [PATCH] Fix local conflicts in 'Introduce import alias' --- .../KotlinIntroduceImportAliasHandler.kt | 18 ++++++++++----- .../conflictLocalName2.kt | 22 +++++++++++++++++++ .../conflictLocalName2.kt.after | 22 +++++++++++++++++++ .../intentions/IntentionTestGenerated.java | 5 +++++ 4 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 idea/testData/intentions/introduceImportAlias/conflictLocalName2.kt create mode 100644 idea/testData/intentions/introduceImportAlias/conflictLocalName2.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceImportAlias/KotlinIntroduceImportAliasHandler.kt b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceImportAlias/KotlinIntroduceImportAliasHandler.kt index 8fc579ae17b..db11a522ee8 100644 --- a/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceImportAlias/KotlinIntroduceImportAliasHandler.kt +++ b/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceImportAlias/KotlinIntroduceImportAliasHandler.kt @@ -17,6 +17,7 @@ import com.intellij.psi.search.searches.ReferencesSearch import com.intellij.refactoring.RefactoringActionHandler import com.intellij.refactoring.rename.inplace.VariableInplaceRenamer import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.resolveImportReference import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils import org.jetbrains.kotlin.idea.core.KotlinNameSuggester @@ -46,6 +47,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElementSelector import org.jetbrains.kotlin.psi.psiUtil.siblings import org.jetbrains.kotlin.resolve.PropertyImportedFromObject import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier import org.jetbrains.kotlin.resolve.scopes.utils.findPackage import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor @@ -70,15 +72,21 @@ object KotlinIntroduceImportAliasHandler : RefactoringActionHandler { } val oldName = element.mainReference.value - val scope = file.getResolutionScope() + val scopes = usages.mapNotNull { + val expression = it.reference.element + expression.getResolutionScope(expression.analyze(BodyResolveMode.PARTIAL_FOR_COMPLETION)) + }.distinct() + val validator = fun(name: String): Boolean { if (oldName == name) return false val identifier = Name.identifier(name) - return scope.getAllAccessibleFunctions(identifier).isEmpty() - && scope.getAllAccessibleVariables(identifier).isEmpty() - && scope.findClassifier(identifier, NoLookupLocation.FROM_IDE) == null - && scope.findPackage(identifier) == null + return scopes.all { scope -> + scope.getAllAccessibleFunctions(identifier).isEmpty() + && scope.getAllAccessibleVariables(identifier).isEmpty() + && scope.findClassifier(identifier, NoLookupLocation.FROM_IDE) == null + && scope.findPackage(identifier) == null + } } val suggestionsName = KotlinNameSuggester.suggestNamesByFqName(fqName, validator = validator) diff --git a/idea/testData/intentions/introduceImportAlias/conflictLocalName2.kt b/idea/testData/intentions/introduceImportAlias/conflictLocalName2.kt new file mode 100644 index 00000000000..a6051133c7b --- /dev/null +++ b/idea/testData/intentions/introduceImportAlias/conflictLocalName2.kt @@ -0,0 +1,22 @@ +import Outer.Middle.Inner.Companion.foo + +class Outer { + class Middle { + class Inner { + companion object { + fun foo() {} + } + } + } +} + +class Test() { + fun test2() { + val foo2 = 42 + foo() + } + fun test() { + val foo1 = 1 + foo() + } +} \ No newline at end of file diff --git a/idea/testData/intentions/introduceImportAlias/conflictLocalName2.kt.after b/idea/testData/intentions/introduceImportAlias/conflictLocalName2.kt.after new file mode 100644 index 00000000000..67b07d0153d --- /dev/null +++ b/idea/testData/intentions/introduceImportAlias/conflictLocalName2.kt.after @@ -0,0 +1,22 @@ +import Outer.Middle.Inner.Companion.foo as foo3 + +class Outer { + class Middle { + class Inner { + companion object { + fun foo() {} + } + } + } +} + +class Test() { + fun test2() { + val foo2 = 42 + foo3() + } + fun test() { + val foo1 = 1 + foo3() + } +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 354eb61bb54..d0031556d73 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -10080,6 +10080,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/introduceImportAlias/conflictLocalName.kt"); } + @TestMetadata("conflictLocalName2.kt") + public void testConflictLocalName2() throws Exception { + runTest("idea/testData/intentions/introduceImportAlias/conflictLocalName2.kt"); + } + @TestMetadata("conflictPackage.kt") public void testConflictPackage() throws Exception { runTest("idea/testData/intentions/introduceImportAlias/conflictPackage.kt");