Get rid of local version of AddModifierFix

This commit is contained in:
Mikhail Glukhikh
2017-05-03 18:49:49 +03:00
parent bb76783654
commit 02345670b2
2 changed files with 10 additions and 51 deletions
@@ -1,39 +0,0 @@
/*
* Copyright 2010-2016 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.FileModificationService
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
import org.jetbrains.kotlin.psi.KtModifierListOwner
import org.jetbrains.kotlin.psi.addRemoveModifier.addModifier
open class AddModifierFix(val modifierListOwner: KtModifierListOwner,
val modifier: KtModifierKeywordToken,
val text: String) : LocalQuickFix {
override fun getName() = text
override fun getFamilyName() = text
final override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(descriptor.psiElement)) return
addModifier(modifierListOwner, modifier)
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInspection.IntentionWrapper
import com.intellij.codeInspection.LocalInspectionToolSession
import com.intellij.codeInspection.ProblemHighlightType.GENERIC_ERROR_OR_WARNING
import com.intellij.codeInspection.ProblemHighlightType.WEAK_WARNING
@@ -25,8 +26,8 @@ import com.intellij.psi.search.searches.DefinitionsScopedSearch
import org.jetbrains.kotlin.cfg.LeakingThisDescriptor.*
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
import org.jetbrains.kotlin.idea.quickfix.AddModifierFix
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.BindingContext.LEAKING_THIS
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
@@ -57,7 +58,7 @@ class LeakingThisInspection : AbstractKotlinInspection() {
val memberFix = memberDescriptorToFix?.let {
if (it.modality == Modality.OPEN) {
val modifierListOwner = DescriptorToSourceUtils.descriptorToDeclaration(it) as? KtDeclaration
MakeFinalFix.create(modifierListOwner, it.name)
createMakeFinalFix(modifierListOwner)
}
else null
}
@@ -65,7 +66,7 @@ class LeakingThisInspection : AbstractKotlinInspection() {
val klass = leakingThisDescriptor.classOrObject as? KtClass
val classFix =
if (klass != null && klass.hasModifier(KtTokens.OPEN_KEYWORD)) {
MakeFinalFix.create(klass, klass.nameAsSafeName)
createMakeFinalFix(klass)
}
else null
@@ -81,16 +82,13 @@ class LeakingThisInspection : AbstractKotlinInspection() {
}
}
class MakeFinalFix private constructor(modifierListOwner: KtModifierListOwner, name: Name) :
AddModifierFix(modifierListOwner, KtTokens.FINAL_KEYWORD, "Make '$name' final") {
companion object {
fun create(declaration: KtDeclaration?, name: Name): MakeFinalFix? {
declaration ?: return null
val useScope = declaration.useScope
if (DefinitionsScopedSearch.search(declaration, useScope).findFirst() != null) return null
return MakeFinalFix(declaration, name)
}
companion object {
private fun createMakeFinalFix(declaration: KtDeclaration?): IntentionWrapper? {
declaration ?: return null
val useScope = declaration.useScope
if (DefinitionsScopedSearch.search(declaration, useScope).findFirst() != null) return null
return IntentionWrapper(AddModifierFix(declaration, KtTokens.FINAL_KEYWORD), declaration.containingFile)
}
}
}