[kotlin] Do not provide change val to var for elements from libraries

#KTIJ-29267 Fixed

GitOrigin-RevId: 4f99471c1cbf26af4b5c6b3d1ed389129d3374a1
This commit is contained in:
Vladimir Dolzhenko
2024-03-29 23:33:12 +00:00
committed by intellij-monorepo-bot
parent feb9688ed7
commit 435b8b4dd8
23 changed files with 135 additions and 19 deletions
@@ -627,6 +627,84 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/quickfix/addValVar")
public abstract static class AddValVar extends AbstractHighLevelQuickFixTest {
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/quickfix/addValVar/inline")
public static class Inline extends AbstractHighLevelQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("basic.kt")
public void testBasic() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addValVar/inline/basic.kt");
}
@TestMetadata("var.kt")
public void testVar() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addValVar/inline/var.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/quickfix/addValVar")
public static class Uncategorized extends AbstractHighLevelQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("addVal.kt")
public void testAddVal() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addValVar/addVal.kt");
}
@TestMetadata("addValAfterVarArg.kt")
public void testAddValAfterVarArg() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addValVar/addValAfterVarArg.kt");
}
@TestMetadata("annotation.kt")
public void testAnnotation() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addValVar/annotation.kt");
}
@TestMetadata("expectAnnotation.kt")
public void testExpectAnnotation() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addValVar/expectAnnotation.kt");
}
@TestMetadata("library.kt")
public void testLibrary() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addValVar/library.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/quickfix/addValVar/value")
public static class Value extends AbstractHighLevelQuickFixTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
@TestMetadata("basic.kt")
public void testBasic() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addValVar/value/basic.kt");
}
@TestMetadata("genericParameter.kt")
public void testGenericParameter() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addValVar/value/genericParameter.kt");
}
@TestMetadata("var.kt")
public void testVar() throws Exception {
runTest("../../../idea/tests/testData/quickfix/addValVar/value/var.kt");
}
}
}
@RunWith(JUnit3RunnerWithInners.class)
@TestMetadata("../../../idea/tests/testData/quickfix/autoImports")
public abstract static class AutoImports extends AbstractHighLevelQuickFixTest {
@@ -22,7 +22,7 @@ class ChangeVariableMutabilityFix(
private val deleteInitializer: Boolean = false
) : KotlinPsiOnlyQuickFixAction<KtValVarKeywordOwner>(element) {
override fun getText() = actionText ?: buildString {
override fun getText(): String = actionText ?: buildString {
if (makeVar) append(KotlinBundle.message("change.to.var")) else append(KotlinBundle.message("change.to.val"))
if (deleteInitializer) append(KotlinBundle.message("and.delete.initializer"))
}
@@ -52,51 +52,58 @@ class ChangeVariableMutabilityFix(
val VAL_WITH_SETTER_FACTORY: QuickFixesPsiBasedFactory<KtPropertyAccessor> =
quickFixesPsiBasedFactory { psiElement: KtPropertyAccessor ->
listOf(ChangeVariableMutabilityFix(psiElement.property, true))
psiElement.property.takeIf(PsiElement::isWritable)?.let {
listOf(ChangeVariableMutabilityFix(it, true))
} ?: emptyList()
}
val VAL_REASSIGNMENT = KotlinQuickFixFactory.IntentionBased { diagnostic: KtFirDiagnostic.ValReassignment ->
val property = diagnostic.variable.psi as? KtValVarKeywordOwner
?: return@IntentionBased emptyList()
listOf(
ChangeVariableMutabilityFix(property, makeVar = true),
)
(diagnostic.variable.psi as? KtValVarKeywordOwner)?.takeIf(PsiElement::isWritable)?.let {
listOf(ChangeVariableMutabilityFix(it, makeVar = true))
} ?: emptyList()
}
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()
}
is KtProperty, is KtParameter -> {
(psiElement as KtValVarKeywordOwner).takeIf(PsiElement::isWritable)?.let {
listOf(ChangeVariableMutabilityFix(it, true))
}
}
else -> null
} ?: emptyList()
}
val VAR_ANNOTATION_PARAMETER_FACTORY: QuickFixesPsiBasedFactory<KtParameter> =
quickFixesPsiBasedFactory { psiElement: KtParameter ->
listOf(ChangeVariableMutabilityFix(psiElement, false))
psiElement.takeIf(PsiElement::isWritable)?.let {
listOf(ChangeVariableMutabilityFix(it, false))
} ?: emptyList()
}
val LATEINIT_VAL_FACTORY: QuickFixesPsiBasedFactory<KtModifierListOwner> =
quickFixesPsiBasedFactory { psiElement: KtModifierListOwner ->
val property = psiElement as? KtProperty ?: return@quickFixesPsiBasedFactory emptyList()
if (property.valOrVarKeyword.text != "val") {
emptyList()
} else {
listOf(ChangeVariableMutabilityFix(property, makeVar = true))
}
(psiElement as? KtProperty)?.takeIf(PsiElement::isWritable)?.let {
if (it.valOrVarKeyword.text != "val") {
null
} else {
listOf(ChangeVariableMutabilityFix(it, makeVar = true))
}
} ?: emptyList()
}
val CONST_VAL_FACTORY: QuickFixesPsiBasedFactory<PsiElement> =
quickFixesPsiBasedFactory { psiElement: PsiElement ->
if (psiElement.node.elementType as? KtModifierKeywordToken != KtTokens.CONST_KEYWORD) return@quickFixesPsiBasedFactory emptyList()
val property = psiElement.getStrictParentOfType<KtProperty>() ?: return@quickFixesPsiBasedFactory emptyList()
val property = psiElement.getStrictParentOfType<KtProperty>()?.takeIf(PsiElement::isWritable) ?: return@quickFixesPsiBasedFactory emptyList()
listOf(ChangeVariableMutabilityFix(property, makeVar = false))
}
val MUST_BE_INITIALIZED_FACTORY: QuickFixesPsiBasedFactory<PsiElement> =
quickFixesPsiBasedFactory { psiElement: PsiElement ->
val property = psiElement as? KtProperty ?: return@quickFixesPsiBasedFactory emptyList()
val property = (psiElement as? KtProperty)?.takeIf(PsiElement::isWritable) ?: return@quickFixesPsiBasedFactory emptyList()
val getter = property.getter ?: return@quickFixesPsiBasedFactory emptyList()
if (!getter.hasBody()) return@quickFixesPsiBasedFactory emptyList()
if (getter.hasBlockBody() && property.typeReference == null) return@quickFixesPsiBasedFactory emptyList()
@@ -2038,6 +2038,11 @@ public abstract class K1QuickFixTestGenerated extends AbstractK1QuickFixTest {
public void testExpectAnnotation() throws Exception {
runTest("testData/quickfix/addValVar/expectAnnotation.kt");
}
@TestMetadata("library.kt")
public void testLibrary() throws Exception {
runTest("testData/quickfix/addValVar/library.kt");
}
}
@RunWith(JUnit3RunnerWithInners.class)
@@ -1,4 +1,5 @@
// "Add 'val' or 'var' to parameter 'x'" "true"
/* IGNORE_K2 */
data class Foo(<caret>x: Int, val y: Int) {
}
@@ -1,4 +1,5 @@
// "Add 'val' or 'var' to parameter 'x'" "true"
/* IGNORE_K2 */
data class Foo(val<caret> x: Int, val y: Int) {
}
@@ -1,4 +1,5 @@
// "Add 'val' or 'var' to parameter 'x'" "true"
/* IGNORE_K2 */
class Foo(vararg <caret>x: Int, val y: Int) {
}
@@ -1,4 +1,5 @@
// "Add 'val' or 'var' to parameter 'x'" "true"
/* IGNORE_K2 */
class Foo(vararg val<caret> x: Int, val y: Int) {
}
@@ -1,3 +1,4 @@
// "Add 'val' to parameter 'x'" "true"
/* IGNORE_K2 */
annotation class A(<caret>x: Int)
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$QuickFix
@@ -1,3 +1,4 @@
// "Add 'val' to parameter 'x'" "true"
/* IGNORE_K2 */
annotation class A(val x: Int)
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$QuickFix
@@ -1,4 +1,5 @@
// "Add 'val' to parameter 'a'" "true"
// ERROR: 'expect' and 'actual' declarations can be used only in multiplatform projects. Learn more about Kotlin Multiplatform: https://kotl.in/multiplatform-setup
/* IGNORE_K2 */
expect annotation class A(<caret>a: Int)
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$QuickFix
@@ -1,4 +1,5 @@
// "Add 'val' to parameter 'a'" "true"
// ERROR: 'expect' and 'actual' declarations can be used only in multiplatform projects. Learn more about Kotlin Multiplatform: https://kotl.in/multiplatform-setup
/* IGNORE_K2 */
expect annotation class A(val a: Int)
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$QuickFix
@@ -1,3 +1,4 @@
// "Add 'val' to parameter 'x'" "true"
/* IGNORE_K2 */
inline class Foo(<caret>x: Int)
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$QuickFix
@@ -1,3 +1,4 @@
// "Add 'val' to parameter 'x'" "true"
/* IGNORE_K2 */
inline class Foo(val x: Int)
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$QuickFix
@@ -1,3 +1,4 @@
// "Change to 'val'" "true"
/* IGNORE_K2 */
inline class Foo(<caret>var x: Int)
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.codeinsights.impl.base.quickFix.ChangeVariableMutabilityFix
@@ -1,3 +1,4 @@
// "Change to 'val'" "true"
/* IGNORE_K2 */
inline class Foo(val x: Int)
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.codeinsights.impl.base.quickFix.ChangeVariableMutabilityFix
@@ -0,0 +1,7 @@
// "Change to 'val'" "false"
// WITH_STDLIB
// DISABLE-ERRORS
// IGNORE_IRRELEVANT_ACTIONS
fun foo() {
"a".length<caret> = 1
}
@@ -1,5 +1,6 @@
// "Add 'val' to parameter 'x'" "true"
// WITH_STDLIB
/* IGNORE_K2 */
@JvmInline
value class Foo(<caret>x: Int)
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$QuickFix
@@ -1,5 +1,6 @@
// "Add 'val' to parameter 'x'" "true"
// WITH_STDLIB
/* IGNORE_K2 */
@JvmInline
value class Foo(val x: Int)
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.intentions.AddValVarToConstructorParameterAction$QuickFix
@@ -1,6 +1,7 @@
// "Add 'val' to parameter 'x'" "true"
// WITH_STDLIB
// COMPILER_ARGUMENTS: -XXLanguage:+GenericInlineClassParameter
/* IGNORE_K2 */
@JvmInline
value class Foo<T>(<caret>x: T)
@@ -1,6 +1,7 @@
// "Add 'val' to parameter 'x'" "true"
// WITH_STDLIB
// COMPILER_ARGUMENTS: -XXLanguage:+GenericInlineClassParameter
/* IGNORE_K2 */
@JvmInline
value class Foo<T>(val x: T)
@@ -1,5 +1,6 @@
// "Change to 'val'" "true"
// WITH_STDLIB
/* IGNORE_K2 */
@JvmInline
value class Foo(<caret>var x: Int)
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.codeinsights.impl.base.quickFix.ChangeVariableMutabilityFix
@@ -1,5 +1,6 @@
// "Change to 'val'" "true"
// WITH_STDLIB
/* IGNORE_K2 */
@JvmInline
value class Foo(val x: Int)
// FUS_QUICKFIX_NAME: org.jetbrains.kotlin.idea.codeinsights.impl.base.quickFix.ChangeVariableMutabilityFix
@@ -17,6 +17,7 @@ internal fun MutableTWorkspace.generateK2FixTests() {
model("$idea/quickfix/addExclExclCall", pattern = pattern)
model("$idea/quickfix/addInitializer", pattern = pattern)
model("$idea/quickfix/addPropertyAccessors", pattern = pattern)
model("$idea/quickfix/addValVar", pattern = pattern)
model("$idea/quickfix/autoImports", pattern = KT_WITHOUT_DOTS, isRecursive = true)
model("$idea/quickfix/checkArguments", pattern = pattern, isRecursive = false)
model("$idea/quickfix/conflictingImports", pattern = pattern)