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
+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
}