From 699ea0aa2b821952778d98c2166420f58ae5f481 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Fri, 17 Jan 2020 19:34:47 +0900 Subject: [PATCH] Replace 'if' with 'when': don't swallow comments if there is no synthetic else branch #KT-34640 Fixed --- .../intentions/IfToWhenIntention.kt | 6 +++++- .../ifWhen/ifToWhen/doNotSwallowComment.kt | 12 ++++++++++++ .../ifWhen/ifToWhen/doNotSwallowComment.kt.after | 14 ++++++++++++++ .../idea/intentions/IntentionTestGenerated.java | 5 +++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/branched/ifWhen/ifToWhen/doNotSwallowComment.kt create mode 100644 idea/testData/intentions/branched/ifWhen/ifToWhen/doNotSwallowComment.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt index 7e3adb78aac..ee65f3456e9 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/IfToWhenIntention.kt @@ -168,7 +168,11 @@ class IfToWhenIntention : SelfTargetingRangeIntention(KtIfExpres val currentElseBranch = currentIfExpression.`else` if (currentElseBranch == null) { // Try to build synthetic if / else according to KT-10750 - val syntheticElseBranch = if (canPassThrough) break else buildNextBranch(baseIfExpressionForSyntheticBranch) ?: break + val syntheticElseBranch = if (canPassThrough) null else buildNextBranch(baseIfExpressionForSyntheticBranch) + if (syntheticElseBranch == null) { + applyFullCommentSaver = false + break + } toDelete.addAll(baseIfExpressionForSyntheticBranch.siblingsUpTo(syntheticElseBranch)) if (syntheticElseBranch is KtIfExpression) { baseIfExpressionForSyntheticBranch = syntheticElseBranch diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/doNotSwallowComment.kt b/idea/testData/intentions/branched/ifWhen/ifToWhen/doNotSwallowComment.kt new file mode 100644 index 00000000000..64cadc9dc04 --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/doNotSwallowComment.kt @@ -0,0 +1,12 @@ +fun test(foo: String) { + if (foo == "1") { + println("1") + } + + if (foo == "a") { + // some comments for "a" + println("a"); + } +} + +fun println(s: String) {} \ No newline at end of file diff --git a/idea/testData/intentions/branched/ifWhen/ifToWhen/doNotSwallowComment.kt.after b/idea/testData/intentions/branched/ifWhen/ifToWhen/doNotSwallowComment.kt.after new file mode 100644 index 00000000000..9693b4154cb --- /dev/null +++ b/idea/testData/intentions/branched/ifWhen/ifToWhen/doNotSwallowComment.kt.after @@ -0,0 +1,14 @@ +fun test(foo: String) { + when (foo) { + "1" -> { + println("1") + } + } + + if (foo == "a") { + // some comments for "a" + println("a"); + } +} + +fun println(s: String) {} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 11380b1a0da..9693509d829 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2327,6 +2327,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest { runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/comment.kt"); } + @TestMetadata("doNotSwallowComment.kt") + public void testDoNotSwallowComment() throws Exception { + runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/doNotSwallowComment.kt"); + } + @TestMetadata("ifElseSwallowComments.kt") public void testIfElseSwallowComments() throws Exception { runTest("idea/testData/intentions/branched/ifWhen/ifToWhen/ifElseSwallowComments.kt");