#KT-10797 Fixed

This commit is contained in:
hedgehog
2016-04-04 13:21:11 +03:00
committed by Dmitry Jemerov
parent 62212e7da9
commit 02d4376b85
7 changed files with 55 additions and 0 deletions
@@ -18,14 +18,21 @@ package org.jetbrains.kotlin.idea.intentions.conventionNameCalls
import com.intellij.codeInsight.intention.HighPriorityAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde.getAnyDeclaration
import org.jetbrains.kotlin.idea.intentions.*
import org.jetbrains.kotlin.idea.refactoring.canRefactor
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.ArgumentMatch
import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.util.OperatorChecks
import org.jetbrains.kotlin.util.OperatorNameConventions
class ReplaceContainsIntention : SelfTargetingRangeIntention<KtDotQualifiedExpression>(KtDotQualifiedExpression::class.java, "Replace 'contains' call with 'in' operator"), HighPriorityAction {
@@ -43,6 +50,9 @@ class ReplaceContainsIntention : SelfTargetingRangeIntention<KtDotQualifiedExpre
if (!element.isReceiverExpressionWithValue()) return null
val functionDescriptor = getFunctionDescriptor(element) ?: return null
if (!functionDescriptor.isOperator || !OperatorChecks.checkOperator(functionDescriptor).isSuccess) return null
return element.callExpression!!.calleeExpression!!.textRange
}
@@ -70,4 +80,10 @@ class ReplaceContainsIntention : SelfTargetingRangeIntention<KtDotQualifiedExpre
}
}
}
private fun getFunctionDescriptor(element: KtDotQualifiedExpression) : FunctionDescriptor? {
val bindingContext = element.analyze(BodyResolveMode.PARTIAL)
val resolvedCall = element.getResolvedCall(bindingContext) ?: return null
return resolvedCall.resultingDescriptor as? FunctionDescriptor
}
}
@@ -0,0 +1,5 @@
class Container {
public boolean contains(int x) {
return true;
}
}
@@ -0,0 +1,5 @@
class Container {
public boolean contains(int x) {
return true;
}
}
@@ -0,0 +1,4 @@
fun foo() {
val c = Container()
c.cont<caret>ains(1)
}
@@ -0,0 +1,4 @@
fun foo() {
val c = Container()
1 in c
}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
fun test() {
class Test{
fun contains(a: Int) : Boolean = true
}
val test = Test()
test.c<caret>ontains(0)
}
@@ -2807,6 +2807,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/conventionNameCalls/replaceContains"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("containsFromJava.kt")
public void testContainsFromJava() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceContains/containsFromJava.kt");
doTest(fileName);
}
@TestMetadata("containsInExpression.kt")
public void testContainsInExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceContains/containsInExpression.kt");
@@ -2908,6 +2914,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceContains/unacceptableVararg2.kt");
doTest(fileName);
}
@TestMetadata("withoutOperatorModifier.kt")
public void testWithoutOperatorModifier() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceContains/withoutOperatorModifier.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/conventionNameCalls/replaceGetOrSet")