diff --git a/idea/src/org/jetbrains/kotlin/idea/formatter/TrailingCommaPostFormatProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/formatter/TrailingCommaPostFormatProcessor.kt index 6ba16909519..d88ee6208e6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/formatter/TrailingCommaPostFormatProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/formatter/TrailingCommaPostFormatProcessor.kt @@ -26,10 +26,7 @@ import org.jetbrains.kotlin.idea.util.module import org.jetbrains.kotlin.idea.util.needTrailingComma import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer -import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespace -import org.jetbrains.kotlin.psi.psiUtil.getPrevSiblingIgnoringWhitespaceAndComments -import org.jetbrains.kotlin.psi.psiUtil.siblings +import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.utils.addToStdlib.cast import org.jetbrains.kotlin.utils.addToStdlib.safeAs import org.jetbrains.kotlin.utils.ifEmpty @@ -150,10 +147,9 @@ private class TrailingCommaVisitor(val settings: CodeStyleSettings) : KtTreeVisi it.createSmartPointer() }?.toList().orEmpty().ifEmpty { return } - val factory = KtPsiFactory(parent) for (pointerToComma in invalidElements) { pointerToComma.element?.let { - correctComma(it, factory) + correctComma(it) } } } @@ -183,21 +179,15 @@ private fun PsiElement.addCommaAfter(factory: KtPsiFactory) { parent.addAfter(comma, this) } -private fun correctComma(comma: PsiElement, factory: KtPsiFactory) { - val prevWithComment = comma.getPrevSiblingIgnoringWhitespace(false) - val prevWithoutComment = comma.getPrevSiblingIgnoringWhitespaceAndComments(false) - when { - prevWithoutComment?.equals(prevWithComment) == false -> { - prevWithoutComment.addCommaAfter(factory) - comma.delete() - } - prevWithoutComment is KtParameter -> { - val check = { element: PsiElement -> element is PsiWhiteSpace || element is PsiComment } - val lastElement = prevWithoutComment.lastChild?.takeIf(check) ?: return - val firstElement = lastElement.siblings(forward = false, withItself = true).takeWhile(check).last() - comma.parent.addRangeAfter(firstElement, lastElement, comma) - prevWithoutComment.deleteChildRange(firstElement, lastElement) - } +private fun correctComma(comma: PsiElement) { + val prevWithComment = comma.prevLeaf { it !is PsiWhiteSpace } ?: return + val prevWithoutComment = comma.prevLeaf { it !is PsiWhiteSpace && it !is PsiComment } ?: return + if (prevWithComment != prevWithoutComment) { + val check = { element: PsiElement -> element is PsiWhiteSpace || element is PsiComment } + val firstElement = prevWithComment.siblings(forward = false, withItself = true).takeWhile(check).last() + val commentOwner = prevWithComment.parent + comma.parent.addRangeAfter(firstElement, prevWithComment, comma) + commentOwner.deleteChildRange(firstElement, prevWithComment) } } diff --git a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListChopAsNeeded.after.inv.kt b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListChopAsNeeded.after.inv.kt index f05aabdc1df..6e536ff0ad8 100644 --- a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListChopAsNeeded.after.inv.kt +++ b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListChopAsNeeded.after.inv.kt @@ -263,4 +263,13 @@ fun foo() { foofoo, fososos, testtest(testtest(foofoo)), ) -} \ No newline at end of file +} + +fun test() { + baz( + f = fun(it: Int): String = "$it", /*dwdwd + */ + name = "", /* + */ + ) +} diff --git a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListChopAsNeeded.after.kt b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListChopAsNeeded.after.kt index 0851a0344f2..b2facd95b5a 100644 --- a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListChopAsNeeded.after.kt +++ b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListChopAsNeeded.after.kt @@ -337,4 +337,13 @@ fun foo() { foofoo, fososos, testtest(testtest(foofoo)), ) -} \ No newline at end of file +} + +fun test() { + baz( + f = fun(it: Int): String = "$it", /*dwdwd + */ + name = "", /* + */ + ) +} diff --git a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListChopAsNeeded.kt b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListChopAsNeeded.kt index 153140b5b7b..6baa34fd9c1 100644 --- a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListChopAsNeeded.kt +++ b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListChopAsNeeded.kt @@ -216,4 +216,13 @@ fun foo() { testtest(foofoo, fososos, testtest(testtest(foofoo)) ,) -} \ No newline at end of file +} + +fun test() { + baz( + f = fun(it: Int): String = "$it" /*dwdwd + */, + name = "" /* + */, + ) +} diff --git a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListDoNotWrap.after.inv.kt b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListDoNotWrap.after.inv.kt index 0282bef8af0..a54b1b7e946 100644 --- a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListDoNotWrap.after.inv.kt +++ b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListDoNotWrap.after.inv.kt @@ -263,4 +263,13 @@ fun foo() { foofoo, fososos, testtest(testtest(foofoo)), ) -} \ No newline at end of file +} + +fun test() { + baz( + f = fun(it: Int): String = "$it", /*dwdwd + */ + name = "", /* + */ + ) +} diff --git a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListDoNotWrap.after.kt b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListDoNotWrap.after.kt index 24a9a5e3adc..f7c5c3c7bcc 100644 --- a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListDoNotWrap.after.kt +++ b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListDoNotWrap.after.kt @@ -337,4 +337,13 @@ fun foo() { foofoo, fososos, testtest(testtest(foofoo)), ) -} \ No newline at end of file +} + +fun test() { + baz( + f = fun(it: Int): String = "$it", /*dwdwd + */ + name = "", /* + */ + ) +} diff --git a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListDoNotWrap.kt b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListDoNotWrap.kt index 3458ca206ed..f71e439c00d 100644 --- a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListDoNotWrap.kt +++ b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListDoNotWrap.kt @@ -216,4 +216,13 @@ fun foo() { testtest(foofoo, fososos, testtest(testtest(foofoo)) ,) -} \ No newline at end of file +} + +fun test() { + baz( + f = fun(it: Int): String = "$it" /*dwdwd + */, + name = "" /* + */, + ) +} diff --git a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAlways.after.inv.kt b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAlways.after.inv.kt index 531978d3516..c5bf8b4dea2 100644 --- a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAlways.after.inv.kt +++ b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAlways.after.inv.kt @@ -356,4 +356,13 @@ fun foo() { testtest(testtest(foofoo)), ) +} + +fun test() { + baz( + f = fun(it: Int): String = "$it", /*dwdwd + */ + name = "", /* + */ + ) } \ No newline at end of file diff --git a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAlways.after.kt b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAlways.after.kt index 6b6ea4d3edf..b7174ceffb6 100644 --- a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAlways.after.kt +++ b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAlways.after.kt @@ -427,4 +427,13 @@ fun foo() { testtest(testtest(foofoo)), ) +} + +fun test() { + baz( + f = fun(it: Int): String = "$it", /*dwdwd + */ + name = "", /* + */ + ) } \ No newline at end of file diff --git a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAlways.kt b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAlways.kt index 26d8901684b..984500b1cb5 100644 --- a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAlways.kt +++ b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAlways.kt @@ -224,4 +224,13 @@ fun foo() { testtest(foofoo, fososos, testtest(testtest(foofoo)) ,) -} \ No newline at end of file +} + +fun test() { + baz( + f = fun(it: Int): String = "$it" /*dwdwd + */, + name = "" /* + */, + ) +} diff --git a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAsNeeded.after.inv.kt b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAsNeeded.after.inv.kt index 4a46d008266..5d5ec47e5e8 100644 --- a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAsNeeded.after.inv.kt +++ b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAsNeeded.after.inv.kt @@ -263,4 +263,13 @@ fun foo() { foofoo, fososos, testtest(testtest(foofoo)), ) -} \ No newline at end of file +} + +fun test() { + baz( + f = fun(it: Int): String = "$it", /*dwdwd + */ + name = "", /* + */ + ) +} diff --git a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAsNeeded.after.kt b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAsNeeded.after.kt index 5410484d4be..a7c0cc626d2 100644 --- a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAsNeeded.after.kt +++ b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAsNeeded.after.kt @@ -337,4 +337,13 @@ fun foo() { foofoo, fososos, testtest(testtest(foofoo)), ) -} \ No newline at end of file +} + +fun test() { + baz( + f = fun(it: Int): String = "$it", /*dwdwd + */ + name = "", /* + */ + ) +} diff --git a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAsNeeded.kt b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAsNeeded.kt index 022ef85b1bf..54c3135b7e6 100644 --- a/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAsNeeded.kt +++ b/idea/testData/formatter/trailingComma/valueArguments/ArgumentListWrapAsNeeded.kt @@ -216,4 +216,13 @@ fun foo() { testtest(foofoo, fososos, testtest(testtest(foofoo)) ,) -} \ No newline at end of file +} + +fun test() { + baz( + f = fun(it: Int): String = "$it" /*dwdwd + */, + name = "" /* + */, + ) +}