Implicit this inspection: minor cleanup
This commit is contained in:
@@ -89,7 +89,7 @@ fun LexicalScope.getImplicitReceiversWithInstanceToExpression(): Map<ReceiverPar
|
|||||||
val factory = if (expressionText != null)
|
val factory = if (expressionText != null)
|
||||||
object : ReceiverExpressionFactory {
|
object : ReceiverExpressionFactory {
|
||||||
override val isImmediate = isImmediateThis
|
override val isImmediate = isImmediateThis
|
||||||
override val expressionText get() = expressionText!!
|
override val expressionText: String get() = expressionText
|
||||||
override fun createExpression(psiFactory: KtPsiFactory, shortThis: Boolean): KtExpression {
|
override fun createExpression(psiFactory: KtPsiFactory, shortThis: Boolean): KtExpression {
|
||||||
return psiFactory.createExpression(if (shortThis && isImmediateThis) "this" else expressionText)
|
return psiFactory.createExpression(if (shortThis && isImmediateThis) "this" else expressionText)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,25 +26,20 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
|
|||||||
import org.jetbrains.kotlin.idea.util.getFactoryForImplicitReceiverWithSubtypeOf
|
import org.jetbrains.kotlin.idea.util.getFactoryForImplicitReceiverWithSubtypeOf
|
||||||
import org.jetbrains.kotlin.idea.util.getResolutionScope
|
import org.jetbrains.kotlin.idea.util.getResolutionScope
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
|
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
|
|
||||||
class ImplicitThisInspection : AbstractKotlinInspection() {
|
class ImplicitThisInspection : AbstractKotlinInspection() {
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) = object : KtVisitorVoid() {
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) = object : KtVisitorVoid() {
|
||||||
override fun visitExpression(expression: KtExpression) {
|
override fun visitSimpleNameExpression(expression: KtSimpleNameExpression) {
|
||||||
if (expression !is KtNameReferenceExpression) return
|
if (expression !is KtNameReferenceExpression) return
|
||||||
if (expression.isSelectorOfDotQualifiedExpression()) return
|
if (expression.isSelectorOfDotQualifiedExpression()) return
|
||||||
val parent = expression.parent
|
val parent = expression.parent
|
||||||
if (parent is KtCallExpression && parent.isSelectorOfDotQualifiedExpression()) return
|
if (parent is KtCallExpression && parent.isSelectorOfDotQualifiedExpression()) return
|
||||||
|
|
||||||
val referenceExpression = expression as? KtNameReferenceExpression
|
|
||||||
?: expression.getChildOfType()
|
|
||||||
?: return
|
|
||||||
|
|
||||||
val context = expression.analyzeFully()
|
val context = expression.analyzeFully()
|
||||||
val scope = expression.getResolutionScope(context) ?: return
|
val scope = expression.getResolutionScope(context) ?: return
|
||||||
|
|
||||||
val descriptor = context[BindingContext.REFERENCE_TARGET, referenceExpression] as? CallableDescriptor ?: return
|
val descriptor = context[BindingContext.REFERENCE_TARGET, expression] as? CallableDescriptor ?: return
|
||||||
val receiverDescriptor = descriptor.extensionReceiverParameter ?: descriptor.dispatchReceiverParameter ?: return
|
val receiverDescriptor = descriptor.extensionReceiverParameter ?: descriptor.dispatchReceiverParameter ?: return
|
||||||
val receiverType = receiverDescriptor.type
|
val receiverType = receiverDescriptor.type
|
||||||
|
|
||||||
@@ -68,7 +63,7 @@ class ImplicitThisInspection : AbstractKotlinInspection() {
|
|||||||
val factory = KtPsiFactory(project)
|
val factory = KtPsiFactory(project)
|
||||||
val call = expression.parent as? KtCallExpression ?: expression
|
val call = expression.parent as? KtCallExpression ?: expression
|
||||||
|
|
||||||
call.replace(factory.createExpressionByPattern("$0.$1", receiverText, call.text))
|
call.replace(factory.createExpressionByPattern("$0.$1", receiverText, call))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user