diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt index 9cf14b60828..12c85d1cc86 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToExpressionBodyIntention.kt @@ -22,17 +22,15 @@ import com.intellij.psi.PsiElement import org.jetbrains.kotlin.analyzer.analyzeInContext import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.CallableDescriptor -import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor +import org.jetbrains.kotlin.idea.core.CommentSaver import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.endOffset import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.resolve.BindingContext -import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf -import kotlin.platform.platformName public class ConvertToExpressionBodyIntention : JetSelfTargetingOffsetIndependentIntention( javaClass(), "Convert to expression body", firstElementOfTypeOnly = true @@ -73,8 +71,13 @@ public class ConvertToExpressionBodyIntention : JetSelfTargetingOffsetIndependen val omitType = declaration.hasDeclaredReturnType() && declaration is JetCallableDeclaration && canOmitType(declaration, value) val body = declaration.getBodyExpression()!! + + val commentSaver = CommentSaver(body) + declaration.addBefore(JetPsiFactory(declaration).createEQ(), body) - body.replace(value) + val newBody = body.replace(value) + + commentSaver.restoreComments(newBody) if (omitType) { onFinish(declaration as JetCallableDeclaration) diff --git a/idea/testData/intentions/convertToExpressionBody/keepComments/1.kt b/idea/testData/intentions/convertToExpressionBody/keepComments/1.kt new file mode 100644 index 00000000000..1af95f3f3f6 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/keepComments/1.kt @@ -0,0 +1,3 @@ +fun getText(): String { + return "xxx" //TODO +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/keepComments/1.kt.after b/idea/testData/intentions/convertToExpressionBody/keepComments/1.kt.after new file mode 100644 index 00000000000..86abc2666e1 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/keepComments/1.kt.after @@ -0,0 +1 @@ +fun getText(): String = "xxx" //TODO \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/keepComments/2.kt b/idea/testData/intentions/convertToExpressionBody/keepComments/2.kt new file mode 100644 index 00000000000..cd25c311871 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/keepComments/2.kt @@ -0,0 +1,4 @@ +fun getText(): String { + // let's return xxx + return "xxx" //TODO +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/keepComments/2.kt.after b/idea/testData/intentions/convertToExpressionBody/keepComments/2.kt.after new file mode 100644 index 00000000000..e82b583d1bc --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/keepComments/2.kt.after @@ -0,0 +1,2 @@ +fun getText(): String = // let's return xxx + "xxx" //TODO \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/keepComments/3.kt b/idea/testData/intentions/convertToExpressionBody/keepComments/3.kt new file mode 100644 index 00000000000..0bcf2e3faa6 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/keepComments/3.kt @@ -0,0 +1,3 @@ +fun getText(): String { // let's return xxx + return "xxx" +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/keepComments/3.kt.after b/idea/testData/intentions/convertToExpressionBody/keepComments/3.kt.after new file mode 100644 index 00000000000..8eedd101ea1 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/keepComments/3.kt.after @@ -0,0 +1,2 @@ +fun getText(): String = // let's return xxx + "xxx" \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/keepComments/4.kt b/idea/testData/intentions/convertToExpressionBody/keepComments/4.kt new file mode 100644 index 00000000000..4f976078ab6 --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/keepComments/4.kt @@ -0,0 +1,4 @@ +fun getText(): String { + /* let's return xxx */ + return "xxx" /* TODO */ +} \ No newline at end of file diff --git a/idea/testData/intentions/convertToExpressionBody/keepComments/4.kt.after b/idea/testData/intentions/convertToExpressionBody/keepComments/4.kt.after new file mode 100644 index 00000000000..7a2b092320f --- /dev/null +++ b/idea/testData/intentions/convertToExpressionBody/keepComments/4.kt.after @@ -0,0 +1,2 @@ +fun getText(): String = /* let's return xxx */ + "xxx" /* TODO */ \ 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 2e5946f0eff..dec64ded410 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -3876,6 +3876,39 @@ public class IntentionTestGenerated extends AbstractIntentionTest { String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/while.kt"); doTest(fileName); } + + @TestMetadata("idea/testData/intentions/convertToExpressionBody/keepComments") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class KeepComments extends AbstractIntentionTest { + @TestMetadata("1.kt") + public void test1() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/keepComments/1.kt"); + doTest(fileName); + } + + @TestMetadata("2.kt") + public void test2() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/keepComments/2.kt"); + doTest(fileName); + } + + @TestMetadata("3.kt") + public void test3() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/keepComments/3.kt"); + doTest(fileName); + } + + @TestMetadata("4.kt") + public void test4() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/keepComments/4.kt"); + doTest(fileName); + } + + public void testAllFilesPresentInKeepComments() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/convertToExpressionBody/keepComments"), Pattern.compile("^(.+)\\.kt$"), true); + } + } } @TestMetadata("idea/testData/intentions/convertToForEachFunctionCall")