ChangeVariableMutability: diagnostic can be reported on constructor parameter
#KT-10416 Fixed
This commit is contained in:
@@ -22,26 +22,31 @@ import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiFile
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.psi.KtPropertyAccessor
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
|
||||
public class ChangeVariableMutabilityFix(element: KtProperty, private val makeVar: Boolean) : KotlinQuickFixAction<KtProperty>(element) {
|
||||
public class ChangeVariableMutabilityFix(element: KtNamedDeclaration, private val makeVar: Boolean) : KotlinQuickFixAction<KtNamedDeclaration>(element) {
|
||||
|
||||
override fun getText() = if (makeVar) "Make variable mutable" else "Make variable immutable"
|
||||
|
||||
override fun getFamilyName(): String = getText()
|
||||
|
||||
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean {
|
||||
return element.isVar() != makeVar
|
||||
return when (element) {
|
||||
is KtProperty -> element.isVar != makeVar
|
||||
is KtParameter -> (element.valOrVarKeyword == KtTokens.VAR_KEYWORD) != makeVar
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
|
||||
val factory = KtPsiFactory(project)
|
||||
val newKeyword = if (makeVar) factory.createVarKeyword() else factory.createValKeyword()
|
||||
element.getValOrVarKeyword().replace(newKeyword)
|
||||
when (element) {
|
||||
is KtProperty -> element.valOrVarKeyword.replace(newKeyword)
|
||||
is KtParameter -> element.valOrVarKeyword!!.replace(newKeyword)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@@ -56,14 +61,18 @@ public class ChangeVariableMutabilityFix(element: KtProperty, private val makeVa
|
||||
public val VAL_REASSIGNMENT_FACTORY: KotlinSingleIntentionActionFactory = object: KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val propertyDescriptor = Errors.VAL_REASSIGNMENT.cast(diagnostic).getA()
|
||||
val declaration = DescriptorToSourceUtils.descriptorToDeclaration(propertyDescriptor) as? KtProperty ?: return null
|
||||
val declaration = DescriptorToSourceUtils.descriptorToDeclaration(propertyDescriptor) as? KtNamedDeclaration ?: return null
|
||||
return ChangeVariableMutabilityFix(declaration, true)
|
||||
}
|
||||
}
|
||||
|
||||
public val VAR_OVERRIDDEN_BY_VAL_FACTORY: KotlinSingleIntentionActionFactory = object: KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
return ChangeVariableMutabilityFix(diagnostic.getPsiElement() as KtProperty, true)
|
||||
val element = diagnostic.psiElement
|
||||
return when (element) {
|
||||
is KtProperty, is KtParameter -> ChangeVariableMutabilityFix(element as KtNamedDeclaration, true)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Make variable mutable" "true"
|
||||
open class A {
|
||||
open var x = 42;
|
||||
}
|
||||
|
||||
class B(override val<caret> x: Int) : A()
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
// "Make variable mutable" "true"
|
||||
open class A {
|
||||
open var x = 42;
|
||||
}
|
||||
|
||||
class B(override var x: Int) : A()
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
// "Make variable mutable" "true"
|
||||
class A(val a: Int) {
|
||||
fun foo() {
|
||||
<caret>a = 5
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// "Make variable mutable" "true"
|
||||
class A(var a: Int) {
|
||||
fun foo() {
|
||||
a = 5
|
||||
}
|
||||
}
|
||||
@@ -7310,6 +7310,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("valOverrideVarConstructorParameter.kt")
|
||||
public void testValOverrideVarConstructorParameter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/variables/changeMutability/valOverrideVarConstructorParameter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("valReassignmentLocal.kt")
|
||||
public void testValReassignmentLocal() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/variables/changeMutability/valReassignmentLocal.kt");
|
||||
@@ -7328,6 +7334,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("valReassignmentPropertyConstructorParameter.kt")
|
||||
public void testValReassignmentPropertyConstructorParameter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/variables/changeMutability/valReassignmentPropertyConstructorParameter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("valWithSetter.kt")
|
||||
public void testValWithSetter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/variables/changeMutability/valWithSetter.kt");
|
||||
|
||||
Reference in New Issue
Block a user