Use expression body: reformat
This commit is contained in:
@@ -33,7 +33,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
|
|||||||
|
|
||||||
class UseExpressionBodyInspection(private val convertEmptyToUnit: Boolean) : AbstractKotlinInspection() {
|
class UseExpressionBodyInspection(private val convertEmptyToUnit: Boolean) : AbstractKotlinInspection() {
|
||||||
|
|
||||||
constructor(): this(convertEmptyToUnit = true)
|
constructor() : this(convertEmptyToUnit = true)
|
||||||
|
|
||||||
private data class Status(val toHighlight: PsiElement?, val subject: String, val highlightType: ProblemHighlightType)
|
private data class Status(val toHighlight: PsiElement?, val subject: String, val highlightType: ProblemHighlightType)
|
||||||
|
|
||||||
@@ -46,7 +46,8 @@ class UseExpressionBodyInspection(private val convertEmptyToUnit: Boolean) : Abs
|
|||||||
val value = valueStatement.getValue()
|
val value = valueStatement.getValue()
|
||||||
if (value.anyDescendantOfType<KtReturnExpression>(
|
if (value.anyDescendantOfType<KtReturnExpression>(
|
||||||
canGoInside = { it !is KtFunctionLiteral && it !is KtNamedFunction && it !is KtPropertyAccessor }
|
canGoInside = { it !is KtFunctionLiteral && it !is KtNamedFunction && it !is KtPropertyAccessor }
|
||||||
)) return null
|
)
|
||||||
|
) return null
|
||||||
|
|
||||||
val toHighlight = valueStatement.toHighlight()
|
val toHighlight = valueStatement.toHighlight()
|
||||||
return when {
|
return when {
|
||||||
@@ -58,39 +59,39 @@ class UseExpressionBodyInspection(private val convertEmptyToUnit: Boolean) : Abs
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) =
|
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean) =
|
||||||
declarationVisitor(fun(declaration) {
|
declarationVisitor(fun(declaration) {
|
||||||
declaration as? KtDeclarationWithBody ?: return
|
declaration as? KtDeclarationWithBody ?: return
|
||||||
val (toHighlightElement, suffix, highlightType) = statusFor(declaration) ?: return
|
val (toHighlightElement, suffix, highlightType) = statusFor(declaration) ?: return
|
||||||
// Change range to start with left brace
|
// Change range to start with left brace
|
||||||
val hasHighlighting = highlightType != INFORMATION
|
val hasHighlighting = highlightType != INFORMATION
|
||||||
|
|
||||||
fun defaultLevel(): HighlightDisplayLevel {
|
fun defaultLevel(): HighlightDisplayLevel {
|
||||||
val project = declaration.project
|
val project = declaration.project
|
||||||
val inspectionProfileManager = ProjectInspectionProfileManager.getInstance(project)
|
val inspectionProfileManager = ProjectInspectionProfileManager.getInstance(project)
|
||||||
val inspectionProfile = inspectionProfileManager.currentProfile
|
val inspectionProfile = inspectionProfileManager.currentProfile
|
||||||
val state = inspectionProfile.getToolDefaultState("UseExpressionBody", project)
|
val state = inspectionProfile.getToolDefaultState("UseExpressionBody", project)
|
||||||
return state.level
|
return state.level
|
||||||
|
}
|
||||||
|
|
||||||
|
val toHighlightRange = toHighlightElement?.textRange?.let {
|
||||||
|
if (hasHighlighting && defaultLevel() != HighlightDisplayLevel.DO_NOT_SHOW) {
|
||||||
|
it
|
||||||
|
} else {
|
||||||
|
// Extend range to [left brace..end of highlight element]
|
||||||
|
val offset = (declaration.blockExpression()?.lBrace?.startOffset ?: it.startOffset) - it.startOffset
|
||||||
|
it.shiftRight(offset).grown(-offset)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val toHighlightRange = toHighlightElement?.textRange?.let {
|
holder.registerProblemWithoutOfflineInformation(
|
||||||
if (hasHighlighting && defaultLevel() != HighlightDisplayLevel.DO_NOT_SHOW) {
|
declaration,
|
||||||
it
|
"Use expression body instead of $suffix",
|
||||||
} else {
|
isOnTheFly,
|
||||||
// Extend range to [left brace..end of highlight element]
|
highlightType,
|
||||||
val offset = (declaration.blockExpression()?.lBrace?.startOffset ?: it.startOffset) - it.startOffset
|
toHighlightRange?.shiftRight(-declaration.startOffset),
|
||||||
it.shiftRight(offset).grown(-offset)
|
ConvertToExpressionBodyFix()
|
||||||
}
|
)
|
||||||
}
|
})
|
||||||
|
|
||||||
holder.registerProblemWithoutOfflineInformation(
|
|
||||||
declaration,
|
|
||||||
"Use expression body instead of $suffix",
|
|
||||||
isOnTheFly,
|
|
||||||
highlightType,
|
|
||||||
toHighlightRange?.shiftRight(-declaration.startOffset),
|
|
||||||
ConvertToExpressionBodyFix()
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
private fun KtDeclarationWithBody.findValueStatement(): KtExpression? {
|
private fun KtDeclarationWithBody.findValueStatement(): KtExpression? {
|
||||||
val body = blockExpression() ?: return null
|
val body = blockExpression() ?: return null
|
||||||
|
|||||||
Reference in New Issue
Block a user