Use correct callElement for variable invoke in replacer

Check only 'status.isSuccess' before inlining, no descriptor check
Add two tests and fixes two other
This commit is contained in:
Mikhail Glukhikh
2017-04-11 15:06:03 +03:00
parent 4ef0096d46
commit 6571a573f1
6 changed files with 61 additions and 4 deletions
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.idea.intentions.OperatorToFunctionIntention
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess
import org.jetbrains.kotlin.resolve.calls.model.VariableAsFunctionResolvedCall
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class CallableUsageReplacementStrategy(
@@ -31,10 +31,17 @@ class CallableUsageReplacementStrategy(
override fun createReplacer(usage: KtSimpleNameExpression): (() -> KtElement?)? {
val bindingContext = usage.analyze(BodyResolveMode.PARTIAL)
val resolvedCall = usage.getResolvedCall(bindingContext) ?: return null
if (!resolvedCall.isReallySuccess()) return null
if (!resolvedCall.status.isSuccess) return null
val callElement = resolvedCall.call.callElement
if (callElement !is KtExpression && callElement !is KtAnnotationEntry) return null
val callElement = when (resolvedCall) {
is VariableAsFunctionResolvedCall -> resolvedCall.variableCall.call.callElement
else -> resolvedCall.call.callElement
}
if (callElement !is KtExpression && callElement !is KtAnnotationEntry) {
return null
}
//TODO: precheck pattern correctness for annotation entry
@@ -0,0 +1,10 @@
class Predicate(val x: Int) {
operator fun invoke() = x
operator fun unaryMinus() = Predicate(-x)
}
fun test(p: Predicate) {
val x = -p
println(<caret>x())
}
@@ -0,0 +1,9 @@
class Predicate(val x: Int) {
operator fun invoke() = x
operator fun unaryMinus() = Predicate(-x)
}
fun test(p: Predicate) {
println((-p)())
}
@@ -0,0 +1,10 @@
class Predicate(val x: Int) {
operator fun set(i: Int, j: Int) {}
operator fun unaryMinus() = Predicate(-x)
}
fun test(p: Predicate) {
val x = -p
<caret>x[13] = 42
}
@@ -0,0 +1,9 @@
class Predicate(val x: Int) {
operator fun set(i: Int, j: Int) {}
operator fun unaryMinus() = Predicate(-x)
}
fun test(p: Predicate) {
(-p)[13] = 42
}
@@ -679,6 +679,12 @@ public class InlineTestGenerated extends AbstractInlineTest {
doTest(fileName);
}
@TestMetadata("CallCorrected.kt")
public void testCallCorrected() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/CallCorrected.kt");
doTest(fileName);
}
@TestMetadata("CallDontAdd.kt")
public void testCallDontAdd() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/CallDontAdd.kt");
@@ -751,6 +757,12 @@ public class InlineTestGenerated extends AbstractInlineTest {
doTest(fileName);
}
@TestMetadata("IndexedCorrected.kt")
public void testIndexedCorrected() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/IndexedCorrected.kt");
doTest(fileName);
}
@TestMetadata("IsDontAdd.kt")
public void testIsDontAdd() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/refactoring/inline/inlineVariableOrProperty/addParenthesis/IsDontAdd.kt");