KT-13113: Add inspection to detect single-expression string template

(cherry picked from squashed commits 49a3ef3 and 4c7a42a)
This commit is contained in:
shiraji
2016-07-29 06:54:36 +03:00
committed by Mikhail Glukhikh
parent a9ee10a8b7
commit 9b8c55d823
18 changed files with 170 additions and 0 deletions
+13
View File
@@ -1273,6 +1273,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.RemoveSingleExpressionStringTemplateIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupName="Kotlin"
@@ -1644,6 +1649,14 @@
language="kotlin"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.RemoveSingleExpressionStringTemplateInspection"
displayName="Remove redundant string template"
groupName="Kotlin"
enabledByDefault="true"
level="WARNING"
language="kotlin"
/>
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
@@ -0,0 +1,53 @@
/*
* 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 org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
private fun KtStringTemplateExpression.singleExpressionOrNull() =
children.singleOrNull()?.children?.firstOrNull() as? KtExpression
class RemoveSingleExpressionStringTemplateInspection : IntentionBasedInspection<KtStringTemplateExpression>(
RemoveSingleExpressionStringTemplateIntention()
) {
override val problemText = "Single-expression string template"
}
class RemoveSingleExpressionStringTemplateIntention : SelfTargetingOffsetIndependentIntention<KtStringTemplateExpression>(
KtStringTemplateExpression::class.java,
"Remove single-expression string template"
) {
override fun isApplicableTo(element: KtStringTemplateExpression) =
element.singleExpressionOrNull() != null
override fun applyTo(element: KtStringTemplateExpression, editor: Editor?) {
val expression = element.singleExpressionOrNull() ?: return
val type = expression.getType(expression.analyze())
val newElement =
if (KotlinBuiltIns.isString(type)) expression
else KtPsiFactory(element).createExpressionByPattern("$0.$1()", expression, "toString")
element.replace(newElement)
}
}