Changes on code review

This commit is contained in:
Valentin Kipyatkov
2015-11-10 20:43:47 +03:00
parent 4f9866f62f
commit d829f585f7
9 changed files with 50 additions and 10 deletions
@@ -29,20 +29,19 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelector
class InvokeOnExtensionFunctionWithExplicitReceiverFix(qualifiedExpression: KtDotQualifiedExpression) : KotlinQuickFixAction<KtDotQualifiedExpression>(qualifiedExpression), CleanupFix {
override fun getFamilyName() = "Fix extension function value call"
override fun getFamilyName() = "Surround callee with parenthesis"
override fun getText() = familyName
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val callExpression = element.selectorExpression as KtCallExpression
val pattern = if (element is KtSafeQualifiedExpression)"($0?.$1)" else "($0.$1)"
val newCallee = KtPsiFactory(file).createExpressionByPattern(pattern, element.receiverExpression, callExpression.calleeExpression!!)
val newCallee = KtPsiFactory(file).createExpressionByPattern("($0.$1)", element.receiverExpression, callExpression.calleeExpression!!)
val newCallExpression = element.replaced(callExpression)
newCallExpression.calleeExpression!!.replace(newCallee)
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val callee = Errors.INVOKE_ON_EXTENSION_FUNCTION_WITH_EXPLICIT_DISPATCH_RECEIVER.cast(diagnostic).a as? KtNameReferenceExpression ?: return null
val callee = Errors.INVOKE_ON_EXTENSION_FUNCTION_WITH_EXPLICIT_DISPATCH_RECEIVER.cast(diagnostic).psiElement as? KtNameReferenceExpression ?: return null
val callExpression = callee.parent as? KtCallExpression ?: return null
val qualifiedExpression = callExpression.getQualifiedExpressionForSelector() as? KtDotQualifiedExpression ?: return null
return InvokeOnExtensionFunctionWithExplicitReceiverFix(qualifiedExpression)
@@ -0,0 +1,18 @@
// "Surround callee with parenthesis" "true"
class A {
val foo: B.(String, Int) -> Unit get() = null!!
}
class B {
fun getA() = A()
}
fun test(b: B) {
with(b) {
b.getA().<caret>foo("", 1)
}
}
public inline fun <T, R> with(receiver: T, f: T.() -> R): R = receiver.f()
@@ -0,0 +1,18 @@
// "Surround callee with parenthesis" "true"
class A {
val foo: B.(String, Int) -> Unit get() = null!!
}
class B {
fun getA() = A()
}
fun test(b: B) {
with(b) {
(b.getA().foo)("", 1)
}
}
public inline fun <T, R> with(receiver: T, f: T.() -> R): R = receiver.f()
@@ -1,4 +1,4 @@
// "Fix extension function value call" "true"
// "Surround callee with parenthesis" "true"
class A {
val foo: B.() -> Unit get() = null!!
@@ -1,4 +1,4 @@
// "Fix extension function value call" "true"
// "Surround callee with parenthesis" "true"
class A {
val foo: B.() -> Unit get() = null!!
@@ -4369,6 +4369,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("notSimple.kt")
public void testNotSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/notSimple.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/migration/invokeOnExtensionFunctionWithExplicitReceiverFix/simple.kt");