Extend range of "use expression body" to left brace..end of return
Range is extended iff we are in DO_NOT_SHOW case, otherwise just return is highlighted to avoid ugly highlighting So #KT-19771 Fixed
This commit is contained in:
@@ -72,14 +72,26 @@ class UseExpressionBodyInspection(private val convertEmptyToUnit: Boolean) : Abs
|
||||
super.visitDeclaration(declaration)
|
||||
|
||||
declaration as? KtDeclarationWithBody ?: return
|
||||
val (toHighlight, suffix, highlightType) = statusFor(declaration) ?: return
|
||||
val (toHighlightElement, suffix, highlightType) = statusFor(declaration) ?: return
|
||||
// Change range to start with left brace
|
||||
val hasHighlighting = highlightType != INFORMATION
|
||||
val toHighlightRange = toHighlightElement?.textRange?.let {
|
||||
if (hasHighlighting) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
holder.registerProblemWithoutOfflineInformation(
|
||||
declaration,
|
||||
"Use expression body instead of $suffix",
|
||||
isOnTheFly,
|
||||
highlightType,
|
||||
toHighlight?.textRange?.shiftRight(-declaration.startOffset),
|
||||
toHighlightRange?.shiftRight(-declaration.startOffset),
|
||||
ConvertToExpressionBodyFix()
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
// PROBLEM: Use expression body instead of return
|
||||
|
||||
fun simple(): Int {<caret>
|
||||
return 1 *
|
||||
(2 + 3)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// HIGHLIGHT: INFORMATION
|
||||
// PROBLEM: Use expression body instead of return
|
||||
|
||||
fun simple(): Int = 1 *
|
||||
(2 + 3)
|
||||
@@ -3965,6 +3965,12 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("leftBrace.kt")
|
||||
public void testLeftBrace() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/useExpressionBody/leftBrace.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("multiLine.kt")
|
||||
public void testMultiLine() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/inspectionsLocal/useExpressionBody/multiLine.kt");
|
||||
|
||||
Reference in New Issue
Block a user