Extension operator function should be automatically imported for java function with the same name (KT-9406)

There's no need to import extension for Java operators anymore. Import for Kotlin functions.

 #KT-9406 Fixed
This commit is contained in:
Nikolay Krasko
2015-10-26 01:58:30 +03:00
parent 4ce87665c3
commit f518f5fb9f
5 changed files with 123 additions and 4 deletions
@@ -239,13 +239,15 @@ internal class MissingInvokeAutoImportFix(expression: KtExpression) : AutoImport
}
}
internal class MissingArrayAccessorAutoImportFix(element: KtArrayAccessExpression, override val importNames: Collection<Name>) :
internal open class MissingArrayAccessorAutoImportFix(element: KtArrayAccessExpression, override val importNames: Collection<Name>, private val showHint: Boolean) :
AutoImportFixBase<KtArrayAccessExpression>(element) {
override fun getCallTypeAndReceiver() =
CallTypeAndReceiver.OPERATOR(element.arrayExpression!!)
override fun getSupportedErrors() = ERRORS
override fun showHint(editor: Editor) = showHint && super.showHint(editor)
companion object : KotlinSingleIntentionActionFactory() {
private fun importName(diagnostic: Diagnostic): Name {
return when (diagnostic.factory) {
@@ -256,11 +258,12 @@ internal class MissingArrayAccessorAutoImportFix(element: KtArrayAccessExpressio
}
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtArrayAccessExpression>? {
assert(diagnostic.factory == Errors.NO_GET_METHOD || diagnostic.factory == Errors.NO_SET_METHOD)
val factory = diagnostic.factory
assert(factory == Errors.NO_GET_METHOD || factory == Errors.NO_SET_METHOD)
val element = diagnostic.psiElement
if (element is KtArrayAccessExpression && element.arrayExpression != null) {
return MissingArrayAccessorAutoImportFix(element, importName(diagnostic).singletonList())
return MissingArrayAccessorAutoImportFix(element, importName(diagnostic).singletonList(), true)
}
return null
@@ -345,4 +348,23 @@ internal class MissingComponentsAutoImportFix(element: KtExpression, override va
private val ERRORS by lazy(LazyThreadSafetyMode.PUBLICATION) { QuickFixes.getInstance().getDiagnostics(this) }
}
}
object AutoImportForMissingOperatorFactory : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val element = diagnostic.psiElement as? KtExpression ?: return null
val operatorDescriptor = Errors.OPERATOR_MODIFIER_REQUIRED.cast(diagnostic).a
val name = operatorDescriptor.name
when (name) {
OperatorNameConventions.GET, OperatorNameConventions.SET -> {
if (element is KtArrayAccessExpression) {
return object: MissingArrayAccessorAutoImportFix(element, name.singletonList(), false) {
override fun getSupportedErrors() = Errors.OPERATOR_MODIFIER_REQUIRED.singletonList()
}
}
}
}
return null
}
}
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.diagnostics.Errors.*
import org.jetbrains.kotlin.idea.core.overrideImplement.ImplementMembersHandler
import org.jetbrains.kotlin.idea.inspections.AddModifierFixFactory
import org.jetbrains.kotlin.idea.inspections.AddReflectionQuickFix
import org.jetbrains.kotlin.idea.intentions.IntroduceBackingPropertyFix
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createCallable.*
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createClass.CreateClassFromCallWithConstructorCalleeActionFactory
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createClass.CreateClassFromConstructorCallActionFactory
@@ -328,6 +327,8 @@ public class QuickFixRegistrar : QuickFixContributor {
NON_CONST_VAL_USED_IN_CONSTANT_EXPRESSION.registerFactory(ConstFixFactory)
OPERATOR_MODIFIER_REQUIRED.registerFactory(AddModifierFixFactory(KtTokens.OPERATOR_KEYWORD))
OPERATOR_MODIFIER_REQUIRED.registerFactory(AutoImportForMissingOperatorFactory)
INFIX_MODIFIER_REQUIRED.registerFactory(AddModifierFixFactory(KtTokens.INFIX_KEYWORD))
UNDERSCORE_IS_RESERVED.registerFactory(RenameUnderscoreFix)
@@ -0,0 +1,42 @@
// FILE: first.before.kt
// "Import" "true"
package testing
import some.Some
fun foo(): Some = Some()
fun testing() {
foo()<caret>["str"]
}
// FILE: second.kt
package some
public class Some {
fun get(s: String) {}
}
operator fun Some.get(s: String) {}
// FILE: first.after.kt
// "Import" "true"
package testing
import some.Some
import some.get
fun foo(): Some = Some()
fun testing() {
foo()<caret>["str"]
}
@@ -0,0 +1,42 @@
// FILE: first.before.kt
// "Import" "true"
package testing
import some.Some
fun foo(): Some = Some()
fun testing() {
foo()<caret>["str"] = 1
}
// FILE: second.kt
package some
public class Some {
fun set(s: String, i: Int) {}
}
operator fun Some.set(s: String, i: Int) {}
// FILE: first.after.kt
// "Import" "true"
package testing
import some.Some
import some.set
fun foo(): Some = Some()
fun testing() {
foo()<caret>["str"] = 1
}
@@ -173,6 +173,18 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
doTestWithExtraFile(fileName);
}
@TestMetadata("indexCallExtensionImportGetOnNoOperatorWarning.test")
public void testIndexCallExtensionImportGetOnNoOperatorWarning() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/indexCallExtensionImportGetOnNoOperatorWarning.test");
doTestWithExtraFile(fileName);
}
@TestMetadata("indexCallExtensionImportSetOnNoOperatorWarning.test")
public void testIndexCallExtensionImportSetOnNoOperatorWarning() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/indexCallExtensionImportSetOnNoOperatorWarning.test");
doTestWithExtraFile(fileName);
}
@TestMetadata("indexCallExtensionSet.test")
public void testIndexCallExtensionSet() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/indexCallExtensionSet.test");