[kotlin] Port AddValVarToConstructorParameterActionIntention to K2

IJ-CR-137425
^KTIJ-29587

GitOrigin-RevId: 4e08de4e418113ea478a64bf27756c02e1344e70
This commit is contained in:
Andrey Cherkasov
2024-09-11 21:31:58 +00:00
committed by intellij-monorepo-bot
parent 373d76c26b
commit 6b0d87c770
9 changed files with 158 additions and 13 deletions
@@ -9,11 +9,9 @@ import org.jetbrains.kotlin.analysis.api.fir.diagnostics.KaFirDiagnostic
import org.jetbrains.kotlin.idea.base.psi.mustHaveOnlyValPropertiesInPrimaryConstructor
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.codeinsight.api.applicators.fixes.KotlinQuickFixFactory
import org.jetbrains.kotlin.idea.codeinsight.utils.ValVarExpression
import org.jetbrains.kotlin.idea.codeinsights.impl.base.intentions.addValVarToConstructorParameter
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.psiUtil.containingClass
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
internal object AddValVarToConstructorParameterFixFactory {
@@ -45,11 +43,7 @@ internal object AddValVarToConstructorParameterFixFactory {
actionContext: ActionContext,
element: KtParameter,
updater: ModPsiUpdater,
) {
val valKeyword = element.addBefore(KtPsiFactory(actionContext.project).createValKeyword(), element.nameIdentifier)
if (element.containingClass()?.mustHaveOnlyValPropertiesInPrimaryConstructor() == true) return
updater.templateBuilder().field(valKeyword, ValVarExpression)
}
) = addValVarToConstructorParameter(actionContext.project, element, updater)
override fun getPresentation(
context: ActionContext,
@@ -0,0 +1,20 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.codeinsights.impl.base.intentions
import com.intellij.modcommand.ModPsiUpdater
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.base.psi.mustHaveOnlyValPropertiesInPrimaryConstructor
import org.jetbrains.kotlin.idea.codeinsight.utils.ValVarExpression
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.psiUtil.containingClass
fun addValVarToConstructorParameter(
project: Project,
element: KtParameter,
updater: ModPsiUpdater,
) {
val valKeyword = element.addBefore(KtPsiFactory(project).createValKeyword(), element.nameIdentifier)
if (element.containingClass()?.mustHaveOnlyValPropertiesInPrimaryConstructor() == true) return
updater.templateBuilder().field(valKeyword, ValVarExpression)
}
@@ -394,5 +394,12 @@
<categoryKey>group.names.kotlin</categoryKey>
</intentionAction>
<intentionAction>
<language>kotlin</language>
<className>org.jetbrains.kotlin.idea.k2.codeinsight.intentions.AddValVarToConstructorParameterActionIntention</className>
<bundleName>messages.KotlinBundle</bundleName>
<categoryKey>group.names.kotlin</categoryKey>
</intentionAction>
</extensions>
</idea-plugin>
@@ -0,0 +1,51 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.k2.codeinsight.intentions
import com.intellij.modcommand.ActionContext
import com.intellij.modcommand.ModPsiUpdater
import com.intellij.modcommand.Presentation
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.analysis.api.KaSession
import org.jetbrains.kotlin.idea.base.psi.mustHaveOnlyPropertiesInPrimaryConstructor
import org.jetbrains.kotlin.idea.base.psi.mustHaveValOrVar
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.codeinsight.api.applicable.intentions.KotlinApplicableModCommandAction
import org.jetbrains.kotlin.idea.codeinsights.impl.base.applicators.ApplicabilityRanges
import org.jetbrains.kotlin.idea.codeinsights.impl.base.intentions.addValVarToConstructorParameter
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtParameterList
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isExpectDeclaration
class AddValVarToConstructorParameterActionIntention : KotlinApplicableModCommandAction<KtParameter, Unit>(KtParameter::class) {
override fun getFamilyName(): String = KotlinBundle.message("add.val.var.to.primary.constructor.parameter")
override fun getPresentation(context: ActionContext, element: KtParameter): Presentation {
val actionName = KotlinBundle.message("add.val.var.to.parameter.0", element.name ?: "")
return Presentation.of(actionName)
}
override fun isApplicableByPsi(element: KtParameter): Boolean {
if (element.valOrVarKeyword != null) return false
val constructor = (element.parent as? KtParameterList)?.parent as? KtPrimaryConstructor ?: return false
if (!constructor.mustHaveValOrVar() && constructor.isExpectDeclaration()) return false
val containingClass = element.getStrictParentOfType<KtClass>()?: return false
return !containingClass.mustHaveOnlyPropertiesInPrimaryConstructor()
}
override fun getApplicableRanges(element: KtParameter): List<TextRange> = ApplicabilityRanges.declarationName(element)
context(KaSession)
override fun prepareContext(element: KtParameter) {
}
override fun invoke(
actionContext: ActionContext,
element: KtParameter,
elementContext: Unit,
updater: ModPsiUpdater,
) = addValVarToConstructorParameter(actionContext.project, element, updater)
}
@@ -10379,7 +10379,79 @@ public abstract class K2IntentionTestGenerated extends AbstractK2IntentionTest {
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/intentions/addValOrVar")
public static class AddValOrVar extends AbstractK2IntentionTest {
@java.lang.Override
@org.jetbrains.annotations.NotNull
public final KotlinPluginMode getPluginMode() {
return KotlinPluginMode.K2;
}
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("actualClass.kt")
public void testActualClass() throws Exception {
runTest("../../../idea/tests/testData/intentions/addValOrVar/actualClass.kt");
}
@TestMetadata("addVal.kt")
public void testAddVal() throws Exception {
runTest("../../../idea/tests/testData/intentions/addValOrVar/addVal.kt");
}
@TestMetadata("dataClass.kt")
public void testDataClass() throws Exception {
runTest("../../../idea/tests/testData/intentions/addValOrVar/dataClass.kt");
}
@TestMetadata("expectAnnotation.kt")
public void testExpectAnnotation() throws Exception {
runTest("../../../idea/tests/testData/intentions/addValOrVar/expectAnnotation.kt");
}
@TestMetadata("expectClass.kt")
public void testExpectClass() throws Exception {
runTest("../../../idea/tests/testData/intentions/addValOrVar/expectClass.kt");
}
@TestMetadata("expectClass2.kt")
public void testExpectClass2() throws Exception {
runTest("../../../idea/tests/testData/intentions/addValOrVar/expectClass2.kt");
}
@TestMetadata("expectInlineClass.kt")
public void testExpectInlineClass() throws Exception {
runTest("../../../idea/tests/testData/intentions/addValOrVar/expectInlineClass.kt");
}
@TestMetadata("funParameter.kt")
public void testFunParameter() throws Exception {
runTest("../../../idea/tests/testData/intentions/addValOrVar/funParameter.kt");
}
@TestMetadata("hasVal.kt")
public void testHasVal() throws Exception {
runTest("../../../idea/tests/testData/intentions/addValOrVar/hasVal.kt");
}
@TestMetadata("hasVar.kt")
public void testHasVar() throws Exception {
runTest("../../../idea/tests/testData/intentions/addValOrVar/hasVar.kt");
}
@TestMetadata("outOfRange.kt")
public void testOutOfRange() throws Exception {
runTest("../../../idea/tests/testData/intentions/addValOrVar/outOfRange.kt");
}
@TestMetadata("secondaryConstructorParameter.kt")
public void testSecondaryConstructorParameter() throws Exception {
runTest("../../../idea/tests/testData/intentions/addValOrVar/secondaryConstructorParameter.kt");
}
}
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.k2.codeinsight.intentions.AddValVarToConstructorParameterActionIntention
@@ -1,6 +1,6 @@
// "Add 'val' or 'var' to parameter 'x'" "true"
/* IGNORE_K2 */
class Foo(vararg <caret>x: Int, val y: Int) {
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$Intention
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$Intention
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.intentions.AddValVarToConstructorParameterActionIntention
@@ -1,6 +1,6 @@
// "Add 'val' or 'var' to parameter 'x'" "true"
/* IGNORE_K2 */
class Foo(vararg val<caret> x: Int, val y: Int) {
}
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$Intention
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$Intention
// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.intentions.AddValVarToConstructorParameterActionIntention
@@ -181,7 +181,7 @@ internal fun MutableTWorkspace.generateK2IntentionTests() {
model("${idea}intentions/convertReceiverToParameter", pattern = pattern)
model("${idea}intentions/convertUnsafeCastCallToUnsafeCast", pattern = pattern, isIgnored = true)
model("${idea}intentions/convertSnakeCaseTestFunctionToSpaced", pattern = pattern, isIgnored = true)
model("${idea}intentions/addValOrVar", pattern = pattern, isIgnored = true)
model("${idea}intentions/addValOrVar", pattern = pattern)
model("${idea}intentions/convertBlockCommentToLineComment", pattern = pattern, isIgnored = true)
model("${idea}intentions/removeSingleExpressionStringTemplate", pattern = pattern, isIgnored = true)
model("${idea}intentions/convertLambdaToMultiLine", pattern = pattern, isIgnored = true)