Add Intention for single character substring #KT-22171 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
29eb594309
commit
a28bc830f5
+1
@@ -0,0 +1 @@
|
|||||||
|
<spot>"abc"[0]</spot>
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
<spot>"abc".substring(0, 1)</spot>
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
This intention replaces call like <code>"abc".substring(0, 1)</code> with <code>"abc"[0]</code> call.
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -1526,6 +1526,11 @@
|
|||||||
<category>Kotlin</category>
|
<category>Kotlin</category>
|
||||||
</intentionAction>
|
</intentionAction>
|
||||||
|
|
||||||
|
<intentionAction>
|
||||||
|
<className>org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithIndexingOperationIntention</className>
|
||||||
|
<category>Kotlin</category>
|
||||||
|
</intentionAction>
|
||||||
|
|
||||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
|
||||||
displayName="Object literal can be converted to lambda"
|
displayName="Object literal can be converted to lambda"
|
||||||
groupPath="Kotlin"
|
groupPath="Kotlin"
|
||||||
|
|||||||
+55
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||||
|
* that can be found in the license/LICENSE.txt file.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.intentions
|
||||||
|
|
||||||
|
import com.intellij.openapi.editor.Editor
|
||||||
|
import com.intellij.openapi.util.TextRange
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
|
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
|
||||||
|
|
||||||
|
class ReplaceSubstringWithIndexingOperationIntention : ReplaceSubstringIntention("Replace 'substring' call with indexing operation call") {
|
||||||
|
override fun applicabilityRange(element: KtDotQualifiedExpression): TextRange? {
|
||||||
|
if (element.isSubstringMethod()) {
|
||||||
|
return applicabilityRangeInner(element)
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange? {
|
||||||
|
val arguments = element.callExpression?.valueArguments ?: return null
|
||||||
|
if (arguments.size != 2) return null
|
||||||
|
|
||||||
|
val arg1 = element.getValueArgument(0) ?: return null
|
||||||
|
val arg2 = element.getValueArgument(1) ?: return null
|
||||||
|
if (arg1 + 1 != arg2) return null
|
||||||
|
|
||||||
|
return getTextRange(element)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
|
||||||
|
val expression = element.callExpression?.valueArguments?.firstOrNull()?.getArgumentExpression() ?: return
|
||||||
|
element.replaceWith("$0[$1]", expression)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun KtDotQualifiedExpression.isSubstringMethod(): Boolean {
|
||||||
|
val resolvedCall = toResolvedCall(BodyResolveMode.PARTIAL) ?: return false
|
||||||
|
return (resolvedCall.resultingDescriptor.fqNameUnsafe.asString() == "kotlin.text.substring")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun KtDotQualifiedExpression.getValueArgument(index: Int): Int? {
|
||||||
|
val bindingContext = analyze()
|
||||||
|
val resolvedCall = callExpression.getResolvedCall(bindingContext) ?: return null
|
||||||
|
val expression = resolvedCall.call.valueArguments[index].getArgumentExpression() as? KtConstantExpression ?: return null
|
||||||
|
val constant = ConstantExpressionEvaluator.getConstant(expression, bindingContext) ?: return null
|
||||||
|
val constantType = bindingContext.getType(expression) ?: return null
|
||||||
|
return constant.getValue(constantType) as? Int
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithIndexingOperationIntention
|
||||||
Vendored
+5
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
"abc".substring<caret>(1, 2)
|
||||||
|
}
|
||||||
Vendored
+5
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
"abc"[1]
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
"abc".substring<caret>(0, 1)
|
||||||
|
}
|
||||||
+5
@@ -0,0 +1,5 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
"abc"[0]
|
||||||
|
}
|
||||||
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun foo() {
|
||||||
|
"abc".substring<caret>(0, 10)
|
||||||
|
}
|
||||||
@@ -14056,6 +14056,33 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("idea/testData/intentions/replaceSubstringWithIndexingOperation")
|
||||||
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
public static class ReplaceSubstringWithIndexingOperation extends AbstractIntentionTest {
|
||||||
|
public void testAllFilesPresentInReplaceSubstringWithIndexingOperation() throws Exception {
|
||||||
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceSubstringWithIndexingOperation"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("oneFirstTwoSecondArgument.kt")
|
||||||
|
public void testOneFirstTwoSecondArgument() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSubstringWithIndexingOperation/oneFirstTwoSecondArgument.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("simple.kt")
|
||||||
|
public void testSimple() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSubstringWithIndexingOperation/simple.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("zeroFirstTenSecondArgument.kt")
|
||||||
|
public void testZeroFirstTenSecondArgument() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/replaceSubstringWithIndexingOperation/zeroFirstTenSecondArgument.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/intentions/replaceSubstringWithSubstringAfter")
|
@TestMetadata("idea/testData/intentions/replaceSubstringWithSubstringAfter")
|
||||||
@TestDataPath("$PROJECT_ROOT")
|
@TestDataPath("$PROJECT_ROOT")
|
||||||
@RunWith(JUnit3RunnerWithInners.class)
|
@RunWith(JUnit3RunnerWithInners.class)
|
||||||
|
|||||||
Reference in New Issue
Block a user