Destructure is available even without usages #KT-14435 Fixed

This commit is contained in:
mglukhikh
2017-03-31 15:14:21 +03:00
committed by Mikhail Glukhikh
parent feb9dd4d83
commit d9e1e82948
6 changed files with 36 additions and 3 deletions
@@ -74,9 +74,14 @@ class DestructureIntention : SelfTargetingRangeIntention<KtDeclaration>(
) )
val names = ArrayList<String>() val names = ArrayList<String>()
val underscoreSupported = element.languageVersionSettings.supportsFeature(LanguageFeature.SingleUnderscoreForParameterName) val underscoreSupported = element.languageVersionSettings.supportsFeature(LanguageFeature.SingleUnderscoreForParameterName)
// For all unused we generate normal names, not underscores
val allUnused = usagesToRemove.all { (_, usagesToReplace, variableToDrop) ->
usagesToReplace.isEmpty() && variableToDrop == null
}
usagesToRemove.forEach { (descriptor, usagesToReplace, variableToDrop, name) -> usagesToRemove.forEach { (descriptor, usagesToReplace, variableToDrop, name) ->
val suggestedName = val suggestedName =
if (usagesToReplace.isEmpty() && variableToDrop == null && underscoreSupported) { if (usagesToReplace.isEmpty() && variableToDrop == null && underscoreSupported && !allUnused) {
"_" "_"
} }
else { else {
@@ -233,7 +238,12 @@ class DestructureIntention : SelfTargetingRangeIntention<KtDeclaration>(
if (!noBadUsages) return null if (!noBadUsages) return null
val droppedLastUnused = usagesToRemove.dropLastWhile { it.usagesToReplace.isEmpty() && it.declarationToDrop == null } val droppedLastUnused = usagesToRemove.dropLastWhile { it.usagesToReplace.isEmpty() && it.declarationToDrop == null }
return UsagesToRemove(droppedLastUnused, removeSelectorInLoopRange) return if (droppedLastUnused.isEmpty()) {
UsagesToRemove(usagesToRemove, removeSelectorInLoopRange)
}
else {
UsagesToRemove(droppedLastUnused, removeSelectorInLoopRange)
}
} }
private fun Query<PsiReference>.iterateOverMapEntryPropertiesUsages( private fun Query<PsiReference>.iterateOverMapEntryPropertiesUsages(
@@ -1,4 +1,3 @@
// IS_APPLICABLE: false
// WITH_RUNTIME // WITH_RUNTIME
data class XY(val x: String, val y: String) data class XY(val x: String, val y: String)
@@ -0,0 +1,6 @@
// WITH_RUNTIME
data class XY(val x: String, val y: String)
fun test(xys: Array<XY>) {
for ((x, y) in xys) {}
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun main(args: Array<String>) {
val map = hashMapOf(1 to 1)
for (<caret>entry in map) {}
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
fun main(args: Array<String>) {
val map = hashMapOf(1 to 1)
for ((key, value) in map) {}
}
@@ -9170,6 +9170,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("NoKeyOrValue.kt")
public void testNoKeyOrValue() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/iterationOverMap/NoKeyOrValue.kt");
doTest(fileName);
}
@TestMetadata("OnlyKeyUsed.kt") @TestMetadata("OnlyKeyUsed.kt")
public void testOnlyKeyUsed() throws Exception { public void testOnlyKeyUsed() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/iterationOverMap/OnlyKeyUsed.kt"); String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/iterationOverMap/OnlyKeyUsed.kt");