Suggest to replace 'substring' calls by take/drop/dropLast calls when possible

Code review fixes
#KT-10196 Fixed
This commit is contained in:
Anton Sukhonosenko
2016-01-14 22:40:50 +03:00
parent ba0efda2f0
commit b2e98e9353
23 changed files with 87 additions and 147 deletions
@@ -1 +0,0 @@
s.<spot>drop(5)</spot>
@@ -1 +0,0 @@
s.<spot>substring(5)</spot>
@@ -1,5 +0,0 @@
<html>
<body>
This intention replaces 'substring' call with 'drop' call.
</body>
</html>
@@ -1,5 +1,5 @@
<html>
<body>
This intention replaces 'substring' call with 'dropLast' call.
This intention replaces call like <code>s.substring(0, s.length - x)</code> with <code>s.dropLast(x)</code> call.
</body>
</html>
@@ -1,5 +1,5 @@
<html>
<body>
This intention replaces 'substring' and 'indexOf' calls with 'substringAfter' call.
This intention replaces call like <code>s.substring(s.indexOf(x))</code> with <code>s.substringAfter(x)</code> call.
</body>
</html>
@@ -1,5 +1,5 @@
<html>
<body>
This intention replaces 'substring' and 'indexOf' calls with 'substringBefore' call.
This intention replaces call like <code>s.substring(0, s.indexOf(x))</code> with <code>s.substringBefore(x)</code> call.
</body>
</html>
@@ -1,5 +1,5 @@
<html>
<body>
This intention replaces 'substring' call with 'take' call.
This intention replaces call like <code>s.substring(0, x)</code> with <code>s.take(x)</code> call.
</body>
</html>
-5
View File
@@ -880,11 +880,6 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithDropIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithDropLastIntention</className>
<category>Kotlin</category>
@@ -16,37 +16,54 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.codeInsight.intention.HighPriorityAction
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.evaluatesTo
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStableVariable
import org.jetbrains.kotlin.psi.KtConstantExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
abstract class ReplaceSubstringIntention(text: String) : SelfTargetingRangeIntention<KtDotQualifiedExpression>(KtDotQualifiedExpression::class.java, text), HighPriorityAction {
abstract class ReplaceSubstringIntention(text: String) : SelfTargetingRangeIntention<KtDotQualifiedExpression>(KtDotQualifiedExpression::class.java, text) {
protected abstract fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange?
override fun applicabilityRange(element: KtDotQualifiedExpression): TextRange? {
if (!element.receiverExpression.isStableVariable()) return null
val resolvedCall = element.toResolvedCall(BodyResolveMode.PARTIAL) ?: return null
if (resolvedCall.resultingDescriptor.fqNameUnsafe.asString() != "kotlin.text.substring") return null
return applicabilityRangeInner(element)
if (element.receiverExpression.isStableVariable() && element.isMethodCall("kotlin.text.substring")) {
return applicabilityRangeInner(element)
}
return null
}
protected fun isIndexOfCall(expression: KtExpression?, expectedReceiver: KtExpression): Boolean {
return expression is KtDotQualifiedExpression
&& expression.isMethodCall("kotlin.text.indexOf")
&& expression.receiverExpression.evaluatesTo(expectedReceiver)
&& expression.callExpression!!.valueArguments.size == 1
}
private fun KtDotQualifiedExpression.isMethodCall(fqMethodName: String): Boolean {
val resolvedCall = toResolvedCall(BodyResolveMode.PARTIAL) ?: return false
return resolvedCall.resultingDescriptor.fqNameUnsafe.asString() == fqMethodName
}
protected fun KtDotQualifiedExpression.isFirstArgumentZero(): Boolean {
val resolvedCall = toResolvedCall(BodyResolveMode.PARTIAL) ?: return false
val bindingContext = analyze()
val resolvedCall = callExpression.getResolvedCall(bindingContext) ?: return false
val expression = resolvedCall.call.valueArguments[0].getArgumentExpression() as? KtConstantExpression ?: return false
val bindingContext = expression.analyze()
val constant = ConstantExpressionEvaluator.getConstant(expression, bindingContext) ?: return false
val constantType = bindingContext.getType(expression) ?: return false
return constant.getValue(constantType) == 0
}
protected fun getTextRange(element: KtDotQualifiedExpression): TextRange? {
return element.callExpression!!.textRange
return element.callExpression?.textRange
}
}
protected fun KtDotQualifiedExpression.replaceWith(pattern: String, argument: KtExpression) {
val psiFactory = KtPsiFactory(this)
replace(psiFactory.createExpressionByPattern(pattern, receiverExpression, argument))
}
}
@@ -1,39 +0,0 @@
/*
* Copyright 2010-2016 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.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
class ReplaceSubstringWithDropIntention : ReplaceSubstringIntention("Replace 'substring' call with 'drop' call") {
override fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange? {
val arguments = element.callExpression?.valueArguments ?: return null
if (arguments.count() != 1) return null
return getTextRange(element)
}
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
val receiver = element.receiverExpression
val argument = element.callExpression!!.valueArguments[0].getArgumentExpression()!!
val psiFactory = KtPsiFactory(element)
element.replace(psiFactory.createExpressionByPattern("$0.drop($1)", receiver, argument))
}
}
@@ -25,7 +25,7 @@ import org.jetbrains.kotlin.psi.*
class ReplaceSubstringWithDropLastIntention : ReplaceSubstringIntention("Replace 'substring' call with 'dropLast' call") {
override fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange? {
val arguments = element.callExpression?.valueArguments ?: return null
if (arguments.count() != 2 || !element.isFirstArgumentZero()) return null
if (arguments.size != 2 || !element.isFirstArgumentZero()) return null
val secondArgumentExpression = arguments[1].getArgumentExpression()
@@ -39,12 +39,10 @@ class ReplaceSubstringWithDropLastIntention : ReplaceSubstringIntention("Replace
}
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
val receiver = element.receiverExpression
val argument = element.callExpression!!.valueArguments[1].getArgumentExpression()!!
val rightExpression = (argument as KtBinaryExpression).right!!
val psiFactory = KtPsiFactory(element)
element.replace(psiFactory.createExpressionByPattern("$0.dropLast($1)", receiver, rightExpression))
element.replaceWith("$0.dropLast($1)", rightExpression)
}
private fun isLengthAccess(expression: KtExpression?, expectedReceiver: KtExpression): Boolean {
@@ -27,7 +27,7 @@ class ReplaceSubstringWithSubstringAfterIntention : ReplaceSubstringIntention("R
override fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange? {
val arguments = element.callExpression?.valueArguments ?: return null
if (arguments.count() == 1 && isIndexOfCall(arguments[0].getArgumentExpression(), element.receiverExpression)) {
if (arguments.size == 1 && isIndexOfCall(arguments[0].getArgumentExpression(), element.receiverExpression)) {
return getTextRange(element)
}
@@ -45,7 +45,7 @@ class ReplaceSubstringWithSubstringBeforeIntention : ReplaceSubstringIntention("
override fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange? {
val arguments = element.callExpression?.valueArguments ?: return null
if (arguments.count() == 2
if (arguments.size == 2
&& element.isFirstArgumentZero()
&& isIndexOfCall(arguments[1].getArgumentExpression(), element.receiverExpression)) {
return getTextRange(element)
@@ -61,21 +61,6 @@ class ReplaceSubstringWithSubstringBeforeIntention : ReplaceSubstringIntention("
}
}
private fun KtDotQualifiedExpression.replaceWith(pattern: String, argument: KtExpression) {
val psiFactory = KtPsiFactory(this)
replace(psiFactory.createExpressionByPattern(pattern, receiverExpression, argument))
}
private fun KtDotQualifiedExpression.getArgumentExpression(index: Int): KtExpression {
return callExpression!!.valueArguments[index].getArgumentExpression()!!
}
private fun isIndexOfCall(expression: KtExpression?, expectedReceiver: KtExpression): Boolean {
if (expression !is KtDotQualifiedExpression) return false
val resolvedCall = expression.toResolvedCall(BodyResolveMode.PARTIAL) ?: return false
if (resolvedCall.resultingDescriptor.fqNameUnsafe.asString() != "kotlin.text.indexOf") return false
if (!expression.receiverExpression.evaluatesTo(expectedReceiver)) return false
return expression.callExpression!!.valueArguments.count() == 1
}
@@ -25,17 +25,14 @@ import org.jetbrains.kotlin.psi.createExpressionByPattern
class ReplaceSubstringWithTakeIntention : ReplaceSubstringIntention("Replace 'substring' call with 'take' call") {
override fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange? {
val arguments = element.callExpression?.valueArguments ?: return null
if (arguments.count() == 2 && element.isFirstArgumentZero()) {
if (arguments.size == 2 && element.isFirstArgumentZero()) {
return getTextRange(element)
}
return null
}
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
val receiver = element.receiverExpression
val argument = element.callExpression!!.valueArguments[1].getArgumentExpression()!!
val psiFactory = KtPsiFactory(element)
element.replace(psiFactory.createExpressionByPattern("$0.take($1)", receiver, argument))
element.replaceWith("$0.take($1)", argument)
}
}
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithDropIntention
@@ -1,5 +0,0 @@
// WITH_RUNTIME
fun foo(s: String) {
s.substring<caret>(4)
}
@@ -1,5 +0,0 @@
// WITH_RUNTIME
fun foo(s: String) {
s.drop(4)
}
@@ -1,5 +0,0 @@
// WITH_RUNTIME
fun foo(s: String) {
s.substring<caret>(4);
}
@@ -1,5 +0,0 @@
// WITH_RUNTIME
fun foo(s: String) {
s.drop(4);
}
@@ -1,6 +0,0 @@
// IS_APPLICABLE: false
// WITH_RUNTIME
fun foo(s: String) {
s.substring<caret>(0, 10)
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class A() {
fun bar(): String = null!!
}
fun foo(a: A) {
a.bar().substring<caret>(0, a.bar().length - 5)
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class A() {
fun bar(): String = null!!
}
fun foo(a: A) {
a.bar().substring<caret>(a.bar().indexOf('x'))
}
@@ -0,0 +1,10 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
class A() {
fun bar(): String = null!!
}
fun foo(a: A) {
a.bar().substring<caret>(0, a.bar().indexOf('x'))
}
@@ -7573,33 +7573,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/replaceSubstringWithDrop")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceSubstringWithDrop extends AbstractIntentionTest {
public void testAllFilesPresentInReplaceSubstringWithDrop() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceSubstringWithDrop"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("replaceWithDrop.kt")
public void testReplaceWithDrop() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSubstringWithDrop/replaceWithDrop.kt");
doTest(fileName);
}
@TestMetadata("semicolon.kt")
public void testSemicolon() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSubstringWithDrop/semicolon.kt");
doTest(fileName);
}
@TestMetadata("substringWithTwoArguments.kt")
public void testSubstringWithTwoArguments() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSubstringWithDrop/substringWithTwoArguments.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/replaceSubstringWithDropLast")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -7614,6 +7587,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("methodCallReceiver.kt")
public void testMethodCallReceiver() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSubstringWithDropLast/methodCallReceiver.kt");
doTest(fileName);
}
@TestMetadata("nonZeroFirstArgument.kt")
public void testNonZeroFirstArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSubstringWithDropLast/nonZeroFirstArgument.kt");
@@ -7647,6 +7626,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("methodCallReceiver.kt")
public void testMethodCallReceiver() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSubstringWithSubstringAfter/methodCallReceiver.kt");
doTest(fileName);
}
@TestMetadata("replaceWithSubstringAfter.kt")
public void testReplaceWithSubstringAfter() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSubstringWithSubstringAfter/replaceWithSubstringAfter.kt");
@@ -7674,6 +7659,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("methodCallReceiver.kt")
public void testMethodCallReceiver() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSubstringWithSubstringBefore/methodCallReceiver.kt");
doTest(fileName);
}
@TestMetadata("nonZeroFirstArgument.kt")
public void testNonZeroFirstArgument() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSubstringWithSubstringBefore/nonZeroFirstArgument.kt");