From b10de3dd2d93d0690ef7197fdbf76bad7fb08cbd Mon Sep 17 00:00:00 2001 From: Tianyu Geng Date: Fri, 11 Jun 2021 15:39:01 -0700 Subject: [PATCH] FIR IDE: Move RemoveNullableFix to fe-independent --- .../kotlin/idea/quickfix/RemoveNullableFix.kt | 61 +++++++++++++++++ .../kotlin/idea/quickfix/QuickFixRegistrar.kt | 8 +-- .../kotlin/idea/quickfix/RemoveNullableFix.kt | 68 ------------------- 3 files changed, 65 insertions(+), 72 deletions(-) create mode 100644 idea/idea-frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt delete mode 100644 idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt diff --git a/idea/idea-frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt b/idea/idea-frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt new file mode 100644 index 00000000000..1fe726658d2 --- /dev/null +++ b/idea/idea-frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt @@ -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(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 { + return quickFixesPsiBasedFactory { e -> + when (typeOfError) { + NullableKind.REDUNDANT, NullableKind.SUPERTYPE, NullableKind.USELESS -> { + val nullType = e.getNonStrictParentOfType() + 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)) + } + } + } + } + } +} diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt index e3ac7880890..f7728accc0b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/QuickFixRegistrar.kt @@ -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) diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt deleted file mode 100644 index 1e089db92f9..00000000000 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/RemoveNullableFix.kt +++ /dev/null @@ -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(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? { - val nullType = diagnostic.psiElement.getNonStrictParentOfType() - if (nullType?.innerType == null) return null - return RemoveNullableFix(nullType, typeOfError) - } - } - - object LATEINIT_FACTORY : KotlinSingleIntentionActionFactory() { - override fun createAction(diagnostic: Diagnostic): KotlinQuickFixAction? { - 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) - } - } -}