Fix local conflicts in 'Introduce import alias'
This commit is contained in:
+13
-5
@@ -17,6 +17,7 @@ import com.intellij.psi.search.searches.ReferencesSearch
|
|||||||
import com.intellij.refactoring.RefactoringActionHandler
|
import com.intellij.refactoring.RefactoringActionHandler
|
||||||
import com.intellij.refactoring.rename.inplace.VariableInplaceRenamer
|
import com.intellij.refactoring.rename.inplace.VariableInplaceRenamer
|
||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
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.caches.resolve.resolveImportReference
|
||||||
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils
|
import org.jetbrains.kotlin.idea.codeInsight.CodeInsightUtils
|
||||||
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
|
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.psi.psiUtil.siblings
|
||||||
import org.jetbrains.kotlin.resolve.PropertyImportedFromObject
|
import org.jetbrains.kotlin.resolve.PropertyImportedFromObject
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
|
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.findClassifier
|
||||||
import org.jetbrains.kotlin.resolve.scopes.utils.findPackage
|
import org.jetbrains.kotlin.resolve.scopes.utils.findPackage
|
||||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor
|
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor
|
||||||
@@ -70,15 +72,21 @@ object KotlinIntroduceImportAliasHandler : RefactoringActionHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val oldName = element.mainReference.value
|
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 {
|
val validator = fun(name: String): Boolean {
|
||||||
if (oldName == name) return false
|
if (oldName == name) return false
|
||||||
val identifier = Name.identifier(name)
|
val identifier = Name.identifier(name)
|
||||||
|
|
||||||
return scope.getAllAccessibleFunctions(identifier).isEmpty()
|
return scopes.all { scope ->
|
||||||
&& scope.getAllAccessibleVariables(identifier).isEmpty()
|
scope.getAllAccessibleFunctions(identifier).isEmpty()
|
||||||
&& scope.findClassifier(identifier, NoLookupLocation.FROM_IDE) == null
|
&& scope.getAllAccessibleVariables(identifier).isEmpty()
|
||||||
&& scope.findPackage(identifier) == null
|
&& scope.findClassifier(identifier, NoLookupLocation.FROM_IDE) == null
|
||||||
|
&& scope.findPackage(identifier) == null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val suggestionsName = KotlinNameSuggester.suggestNamesByFqName(fqName, validator = validator)
|
val suggestionsName = KotlinNameSuggester.suggestNamesByFqName(fqName, validator = validator)
|
||||||
|
|||||||
@@ -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<caret>()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10080,6 +10080,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
runTest("idea/testData/intentions/introduceImportAlias/conflictLocalName.kt");
|
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")
|
@TestMetadata("conflictPackage.kt")
|
||||||
public void testConflictPackage() throws Exception {
|
public void testConflictPackage() throws Exception {
|
||||||
runTest("idea/testData/intentions/introduceImportAlias/conflictPackage.kt");
|
runTest("idea/testData/intentions/introduceImportAlias/conflictPackage.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user