Move statement: move parameters/arguments with a comment correctly
#KT-34705 Fixed #KT-34707 Fixed #KT-34587 Fixed
This commit is contained in:
committed by
Vladimir Dolzhenko
parent
9c8904f165
commit
12cd3785b0
+16
-4
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.idea.util.isComma
|
|||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypesAndPredicate
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfTypesAndPredicate
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
|
||||||
import java.util.function.Predicate
|
import java.util.function.Predicate
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ class KotlinExpressionMover : AbstractKotlinUpDownMover() {
|
|||||||
sibling: PsiElement,
|
sibling: PsiElement,
|
||||||
down: Boolean
|
down: Boolean
|
||||||
): LineRange? {
|
): LineRange? {
|
||||||
val next = if (sibling.node.elementType === KtTokens.COMMA) {
|
val next = if (sibling.node.elementType === KtTokens.COMMA || sibling is PsiComment) {
|
||||||
firstNonWhiteSibling(sibling, down)
|
firstNonWhiteSibling(sibling, down)
|
||||||
} else {
|
} else {
|
||||||
sibling
|
sibling
|
||||||
@@ -544,7 +545,17 @@ class KotlinExpressionMover : AbstractKotlinUpDownMover() {
|
|||||||
down: Boolean
|
down: Boolean
|
||||||
): PsiElement? {
|
): PsiElement? {
|
||||||
val element = if (down) sourceRange.lastElement else sourceRange.firstElement
|
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 whiteSpaceTestSubject = sibling ?: kotlin.run {
|
||||||
val parent = element.parent
|
val parent = element.parent
|
||||||
if (parent == null || !isBracelessBlock(parent)) return@run null
|
if (parent == null || !isBracelessBlock(parent)) return@run null
|
||||||
@@ -589,8 +600,9 @@ class KotlinExpressionMover : AbstractKotlinUpDownMover() {
|
|||||||
if (willBeLast && comma != null && !withTrailingComma) {
|
if (willBeLast && comma != null && !withTrailingComma) {
|
||||||
comma.delete()
|
comma.delete()
|
||||||
} else if (!willBeLast && comma == null) {
|
} else if (!willBeLast && comma == null) {
|
||||||
val parent = element.parent
|
element.children.lastOrNull()?.let {
|
||||||
parent.addAfter(KtPsiFactory(parent.project).createComma(), element)
|
element.addAfter(KtPsiFactory(element.project).createComma(), it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// MOVE: down
|
||||||
|
val x = foo(
|
||||||
|
<caret>a, // a
|
||||||
|
b, // b
|
||||||
|
c // c
|
||||||
|
)
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// MOVE: down
|
||||||
|
val x = foo(
|
||||||
|
b, // b
|
||||||
|
<caret>a, // a
|
||||||
|
c // c
|
||||||
|
)
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// MOVE: down
|
||||||
|
class A(
|
||||||
|
<caret>val a: Int, // a
|
||||||
|
val b: Int, // b
|
||||||
|
val c: Int // c
|
||||||
|
)
|
||||||
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// MOVE: down
|
||||||
|
class A(
|
||||||
|
val b: Int, // b
|
||||||
|
<caret>val a: Int, // a
|
||||||
|
val c: Int // c
|
||||||
|
)
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// MOVE: down
|
||||||
|
class A(
|
||||||
|
val b: Int, // b
|
||||||
|
<caret>val a: Int, // a
|
||||||
|
val c: Int // c
|
||||||
|
)
|
||||||
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// MOVE: down
|
||||||
|
class A(
|
||||||
|
val b: Int, // b
|
||||||
|
val c: Int, // c
|
||||||
|
<caret>val a: Int // a
|
||||||
|
)
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// MOVE: up
|
||||||
|
class A(
|
||||||
|
val b: Int, // b
|
||||||
|
val c: Int, // c
|
||||||
|
<caret>val a: Int // a
|
||||||
|
)
|
||||||
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// MOVE: up
|
||||||
|
class A(
|
||||||
|
val b: Int, // b
|
||||||
|
<caret>val a: Int, // a
|
||||||
|
val c: Int // c
|
||||||
|
)
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// MOVE: up
|
||||||
|
class A(
|
||||||
|
val b: Int, // b
|
||||||
|
<caret>val a: Int, // a
|
||||||
|
val c: Int // c
|
||||||
|
)
|
||||||
Vendored
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// MOVE: up
|
||||||
|
class A(
|
||||||
|
<caret>val a: Int, // a
|
||||||
|
val b: Int, // b
|
||||||
|
val c: Int // c
|
||||||
|
)
|
||||||
Generated
+25
@@ -1391,6 +1391,11 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
|
|||||||
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/callArgs8.kt");
|
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")
|
@TestMetadata("classParams1.kt")
|
||||||
public void testClassParams1() throws Exception {
|
public void testClassParams1() throws Exception {
|
||||||
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/classParams1.kt");
|
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");
|
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")
|
@TestMetadata("funParams1.kt")
|
||||||
public void testFunParams1() throws Exception {
|
public void testFunParams1() throws Exception {
|
||||||
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/funParams1.kt");
|
runTest("idea/testData/codeInsight/moveUpDown/parametersAndArguments/funParams1.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user