From 34609b264d939290e477389895daadfbf02b25d0 Mon Sep 17 00:00:00 2001 From: Mark Punzalan Date: Wed, 14 Apr 2021 20:26:15 +0000 Subject: [PATCH] FIR IDE: Move RemoveExclExclCallFix to idea-frontend-independent. --- .../kotlin/idea/quickfix/ExclExclCallFixes.kt | 52 ++++++++++++++++++ ...ExclCallFixes.kt => AddExclExclCallFix.kt} | 54 ++----------------- 2 files changed, 56 insertions(+), 50 deletions(-) create mode 100644 idea/idea-frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/ExclExclCallFixes.kt rename idea/src/org/jetbrains/kotlin/idea/quickfix/{ExclExclCallFixes.kt => AddExclExclCallFix.kt} (79%) diff --git a/idea/idea-frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/ExclExclCallFixes.kt b/idea/idea-frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/ExclExclCallFixes.kt new file mode 100644 index 00000000000..533a5cea793 --- /dev/null +++ b/idea/idea-frontend-independent/src/org/jetbrains/kotlin/idea/quickfix/ExclExclCallFixes.kt @@ -0,0 +1,52 @@ +/* + * 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.codeInsight.FileModificationService +import com.intellij.codeInsight.intention.HighPriorityAction +import com.intellij.codeInsight.intention.IntentionAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.idea.KotlinBundle +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtPostfixExpression +import org.jetbrains.kotlin.psi.KtPsiFactory + +abstract class ExclExclCallFix(psiElement: PsiElement) : KotlinPsiOnlyQuickFixAction(psiElement) { + override fun getFamilyName(): String = text + + override fun startInWriteAction(): Boolean = true +} + +class RemoveExclExclCallFix(psiElement: PsiElement) : ExclExclCallFix(psiElement), CleanupFix, HighPriorityAction { + override fun getText(): String = KotlinBundle.message("fix.remove.non.null.assertion") + + override fun isAvailable(project: Project, editor: Editor?, file: KtFile): Boolean = + getExclExclPostfixExpression() != null + + override fun invoke(project: Project, editor: Editor?, file: KtFile) { + if (!FileModificationService.getInstance().prepareFileForWrite(file)) return + + val postfixExpression = getExclExclPostfixExpression() ?: return + val expression = KtPsiFactory(project).createExpression(postfixExpression.baseExpression!!.text) + postfixExpression.replace(expression) + } + + private fun getExclExclPostfixExpression(): KtPostfixExpression? { + val operationParent = element?.parent + if (operationParent is KtPostfixExpression && operationParent.baseExpression != null) { + return operationParent + } + return null + } + + companion object : QuickFixesPsiBasedFactory(PsiElement::class, PsiElementSuitabilityCheckers.ALWAYS_SUITABLE) { + override fun doCreateQuickFix(psiElement: PsiElement): List { + return listOfNotNull(RemoveExclExclCallFix(psiElement)) + } + } +} \ No newline at end of file diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ExclExclCallFixes.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddExclExclCallFix.kt similarity index 79% rename from idea/src/org/jetbrains/kotlin/idea/quickfix/ExclExclCallFixes.kt rename to idea/src/org/jetbrains/kotlin/idea/quickfix/AddExclExclCallFix.kt index a3fb52a7471..0a936be3257 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ExclExclCallFixes.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/AddExclExclCallFix.kt @@ -1,23 +1,11 @@ /* - * Copyright 2010-2017 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. + * 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.codeInsight.FileModificationService -import com.intellij.codeInsight.intention.HighPriorityAction import com.intellij.codeInsight.intention.IntentionAction import com.intellij.codeInsight.intention.LowPriorityAction import com.intellij.openapi.editor.Editor @@ -50,42 +38,8 @@ import org.jetbrains.kotlin.util.OperatorNameConventions import org.jetbrains.kotlin.util.isValidOperator import org.jetbrains.kotlin.utils.addToStdlib.safeAs - -abstract class ExclExclCallFix(psiElement: PsiElement) : KotlinQuickFixAction(psiElement) { - override fun getFamilyName(): String = text - - override fun startInWriteAction(): Boolean = true -} - -class RemoveExclExclCallFix(psiElement: PsiElement) : ExclExclCallFix(psiElement), CleanupFix, HighPriorityAction { - override fun getText(): String = KotlinBundle.message("fix.remove.non.null.assertion") - - override fun isAvailable(project: Project, editor: Editor?, file: KtFile): Boolean = - getExclExclPostfixExpression() != null - - override fun invoke(project: Project, editor: Editor?, file: KtFile) { - if (!FileModificationService.getInstance().prepareFileForWrite(file)) return - - val postfixExpression = getExclExclPostfixExpression() ?: return - val expression = KtPsiFactory(project).createExpression(postfixExpression.baseExpression!!.text) - postfixExpression.replace(expression) - } - - private fun getExclExclPostfixExpression(): KtPostfixExpression? { - val operationParent = element?.parent - if (operationParent is KtPostfixExpression && operationParent.baseExpression != null) { - return operationParent - } - return null - } - - companion object : KotlinSingleIntentionActionFactory() { - override fun createAction(diagnostic: Diagnostic): IntentionAction = RemoveExclExclCallFix(diagnostic.psiElement) - } -} - -class AddExclExclCallFix(psiElement: PsiElement, val checkImplicitReceivers: Boolean) : ExclExclCallFix(psiElement), LowPriorityAction { - constructor(psiElement: PsiElement) : this(psiElement, true) +class AddExclExclCallFix(psiElement: PsiElement, val checkImplicitReceivers: Boolean = true) : ExclExclCallFix(psiElement), + LowPriorityAction { override fun getText() = KotlinBundle.message("fix.introduce.non.null.assertion")