Fix import member quickfix to check receiver

#KT-17037 fixed
This commit is contained in:
Simon Ogorodnik
2017-04-11 16:10:31 +03:00
parent a47bc1492d
commit c6cdbe7eed
4 changed files with 39 additions and 2 deletions
@@ -53,6 +53,7 @@ import org.jetbrains.kotlin.idea.project.TargetPlatformDetector
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.util.CallTypeAndReceiver
import org.jetbrains.kotlin.idea.util.getResolutionScope
import org.jetbrains.kotlin.idea.util.receiverTypes
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.js.resolve.JsPlatform
import org.jetbrains.kotlin.name.FqName
@@ -70,6 +71,7 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.resolve.scopes.ExplicitImportsScope
import org.jetbrains.kotlin.resolve.scopes.utils.addImportingScope
import org.jetbrains.kotlin.resolve.scopes.utils.collectFunctions
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
import org.jetbrains.kotlin.util.OperatorNameConventions
import java.util.*
@@ -283,6 +285,7 @@ internal class ImportFix(expression: KtSimpleNameExpression) : OrdinaryImportFix
private fun collectMemberCandidates(
name: String,
callTypeAndReceiver: CallTypeAndReceiver<*, *>,
bindingContext: BindingContext,
indicesHelper: KotlinIndicesHelper
): List<DeclarationDescriptor> {
@@ -295,9 +298,18 @@ internal class ImportFix(expression: KtSimpleNameExpression) : OrdinaryImportFix
indicesHelper.getKotlinEnumsByName(name).filterTo(result, filterByCallType)
val resolutionFacade = element.getResolutionFacade()
val actualReceiverTypes =
callTypeAndReceiver.receiverTypes(bindingContext, element, resolutionFacade.moduleDescriptor, resolutionFacade, false).orEmpty()
val processor = { descriptor: CallableDescriptor ->
if (descriptor.canBeReferencedViaImport() && filterByCallType(descriptor)) {
result.add(descriptor)
val extensionReceiverType = descriptor.extensionReceiverParameter?.type
if ((actualReceiverTypes.isEmpty() && extensionReceiverType == null) ||
(extensionReceiverType != null && actualReceiverTypes.any { it.isSubtypeOf(extensionReceiverType) })) {
result.add(descriptor)
}
}
}
@@ -324,7 +336,7 @@ internal class ImportFix(expression: KtSimpleNameExpression) : OrdinaryImportFix
bindingContext: BindingContext,
indicesHelper: KotlinIndicesHelper
): List<DeclarationDescriptor> {
return super.fillCandidates(name, callTypeAndReceiver, bindingContext, indicesHelper) + collectMemberCandidates(name, callTypeAndReceiver, indicesHelper)
return super.fillCandidates(name, callTypeAndReceiver, bindingContext, indicesHelper) + collectMemberCandidates(name, callTypeAndReceiver, bindingContext, indicesHelper)
}
companion object MyFactory : Factory() {
@@ -0,0 +1,7 @@
package some
object AAA {
operator fun plus(i: Int): Int {
return 42
}
}
@@ -0,0 +1,12 @@
// "class org.jetbrains.kotlin.idea.quickfix.ImportFix" "false"
// ERROR: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: <br>public operator fun String?.plus(other: Any?): String defined in kotlin
// ACTION: Create extension function 'JustClass.plus'
// ACTION: Create member function 'JustClass.plus'
// ACTION: Flip '+'
// ACTION: Replace overloaded operator with function call
class JustClass
fun some() {
val x = JustClass() +<caret> 1
}
@@ -576,6 +576,12 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
doTestWithExtraFile(fileName);
}
@TestMetadata("plusOperatorWithTypeMismatch.before.Main.kt")
public void testPlusOperatorWithTypeMismatch() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/plusOperatorWithTypeMismatch.before.Main.kt");
doTestWithExtraFile(fileName);
}
@TestMetadata("postfixOperator.before.Main.kt")
public void testPostfixOperator() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/postfixOperator.before.Main.kt");