Add intention to add missing components to destructuring assignment #KT-16258 Fixed

This commit is contained in:
Kirill Rakhman
2017-02-17 17:09:41 +01:00
committed by Mikhail Glukhikh
parent 4d47c0fd63
commit 8d425a6f94
14 changed files with 157 additions and 0 deletions
@@ -0,0 +1,5 @@
data class Foo(val a: String, val b: String, val c: String)
fun bar(f: Foo) {
val (a, b<spot>, c</spot>) = f
}
@@ -0,0 +1,5 @@
data class Foo(val a: String, val b: String, val c: String)
fun bar(f: Foo) {
val (a, b) = f
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention adds missing components in a destructuring declaration.
</body>
</html>
+5
View File
@@ -1507,6 +1507,11 @@
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.AddMissingDestructuringIntention</className>
<category>Kotlin</category>
</intentionAction>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
displayName="Object literal can be converted to lambda"
groupName="Kotlin"
@@ -0,0 +1,67 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.CollectingNameValidator
import org.jetbrains.kotlin.idea.core.KotlinNameSuggester
import org.jetbrains.kotlin.idea.core.NewDeclarationNameValidator
import org.jetbrains.kotlin.psi.KtDestructuringDeclaration
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.createDestructuringDeclarationByPattern
class AddMissingDestructuringIntention : SelfTargetingIntention<KtDestructuringDeclaration>(KtDestructuringDeclaration::class.java, "Add missing component") {
override fun isApplicableTo(element: KtDestructuringDeclaration, caretOffset: Int): Boolean {
val entriesCount = element.entries.size
val classDescriptor = element.classDescriptor() ?: return false
if (!classDescriptor.isData) return false
val primaryParameters = classDescriptor.primaryParameters() ?: return false
return primaryParameters.size > entriesCount
}
override fun applyTo(element: KtDestructuringDeclaration, editor: Editor?) {
val entries = element.entries
val primaryParameters = element.classDescriptor()?.primaryParameters() ?: return
val factory = KtPsiFactory(element)
val nameValidator = CollectingNameValidator(
filter = NewDeclarationNameValidator(element.parent.parent, null, NewDeclarationNameValidator.Target.VARIABLES)
)
val newEntries = entries.joinToString(postfix = ", ") { it.text } +
primaryParameters.drop(entries.size).joinToString {
KotlinNameSuggester.suggestNameByName(it.name.asString(), nameValidator)
}
val initializer = element.initializer ?: return
val newDestructuringDeclaration = factory.createDestructuringDeclarationByPattern(
if (element.isVar) "var ($0) = $1" else "val ($0) = $1",
newEntries, initializer)
element.replace(newDestructuringDeclaration)
}
private fun KtDestructuringDeclaration.classDescriptor(): ClassDescriptor? {
val type = initializer?.let { it.analyze().getType(it) } ?: return null
return type.constructor.declarationDescriptor as? ClassDescriptor ?: return null
}
private fun ClassDescriptor.primaryParameters() = constructors.firstOrNull { it.isPrimary }?.valueParameters
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.AddMissingDestructuringIntention
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
data class Foo(val a: String, val b: String, val c: String)
fun bar(f: Foo) {
val (a, b, c<caret>) = f
}
@@ -0,0 +1,5 @@
data class Foo(val a: String, val b: String, val c: String)
fun bar(f: Foo) {
val (a, c<caret>) = f
}
@@ -0,0 +1,5 @@
data class Foo(val a: String, val b: String, val c: String)
fun bar(f: Foo) {
val (a, c<caret>, c1) = f
}
@@ -0,0 +1,5 @@
data class Foo(val a: String, val b: String, val c: String)
fun bar(f: Foo) {
val (a, b<caret>) = f
}
@@ -0,0 +1,5 @@
data class Foo(val a: String, val b: String, val c: String)
fun bar(f: Foo) {
val (a, b<caret>, c) = f
}
@@ -0,0 +1,5 @@
data class Foo(val a: String, val b: String, val c: String)
fun bar(f: Foo) {
var (a, b<caret>) = f
}
@@ -0,0 +1,5 @@
data class Foo(val a: String, val b: String, val c: String)
fun bar(f: Foo) {
var (a, b<caret>, c) = f
}
@@ -288,6 +288,39 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/addMissingDestructuring")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class AddMissingDestructuring extends AbstractIntentionTest {
public void testAllFilesPresentInAddMissingDestructuring() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/addMissingDestructuring"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("notAvailable.kt")
public void testNotAvailable() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addMissingDestructuring/notAvailable.kt");
doTest(fileName);
}
@TestMetadata("sameName.kt")
public void testSameName() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addMissingDestructuring/sameName.kt");
doTest(fileName);
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addMissingDestructuring/simple.kt");
doTest(fileName);
}
@TestMetadata("var.kt")
public void testVar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/addMissingDestructuring/var.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/addNameToArgument")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)