From 1ad7a50087635cb5353750eac0fc0f39299fc338 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Wed, 5 Jun 2019 17:38:06 +0700 Subject: [PATCH] IfThenToElvis & FoldInitializerAndIfToElvis should add new line for long expression Relates to #KT-19643 #KT-16067 --- .../FoldInitializerAndIfToElvisInspection.kt | 8 ++++---- .../IfThenToElvisInspection.kt | 8 ++++++-- .../branchedTransformations/IfThenUtils.kt | 2 ++ .../branched/ifThenToElvis/longLine.kt | 13 +++++++++++++ .../branched/ifThenToElvis/longLine.kt.after | 10 ++++++++++ .../foldInitializerAndIfToElvis/LongName.kt | 6 ++++++ .../foldInitializerAndIfToElvis/LongName.kt.after | 6 ++++++ .../inspections/LocalInspectionTestGenerated.java | 10 ++++++++++ 8 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 idea/testData/inspectionsLocal/branched/ifThenToElvis/longLine.kt create mode 100644 idea/testData/inspectionsLocal/branched/ifThenToElvis/longLine.kt.after create mode 100644 idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/LongName.kt create mode 100644 idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/LongName.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt index 7baaf8ef0a5..5da9321c4a5 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/FoldInitializerAndIfToElvisInspection.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.idea.inspections +import com.intellij.application.options.CodeStyle import com.intellij.codeInspection.ProblemHighlightType import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project @@ -19,6 +20,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.core.setType +import org.jetbrains.kotlin.idea.intentions.branchedTransformations.elvisPattern import org.jetbrains.kotlin.idea.intentions.branchedTransformations.expressionComparedToNull import org.jetbrains.kotlin.idea.intentions.branchedTransformations.shouldBeTransformed import org.jetbrains.kotlin.idea.intentions.branchedTransformations.textRange @@ -83,10 +85,8 @@ class FoldInitializerAndIfToElvisInspection : AbstractApplicabilityBasedInspecti val commentSaver = CommentSaver(childRangeBefore) val childRangeAfter = childRangeBefore.withoutLastStatement() - val pattern = if (element.then?.hasComments() == true) - "$0\n?: $1" - else - "$0 ?: $1" + val margin = CodeStyle.getSettings(element.containingKtFile).defaultRightMargin + val pattern = elvisPattern(declaration.textLength + ifNullExpr.textLength + 5 >= margin || element.then?.hasComments() == true) val elvis = factory.createExpressionByPattern(pattern, initializer, ifNullExpr) as KtBinaryExpression if (typeReference != null) { diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToElvisInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToElvisInspection.kt index 0e55fc323bb..5e750839887 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToElvisInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/branchedTransformations/IfThenToElvisInspection.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.idea.inspections.branchedTransformations +import com.intellij.application.options.CodeStyle import com.intellij.codeInspection.ProblemHighlightType import com.intellij.codeInspection.ui.MultipleCheckboxOptionsPanel import com.intellij.openapi.editor.Editor @@ -63,14 +64,17 @@ class IfThenToElvisInspection( val factory = KtPsiFactory(element) val commentSaver = CommentSaver(element, saveLineBreaks = false) + val margin = CodeStyle.getSettings(element.containingKtFile).defaultRightMargin + val elvis = runWriteAction { val replacedBaseClause = ifThenToSelectData.replacedBaseClause(factory) + val negatedClause = ifThenToSelectData.negatedClause!! val newExpr = element.replaced( factory.createExpressionByPattern( - "$0 ?: $1", + elvisPattern(replacedBaseClause.textLength + negatedClause.textLength + 5 >= margin), replacedBaseClause, - ifThenToSelectData.negatedClause!! + negatedClause ) ) diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt index 3d798da0222..dcba8442c6a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/IfThenUtils.kt @@ -177,6 +177,8 @@ fun KtExpression.isStableVal(context: BindingContext = this.analyze()): Boolean return this.toDataFlowValue(context)?.kind == DataFlowValue.Kind.STABLE_VALUE } +fun elvisPattern(newLine: Boolean): String = if (newLine) "$0\n?: $1" else "$0 ?: $1" + private fun KtExpression.toDataFlowValue(context: BindingContext): DataFlowValue? { val expressionType = this.getType(context) ?: return null val dataFlowValueFactory = this.getResolutionFacade().frontendService() diff --git a/idea/testData/inspectionsLocal/branched/ifThenToElvis/longLine.kt b/idea/testData/inspectionsLocal/branched/ifThenToElvis/longLine.kt new file mode 100644 index 00000000000..4fdd294996d --- /dev/null +++ b/idea/testData/inspectionsLocal/branched/ifThenToElvis/longLine.kt @@ -0,0 +1,13 @@ +fun maybeFoo(): String? { + return "foo" +} + +val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = maybeFoo() + +fun main() { + val c = if (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx != null) { + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + } else { + "abcdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdf" + } +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/branched/ifThenToElvis/longLine.kt.after b/idea/testData/inspectionsLocal/branched/ifThenToElvis/longLine.kt.after new file mode 100644 index 00000000000..9dc33085f49 --- /dev/null +++ b/idea/testData/inspectionsLocal/branched/ifThenToElvis/longLine.kt.after @@ -0,0 +1,10 @@ +fun maybeFoo(): String? { + return "foo" +} + +val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = maybeFoo() + +fun main() { + val c = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + ?: "abcdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdfdf" +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/LongName.kt b/idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/LongName.kt new file mode 100644 index 00000000000..62776cdd7a4 --- /dev/null +++ b/idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/LongName.kt @@ -0,0 +1,6 @@ +fun foo(): Any { + val yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy = 7 + val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = 24 + if (xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx == null) return yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy + return 42 +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/LongName.kt.after b/idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/LongName.kt.after new file mode 100644 index 00000000000..96f9bf04d70 --- /dev/null +++ b/idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/LongName.kt.after @@ -0,0 +1,6 @@ +fun foo(): Any { + val yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy = 7 + val xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx = 24 + ?: return yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy + return 42 +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 84f7215efbc..d1c44fee5a7 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -332,6 +332,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/branched/ifThenToElvis/lhsNotEqualsNull.kt"); } + @TestMetadata("longLine.kt") + public void testLongLine() throws Exception { + runTest("idea/testData/inspectionsLocal/branched/ifThenToElvis/longLine.kt"); + } + @TestMetadata("missingElseClause.kt") public void testMissingElseClause() throws Exception { runTest("idea/testData/inspectionsLocal/branched/ifThenToElvis/missingElseClause.kt"); @@ -3798,6 +3803,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/IsSuperTypeFake.kt"); } + @TestMetadata("LongName.kt") + public void testLongName() throws Exception { + runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/LongName.kt"); + } + @TestMetadata("MultiStatementBlock.kt") public void testMultiStatementBlock() throws Exception { runTest("idea/testData/inspectionsLocal/foldInitializerAndIfToElvis/MultiStatementBlock.kt");