Migrate RemoveModifierFix factories to QuickFixesPsiBasedFactory to use in FIR IDE
This commit is contained in:
@@ -7,10 +7,10 @@ package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil
|
||||
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
@@ -53,84 +53,72 @@ class RemoveModifierFix(
|
||||
fun createRemoveModifierFromListOwnerFactory(
|
||||
modifier: KtModifierKeywordToken,
|
||||
isRedundant: Boolean = false
|
||||
): KotlinSingleIntentionActionFactory {
|
||||
return object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): RemoveModifierFix? {
|
||||
val modifierListOwner = QuickFixUtil.getParentElementOfType(diagnostic, KtModifierListOwner::class.java) ?: return null
|
||||
): QuickFixesPsiBasedFactory {
|
||||
return object : QuickFixesPsiBasedFactory() {
|
||||
override fun createQuickFix(psiElement: PsiElement): RemoveModifierFix? {
|
||||
val modifierListOwner = PsiTreeUtil.getParentOfType(psiElement, KtModifierListOwner::class.java, false) ?: return null
|
||||
return RemoveModifierFix(modifierListOwner, modifier, isRedundant)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun createRemoveModifierFactory(isRedundant: Boolean = false): KotlinSingleIntentionActionFactory {
|
||||
return object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): RemoveModifierFix? {
|
||||
val psiElement = diagnostic.psiElement
|
||||
val elementType = psiElement.node.elementType as? KtModifierKeywordToken ?: return null
|
||||
val modifierListOwner = psiElement.getStrictParentOfType<KtModifierListOwner>() ?: return null
|
||||
return RemoveModifierFix(modifierListOwner, elementType, isRedundant)
|
||||
}
|
||||
fun createRemoveModifierFactory(isRedundant: Boolean = false): QuickFixesPsiBasedFactory<PsiElement> {
|
||||
return quickFixesPsiBasedFactory { psiElement: PsiElement ->
|
||||
val elementType = psiElement.node.elementType as? KtModifierKeywordToken ?: return@quickFixesPsiBasedFactory emptyList()
|
||||
val modifierListOwner = psiElement.getStrictParentOfType<KtModifierListOwner>()
|
||||
?: return@quickFixesPsiBasedFactory emptyList()
|
||||
listOf(RemoveModifierFix(modifierListOwner, elementType, isRedundant))
|
||||
}
|
||||
}
|
||||
|
||||
fun createRemoveProjectionFactory(isRedundant: Boolean): KotlinSingleIntentionActionFactory {
|
||||
return object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): RemoveModifierFix? {
|
||||
val projection = diagnostic.psiElement as KtTypeProjection
|
||||
val elementType = projection.projectionToken?.node?.elementType as? KtModifierKeywordToken ?: return null
|
||||
return RemoveModifierFix(projection, elementType, isRedundant)
|
||||
}
|
||||
|
||||
fun createRemoveProjectionFactory(isRedundant: Boolean): QuickFixesPsiBasedFactory<PsiElement> {
|
||||
return quickFixesPsiBasedFactory { psiElement: PsiElement ->
|
||||
val projection = psiElement as KtTypeProjection
|
||||
val elementType = projection.projectionToken?.node?.elementType as? KtModifierKeywordToken
|
||||
?: return@quickFixesPsiBasedFactory listOf()
|
||||
listOf(RemoveModifierFix(projection, elementType, isRedundant))
|
||||
}
|
||||
}
|
||||
|
||||
fun createRemoveVarianceFactory(): KotlinSingleIntentionActionFactory {
|
||||
return object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): RemoveModifierFix? {
|
||||
val psiElement = diagnostic.psiElement as KtTypeParameter
|
||||
val modifier = when (psiElement.variance) {
|
||||
Variance.IN_VARIANCE -> KtTokens.IN_KEYWORD
|
||||
Variance.OUT_VARIANCE -> KtTokens.OUT_KEYWORD
|
||||
else -> return null
|
||||
}
|
||||
return RemoveModifierFix(psiElement, modifier, isRedundant = false)
|
||||
fun createRemoveVarianceFactory(): QuickFixesPsiBasedFactory<PsiElement> {
|
||||
return quickFixesPsiBasedFactory { psiElement: PsiElement ->
|
||||
require(psiElement is KtTypeParameter)
|
||||
val modifier = when (psiElement.variance) {
|
||||
Variance.IN_VARIANCE -> KtTokens.IN_KEYWORD
|
||||
Variance.OUT_VARIANCE -> KtTokens.OUT_KEYWORD
|
||||
else -> return@quickFixesPsiBasedFactory emptyList()
|
||||
}
|
||||
listOf(RemoveModifierFix(psiElement, modifier, isRedundant = false))
|
||||
}
|
||||
}
|
||||
|
||||
fun createRemoveSuspendFactory(): KotlinSingleIntentionActionFactory {
|
||||
return object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): RemoveModifierFix? {
|
||||
val suspendKeyword = diagnostic.psiElement
|
||||
val modifierList = suspendKeyword.parent as KtDeclarationModifierList
|
||||
val type = modifierList.parent as KtTypeReference
|
||||
if (!type.hasModifier(KtTokens.SUSPEND_KEYWORD)) return null
|
||||
return RemoveModifierFix(type, KtTokens.SUSPEND_KEYWORD, isRedundant = false)
|
||||
}
|
||||
fun createRemoveSuspendFactory(): QuickFixesPsiBasedFactory<PsiElement> {
|
||||
return quickFixesPsiBasedFactory { psiElement: PsiElement ->
|
||||
val modifierList = psiElement.parent as KtDeclarationModifierList
|
||||
val type = modifierList.parent as KtTypeReference
|
||||
if (!type.hasModifier(KtTokens.SUSPEND_KEYWORD)) return@quickFixesPsiBasedFactory emptyList()
|
||||
listOf(RemoveModifierFix(type, KtTokens.SUSPEND_KEYWORD, isRedundant = false))
|
||||
}
|
||||
}
|
||||
|
||||
fun createRemoveLateinitFactory(): KotlinSingleIntentionActionFactory {
|
||||
return object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): RemoveModifierFix? {
|
||||
val keyword = diagnostic.psiElement
|
||||
val modifierList = keyword.parent as? KtDeclarationModifierList ?: return null
|
||||
val property = modifierList.parent as? KtProperty ?: return null
|
||||
if (!property.hasModifier(KtTokens.LATEINIT_KEYWORD)) return null
|
||||
return RemoveModifierFix(property, KtTokens.LATEINIT_KEYWORD, isRedundant = false)
|
||||
}
|
||||
fun createRemoveLateinitFactory(): QuickFixesPsiBasedFactory<PsiElement> {
|
||||
return quickFixesPsiBasedFactory { psiElement: PsiElement ->
|
||||
val modifierList = psiElement.parent as? KtDeclarationModifierList ?: return@quickFixesPsiBasedFactory emptyList()
|
||||
val property = modifierList.parent as? KtProperty ?: return@quickFixesPsiBasedFactory emptyList()
|
||||
if (!property.hasModifier(KtTokens.LATEINIT_KEYWORD)) return@quickFixesPsiBasedFactory emptyList()
|
||||
listOf(RemoveModifierFix(property, KtTokens.LATEINIT_KEYWORD, isRedundant = false))
|
||||
}
|
||||
}
|
||||
|
||||
fun createRemoveFunFromInterfaceFactory(): KotlinSingleIntentionActionFactory {
|
||||
return object : KotlinSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): RemoveModifierFix? {
|
||||
val keyword = diagnostic.psiElement
|
||||
val modifierList = keyword.parent as? KtDeclarationModifierList ?: return null
|
||||
val funInterface = (modifierList.parent as? KtClass)?.takeIf {
|
||||
it.isInterface() && it.hasModifier(KtTokens.FUN_KEYWORD)
|
||||
} ?: return null
|
||||
return RemoveModifierFix(funInterface, KtTokens.FUN_KEYWORD, isRedundant = false)
|
||||
}
|
||||
fun createRemoveFunFromInterfaceFactory(): QuickFixesPsiBasedFactory<PsiElement> {
|
||||
return quickFixesPsiBasedFactory { psiElement: PsiElement ->
|
||||
val modifierList = psiElement.parent as? KtDeclarationModifierList
|
||||
?: return@quickFixesPsiBasedFactory emptyList()
|
||||
val funInterface = (modifierList.parent as? KtClass)?.takeIf {
|
||||
it.isInterface() && it.hasModifier(KtTokens.FUN_KEYWORD)
|
||||
} ?: return@quickFixesPsiBasedFactory emptyList()
|
||||
listOf(RemoveModifierFix(funInterface, KtTokens.FUN_KEYWORD, isRedundant = false))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user