Add "flip equals" intention for String.equals extension from stdlib
So #KT-19282 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
0efdcb59fa
commit
82fc221470
+1
@@ -0,0 +1 @@
|
|||||||
|
"ABC".equals("abc", true)
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
"abc".equals("ABC", true)
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
This intention replaces string1.equals(string2, ignoreCase) with string2.equals(string1, ignoreCase).
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1151,6 +1151,11 @@
|
|||||||
<category>Kotlin</category>
|
<category>Kotlin</category>
|
||||||
</intentionAction>
|
</intentionAction>
|
||||||
|
|
||||||
|
<intentionAction>
|
||||||
|
<className>org.jetbrains.kotlin.idea.intentions.SwapStringEqualsIgnoreCaseIntention</className>
|
||||||
|
<category>Kotlin</category>
|
||||||
|
</intentionAction>
|
||||||
|
|
||||||
<intentionAction>
|
<intentionAction>
|
||||||
<className>org.jetbrains.kotlin.idea.intentions.SplitIfIntention</className>
|
<className>org.jetbrains.kotlin.idea.intentions.SplitIfIntention</className>
|
||||||
<category>Kotlin</category>
|
<category>Kotlin</category>
|
||||||
|
|||||||
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.intentions
|
||||||
|
|
||||||
|
import com.intellij.codeInsight.intention.LowPriorityAction
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||||
|
import org.jetbrains.kotlin.idea.core.moveCaret
|
||||||
|
import org.jetbrains.kotlin.idea.core.replaced
|
||||||
|
import org.jetbrains.kotlin.name.FqName
|
||||||
|
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
||||||
|
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||||
|
import org.jetbrains.kotlin.psi.createExpressionByPattern
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
|
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
|
||||||
|
|
||||||
|
class SwapStringEqualsIgnoreCaseIntention : SelfTargetingRangeIntention<KtDotQualifiedExpression>(KtDotQualifiedExpression::class.java, "Flip 'equals'"), LowPriorityAction {
|
||||||
|
|
||||||
|
override fun applicabilityRange(element: KtDotQualifiedExpression): TextRange? {
|
||||||
|
val descriptor = element.getCallableDescriptor() ?: return null
|
||||||
|
|
||||||
|
val fqName: FqName = descriptor.fqNameOrNull() ?: return null
|
||||||
|
if (fqName.asString() != "kotlin.text.equals") return null
|
||||||
|
|
||||||
|
val valueParameters = descriptor.valueParameters.takeIf { it.size == 2 } ?: return null
|
||||||
|
if (!KotlinBuiltIns.isStringOrNullableString(valueParameters[0].type)) return null
|
||||||
|
if (!KotlinBuiltIns.isBoolean(valueParameters[1].type)) return null
|
||||||
|
|
||||||
|
return element.callExpression?.calleeExpression?.textRange
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
|
||||||
|
val callExpression = element.callExpression ?: return
|
||||||
|
val offset = (editor?.caretModel?.offset ?: 0) - (callExpression.calleeExpression?.startOffset ?: 0)
|
||||||
|
val receiverExpression = element.receiverExpression
|
||||||
|
val valueArguments = callExpression.valueArguments.takeIf { it.size == 2 } ?: return
|
||||||
|
val newElement = KtPsiFactory(element).createExpressionByPattern(
|
||||||
|
"$0.equals($1, $2)",
|
||||||
|
valueArguments[0].getArgumentExpression()!!,
|
||||||
|
receiverExpression,
|
||||||
|
valueArguments[1].text
|
||||||
|
)
|
||||||
|
val replacedElement = element.replaced(newElement) as? KtDotQualifiedExpression
|
||||||
|
replacedElement?.callExpression?.calleeExpression?.startOffset?.let {
|
||||||
|
editor?.moveCaret(it + offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
org.jetbrains.kotlin.idea.intentions.SwapStringEqualsIgnoreCaseIntention
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
fun test() {
|
||||||
|
"ABC".equals<caret>("abc")
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
fun test() {
|
||||||
|
"ABC".equa<caret>ls(Foo().bar(), ignoreCase = true)
|
||||||
|
}
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
fun bar(): String = ""
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
fun test() {
|
||||||
|
Foo().bar().equa<caret>ls("ABC", ignoreCase = true)
|
||||||
|
}
|
||||||
|
|
||||||
|
class Foo {
|
||||||
|
fun bar(): String = ""
|
||||||
|
}
|
||||||
@@ -15348,6 +15348,27 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/intentions/swapStringEqualsIgnoreCase")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class SwapStringEqualsIgnoreCase extends AbstractIntentionTest {
|
||||||
|
public void testAllFilesPresentInSwapStringEqualsIgnoreCase() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/swapStringEqualsIgnoreCase"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("equals.kt")
|
||||||
|
public void testEquals() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/swapStringEqualsIgnoreCase/equals.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("equalsIgnoreCase.kt")
|
||||||
|
public void testEqualsIgnoreCase() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/swapStringEqualsIgnoreCase/equalsIgnoreCase.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/intentions/toInfixCall")
|
@TestMetadata("idea/testData/intentions/toInfixCall")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user