Allow "Add val/var to primary constructor parameter" for inline & annotation classes
Relates to #KT-29731
This commit is contained in:
+2
-1
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.idea.quickfix.KotlinQuickFixAction
|
||||
import org.jetbrains.kotlin.idea.quickfix.KotlinSingleIntentionActionFactory
|
||||
import org.jetbrains.kotlin.idea.refactoring.ValVarExpression
|
||||
import org.jetbrains.kotlin.idea.util.allowedValOrVar
|
||||
import org.jetbrains.kotlin.idea.util.isExpectDeclaration
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
|
||||
@@ -42,7 +43,7 @@ interface AddValVarToConstructorParameterAction {
|
||||
|
||||
fun canInvoke(element: KtParameter): Boolean {
|
||||
return element.valOrVarKeyword == null && ((element.parent as? KtParameterList)?.parent as? KtPrimaryConstructor)
|
||||
?.takeUnless(KtDeclaration::isExpectDeclaration) != null
|
||||
?.takeIf { it.allowedValOrVar() || !it.isExpectDeclaration() } != null
|
||||
}
|
||||
|
||||
fun invoke(element: KtParameter, editor: Editor?) {
|
||||
|
||||
@@ -25,13 +25,12 @@ import org.jetbrains.kotlin.idea.core.overrideImplement.makeNotActual
|
||||
import org.jetbrains.kotlin.idea.core.toDescriptor
|
||||
import org.jetbrains.kotlin.idea.core.util.DescriptorMemberChooserObject
|
||||
import org.jetbrains.kotlin.idea.quickfix.KotlinIntentionActionsFactory
|
||||
import org.jetbrains.kotlin.idea.util.allowedValOrVar
|
||||
import org.jetbrains.kotlin.idea.util.application.executeWriteCommand
|
||||
import org.jetbrains.kotlin.idea.util.hasInlineModifier
|
||||
import org.jetbrains.kotlin.idea.util.isEffectivelyActual
|
||||
import org.jetbrains.kotlin.idea.util.liftToExpected
|
||||
import org.jetbrains.kotlin.idea.util.module
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.containingClass
|
||||
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
|
||||
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
|
||||
import org.jetbrains.kotlin.psi.psiUtil.hasActualModifier
|
||||
@@ -197,17 +196,11 @@ private tailrec fun KtDeclaration.recursivelyMakeActual() {
|
||||
containingClassOrObject?.takeUnless(KtDeclaration::hasActualModifier)?.recursivelyMakeActual()
|
||||
}
|
||||
|
||||
private fun KtDeclaration.isAlwaysActual(): Boolean {
|
||||
val primaryConstructor = when (this) {
|
||||
is KtPrimaryConstructor -> this
|
||||
is KtParameter -> (parent as? KtParameterList)?.parent as? KtPrimaryConstructor
|
||||
else -> null
|
||||
} ?: return false
|
||||
|
||||
return primaryConstructor.containingClass()?.let {
|
||||
it.isAnnotation() || it.hasInlineModifier()
|
||||
} ?: false
|
||||
}
|
||||
private fun KtDeclaration.isAlwaysActual(): Boolean = when (this) {
|
||||
is KtPrimaryConstructor -> this
|
||||
is KtParameter -> (parent as? KtParameterList)?.parent as? KtPrimaryConstructor
|
||||
else -> null
|
||||
}?.allowedValOrVar() ?: false
|
||||
|
||||
class CreateExpectedPropertyFix(
|
||||
property: KtNamedDeclaration,
|
||||
|
||||
@@ -8,7 +8,9 @@ package org.jetbrains.kotlin.idea.util
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.KtCallExpression
|
||||
import org.jetbrains.kotlin.psi.KtModifierListOwner
|
||||
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
|
||||
import org.jetbrains.kotlin.psi.KtTypeArgumentList
|
||||
import org.jetbrains.kotlin.psi.psiUtil.containingClass
|
||||
|
||||
|
||||
fun KtCallExpression.replaceOrCreateTypeArgumentList(newTypeArgumentList: KtTypeArgumentList) {
|
||||
@@ -19,4 +21,8 @@ fun KtCallExpression.replaceOrCreateTypeArgumentList(newTypeArgumentList: KtType
|
||||
)
|
||||
}
|
||||
|
||||
fun KtModifierListOwner.hasInlineModifier() = hasModifier(KtTokens.INLINE_KEYWORD)
|
||||
fun KtModifierListOwner.hasInlineModifier() = hasModifier(KtTokens.INLINE_KEYWORD)
|
||||
|
||||
fun KtPrimaryConstructor.allowedValOrVar(): Boolean = containingClass()?.let {
|
||||
it.isAnnotation() || it.hasInlineModifier()
|
||||
} ?: false
|
||||
@@ -0,0 +1,2 @@
|
||||
// DISABLE-ERRORS
|
||||
expect annotation class A(<caret>a: Int)
|
||||
@@ -0,0 +1,2 @@
|
||||
// DISABLE-ERRORS
|
||||
expect annotation class A(val a: Int)
|
||||
@@ -0,0 +1,2 @@
|
||||
// DISABLE-ERRORS
|
||||
expect inline class A(<caret>a: Int)
|
||||
@@ -0,0 +1,2 @@
|
||||
// DISABLE-ERRORS
|
||||
expect inline class A(val a: Int)
|
||||
@@ -1931,6 +1931,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
runTest("idea/testData/intentions/addValOrVar/dataClass.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("expectAnnotation.kt")
|
||||
public void testExpectAnnotation() throws Exception {
|
||||
runTest("idea/testData/intentions/addValOrVar/expectAnnotation.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("expectClass.kt")
|
||||
public void testExpectClass() throws Exception {
|
||||
runTest("idea/testData/intentions/addValOrVar/expectClass.kt");
|
||||
@@ -1941,6 +1946,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
runTest("idea/testData/intentions/addValOrVar/expectClass2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("expectInlineClass.kt")
|
||||
public void testExpectInlineClass() throws Exception {
|
||||
runTest("idea/testData/intentions/addValOrVar/expectInlineClass.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("funParameter.kt")
|
||||
public void testFunParameter() throws Exception {
|
||||
runTest("idea/testData/intentions/addValOrVar/funParameter.kt");
|
||||
|
||||
Reference in New Issue
Block a user