Change necessary labels in "Call on collection type may be reduced"

#KT-24492 Fixed
This commit is contained in:
kenji tomita
2018-10-01 18:22:58 +02:00
committed by Mikhail Glukhikh
parent 5e7d974767
commit 6532916dd2
8 changed files with 87 additions and 3 deletions
@@ -20,9 +20,8 @@ import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
class RenameUselessCallFix(val newName: String) : LocalQuickFix {
override fun getName() = "Change call to '$newName'"
@@ -35,6 +34,24 @@ class RenameUselessCallFix(val newName: String) : LocalQuickFix {
val selectorCallExpression = it.selectorExpression as? KtCallExpression
val calleeExpression = selectorCallExpression?.calleeExpression ?: return
calleeExpression.replaced(factory.createExpression(newName))
selectorCallExpression.renameGivenReturnLabels(factory, calleeExpression.text, newName)
}
}
private fun KtCallExpression.renameGivenReturnLabels(factory: KtPsiFactory, labelName: String, newName: String) {
val lambdaExpression = lambdaArguments.firstOrNull()?.getLambdaExpression() ?: return
val bodyExpression = lambdaExpression.bodyExpression ?: return
bodyExpression.forEachDescendantOfType<KtReturnExpression> {
if (it.getLabelName() != labelName) return@forEachDescendantOfType
it.replaced(
factory.createExpressionByPattern(
"return@$0 $1",
newName,
it.returnedExpression ?: ""
)
)
}
}
}