Merge PR#471: Bug Fix: KT-4613

This commit is contained in:
Pavel V. Talanov
2014-05-22 15:24:22 +04:00
7 changed files with 88 additions and 12 deletions
@@ -276,6 +276,8 @@ replace.with.dot.qualified.method.call.intention=Replace with simple method call
replace.with.dot.qualified.method.call.intention.family=Replace with Simple Method Call
replace.with.infix.function.call.intention=Replace with infix function call
replace.with.infix.function.call.intention.family=Replace with Infix Function Call
replace.with.infix.function.call.intention.error.resolution.failed=The element cannot be resolved
replace.with.infix.function.call.intention.error.package.call=Cannot be applied with a package as the receiver
replace.explicit.function.literal.param.with.it=Replace explicit parameter ''{0}'' with ''it''
replace.explicit.function.literal.param.with.it.family=Replace Explicit Parameter with 'it'
move.lambda.inside.parentheses=Move lambda function into parentheses
@@ -19,14 +19,18 @@ package org.jetbrains.jet.plugin.intentions
import org.jetbrains.jet.lang.psi.JetCallExpression
import com.intellij.openapi.editor.Editor
import org.jetbrains.jet.lang.psi.JetDotQualifiedExpression
import org.jetbrains.jet.lang.psi.JetValueArgument
import org.jetbrains.jet.lang.psi.JetPsiFactory
import org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.plugin.caches.resolve.getBindingContext
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.plugin.JetBundle
import com.intellij.openapi.ui.popup.JBPopupFactory
import org.jetbrains.jet.lang.psi.JetValueArgument
import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache
import org.jetbrains.jet.analyzer.computeTypeInfoInContext
import org.jetbrains.jet.lang.types.PackageType
import org.jetbrains.jet.lang.psi.JetPsiUnparsingUtils
import org.jetbrains.jet.lang.psi.JetPsiFactory
public class ReplaceWithInfixFunctionCallIntention : JetSelfTargetingIntention<JetCallExpression>("replace.with.infix.function.call.intention", javaClass()) {
public open class ReplaceWithInfixFunctionCallIntention : JetSelfTargetingIntention<JetCallExpression>("replace.with.infix.function.call.intention", javaClass()) {
override fun isApplicableTo(element: JetCallExpression): Boolean {
throw IllegalStateException("isApplicableTo(JetExpressionImpl, Editor) should be called instead")
}
@@ -54,11 +58,12 @@ public class ReplaceWithInfixFunctionCallIntention : JetSelfTargetingIntention<J
if (typeArguments?.getArguments()?.size() ?: 0 == 0 &&
numOfTotalValueArguments == 1 &&
callee != null) {
if (valueArguments?.getArguments()?.size() == 1 && valueArguments?.getArguments()?.first()?.isNamed() ?: false) {
val file: JetFile = element.getContainingJetFile()
val file = element.getContainingJetFile()
val bindingContext = file.getBindingContext()
val descriptor = bindingContext.get(BindingContext.RESOLVED_CALL, callee)
val valueArgumentsMap = descriptor?.getValueArguments()
val resolvedCallDescriptor = bindingContext[BindingContext.RESOLVED_CALL, callee]
val valueArgumentsMap = resolvedCallDescriptor?.getValueArguments()
val firstArgument = valueArguments?.getArguments()?.first()
return valueArgumentsMap?.keySet()?.any { it.getName().asString() == firstArgument?.getArgumentName()?.getText() && it.getIndex() == 0 } ?: false
@@ -73,13 +78,34 @@ public class ReplaceWithInfixFunctionCallIntention : JetSelfTargetingIntention<J
}
}
open fun intentionFailed(editor: Editor, messageID: String) {
JBPopupFactory.getInstance()!!.createMessage("Intention failed: ${JetBundle.message("replace.with.infix.function.call.intention.error.$messageID")}").showInBestPositionFor(editor)
}
override fun applyTo(element: JetCallExpression, editor: Editor) {
val parent = element.getParent() as JetDotQualifiedExpression
val receiver = parent.getReceiverExpression()
val leftHandText = parent.getReceiverExpression().getText()
val rightHandTextStringBuilder = StringBuilder()
val operatorText = element.getCalleeExpression()!!.getText()
val valueArguments = element.getValueArgumentList()?.getArguments() ?: listOf<JetValueArgument>()
val functionLiteralArguments = element.getFunctionLiteralArguments()
val bindingContext = AnalyzerFacadeWithCache.getContextForElement(parent)
val scope = bindingContext[BindingContext.RESOLUTION_SCOPE, parent]
when {
scope == null -> {
intentionFailed(editor, "resolution.failed")
return
}
else ->
when (receiver.computeTypeInfoInContext(scope).getType()) {
is PackageType -> {
intentionFailed(editor, "package.call")
return
}
}
}
rightHandTextStringBuilder.append(
if (valueArguments.size() > 0)
@@ -92,5 +118,4 @@ public class ReplaceWithInfixFunctionCallIntention : JetSelfTargetingIntention<J
parent.replace(replacement)
}
}
}