Lift return out refactoring: extract function for problem report
This commit is contained in:
committed by
Mikhail Glukhikh
parent
3e6007e3d3
commit
ad8c58d176
+27
-29
@@ -17,6 +17,8 @@
|
|||||||
package org.jetbrains.kotlin.idea.inspections
|
package org.jetbrains.kotlin.idea.inspections
|
||||||
|
|
||||||
import com.intellij.codeInspection.*
|
import com.intellij.codeInspection.*
|
||||||
|
import com.intellij.codeInspection.ProblemHighlightType.GENERIC_ERROR_OR_WARNING
|
||||||
|
import com.intellij.codeInspection.ProblemHighlightType.INFORMATION
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils
|
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils
|
||||||
@@ -38,44 +40,40 @@ class LiftReturnOrAssignmentInspection : AbstractKotlinInspection() {
|
|||||||
if (foldableReturns?.isNotEmpty() == true) {
|
if (foldableReturns?.isNotEmpty() == true) {
|
||||||
val hasOtherReturns = expression.anyDescendantOfType<KtReturnExpression> { it !in foldableReturns }
|
val hasOtherReturns = expression.anyDescendantOfType<KtReturnExpression> { it !in foldableReturns }
|
||||||
val isSerious = !hasOtherReturns && foldableReturns.size > 1
|
val isSerious = !hasOtherReturns && foldableReturns.size > 1
|
||||||
val verb = if (isSerious) "should" else "can"
|
registerProblem(expression, keyword, isSerious, LiftReturnOutFix(keyword.text))
|
||||||
val description = "Return $verb be lifted out of '${keyword.text}'"
|
|
||||||
holder.registerProblemWithoutOfflineInformation(
|
|
||||||
expression,
|
|
||||||
description,
|
|
||||||
isOnTheFly,
|
|
||||||
if (isSerious) ProblemHighlightType.GENERIC_ERROR_OR_WARNING
|
|
||||||
else ProblemHighlightType.INFORMATION,
|
|
||||||
keyword.textRange?.shiftRight(-expression.startOffset),
|
|
||||||
LiftReturnOutFix(keyword.text)
|
|
||||||
)
|
|
||||||
foldableReturns.forEach {
|
foldableReturns.forEach {
|
||||||
holder.registerProblemWithoutOfflineInformation(
|
registerProblem(expression, keyword, isSerious, LiftReturnOutFix(keyword.text), it, INFORMATION)
|
||||||
expression,
|
|
||||||
description,
|
|
||||||
isOnTheFly,
|
|
||||||
ProblemHighlightType.INFORMATION,
|
|
||||||
it.returnKeyword.textRange?.shiftRight(-expression.startOffset),
|
|
||||||
LiftReturnOutFix(keyword.text)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val assignmentNumber = BranchedFoldingUtils.getFoldableAssignmentNumber(expression)
|
val assignmentNumber = BranchedFoldingUtils.getFoldableAssignmentNumber(expression)
|
||||||
if (assignmentNumber > 0) {
|
if (assignmentNumber > 0) {
|
||||||
val verb = if (assignmentNumber > 1) "should" else "can"
|
val isSerious = assignmentNumber > 1
|
||||||
holder.registerProblemWithoutOfflineInformation(
|
registerProblem(expression, keyword, isSerious, LiftAssignmentOutFix(keyword.text))
|
||||||
expression,
|
|
||||||
"Assignment $verb be lifted out of '${keyword.text}'",
|
|
||||||
isOnTheFly,
|
|
||||||
if (assignmentNumber > 1) ProblemHighlightType.GENERIC_ERROR_OR_WARNING
|
|
||||||
else ProblemHighlightType.INFORMATION,
|
|
||||||
keyword.textRange?.shiftRight(-expression.startOffset),
|
|
||||||
LiftAssignmentOutFix(keyword.text)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun registerProblem(
|
||||||
|
expression: KtExpression,
|
||||||
|
keyword: PsiElement,
|
||||||
|
isSerious: Boolean,
|
||||||
|
fix: LocalQuickFix,
|
||||||
|
highlightElement: PsiElement = keyword,
|
||||||
|
highlightType: ProblemHighlightType = if (isSerious) GENERIC_ERROR_OR_WARNING else INFORMATION
|
||||||
|
) {
|
||||||
|
val subject = if (fix is LiftReturnOutFix) "Return" else "Assignment"
|
||||||
|
val verb = if (isSerious) "should" else "can"
|
||||||
|
holder.registerProblemWithoutOfflineInformation(
|
||||||
|
expression,
|
||||||
|
"$subject $verb be lifted out of '${keyword.text}'",
|
||||||
|
isOnTheFly,
|
||||||
|
highlightType,
|
||||||
|
highlightElement.textRange?.shiftRight(-expression.startOffset),
|
||||||
|
fix
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
override fun visitIfExpression(expression: KtIfExpression) {
|
override fun visitIfExpression(expression: KtIfExpression) {
|
||||||
super.visitIfExpression(expression)
|
super.visitIfExpression(expression)
|
||||||
visitIfOrWhenOrTry(expression, expression.ifKeyword)
|
visitIfOrWhenOrTry(expression, expression.ifKeyword)
|
||||||
|
|||||||
Reference in New Issue
Block a user