From 22a4bbe1aae5a54d1747099fc79b1c33bd357bb7 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Thu, 7 Apr 2016 10:57:03 +0300 Subject: [PATCH] KT-11805 Invert if-condition intention breaks code in case of end of line comment #KT-11805 Fixed --- .../kotlin/idea/intentions/InvertIfConditionIntention.kt | 2 +- .../intentions/invertIfCondition/endOfLineCommentBug.kt | 4 ++++ .../invertIfCondition/endOfLineCommentBug.kt.after | 6 ++++++ .../kotlin/idea/intentions/IntentionTestGenerated.java | 6 ++++++ 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 idea/testData/intentions/invertIfCondition/endOfLineCommentBug.kt create mode 100644 idea/testData/intentions/invertIfCondition/endOfLineCommentBug.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/InvertIfConditionIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/InvertIfConditionIntention.kt index 8cf19900d5a..94c6f965e58 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/InvertIfConditionIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/InvertIfConditionIntention.kt @@ -117,7 +117,7 @@ class InvertIfConditionIntention : SelfTargetingIntention(KtIfEx } //TODO: no block if single? - val newIf = factory.createExpressionByPattern("if ($0) { $1 }", newCondition, newIfBodyText) + val newIf = factory.createExpressionByPattern("if ($0) { $1\n}", newCondition, newIfBodyText) // we need to insert '\n' because the text can end with an end-of-line comment return updatedIf.replace(newIf) as KtIfExpression } } diff --git a/idea/testData/intentions/invertIfCondition/endOfLineCommentBug.kt b/idea/testData/intentions/invertIfCondition/endOfLineCommentBug.kt new file mode 100644 index 00000000000..93bf757e59d --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/endOfLineCommentBug.kt @@ -0,0 +1,4 @@ +fun foo(p: String?): Int? { + if (p == null) return null + return p.hashCode() // comment +} \ No newline at end of file diff --git a/idea/testData/intentions/invertIfCondition/endOfLineCommentBug.kt.after b/idea/testData/intentions/invertIfCondition/endOfLineCommentBug.kt.after new file mode 100644 index 00000000000..8a1641b28fd --- /dev/null +++ b/idea/testData/intentions/invertIfCondition/endOfLineCommentBug.kt.after @@ -0,0 +1,6 @@ +fun foo(p: String?): Int? { + if (p != null) { + return p.hashCode() // comment + } + return null +} \ 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 2d520b62243..bb0f3d64463 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -6105,6 +6105,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("endOfLineCommentBug.kt") + public void testEndOfLineCommentBug() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/invertIfCondition/endOfLineCommentBug.kt"); + doTest(fileName); + } + @TestMetadata("forLoopWithMultipleExpressions.kt") public void testForLoopWithMultipleExpressions() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/invertIfCondition/forLoopWithMultipleExpressions.kt");