"Replace 'invoke' with direct call" intention: fix false positive when function is not operator
#KT-37977 Fixed
This commit is contained in:
committed by
Vladimir Dolzhenko
parent
e0da30fb13
commit
2f29b38b19
+7
-1
@@ -8,12 +8,15 @@ package org.jetbrains.kotlin.idea.intentions.conventionNameCalls
|
|||||||
import com.intellij.codeInsight.intention.HighPriorityAction
|
import com.intellij.codeInsight.intention.HighPriorityAction
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||||
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
|
import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention
|
||||||
import org.jetbrains.kotlin.idea.intentions.callExpression
|
import org.jetbrains.kotlin.idea.intentions.callExpression
|
||||||
import org.jetbrains.kotlin.idea.intentions.calleeName
|
import org.jetbrains.kotlin.idea.intentions.calleeName
|
||||||
|
import org.jetbrains.kotlin.idea.intentions.toResolvedCall
|
||||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||||
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
||||||
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
import org.jetbrains.kotlin.util.OperatorNameConventions
|
import org.jetbrains.kotlin.util.OperatorNameConventions
|
||||||
|
|
||||||
class ReplaceInvokeIntention : SelfTargetingRangeIntention<KtDotQualifiedExpression>(
|
class ReplaceInvokeIntention : SelfTargetingRangeIntention<KtDotQualifiedExpression>(
|
||||||
@@ -21,7 +24,10 @@ class ReplaceInvokeIntention : SelfTargetingRangeIntention<KtDotQualifiedExpress
|
|||||||
KotlinBundle.lazyMessage("replace.invoke.with.direct.call")
|
KotlinBundle.lazyMessage("replace.invoke.with.direct.call")
|
||||||
), HighPriorityAction {
|
), HighPriorityAction {
|
||||||
override fun applicabilityRange(element: KtDotQualifiedExpression): TextRange? {
|
override fun applicabilityRange(element: KtDotQualifiedExpression): TextRange? {
|
||||||
if (element.calleeName != OperatorNameConventions.INVOKE.asString() || element.callExpression?.typeArgumentList != null) return null
|
if (element.calleeName != OperatorNameConventions.INVOKE.asString() ||
|
||||||
|
element.callExpression?.typeArgumentList != null ||
|
||||||
|
(element.toResolvedCall(BodyResolveMode.PARTIAL)?.resultingDescriptor as? FunctionDescriptor)?.isOperator != true
|
||||||
|
) return null
|
||||||
return element.callExpression?.calleeExpression?.textRange
|
return element.callExpression?.calleeExpression?.textRange
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
public class JavaClass {
|
||||||
|
void invoke() {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
public class JavaClass {
|
||||||
|
void invoke() {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun main() {
|
||||||
|
JavaClass().<caret>invoke()
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun main() {
|
||||||
|
JavaClass()()
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
class C {
|
||||||
|
fun invoke() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
C().<caret>invoke()
|
||||||
|
}
|
||||||
@@ -3930,6 +3930,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
runTest("idea/testData/intentions/conventionNameCalls/replaceInvoke/invokeInExpression.kt");
|
runTest("idea/testData/intentions/conventionNameCalls/replaceInvoke/invokeInExpression.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("java.kt")
|
||||||
|
public void testJava() throws Exception {
|
||||||
|
runTest("idea/testData/intentions/conventionNameCalls/replaceInvoke/java.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("namedArgumentInvoke.kt")
|
@TestMetadata("namedArgumentInvoke.kt")
|
||||||
public void testNamedArgumentInvoke() throws Exception {
|
public void testNamedArgumentInvoke() throws Exception {
|
||||||
runTest("idea/testData/intentions/conventionNameCalls/replaceInvoke/namedArgumentInvoke.kt");
|
runTest("idea/testData/intentions/conventionNameCalls/replaceInvoke/namedArgumentInvoke.kt");
|
||||||
@@ -3940,6 +3945,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
runTest("idea/testData/intentions/conventionNameCalls/replaceInvoke/noArgumentInvoke.kt");
|
runTest("idea/testData/intentions/conventionNameCalls/replaceInvoke/noArgumentInvoke.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("notOperator.kt")
|
||||||
|
public void testNotOperator() throws Exception {
|
||||||
|
runTest("idea/testData/intentions/conventionNameCalls/replaceInvoke/notOperator.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("typeAndValueArgument.kt")
|
@TestMetadata("typeAndValueArgument.kt")
|
||||||
public void testTypeAndValueArgument() throws Exception {
|
public void testTypeAndValueArgument() throws Exception {
|
||||||
runTest("idea/testData/intentions/conventionNameCalls/replaceInvoke/typeAndValueArgument.kt");
|
runTest("idea/testData/intentions/conventionNameCalls/replaceInvoke/typeAndValueArgument.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user