FoldInitializerAndIfToElvis: should not add new line for multiline initializer

#KT-35805 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-01-05 14:50:47 +09:00
committed by igoriakovlev
parent 5eae262264
commit 6cb0190fad
6 changed files with 52 additions and 1 deletions
@@ -86,7 +86,8 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti
val childRangeAfter = childRangeBefore.withoutLastStatement()
val margin = CodeStyle.getSettings(element.containingKtFile).defaultRightMargin
val pattern = elvisPattern(declaration.textLength + ifNullExpr.textLength + 5 >= margin || element.then?.hasComments() == true)
val declarationTextLength = declaration.text.split("\n").lastOrNull()?.trim()?.length ?: 0
val pattern = elvisPattern(declarationTextLength + ifNullExpr.textLength + 5 >= margin || element.then?.hasComments() == true)
val elvis = factory.createExpressionByPattern(pattern, initializer, ifNullExpr) as KtBinaryExpression
@@ -0,0 +1,7 @@
fun foo(): Any {
val yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy = 7
val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
= 24
if (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<caret> == null) return yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
return 42
}
@@ -0,0 +1,6 @@
fun foo(): Any {
val yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy = 7
val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
= 24 ?: return yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
return 42
}
@@ -0,0 +1,14 @@
fun foo(vararg args: String): String? = null
fun test(): Int {
val foo = foo(
"1111111111",
"2222222222",
"3333333333",
"4444444444",
"5555555555"
)
<caret>if (foo == null) return 0
return 1
}
@@ -0,0 +1,13 @@
fun foo(vararg args: String): String? = null
fun test(): Int {
val foo = foo(
"1111111111",
"2222222222",
"3333333333",
"4444444444",
"5555555555"
) ?: return 0
return 1
}
@@ -4704,6 +4704,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/LongName.kt");
}
@TestMetadata("LongName2.kt")
public void testLongName2() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/LongName2.kt");
}
@TestMetadata("MultiLineInitializer.kt")
public void testMultiLineInitializer() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/MultiLineInitializer.kt");
}
@TestMetadata("MultiStatementBlock.kt")
public void testMultiStatementBlock() throws Exception {
runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/MultiStatementBlock.kt");