Skip to content

Commit 1ad7a50

Browse files
committed
IfThenToElvis & FoldInitializerAndIfToElvis should add new line for long expression
Relates to #KT-19643 #KT-16067
1 parent 9c1ff75 commit 1ad7a50

File tree

8 files changed

+57
-6
lines changed

8 files changed

+57
-6
lines changed

idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.jetbrains.kotlin.idea.inspections
77

8+
import com.intellij.application.options.CodeStyle
89
import com.intellij.codeInspection.ProblemHighlightType
910
import com.intellij.openapi.editor.Editor
1011
import com.intellij.openapi.project.Project
@@ -19,6 +20,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
1920
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
2021
import org.jetbrains.kotlin.idea.core.replaced
2122
import org.jetbrains.kotlin.idea.core.setType
23+
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.elvisPattern
2224
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.expressionComparedToNull
2325
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.shouldBeTransformed
2426
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.textRange
@@ -83,10 +85,8 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti
8385
val commentSaver = CommentSaver(childRangeBefore)
8486
val childRangeAfter = childRangeBefore.withoutLastStatement()
8587

86-
val pattern = if (element.then?.hasComments() == true)
87-
"$0\n?: $1"
88-
else
89-
"$0 ?: $1"
88+
val margin = CodeStyle.getSettings(element.containingKtFile).defaultRightMargin
89+
val pattern = elvisPattern(declaration.textLength + ifNullExpr.textLength + 5 >= margin || element.then?.hasComments() == true)
9090

9191
val elvis = factory.createExpressionByPattern(pattern, initializer, ifNullExpr) as KtBinaryExpression
9292
if (typeReference != null) {

idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToElvisInspection.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.jetbrains.kotlin.idea.inspections.branchedTransformations
77

8+
import com.intellij.application.options.CodeStyle
89
import com.intellij.codeInspection.ProblemHighlightType
910
import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel
1011
import com.intellij.openapi.editor.Editor
@@ -63,14 +64,17 @@ class IfThenToElvisInspection(
6364
val factory = KtPsiFactory(element)
6465

6566
val commentSaver = CommentSaver(element, saveLineBreaks = false)
67+
val margin = CodeStyle.getSettings(element.containingKtFile).defaultRightMargin
68+
6669

6770
val elvis = runWriteAction {
6871
val replacedBaseClause = ifThenToSelectData.replacedBaseClause(factory)
72+
val negatedClause = ifThenToSelectData.negatedClause!!
6973
val newExpr = element.replaced(
7074
factory.createExpressionByPattern(
71-
"$0 ?: $1",
75+
elvisPattern(replacedBaseClause.textLength + negatedClause.textLength + 5 >= margin),
7276
replacedBaseClause,
73-
ifThenToSelectData.negatedClause!!
77+
negatedClause
7478
)
7579
)
7680

idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ fun KtExpression.isStableVal(context: BindingContext = this.analyze()): Boolean
177177
return this.toDataFlowValue(context)?.kind == DataFlowValue.Kind.STABLE_VALUE
178178
}
179179

180+
fun elvisPattern(newLine: Boolean): String = if (newLine) "$0\n?: $1" else "$0 ?: $1"
181+
180182
private fun KtExpression.toDataFlowValue(context: BindingContext): DataFlowValue? {
181183
val expressionType = this.getType(context) ?: return null
182184
val dataFlowValueFactory = this.getResolutionFacade().frontendService<DataFlowValueFactory>()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fun maybeFoo(): String? {
2+
return "foo"
3+
}
4+
5+
val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = maybeFoo()
6+
7+
fun main() {
8+
val c = if (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx != null)<caret> {
9+
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
10+
} else {
11+
"abcdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdf"
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fun maybeFoo(): String? {
2+
return "foo"
3+
}
4+
5+
val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = maybeFoo()
6+
7+
fun main() {
8+
val c = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
9+
?: "abcdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdf"
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fun foo(): Any {
2+
val yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy = 7
3+
val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = 24
4+
if (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx<caret> == null) return yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
5+
return 42
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fun foo(): Any {
2+
val yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy = 7
3+
val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = 24
4+
?: return yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
5+
return 42
6+
}

idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)