Convert ReplaceSubstringIntention to ReplaceSubstringInspection

Relates to #KT-31502
This commit is contained in:
Dmitry Gridin
2019-05-27 19:49:59 +07:00
parent 2310826ec9
commit 8988b4b344
77 changed files with 463 additions and 476 deletions
@@ -19,38 +19,42 @@ package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInspection.*
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtVisitorVoid
abstract class AbstractApplicabilityBasedInspection<TElement: KtElement>(
val elementType: Class<TElement>
abstract class AbstractApplicabilityBasedInspection<TElement : KtElement>(
val elementType: Class<TElement>
) : AbstractKotlinInspection() {
final override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession) =
object : KtVisitorVoid() {
override fun visitKtElement(element: KtElement) {
super.visitKtElement(element)
object : KtVisitorVoid() {
override fun visitKtElement(element: KtElement) {
super.visitKtElement(element)
if (!elementType.isInstance(element) || element.textLength == 0) return
@Suppress("UNCHECKED_CAST")
visitTargetElement(element as TElement, holder, isOnTheFly)
}
if (!elementType.isInstance(element) || element.textLength == 0) return
@Suppress("UNCHECKED_CAST")
visitTargetElement(element as TElement, holder, isOnTheFly)
}
}
// This function should be called from visitor built by a derived inspection
protected fun visitTargetElement(element: TElement, holder: ProblemsHolder, isOnTheFly: Boolean) {
if (!isApplicable(element)) return
holder.registerProblemWithoutOfflineInformation(
inspectionTarget(element),
inspectionText(element),
isOnTheFly,
inspectionHighlightType(element),
LocalFix(fixText(element))
inspectionTarget(element),
inspectionText(element),
isOnTheFly,
inspectionHighlightType(element),
inspectionRange(element),
LocalFix(fixText(element))
)
}
open fun inspectionRange(element: TElement): TextRange? = null
open fun inspectionTarget(element: TElement): PsiElement = element
open fun inspectionHighlightType(element: TElement): ProblemHighlightType = ProblemHighlightType.GENERIC_ERROR_OR_WARNING
+45 -25
View File
@@ -1053,26 +1053,6 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithDropLastIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithSubstringAfterIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithSubstringBeforeIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithTakeIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveBracesIntention</className>
<category>Kotlin</category>
@@ -1578,11 +1558,6 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithIndexingOperationIntention</className>
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertUnsafeCastCallToUnsafeCastIntention</className>
<category>Kotlin</category>
@@ -2072,6 +2047,51 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.substring.ReplaceSubstringWithDropLastInspection"
displayName="Replace 'substring' call with 'dropLast' call"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.substring.ReplaceSubstringWithTakeInspection"
displayName="Replace 'substring' call with 'take' call"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.substring.ReplaceSubstringWithIndexingOperationInspection"
displayName="Replace 'substring' call with indexing operation call"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.substring.ReplaceSubstringWithSubstringAfterInspection"
displayName="Replace 'substring' call with 'substringAfter' call"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.substring.ReplaceSubstringWithSubstringBeforeInspection"
displayName="Replace 'substring' call with 'substringBefore' call"
groupPath="Kotlin"
groupName="Style issues"
enabledByDefault="true"
level="INFORMATION"
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.CanBeParameterInspection"
displayName="Constructor parameter is never used as a property"
groupPath="Kotlin"
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports calls like <b>s.substring(0, s.length - x)</b> replaceable with <b>s.dropLast(x)</b>.
</body>
</html>
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports calls like <b>"abc".substring(0, 1)</b> replaceable with <b>"abc"[0]</b>.
</body>
</html>
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports calls like <b>s.substring(s.indexOf(x))</b> replaceable with <b>s.substringAfter(x)</b>.
</body>
</html>
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports calls like <b>s.substring(0, s.indexOf(x))</b> replaceable with <b>s.substringBefore(x)</b>.
</body>
</html>
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports calls like <b>s.substring(0, x)</b> replaceable with <b>s.take(x)</b>.
</body>
</html>
@@ -1 +0,0 @@
s.<spot>dropLast(5)</spot>
@@ -1 +0,0 @@
s.<spot>substring(0, s.length - 5)</spot>
@@ -1,5 +0,0 @@
<html>
<body>
This intention replaces calls like <b>s.substring(0, s.length - x)</b> with <b>s.dropLast(x)</b>.
</body>
</html>
@@ -1 +0,0 @@
<spot>"abc".substring(0, 1)</spot>
@@ -1,5 +0,0 @@
<html>
<body>
This intention replaces calls like <b>"abc".substring(0, 1)</b> with <b>"abc"[0]</b>.
</body>
</html>
@@ -1 +0,0 @@
s.<spot>substringAfter('x')</spot>
@@ -1 +0,0 @@
s.<spot>substring(s.indexOf('x'))</spot>
@@ -1,5 +0,0 @@
<html>
<body>
This intention replaces calls like <b>s.substring(s.indexOf(x))</b> with <b>s.substringAfter(x)</b>.
</body>
</html>
@@ -1 +0,0 @@
s.<spot>substringBefore('x')</spot>
@@ -1 +0,0 @@
s.<spot>substring(0, s.indexOf('x'))</spot>
@@ -1,5 +0,0 @@
<html>
<body>
This intention replaces calls like <b>s.substring(0, s.indexOf(x))</b> with <b>s.substringBefore(x)</b>.
</body>
</html>
@@ -1 +0,0 @@
s.<spot>take(10)</spot>
@@ -1 +0,0 @@
s.<spot>substring(0, 10)</spot>
@@ -1,5 +0,0 @@
<html>
<body>
This intention replaces calls like <b>s.substring(0, x)</b> with <b>s.take(x)</b>.
</body>
</html>
@@ -1,46 +1,43 @@
/*
* 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.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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
package org.jetbrains.kotlin.idea.inspections.substring
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.inspections.AbstractApplicabilityBasedInspection
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.evaluatesTo
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStableSimpleExpression
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.idea.intentions.toResolvedCall
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.startOffset
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) {
protected abstract fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange?
abstract class ReplaceSubstringInspection :
AbstractApplicabilityBasedInspection<KtDotQualifiedExpression>(KtDotQualifiedExpression::class.java) {
protected abstract fun isApplicableInner(element: KtDotQualifiedExpression): Boolean
protected open val isAlwaysStable: Boolean = false
override fun applicabilityRange(element: KtDotQualifiedExpression): TextRange? {
if (element.receiverExpression.isStableSimpleExpression() && element.isMethodCall("kotlin.text.substring")) {
return applicabilityRangeInner(element)
}
return null
}
final override fun isApplicable(element: KtDotQualifiedExpression): Boolean =
if ((isAlwaysStable || element.receiverExpression.isStableSimpleExpression()) && element.isMethodCall("kotlin.text.substring")) {
isApplicableInner(element)
} else
false
override fun inspectionRange(element: KtDotQualifiedExpression): TextRange? =
element.callExpression?.calleeExpression?.textRange?.shiftLeft(element.startOffset)
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
&& expression.isMethodCall("kotlin.text.indexOf")
&& expression.receiverExpression.evaluatesTo(expectedReceiver)
&& expression.callExpression!!.valueArguments.size == 1
}
private fun KtDotQualifiedExpression.isMethodCall(fqMethodName: String): Boolean {
@@ -58,10 +55,6 @@ abstract class ReplaceSubstringIntention(text: String) : SelfTargetingRangeInten
return constant.getValue(constantType) == 0
}
protected fun getTextRange(element: KtDotQualifiedExpression): TextRange? {
return element.callExpression?.textRange
}
protected fun KtDotQualifiedExpression.replaceWith(pattern: String, argument: KtExpression) {
val psiFactory = KtPsiFactory(this)
replace(psiFactory.createExpressionByPattern(pattern, receiverExpression, argument))
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.inspections.substring
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.evaluatesTo
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
class ReplaceSubstringWithDropLastInspection : ReplaceSubstringInspection() {
override fun inspectionText(element: KtDotQualifiedExpression): String = "Replace 'substring' call with 'dropLast' call"
override val defaultFixText: String = "Replace 'substring' call with 'dropLast' call"
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
if (element !is KtDotQualifiedExpression) return
val argument = element.callExpression!!.valueArguments[1].getArgumentExpression()!!
val rightExpression = (argument as KtBinaryExpression).right!!
element.replaceWith("$0.dropLast($1)", rightExpression)
}
override fun isApplicableInner(element: KtDotQualifiedExpression): Boolean {
val arguments = element.callExpression?.valueArguments ?: return false
if (arguments.size != 2 || !element.isFirstArgumentZero()) return false
val secondArgumentExpression = arguments[1].getArgumentExpression() as? KtBinaryExpression ?: return false
if (secondArgumentExpression.operationReference.getReferencedNameElementType() != KtTokens.MINUS) return false
return isLengthAccess(secondArgumentExpression.left, element.receiverExpression)
}
private fun isLengthAccess(expression: KtExpression?, expectedReceiver: KtExpression): Boolean =
expression is KtDotQualifiedExpression
&& expression.selectorExpression.let { it is KtNameReferenceExpression && it.getReferencedName() == "length" }
&& expression.receiverExpression.evaluatesTo(expectedReceiver)
}
@@ -1,48 +1,39 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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
package org.jetbrains.kotlin.idea.inspections.substring
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.psi.KtConstantExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
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)
}
class ReplaceSubstringWithIndexingOperationInspection :
ReplaceSubstringInspection() {
override fun inspectionText(element: KtDotQualifiedExpression): String = "Replace 'substring' call with indexing operation call"
override val defaultFixText: String = "Replace 'substring' call with indexing operation call"
override val isAlwaysStable: Boolean = true
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?) {
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
if (element !is KtDotQualifiedExpression) return
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")
override fun isApplicableInner(element: KtDotQualifiedExpression): Boolean {
val arguments = element.callExpression?.valueArguments ?: return false
if (arguments.size != 2) return false
val arg1 = element.getValueArgument(0) ?: return false
val arg2 = element.getValueArgument(1) ?: return false
return arg1 + 1 == arg2
}
private fun KtDotQualifiedExpression.getValueArgument(index: Int): Int? {
@@ -0,0 +1,58 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.inspections.substring
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtExpression
class ReplaceSubstringWithSubstringAfterInspection : ReplaceSubstringInspection() {
override fun inspectionText(element: KtDotQualifiedExpression): String = "Replace 'substring' call with 'substringAfter' call"
override val defaultFixText: String = "Replace 'substring' call with 'substringAfter' call"
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
if (element !is KtDotQualifiedExpression) return
element.replaceWith(
"$0.substringAfter($1)",
(element.getArgumentExpression(0) as KtDotQualifiedExpression).getArgumentExpression(0)
)
}
override fun isApplicableInner(element: KtDotQualifiedExpression): Boolean {
val arguments = element.callExpression?.valueArguments ?: return false
return arguments.size == 1 && isIndexOfCall(arguments[0].getArgumentExpression(), element.receiverExpression)
}
}
class ReplaceSubstringWithSubstringBeforeInspection : ReplaceSubstringInspection() {
override fun inspectionText(element: KtDotQualifiedExpression): String = "Replace 'substring' call with 'substringBefore' call"
override val defaultFixText: String = "Replace 'substring' call with 'substringBefore' call"
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
if (element !is KtDotQualifiedExpression) return
element.replaceWith(
"$0.substringBefore($1)",
(element.getArgumentExpression(1) as KtDotQualifiedExpression).getArgumentExpression(0)
)
}
override fun isApplicableInner(element: KtDotQualifiedExpression): Boolean {
val arguments = element.callExpression?.valueArguments ?: return false
return arguments.size == 2
&& element.isFirstArgumentZero()
&& isIndexOfCall(arguments[1].getArgumentExpression(), element.receiverExpression)
}
}
private fun KtDotQualifiedExpression.getArgumentExpression(index: Int): KtExpression {
return callExpression!!.valueArguments[index].getArgumentExpression()!!
}
@@ -0,0 +1,29 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.inspections.substring
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.intentions.callExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
class ReplaceSubstringWithTakeInspection : ReplaceSubstringInspection() {
override fun inspectionText(element: KtDotQualifiedExpression): String = "Replace 'substring' call with 'take' call"
override val defaultFixText: String = "Replace 'substring' call with 'take' call"
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
if (element !is KtDotQualifiedExpression) return
val argument = element.callExpression!!.valueArguments[1].getArgumentExpression()!!
element.replaceWith("$0.take($1)", argument)
}
override fun isApplicableInner(element: KtDotQualifiedExpression): Boolean {
val arguments = element.callExpression?.valueArguments ?: return false
return arguments.size == 2 && element.isFirstArgumentZero()
}
}
@@ -1,56 +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.idea.intentions.branchedTransformations.evaluatesTo
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
class ReplaceSubstringWithDropLastIntention : ReplaceSubstringIntention("Replace 'substring' call with 'dropLast' call") {
override fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange? {
val arguments = element.callExpression?.valueArguments ?: return null
if (arguments.size != 2 || !element.isFirstArgumentZero()) return null
val secondArgumentExpression = arguments[1].getArgumentExpression()
if (secondArgumentExpression !is KtBinaryExpression) return null
if (secondArgumentExpression.operationReference.getReferencedNameElementType() != KtTokens.MINUS) return null
if (isLengthAccess(secondArgumentExpression.left, element.receiverExpression)) {
return getTextRange(element)
}
return null
}
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
val argument = element.callExpression!!.valueArguments[1].getArgumentExpression()!!
val rightExpression = (argument as KtBinaryExpression).right!!
element.replaceWith("$0.dropLast($1)", rightExpression)
}
private fun isLengthAccess(expression: KtExpression?, expectedReceiver: KtExpression): Boolean {
return expression is KtDotQualifiedExpression
&& expression.selectorExpression.let { it is KtNameReferenceExpression && it.getReferencedName() == "length" }
&& expression.receiverExpression.evaluatesTo(expectedReceiver)
}
}
@@ -1,64 +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.KtExpression
class ReplaceSubstringWithSubstringAfterIntention : ReplaceSubstringIntention("Replace 'substring' call with 'substringAfter' call") {
override fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange? {
val arguments = element.callExpression?.valueArguments ?: return null
if (arguments.size == 1 && isIndexOfCall(arguments[0].getArgumentExpression(), element.receiverExpression)) {
return getTextRange(element)
}
return null
}
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
element.replaceWith(
"$0.substringAfter($1)",
(element.getArgumentExpression(0) as KtDotQualifiedExpression).getArgumentExpression(0))
}
}
class ReplaceSubstringWithSubstringBeforeIntention : ReplaceSubstringIntention("Replace 'substring' call with 'substringBefore' call") {
override fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange? {
val arguments = element.callExpression?.valueArguments ?: return null
if (arguments.size == 2
&& element.isFirstArgumentZero()
&& isIndexOfCall(arguments[1].getArgumentExpression(), element.receiverExpression)) {
return getTextRange(element)
}
return null
}
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
element.replaceWith(
"$0.substringBefore($1)",
(element.getArgumentExpression(1) as KtDotQualifiedExpression).getArgumentExpression(0))
}
}
private fun KtDotQualifiedExpression.getArgumentExpression(index: Int): KtExpression {
return callExpression!!.valueArguments[index].getArgumentExpression()!!
}
@@ -1,36 +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
class ReplaceSubstringWithTakeIntention : ReplaceSubstringIntention("Replace 'substring' call with 'take' call") {
override fun applicabilityRangeInner(element: KtDotQualifiedExpression): TextRange? {
val arguments = element.callExpression?.valueArguments ?: return null
if (arguments.size == 2 && element.isFirstArgumentZero()) {
return getTextRange(element)
}
return null
}
override fun applyTo(element: KtDotQualifiedExpression, editor: Editor?) {
val argument = element.callExpression!!.valueArguments[1].getArgumentExpression()!!
element.replaceWith("$0.take($1)", argument)
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.substring.ReplaceSubstringWithDropLastInspection
@@ -1,5 +1,5 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// PROBLEM: none
class A() {
fun bar(): String = null!!
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
// WITH_RUNTIME
fun foo(s: String) {
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.substring.ReplaceSubstringWithIndexingOperationInspection
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.substring.ReplaceSubstringWithSubstringAfterInspection
@@ -1,5 +1,5 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// PROBLEM: none
class A() {
fun bar(): String = null!!
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.substring.ReplaceSubstringWithSubstringBeforeInspection
@@ -1,5 +1,5 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// PROBLEM: none
class A() {
fun bar(): String = null!!
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
// WITH_RUNTIME
fun foo(s: String) {
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.substring.ReplaceSubstringWithTakeInspection
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
// WITH_RUNTIME
const val x = 0
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
// WITH_RUNTIME
fun foo(s: String) {
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
// WITH_RUNTIME
fun foo(s: String) {
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithDropLastIntention
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithIndexingOperationIntention
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithSubstringAfterIntention
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithSubstringBeforeIntention
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.ReplaceSubstringWithTakeIntention
@@ -8211,6 +8211,194 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
}
}
@TestMetadata("idea/testData/inspectionsLocal/replaceSubstring")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceSubstring extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInReplaceSubstring() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceSubstring"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("idea/testData/inspectionsLocal/replaceSubstring/withDropLast")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class WithDropLast extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInWithDropLast() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceSubstring/withDropLast"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("immutableProperty.kt")
public void testImmutableProperty() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withDropLast/immutableProperty.kt");
}
@TestMetadata("methodCallReceiver.kt")
public void testMethodCallReceiver() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withDropLast/methodCallReceiver.kt");
}
@TestMetadata("nonZeroFirstArgument.kt")
public void testNonZeroFirstArgument() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withDropLast/nonZeroFirstArgument.kt");
}
@TestMetadata("replaceWithDropLast.kt")
public void testReplaceWithDropLast() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withDropLast/replaceWithDropLast.kt");
}
@TestMetadata("semicolon.kt")
public void testSemicolon() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withDropLast/semicolon.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/replaceSubstring/withIndexingOperation")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class WithIndexingOperation extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInWithIndexingOperation() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceSubstring/withIndexingOperation"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("oneFirstTwoSecondArgument.kt")
public void testOneFirstTwoSecondArgument() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withIndexingOperation/oneFirstTwoSecondArgument.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withIndexingOperation/simple.kt");
}
@TestMetadata("zeroFirstTenSecondArgument.kt")
public void testZeroFirstTenSecondArgument() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withIndexingOperation/zeroFirstTenSecondArgument.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/replaceSubstring/withSubstringAfter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class WithSubstringAfter extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInWithSubstringAfter() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceSubstring/withSubstringAfter"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("immutableProperty.kt")
public void testImmutableProperty() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withSubstringAfter/immutableProperty.kt");
}
@TestMetadata("methodCallReceiver.kt")
public void testMethodCallReceiver() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withSubstringAfter/methodCallReceiver.kt");
}
@TestMetadata("replaceWithSubstringAfter.kt")
public void testReplaceWithSubstringAfter() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withSubstringAfter/replaceWithSubstringAfter.kt");
}
@TestMetadata("semicolon.kt")
public void testSemicolon() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withSubstringAfter/semicolon.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/replaceSubstring/withSubstringBefore")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class WithSubstringBefore extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInWithSubstringBefore() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceSubstring/withSubstringBefore"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("immutableProperty.kt")
public void testImmutableProperty() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withSubstringBefore/immutableProperty.kt");
}
@TestMetadata("methodCallReceiver.kt")
public void testMethodCallReceiver() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withSubstringBefore/methodCallReceiver.kt");
}
@TestMetadata("nonZeroFirstArgument.kt")
public void testNonZeroFirstArgument() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withSubstringBefore/nonZeroFirstArgument.kt");
}
@TestMetadata("replaceWithSubstringBefore.kt")
public void testReplaceWithSubstringBefore() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withSubstringBefore/replaceWithSubstringBefore.kt");
}
@TestMetadata("semicolon.kt")
public void testSemicolon() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withSubstringBefore/semicolon.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/replaceSubstring/withTake")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class WithTake extends AbstractLocalInspectionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInWithTake() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceSubstring/withTake"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("constantAsFirstArgument.kt")
public void testConstantAsFirstArgument() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withTake/constantAsFirstArgument.kt");
}
@TestMetadata("expressionAsFirstArgument.kt")
public void testExpressionAsFirstArgument() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withTake/expressionAsFirstArgument.kt");
}
@TestMetadata("nonZeroFirstArgument.kt")
public void testNonZeroFirstArgument() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withTake/nonZeroFirstArgument.kt");
}
@TestMetadata("replaceWithTake.kt")
public void testReplaceWithTake() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withTake/replaceWithTake.kt");
}
@TestMetadata("semicolon.kt")
public void testSemicolon() throws Exception {
runTest("idea/testData/inspectionsLocal/replaceSubstring/withTake/semicolon.kt");
}
}
}
@TestMetadata("idea/testData/inspectionsLocal/replaceToStringWithStringTemplate")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -15115,181 +15115,6 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/replaceSubstringWithDropLast")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceSubstringWithDropLast extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInReplaceSubstringWithDropLast() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceSubstringWithDropLast"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("immutableProperty.kt")
public void testImmutableProperty() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithDropLast/immutableProperty.kt");
}
@TestMetadata("methodCallReceiver.kt")
public void testMethodCallReceiver() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithDropLast/methodCallReceiver.kt");
}
@TestMetadata("nonZeroFirstArgument.kt")
public void testNonZeroFirstArgument() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithDropLast/nonZeroFirstArgument.kt");
}
@TestMetadata("replaceWithDropLast.kt")
public void testReplaceWithDropLast() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithDropLast/replaceWithDropLast.kt");
}
@TestMetadata("semicolon.kt")
public void testSemicolon() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithDropLast/semicolon.kt");
}
}
@TestMetadata("idea/testData/intentions/replaceSubstringWithIndexingOperation")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceSubstringWithIndexingOperation extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
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 {
runTest("idea/testData/intentions/replaceSubstringWithIndexingOperation/oneFirstTwoSecondArgument.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithIndexingOperation/simple.kt");
}
@TestMetadata("zeroFirstTenSecondArgument.kt")
public void testZeroFirstTenSecondArgument() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithIndexingOperation/zeroFirstTenSecondArgument.kt");
}
}
@TestMetadata("idea/testData/intentions/replaceSubstringWithSubstringAfter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceSubstringWithSubstringAfter extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInReplaceSubstringWithSubstringAfter() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceSubstringWithSubstringAfter"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("immutableProperty.kt")
public void testImmutableProperty() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithSubstringAfter/immutableProperty.kt");
}
@TestMetadata("methodCallReceiver.kt")
public void testMethodCallReceiver() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithSubstringAfter/methodCallReceiver.kt");
}
@TestMetadata("replaceWithSubstringAfter.kt")
public void testReplaceWithSubstringAfter() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithSubstringAfter/replaceWithSubstringAfter.kt");
}
@TestMetadata("semicolon.kt")
public void testSemicolon() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithSubstringAfter/semicolon.kt");
}
}
@TestMetadata("idea/testData/intentions/replaceSubstringWithSubstringBefore")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceSubstringWithSubstringBefore extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInReplaceSubstringWithSubstringBefore() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceSubstringWithSubstringBefore"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("immutableProperty.kt")
public void testImmutableProperty() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithSubstringBefore/immutableProperty.kt");
}
@TestMetadata("methodCallReceiver.kt")
public void testMethodCallReceiver() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithSubstringBefore/methodCallReceiver.kt");
}
@TestMetadata("nonZeroFirstArgument.kt")
public void testNonZeroFirstArgument() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithSubstringBefore/nonZeroFirstArgument.kt");
}
@TestMetadata("replaceWithSubstringBefore.kt")
public void testReplaceWithSubstringBefore() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithSubstringBefore/replaceWithSubstringBefore.kt");
}
@TestMetadata("semicolon.kt")
public void testSemicolon() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithSubstringBefore/semicolon.kt");
}
}
@TestMetadata("idea/testData/intentions/replaceSubstringWithTake")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ReplaceSubstringWithTake extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInReplaceSubstringWithTake() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/replaceSubstringWithTake"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("constantAsFirstArgument.kt")
public void testConstantAsFirstArgument() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithTake/constantAsFirstArgument.kt");
}
@TestMetadata("expressionAsFirstArgument.kt")
public void testExpressionAsFirstArgument() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithTake/expressionAsFirstArgument.kt");
}
@TestMetadata("nonZeroFirstArgument.kt")
public void testNonZeroFirstArgument() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithTake/nonZeroFirstArgument.kt");
}
@TestMetadata("replaceWithTake.kt")
public void testReplaceWithTake() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithTake/replaceWithTake.kt");
}
@TestMetadata("semicolon.kt")
public void testSemicolon() throws Exception {
runTest("idea/testData/intentions/replaceSubstringWithTake/semicolon.kt");
}
}
@TestMetadata("idea/testData/intentions/replaceUnderscoreWithParameterName")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)