Object literal to lambda: handle last comment correctly #KT-15670 Fixed

This commit is contained in:
Mikhail Glukhikh
2017-01-26 16:16:23 +03:00
parent c0f5cafbd8
commit 08caf0a011
7 changed files with 63 additions and 7 deletions
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiComment
import com.intellij.psi.search.LocalSearchScope
import com.intellij.psi.search.searches.ReferencesSearch
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
@@ -34,10 +35,7 @@ import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.load.java.sam.SingleAbstractMethodUtils
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
import org.jetbrains.kotlin.psi.psiUtil.contentRange
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.psi.psiUtil.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getCalleeExpressionIfAny
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
@@ -85,7 +83,7 @@ class ObjectLiteralToLambdaIntention : SelfTargetingRangeIntention<KtObjectLiter
override fun applyTo(element: KtObjectLiteralExpression, editor: Editor?) {
val commentSaver = CommentSaver(element)
val (baseTypeRef, baseType, singleFunction) = extractData(element)!!
val (_, baseType, singleFunction) = extractData(element)!!
val returnSaver = ReturnSaver(singleFunction)
@@ -111,13 +109,19 @@ class ObjectLiteralToLambdaIntention : SelfTargetingRangeIntention<KtObjectLiter
appendFixedText("->")
}
if (singleFunction.hasBlockBody()) {
appendChildRange((body as KtBlockExpression).contentRange())
val lastCommentOwner = if (singleFunction.hasBlockBody()) {
val contentRange = (body as KtBlockExpression).contentRange()
appendChildRange(contentRange)
contentRange.last
}
else {
appendExpression(body)
body
}
if (lastCommentOwner?.anyDescendantOfType<PsiComment> { it.tokenType == KtTokens.EOL_COMMENT } ?: false) {
appendFixedText("\n")
}
appendFixedText("}")
}
@@ -0,0 +1,7 @@
// WITH_RUNTIME
val r = object: Runnable<caret> {
override fun run() {
TODO("not implemented") //To change body ...
}
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
val r = Runnable {
TODO("not implemented") //To change body ...
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
val r = object: Runnable<caret> {
override fun run() = TODO("not implemented") //To change body ...
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
val r = Runnable {
TODO("not implemented") //To change body ...
}
@@ -166,4 +166,22 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Convert object literal to lambda</problem_class>
<description>Convert to lambda</description>
</problem>
<problem>
<file>WithComment.kt</file>
<line>3</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/WithComment.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Object literal can be converted to lambda</problem_class>
<description>Convert to lambda</description>
</problem>
<problem>
<file>WithCommentAfterExpression.kt</file>
<line>3</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/WithCommentAfterExpression.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Object literal can be converted to lambda</problem_class>
<description>Convert to lambda</description>
</problem>
</problems>
@@ -11112,6 +11112,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("WithComment.kt")
public void testWithComment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/objectLiteralToLambda/WithComment.kt");
doTest(fileName);
}
@TestMetadata("WithCommentAfterExpression.kt")
public void testWithCommentAfterExpression() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/objectLiteralToLambda/WithCommentAfterExpression.kt");
doTest(fileName);
}
}
@TestMetadata("idea/testData/intentions/operatorToFunction")