Move statement: move parameters/arguments with a comment correctly

#KT-34705 Fixed
#KT-34707 Fixed
#KT-34587 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-05-15 09:24:33 +02:00
committed by Vladimir Dolzhenko
parent 9c8904f165
commit 12cd3785b0
12 changed files with 101 additions and 4 deletions
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.idea.util.isComma
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypesAndPredicate
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import java.util.function.Predicate
@@ -31,7 +32,7 @@ class KotlinExpressionMover : AbstractKotlinUpDownMover() {
sibling: PsiElement,
down: Boolean
): LineRange? {
val next = if (sibling.node.elementType === KtTokens.COMMA) {
val next = if (sibling.node.elementType === KtTokens.COMMA || sibling is PsiComment) {
firstNonWhiteSibling(sibling, down)
} else {
sibling
@@ -544,7 +545,17 @@ class KotlinExpressionMover : AbstractKotlinUpDownMover() {
down: Boolean
): PsiElement? {
val element = if (down) sourceRange.lastElement else sourceRange.firstElement
var sibling = if (down) element.nextSibling else element.prevSibling
var sibling = if (down) {
val elementToCheck = sourceRange.firstElement
if (element is PsiComment && (elementToCheck is KtParameter || elementToCheck is KtValueArgument)) {
element.getPrevSiblingIgnoringWhitespaceAndComments()
} else {
element.nextSibling
}
} else {
element.prevSibling
}
val whiteSpaceTestSubject = sibling ?: kotlin.run {
val parent = element.parent
if (parent == null || !isBracelessBlock(parent)) return@run null
@@ -589,8 +600,9 @@ class KotlinExpressionMover : AbstractKotlinUpDownMover() {
if (willBeLast && comma != null && !withTrailingComma) {
comma.delete()
} else if (!willBeLast && comma == null) {
val parent = element.parent
parent.addAfter(KtPsiFactory(parent.project).createComma(), element)
element.children.lastOrNull()?.let {
element.addAfter(KtPsiFactory(element.project).createComma(), it)
}
}
}
@@ -0,0 +1,6 @@
// MOVE: down
val x = foo(
<caret>a, // a
b, // b
c // c
)
@@ -0,0 +1,6 @@
// MOVE: down
val x = foo(
b, // b
<caret>a, // a
c // c
)
@@ -0,0 +1,6 @@
// MOVE: down
class A(
<caret>val a: Int, // a
val b: Int, // b
val c: Int // c
)
@@ -0,0 +1,6 @@
// MOVE: down
class A(
val b: Int, // b
<caret>val a: Int, // a
val c: Int // c
)
@@ -0,0 +1,6 @@
// MOVE: down
class A(
val b: Int, // b
<caret>val a: Int, // a
val c: Int // c
)
@@ -0,0 +1,6 @@
// MOVE: down
class A(
val b: Int, // b
val c: Int, // c
<caret>val a: Int // a
)
@@ -0,0 +1,6 @@
// MOVE: up
class A(
val b: Int, // b
val c: Int, // c
<caret>val a: Int // a
)
@@ -0,0 +1,6 @@
// MOVE: up
class A(
val b: Int, // b
<caret>val a: Int, // a
val c: Int // c
)
@@ -0,0 +1,6 @@
// MOVE: up
class A(
val b: Int, // b
<caret>val a: Int, // a
val c: Int // c
)
@@ -0,0 +1,6 @@
// MOVE: up
class A(
<caret>val a: Int, // a
val b: Int, // b
val c: Int // c
)
@@ -1391,6 +1391,11 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/callArgs8.kt");
}
@TestMetadata("callArgsWithComment1.kt")
public void testCallArgsWithComment1() throws Exception {
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/callArgsWithComment1.kt");
}
@TestMetadata("classParams1.kt")
public void testClassParams1() throws Exception {
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/classParams1.kt");
@@ -1431,6 +1436,26 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/classParams8.kt");
}
@TestMetadata("classParamsWithComment1.kt")
public void testClassParamsWithComment1() throws Exception {
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/classParamsWithComment1.kt");
}
@TestMetadata("classParamsWithComment2.kt")
public void testClassParamsWithComment2() throws Exception {
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/classParamsWithComment2.kt");
}
@TestMetadata("classParamsWithComment3.kt")
public void testClassParamsWithComment3() throws Exception {
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/classParamsWithComment3.kt");
}
@TestMetadata("classParamsWithComment4.kt")
public void testClassParamsWithComment4() throws Exception {
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/classParamsWithComment4.kt");
}
@TestMetadata("funParams1.kt")
public void testFunParams1() throws Exception {
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/funParams1.kt");