Extract 'isToString()' function to Utils

This commit is contained in:
Toshiaki Kameyama
2018-11-08 16:20:23 +09:00
committed by Vyacheslav Gerasimov
parent a0c25c7a87
commit 825d1d8b35
3 changed files with 29 additions and 39 deletions
@@ -8,36 +8,28 @@ package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.FileModificationService
import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.idea.core.getDeepestSuperDeclarations
import org.jetbrains.kotlin.idea.intentions.toResolvedCall
import org.jetbrains.kotlin.idea.intentions.isToString
import org.jetbrains.kotlin.psi.*
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 {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession) =
dotQualifiedExpressionVisitor(fun(expression) {
if (expression.parent !is KtBlockStringTemplateEntry) return
if (expression.receiverExpression is KtSuperExpression) return
val selectorExpression = expression.selectorExpression ?: return
if (!expression.isToString()) return
dotQualifiedExpressionVisitor(fun(expression) {
if (expression.parent !is KtBlockStringTemplateEntry) return
if (expression.receiverExpression is KtSuperExpression) return
val selectorExpression = expression.selectorExpression ?: return
if (!expression.isToString()) return
holder.registerProblem(selectorExpression,
"Redundant 'toString()' call in string template",
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
RemoveToStringFix())
})
holder.registerProblem(
selectorExpression,
"Redundant 'toString()' call in string template",
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
RemoveToStringFix()
)
})
}
private fun KtDotQualifiedExpression.isToString(): Boolean {
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 {
class RemoveToStringFix : LocalQuickFix {
override fun getName() = "Remove 'toString()' call"
override fun getFamilyName() = name
@@ -8,14 +8,12 @@ package org.jetbrains.kotlin.idea.inspections
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
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.idea.core.getDeepestSuperDeclarations
import org.jetbrains.kotlin.idea.intentions.toResolvedCall
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtReferenceExpression
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>(
KtDotQualifiedExpression::class.java
@@ -37,13 +35,4 @@ class ReplaceToStringWithStringTemplateInspection : AbstractApplicabilityBasedIn
override fun inspectionTarget(element: KtDotQualifiedExpression) = element
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.idea.caches.resolve.analyze
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.setType
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.isFlexible
import org.jetbrains.kotlin.types.typeUtil.isUnit
import java.lang.IllegalArgumentException
fun KtContainerNode.description(): String? {
when (node.elementType) {
@@ -320,4 +320,13 @@ fun KtDeclaration.isFinalizeMethod(descriptor: DeclarationDescriptor? = null): B
return function.name == "finalize"
&& function.valueParameters.isEmpty()
&& ((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" }
}