Can be parameter inspection: delete only particular modifiers (e.g. no vararg) #KT-12768 Fixed
(cherry picked from commit d4116b5)
This commit is contained in:
committed by
Mikhail Glukhikh
parent
4abcc278fd
commit
5b328e9042
@@ -33,11 +33,17 @@ import com.intellij.psi.util.PsiTreeUtil
|
|||||||
import org.jetbrains.kotlin.idea.quickfix.RemoveValVarFromParameterFix
|
import org.jetbrains.kotlin.idea.quickfix.RemoveValVarFromParameterFix
|
||||||
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
|
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
|
||||||
import org.jetbrains.kotlin.idea.search.usagesSearch.getAccessorNames
|
import org.jetbrains.kotlin.idea.search.usagesSearch.getAccessorNames
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens.*
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
|
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
|
|
||||||
|
private val CONSTRUCTOR_VAL_VAR_MODIFIERS = listOf(
|
||||||
|
OPEN_KEYWORD, FINAL_KEYWORD,
|
||||||
|
PUBLIC_KEYWORD, INTERNAL_KEYWORD, PROTECTED_KEYWORD, PRIVATE_KEYWORD,
|
||||||
|
LATEINIT_KEYWORD
|
||||||
|
)
|
||||||
|
|
||||||
class CanBeParameterInspection : AbstractKotlinInspection() {
|
class CanBeParameterInspection : AbstractKotlinInspection() {
|
||||||
private fun PsiReference.usedAsPropertyIn(klass: KtClass): Boolean {
|
private fun PsiReference.usedAsPropertyIn(klass: KtClass): Boolean {
|
||||||
if (this !is KtSimpleNameReference) return true
|
if (this !is KtSimpleNameReference) return true
|
||||||
@@ -75,7 +81,7 @@ class CanBeParameterInspection : AbstractKotlinInspection() {
|
|||||||
// Applicable to val / var parameters of a class / object primary constructors
|
// Applicable to val / var parameters of a class / object primary constructors
|
||||||
val valOrVar = parameter.valOrVarKeyword ?: return
|
val valOrVar = parameter.valOrVarKeyword ?: return
|
||||||
val name = parameter.name ?: return
|
val name = parameter.name ?: return
|
||||||
if (parameter.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return
|
if (parameter.hasModifier(OVERRIDE_KEYWORD)) return
|
||||||
if (parameter.annotationEntries.isNotEmpty()) return
|
if (parameter.annotationEntries.isNotEmpty()) return
|
||||||
val constructor = parameter.parent.parent as? KtPrimaryConstructor ?: return
|
val constructor = parameter.parent.parent as? KtPrimaryConstructor ?: return
|
||||||
val klass = constructor.getContainingClassOrObject() as? KtClass ?: return
|
val klass = constructor.getContainingClassOrObject() as? KtClass ?: return
|
||||||
@@ -119,8 +125,13 @@ class CanBeParameterInspection : AbstractKotlinInspection() {
|
|||||||
|
|
||||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||||
parameter.valOrVarKeyword?.delete()
|
parameter.valOrVarKeyword?.delete()
|
||||||
// Delete visibility / modality, if any
|
// Delete visibility / open / final / lateinit, if any
|
||||||
parameter.modifierList?.delete()
|
// Retain annotations / vararg
|
||||||
|
// override should never be here
|
||||||
|
val modifierList = parameter.modifierList ?: return
|
||||||
|
for (modifier in CONSTRUCTOR_VAL_VAR_MODIFIERS) {
|
||||||
|
modifierList.getModifier(modifier)?.delete()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Remove 'val' from parameter" "true"
|
||||||
|
|
||||||
|
class Wrapper(vararg <caret>val x: Int) {
|
||||||
|
val y = x
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// "Remove 'val' from parameter" "true"
|
||||||
|
|
||||||
|
class Wrapper(vararg x: Int) {
|
||||||
|
val y = x
|
||||||
|
}
|
||||||
@@ -714,6 +714,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/canBeParameter/usedPrivateInInitializer.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/canBeParameter/usedPrivateInInitializer.kt");
|
||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("usedVarArg.kt")
|
||||||
|
public void testUsedVarArg() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/canBeParameter/usedVarArg.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("idea/testData/quickfix/canBePrimaryConstructorProperty")
|
@TestMetadata("idea/testData/quickfix/canBePrimaryConstructorProperty")
|
||||||
|
|||||||
Reference in New Issue
Block a user