From 7b56733bdabc3b182a6131f63156eab35a98e598 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Thu, 19 Sep 2019 23:55:20 +0900 Subject: [PATCH] Reforma ReplaceCallFix --- .../kotlin/idea/quickfix/ReplaceCallFix.kt | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceCallFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceCallFix.kt index 3b8d41b2536..34212e9a023 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceCallFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceCallFix.kt @@ -30,9 +30,9 @@ import org.jetbrains.kotlin.resolve.calls.resolvedCallUtil.getImplicitReceiverVa import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe abstract class ReplaceCallFix( - expression: KtQualifiedExpression, - private val operation: String, - private val notNullNeeded: Boolean = false + expression: KtQualifiedExpression, + private val operation: String, + private val notNullNeeded: Boolean = false ) : KotlinQuickFixAction(expression) { override fun getFamilyName() = text @@ -45,8 +45,10 @@ abstract class ReplaceCallFix( override fun invoke(project: Project, editor: Editor?, file: KtFile) { val element = element ?: return val elvis = elvisOrEmpty(notNullNeeded) - val newExpression = KtPsiFactory(element).createExpressionByPattern("$0$operation$1$elvis", - element.receiverExpression, element.selectorExpression!!) + val newExpression = KtPsiFactory(element).createExpressionByPattern( + "$0$operation$1$elvis", + element.receiverExpression, element.selectorExpression!! + ) val replacement = element.replace(newExpression) if (notNullNeeded) { replacement.moveCaretToEnd(editor, project) @@ -55,8 +57,8 @@ abstract class ReplaceCallFix( } class ReplaceImplicitReceiverCallFix( - expression: KtExpression, - private val notNullNeeded: Boolean + expression: KtExpression, + private val notNullNeeded: Boolean ) : KotlinQuickFixAction(expression) { override fun getFamilyName() = text @@ -74,8 +76,8 @@ class ReplaceImplicitReceiverCallFix( } class ReplaceWithSafeCallFix( - expression: KtDotQualifiedExpression, - notNullNeeded: Boolean + expression: KtDotQualifiedExpression, + notNullNeeded: Boolean ) : ReplaceCallFix(expression, "?.", notNullNeeded) { override fun getText() = "Replace with safe (?.) call" @@ -86,8 +88,7 @@ class ReplaceWithSafeCallFix( val qualifiedExpression = psiElement.parent as? KtDotQualifiedExpression if (qualifiedExpression != null) { return ReplaceWithSafeCallFix(qualifiedExpression, qualifiedExpression.shouldHaveNotNullType()) - } - else { + } else { if (psiElement !is KtNameReferenceExpression) return null if (psiElement.getResolvedCall(psiElement.analyze())?.getImplicitReceiverValue() != null) { val expressionToReplace: KtExpression = psiElement.parent as? KtCallExpression ?: psiElement @@ -100,8 +101,8 @@ class ReplaceWithSafeCallFix( } class ReplaceWithSafeCallForScopeFunctionFix( - expression: KtDotQualifiedExpression, - notNullNeeded: Boolean + expression: KtDotQualifiedExpression, + notNullNeeded: Boolean ) : ReplaceCallFix(expression, "?.", notNullNeeded) { override fun getText() = "Replace scope function with safe (?.) call" @@ -120,7 +121,7 @@ class ReplaceWithSafeCallForScopeFunctionFix( val internalReceiver = (element.parent as? KtDotQualifiedExpression)?.receiverExpression val internalReceiverDescriptor = internalReceiver.getResolvedCall(context)?.candidateDescriptor val internalResolvedCall = (element.getParentOfType(strict = false))?.getResolvedCall(context) - ?: return null + ?: return null when (scopeFunctionKind) { ScopeFunctionKind.WITH_PARAMETER -> { @@ -130,14 +131,16 @@ class ReplaceWithSafeCallForScopeFunctionFix( } ScopeFunctionKind.WITH_RECEIVER -> { if (internalReceiverDescriptor != scopeFunctionLiteralDescriptor.extensionReceiverParameter && - internalResolvedCall.getImplicitReceiverValue() == null) { + internalResolvedCall.getImplicitReceiverValue() == null + ) { return null } } } return ReplaceWithSafeCallForScopeFunctionFix( - scopeDotQualifiedExpression, scopeDotQualifiedExpression.shouldHaveNotNullType()) + scopeDotQualifiedExpression, scopeDotQualifiedExpression.shouldHaveNotNullType() + ) } private fun KtCallExpression.scopeFunctionKind(context: BindingContext): ScopeFunctionKind? {