Fixed KT-7755 Convert to expression body kills comment
#KT-7755
This commit is contained in:
@@ -22,17 +22,15 @@ import com.intellij.psi.PsiElement
|
|||||||
import org.jetbrains.kotlin.analyzer.analyzeInContext
|
import org.jetbrains.kotlin.analyzer.analyzeInContext
|
||||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
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.analyze
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
|
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.lexer.JetTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement
|
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
||||||
import kotlin.platform.platformName
|
|
||||||
|
|
||||||
public class ConvertToExpressionBodyIntention : JetSelfTargetingOffsetIndependentIntention<JetDeclarationWithBody>(
|
public class ConvertToExpressionBodyIntention : JetSelfTargetingOffsetIndependentIntention<JetDeclarationWithBody>(
|
||||||
javaClass(), "Convert to expression body", firstElementOfTypeOnly = true
|
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 omitType = declaration.hasDeclaredReturnType() && declaration is JetCallableDeclaration && canOmitType(declaration, value)
|
||||||
|
|
||||||
val body = declaration.getBodyExpression()!!
|
val body = declaration.getBodyExpression()!!
|
||||||
|
|
||||||
|
val commentSaver = CommentSaver(body)
|
||||||
|
|
||||||
declaration.addBefore(JetPsiFactory(declaration).createEQ(), body)
|
declaration.addBefore(JetPsiFactory(declaration).createEQ(), body)
|
||||||
body.replace(value)
|
val newBody = body.replace(value)
|
||||||
|
|
||||||
|
commentSaver.restoreComments(newBody)
|
||||||
|
|
||||||
if (omitType) {
|
if (omitType) {
|
||||||
onFinish(declaration as JetCallableDeclaration)
|
onFinish(declaration as JetCallableDeclaration)
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun getText(): String {
|
||||||
|
<caret>return "xxx" //TODO
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
fun getText()<selection>: String</selection> = "xxx" //TODO
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fun getText(): String {
|
||||||
|
// let's return xxx
|
||||||
|
<caret>return "xxx" //TODO
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
fun getText()<selection>: String</selection> = // let's return xxx
|
||||||
|
"xxx" //TODO
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun getText(): String { // let's return xxx
|
||||||
|
<caret>return "xxx"
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
fun getText()<selection>: String</selection> = // let's return xxx
|
||||||
|
"xxx"
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
fun getText(): String {
|
||||||
|
/* let's return xxx */
|
||||||
|
<caret>return "xxx" /* TODO */
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
fun getText(): String = /* let's return xxx */
|
||||||
|
"xxx" /* TODO */
|
||||||
@@ -3876,6 +3876,39 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/while.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/while.kt");
|
||||||
doTest(fileName);
|
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")
|
@TestMetadata("idea/testData/intentions/convertToForEachFunctionCall")
|
||||||
|
|||||||
Reference in New Issue
Block a user