Extract RemoveReturnLabelFix

This commit is contained in:
Dmitry Gridin
2020-01-29 14:34:23 +07:00
parent 311860699e
commit e9dc1e07b9
2 changed files with 27 additions and 18 deletions
@@ -1,24 +1,22 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.idea.quickfix.RemoveReturnLabelFix
import org.jetbrains.kotlin.psi.KtLambdaExpression
import org.jetbrains.kotlin.psi.KtNamedFunction
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.psi.returnExpressionVisitor
class RedundantReturnLabelInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor =
returnExpressionVisitor(fun(returnExpression) {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = returnExpressionVisitor(
fun(returnExpression) {
val label = returnExpression.getTargetLabel() ?: return
val function = returnExpression.getParentOfType<KtNamedFunction>(true, KtLambdaExpression::class.java) ?: return
if (function.name == null) return
@@ -27,17 +25,8 @@ class RedundantReturnLabelInspection : AbstractKotlinInspection() {
label,
"Redundant '@$labelName'",
ProblemHighlightType.LIKE_UNUSED_SYMBOL,
RemoveReturnLabelFix(labelName)
RemoveReturnLabelFix(labelName),
)
})
},
)
}
private class RemoveReturnLabelFix(private val labelName: String) : LocalQuickFix {
override fun getName() = "Remove redundant '@$labelName'"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
descriptor.psiElement?.delete()
}
}
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2020 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.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
class RemoveReturnLabelFix(private val labelName: String) : LocalQuickFix {
override fun getName() = "Remove redundant '@$labelName'"
override fun getFamilyName() = name
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
descriptor.psiElement?.delete()
}
}