From 8a02ce3dc2e82f4e98554f06c41a0f4cb594f7ad Mon Sep 17 00:00:00 2001 From: mglukhikh Date: Fri, 31 Mar 2017 13:01:17 +0300 Subject: [PATCH] KT-16828: use _ when appropriate in destructure intention --- .../kotlin/idea/intentions/DestructureIntention.kt | 9 ++++++++- .../destructuringInLambda/inspectionData/expected.xml | 8 ++++++++ .../intentions/destructuringInLambda/last.kt.after | 2 +- .../intentions/destructuringInLambda/mapIndexedLast.kt | 5 +++++ .../destructuringInLambda/mapIndexedLast.kt.after | 5 +++++ .../intentions/iterationOverMap/DataClassLast.kt.after | 2 +- .../DataClassNotAllPropertiesUsed.kt.after | 2 +- .../intentions/iterationOverMap/ValueOnly.kt.after | 2 +- .../kotlin/idea/intentions/IntentionTestGenerated.java | 6 ++++++ 9 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 idea/testData/intentions/destructuringInLambda/mapIndexedLast.kt create mode 100644 idea/testData/intentions/destructuringInLambda/mapIndexedLast.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 ee15047e1b8..67cf99ebba3 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/DestructureIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/DestructureIntention.kt @@ -68,8 +68,15 @@ class DestructureIntention : SelfTargetingRangeIntention( excludedDeclarations = usagesToRemove.map { listOfNotNull(it.declarationToDrop) }.flatten() ) val names = ArrayList() + val underscoreSupported = element.languageVersionSettings.supportsFeature(LanguageFeature.SingleUnderscoreForParameterName) usagesToRemove.forEach { (descriptor, usagesToReplace, variableToDrop, name) -> - val suggestedName = name ?: KotlinNameSuggester.suggestNameByName(descriptor.name.asString(), validator) + val suggestedName = + if (usagesToReplace.isEmpty() && variableToDrop == null && underscoreSupported) { + "_" + } + else { + name ?: KotlinNameSuggester.suggestNameByName(descriptor.name.asString(), validator) + } variableToDrop?.delete() usagesToReplace.forEach { it.replace(factory.createExpression(suggestedName)) diff --git a/idea/testData/intentions/destructuringInLambda/inspectionData/expected.xml b/idea/testData/intentions/destructuringInLambda/inspectionData/expected.xml index 67ff648fbcb..2b9d0973a28 100644 --- a/idea/testData/intentions/destructuringInLambda/inspectionData/expected.xml +++ b/idea/testData/intentions/destructuringInLambda/inspectionData/expected.xml @@ -63,4 +63,12 @@ Use destructuring declaration Use destructuring declaration + + mapIndexedLast.kt + 5 + light_idea_test_case + + Use destructuring declaration + Use destructuring declaration + \ No newline at end of file diff --git a/idea/testData/intentions/destructuringInLambda/last.kt.after b/idea/testData/intentions/destructuringInLambda/last.kt.after index 718e1b854be..14359ca20e1 100644 --- a/idea/testData/intentions/destructuringInLambda/last.kt.after +++ b/idea/testData/intentions/destructuringInLambda/last.kt.after @@ -3,7 +3,7 @@ data class My(val first: String, val second: Int) fun foo(list: List) { - list.forEach { (first, second) -> + list.forEach { (_, second) -> println(second) } } \ No newline at end of file diff --git a/idea/testData/intentions/destructuringInLambda/mapIndexedLast.kt b/idea/testData/intentions/destructuringInLambda/mapIndexedLast.kt new file mode 100644 index 00000000000..5dbc28952e7 --- /dev/null +++ b/idea/testData/intentions/destructuringInLambda/mapIndexedLast.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +data class Package(val name: String, val version: String, val source: String, val id: String) + +val pkgs = listOf().mapIndexed { i, p -> p.id to i }.toMap() \ No newline at end of file diff --git a/idea/testData/intentions/destructuringInLambda/mapIndexedLast.kt.after b/idea/testData/intentions/destructuringInLambda/mapIndexedLast.kt.after new file mode 100644 index 00000000000..83dd45e85cd --- /dev/null +++ b/idea/testData/intentions/destructuringInLambda/mapIndexedLast.kt.after @@ -0,0 +1,5 @@ +// WITH_RUNTIME + +data class Package(val name: String, val version: String, val source: String, val id: String) + +val pkgs = listOf().mapIndexed { i, (_, _, _, id) -> id to i }.toMap() \ No newline at end of file diff --git a/idea/testData/intentions/iterationOverMap/DataClassLast.kt.after b/idea/testData/intentions/iterationOverMap/DataClassLast.kt.after index 8762aec5d14..31e076e9fb1 100644 --- a/idea/testData/intentions/iterationOverMap/DataClassLast.kt.after +++ b/idea/testData/intentions/iterationOverMap/DataClassLast.kt.after @@ -3,7 +3,7 @@ data class My(val first: String, val second: Int) fun foo(list: List) { - for ((first, second) in list) { + for ((_, second) in list) { println(second) } } \ No newline at end of file diff --git a/idea/testData/intentions/iterationOverMap/DataClassNotAllPropertiesUsed.kt.after b/idea/testData/intentions/iterationOverMap/DataClassNotAllPropertiesUsed.kt.after index 77dd84aff07..996f048669f 100644 --- a/idea/testData/intentions/iterationOverMap/DataClassNotAllPropertiesUsed.kt.after +++ b/idea/testData/intentions/iterationOverMap/DataClassNotAllPropertiesUsed.kt.after @@ -2,7 +2,7 @@ fun main(args: Array) { val list = listOf(MyClass(1, 2, 3, 4)) - for ((a, b, c) in list) { + for ((a, _, c) in list) { println("$a$c") } } diff --git a/idea/testData/intentions/iterationOverMap/ValueOnly.kt.after b/idea/testData/intentions/iterationOverMap/ValueOnly.kt.after index ef549c73099..78f3a424287 100644 --- a/idea/testData/intentions/iterationOverMap/ValueOnly.kt.after +++ b/idea/testData/intentions/iterationOverMap/ValueOnly.kt.after @@ -2,7 +2,7 @@ fun main(args: Array) { val map = hashMapOf(1 to 1) - for ((key, value) in map) { + for ((_, value) in map) { println(value) } } \ 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 d23a75da03b..ceee5a951b1 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -7645,6 +7645,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("mapIndexedLast.kt") + public void testMapIndexedLast() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/destructuringInLambda/mapIndexedLast.kt"); + doTest(fileName); + } + @TestMetadata("noIt.kt") public void testNoIt() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/destructuringInLambda/noIt.kt");