From f13997750f958eb13c3d3515eb990dd7abcf10a3 Mon Sep 17 00:00:00 2001 From: mglukhikh Date: Fri, 31 Mar 2017 13:53:25 +0300 Subject: [PATCH] Make destructure intention work on library data classes So #KT-16468 Fixed So #KT-14402 Fixed --- .../idea/intentions/DestructureIntention.kt | 23 +++++++++---------- .../intentions/destructuringInLambda/pair.kt | 8 +++++++ .../destructuringInLambda/pair.kt.after | 8 +++++++ .../intentions/IntentionTestGenerated.java | 6 +++++ 4 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 idea/testData/intentions/destructuringInLambda/pair.kt create mode 100644 idea/testData/intentions/destructuringInLambda/pair.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/DestructureIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/DestructureIntention.kt index 67cf99ebba3..07c9e98cb7a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/DestructureIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/DestructureIntention.kt @@ -26,6 +26,7 @@ import com.intellij.util.Query import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.core.KotlinNameSuggester import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator @@ -211,15 +212,13 @@ class DestructureIntention : SelfTargetingRangeIntention( else -> Name.identifier("it") } ?: return null - val descriptorToIndex = mutableMapOf() - for (valueParameter in valueParameters) { - val propertyDescriptor = context.get(BindingContext.VALUE_PARAMETER_AS_PROPERTY, valueParameter) ?: continue - descriptorToIndex[propertyDescriptor] = valueParameter.index - } + val constructorParameterNameMap = mutableMapOf() + valueParameters.forEach { constructorParameterNameMap[it.name] = it } + usageScopeElement.iterateOverDataClassPropertiesUsagesWithIndex( context, nameToSearch, - descriptorToIndex, + constructorParameterNameMap, { index, usageData -> noBadUsages = usagesToRemove[index].add(usageData, index) && noBadUsages }, { noBadUsages = false } ) @@ -268,7 +267,7 @@ class DestructureIntention : SelfTargetingRangeIntention( private fun PsiElement.iterateOverDataClassPropertiesUsagesWithIndex( context: BindingContext, parameterName: Name, - descriptorToIndex: Map, + constructorParameterNameMap: Map, process: (Int, SingleUsageData) -> Unit, cancel: () -> Unit ) { @@ -279,14 +278,14 @@ class DestructureIntention : SelfTargetingRangeIntention( if (applicableUsage != null) { val usageDescriptor = applicableUsage.descriptor if (usageDescriptor == null) { - for (index in descriptorToIndex.values) { - process(index, applicableUsage) + for (parameter in constructorParameterNameMap.values) { + process(parameter.index, applicableUsage) } return@anyDescendantOfType false } - val index = descriptorToIndex[usageDescriptor] - if (index != null) { - process(index, applicableUsage) + val parameter = constructorParameterNameMap[usageDescriptor.name] + if (parameter != null) { + process(parameter.index, applicableUsage) return@anyDescendantOfType false } } diff --git a/idea/testData/intentions/destructuringInLambda/pair.kt b/idea/testData/intentions/destructuringInLambda/pair.kt new file mode 100644 index 00000000000..beea9399461 --- /dev/null +++ b/idea/testData/intentions/destructuringInLambda/pair.kt @@ -0,0 +1,8 @@ +// WITH_RUNTIME + +fun foo() = 0 to 10 + +fun bar(): Int { + val b = foo() + return b.first + b.second +} \ No newline at end of file diff --git a/idea/testData/intentions/destructuringInLambda/pair.kt.after b/idea/testData/intentions/destructuringInLambda/pair.kt.after new file mode 100644 index 00000000000..f48c07af310 --- /dev/null +++ b/idea/testData/intentions/destructuringInLambda/pair.kt.after @@ -0,0 +1,8 @@ +// WITH_RUNTIME + +fun foo() = 0 to 10 + +fun bar(): Int { + val (first, second) = foo() + return first + second +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index ceee5a951b1..2bc875550bc 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -7675,6 +7675,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("pair.kt") + public void testPair() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/destructuringInLambda/pair.kt"); + doTest(fileName); + } + @TestMetadata("simple.kt") public void testSimple() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/destructuringInLambda/simple.kt");