FIR IDE: Fix import of properties; also fix bug in alreadyHasImport

This commit is contained in:
Roman Golyshev
2021-03-16 15:25:51 +03:00
parent 054d6ccdfb
commit 184838ae35
5 changed files with 41 additions and 11 deletions
@@ -0,0 +1,8 @@
// FIR_COMPARISON
package first
import second.extensionProp
fun foo() {
"".extensi<caret>
}
@@ -0,0 +1,4 @@
package second
val String.extensionProp: Int
get() = 1
@@ -0,0 +1,8 @@
// FIR_COMPARISON
package first
import second.extensionProp
fun foo() {
"".extensionProp<caret>
}
@@ -26,6 +26,10 @@ open class CompletionMultiFileHandlerTest : KotlinFixtureCompletionBaseTestCase(
doTest() doTest()
} }
fun testAlreadyImportedExtensionPropertyNoImport() {
doTest()
}
fun testExtensionPropertyInSamePackageNoImport() { fun testExtensionPropertyInSamePackageNoImport() {
doTest() doTest()
} }
@@ -132,11 +132,15 @@ private class VariableLookupElementFactory {
if (setterName != null) "$getterName()/$setterName()" else "$getterName()" if (setterName != null) "$getterName()/$setterName()" else "$getterName()"
private fun detectImportStrategy(symbol: KtVariableLikeSymbol): CallableImportStrategy { private fun detectImportStrategy(symbol: KtVariableLikeSymbol): CallableImportStrategy {
if (symbol !is KtKotlinPropertySymbol) return CallableImportStrategy.DoNothing if (symbol !is KtKotlinPropertySymbol || symbol.dispatchType != null) return CallableImportStrategy.DoNothing
if (symbol.dispatchType != null || symbol.receiverType != null) return CallableImportStrategy.DoNothing
return symbol.callableIdIfNonLocal?.let(CallableImportStrategy::InsertFqNameAndShorten) val propertyId = symbol.callableIdIfNonLocal ?: return CallableImportStrategy.DoNothing
?: CallableImportStrategy.DoNothing
return if (symbol.isExtension) {
CallableImportStrategy.AddImport(propertyId)
} else {
CallableImportStrategy.InsertFqNameAndShorten(propertyId)
}
} }
} }
@@ -163,12 +167,14 @@ private class FunctionLookupElementFactory {
} }
private fun detectImportStrategy(symbol: KtFunctionSymbol): CallableImportStrategy { private fun detectImportStrategy(symbol: KtFunctionSymbol): CallableImportStrategy {
val functionFqName = symbol.callableIdIfNonLocal if (symbol.dispatchType != null) return CallableImportStrategy.DoNothing
return when {
functionFqName == null -> CallableImportStrategy.DoNothing val functionFqName = symbol.callableIdIfNonLocal ?: return CallableImportStrategy.DoNothing
symbol.dispatchType != null -> CallableImportStrategy.DoNothing
!symbol.isExtension -> CallableImportStrategy.InsertFqNameAndShorten(functionFqName) return if (symbol.isExtension) {
else -> CallableImportStrategy.AddImport(functionFqName) CallableImportStrategy.AddImport(functionFqName)
} else {
CallableImportStrategy.InsertFqNameAndShorten(functionFqName)
} }
} }
@@ -356,7 +362,7 @@ private fun addCallableImportIfRequired(targetFile: KtFile, nameToImport: FqName
} }
private fun alreadyHasImport(file: KtFile, nameToImport: FqName): Boolean { private fun alreadyHasImport(file: KtFile, nameToImport: FqName): Boolean {
if (file.importDirectives.any { it.importPath?.fqName == nameToImport }) return false if (file.importDirectives.any { it.importPath?.fqName == nameToImport }) return true
withAllowedResolve { withAllowedResolve {
analyze(file) { analyze(file) {