Quick-fix to add type to LHS of incorrect callable references

This commit is contained in:
Alexander Udalov
2015-10-02 22:39:57 +03:00
parent 661f4efc68
commit 012a2971ff
6 changed files with 98 additions and 0 deletions
@@ -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 <R, D> accept(visitor: JetVisitor<R, D>, data: D): R {
@@ -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<JetCallableReferenceExpression>(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)
}
}
}
@@ -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)
}
}
@@ -0,0 +1,8 @@
// "Add type to left-hand side" "true"
package foo.bar
class A {
fun foo() {}
fun bar() = ::fo<caret>o
}
@@ -0,0 +1,8 @@
// "Add type to left-hand side" "true"
package foo.bar
class A {
fun foo() {}
fun bar() = A::foo
}
@@ -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)