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