Minor. Divide method moveInsideParenthesesAndReplaceWith

This commit is contained in:
Stanislav Erokhin
2015-03-25 19:57:17 +03:00
parent 58b033d9fb
commit 3fd8c5e980
@@ -16,32 +16,42 @@
package org.jetbrains.kotlin.idea.util.psiModificationUtil package org.jetbrains.kotlin.idea.util.psiModificationUtil
import org.jetbrains.kotlin.psi.JetFunctionLiteralArgument import com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.JetCallExpression import org.jetbrains.kotlin.psi.JetCallExpression
import org.jetbrains.kotlin.psi.JetExpression import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.psi.JetFunctionLiteralArgument
import org.jetbrains.kotlin.resolve.calls.callUtil.getValueArgumentsInParentheses
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
import org.jetbrains.kotlin.psi.JetPsiFactory import org.jetbrains.kotlin.psi.JetPsiFactory
import com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.callUtil.getValueArgumentsInParentheses
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
fun JetFunctionLiteralArgument.moveInsideParentheses(bindingContext: BindingContext): JetCallExpression { fun JetFunctionLiteralArgument.moveInsideParentheses(bindingContext: BindingContext): JetCallExpression {
return moveInsideParenthesesAndReplaceWith(this.getArgumentExpression(), bindingContext) return moveInsideParenthesesAndReplaceWith(this.getArgumentExpression(), bindingContext)
} }
fun JetFunctionLiteralArgument.getFunctionLiteralArgumentName(bindingContext: BindingContext): String? {
val callExpression = getParent() as JetCallExpression
val resolvedCall = callExpression.getResolvedCall(bindingContext)
return (resolvedCall?.getArgumentMapping(this) as? ArgumentMatch)?.valueParameter?.getName()?.toString()
}
fun JetFunctionLiteralArgument.moveInsideParenthesesAndReplaceWith( fun JetFunctionLiteralArgument.moveInsideParenthesesAndReplaceWith(
replacement: JetExpression, replacement: JetExpression,
bindingContext: BindingContext bindingContext: BindingContext
): JetCallExpression = moveInsideParenthesesAndReplaceWith(replacement, getFunctionLiteralArgumentName(bindingContext))
fun JetFunctionLiteralArgument.moveInsideParenthesesAndReplaceWith(
replacement: JetExpression,
functionLiteralArgumentName: String?
): JetCallExpression { ): JetCallExpression {
val oldCallExpression = getParent() as JetCallExpression val oldCallExpression = getParent() as JetCallExpression
val newCallExpression = oldCallExpression.copy() as JetCallExpression val newCallExpression = oldCallExpression.copy() as JetCallExpression
val psiFactory = JetPsiFactory(getProject()) val psiFactory = JetPsiFactory(getProject())
val argument = if (newCallExpression.getValueArgumentsInParentheses().any { it.getArgumentName() != null }) { val argument = if (newCallExpression.getValueArgumentsInParentheses().any { it.getArgumentName() != null }) {
val resolvedCall = oldCallExpression.getResolvedCall(bindingContext) psiFactory.createArgumentWithName(functionLiteralArgumentName, replacement)
val name = (resolvedCall?.getArgumentMapping(this) as? ArgumentMatch)?.valueParameter?.getName()?.toString()
psiFactory.createArgumentWithName(name, replacement)
} }
else { else {
psiFactory.createArgument(replacement) psiFactory.createArgument(replacement)