"Merge 'if's" intention: do not remove nested comments
#KT-33258 Fixed #KT-39552 Fixed
This commit is contained in:
committed by
igoriakovlev
parent
f6e70cfed8
commit
9ff7539ff0
@@ -6,8 +6,12 @@
|
||||
package org.jetbrains.kotlin.idea.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.psi.PsiComment
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.inspections.findExistingEditor
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.allChildren
|
||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||
|
||||
class MergeIfsIntention : SelfTargetingIntention<KtIfExpression>(KtIfExpression::class.java, KotlinBundle.lazyMessage("merge.if.s")) {
|
||||
override fun isApplicableTo(element: KtIfExpression, caretOffset: Int): Boolean {
|
||||
@@ -33,6 +37,18 @@ class MergeIfsIntention : SelfTargetingIntention<KtIfExpression>(KtIfExpression:
|
||||
|
||||
val factory = KtPsiFactory(element)
|
||||
|
||||
val comments = element.allChildren.filter { it is PsiComment }.toList() +
|
||||
(element.then as? KtBlockExpression)?.allChildren?.filter { it is PsiComment }?.toList().orEmpty()
|
||||
if (comments.isNotEmpty()) {
|
||||
val parent = element.parent
|
||||
comments.forEach { comment ->
|
||||
parent.addBefore(comment, element)
|
||||
parent.addBefore(factory.createNewLine(), element)
|
||||
comment.delete()
|
||||
}
|
||||
element.findExistingEditor()?.caretModel?.moveToOffset(element.startOffset)
|
||||
}
|
||||
|
||||
condition.replace(factory.createExpressionByPattern("$0 && $1", condition, secondCondition))
|
||||
val newBody = element.then!!.replace(nestedBody)
|
||||
|
||||
|
||||
+3
-1
@@ -1,6 +1,8 @@
|
||||
fun foo() {
|
||||
// comment 1
|
||||
<caret>if (/* comment 2 */ true && false /* comment 3 */) {
|
||||
/* comment 2 */
|
||||
/* comment 3 */
|
||||
<caret>if (true && false) {
|
||||
// comment 4
|
||||
foo()
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fun foo() {
|
||||
// comment1
|
||||
<caret>if (true) /* comment2 */ /* comment3 */ {
|
||||
// comment4
|
||||
// comment5
|
||||
if (false) {
|
||||
}
|
||||
// comment6
|
||||
// comment7
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fun foo() {
|
||||
// comment1
|
||||
/* comment2 */
|
||||
/* comment3 */
|
||||
// comment4
|
||||
// comment5
|
||||
// comment6
|
||||
// comment7
|
||||
<caret>if (true && false) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fun foo() {
|
||||
// comment1
|
||||
<caret>if (true)
|
||||
// comment2
|
||||
// comment3
|
||||
if (false) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fun foo() {
|
||||
// comment1
|
||||
// comment2
|
||||
// comment3
|
||||
<caret>if (true && false) {
|
||||
}
|
||||
}
|
||||
@@ -12225,6 +12225,16 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
runTest("idea/testData/intentions/mergeIfs/comments.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("comments2.kt")
|
||||
public void testComments2() throws Exception {
|
||||
runTest("idea/testData/intentions/mergeIfs/comments2.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("comments3.kt")
|
||||
public void testComments3() throws Exception {
|
||||
runTest("idea/testData/intentions/mergeIfs/comments3.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("else1.kt")
|
||||
public void testElse1() throws Exception {
|
||||
runTest("idea/testData/intentions/mergeIfs/else1.kt");
|
||||
|
||||
Reference in New Issue
Block a user