FIR IDE: Enable RemoveValVarFromParameterFix for

VAL_OR_VAR_ON_*_PARAMETER.
This commit is contained in:
Mark Punzalan
2021-05-20 00:15:26 +00:00
committed by teamcityserver
parent af99ad0736
commit 23605e08be
4 changed files with 66 additions and 19 deletions
@@ -1146,6 +1146,7 @@ fun main(args: Array<String>) {
model("quickfix/replaceWithDotCall", pattern = pattern, filenameStartsLowerCase = true) model("quickfix/replaceWithDotCall", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/replaceWithSafeCall", pattern = pattern, filenameStartsLowerCase = true) model("quickfix/replaceWithSafeCall", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/variables/changeMutability", pattern = pattern, filenameStartsLowerCase = true) model("quickfix/variables/changeMutability", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/variables/removeValVarFromParameter", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/when", pattern = pattern, filenameStartsLowerCase = true) model("quickfix/when", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/wrapWithSafeLetCall", pattern = pattern, filenameStartsLowerCase = true) model("quickfix/wrapWithSafeLetCall", pattern = pattern, filenameStartsLowerCase = true)
model("quickfix/typeMismatch/componentFunctionReturnTypeMismatch", pattern = pattern, filenameStartsLowerCase = true) model("quickfix/typeMismatch/componentFunctionReturnTypeMismatch", pattern = pattern, filenameStartsLowerCase = true)
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.idea.quickfix.fixes.*
import org.jetbrains.kotlin.idea.quickfix.fixes.InitializePropertyQuickFixFactory import org.jetbrains.kotlin.idea.quickfix.fixes.InitializePropertyQuickFixFactory
class MainKtQuickFixRegistrar : KtQuickFixRegistrar() { class MainKtQuickFixRegistrar : KtQuickFixRegistrar() {
private val modifiers = KtQuickFixesListBuilder.registerPsiQuickFix { private val keywords = KtQuickFixesListBuilder.registerPsiQuickFix {
registerPsiQuickFixes(KtFirDiagnostic.RedundantModifier::class, RemoveModifierFix.removeRedundantModifier) registerPsiQuickFixes(KtFirDiagnostic.RedundantModifier::class, RemoveModifierFix.removeRedundantModifier)
registerPsiQuickFixes(KtFirDiagnostic.IncompatibleModifiers::class, RemoveModifierFix.removeNonRedundantModifier) registerPsiQuickFixes(KtFirDiagnostic.IncompatibleModifiers::class, RemoveModifierFix.removeNonRedundantModifier)
registerPsiQuickFixes(KtFirDiagnostic.RepeatedModifier::class, RemoveModifierFix.removeNonRedundantModifier) registerPsiQuickFixes(KtFirDiagnostic.RepeatedModifier::class, RemoveModifierFix.removeNonRedundantModifier)
@@ -63,6 +63,10 @@ class MainKtQuickFixRegistrar : KtQuickFixRegistrar() {
KtFirDiagnostic.AbstractClassMemberNotImplemented::class, KtFirDiagnostic.AbstractClassMemberNotImplemented::class,
AddModifierFix.addAbstractModifier AddModifierFix.addAbstractModifier
) )
registerPsiQuickFixes(KtFirDiagnostic.ValOrVarOnLoopParameter::class, RemoveValVarFromParameterFix)
registerPsiQuickFixes(KtFirDiagnostic.ValOrVarOnFunParameter::class, RemoveValVarFromParameterFix)
registerPsiQuickFixes(KtFirDiagnostic.ValOrVarOnCatchParameter::class, RemoveValVarFromParameterFix)
registerPsiQuickFixes(KtFirDiagnostic.ValOrVarOnSecondaryConstructorParameter::class, RemoveValVarFromParameterFix)
} }
private val propertyInitialization = KtQuickFixesListBuilder.registerPsiQuickFix { private val propertyInitialization = KtQuickFixesListBuilder.registerPsiQuickFix {
@@ -124,7 +128,7 @@ class MainKtQuickFixRegistrar : KtQuickFixRegistrar() {
} }
override val list: KtQuickFixesList = KtQuickFixesList.createCombined( override val list: KtQuickFixesList = KtQuickFixesList.createCombined(
modifiers, keywords,
propertyInitialization, propertyInitialization,
overrides, overrides,
imports, imports,
@@ -1493,6 +1493,54 @@ public class HighLevelQuickFixTestGenerated extends AbstractHighLevelQuickFixTes
} }
} }
@TestMetadata("idea/testData/quickfix/variables/removeValVarFromParameter")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class RemoveValVarFromParameter extends AbstractHighLevelQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInRemoveValVarFromParameter() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/variables/removeValVarFromParameter"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, true);
}
@TestMetadata("catchParameter.kt")
public void testCatchParameter() throws Exception {
runTest("idea/testData/quickfix/variables/removeValVarFromParameter/catchParameter.kt");
}
@TestMetadata("constructorParameter.kt")
public void testConstructorParameter() throws Exception {
runTest("idea/testData/quickfix/variables/removeValVarFromParameter/constructorParameter.kt");
}
@TestMetadata("funParameter.kt")
public void testFunParameter() throws Exception {
runTest("idea/testData/quickfix/variables/removeValVarFromParameter/funParameter.kt");
}
@TestMetadata("loopMultiParameter.kt")
public void testLoopMultiParameter() throws Exception {
runTest("idea/testData/quickfix/variables/removeValVarFromParameter/loopMultiParameter.kt");
}
@TestMetadata("loopParameter.kt")
public void testLoopParameter() throws Exception {
runTest("idea/testData/quickfix/variables/removeValVarFromParameter/loopParameter.kt");
}
@TestMetadata("parameterWithComment.kt")
public void testParameterWithComment() throws Exception {
runTest("idea/testData/quickfix/variables/removeValVarFromParameter/parameterWithComment.kt");
}
@TestMetadata("secondaryConstructorParameter.kt")
public void testSecondaryConstructorParameter() throws Exception {
runTest("idea/testData/quickfix/variables/removeValVarFromParameter/secondaryConstructorParameter.kt");
}
}
@TestMetadata("idea/testData/quickfix/when") @TestMetadata("idea/testData/quickfix/when")
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @RunWith(JUnit3RunnerWithInners.class)
@@ -1,32 +1,24 @@
/* /*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
package org.jetbrains.kotlin.idea.quickfix package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.KotlinBundle import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtValVarKeywordOwner import org.jetbrains.kotlin.psi.KtValVarKeywordOwner
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
class RemoveValVarFromParameterFix(element: KtValVarKeywordOwner) : KotlinQuickFixAction<KtValVarKeywordOwner>(element) {
private val varOrVal: String
init {
val valOrVarNode =
element.valOrVarKeyword ?: throw AssertionError("Val or var node not found for " + element.getElementTextWithContext())
varOrVal = valOrVarNode.text
}
class RemoveValVarFromParameterFix(element: KtValVarKeywordOwner) : KotlinPsiOnlyQuickFixAction<KtValVarKeywordOwner>(element) {
override fun getFamilyName() = KotlinBundle.message("remove.val.var.from.parameter") override fun getFamilyName() = KotlinBundle.message("remove.val.var.from.parameter")
override fun getText(): String { override fun getText(): String {
val element = element ?: return "" val varOrVal = element?.valOrVarKeyword?.text ?: return familyName
val varOrVal = element.valOrVarKeyword?.text ?: return familyName
return KotlinBundle.message("remove.0.from.parameter", varOrVal) return KotlinBundle.message("remove.0.from.parameter", varOrVal)
} }
@@ -34,8 +26,10 @@ class RemoveValVarFromParameterFix(element: KtValVarKeywordOwner) : KotlinQuickF
element?.valOrVarKeyword?.delete() element?.valOrVarKeyword?.delete()
} }
companion object : KotlinSingleIntentionActionFactory() { companion object : QuickFixesPsiBasedFactory<PsiElement>(PsiElement::class, PsiElementSuitabilityCheckers.ALWAYS_SUITABLE) {
override fun createAction(diagnostic: Diagnostic) = override fun doCreateQuickFix(psiElement: PsiElement): List<IntentionAction> {
RemoveValVarFromParameterFix(diagnostic.psiElement.parent as KtValVarKeywordOwner) val owner = psiElement.getNonStrictParentOfType<KtValVarKeywordOwner>() ?: return emptyList()
return listOf(RemoveValVarFromParameterFix(owner))
}
} }
} }