From 0d27e7a259b944935a2fd4a9727195ad19fc2a32 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 12 Aug 2015 15:44:15 +0300 Subject: [PATCH] Faster replacement of get/set to synthetic extensions in converter - don't search for extension property again --- .../UsePropertyAccessSyntaxIntention.kt | 16 +++++++--------- .../kotlin/idea/j2k/J2kPostProcessings.kt | 12 +++++++++++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/UsePropertyAccessSyntaxIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/UsePropertyAccessSyntaxIntention.kt index b51dd074be5..612148bd5b3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/UsePropertyAccessSyntaxIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/UsePropertyAccessSyntaxIntention.kt @@ -20,7 +20,6 @@ import com.intellij.openapi.editor.Editor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility import org.jetbrains.kotlin.descriptors.FunctionDescriptor -import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade import org.jetbrains.kotlin.idea.codeInsight.ReferenceVariantsHelper @@ -41,17 +40,14 @@ class UsePropertyAccessSyntaxInspection : IntentionBasedInspection(javaClass(), "Use property access syntax") { override fun isApplicableTo(element: JetCallExpression): Boolean { - if (element.getQualifiedExpressionForSelector()?.getReceiverExpression() is JetSuperExpression) return false // cannot call extensions on "super" - - return findExtensionPropertyToUse(element) != null + return detectPropertyNameToUse(element) != null } override fun applyTo(element: JetCallExpression, editor: Editor) { - applyTo(element) + applyTo(element, detectPropertyNameToUse(element)!!) } - public fun applyTo(element: JetCallExpression) { - val propertyName = findExtensionPropertyToUse(element)!!.getName() + public fun applyTo(element: JetCallExpression, propertyName: Name) { val arguments = element.getValueArguments() when (arguments.size()) { 0 -> replaceWithPropertyGet(element, propertyName) @@ -60,7 +56,9 @@ class UsePropertyAccessSyntaxIntention : JetSelfTargetingOffsetIndependentIntent } } - private fun findExtensionPropertyToUse(callExpression: JetCallExpression): PropertyDescriptor? { + public fun detectPropertyNameToUse(callExpression: JetCallExpression): Name? { + if (callExpression.getQualifiedExpressionForSelector()?.getReceiverExpression() is JetSuperExpression) return null // cannot call extensions on "super" + val callee = callExpression.getCalleeExpression() as? JetSimpleNameExpression ?: return null val bindingContext = callExpression.analyze(BodyResolveMode.PARTIAL) @@ -87,7 +85,7 @@ class UsePropertyAccessSyntaxIntention : JetSelfTargetingOffsetIndependentIntent .map { it.original } if (property !in accessibleVariables) return null // shadowed by something else - return property + return property.name } private fun findSyntheticProperty(function: FunctionDescriptor, resolutionScope: JetScope): SyntheticJavaPropertyDescriptor? { diff --git a/idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessings.kt b/idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessings.kt index cf597a55b8d..ab6c3028f2b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessings.kt +++ b/idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessings.kt @@ -45,8 +45,8 @@ object J2KPostProcessingRegistrar { _processings.add(RemoveExplicitTypeArgumentsProcessing()) _processings.add(MoveLambdaOutsideParenthesesProcessing()) _processings.add(ConvertToStringTemplateProcessing()) + _processings.add(UsePropertyAccessSyntaxProcessing()) - registerIntentionBasedProcessing(UsePropertyAccessSyntaxIntention()) { applyTo(it) } registerIntentionBasedProcessing(IfThenToSafeAccessIntention()) { applyTo(it) } registerIntentionBasedProcessing(IfThenToElvisIntention()) { applyTo(it) } registerIntentionBasedProcessing(IfNullToElvisIntention()) { applyTo(it) } @@ -143,4 +143,14 @@ object J2KPostProcessingRegistrar { } } } + + private class UsePropertyAccessSyntaxProcessing : J2kPostProcessing { + private val intention = UsePropertyAccessSyntaxIntention() + + override fun createAction(element: JetElement, diagnostics: Diagnostics): (() -> Unit)? { + if (element !is JetCallExpression) return null + val propertyName = intention.detectPropertyNameToUse(element) ?: return null + return { intention.applyTo(element, propertyName) } + } + } } \ No newline at end of file