TrailingCommaPostFormatProcessor: fix comments position before comma

#KT-34744
This commit is contained in:
Dmitry Gridin
2020-01-15 20:10:33 +07:00
parent 5a922e5cff
commit c5c035b67c
13 changed files with 129 additions and 31 deletions
@@ -26,10 +26,7 @@ import org.jetbrains.kotlin.idea.util.module
import org.jetbrains.kotlin.idea.util.needTrailingComma import org.jetbrains.kotlin.idea.util.needTrailingComma
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.createSmartPointer import org.jetbrains.kotlin.psi.psiUtil.*
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.utils.addToStdlib.cast import org.jetbrains.kotlin.utils.addToStdlib.cast
import org.jetbrains.kotlin.utils.addToStdlib.safeAs import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.kotlin.utils.ifEmpty import org.jetbrains.kotlin.utils.ifEmpty
@@ -150,10 +147,9 @@ private class TrailingCommaVisitor(val settings: CodeStyleSettings) : KtTreeVisi
it.createSmartPointer() it.createSmartPointer()
}?.toList().orEmpty().ifEmpty { return } }?.toList().orEmpty().ifEmpty { return }
val factory = KtPsiFactory(parent)
for (pointerToComma in invalidElements) { for (pointerToComma in invalidElements) {
pointerToComma.element?.let { pointerToComma.element?.let {
correctComma(it, factory) correctComma(it)
} }
} }
} }
@@ -183,21 +179,15 @@ private fun PsiElement.addCommaAfter(factory: KtPsiFactory) {
parent.addAfter(comma, this) parent.addAfter(comma, this)
} }
private fun correctComma(comma: PsiElement, factory: KtPsiFactory) { private fun correctComma(comma: PsiElement) {
val prevWithComment = comma.getPrevSiblingIgnoringWhitespace(false) val prevWithComment = comma.prevLeaf { it !is PsiWhiteSpace } ?: return
val prevWithoutComment = comma.getPrevSiblingIgnoringWhitespaceAndComments(false) val prevWithoutComment = comma.prevLeaf { it !is PsiWhiteSpace && it !is PsiComment } ?: return
when { if (prevWithComment != prevWithoutComment) {
prevWithoutComment?.equals(prevWithComment) == false -> { val check = { element: PsiElement -> element is PsiWhiteSpace || element is PsiComment }
prevWithoutComment.addCommaAfter(factory) val firstElement = prevWithComment.siblings(forward = false, withItself = true).takeWhile(check).last()
comma.delete() val commentOwner = prevWithComment.parent
} comma.parent.addRangeAfter(firstElement, prevWithComment, comma)
prevWithoutComment is KtParameter -> { commentOwner.deleteChildRange(firstElement, prevWithComment)
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)
}
} }
} }
@@ -263,4 +263,13 @@ fun foo() {
foofoo, fososos, testtest(testtest(foofoo)), foofoo, fososos, testtest(testtest(foofoo)),
) )
} }
fun test() {
baz(
f = fun(it: Int): String = "$it", /*dwdwd
*/
name = "", /*
*/
)
}
@@ -337,4 +337,13 @@ fun foo() {
foofoo, fososos, testtest(testtest(foofoo)), foofoo, fososos, testtest(testtest(foofoo)),
) )
} }
fun test() {
baz(
f = fun(it: Int): String = "$it", /*dwdwd
*/
name = "", /*
*/
)
}
@@ -216,4 +216,13 @@ fun foo() {
testtest(foofoo, fososos, testtest(testtest(foofoo)) testtest(foofoo, fososos, testtest(testtest(foofoo))
,) ,)
} }
fun test() {
baz(
f = fun(it: Int): String = "$it" /*dwdwd
*/,
name = "" /*
*/,
)
}
@@ -263,4 +263,13 @@ fun foo() {
foofoo, fososos, testtest(testtest(foofoo)), foofoo, fososos, testtest(testtest(foofoo)),
) )
} }
fun test() {
baz(
f = fun(it: Int): String = "$it", /*dwdwd
*/
name = "", /*
*/
)
}
@@ -337,4 +337,13 @@ fun foo() {
foofoo, fososos, testtest(testtest(foofoo)), foofoo, fososos, testtest(testtest(foofoo)),
) )
} }
fun test() {
baz(
f = fun(it: Int): String = "$it", /*dwdwd
*/
name = "", /*
*/
)
}
@@ -216,4 +216,13 @@ fun foo() {
testtest(foofoo, fososos, testtest(testtest(foofoo)) testtest(foofoo, fososos, testtest(testtest(foofoo))
,) ,)
} }
fun test() {
baz(
f = fun(it: Int): String = "$it" /*dwdwd
*/,
name = "" /*
*/,
)
}
@@ -356,4 +356,13 @@ fun foo() {
testtest(testtest(foofoo)), testtest(testtest(foofoo)),
) )
}
fun test() {
baz(
f = fun(it: Int): String = "$it", /*dwdwd
*/
name = "", /*
*/
)
} }
@@ -427,4 +427,13 @@ fun foo() {
testtest(testtest(foofoo)), testtest(testtest(foofoo)),
) )
}
fun test() {
baz(
f = fun(it: Int): String = "$it", /*dwdwd
*/
name = "", /*
*/
)
} }
@@ -224,4 +224,13 @@ fun foo() {
testtest(foofoo, fososos, testtest(testtest(foofoo)) testtest(foofoo, fososos, testtest(testtest(foofoo))
,) ,)
} }
fun test() {
baz(
f = fun(it: Int): String = "$it" /*dwdwd
*/,
name = "" /*
*/,
)
}
@@ -263,4 +263,13 @@ fun foo() {
foofoo, fososos, testtest(testtest(foofoo)), foofoo, fososos, testtest(testtest(foofoo)),
) )
} }
fun test() {
baz(
f = fun(it: Int): String = "$it", /*dwdwd
*/
name = "", /*
*/
)
}
@@ -337,4 +337,13 @@ fun foo() {
foofoo, fososos, testtest(testtest(foofoo)), foofoo, fososos, testtest(testtest(foofoo)),
) )
} }
fun test() {
baz(
f = fun(it: Int): String = "$it", /*dwdwd
*/
name = "", /*
*/
)
}
@@ -216,4 +216,13 @@ fun foo() {
testtest(foofoo, fososos, testtest(testtest(foofoo)) testtest(foofoo, fososos, testtest(testtest(foofoo))
,) ,)
} }
fun test() {
baz(
f = fun(it: Int): String = "$it" /*dwdwd
*/,
name = "" /*
*/,
)
}