Simplify call chain fix: extract 'apply'
This commit is contained in:
+66
-64
@@ -34,71 +34,73 @@ class SimplifyCallChainFix(private val newCallText: String) : LocalQuickFix {
|
|||||||
|
|
||||||
override fun getFamilyName() = name
|
override fun getFamilyName() = name
|
||||||
|
|
||||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
private fun apply(secondQualifiedExpression: KtQualifiedExpression) {
|
||||||
(descriptor.psiElement as? KtQualifiedExpression)?.let { secondQualifiedExpression ->
|
val factory = KtPsiFactory(secondQualifiedExpression)
|
||||||
val factory = KtPsiFactory(secondQualifiedExpression)
|
val firstExpression = secondQualifiedExpression.receiverExpression
|
||||||
val firstExpression = secondQualifiedExpression.receiverExpression
|
|
||||||
|
|
||||||
val operationSign = when (firstExpression) {
|
val operationSign = when (firstExpression) {
|
||||||
is KtSafeQualifiedExpression -> "?."
|
is KtSafeQualifiedExpression -> "?."
|
||||||
is KtQualifiedExpression -> "."
|
is KtQualifiedExpression -> "."
|
||||||
else -> ""
|
else -> ""
|
||||||
}
|
|
||||||
|
|
||||||
val receiverExpression = (firstExpression as? KtQualifiedExpression)?.receiverExpression
|
|
||||||
|
|
||||||
val firstCallExpression = AbstractCallChainChecker.getCallExpression(firstExpression) ?: return
|
|
||||||
val secondCallExpression = secondQualifiedExpression.selectorExpression as? KtCallExpression ?: return
|
|
||||||
|
|
||||||
val lastArgumentName = if (newCallText.startsWith("joinTo")) Name.identifier("transform") else null
|
|
||||||
if (lastArgumentName != null) {
|
|
||||||
val lastArgument = firstCallExpression.valueArgumentList?.arguments?.singleOrNull()
|
|
||||||
val argumentExpression = lastArgument?.getArgumentExpression()
|
|
||||||
if (argumentExpression != null) {
|
|
||||||
lastArgument.replace(factory.createArgument(argumentExpression, lastArgumentName))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val firstCallArgumentList = firstCallExpression.valueArgumentList
|
|
||||||
val secondCallArgumentList = secondCallExpression.valueArgumentList
|
|
||||||
|
|
||||||
fun KtValueArgumentList.getTextInsideParentheses(): String {
|
|
||||||
val range = PsiChildRange(leftParenthesis?.nextSibling ?: firstChild, rightParenthesis?.prevSibling ?: lastChild)
|
|
||||||
return range.joinToString(separator = "") { it.text }
|
|
||||||
}
|
|
||||||
|
|
||||||
val lambdaExpression = firstCallExpression.lambdaArguments.singleOrNull()?.getLambdaExpression()
|
|
||||||
val argumentsText = listOfNotNull(
|
|
||||||
secondCallArgumentList.takeIf { it?.arguments?.isNotEmpty() == true },
|
|
||||||
firstCallArgumentList.takeIf { it?.arguments?.isNotEmpty() == true }
|
|
||||||
).let {
|
|
||||||
if (it.isEmpty()) ""
|
|
||||||
else it.joinToString(
|
|
||||||
separator = ", ",
|
|
||||||
prefix = "(",
|
|
||||||
postfix = ")"
|
|
||||||
) { callArgumentList ->
|
|
||||||
callArgumentList.getTextInsideParentheses()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val newQualifiedExpression = if (lambdaExpression != null) factory.createExpressionByPattern(
|
|
||||||
"$0$1$2 $3 $4",
|
|
||||||
receiverExpression ?: "",
|
|
||||||
operationSign,
|
|
||||||
newCallText,
|
|
||||||
argumentsText,
|
|
||||||
lambdaExpression.text
|
|
||||||
)
|
|
||||||
else factory.createExpressionByPattern(
|
|
||||||
"$0$1$2 $3",
|
|
||||||
receiverExpression ?: "",
|
|
||||||
operationSign,
|
|
||||||
newCallText,
|
|
||||||
argumentsText
|
|
||||||
)
|
|
||||||
|
|
||||||
val result = secondQualifiedExpression.replaced(newQualifiedExpression)
|
|
||||||
ShortenReferences.DEFAULT.process(result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val receiverExpression = (firstExpression as? KtQualifiedExpression)?.receiverExpression
|
||||||
|
|
||||||
|
val firstCallExpression = AbstractCallChainChecker.getCallExpression(firstExpression) ?: return
|
||||||
|
val secondCallExpression = secondQualifiedExpression.selectorExpression as? KtCallExpression ?: return
|
||||||
|
|
||||||
|
val lastArgumentName = if (newCallText.startsWith("joinTo")) Name.identifier("transform") else null
|
||||||
|
if (lastArgumentName != null) {
|
||||||
|
val lastArgument = firstCallExpression.valueArgumentList?.arguments?.singleOrNull()
|
||||||
|
val argumentExpression = lastArgument?.getArgumentExpression()
|
||||||
|
if (argumentExpression != null) {
|
||||||
|
lastArgument.replace(factory.createArgument(argumentExpression, lastArgumentName))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val firstCallArgumentList = firstCallExpression.valueArgumentList
|
||||||
|
val secondCallArgumentList = secondCallExpression.valueArgumentList
|
||||||
|
|
||||||
|
fun KtValueArgumentList.getTextInsideParentheses(): String {
|
||||||
|
val range = PsiChildRange(leftParenthesis?.nextSibling ?: firstChild, rightParenthesis?.prevSibling ?: lastChild)
|
||||||
|
return range.joinToString(separator = "") { it.text }
|
||||||
|
}
|
||||||
|
|
||||||
|
val lambdaExpression = firstCallExpression.lambdaArguments.singleOrNull()?.getLambdaExpression()
|
||||||
|
val argumentsText = listOfNotNull(
|
||||||
|
secondCallArgumentList.takeIf { it?.arguments?.isNotEmpty() == true },
|
||||||
|
firstCallArgumentList.takeIf { it?.arguments?.isNotEmpty() == true }
|
||||||
|
).let {
|
||||||
|
if (it.isEmpty()) ""
|
||||||
|
else it.joinToString(
|
||||||
|
separator = ", ",
|
||||||
|
prefix = "(",
|
||||||
|
postfix = ")"
|
||||||
|
) { callArgumentList ->
|
||||||
|
callArgumentList.getTextInsideParentheses()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val newQualifiedExpression = if (lambdaExpression != null) factory.createExpressionByPattern(
|
||||||
|
"$0$1$2 $3 $4",
|
||||||
|
receiverExpression ?: "",
|
||||||
|
operationSign,
|
||||||
|
newCallText,
|
||||||
|
argumentsText,
|
||||||
|
lambdaExpression.text
|
||||||
|
)
|
||||||
|
else factory.createExpressionByPattern(
|
||||||
|
"$0$1$2 $3",
|
||||||
|
receiverExpression ?: "",
|
||||||
|
operationSign,
|
||||||
|
newCallText,
|
||||||
|
argumentsText
|
||||||
|
)
|
||||||
|
|
||||||
|
val result = secondQualifiedExpression.replaced(newQualifiedExpression)
|
||||||
|
ShortenReferences.DEFAULT.process(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||||
|
(descriptor.psiElement as? KtQualifiedExpression)?.let(this::apply)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user