FIR IDE: Add quickfix for VAR_OVERRIDDEN_BY_VAL.

This commit is contained in:
Mark Punzalan
2021-02-10 08:28:42 +00:00
committed by Ilya Kirillov
parent 57e06992c9
commit 7962224804
9 changed files with 96 additions and 12 deletions
@@ -1105,6 +1105,7 @@ fun main(args: Array<String>) {
val pattern = "^([\\w\\-_]+)\\.kt$"
model("quickfix/modifiers", pattern = pattern, filenameStartsLowerCase = true, recursive = false)
model("quickfix/override/typeMismatchOnOverride", pattern = pattern, filenameStartsLowerCase = true, recursive = false)
model("quickfix/variables/changeMutability", pattern = pattern, filenameStartsLowerCase = true, recursive = false)
}
testClass<AbstractHLInspectionTest> {
@@ -10,12 +10,14 @@ import org.jetbrains.kotlin.idea.fir.api.fixes.KtQuickFixRegistrar
import org.jetbrains.kotlin.idea.fir.api.fixes.KtQuickFixesList
import org.jetbrains.kotlin.idea.fir.api.fixes.KtQuickFixesListBuilder
import org.jetbrains.kotlin.idea.frontend.api.fir.diagnostics.KtFirDiagnostic
import org.jetbrains.kotlin.idea.quickfix.ChangeVariableMutabilityFix.Companion.VAR_OVERRIDDEN_BY_VAL_FACTORY
import org.jetbrains.kotlin.idea.quickfix.fixes.ChangeTypeQuickFix
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtModifierListOwner
class MainKtQuickFixRegistrar : KtQuickFixRegistrar() {
private val modifiers = KtQuickFixesListBuilder.registerPsiQuickFix {
// RemoveModifierFix
registerPsiQuickFix<PsiElement, KtFirDiagnostic.RedundantModifier>(RemoveModifierFix.createRemoveModifierFactory(isRedundant = true))
registerPsiQuickFix<PsiElement, KtFirDiagnostic.IncompatibleModifiers>(RemoveModifierFix.createRemoveModifierFactory(isRedundant = false))
registerPsiQuickFix<PsiElement, KtFirDiagnostic.RepeatedModifier>(RemoveModifierFix.createRemoveModifierFactory(isRedundant = false))
@@ -27,6 +29,9 @@ class MainKtQuickFixRegistrar : KtQuickFixRegistrar() {
isRedundant = true
)
)
// ChangeVariableMutabilityFix
registerPsiQuickFix<PsiElement, KtFirDiagnostic.VarOverriddenByVal>(VAR_OVERRIDDEN_BY_VAL_FACTORY)
}
private val overrides = KtQuickFixesListBuilder.registerPsiQuickFix {
@@ -449,4 +449,77 @@ public class HighLevelQuickFixTestGenerated extends AbstractHighLevelQuickFixTes
runTest("idea/testData/quickfix/override/typeMismatchOnOverride/returnTypeMismatchOnOverrideUnitInt.kt");
}
}
@TestMetadata("idea/testData/quickfix/variables/changeMutability")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class ChangeMutability extends AbstractHighLevelQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInChangeMutability() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/quickfix/variables/changeMutability"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), null, false);
}
@TestMetadata("capturedMemberValInitialization.kt")
public void testCapturedMemberValInitialization() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/capturedMemberValInitialization.kt");
}
@TestMetadata("capturedValInitialization.kt")
public void testCapturedValInitialization() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/capturedValInitialization.kt");
}
@TestMetadata("const.kt")
public void testConst() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/const.kt");
}
@TestMetadata("funParameter.kt")
public void testFunParameter() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/funParameter.kt");
}
@TestMetadata("localInGetter.kt")
public void testLocalInGetter() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/localInGetter.kt");
}
@TestMetadata("valOverrideVar.kt")
public void testValOverrideVar() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/valOverrideVar.kt");
}
@TestMetadata("valOverrideVarConstructorParameter.kt")
public void testValOverrideVarConstructorParameter() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/valOverrideVarConstructorParameter.kt");
}
@TestMetadata("valReassignmentLocal.kt")
public void testValReassignmentLocal() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/valReassignmentLocal.kt");
}
@TestMetadata("valReassignmentOuterDecl.kt")
public void testValReassignmentOuterDecl() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/valReassignmentOuterDecl.kt");
}
@TestMetadata("valReassignmentProperty.kt")
public void testValReassignmentProperty() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/valReassignmentProperty.kt");
}
@TestMetadata("valReassignmentPropertyConstructorParameter.kt")
public void testValReassignmentPropertyConstructorParameter() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/valReassignmentPropertyConstructorParameter.kt");
}
@TestMetadata("valWithSetter.kt")
public void testValWithSetter() throws Exception {
runTest("idea/testData/quickfix/variables/changeMutability/valWithSetter.kt");
}
}
}
@@ -19,12 +19,14 @@ package org.jetbrains.kotlin.idea.quickfix
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.quickfix.createFromUsage.createCallable.CreatePropertyDelegateAccessorsActionFactory
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
@@ -86,15 +88,13 @@ class ChangeVariableMutabilityFix(
val CAPTURED_MEMBER_VAL_INITIALIZATION_FACTORY = ReassignmentActionFactory(Errors.CAPTURED_MEMBER_VAL_INITIALIZATION)
val VAR_OVERRIDDEN_BY_VAL_FACTORY: KotlinSingleIntentionActionFactory = object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val element = diagnostic.psiElement
return when (element) {
is KtProperty, is KtParameter -> ChangeVariableMutabilityFix(element as KtValVarKeywordOwner, true)
else -> null
val VAR_OVERRIDDEN_BY_VAL_FACTORY: QuickFixesPsiBasedFactory<PsiElement> =
quickFixesPsiBasedFactory { psiElement: PsiElement ->
when (psiElement) {
is KtProperty, is KtParameter -> listOf(ChangeVariableMutabilityFix(psiElement as KtValVarKeywordOwner, true))
else -> emptyList()
}
}
}
val VAR_ANNOTATION_PARAMETER_FACTORY: KotlinSingleIntentionActionFactory = object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
@@ -4,4 +4,5 @@
// ERROR: Val cannot be reassigned
fun fun1(i: Int) {
<caret>i = 2
}
}
/* FIR_COMPARISON */
@@ -5,4 +5,5 @@ open class A {
class B : A() {
override val<caret> x: Int = 3;
}
}
/* FIR_COMPARISON */
@@ -5,4 +5,5 @@ open class A {
class B : A() {
override var x: Int = 3;
}
}
/* FIR_COMPARISON */
@@ -3,4 +3,5 @@ open class A {
open var x = 42;
}
class B(override val<caret> x: Int) : A()
class B(override val<caret> x: Int) : A()
/* FIR_COMPARISON */
@@ -3,4 +3,5 @@ open class A {
open var x = 42;
}
class B(override var x: Int) : A()
class B(override var x: Int) : A()
/* FIR_COMPARISON */