Extract 'isToString()' function to Utils
This commit is contained in:
committed by
Vyacheslav Gerasimov
parent
a0c25c7a87
commit
825d1d8b35
+14
-22
@@ -8,36 +8,28 @@ package org.jetbrains.kotlin.idea.inspections
|
|||||||
import com.intellij.codeInsight.FileModificationService
|
import com.intellij.codeInsight.FileModificationService
|
||||||
import com.intellij.codeInspection.*
|
import com.intellij.codeInspection.*
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
import org.jetbrains.kotlin.idea.intentions.isToString
|
||||||
import org.jetbrains.kotlin.idea.core.getDeepestSuperDeclarations
|
|
||||||
import org.jetbrains.kotlin.idea.intentions.toResolvedCall
|
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.canPlaceAfterSimpleNameEntry
|
import org.jetbrains.kotlin.psi.psiUtil.canPlaceAfterSimpleNameEntry
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
|
||||||
|
|
||||||
class RemoveToStringInStringTemplateInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
|
class RemoveToStringInStringTemplateInspection : AbstractKotlinInspection(), CleanupLocalInspectionTool {
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession) =
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession) =
|
||||||
dotQualifiedExpressionVisitor(fun(expression) {
|
dotQualifiedExpressionVisitor(fun(expression) {
|
||||||
if (expression.parent !is KtBlockStringTemplateEntry) return
|
if (expression.parent !is KtBlockStringTemplateEntry) return
|
||||||
if (expression.receiverExpression is KtSuperExpression) return
|
if (expression.receiverExpression is KtSuperExpression) return
|
||||||
val selectorExpression = expression.selectorExpression ?: return
|
val selectorExpression = expression.selectorExpression ?: return
|
||||||
if (!expression.isToString()) return
|
if (!expression.isToString()) return
|
||||||
|
|
||||||
holder.registerProblem(selectorExpression,
|
holder.registerProblem(
|
||||||
"Redundant 'toString()' call in string template",
|
selectorExpression,
|
||||||
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
|
"Redundant 'toString()' call in string template",
|
||||||
RemoveToStringFix())
|
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
|
||||||
})
|
RemoveToStringFix()
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun KtDotQualifiedExpression.isToString(): Boolean {
|
class RemoveToStringFix : LocalQuickFix {
|
||||||
val resolvedCall = toResolvedCall(BodyResolveMode.PARTIAL) ?: return false
|
|
||||||
val callableDescriptor = resolvedCall.resultingDescriptor as? CallableMemberDescriptor ?: return false
|
|
||||||
return callableDescriptor.getDeepestSuperDeclarations().any { it.fqNameUnsafe.asString() == "kotlin.Any.toString" }
|
|
||||||
}
|
|
||||||
|
|
||||||
class RemoveToStringFix: LocalQuickFix {
|
|
||||||
override fun getName() = "Remove 'toString()' call"
|
override fun getName() = "Remove 'toString()' call"
|
||||||
override fun getFamilyName() = name
|
override fun getFamilyName() = name
|
||||||
|
|
||||||
|
|||||||
+4
-15
@@ -8,14 +8,12 @@ package org.jetbrains.kotlin.idea.inspections
|
|||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
|
import org.jetbrains.kotlin.idea.intentions.isToString
|
||||||
|
import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry
|
||||||
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
|
||||||
import org.jetbrains.kotlin.idea.core.getDeepestSuperDeclarations
|
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||||
import org.jetbrains.kotlin.idea.intentions.toResolvedCall
|
import org.jetbrains.kotlin.psi.KtReferenceExpression
|
||||||
import org.jetbrains.kotlin.psi.*
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
|
||||||
|
|
||||||
class ReplaceToStringWithStringTemplateInspection : AbstractApplicabilityBasedInspection<KtDotQualifiedExpression>(
|
class ReplaceToStringWithStringTemplateInspection : AbstractApplicabilityBasedInspection<KtDotQualifiedExpression>(
|
||||||
KtDotQualifiedExpression::class.java
|
KtDotQualifiedExpression::class.java
|
||||||
@@ -37,13 +35,4 @@ class ReplaceToStringWithStringTemplateInspection : AbstractApplicabilityBasedIn
|
|||||||
override fun inspectionTarget(element: KtDotQualifiedExpression) = element
|
override fun inspectionTarget(element: KtDotQualifiedExpression) = element
|
||||||
|
|
||||||
override val defaultFixText = "Replace 'toString' with string template"
|
override val defaultFixText = "Replace 'toString' with string template"
|
||||||
|
|
||||||
private fun KtDotQualifiedExpression.isToString(): Boolean {
|
|
||||||
val callExpression = selectorExpression as? KtCallExpression ?: return false
|
|
||||||
val referenceExpression = callExpression.calleeExpression as? KtNameReferenceExpression ?: return false
|
|
||||||
if (referenceExpression.getReferencedName() != "toString") return false
|
|
||||||
val resolvedCall = toResolvedCall(BodyResolveMode.PARTIAL) ?: return false
|
|
||||||
val callableDescriptor = resolvedCall.resultingDescriptor as? CallableMemberDescriptor ?: return false
|
|
||||||
return callableDescriptor.getDeepestSuperDeclarations().any { it.fqNameUnsafe.asString() == "kotlin.Any.toString" }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
|||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
|
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
|
||||||
|
import org.jetbrains.kotlin.idea.core.getDeepestSuperDeclarations
|
||||||
import org.jetbrains.kotlin.idea.core.replaced
|
import org.jetbrains.kotlin.idea.core.replaced
|
||||||
import org.jetbrains.kotlin.idea.core.setType
|
import org.jetbrains.kotlin.idea.core.setType
|
||||||
import org.jetbrains.kotlin.idea.references.mainReference
|
import org.jetbrains.kotlin.idea.references.mainReference
|
||||||
@@ -41,7 +42,6 @@ import org.jetbrains.kotlin.resolve.source.getPsi
|
|||||||
import org.jetbrains.kotlin.types.KotlinType
|
import org.jetbrains.kotlin.types.KotlinType
|
||||||
import org.jetbrains.kotlin.types.isFlexible
|
import org.jetbrains.kotlin.types.isFlexible
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
import org.jetbrains.kotlin.types.typeUtil.isUnit
|
||||||
import java.lang.IllegalArgumentException
|
|
||||||
|
|
||||||
fun KtContainerNode.description(): String? {
|
fun KtContainerNode.description(): String? {
|
||||||
when (node.elementType) {
|
when (node.elementType) {
|
||||||
@@ -320,4 +320,13 @@ fun KtDeclaration.isFinalizeMethod(descriptor: DeclarationDescriptor? = null): B
|
|||||||
return function.name == "finalize"
|
return function.name == "finalize"
|
||||||
&& function.valueParameters.isEmpty()
|
&& function.valueParameters.isEmpty()
|
||||||
&& ((descriptor ?: function.descriptor) as? FunctionDescriptor)?.returnType?.isUnit() == true
|
&& ((descriptor ?: function.descriptor) as? FunctionDescriptor)?.returnType?.isUnit() == true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun KtDotQualifiedExpression.isToString(): Boolean {
|
||||||
|
val callExpression = selectorExpression as? KtCallExpression ?: return false
|
||||||
|
val referenceExpression = callExpression.calleeExpression as? KtNameReferenceExpression ?: return false
|
||||||
|
if (referenceExpression.getReferencedName() != "toString") return false
|
||||||
|
val resolvedCall = toResolvedCall(BodyResolveMode.PARTIAL) ?: return false
|
||||||
|
val callableDescriptor = resolvedCall.resultingDescriptor as? CallableMemberDescriptor ?: return false
|
||||||
|
return callableDescriptor.getDeepestSuperDeclarations().any { it.fqNameUnsafe.asString() == "kotlin.Any.toString" }
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user