Introduce "Convert property getter to initializer" intention

#KT-13854 Fixed
This commit is contained in:
Toshiaki Kameyama
2018-08-03 18:04:15 +03:00
committed by Mikhail Glukhikh
parent 6fbf6b0a93
commit 1898df3fb7
34 changed files with 223 additions and 0 deletions
@@ -0,0 +1,3 @@
class A {
val foo: Int = <spot>1</spot>
}
@@ -0,0 +1,4 @@
class A {
val foo: Int
<spot>get() = 1</spot>
}
@@ -0,0 +1,5 @@
<html>
<body>
This intention converts a property getter to to a initializer.
</body>
</html>
+5
View File
@@ -1676,6 +1676,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertPropertyGetterToInitializerIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
+5
View File
@@ -1675,6 +1675,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertPropertyGetterToInitializerIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
+5
View File
@@ -1675,6 +1675,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertPropertyGetterToInitializerIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
+5
View File
@@ -1676,6 +1676,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertPropertyGetterToInitializerIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
+5
View File
@@ -1675,6 +1675,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertPropertyGetterToInitializerIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
+5
View File
@@ -1675,6 +1675,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertPropertyGetterToInitializerIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
+5
View File
@@ -1676,6 +1676,11 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
<category>Kotlin</category>
</intentionAction>
<intentionAction>
<className>org.jetbrains.kotlin.idea.intentions.ConvertPropertyGetterToInitializerIntention</className>
<category>Kotlin</category>
</intentionAction>
<lang.inspectionSuppressor language="kotlin" implementationClass="org.jetbrains.kotlin.idea.inspections.KotlinInspectionSuppressor"/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.intentions.ObjectLiteralToLambdaInspection"
@@ -0,0 +1,49 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClass
import org.jetbrains.kotlin.psi.psiUtil.endOffset
class ConvertPropertyGetterToInitializerIntention : SelfTargetingIntention<KtPropertyAccessor>(
KtPropertyAccessor::class.java, "Convert property getter to initializer"
) {
override fun isApplicableTo(element: KtPropertyAccessor, caretOffset: Int): Boolean {
if (!element.isGetter || element.singleExpression() == null) return false
val property = element.parent as? KtProperty ?: return false
if (property.hasInitializer()
|| property.receiverTypeReference != null
|| property.containingClass()?.isInterface() == true
|| (property.descriptor as? PropertyDescriptor)?.isExpect == true
) return false
return true
}
override fun applyTo(element: KtPropertyAccessor, editor: Editor?) {
val property = element.parent as? KtProperty ?: return
val commentSaver = CommentSaver(property)
property.initializer = element.singleExpression()
property.deleteChildRange(property.initializer?.nextSibling, element)
editor?.caretModel?.moveToOffset(property.endOffset)
commentSaver.restore(property)
}
}
private fun KtPropertyAccessor.singleExpression(): KtExpression? {
val bodyExpression = this.bodyExpression
return if (bodyExpression is KtBlockExpression)
(bodyExpression.statements.singleOrNull() as? KtReturnExpression)?.returnedExpression
else
bodyExpression
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.intentions.ConvertPropertyGetterToInitializerIntention
@@ -0,0 +1,4 @@
val p: Int
get() {
return 1
}<caret>
@@ -0,0 +1 @@
val p: Int = 1
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
class C
val C.p: Int
<caret>get() = 1
@@ -0,0 +1,2 @@
val p: Int // comment
<caret>get() = 1
@@ -0,0 +1,2 @@
val p: Int // comment
= 1
@@ -0,0 +1,3 @@
val p2: Int
// comment
<caret>get() = 1
@@ -0,0 +1,2 @@
val p2: Int// comment
= 1
@@ -0,0 +1,2 @@
val p3: Int
<caret>get() = 1 // comment
@@ -0,0 +1 @@
val p3: Int = 1 // comment
@@ -0,0 +1,3 @@
// IS_APPLICABLE: false
var hasInitializer: Int = 0
<caret>get() = 1
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
interface I {
val p: Int
<caret>get() = 1
}
@@ -0,0 +1,6 @@
// IS_APPLICABLE: false
val multiStatementBlock: Int
<caret>get() {
val a = 1
return 6
}
@@ -0,0 +1,3 @@
// WITH_RUNTIME
val p: Int
<caret>get() = run { 1 }
@@ -0,0 +1,2 @@
// WITH_RUNTIME
val p: Int = run { 1 }
@@ -0,0 +1,2 @@
val p: Int
<caret>get() = 1
@@ -0,0 +1 @@
val p: Int = 1<caret>
@@ -0,0 +1,7 @@
// "Convert property getter to initializer" "false"
// ERROR: Expected declaration must not have a body
// ACTION: Convert to block body
expect class C {
val p: Int
<caret>get() = 1
}
@@ -0,0 +1,3 @@
actual class C {
actual val p: Int = 1
}
@@ -0,0 +1,3 @@
actual class C {
actual val p: Int = 1
}
@@ -1,5 +1,6 @@
// "Surround with null check" "false"
// ACTION: Add non-null asserted (!!) call
// ACTION: Convert property getter to initializer
// ACTION: Convert to block body
// ACTION: Introduce local variable
// ACTION: Replace with safe (?.) call
@@ -5816,6 +5816,69 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
}
}
@TestMetadata("idea/testData/intentions/convertPropertyGetterToInitializer")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ConvertPropertyGetterToInitializer extends AbstractIntentionTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInConvertPropertyGetterToInitializer() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertPropertyGetterToInitializer"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("block.kt")
public void testBlock() throws Exception {
runTest("idea/testData/intentions/convertPropertyGetterToInitializer/block.kt");
}
@TestMetadata("extentionProperty.kt")
public void testExtentionProperty() throws Exception {
runTest("idea/testData/intentions/convertPropertyGetterToInitializer/extentionProperty.kt");
}
@TestMetadata("hasComment.kt")
public void testHasComment() throws Exception {
runTest("idea/testData/intentions/convertPropertyGetterToInitializer/hasComment.kt");
}
@TestMetadata("hasComment2.kt")
public void testHasComment2() throws Exception {
runTest("idea/testData/intentions/convertPropertyGetterToInitializer/hasComment2.kt");
}
@TestMetadata("hasComment3.kt")
public void testHasComment3() throws Exception {
runTest("idea/testData/intentions/convertPropertyGetterToInitializer/hasComment3.kt");
}
@TestMetadata("hasInitializer.kt")
public void testHasInitializer() throws Exception {
runTest("idea/testData/intentions/convertPropertyGetterToInitializer/hasInitializer.kt");
}
@TestMetadata("inInterface.kt")
public void testInInterface() throws Exception {
runTest("idea/testData/intentions/convertPropertyGetterToInitializer/inInterface.kt");
}
@TestMetadata("multiStatementBlock.kt")
public void testMultiStatementBlock() throws Exception {
runTest("idea/testData/intentions/convertPropertyGetterToInitializer/multiStatementBlock.kt");
}
@TestMetadata("run.kt")
public void testRun() throws Exception {
runTest("idea/testData/intentions/convertPropertyGetterToInitializer/run.kt");
}
@TestMetadata("simple.kt")
public void testSimple() throws Exception {
runTest("idea/testData/intentions/convertPropertyGetterToInitializer/simple.kt");
}
}
@TestMetadata("idea/testData/intentions/convertPropertyInitializerToGetter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -144,6 +144,11 @@ public class QuickFixMultiModuleTestGenerated extends AbstractQuickFixMultiModul
runTest("idea/testData/multiModuleQuickFix/convertExpectSealedClassToEnum/");
}
@TestMetadata("convertPropertyGetterToInitializer")
public void testConvertPropertyGetterToInitializer() throws Exception {
runTest("idea/testData/multiModuleQuickFix/convertPropertyGetterToInitializer/");
}
@TestMetadata("createFunInExpectClass")
public void testCreateFunInExpectClass() throws Exception {
runTest("idea/testData/multiModuleQuickFix/createFunInExpectClass/");