FIR IDE: move RemoveModifierFix to ...

idea-frontend-independent
This commit is contained in:
Tianyu Geng
2021-02-10 14:29:43 -08:00
committed by Ilya Kirillov
parent 75f6780b90
commit 6882cf820e
11 changed files with 58 additions and 89 deletions
@@ -1,31 +0,0 @@
/*
* 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.
*/
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
interface KotlinUniversalQuickFix : IntentionAction, LocalQuickFix {
@JvmDefault
override fun getName() = text
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
invoke(project, null, descriptor.psiElement?.containingFile)
}
}
@@ -65,4 +65,9 @@ find.usages.type.packageMemberAccess=Package member access
and.delete.initializer=\ and delete initializer
change.to.val=Change to val
change.to.var=Change to var
change.to.var=Change to var
remove.redundant.0.modifier=Remove redundant ''{0}'' modifier
make.0.not.1=Make {0} not {1}
remove.0.modifier=Remove ''{0}'' modifier
remove.modifier=Remove modifier
@@ -0,0 +1,20 @@
/*
* 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.inspections
import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
interface KotlinUniversalQuickFix : IntentionAction, LocalQuickFix {
@JvmDefault
override fun getName() = text
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
invoke(project, null, descriptor.psiElement?.containingFile)
}
}
@@ -1,17 +1,6 @@
/*
* 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
@@ -1,17 +1,6 @@
/*
* 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.
* 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
@@ -9,8 +9,9 @@ import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.KotlinBundleIndependent
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
@@ -27,15 +28,15 @@ class RemoveModifierFix(
val modifierText = modifier.value
when {
isRedundant ->
KotlinBundle.message("remove.redundant.0.modifier", modifierText)
KotlinBundleIndependent.message("remove.redundant.0.modifier", modifierText)
modifier === KtTokens.ABSTRACT_KEYWORD || modifier === KtTokens.OPEN_KEYWORD ->
KotlinBundle.message("make.0.not.1", AddModifierFix.getElementName(element), modifierText)
KotlinBundleIndependent.message("make.0.not.1", getElementName(element), modifierText)
else ->
KotlinBundle.message("remove.0.modifier", modifierText, modifier)
KotlinBundleIndependent.message("remove.0.modifier", modifierText, modifier)
}
}
override fun getFamilyName() = KotlinBundle.message("remove.modifier")
override fun getFamilyName() = KotlinBundleIndependent.message("remove.modifier")
override fun getText() = text
@@ -137,5 +138,23 @@ class RemoveModifierFix(
listOf(RemoveModifierFix(funInterface, KtTokens.FUN_KEYWORD, isRedundant = false))
}
}
fun getElementName(modifierListOwner: KtModifierListOwner): String {
var name: String? = null
if (modifierListOwner is PsiNameIdentifierOwner) {
val nameIdentifier = modifierListOwner.nameIdentifier
if (nameIdentifier != null) {
name = nameIdentifier.text
} else if ((modifierListOwner as? KtObjectDeclaration)?.isCompanion() == true) {
name = "companion object"
}
} else if (modifierListOwner is KtPropertyAccessor) {
name = modifierListOwner.namePlaceholder.text
}
if (name == null) {
name = modifierListOwner.text
}
return "'$name'"
}
}
}
@@ -1104,7 +1104,6 @@ remove.from.annotation.argument=Remove @ from annotation argument
remove.default.parameter.value=Remove default parameter value
remove.final.upper.bound=Remove final upper bound
remove.function.body=Remove function body
remove.redundant.0.modifier=Remove redundant ''{0}'' modifier
make.0.not.1=Make {0} not {1}
remove.0.modifier=Remove ''{0}'' modifier
remove.modifier=Remove modifier
@@ -20,7 +20,6 @@ import com.intellij.codeInsight.intention.IntentionAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiNameIdentifierOwner
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
@@ -43,14 +42,14 @@ import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
import org.jetbrains.kotlin.types.TypeUtils
open class AddModifierFix(
open class AddModifierFix(
element: KtModifierListOwner,
protected val modifier: KtModifierKeywordToken
) : KotlinCrossLanguageQuickFixAction<KtModifierListOwner>(element), KotlinUniversalQuickFix {
override fun getText(): String {
val element = element ?: return ""
if (modifier in modalityModifiers || modifier in VISIBILITY_MODIFIERS || modifier == CONST_KEYWORD) {
return KotlinBundle.message("fix.add.modifier.text", getElementName(element), modifier.value)
return KotlinBundle.message("fix.add.modifier.text", RemoveModifierFix.getElementName(element), modifier.value)
}
return KotlinBundle.message("fix.add.modifier.text.generic", modifier.value)
}
@@ -90,24 +89,6 @@ import org.jetbrains.kotlin.types.TypeUtils
private val modalityModifiers = setOf(ABSTRACT_KEYWORD, OPEN_KEYWORD, FINAL_KEYWORD)
fun getElementName(modifierListOwner: KtModifierListOwner): String {
var name: String? = null
if (modifierListOwner is PsiNameIdentifierOwner) {
val nameIdentifier = modifierListOwner.nameIdentifier
if (nameIdentifier != null) {
name = nameIdentifier.text
} else if ((modifierListOwner as? KtObjectDeclaration)?.isCompanion() == true) {
name = "companion object"
}
} else if (modifierListOwner is KtPropertyAccessor) {
name = modifierListOwner.namePlaceholder.text
}
if (name == null) {
name = modifierListOwner.text
}
return "'$name'"
}
fun createFactory(modifier: KtModifierKeywordToken): KotlinSingleIntentionActionFactory {
return createFactory(modifier, KtModifierListOwner::class.java)
}
@@ -50,10 +50,10 @@ class AddReifiedToTypeParameterOfFunctionFix(
) : AddModifierFix(typeParameter, KtTokens.REIFIED_KEYWORD) {
private val inlineFix = AddInlineToFunctionWithReifiedFix(function)
private val elementName = getElementName(function)
private val elementName = RemoveModifierFix.getElementName(function)
override fun getText() =
element?.let { KotlinBundle.message("fix.make.type.parameter.reified", getElementName(it), elementName) } ?: ""
element?.let { KotlinBundle.message("fix.make.type.parameter.reified", RemoveModifierFix.getElementName(it), elementName) } ?: ""
override fun invokeImpl(project: Project, editor: Editor?, file: PsiFile) {
super.invokeImpl(project, editor, file)
@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.jetbrains.kotlin.idea.quickfix.KotlinQuickFixAction
import org.jetbrains.kotlin.idea.quickfix.KotlinSingleIntentionActionFactory
import org.jetbrains.kotlin.idea.resolve.frontendService
import org.jetbrains.kotlin.idea.resolve.getDataFlowValueFactory
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory
import org.jetbrains.kotlin.idea.core.util.range
import org.jetbrains.kotlin.idea.quickfix.AddExclExclCallFix
import org.jetbrains.kotlin.idea.quickfix.KotlinIntentionActionsFactory
import org.jetbrains.kotlin.idea.quickfix.KotlinSingleIntentionActionFactory
import org.jetbrains.kotlin.idea.quickfix.QuickFixFactory
import org.jetbrains.kotlin.idea.resolve.ResolutionFacade