From fbd992f8c735b22bcdd9bc72edc3d14a4c8b96de Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Wed, 17 Apr 2019 10:02:02 +0300 Subject: [PATCH] Add intentions to convert lazy <--> ordinary property #KT-23501 Fixed --- .../org/jetbrains/kotlin/psi/KtPsiFactory.kt | 8 ++ idea/resources/META-INF/plugin-common.xml | 10 +++ .../after.kt.template | 1 + .../before.kt.template | 1 + .../description.html | 5 ++ .../after.kt.template | 1 + .../before.kt.template | 1 + .../description.html | 6 ++ .../ConvertLazyPropertyToOrdinaryIntention.kt | 42 ++++++++++ .../ConvertOrdinaryPropertyToLazyIntention.kt | 35 +++++++++ .../convertLazyPropertyToOrdinary/.intention | 1 + .../convertLazyPropertyToOrdinary/comment.kt | 5 ++ .../comment.kt.after | 5 ++ .../multiStatement.kt | 7 ++ .../multiStatement.kt.after | 7 ++ .../noStatement.kt | 3 + .../singleStatement.kt | 2 + .../singleStatement.kt.after | 2 + .../singleStatement2.kt | 4 + .../singleStatement2.kt.after | 2 + .../convertLazyPropertyToOrdinary/var.kt | 3 + .../var.kt.after | 3 + .../convertOrdinaryPropertyToLazy/.intention | 1 + .../convertOrdinaryPropertyToLazy/basic.kt | 2 + .../basic.kt.after | 2 + .../noInitializer.kt | 4 + .../convertOrdinaryPropertyToLazy/run.kt | 7 ++ .../run.kt.after | 7 ++ .../convertOrdinaryPropertyToLazy/var.kt | 2 + .../use-site_field_member_with_delegate.kt | 1 + .../use-site_field_toplevel_with_delegate.kt | 1 + .../testData/quickfix/addExclExclCall/null.kt | 1 + ...QualifiedExpressionNotFirst.before.Main.kt | 1 + ...QualifiedExpressionNotFirst.before.Main.kt | 1 + ...tsForClassInExcludedPackage.before.Main.kt | 1 + .../noImportsForExcludedClass.before.Main.kt | 1 + ...orFunctionInExcludedPackage.before.Main.kt | 1 + .../createVariable/localVariable/inClass.kt | 1 + .../localVariable/onTopLevel.kt | 1 + .../inPropertyInitializerInClassObject.kt | 1 + .../inPropertyInitializerInObject.kt | 1 + .../parameter/inPropertyInitializerNoClass.kt | 1 + .../privateMemberToInternalWithExposed.kt | 1 + .../openInCompanion.kt | 1 + .../quickfix/modifiers/suspend/topLevel.kt | 1 + .../replaceJvmFieldWithConst/class.kt | 1 + .../nonConstantInitializer.kt | 1 + .../replaceJvmFieldWithConst/nullable.kt | 1 + .../stringTemplateWithVal.kt | 1 + .../suppress/external/suppressActive.kt | 1 + .../quickfix/typeMismatch/casts/unsigned.kt | 1 + .../doubleToIntDecimalPlaces.kt | 1 + .../wrongPrimitive/doubleToLongNotInRange.kt | 1 + .../wrongPrimitive/doubleToShortNotInRange.kt | 1 + .../intentions/IntentionTestGenerated.java | 76 +++++++++++++++++++ 55 files changed, 280 insertions(+) create mode 100644 idea/resources/intentionDescriptions/ConvertLazyPropertyToOrdinaryIntention/after.kt.template create mode 100644 idea/resources/intentionDescriptions/ConvertLazyPropertyToOrdinaryIntention/before.kt.template create mode 100644 idea/resources/intentionDescriptions/ConvertLazyPropertyToOrdinaryIntention/description.html create mode 100644 idea/resources/intentionDescriptions/ConvertOrdinaryPropertyToLazyIntention/after.kt.template create mode 100644 idea/resources/intentionDescriptions/ConvertOrdinaryPropertyToLazyIntention/before.kt.template create mode 100644 idea/resources/intentionDescriptions/ConvertOrdinaryPropertyToLazyIntention/description.html create mode 100644 idea/src/org/jetbrains/kotlin/idea/intentions/ConvertLazyPropertyToOrdinaryIntention.kt create mode 100644 idea/src/org/jetbrains/kotlin/idea/intentions/ConvertOrdinaryPropertyToLazyIntention.kt create mode 100644 idea/testData/intentions/convertLazyPropertyToOrdinary/.intention create mode 100644 idea/testData/intentions/convertLazyPropertyToOrdinary/comment.kt create mode 100644 idea/testData/intentions/convertLazyPropertyToOrdinary/comment.kt.after create mode 100644 idea/testData/intentions/convertLazyPropertyToOrdinary/multiStatement.kt create mode 100644 idea/testData/intentions/convertLazyPropertyToOrdinary/multiStatement.kt.after create mode 100644 idea/testData/intentions/convertLazyPropertyToOrdinary/noStatement.kt create mode 100644 idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement.kt create mode 100644 idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement.kt.after create mode 100644 idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement2.kt create mode 100644 idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement2.kt.after create mode 100644 idea/testData/intentions/convertLazyPropertyToOrdinary/var.kt create mode 100644 idea/testData/intentions/convertLazyPropertyToOrdinary/var.kt.after create mode 100644 idea/testData/intentions/convertOrdinaryPropertyToLazy/.intention create mode 100644 idea/testData/intentions/convertOrdinaryPropertyToLazy/basic.kt create mode 100644 idea/testData/intentions/convertOrdinaryPropertyToLazy/basic.kt.after create mode 100644 idea/testData/intentions/convertOrdinaryPropertyToLazy/noInitializer.kt create mode 100644 idea/testData/intentions/convertOrdinaryPropertyToLazy/run.kt create mode 100644 idea/testData/intentions/convertOrdinaryPropertyToLazy/run.kt.after create mode 100644 idea/testData/intentions/convertOrdinaryPropertyToLazy/var.kt diff --git a/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt b/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt index 335c224d58b..046e058a60a 100644 --- a/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt +++ b/compiler/psi/src/org/jetbrains/kotlin/psi/KtPsiFactory.kt @@ -278,6 +278,14 @@ class KtPsiFactory @JvmOverloads constructor(private val project: Project, val m bodyExpression.replace(expression) return setter } + + fun createPropertyDelegate(expression: KtExpression): KtPropertyDelegate { + val property = createProperty("val x by lazy { 1 }") + val delegate = property.delegate!! + val delegateExpression = delegate.expression!! + delegateExpression.replace(expression) + return delegate + } fun createDestructuringDeclaration(text: String): KtDestructuringDeclaration { return createFunction("fun foo() {$text}").bodyBlockExpression!!.statements.first() as KtDestructuringDeclaration diff --git a/idea/resources/META-INF/plugin-common.xml b/idea/resources/META-INF/plugin-common.xml index 92da75bc49c..acc64b27dc0 100644 --- a/idea/resources/META-INF/plugin-common.xml +++ b/idea/resources/META-INF/plugin-common.xml @@ -1663,6 +1663,16 @@ Kotlin + + org.jetbrains.kotlin.idea.intentions.ConvertOrdinaryPropertyToLazyIntention + Kotlin + + + + org.jetbrains.kotlin.idea.intentions.ConvertLazyPropertyToOrdinaryIntention + Kotlin + + + +This intention converts a lazy property / variable to an ordinary property / variable. + + \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/ConvertOrdinaryPropertyToLazyIntention/after.kt.template b/idea/resources/intentionDescriptions/ConvertOrdinaryPropertyToLazyIntention/after.kt.template new file mode 100644 index 00000000000..8612978b851 --- /dev/null +++ b/idea/resources/intentionDescriptions/ConvertOrdinaryPropertyToLazyIntention/after.kt.template @@ -0,0 +1 @@ +val x: String by lazy { "Hello" } \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/ConvertOrdinaryPropertyToLazyIntention/before.kt.template b/idea/resources/intentionDescriptions/ConvertOrdinaryPropertyToLazyIntention/before.kt.template new file mode 100644 index 00000000000..691fc8ff5ce --- /dev/null +++ b/idea/resources/intentionDescriptions/ConvertOrdinaryPropertyToLazyIntention/before.kt.template @@ -0,0 +1 @@ +val x: String = "Hello" \ No newline at end of file diff --git a/idea/resources/intentionDescriptions/ConvertOrdinaryPropertyToLazyIntention/description.html b/idea/resources/intentionDescriptions/ConvertOrdinaryPropertyToLazyIntention/description.html new file mode 100644 index 00000000000..52ef6fb39f4 --- /dev/null +++ b/idea/resources/intentionDescriptions/ConvertOrdinaryPropertyToLazyIntention/description.html @@ -0,0 +1,6 @@ + + +This intention converts a ordinary property / variable to a lazy property / variable. + + + \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertLazyPropertyToOrdinaryIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertLazyPropertyToOrdinaryIntention.kt new file mode 100644 index 00000000000..358ca283bcd --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertLazyPropertyToOrdinaryIntention.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2010-2019 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 com.intellij.psi.PsiComment +import org.jetbrains.kotlin.idea.inspections.collections.isCalling +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.allChildren + +class ConvertLazyPropertyToOrdinaryIntention : SelfTargetingIntention( + KtProperty::class.java, "Convert to ordinary property" +) { + override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean { + val delegateExpression = element.delegate?.expression as? KtCallExpression ?: return false + val lambdaBody = delegateExpression.functionLiteral()?.bodyExpression ?: return false + if (lambdaBody.statements.isEmpty()) return false + return delegateExpression.isCalling(FqName("kotlin.lazy")) + } + + override fun applyTo(element: KtProperty, editor: Editor?) { + val delegate = element.delegate ?: return + val delegateExpression = delegate.expression as? KtCallExpression ?: return + val functionLiteral = delegateExpression.functionLiteral() ?: return + element.initializer = functionLiteral.singleStatement() ?: KtPsiFactory(element).createExpression("run ${functionLiteral.text}") + delegate.delete() + } + + private fun KtCallExpression.functionLiteral(): KtFunctionLiteral? { + return lambdaArguments.singleOrNull()?.getLambdaExpression()?.functionLiteral + } + + private fun KtFunctionLiteral.singleStatement(): KtExpression? { + val body = this.bodyExpression ?: return null + if (body.allChildren.any { it is PsiComment }) return null + return body.statements.singleOrNull() + } +} diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertOrdinaryPropertyToLazyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertOrdinaryPropertyToLazyIntention.kt new file mode 100644 index 00000000000..a8d29b012db --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertOrdinaryPropertyToLazyIntention.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2010-2019 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.idea.inspections.collections.isCalling +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.psi.KtCallExpression +import org.jetbrains.kotlin.psi.KtProperty +import org.jetbrains.kotlin.psi.KtPsiFactory +import org.jetbrains.kotlin.psi.createExpressionByPattern + +class ConvertOrdinaryPropertyToLazyIntention : SelfTargetingIntention( + KtProperty::class.java, "Convert to lazy property" +) { + override fun isApplicableTo(element: KtProperty, caretOffset: Int): Boolean { + return !element.isVar && element.initializer != null && element.getter == null && !element.isLocal + } + + override fun applyTo(element: KtProperty, editor: Editor?) { + val initializer = element.initializer ?: return + val psiFactory = KtPsiFactory(element) + val newExpression = if (initializer is KtCallExpression && initializer.isCalling(FqName("kotlin.run"))) { + initializer.calleeExpression?.replace(psiFactory.createExpression("lazy")) + initializer + } else { + psiFactory.createExpressionByPattern("lazy { $0 }", initializer) + } + element.addAfter(psiFactory.createPropertyDelegate(newExpression), initializer) + element.initializer = null + } +} diff --git a/idea/testData/intentions/convertLazyPropertyToOrdinary/.intention b/idea/testData/intentions/convertLazyPropertyToOrdinary/.intention new file mode 100644 index 00000000000..01693875ff1 --- /dev/null +++ b/idea/testData/intentions/convertLazyPropertyToOrdinary/.intention @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.intentions.ConvertLazyPropertyToOrdinaryIntention diff --git a/idea/testData/intentions/convertLazyPropertyToOrdinary/comment.kt b/idea/testData/intentions/convertLazyPropertyToOrdinary/comment.kt new file mode 100644 index 00000000000..a7a3f1471b1 --- /dev/null +++ b/idea/testData/intentions/convertLazyPropertyToOrdinary/comment.kt @@ -0,0 +1,5 @@ +// WITH_RUNTIME +val x by lazy { + // comment + 1 +} \ No newline at end of file diff --git a/idea/testData/intentions/convertLazyPropertyToOrdinary/comment.kt.after b/idea/testData/intentions/convertLazyPropertyToOrdinary/comment.kt.after new file mode 100644 index 00000000000..f2d5eed99e3 --- /dev/null +++ b/idea/testData/intentions/convertLazyPropertyToOrdinary/comment.kt.after @@ -0,0 +1,5 @@ +// WITH_RUNTIME +val x = run { + // comment + 1 +} \ No newline at end of file diff --git a/idea/testData/intentions/convertLazyPropertyToOrdinary/multiStatement.kt b/idea/testData/intentions/convertLazyPropertyToOrdinary/multiStatement.kt new file mode 100644 index 00000000000..e8a29226c4d --- /dev/null +++ b/idea/testData/intentions/convertLazyPropertyToOrdinary/multiStatement.kt @@ -0,0 +1,7 @@ +// WITH_RUNTIME +val x by lazy { + foo() + 1 +} + +fun foo() {} \ No newline at end of file diff --git a/idea/testData/intentions/convertLazyPropertyToOrdinary/multiStatement.kt.after b/idea/testData/intentions/convertLazyPropertyToOrdinary/multiStatement.kt.after new file mode 100644 index 00000000000..75270b032c9 --- /dev/null +++ b/idea/testData/intentions/convertLazyPropertyToOrdinary/multiStatement.kt.after @@ -0,0 +1,7 @@ +// WITH_RUNTIME +val x = run { + foo() + 1 +} + +fun foo() {} \ No newline at end of file diff --git a/idea/testData/intentions/convertLazyPropertyToOrdinary/noStatement.kt b/idea/testData/intentions/convertLazyPropertyToOrdinary/noStatement.kt new file mode 100644 index 00000000000..0040fa187a2 --- /dev/null +++ b/idea/testData/intentions/convertLazyPropertyToOrdinary/noStatement.kt @@ -0,0 +1,3 @@ +// IS_APPLICABLE: false +// WITH_RUNTIME +val x by lazy { } \ No newline at end of file diff --git a/idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement.kt b/idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement.kt new file mode 100644 index 00000000000..b1022ee5ff2 --- /dev/null +++ b/idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement.kt @@ -0,0 +1,2 @@ +// WITH_RUNTIME +val x: Int by lazy { 1 } \ No newline at end of file diff --git a/idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement.kt.after b/idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement.kt.after new file mode 100644 index 00000000000..935d969c649 --- /dev/null +++ b/idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement.kt.after @@ -0,0 +1,2 @@ +// WITH_RUNTIME +val x: Int = 1 \ No newline at end of file diff --git a/idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement2.kt b/idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement2.kt new file mode 100644 index 00000000000..bc9ba9669b1 --- /dev/null +++ b/idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement2.kt @@ -0,0 +1,4 @@ +// WITH_RUNTIME +val x by lazy { + 1 +} \ No newline at end of file diff --git a/idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement2.kt.after b/idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement2.kt.after new file mode 100644 index 00000000000..55807895fd3 --- /dev/null +++ b/idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement2.kt.after @@ -0,0 +1,2 @@ +// WITH_RUNTIME +val x = 1 \ No newline at end of file diff --git a/idea/testData/intentions/convertLazyPropertyToOrdinary/var.kt b/idea/testData/intentions/convertLazyPropertyToOrdinary/var.kt new file mode 100644 index 00000000000..ed5e29253be --- /dev/null +++ b/idea/testData/intentions/convertLazyPropertyToOrdinary/var.kt @@ -0,0 +1,3 @@ +// DISABLE-ERRORS +// WITH_RUNTIME +var x: Int by lazy { 1 } \ No newline at end of file diff --git a/idea/testData/intentions/convertLazyPropertyToOrdinary/var.kt.after b/idea/testData/intentions/convertLazyPropertyToOrdinary/var.kt.after new file mode 100644 index 00000000000..dd9214c9b40 --- /dev/null +++ b/idea/testData/intentions/convertLazyPropertyToOrdinary/var.kt.after @@ -0,0 +1,3 @@ +// DISABLE-ERRORS +// WITH_RUNTIME +var x: Int = 1 \ No newline at end of file diff --git a/idea/testData/intentions/convertOrdinaryPropertyToLazy/.intention b/idea/testData/intentions/convertOrdinaryPropertyToLazy/.intention new file mode 100644 index 00000000000..204a88143fc --- /dev/null +++ b/idea/testData/intentions/convertOrdinaryPropertyToLazy/.intention @@ -0,0 +1 @@ +org.jetbrains.kotlin.idea.intentions.ConvertOrdinaryPropertyToLazyIntention \ No newline at end of file diff --git a/idea/testData/intentions/convertOrdinaryPropertyToLazy/basic.kt b/idea/testData/intentions/convertOrdinaryPropertyToLazy/basic.kt new file mode 100644 index 00000000000..bad91299046 --- /dev/null +++ b/idea/testData/intentions/convertOrdinaryPropertyToLazy/basic.kt @@ -0,0 +1,2 @@ +// WITH_RUNTIME +val x: Int = 1 \ No newline at end of file diff --git a/idea/testData/intentions/convertOrdinaryPropertyToLazy/basic.kt.after b/idea/testData/intentions/convertOrdinaryPropertyToLazy/basic.kt.after new file mode 100644 index 00000000000..100ed0c1af7 --- /dev/null +++ b/idea/testData/intentions/convertOrdinaryPropertyToLazy/basic.kt.after @@ -0,0 +1,2 @@ +// WITH_RUNTIME +val x: Int by lazy { 1 } \ No newline at end of file diff --git a/idea/testData/intentions/convertOrdinaryPropertyToLazy/noInitializer.kt b/idea/testData/intentions/convertOrdinaryPropertyToLazy/noInitializer.kt new file mode 100644 index 00000000000..752f954fa0c --- /dev/null +++ b/idea/testData/intentions/convertOrdinaryPropertyToLazy/noInitializer.kt @@ -0,0 +1,4 @@ +// IS_APPLICABLE: false +class Foo { + lateinit var x: String +} \ No newline at end of file diff --git a/idea/testData/intentions/convertOrdinaryPropertyToLazy/run.kt b/idea/testData/intentions/convertOrdinaryPropertyToLazy/run.kt new file mode 100644 index 00000000000..e10d3b19b81 --- /dev/null +++ b/idea/testData/intentions/convertOrdinaryPropertyToLazy/run.kt @@ -0,0 +1,7 @@ +// WITH_RUNTIME +val x = run { + foo() + 3 +} + +fun foo() {} \ No newline at end of file diff --git a/idea/testData/intentions/convertOrdinaryPropertyToLazy/run.kt.after b/idea/testData/intentions/convertOrdinaryPropertyToLazy/run.kt.after new file mode 100644 index 00000000000..fa830507872 --- /dev/null +++ b/idea/testData/intentions/convertOrdinaryPropertyToLazy/run.kt.after @@ -0,0 +1,7 @@ +// WITH_RUNTIME +val x by lazy { + foo() + 3 +} + +fun foo() {} \ No newline at end of file diff --git a/idea/testData/intentions/convertOrdinaryPropertyToLazy/var.kt b/idea/testData/intentions/convertOrdinaryPropertyToLazy/var.kt new file mode 100644 index 00000000000..571a692e25b --- /dev/null +++ b/idea/testData/intentions/convertOrdinaryPropertyToLazy/var.kt @@ -0,0 +1,2 @@ +// IS_APPLICABLE: false +var x: Int = 1 \ No newline at end of file diff --git a/idea/testData/quickfix/addAnnotationTarget/use-site_field_member_with_delegate.kt b/idea/testData/quickfix/addAnnotationTarget/use-site_field_member_with_delegate.kt index f6972874328..4cd287a6233 100644 --- a/idea/testData/quickfix/addAnnotationTarget/use-site_field_member_with_delegate.kt +++ b/idea/testData/quickfix/addAnnotationTarget/use-site_field_member_with_delegate.kt @@ -1,5 +1,6 @@ // "Add annotation target" "false" // WITH_RUNTIME +// ACTION: Convert to ordinary property // ACTION: Introduce import alias // ACTION: Make internal // ACTION: Make private diff --git a/idea/testData/quickfix/addAnnotationTarget/use-site_field_toplevel_with_delegate.kt b/idea/testData/quickfix/addAnnotationTarget/use-site_field_toplevel_with_delegate.kt index f1cefc6c390..8d28ec65fed 100644 --- a/idea/testData/quickfix/addAnnotationTarget/use-site_field_toplevel_with_delegate.kt +++ b/idea/testData/quickfix/addAnnotationTarget/use-site_field_toplevel_with_delegate.kt @@ -1,5 +1,6 @@ // "Add annotation target" "false" // WITH_RUNTIME +// ACTION: Convert to ordinary property // ACTION: Introduce import alias // ACTION: Make internal // ACTION: Make private diff --git a/idea/testData/quickfix/addExclExclCall/null.kt b/idea/testData/quickfix/addExclExclCall/null.kt index 693783e8e27..902661aac0b 100644 --- a/idea/testData/quickfix/addExclExclCall/null.kt +++ b/idea/testData/quickfix/addExclExclCall/null.kt @@ -1,4 +1,5 @@ // "Add non-null asserted (!!) call" "false" +// ACTION: Convert to lazy property // ACTION: Add 'const' modifier // ACTION: Add 'toString()' call // ACTION: Change type of 'x' to 'String?' diff --git a/idea/testData/quickfix/autoImports/noImportInQualifiedExpressionNotFirst.before.Main.kt b/idea/testData/quickfix/autoImports/noImportInQualifiedExpressionNotFirst.before.Main.kt index ae3e23fa8ef..2133f15b0f1 100644 --- a/idea/testData/quickfix/autoImports/noImportInQualifiedExpressionNotFirst.before.Main.kt +++ b/idea/testData/quickfix/autoImports/noImportInQualifiedExpressionNotFirst.before.Main.kt @@ -1,4 +1,5 @@ // "class org.jetbrains.kotlin.idea.quickfix.ImportFix" "false" +// ACTION: Convert to lazy property // ACTION: Convert property initializer to getter // ACTION: Create class 'SomeTest' // ACTION: Rename reference diff --git a/idea/testData/quickfix/autoImports/noImportInSafeQualifiedExpressionNotFirst.before.Main.kt b/idea/testData/quickfix/autoImports/noImportInSafeQualifiedExpressionNotFirst.before.Main.kt index 283f69a8a60..afa5c1cb4af 100644 --- a/idea/testData/quickfix/autoImports/noImportInSafeQualifiedExpressionNotFirst.before.Main.kt +++ b/idea/testData/quickfix/autoImports/noImportInSafeQualifiedExpressionNotFirst.before.Main.kt @@ -1,6 +1,7 @@ // "class org.jetbrains.kotlin.idea.quickfix.ImportFix" "false" // ERROR: Unresolved reference: SomeTest // ERROR: Expression expected, but a package name found +// ACTION: Convert to lazy property // ACTION: Convert property initializer to getter // ACTION: Create class 'SomeTest' // ACTION: Replace safe access expression with 'if' expression diff --git a/idea/testData/quickfix/autoImports/noImportsForClassInExcludedPackage.before.Main.kt b/idea/testData/quickfix/autoImports/noImportsForClassInExcludedPackage.before.Main.kt index 760d92c4c51..14cf5690d36 100644 --- a/idea/testData/quickfix/autoImports/noImportsForClassInExcludedPackage.before.Main.kt +++ b/idea/testData/quickfix/autoImports/noImportsForClassInExcludedPackage.before.Main.kt @@ -1,4 +1,5 @@ // "class org.jetbrains.kotlin.idea.quickfix.ImportFix" "false" +// ACTION: Convert to lazy property // ACTION: Convert property initializer to getter // ACTION: Create class 'SomeClass' // ACTION: Create function 'SomeClass' diff --git a/idea/testData/quickfix/autoImports/noImportsForExcludedClass.before.Main.kt b/idea/testData/quickfix/autoImports/noImportsForExcludedClass.before.Main.kt index 6621a5d3fe3..053f4faef18 100644 --- a/idea/testData/quickfix/autoImports/noImportsForExcludedClass.before.Main.kt +++ b/idea/testData/quickfix/autoImports/noImportsForExcludedClass.before.Main.kt @@ -1,4 +1,5 @@ // "class org.jetbrains.kotlin.idea.quickfix.ImportFix" "false" +// ACTION: Convert to lazy property // ACTION: Convert property initializer to getter // ACTION: Create class 'ExcludedClass' // ACTION: Create function 'ExcludedClass' diff --git a/idea/testData/quickfix/autoImports/noImportsForFunctionInExcludedPackage.before.Main.kt b/idea/testData/quickfix/autoImports/noImportsForFunctionInExcludedPackage.before.Main.kt index e669f5aaf25..7f0796ddafc 100644 --- a/idea/testData/quickfix/autoImports/noImportsForFunctionInExcludedPackage.before.Main.kt +++ b/idea/testData/quickfix/autoImports/noImportsForFunctionInExcludedPackage.before.Main.kt @@ -1,4 +1,5 @@ // "class org.jetbrains.kotlin.idea.quickfix.ImportFix" "false" +// ACTION: Convert to lazy property // ACTION: Convert property initializer to getter // ACTION: Create function 'someFunction' // ACTION: Rename reference diff --git a/idea/testData/quickfix/createFromUsage/createVariable/localVariable/inClass.kt b/idea/testData/quickfix/createFromUsage/createVariable/localVariable/inClass.kt index 401789f7068..9f2b6bc746d 100644 --- a/idea/testData/quickfix/createFromUsage/createVariable/localVariable/inClass.kt +++ b/idea/testData/quickfix/createFromUsage/createVariable/localVariable/inClass.kt @@ -1,4 +1,5 @@ // "Create local variable 'foo'" "false" +// ACTION: Convert to lazy property // ACTION: Convert property initializer to getter // ACTION: Create parameter 'foo' // ACTION: Create property 'foo' diff --git a/idea/testData/quickfix/createFromUsage/createVariable/localVariable/onTopLevel.kt b/idea/testData/quickfix/createFromUsage/createVariable/localVariable/onTopLevel.kt index 0025db2a185..c8520eff7a8 100644 --- a/idea/testData/quickfix/createFromUsage/createVariable/localVariable/onTopLevel.kt +++ b/idea/testData/quickfix/createFromUsage/createVariable/localVariable/onTopLevel.kt @@ -1,4 +1,5 @@ // "Create local variable 'foo'" "false" +// ACTION: Convert to lazy property // ACTION: Convert property initializer to getter // ACTION: Create property 'foo' // ACTION: Rename reference diff --git a/idea/testData/quickfix/createFromUsage/createVariable/parameter/inPropertyInitializerInClassObject.kt b/idea/testData/quickfix/createFromUsage/createVariable/parameter/inPropertyInitializerInClassObject.kt index 359ae220589..02d2f348ac9 100644 --- a/idea/testData/quickfix/createFromUsage/createVariable/parameter/inPropertyInitializerInClassObject.kt +++ b/idea/testData/quickfix/createFromUsage/createVariable/parameter/inPropertyInitializerInClassObject.kt @@ -1,4 +1,5 @@ // "Create parameter 'foo'" "false" +// ACTION: Convert to lazy property // ACTION: Convert property initializer to getter // ACTION: Create property 'foo' // ACTION: Rename reference diff --git a/idea/testData/quickfix/createFromUsage/createVariable/parameter/inPropertyInitializerInObject.kt b/idea/testData/quickfix/createFromUsage/createVariable/parameter/inPropertyInitializerInObject.kt index bcfbab62992..51a3b118de8 100644 --- a/idea/testData/quickfix/createFromUsage/createVariable/parameter/inPropertyInitializerInObject.kt +++ b/idea/testData/quickfix/createFromUsage/createVariable/parameter/inPropertyInitializerInObject.kt @@ -1,4 +1,5 @@ // "Create parameter 'foo'" "false" +// ACTION: Convert to lazy property // ACTION: Convert property initializer to getter // ACTION: Create property 'foo' // ACTION: Rename reference diff --git a/idea/testData/quickfix/createFromUsage/createVariable/parameter/inPropertyInitializerNoClass.kt b/idea/testData/quickfix/createFromUsage/createVariable/parameter/inPropertyInitializerNoClass.kt index 7636d64874a..1612f1a5bb2 100644 --- a/idea/testData/quickfix/createFromUsage/createVariable/parameter/inPropertyInitializerNoClass.kt +++ b/idea/testData/quickfix/createFromUsage/createVariable/parameter/inPropertyInitializerNoClass.kt @@ -1,4 +1,5 @@ // "Create parameter 'foo'" "false" +// ACTION: Convert to lazy property // ACTION: Convert property initializer to getter // ACTION: Create property 'foo' // ACTION: Rename reference diff --git a/idea/testData/quickfix/increaseVisibility/privateMemberToInternalWithExposed.kt b/idea/testData/quickfix/increaseVisibility/privateMemberToInternalWithExposed.kt index 3cae1b35c99..133ade3b598 100644 --- a/idea/testData/quickfix/increaseVisibility/privateMemberToInternalWithExposed.kt +++ b/idea/testData/quickfix/increaseVisibility/privateMemberToInternalWithExposed.kt @@ -1,4 +1,5 @@ // "Make bar internal" "false" +// ACTION: Convert to lazy property // ACTION: Convert property initializer to getter // ACTION: Add names to call arguments // ACTION: Move to constructor diff --git a/idea/testData/quickfix/modifiers/addOpenToClassDeclaration/openInCompanion.kt b/idea/testData/quickfix/modifiers/addOpenToClassDeclaration/openInCompanion.kt index bba2fd32cbe..f4f0938af47 100644 --- a/idea/testData/quickfix/modifiers/addOpenToClassDeclaration/openInCompanion.kt +++ b/idea/testData/quickfix/modifiers/addOpenToClassDeclaration/openInCompanion.kt @@ -1,4 +1,5 @@ // "Make 'MyClass' open" "false" +// ACTION: Convert to lazy property // ACTION: Make 'y' not open // ACTION: Add 'const' modifier // ACTION: Make internal diff --git a/idea/testData/quickfix/modifiers/suspend/topLevel.kt b/idea/testData/quickfix/modifiers/suspend/topLevel.kt index c210bcb6689..f94c6c4d383 100644 --- a/idea/testData/quickfix/modifiers/suspend/topLevel.kt +++ b/idea/testData/quickfix/modifiers/suspend/topLevel.kt @@ -1,4 +1,5 @@ // "Make bar suspend" "false" +// ACTION: Convert to lazy property // ACTION: Convert property initializer to getter // ACTION: Introduce import alias // ERROR: Suspend function 'foo' should be called only from a coroutine or another suspend function diff --git a/idea/testData/quickfix/replaceJvmFieldWithConst/class.kt b/idea/testData/quickfix/replaceJvmFieldWithConst/class.kt index 50b121e17cd..ccc5aa8b784 100644 --- a/idea/testData/quickfix/replaceJvmFieldWithConst/class.kt +++ b/idea/testData/quickfix/replaceJvmFieldWithConst/class.kt @@ -1,6 +1,7 @@ // "Replace '@JvmField' with 'const'" "false" // WITH_RUNTIME // ERROR: JvmField has no effect on a private property +// ACTION: Convert to lazy property // ACTION: Make internal // ACTION: Make protected // ACTION: Make public diff --git a/idea/testData/quickfix/replaceJvmFieldWithConst/nonConstantInitializer.kt b/idea/testData/quickfix/replaceJvmFieldWithConst/nonConstantInitializer.kt index 5cecfddf590..12a8fe0b274 100644 --- a/idea/testData/quickfix/replaceJvmFieldWithConst/nonConstantInitializer.kt +++ b/idea/testData/quickfix/replaceJvmFieldWithConst/nonConstantInitializer.kt @@ -1,6 +1,7 @@ // "Replace '@JvmField' with 'const'" "false" // WITH_RUNTIME // ERROR: JvmField has no effect on a private property +// ACTION: Convert to lazy property // ACTION: Make internal // ACTION: Make public // ACTION: Remove explicit type specification diff --git a/idea/testData/quickfix/replaceJvmFieldWithConst/nullable.kt b/idea/testData/quickfix/replaceJvmFieldWithConst/nullable.kt index a230d4c1aba..9111eb71fed 100644 --- a/idea/testData/quickfix/replaceJvmFieldWithConst/nullable.kt +++ b/idea/testData/quickfix/replaceJvmFieldWithConst/nullable.kt @@ -1,6 +1,7 @@ // "Replace '@JvmField' with 'const'" "false" // WITH_RUNTIME // ERROR: JvmField has no effect on a private property +// ACTION: Convert to lazy property // ACTION: Make internal // ACTION: Make public // ACTION: Remove explicit type specification diff --git a/idea/testData/quickfix/replaceJvmFieldWithConst/stringTemplateWithVal.kt b/idea/testData/quickfix/replaceJvmFieldWithConst/stringTemplateWithVal.kt index e9cfeaa9234..dfc19ca9b65 100644 --- a/idea/testData/quickfix/replaceJvmFieldWithConst/stringTemplateWithVal.kt +++ b/idea/testData/quickfix/replaceJvmFieldWithConst/stringTemplateWithVal.kt @@ -1,6 +1,7 @@ // "Replace '@JvmField' with 'const'" "false" // WITH_RUNTIME // ERROR: JvmField has no effect on a private property +// ACTION: Convert to lazy property // ACTION: Add 'const' modifier // ACTION: Make internal // ACTION: Make public diff --git a/idea/testData/quickfix/suppress/external/suppressActive.kt b/idea/testData/quickfix/suppress/external/suppressActive.kt index c3aa6c8c2ad..f0621dee249 100644 --- a/idea/testData/quickfix/suppress/external/suppressActive.kt +++ b/idea/testData/quickfix/suppress/external/suppressActive.kt @@ -1,5 +1,6 @@ // "Typo: Change to..." "false" // TOOL: com.intellij.spellchecker.inspections.SpellCheckingInspection +// ACTION: Convert to lazy property // ACTION: Add 'const' modifier // ACTION: Convert property initializer to getter // ACTION: To raw string literal diff --git a/idea/testData/quickfix/typeMismatch/casts/unsigned.kt b/idea/testData/quickfix/typeMismatch/casts/unsigned.kt index 90c9a7fd6f8..c991c87bd78 100644 --- a/idea/testData/quickfix/typeMismatch/casts/unsigned.kt +++ b/idea/testData/quickfix/typeMismatch/casts/unsigned.kt @@ -1,6 +1,7 @@ // "Cast expression '1' to 'UInt'" "false" // WITH_RUNTIME // ERROR: Conversion of signed constants to unsigned ones is prohibited +// ACTION: Convert to lazy property // ACTION: Change parameter 'u' type of function 'takeUInt' to 'Int' // ACTION: Convert property initializer to getter // ACTION: Change to '1u' diff --git a/idea/testData/quickfix/typeMismatch/wrongPrimitive/doubleToIntDecimalPlaces.kt b/idea/testData/quickfix/typeMismatch/wrongPrimitive/doubleToIntDecimalPlaces.kt index c8b5b6cd66a..d31341ae02e 100644 --- a/idea/testData/quickfix/typeMismatch/wrongPrimitive/doubleToIntDecimalPlaces.kt +++ b/idea/testData/quickfix/typeMismatch/wrongPrimitive/doubleToIntDecimalPlaces.kt @@ -1,4 +1,5 @@ // "Change to '1'" "false" +// ACTION: Convert to lazy property // ACTION: Add 'const' modifier // ACTION: Change type of 'a' to 'Double' // ACTION: Convert expression to 'Int' diff --git a/idea/testData/quickfix/typeMismatch/wrongPrimitive/doubleToLongNotInRange.kt b/idea/testData/quickfix/typeMismatch/wrongPrimitive/doubleToLongNotInRange.kt index 78ee723ba7c..43acbabe194 100644 --- a/idea/testData/quickfix/typeMismatch/wrongPrimitive/doubleToLongNotInRange.kt +++ b/idea/testData/quickfix/typeMismatch/wrongPrimitive/doubleToLongNotInRange.kt @@ -1,4 +1,5 @@ // "Change to '10000000000000000000L'" "false" +// ACTION: Convert to lazy property // ACTION: Add 'const' modifier // ACTION: Change type of 'a' to 'Double' // ACTION: Convert expression to 'Long' diff --git a/idea/testData/quickfix/typeMismatch/wrongPrimitive/doubleToShortNotInRange.kt b/idea/testData/quickfix/typeMismatch/wrongPrimitive/doubleToShortNotInRange.kt index ba82d8ab413..df12ba1e497 100644 --- a/idea/testData/quickfix/typeMismatch/wrongPrimitive/doubleToShortNotInRange.kt +++ b/idea/testData/quickfix/typeMismatch/wrongPrimitive/doubleToShortNotInRange.kt @@ -1,4 +1,5 @@ // "Change to '65000'" "false" +// ACTION: Convert to lazy property // ACTION: Add 'const' modifier // ACTION: Change type of 'a' to 'Double' // ACTION: Convert expression to 'Short' diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index e749862fd69..3f8ece8cba5 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -5049,6 +5049,49 @@ public class IntentionTestGenerated extends AbstractIntentionTest { } } + @TestMetadata("idea/testData/intentions/convertLazyPropertyToOrdinary") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class ConvertLazyPropertyToOrdinary extends AbstractIntentionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInConvertLazyPropertyToOrdinary() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertLazyPropertyToOrdinary"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); + } + + @TestMetadata("comment.kt") + public void testComment() throws Exception { + runTest("idea/testData/intentions/convertLazyPropertyToOrdinary/comment.kt"); + } + + @TestMetadata("multiStatement.kt") + public void testMultiStatement() throws Exception { + runTest("idea/testData/intentions/convertLazyPropertyToOrdinary/multiStatement.kt"); + } + + @TestMetadata("noStatement.kt") + public void testNoStatement() throws Exception { + runTest("idea/testData/intentions/convertLazyPropertyToOrdinary/noStatement.kt"); + } + + @TestMetadata("singleStatement.kt") + public void testSingleStatement() throws Exception { + runTest("idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement.kt"); + } + + @TestMetadata("singleStatement2.kt") + public void testSingleStatement2() throws Exception { + runTest("idea/testData/intentions/convertLazyPropertyToOrdinary/singleStatement2.kt"); + } + + @TestMetadata("var.kt") + public void testVar() throws Exception { + runTest("idea/testData/intentions/convertLazyPropertyToOrdinary/var.kt"); + } + } + @TestMetadata("idea/testData/intentions/convertLineCommentToBlockComment") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -5248,6 +5291,39 @@ public class IntentionTestGenerated extends AbstractIntentionTest { } } + @TestMetadata("idea/testData/intentions/convertOrdinaryPropertyToLazy") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class ConvertOrdinaryPropertyToLazy extends AbstractIntentionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInConvertOrdinaryPropertyToLazy() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertOrdinaryPropertyToLazy"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true); + } + + @TestMetadata("basic.kt") + public void testBasic() throws Exception { + runTest("idea/testData/intentions/convertOrdinaryPropertyToLazy/basic.kt"); + } + + @TestMetadata("noInitializer.kt") + public void testNoInitializer() throws Exception { + runTest("idea/testData/intentions/convertOrdinaryPropertyToLazy/noInitializer.kt"); + } + + @TestMetadata("run.kt") + public void testRun() throws Exception { + runTest("idea/testData/intentions/convertOrdinaryPropertyToLazy/run.kt"); + } + + @TestMetadata("var.kt") + public void testVar() throws Exception { + runTest("idea/testData/intentions/convertOrdinaryPropertyToLazy/var.kt"); + } + } + @TestMetadata("idea/testData/intentions/convertParameterToReceiver") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)