Replace "contains" with "in" intention handles negation
This commit is contained in:
+12
-7
@@ -46,19 +46,24 @@ public class ReplaceContainsIntention : JetSelfTargetingRangeIntention<JetDotQua
|
|||||||
val argument = element.callExpression!!.getValueArguments().single().getArgumentExpression()!!
|
val argument = element.callExpression!!.getValueArguments().single().getArgumentExpression()!!
|
||||||
val receiver = element.getReceiverExpression()
|
val receiver = element.getReceiverExpression()
|
||||||
|
|
||||||
// Append semicolon to previous statement if needed
|
|
||||||
val psiFactory = JetPsiFactory(element)
|
val psiFactory = JetPsiFactory(element)
|
||||||
|
|
||||||
|
val prefixExpression = element.getParent() as? JetPrefixExpression
|
||||||
|
val expression = if (prefixExpression != null && prefixExpression.getOperationToken() == JetTokens.EXCL) {
|
||||||
|
prefixExpression.replace(psiFactory.createExpressionByPattern("$0 !in $1", argument, receiver))
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
element.replace(psiFactory.createExpressionByPattern("$0 in $1", argument, receiver))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Append semicolon to previous statement if needed
|
||||||
if (argument is JetFunctionLiteralExpression) {
|
if (argument is JetFunctionLiteralExpression) {
|
||||||
val previousElement = JetPsiUtil.skipSiblingsBackwardByPredicate(element) {
|
val previousElement = JetPsiUtil.skipSiblingsBackwardByPredicate(expression) {
|
||||||
// I checked, it can't be null.
|
it.getNode().getElementType() in JetTokens.WHITE_SPACE_OR_COMMENT_BIT_SET
|
||||||
it!!.getNode()?.getElementType() in JetTokens.WHITE_SPACE_OR_COMMENT_BIT_SET
|
|
||||||
}
|
}
|
||||||
if (previousElement != null && previousElement is JetExpression) {
|
if (previousElement != null && previousElement is JetExpression) {
|
||||||
// If the parent is null, something is very wrong.
|
|
||||||
previousElement.getParent()!!.addAfter(psiFactory.createSemicolon(), previousElement)
|
previousElement.getParent()!!.addAfter(psiFactory.createSemicolon(), previousElement)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
element.replace(psiFactory.createExpressionByPattern("$0 in $1", argument, receiver))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fun test() {
|
||||||
|
class Test{
|
||||||
|
fun contains(a: Int) : Boolean = true
|
||||||
|
}
|
||||||
|
val test = Test()
|
||||||
|
if (!test.<caret>contains(1)) return
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fun test() {
|
||||||
|
class Test{
|
||||||
|
fun contains(a: Int) : Boolean = true
|
||||||
|
}
|
||||||
|
val test = Test()
|
||||||
|
if (1 !in test) return
|
||||||
|
}
|
||||||
@@ -2222,6 +2222,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("notContains.kt")
|
||||||
|
public void testNotContains() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceContains/notContains.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simpleArgument.kt")
|
@TestMetadata("simpleArgument.kt")
|
||||||
public void testSimpleArgument() throws Exception {
|
public void testSimpleArgument() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceContains/simpleArgument.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/conventionNameCalls/replaceContains/simpleArgument.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user