From 0928661ce5d080d866db8fc5bb65135fe37091a9 Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Mon, 9 Aug 2021 20:48:01 +0200 Subject: [PATCH] [kotlin] Add "Introduce Constant" refactoring #KTIJ-4841 Fixed GitOrigin-RevId: d81aba8e9c2d4efca10779cac30389db415a1cf0 --- .../kotlin/testGenerator/GenerateTests.kt | 1 + .../messages/KotlinBundle.properties | 3 + .../KotlinRefactoringSupportProvider.kt | 3 + .../ExtractableCodeDescriptor.kt | 3 +- .../extractionEngine/extractorUtil.kt | 6 +- .../IntroduceConstantAction.kt | 14 + .../KotlinIntroduceConstantHandler.kt | 174 ++++++++++++ .../introduce/AbstractExtractionTest.kt | 35 +++ .../introduce/ExtractionTestGenerated.java | 247 ++++++++++++++++++ .../binaryExpression/integerCompareTo.kt | 6 + .../integerCompareTo.kt.after | 8 + .../binaryExpression/integerDec.kt | 6 + .../binaryExpression/integerDec.kt.conflicts | 1 + .../binaryExpression/integerDiv.kt | 6 + .../binaryExpression/integerDiv.kt.after | 8 + .../binaryExpression/integerEquals.kt | 6 + .../binaryExpression/integerEquals.kt.after | 8 + .../binaryExpression/integerInc.kt | 6 + .../binaryExpression/integerInc.kt.conflicts | 1 + .../binaryExpression/integerMinus.kt | 6 + .../binaryExpression/integerMinus.kt.after | 8 + .../binaryExpression/integerPlus.kt | 7 + .../binaryExpression/integerPlus.kt.after | 9 + .../integerPlusPartNotConst.kt | 10 + .../integerPlusPartNotConst.kt.conflicts | 1 + .../binaryExpression/integerRem.kt | 6 + .../binaryExpression/integerRem.kt.after | 8 + .../binaryExpression/integerTimes.kt | 6 + .../binaryExpression/integerTimes.kt.after | 8 + .../binaryExpression/integerUnaryMinus.kt | 6 + .../integerUnaryMinus.kt.after | 8 + .../binaryExpression/integerUnaryPlus.kt | 6 + .../integerUnaryPlus.kt.after | 8 + .../binaryExpression/paranthesis.kt | 6 + .../binaryExpression/paranthesis.kt.after | 8 + .../binaryExpression/paranthesisBroken.kt | 6 + .../paranthesisBroken.kt.conflicts | 1 + .../binaryExpression/stringCompareTo.kt | 6 + .../binaryExpression/stringCompareTo.kt.after | 8 + .../binaryExpression/stringCustomOperator.kt | 7 + .../stringCustomOperator.kt.conflicts | 1 + .../binaryExpression/stringEquals.kt | 6 + .../binaryExpression/stringEquals.kt.after | 8 + .../binaryExpression/stringPlus.kt | 7 + .../binaryExpression/stringPlus.kt.after | 9 + .../stringPlusPartNotConst.kt | 10 + .../stringPlusPartNotConst.kt.conflicts | 1 + .../dotQualifiedExpression/and.kt | 6 + .../dotQualifiedExpression/and.kt.after | 8 + .../dotQualifiedExpression/inv.kt | 6 + .../dotQualifiedExpression/inv.kt.after | 8 + .../dotQualifiedExpression/or.kt | 6 + .../dotQualifiedExpression/or.kt.after | 8 + .../dotQualifiedExpression/plus.kt | 6 + .../dotQualifiedExpression/plus.kt.after | 8 + .../dotQualifiedExpression/shl.kt | 6 + .../dotQualifiedExpression/shl.kt.after | 8 + .../dotQualifiedExpression/shr.kt | 6 + .../dotQualifiedExpression/shr.kt.after | 8 + .../dotQualifiedExpression/toByte.kt | 7 + .../dotQualifiedExpression/toByte.kt.after | 9 + .../dotQualifiedExpression/toFloat.kt | 6 + .../dotQualifiedExpression/toFloat.kt.after | 8 + .../dotQualifiedExpression/ushr.kt | 6 + .../dotQualifiedExpression/ushr.kt.after | 8 + .../dotQualifiedExpression/xor.kt | 6 + .../dotQualifiedExpression/xor.kt.after | 8 + .../introduceConstant/extractInteger.kt | 9 + .../introduceConstant/extractInteger.kt.after | 11 + .../extractMultipleExpressions.kt | 10 + .../extractMultipleExpressions.kt.conflicts | 1 + .../extractPartNonConstVal.kt | 7 + .../extractPartNonConstVal.kt.conflicts | 1 + .../introduceConstant/extractWithNameClash.kt | 7 + .../extractWithNameClash.kt.after | 8 + .../brokenEntryWithBlockExpr.kt | 6 + .../brokenEntryWithBlockExpr.kt.conflicts | 1 + .../stringTemplates/brokenEntryWithExpr.kt | 6 + .../brokenEntryWithExpr.kt.conflicts | 1 + .../stringTemplates/brokenEscapeEntry.kt | 6 + .../brokenEscapeEntry.kt.conflicts | 1 + .../stringTemplates/extractFalse.kt | 9 + .../stringTemplates/extractFalse.kt.after | 11 + .../stringTemplates/extractIntegerLiteral.kt | 10 + .../extractIntegerLiteral.kt.after | 12 + .../stringTemplates/extractTrue.kt | 9 + .../stringTemplates/extractTrue.kt.after | 11 + .../stringTemplates/partTemplate.kt | 6 + .../stringTemplates/partTemplate.kt.conflicts | 1 + .../stringTemplates/singleEntryPrefix.kt | 9 + .../singleEntryPrefix.kt.after | 11 + .../stringTemplates/singleEntrySubstring.kt | 9 + .../singleEntrySubstring.kt.after | 11 + .../stringTemplates/singleEntrySuffix.kt | 9 + .../singleEntrySuffix.kt.after | 11 + .../resources/META-INF/refactorings.xml | 5 + 96 files changed, 1070 insertions(+), 4 deletions(-) create mode 100644 plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceConstant/IntroduceConstantAction.kt create mode 100644 plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceConstant/KotlinIntroduceConstantHandler.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerCompareTo.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerCompareTo.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDec.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDec.kt.conflicts create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDiv.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDiv.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerEquals.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerEquals.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerInc.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerInc.kt.conflicts create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerMinus.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerMinus.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlus.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlus.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlusPartNotConst.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlusPartNotConst.kt.conflicts create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerRem.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerRem.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerTimes.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerTimes.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryMinus.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryMinus.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryPlus.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryPlus.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesis.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesis.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesisBroken.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesisBroken.kt.conflicts create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCompareTo.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCompareTo.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCustomOperator.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCustomOperator.kt.conflicts create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringEquals.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringEquals.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlus.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlus.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlusPartNotConst.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlusPartNotConst.kt.conflicts create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/and.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/and.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/inv.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/inv.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/or.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/or.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/plus.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/plus.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shl.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shl.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shr.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shr.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toByte.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toByte.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toFloat.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toFloat.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/ushr.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/ushr.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/xor.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/xor.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractInteger.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractInteger.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractMultipleExpressions.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractMultipleExpressions.kt.conflicts create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractPartNonConstVal.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractPartNonConstVal.kt.conflicts create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractWithNameClash.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractWithNameClash.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithBlockExpr.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithBlockExpr.kt.conflicts create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithExpr.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithExpr.kt.conflicts create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEscapeEntry.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEscapeEntry.kt.conflicts create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractFalse.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractFalse.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractIntegerLiteral.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractIntegerLiteral.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractTrue.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractTrue.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/partTemplate.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/partTemplate.kt.conflicts create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntryPrefix.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntryPrefix.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySubstring.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySubstring.kt.after create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySuffix.kt create mode 100644 plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySuffix.kt.after diff --git a/plugins/kotlin/generators/test/org/jetbrains/kotlin/testGenerator/GenerateTests.kt b/plugins/kotlin/generators/test/org/jetbrains/kotlin/testGenerator/GenerateTests.kt index c7cce43b9e3e..8c6428c6d7df 100644 --- a/plugins/kotlin/generators/test/org/jetbrains/kotlin/testGenerator/GenerateTests.kt +++ b/plugins/kotlin/generators/test/org/jetbrains/kotlin/testGenerator/GenerateTests.kt @@ -840,6 +840,7 @@ private fun assembleWorkspace(): TWorkspace = workspace { model("refactoring/introduceJavaParameter", pattern = JAVA, testMethodName = "doIntroduceJavaParameterTest") model("refactoring/introduceTypeParameter", pattern = KT_OR_KTS, testMethodName = "doIntroduceTypeParameterTest") model("refactoring/introduceTypeAlias", pattern = KT_OR_KTS, testMethodName = "doIntroduceTypeAliasTest") + model("refactoring/introduceConstant", pattern = KT_OR_KTS, testMethodName = "doIntroduceConstantTest") model("refactoring/extractSuperclass", pattern = KT_OR_KTS_WITHOUT_DOTS, testMethodName = "doExtractSuperclassTest") model("refactoring/extractInterface", pattern = KT_OR_KTS_WITHOUT_DOTS, testMethodName = "doExtractInterfaceTest") } diff --git a/plugins/kotlin/idea/resources-en/messages/KotlinBundle.properties b/plugins/kotlin/idea/resources-en/messages/KotlinBundle.properties index 6c9e33069f0c..724e4a0c06f3 100644 --- a/plugins/kotlin/idea/resources-en/messages/KotlinBundle.properties +++ b/plugins/kotlin/idea/resources-en/messages/KotlinBundle.properties @@ -769,6 +769,8 @@ error.text.can.t.copy.class.to.the.containing.file=Can't copy class to the conta error.text.can.t.generate.0.1=Can''t generate {0}: {1} error.text.can.t.introduce.lambda.parameter.for.this.expression=Can't introduce lambda parameter for this expression error.text.can.t.introduce.property.for.this.expression=Can't introduce property for this expression +error.text.can.t.introduce.constant.for.this.expression.because.not.constant=Can't introduce constant for non constant expression +error.text.can.t.introduce.constant.for.this.expression=Can't introduce constant for this expression error.text.different.name.expected=Different name expected error.text.expression.has.no.type=Expression has no type error.text.extraction.from.expect.class.is.not.yet.supported=Extraction from expect class is not yet supported @@ -799,6 +801,7 @@ introduce.property=Introduce Property introduce.type.parameter.to.declaration=Introduce type parameter to declaration introduce.type.parameter=Introduce Type Parameter introduce.variable=Introduce Variable +introduce.constant=Introduce Constant label.text.default.receiver.value=&Default receiver value:\u0020 label.text.destination.directory=&Destination directory: label.text.file.name=File &name: diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/KotlinRefactoringSupportProvider.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/KotlinRefactoringSupportProvider.kt index 5d8b28706e7f..e56ca0031198 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/KotlinRefactoringSupportProvider.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/KotlinRefactoringSupportProvider.kt @@ -11,6 +11,7 @@ import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinChangeSignatu import org.jetbrains.kotlin.idea.refactoring.introduce.extractClass.KotlinExtractInterfaceHandler import org.jetbrains.kotlin.idea.refactoring.introduce.extractClass.KotlinExtractSuperclassHandler import org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.ExtractKotlinFunctionHandler +import org.jetbrains.kotlin.idea.refactoring.introduce.introduceConstant.KotlinIntroduceConstantHandler import org.jetbrains.kotlin.idea.refactoring.introduce.introduceParameter.KotlinIntroduceLambdaParameterHandler import org.jetbrains.kotlin.idea.refactoring.introduce.introduceParameter.KotlinIntroduceParameterHandler import org.jetbrains.kotlin.idea.refactoring.introduce.introduceProperty.KotlinIntroducePropertyHandler @@ -31,6 +32,8 @@ class KotlinRefactoringSupportProvider : RefactoringSupportProvider() { override fun getIntroduceFunctionalParameterHandler() = KotlinIntroduceLambdaParameterHandler() + override fun getIntroduceConstantHandler() = KotlinIntroduceConstantHandler() + fun getIntroducePropertyHandler(): RefactoringActionHandler = KotlinIntroducePropertyHandler() fun getIntroduceTypeParameterHandler(): RefactoringActionHandler = KotlinIntroduceTypeParameterHandler diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/ExtractableCodeDescriptor.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/ExtractableCodeDescriptor.kt index 86b690691614..c3451649cccd 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/ExtractableCodeDescriptor.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/ExtractableCodeDescriptor.kt @@ -462,7 +462,8 @@ data class ExtractionGeneratorOptions( val target: ExtractionTarget = ExtractionTarget.FUNCTION, val dummyName: String? = null, val allowExpressionBody: Boolean = true, - val delayInitialOccurrenceReplacement: Boolean = false + val delayInitialOccurrenceReplacement: Boolean = false, + val isConst: Boolean = false ) { companion object { @JvmField diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/extractorUtil.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/extractorUtil.kt index 16feea4f8db2..de42b78e797d 100644 --- a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/extractorUtil.kt +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/extractionEngine/extractorUtil.kt @@ -13,8 +13,8 @@ import com.intellij.refactoring.BaseRefactoringProcessor import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.isFunctionType import org.jetbrains.kotlin.descriptors.ClassDescriptor -import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.descriptors.DescriptorVisibilities +import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.core.* import org.jetbrains.kotlin.idea.core.util.isMultiLine @@ -34,7 +34,6 @@ import org.jetbrains.kotlin.idea.util.getResolutionScope import org.jetbrains.kotlin.idea.util.psi.patternMatching.* import org.jetbrains.kotlin.idea.util.psi.patternMatching.UnificationResult.StronglyMatched import org.jetbrains.kotlin.idea.util.psi.patternMatching.UnificationResult.WeaklyMatched -import org.jetbrains.kotlin.idea.util.reformatted import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.* @@ -75,7 +74,8 @@ private fun buildSignature(config: ExtractionGeneratorConfiguration, renderer: D config.descriptor.annotations.joinToString(separator = "\n", postfix = "\n") { renderer.renderAnnotation(it) } } val extraModifiers = config.descriptor.modifiers.map { it.value } + - listOfNotNull(if (shouldBeInline) KtTokens.INLINE_KEYWORD.value else null) + listOfNotNull(if (shouldBeInline) KtTokens.INLINE_KEYWORD.value else null) + + listOfNotNull(if (config.generatorOptions.isConst) KtTokens.CONST_KEYWORD.value else null) val modifiers = if (visibility.isNotEmpty()) listOf(visibility) + extraModifiers else extraModifiers modifier(annotations + modifiers.joinToString(separator = " ")) diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceConstant/IntroduceConstantAction.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceConstant/IntroduceConstantAction.kt new file mode 100644 index 000000000000..49e719be13a2 --- /dev/null +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceConstant/IntroduceConstantAction.kt @@ -0,0 +1,14 @@ +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + +package org.jetbrains.kotlin.idea.refactoring.introduce.introduceConstant + +import com.intellij.lang.refactoring.RefactoringSupportProvider +import com.intellij.refactoring.RefactoringActionHandler +import org.jetbrains.kotlin.idea.refactoring.KotlinRefactoringSupportProvider +import org.jetbrains.kotlin.idea.refactoring.introduce.AbstractIntroduceAction +import org.jetbrains.kotlin.utils.addToStdlib.safeAs + +class IntroduceConstantAction : AbstractIntroduceAction() { + override fun getRefactoringHandler(provider: RefactoringSupportProvider): RefactoringActionHandler? = + provider.safeAs()?.introduceConstantHandler +} diff --git a/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceConstant/KotlinIntroduceConstantHandler.kt b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceConstant/KotlinIntroduceConstantHandler.kt new file mode 100644 index 000000000000..24cab881602a --- /dev/null +++ b/plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/introduce/introduceConstant/KotlinIntroduceConstantHandler.kt @@ -0,0 +1,174 @@ +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + +package org.jetbrains.kotlin.idea.refactoring.introduce.introduceConstant + +import com.intellij.openapi.actionSystem.DataContext +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiDocumentManager +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import com.intellij.psi.codeStyle.NameUtil +import com.intellij.refactoring.RefactoringActionHandler +import org.jetbrains.annotations.Nls +import org.jetbrains.kotlin.idea.KotlinBundle +import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.core.util.CodeInsightUtils +import org.jetbrains.kotlin.idea.refactoring.getExtractionContainers +import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.* +import org.jetbrains.kotlin.idea.refactoring.introduce.introduceProperty.KotlinInplacePropertyIntroducer +import org.jetbrains.kotlin.idea.refactoring.introduce.selectElementsWithTargetSibling +import org.jetbrains.kotlin.idea.refactoring.introduce.showErrorHint +import org.jetbrains.kotlin.idea.refactoring.introduce.showErrorHintByKey +import org.jetbrains.kotlin.idea.refactoring.introduce.validateExpressionElements +import org.jetbrains.kotlin.idea.util.application.isUnitTestMode +import org.jetbrains.kotlin.idea.util.psi.patternMatching.toRange +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.plainContent +import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode +import org.jetbrains.kotlin.utils.addToStdlib.safeAs + +class KotlinIntroduceConstantHandler( + val helper: ExtractionEngineHelper = InteractiveExtractionHelper +) : RefactoringActionHandler { + object InteractiveExtractionHelper : ExtractionEngineHelper(INTRODUCE_CONSTANT) { + private fun getExtractionTarget(descriptor: ExtractableCodeDescriptor) = + propertyTargets.firstOrNull { it.isAvailable(descriptor) } + + override fun validate(descriptor: ExtractableCodeDescriptor) = + descriptor.validate(getExtractionTarget(descriptor) ?: ExtractionTarget.FUNCTION) + + override fun configureAndRun( + project: Project, + editor: Editor, + descriptorWithConflicts: ExtractableCodeDescriptorWithConflicts, + onFinish: (ExtractionResult) -> Unit + ) { + val descriptor = descriptorWithConflicts.descriptor + val target = getExtractionTarget(descriptor) + if (target != null) { + val options = ExtractionGeneratorOptions(target = target, delayInitialOccurrenceReplacement = true, isConst = true) + doRefactor(ExtractionGeneratorConfiguration(descriptor, options), onFinish) + } else { + showErrorHint( + project, + editor, + KotlinBundle.message("error.text.can.t.introduce.constant.for.this.expression"), + INTRODUCE_CONSTANT + ) + } + } + } + + fun doInvoke(project: Project, editor: Editor, file: KtFile, elements: List, target: PsiElement) { + val adjustedElements = (elements.singleOrNull() as? KtBlockExpression)?.statements ?: elements + when { + adjustedElements.isEmpty() -> { + showErrorHintByKey( + project, editor, "cannot.refactor.no.expression", + INTRODUCE_CONSTANT + ) + } + else -> { + val options = ExtractionOptions(extractAsProperty = true) + val extractionData = ExtractionData(file, adjustedElements.toRange(), target, null, options) + ExtractionEngine(helper).run(editor, extractionData) { + val property = it.declaration as KtProperty + val descriptor = it.config.descriptor + + editor.caretModel.moveToOffset(property.textOffset) + editor.selectionModel.removeSelection() + if (editor.settings.isVariableInplaceRenameEnabled && !isUnitTestMode()) { + with(PsiDocumentManager.getInstance(project)) { + commitDocument(editor.document) + doPostponedOperationsAndUnblockDocument(editor.document) + } + + val introducer = KotlinInplacePropertyIntroducer( + property = property, + editor = editor, + project = project, + title = INTRODUCE_CONSTANT, + doNotChangeVar = false, + exprType = descriptor.returnType, + extractionResult = it, + availableTargets = listOf(ExtractionTarget.PROPERTY_WITH_GETTER) + ) + introducer.performInplaceRefactoring(LinkedHashSet(getNameSuggestions(property) + descriptor.suggestedNames)) + } else { + processDuplicatesSilently(it.duplicateReplacers, project) + } + } + } + } + } + + private fun getNameSuggestions(property: KtProperty): List { + val initializerValue = property.initializer.safeAs()?.plainContent + val identifierValue = property.identifyingElement?.text + + return listOfNotNull(initializerValue, identifierValue).map { NameUtil.capitalizeAndUnderscore(it) } + } + + override fun invoke(project: Project, editor: Editor, file: PsiFile, dataContext: DataContext?) { + if (file !is KtFile) return + selectElements(editor, file) { elements, targets -> doInvoke(project, editor, file, elements, targets) } + } + + fun selectElements( + editor: Editor, + file: KtFile, + continuation: (elements: List, targets: PsiElement) -> Unit + ) { + + selectElementsWithTargetSibling( + INTRODUCE_CONSTANT, + editor, + file, + KotlinBundle.message("title.select.target.code.block"), + listOf(CodeInsightUtils.ElementKind.EXPRESSION), + ::validateElements, + { _, sibling -> + sibling.getExtractionContainers(strict = true, includeAll = true) + .filter { (it is KtFile && !it.isScript()) } + }, + continuation + ) + } + + private fun validateElements(elements: List): String? { + val errorMessage = validateExpressionElements(elements) + return when { + errorMessage != null -> errorMessage + elements.any { + // unchecked cast always succeeds because only expressions are selected in selectElements + (it as KtExpression).isNotConst() + } -> KotlinBundle.message( + "error.text.can.t.introduce.constant.for.this.expression.because.not.constant" + ) + else -> null + } + } + + private fun KtExpression.isNotConst(): Boolean { + when (this) { + // Handle these two expressions separately because in case of selecting part of a string + // a temp file will be created in which the analysis fails + is KtConstantExpression -> return false + is KtStringTemplateExpression -> return this.hasInterpolation() + else -> { + val constInfo = ConstantExpressionEvaluator.getConstant(this, analyze(BodyResolveMode.PARTIAL)) + return constInfo == null || constInfo.usesNonConstValAsConstant || constInfo.usesVariableAsConstant + } + } + } + + override fun invoke(project: Project, elements: Array, dataContext: DataContext?) { + throw AssertionError("$INTRODUCE_CONSTANT can only be invoked from editor") + } +} + +val INTRODUCE_CONSTANT: String + @Nls + get() = KotlinBundle.message("introduce.constant") diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/introduce/AbstractExtractionTest.kt b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/introduce/AbstractExtractionTest.kt index e3aeee62a173..89e9bc42c77c 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/introduce/AbstractExtractionTest.kt +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/introduce/AbstractExtractionTest.kt @@ -37,6 +37,8 @@ import org.jetbrains.kotlin.idea.refactoring.introduce.extractClass.ExtractSuper import org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.EXTRACT_FUNCTION import org.jetbrains.kotlin.idea.refactoring.introduce.extractFunction.ExtractKotlinFunctionHandler import org.jetbrains.kotlin.idea.refactoring.introduce.extractionEngine.* +import org.jetbrains.kotlin.idea.refactoring.introduce.introduceConstant.INTRODUCE_CONSTANT +import org.jetbrains.kotlin.idea.refactoring.introduce.introduceConstant.KotlinIntroduceConstantHandler import org.jetbrains.kotlin.idea.refactoring.introduce.introduceParameter.* import org.jetbrains.kotlin.idea.refactoring.introduce.introduceProperty.INTRODUCE_PROPERTY import org.jetbrains.kotlin.idea.refactoring.introduce.introduceProperty.KotlinIntroducePropertyHandler @@ -281,6 +283,39 @@ abstract class AbstractExtractionTest : KotlinLightCodeInsightFixtureTestCase() } } + protected fun doIntroduceConstantTest(unused: String) { + doTest { file -> + file as KtFile + + val extractionTarget = propertyTargets.single { + it.targetName == InTextDirectivesUtils.findStringWithPrefixes(file.getText(), "// EXTRACTION_TARGET: ") + } + + val helper = object : ExtractionEngineHelper(INTRODUCE_CONSTANT) { + override fun configureAndRun( + project: Project, + editor: Editor, + descriptorWithConflicts: ExtractableCodeDescriptorWithConflicts, + onFinish: (ExtractionResult) -> Unit + ) { + doRefactor( + ExtractionGeneratorConfiguration( + descriptorWithConflicts.descriptor, + ExtractionGeneratorOptions(target = extractionTarget, delayInitialOccurrenceReplacement = true, isConst = true) + ), + onFinish + ) + } + } + + val handler = KotlinIntroduceConstantHandler(helper) + val editor = fixture.editor + handler.selectElements(editor, file) { elements, target -> + handler.doInvoke(project, editor, file, elements, target) + } + } + } + protected fun doExtractSuperTest(unused: String, isInterface: Boolean) { doTest(true) { file -> file as KtFile diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java index e672f601e4f2..3109c6a17f27 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/refactoring/introduce/ExtractionTestGenerated.java @@ -3696,6 +3696,253 @@ public abstract class ExtractionTestGenerated extends AbstractExtractionTest { } } + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/refactoring/introduceConstant") + public static class IntroduceConstant extends AbstractExtractionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doIntroduceConstantTest, this, testDataFilePath); + } + + @TestMetadata("extractInteger.kt") + public void testExtractInteger() throws Exception { + runTest("testData/refactoring/introduceConstant/extractInteger.kt"); + } + + @TestMetadata("extractMultipleExpressions.kt") + public void testExtractMultipleExpressions() throws Exception { + runTest("testData/refactoring/introduceConstant/extractMultipleExpressions.kt"); + } + + @TestMetadata("extractPartNonConstVal.kt") + public void testExtractPartNonConstVal() throws Exception { + runTest("testData/refactoring/introduceConstant/extractPartNonConstVal.kt"); + } + + @TestMetadata("extractWithNameClash.kt") + public void testExtractWithNameClash() throws Exception { + runTest("testData/refactoring/introduceConstant/extractWithNameClash.kt"); + } + + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/refactoring/introduceConstant/binaryExpression") + public static class BinaryExpression extends AbstractExtractionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doIntroduceConstantTest, this, testDataFilePath); + } + + @TestMetadata("integerCompareTo.kt") + public void testIntegerCompareTo() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/integerCompareTo.kt"); + } + + @TestMetadata("integerDec.kt") + public void testIntegerDec() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/integerDec.kt"); + } + + @TestMetadata("integerDiv.kt") + public void testIntegerDiv() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/integerDiv.kt"); + } + + @TestMetadata("integerEquals.kt") + public void testIntegerEquals() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/integerEquals.kt"); + } + + @TestMetadata("integerInc.kt") + public void testIntegerInc() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/integerInc.kt"); + } + + @TestMetadata("integerMinus.kt") + public void testIntegerMinus() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/integerMinus.kt"); + } + + @TestMetadata("integerPlus.kt") + public void testIntegerPlus() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/integerPlus.kt"); + } + + @TestMetadata("integerPlusPartNotConst.kt") + public void testIntegerPlusPartNotConst() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/integerPlusPartNotConst.kt"); + } + + @TestMetadata("integerRem.kt") + public void testIntegerRem() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/integerRem.kt"); + } + + @TestMetadata("integerTimes.kt") + public void testIntegerTimes() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/integerTimes.kt"); + } + + @TestMetadata("integerUnaryMinus.kt") + public void testIntegerUnaryMinus() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/integerUnaryMinus.kt"); + } + + @TestMetadata("integerUnaryPlus.kt") + public void testIntegerUnaryPlus() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/integerUnaryPlus.kt"); + } + + @TestMetadata("paranthesis.kt") + public void testParanthesis() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/paranthesis.kt"); + } + + @TestMetadata("paranthesisBroken.kt") + public void testParanthesisBroken() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/paranthesisBroken.kt"); + } + + @TestMetadata("stringCompareTo.kt") + public void testStringCompareTo() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/stringCompareTo.kt"); + } + + @TestMetadata("stringCustomOperator.kt") + public void testStringCustomOperator() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/stringCustomOperator.kt"); + } + + @TestMetadata("stringEquals.kt") + public void testStringEquals() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/stringEquals.kt"); + } + + @TestMetadata("stringPlus.kt") + public void testStringPlus() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/stringPlus.kt"); + } + + @TestMetadata("stringPlusPartNotConst.kt") + public void testStringPlusPartNotConst() throws Exception { + runTest("testData/refactoring/introduceConstant/binaryExpression/stringPlusPartNotConst.kt"); + } + } + + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/refactoring/introduceConstant/dotQualifiedExpression") + public static class DotQualifiedExpression extends AbstractExtractionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doIntroduceConstantTest, this, testDataFilePath); + } + + @TestMetadata("and.kt") + public void testAnd() throws Exception { + runTest("testData/refactoring/introduceConstant/dotQualifiedExpression/and.kt"); + } + + @TestMetadata("inv.kt") + public void testInv() throws Exception { + runTest("testData/refactoring/introduceConstant/dotQualifiedExpression/inv.kt"); + } + + @TestMetadata("or.kt") + public void testOr() throws Exception { + runTest("testData/refactoring/introduceConstant/dotQualifiedExpression/or.kt"); + } + + @TestMetadata("plus.kt") + public void testPlus() throws Exception { + runTest("testData/refactoring/introduceConstant/dotQualifiedExpression/plus.kt"); + } + + @TestMetadata("shl.kt") + public void testShl() throws Exception { + runTest("testData/refactoring/introduceConstant/dotQualifiedExpression/shl.kt"); + } + + @TestMetadata("shr.kt") + public void testShr() throws Exception { + runTest("testData/refactoring/introduceConstant/dotQualifiedExpression/shr.kt"); + } + + @TestMetadata("toByte.kt") + public void testToByte() throws Exception { + runTest("testData/refactoring/introduceConstant/dotQualifiedExpression/toByte.kt"); + } + + @TestMetadata("toFloat.kt") + public void testToFloat() throws Exception { + runTest("testData/refactoring/introduceConstant/dotQualifiedExpression/toFloat.kt"); + } + + @TestMetadata("ushr.kt") + public void testUshr() throws Exception { + runTest("testData/refactoring/introduceConstant/dotQualifiedExpression/ushr.kt"); + } + + @TestMetadata("xor.kt") + public void testXor() throws Exception { + runTest("testData/refactoring/introduceConstant/dotQualifiedExpression/xor.kt"); + } + } + + @RunWith(JUnit3RunnerWithInners.class) + @TestMetadata("testData/refactoring/introduceConstant/stringTemplates") + public static class StringTemplates extends AbstractExtractionTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doIntroduceConstantTest, this, testDataFilePath); + } + + @TestMetadata("brokenEntryWithBlockExpr.kt") + public void testBrokenEntryWithBlockExpr() throws Exception { + runTest("testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithBlockExpr.kt"); + } + + @TestMetadata("brokenEntryWithExpr.kt") + public void testBrokenEntryWithExpr() throws Exception { + runTest("testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithExpr.kt"); + } + + @TestMetadata("brokenEscapeEntry.kt") + public void testBrokenEscapeEntry() throws Exception { + runTest("testData/refactoring/introduceConstant/stringTemplates/brokenEscapeEntry.kt"); + } + + @TestMetadata("extractFalse.kt") + public void testExtractFalse() throws Exception { + runTest("testData/refactoring/introduceConstant/stringTemplates/extractFalse.kt"); + } + + @TestMetadata("extractIntegerLiteral.kt") + public void testExtractIntegerLiteral() throws Exception { + runTest("testData/refactoring/introduceConstant/stringTemplates/extractIntegerLiteral.kt"); + } + + @TestMetadata("extractTrue.kt") + public void testExtractTrue() throws Exception { + runTest("testData/refactoring/introduceConstant/stringTemplates/extractTrue.kt"); + } + + @TestMetadata("partTemplate.kt") + public void testPartTemplate() throws Exception { + runTest("testData/refactoring/introduceConstant/stringTemplates/partTemplate.kt"); + } + + @TestMetadata("singleEntryPrefix.kt") + public void testSingleEntryPrefix() throws Exception { + runTest("testData/refactoring/introduceConstant/stringTemplates/singleEntryPrefix.kt"); + } + + @TestMetadata("singleEntrySubstring.kt") + public void testSingleEntrySubstring() throws Exception { + runTest("testData/refactoring/introduceConstant/stringTemplates/singleEntrySubstring.kt"); + } + + @TestMetadata("singleEntrySuffix.kt") + public void testSingleEntrySuffix() throws Exception { + runTest("testData/refactoring/introduceConstant/stringTemplates/singleEntrySuffix.kt"); + } + } + } + @RunWith(JUnit3RunnerWithInners.class) @TestMetadata("testData/refactoring/extractSuperclass") public static class ExtractSuperclass extends AbstractExtractionTest { diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerCompareTo.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerCompareTo.kt new file mode 100644 index 000000000000..b645341fcbf2 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerCompareTo.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Boolean { + return 1>1 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerCompareTo.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerCompareTo.kt.after new file mode 100644 index 000000000000..60f7daf0ee64 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerCompareTo.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val b = 1 > 1 + +fun foo(): Boolean { + return b +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDec.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDec.kt new file mode 100644 index 000000000000..c6eb8902cadd --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDec.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1-- +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDec.kt.conflicts b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDec.kt.conflicts new file mode 100644 index 000000000000..6042be53b1be --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDec.kt.conflicts @@ -0,0 +1 @@ +Can't introduce constant for non constant expression diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDiv.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDiv.kt new file mode 100644 index 000000000000..7736f2735ed5 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDiv.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1/1 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDiv.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDiv.kt.after new file mode 100644 index 000000000000..cdb17439f2b4 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerDiv.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1 / 1 + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerEquals.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerEquals.kt new file mode 100644 index 000000000000..d94c0bafe547 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerEquals.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Boolean { + return 1 == 1 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerEquals.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerEquals.kt.after new file mode 100644 index 000000000000..535da83b1fd3 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerEquals.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val b = 1 == 1 + +fun foo(): Boolean { + return b +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerInc.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerInc.kt new file mode 100644 index 000000000000..6a10fbed97a2 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerInc.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1++ +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerInc.kt.conflicts b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerInc.kt.conflicts new file mode 100644 index 000000000000..6042be53b1be --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerInc.kt.conflicts @@ -0,0 +1 @@ +Can't introduce constant for non constant expression diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerMinus.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerMinus.kt new file mode 100644 index 000000000000..8d7c1c5e08aa --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerMinus.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1-1 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerMinus.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerMinus.kt.after new file mode 100644 index 000000000000..326dd4817dc3 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerMinus.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1 - 1 + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlus.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlus.kt new file mode 100644 index 000000000000..eb6030a44dd4 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlus.kt @@ -0,0 +1,7 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + val x = "1"+"1" + return 1+1 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlus.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlus.kt.after new file mode 100644 index 000000000000..6f72dab7f54f --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlus.kt.after @@ -0,0 +1,9 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1 + 1 + +fun foo(): Int { + val x = "1"+"1" + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlusPartNotConst.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlusPartNotConst.kt new file mode 100644 index 000000000000..4ac5a7333c4f --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlusPartNotConst.kt @@ -0,0 +1,10 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1+getNumber() +} + +fun getNumber(): Int { + return 1 +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlusPartNotConst.kt.conflicts b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlusPartNotConst.kt.conflicts new file mode 100644 index 000000000000..6042be53b1be --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerPlusPartNotConst.kt.conflicts @@ -0,0 +1 @@ +Can't introduce constant for non constant expression diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerRem.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerRem.kt new file mode 100644 index 000000000000..d6accf0bf79a --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerRem.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1%1 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerRem.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerRem.kt.after new file mode 100644 index 000000000000..fff2b2fabcaf --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerRem.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1 % 1 + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerTimes.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerTimes.kt new file mode 100644 index 000000000000..0b5a7e6ab085 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerTimes.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1*1 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerTimes.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerTimes.kt.after new file mode 100644 index 000000000000..59b39f5cc4b1 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerTimes.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1 * 1 + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryMinus.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryMinus.kt new file mode 100644 index 000000000000..13e5145b7757 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryMinus.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return -1 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryMinus.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryMinus.kt.after new file mode 100644 index 000000000000..66c4ea6b58a9 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryMinus.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = -1 + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryPlus.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryPlus.kt new file mode 100644 index 000000000000..560c79f284f1 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryPlus.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return +1 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryPlus.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryPlus.kt.after new file mode 100644 index 000000000000..25c800a50de8 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/integerUnaryPlus.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = +1 + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesis.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesis.kt new file mode 100644 index 000000000000..4248d42b7fcb --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesis.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return (1+1)*2 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesis.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesis.kt.after new file mode 100644 index 000000000000..1ae7d5353a57 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesis.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = (1 + 1) * 2 + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesisBroken.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesisBroken.kt new file mode 100644 index 000000000000..ee45d59156a5 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesisBroken.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return (1+1)*2 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesisBroken.kt.conflicts b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesisBroken.kt.conflicts new file mode 100644 index 000000000000..072400d731d7 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/paranthesisBroken.kt.conflicts @@ -0,0 +1 @@ +Cannot perform refactoring without an expression diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCompareTo.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCompareTo.kt new file mode 100644 index 000000000000..b8f50d24078f --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCompareTo.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): String { + return "1">"1" +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCompareTo.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCompareTo.kt.after new file mode 100644 index 000000000000..e3f4c32e1469 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCompareTo.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val b = "1" > "1" + +fun foo(): String { + return b +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCustomOperator.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCustomOperator.kt new file mode 100644 index 000000000000..adeff7cdd6dc --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCustomOperator.kt @@ -0,0 +1,7 @@ +// EXTRACTION_TARGET: property with initializer + +fun foo(): String { + return "1"-1 +} + +operator fun String.minus(i: Int): String = this.substring(0, length - i) diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCustomOperator.kt.conflicts b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCustomOperator.kt.conflicts new file mode 100644 index 000000000000..6042be53b1be --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringCustomOperator.kt.conflicts @@ -0,0 +1 @@ +Can't introduce constant for non constant expression diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringEquals.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringEquals.kt new file mode 100644 index 000000000000..17f8994a4885 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringEquals.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): String { + return "1" == "1" +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringEquals.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringEquals.kt.after new file mode 100644 index 000000000000..3e8ba282eddd --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringEquals.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val b = "1" == "1" + +fun foo(): String { + return b +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlus.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlus.kt new file mode 100644 index 000000000000..9980a5100a5a --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlus.kt @@ -0,0 +1,7 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): String { + val x = 1 + 1 + return "1"+"1" +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlus.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlus.kt.after new file mode 100644 index 000000000000..e7f40ae33126 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlus.kt.after @@ -0,0 +1,9 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val s = "1" + "1" + +fun foo(): String { + val x = 1 + 1 + return s +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlusPartNotConst.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlusPartNotConst.kt new file mode 100644 index 000000000000..8d53cb9ba066 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlusPartNotConst.kt @@ -0,0 +1,10 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): String { + return "1"+getString() +} + +fun getString(): String { + return "1" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlusPartNotConst.kt.conflicts b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlusPartNotConst.kt.conflicts new file mode 100644 index 000000000000..6042be53b1be --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/binaryExpression/stringPlusPartNotConst.kt.conflicts @@ -0,0 +1 @@ +Can't introduce constant for non constant expression diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/and.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/and.kt new file mode 100644 index 000000000000..47fa8f7594ba --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/and.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1.and(1) +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/and.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/and.kt.after new file mode 100644 index 000000000000..edf44b2dda67 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/and.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1.and(1) + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/inv.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/inv.kt new file mode 100644 index 000000000000..ac3867e756c5 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/inv.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1.inv(1) +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/inv.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/inv.kt.after new file mode 100644 index 000000000000..c476bde90162 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/inv.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1.inv(1) + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/or.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/or.kt new file mode 100644 index 000000000000..b14369986355 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/or.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1.or(1) +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/or.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/or.kt.after new file mode 100644 index 000000000000..f71352219bfa --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/or.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1.or(1) + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/plus.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/plus.kt new file mode 100644 index 000000000000..2a35e1154085 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/plus.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1.plus(1) +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/plus.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/plus.kt.after new file mode 100644 index 000000000000..29ce1585fff7 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/plus.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1.plus(1) + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shl.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shl.kt new file mode 100644 index 000000000000..8dd146f436ee --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shl.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1.shl(1) +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shl.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shl.kt.after new file mode 100644 index 000000000000..3d350b0e6a4e --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shl.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1.shl(1) + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shr.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shr.kt new file mode 100644 index 000000000000..03584ce43895 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shr.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1.shr(1) +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shr.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shr.kt.after new file mode 100644 index 000000000000..b40c5f8214b8 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/shr.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1.shr(1) + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toByte.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toByte.kt new file mode 100644 index 000000000000..7511a89d2770 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toByte.kt @@ -0,0 +1,7 @@ +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Byte { + return 1.toByte() +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toByte.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toByte.kt.after new file mode 100644 index 000000000000..2c76f078e540 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toByte.kt.after @@ -0,0 +1,9 @@ +// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val byte = 1.toByte() + +fun foo(): Byte { + return byte +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toFloat.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toFloat.kt new file mode 100644 index 000000000000..3d0eb745200f --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toFloat.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1.toFloat() +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toFloat.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toFloat.kt.after new file mode 100644 index 000000000000..66c88a00ff27 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/toFloat.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val fl = 1.toFloat() + +fun foo(): Int { + return fl +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/ushr.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/ushr.kt new file mode 100644 index 000000000000..fcfc1cdc2474 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/ushr.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1.ushr(1) +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/ushr.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/ushr.kt.after new file mode 100644 index 000000000000..e3f0e2136053 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/ushr.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1.ushr(1) + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/xor.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/xor.kt new file mode 100644 index 000000000000..f5d42453ceae --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/xor.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + return 1.xor(1) +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/xor.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/xor.kt.after new file mode 100644 index 000000000000..7bece17d92dd --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/dotQualifiedExpression/xor.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 1.xor(1) + +fun foo(): Int { + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractInteger.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractInteger.kt new file mode 100644 index 000000000000..2bade67ed5fe --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractInteger.kt @@ -0,0 +1,9 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): Int { + val x = 2 + 3 + val y = 3 + 2 + 22 + val z = "2" + return 2 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractInteger.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractInteger.kt.after new file mode 100644 index 000000000000..3de721c041d9 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractInteger.kt.after @@ -0,0 +1,11 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 2 + +fun foo(): Int { + val x = i + 3 + val y = 3 + i + 22 + val z = "2" + return i +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractMultipleExpressions.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractMultipleExpressions.kt new file mode 100644 index 000000000000..c0c4841ab463 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractMultipleExpressions.kt @@ -0,0 +1,10 @@ +// EXTRACTION_TARGET: property with initializer +class A { + fun foo(): Int { + + val a = 1 + 2 + val b = a * 2 + val c = b - 1 + } +} + diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractMultipleExpressions.kt.conflicts b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractMultipleExpressions.kt.conflicts new file mode 100644 index 000000000000..6042be53b1be --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractMultipleExpressions.kt.conflicts @@ -0,0 +1 @@ +Can't introduce constant for non constant expression diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractPartNonConstVal.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractPartNonConstVal.kt new file mode 100644 index 000000000000..9a897626cd7a --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractPartNonConstVal.kt @@ -0,0 +1,7 @@ +// EXTRACTION_TARGET: property with initializer + +val n: Int = 1 + +fun foo(): Int { + return n+1 +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractPartNonConstVal.kt.conflicts b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractPartNonConstVal.kt.conflicts new file mode 100644 index 000000000000..6042be53b1be --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractPartNonConstVal.kt.conflicts @@ -0,0 +1 @@ +Can't introduce constant for non constant expression diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractWithNameClash.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractWithNameClash.kt new file mode 100644 index 000000000000..011b0bfb59c3 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractWithNameClash.kt @@ -0,0 +1,7 @@ +// EXTRACTION_TARGET: property with initializer +val i = 1 + +fun foo(): Int { + return 2 +} + diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractWithNameClash.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractWithNameClash.kt.after new file mode 100644 index 000000000000..3a93aef24d82 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/extractWithNameClash.kt.after @@ -0,0 +1,8 @@ +// EXTRACTION_TARGET: property with initializer +val i = 1 + +private const val i1 = 2 + +fun foo(): Int { + return i1 +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithBlockExpr.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithBlockExpr.kt new file mode 100644 index 000000000000..8f9d390a6580 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithBlockExpr.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val prop = 1 + +fun foo(): String { + return "abc${pr < / selection > op}def" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithBlockExpr.kt.conflicts b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithBlockExpr.kt.conflicts new file mode 100644 index 000000000000..072400d731d7 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithBlockExpr.kt.conflicts @@ -0,0 +1 @@ +Cannot perform refactoring without an expression diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithExpr.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithExpr.kt new file mode 100644 index 000000000000..e0f5e75e69f4 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithExpr.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val prop = 1 + +fun foo(): String { + return "abc$prop" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithExpr.kt.conflicts b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithExpr.kt.conflicts new file mode 100644 index 000000000000..072400d731d7 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEntryWithExpr.kt.conflicts @@ -0,0 +1 @@ +Cannot perform refactoring without an expression diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEscapeEntry.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEscapeEntry.kt new file mode 100644 index 000000000000..d0010dcff5fb --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEscapeEntry.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): String { + return "abc$a\ndef" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEscapeEntry.kt.conflicts b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEscapeEntry.kt.conflicts new file mode 100644 index 000000000000..072400d731d7 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/brokenEscapeEntry.kt.conflicts @@ -0,0 +1 @@ +Cannot perform refactoring without an expression diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractFalse.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractFalse.kt new file mode 100644 index 000000000000..3a9728d36c37 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractFalse.kt @@ -0,0 +1,9 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): String { + val x = "xyfalsez" + val y = "xyFalsez" + val z = false + return "abfalsedef" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractFalse.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractFalse.kt.after new file mode 100644 index 000000000000..1c68e94e5129 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractFalse.kt.after @@ -0,0 +1,11 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val b = false + +fun foo(): String { + val x = "xy${b}z" + val y = "xyFalsez" + val z = false + return "ab${b}def" +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractIntegerLiteral.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractIntegerLiteral.kt new file mode 100644 index 000000000000..72e86f108522 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractIntegerLiteral.kt @@ -0,0 +1,10 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): String { + val x = "a1234_" + val y = "-4123a" + val z = "+1243a" + val u = 123 + return "ab123def" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractIntegerLiteral.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractIntegerLiteral.kt.after new file mode 100644 index 000000000000..4273955bd755 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractIntegerLiteral.kt.after @@ -0,0 +1,12 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val i = 123 + +fun foo(): String { + val x = "a${i}4_" + val y = "-4${i}a" + val z = "+1243a" + val u = 123 + return "ab${i}def" +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractTrue.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractTrue.kt new file mode 100644 index 000000000000..11bece45cd04 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractTrue.kt @@ -0,0 +1,9 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): String { + val x = "atrue123" + val x = "aTRUE123" + val z = true + return "abtruedef" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractTrue.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractTrue.kt.after new file mode 100644 index 000000000000..31a079bb6002 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/extractTrue.kt.after @@ -0,0 +1,11 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val b = true + +fun foo(): String { + val x = "a${b}123" + val x = "aTRUE123" + val z = true + return "ab${b}def" +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/partTemplate.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/partTemplate.kt new file mode 100644 index 000000000000..a2a33101313d --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/partTemplate.kt @@ -0,0 +1,6 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): String { + val x = "a$a" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/partTemplate.kt.conflicts b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/partTemplate.kt.conflicts new file mode 100644 index 000000000000..6042be53b1be --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/partTemplate.kt.conflicts @@ -0,0 +1 @@ +Can't introduce constant for non constant expression diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntryPrefix.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntryPrefix.kt new file mode 100644 index 000000000000..94f7c604f765 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntryPrefix.kt @@ -0,0 +1,9 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): String { + val x = "xabc$a" + val y = "${a}abcx" + val z = "xacb$a" + return "abcdef" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntryPrefix.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntryPrefix.kt.after new file mode 100644 index 000000000000..f061d01a8010 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntryPrefix.kt.after @@ -0,0 +1,11 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val s = "abc" + +fun foo(): String { + val x = "x$s$a" + val y = "${a}${s}x" + val z = "xacb$a" + return "${s}def" +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySubstring.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySubstring.kt new file mode 100644 index 000000000000..b95ef5df721e --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySubstring.kt @@ -0,0 +1,9 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): String { + val x = "xcd$a" + val y = "${a}cdx" + val z = "xcf$a" + return "abcdef" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySubstring.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySubstring.kt.after new file mode 100644 index 000000000000..98371e1ed17b --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySubstring.kt.after @@ -0,0 +1,11 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val s = "cd" + +fun foo(): String { + val x = "x$s$a" + val y = "${a}${s}x" + val z = "xcf$a" + return "ab${s}ef" +} diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySuffix.kt b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySuffix.kt new file mode 100644 index 000000000000..06f3236c0be6 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySuffix.kt @@ -0,0 +1,9 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +fun foo(): String { + val x = "xdef$a" + val y = "${a}defx" + val z = "xddf$a" + return "abcdef" +} \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySuffix.kt.after b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySuffix.kt.after new file mode 100644 index 000000000000..b57ea4ac4a30 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/refactoring/introduceConstant/stringTemplates/singleEntrySuffix.kt.after @@ -0,0 +1,11 @@ +// EXTRACTION_TARGET: property with initializer +val a = 1 + +private const val s = "def" + +fun foo(): String { + val x = "x$s$a" + val y = "${a}${s}x" + val z = "xddf$a" + return "abc$s" +} diff --git a/plugins/kotlin/plugin/resources/META-INF/refactorings.xml b/plugins/kotlin/plugin/resources/META-INF/refactorings.xml index 2e0b7ca70211..b547364984c7 100644 --- a/plugins/kotlin/plugin/resources/META-INF/refactorings.xml +++ b/plugins/kotlin/plugin/resources/META-INF/refactorings.xml @@ -201,6 +201,11 @@ + + + +