FIR IDE: Move RemoveNullableFix to fe-independent

This commit is contained in:
Tianyu Geng
2021-06-11 15:39:01 -07:00
committed by Ilya Kirillov
parent 559bedf5ae
commit b10de3dd2d
3 changed files with 65 additions and 72 deletions
@@ -0,0 +1,61 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNullableType
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
class RemoveNullableFix(element: KtNullableType, private val typeOfError: NullableKind) :
KotlinPsiOnlyQuickFixAction<KtNullableType>(element) {
enum class NullableKind(val message: String) {
REDUNDANT(KotlinBundle.message("remove.redundant")),
SUPERTYPE(KotlinBundle.message("text.remove.question")),
USELESS(KotlinBundle.message("remove.useless")),
PROPERTY(KotlinBundle.message("make.not.nullable"))
}
override fun getFamilyName() = KotlinBundle.message("text.remove.question")
override fun getText() = typeOfError.message
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val element = element ?: return
val type = element.innerType ?: error("No inner type ${element.text}, should have been rejected in createFactory()")
element.replace(type)
}
companion object {
val removeForRedundant = createFactory(NullableKind.REDUNDANT)
val removeForSuperType = createFactory(NullableKind.SUPERTYPE)
val removeForUseless = createFactory(NullableKind.USELESS)
val removeForLateInitProperty = createFactory(NullableKind.PROPERTY)
private fun createFactory(typeOfError: NullableKind): QuickFixesPsiBasedFactory<KtElement> {
return quickFixesPsiBasedFactory { e ->
when (typeOfError) {
NullableKind.REDUNDANT, NullableKind.SUPERTYPE, NullableKind.USELESS -> {
val nullType = e.getNonStrictParentOfType<KtNullableType>()
if (nullType?.innerType == null) return@quickFixesPsiBasedFactory emptyList()
listOf(RemoveNullableFix(nullType, typeOfError))
}
NullableKind.PROPERTY -> {
val property = e as? KtProperty ?: return@quickFixesPsiBasedFactory emptyList()
val typeReference = property.typeReference ?: return@quickFixesPsiBasedFactory emptyList()
val typeElement = typeReference.typeElement as? KtNullableType ?: return@quickFixesPsiBasedFactory emptyList()
if (typeElement.innerType == null) return@quickFixesPsiBasedFactory emptyList()
listOf(RemoveNullableFix(typeElement, NullableKind.PROPERTY))
}
}
}
}
}
}
@@ -208,9 +208,9 @@ class QuickFixRegistrar : QuickFixContributor {
exposed.registerFactory(ChangeVisibilityOnExposureFactory)
}
REDUNDANT_NULLABLE.registerFactory(RemoveNullableFix.Factory(RemoveNullableFix.NullableKind.REDUNDANT))
NULLABLE_SUPERTYPE.registerFactory(RemoveNullableFix.Factory(RemoveNullableFix.NullableKind.SUPERTYPE))
USELESS_NULLABLE_CHECK.registerFactory(RemoveNullableFix.Factory(RemoveNullableFix.NullableKind.USELESS))
REDUNDANT_NULLABLE.registerFactory(RemoveNullableFix.removeForRedundant)
NULLABLE_SUPERTYPE.registerFactory(RemoveNullableFix.removeForSuperType)
USELESS_NULLABLE_CHECK.registerFactory(RemoveNullableFix.removeForUseless)
val implementMembersHandler = ImplementMembersHandler()
@@ -527,7 +527,7 @@ class QuickFixRegistrar : QuickFixContributor {
ANNOTATIONS_ON_BLOCK_LEVEL_EXPRESSION_ON_THE_SAME_LINE.registerFactory(AddNewLineAfterAnnotationsFix)
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(ChangeVariableMutabilityFix.LATEINIT_VAL_FACTORY)
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(RemoveNullableFix.LATEINIT_FACTORY)
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(RemoveNullableFix.removeForLateInitProperty)
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(RemovePartsFromPropertyFix.LateInitFactory)
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(RemoveModifierFix.createRemoveLateinitFactory())
INAPPLICABLE_LATEINIT_MODIFIER.registerFactory(ConvertLateinitPropertyToNotNullDelegateFix)
@@ -1,68 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.quickfix
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtNullableType
import org.jetbrains.kotlin.psi.KtProperty
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
class RemoveNullableFix(
element: KtNullableType,
private val typeOfError: NullableKind
) : KotlinQuickFixAction<KtNullableType>(element) {
enum class NullableKind(val message: String) {
REDUNDANT(KotlinBundle.message("remove.redundant")),
SUPERTYPE(KotlinBundle.message("text.remove.question")),
USELESS(KotlinBundle.message("remove.useless")),
PROPERTY(KotlinBundle.message("make.not.nullable"))
}
override fun getFamilyName() = KotlinBundle.message("text.remove.question")
override fun getText() = typeOfError.message
override fun invoke(project: Project, editor: Editor?, file: KtFile) {
val element = element ?: return
val type = element.innerType ?: error("No inner type " + element.text + ", should have been rejected in createFactory()")
element.replace(type)
}
class Factory(private val typeOfError: NullableKind) : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtNullableType>? {
val nullType = diagnostic.psiElement.getNonStrictParentOfType<KtNullableType>()
if (nullType?.innerType == null) return null
return RemoveNullableFix(nullType, typeOfError)
}
}
object LATEINIT_FACTORY : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction<KtNullableType>? {
val property = Errors.INAPPLICABLE_LATEINIT_MODIFIER.cast(diagnostic).psiElement as? KtProperty ?: return null
val typeReference = property.typeReference ?: return null
val typeElement = (typeReference.typeElement ?: return null) as? KtNullableType ?: return null
if (typeElement.innerType == null) return null
return RemoveNullableFix(typeElement, NullableKind.PROPERTY)
}
}
}