diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetDoubleColonExpression.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetDoubleColonExpression.kt index a540a86b24b..859a195ac1b 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/JetDoubleColonExpression.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/JetDoubleColonExpression.kt @@ -24,6 +24,16 @@ import org.jetbrains.kotlin.lexer.JetTokens public abstract class JetDoubleColonExpression(node: ASTNode) : JetExpressionImpl(node) { public fun getTypeReference(): JetTypeReference? = findChildByType(JetNodeTypes.TYPE_REFERENCE) + public fun setTypeReference(typeReference: JetTypeReference) { + val oldTypeReference = getTypeReference() + if (oldTypeReference != null) { + oldTypeReference.replace(typeReference) + } + else { + addBefore(typeReference, getDoubleColonTokenReference()) + } + } + public fun getDoubleColonTokenReference(): PsiElement = findChildByType(JetTokens.COLONCOLON)!! override fun accept(visitor: JetVisitor, data: D): R { diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/AddTypeToLHSOfCallableReferenceFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddTypeToLHSOfCallableReferenceFix.kt new file mode 100644 index 00000000000..7c3a6d9c706 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddTypeToLHSOfCallableReferenceFix.kt @@ -0,0 +1,55 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.idea.quickfix + +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.diagnostics.Diagnostic +import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers +import org.jetbrains.kotlin.idea.util.ShortenReferences +import org.jetbrains.kotlin.psi.JetCallableReferenceExpression +import org.jetbrains.kotlin.psi.JetFile +import org.jetbrains.kotlin.psi.JetPsiFactory +import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode + +class AddTypeToLHSOfCallableReferenceFix( + expression: JetCallableReferenceExpression +) : JetIntentionAction(expression) { + override fun getFamilyName() = "Add type to left-hand side" + override fun getText() = familyName + + override fun invoke(project: Project, editor: Editor?, file: JetFile) { + val resolvedCall = element.callableReference.getResolvedCall(element.analyze(BodyResolveMode.PARTIAL)) ?: return + val receiver = with(resolvedCall) { + if (dispatchReceiver.exists()) dispatchReceiver + else if (extensionReceiver.exists()) extensionReceiver + else return + } + val type = JetPsiFactory(project).createType(IdeDescriptorRenderers.SOURCE_CODE.renderType(receiver.type)) + element.setTypeReference(type) + ShortenReferences.DEFAULT.process(element) + } + + companion object : JetSingleIntentionActionFactory() { + override fun createAction(diagnostic: Diagnostic): IntentionAction? { + return AddTypeToLHSOfCallableReferenceFix(diagnostic.psiElement.parent as JetCallableReferenceExpression) + } + } +} diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index 6121d67b488..54d886dc4e0 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -333,5 +333,7 @@ public class QuickFixRegistrar : QuickFixContributor { OPERATOR_MODIFIER_REQUIRED.registerFactory(OperatorModifierFixFactory) UNDERSCORE_IS_DEPRECATED.registerFactory(RenameUnderscoreFix) + + CALLABLE_REFERENCE_TO_MEMBER_OR_EXTENSION_WITH_EMPTY_LHS.registerFactory(AddTypeToLHSOfCallableReferenceFix) } } diff --git a/idea/testData/quickfix/migration/addTypeToLHSOfCallableReference/member.kt b/idea/testData/quickfix/migration/addTypeToLHSOfCallableReference/member.kt new file mode 100644 index 00000000000..439dc647908 --- /dev/null +++ b/idea/testData/quickfix/migration/addTypeToLHSOfCallableReference/member.kt @@ -0,0 +1,8 @@ +// "Add type to left-hand side" "true" +package foo.bar + +class A { + fun foo() {} + + fun bar() = ::foo +} diff --git a/idea/testData/quickfix/migration/addTypeToLHSOfCallableReference/member.kt.after b/idea/testData/quickfix/migration/addTypeToLHSOfCallableReference/member.kt.after new file mode 100644 index 00000000000..d67915b2876 --- /dev/null +++ b/idea/testData/quickfix/migration/addTypeToLHSOfCallableReference/member.kt.after @@ -0,0 +1,8 @@ +// "Add type to left-hand side" "true" +package foo.bar + +class A { + fun foo() {} + + fun bar() = A::foo +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index cd041efe193..a312ae67d02 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -4073,6 +4073,21 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); } + @TestMetadata("idea/testData/quickfix/migration/addTypeToLHSOfCallableReference") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class AddTypeToLHSOfCallableReference extends AbstractQuickFixTest { + public void testAllFilesPresentInAddTypeToLHSOfCallableReference() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/addTypeToLHSOfCallableReference"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true); + } + + @TestMetadata("member.kt") + public void testMember() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/migration/addTypeToLHSOfCallableReference/member.kt"); + doTest(fileName); + } + } + @TestMetadata("idea/testData/quickfix/migration/backingFieldSyntax") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)