Formatter: support trailing comma in collection literal expression

#KT-34744
This commit is contained in:
Dmitry Gridin
2019-12-26 20:21:02 +07:00
parent bf92517500
commit 07bda5a759
7 changed files with 490 additions and 10 deletions
@@ -611,17 +611,13 @@ abstract class KotlinCommonBlock(
)
}
elementType === INDICES -> return { childElement ->
trailingCommaWrappingStrategyWithMultiLineCheck(LBRACKET, RBRACKET)(childElement)
}
elementType === INDICES -> return defaultTrailingCommaWrappingStrategy(LBRACKET, RBRACKET)
elementType === TYPE_PARAMETER_LIST -> return { childElement ->
trailingCommaWrappingStrategyWithMultiLineCheck(LT, GT)(childElement)
}
elementType === TYPE_PARAMETER_LIST -> return defaultTrailingCommaWrappingStrategy(LT, GT)
elementType === TYPE_ARGUMENT_LIST -> return { childElement ->
trailingCommaWrappingStrategyWithMultiLineCheck(LT, GT)(childElement)
}
elementType === TYPE_ARGUMENT_LIST -> return defaultTrailingCommaWrappingStrategy(LT, GT)
elementType === COLLECTION_LITERAL_EXPRESSION -> return defaultTrailingCommaWrappingStrategy(LBRACKET, RBRACKET)
elementType === SUPER_TYPE_LIST -> {
val wrap = Wrap.createWrap(commonSettings.EXTENDS_LIST_WRAP, false)
@@ -719,6 +715,9 @@ abstract class KotlinCommonBlock(
return ::noWrapping
}
private fun defaultTrailingCommaWrappingStrategy(leftAnchor: IElementType, rightAnchor: IElementType): WrappingStrategy =
fun(childElement: ASTNode): Wrap? = trailingCommaWrappingStrategyWithMultiLineCheck(leftAnchor, rightAnchor)(childElement)
private val ASTNode.withTrailingComma: Boolean
get() = when {
lastChildNode?.let { getPrevWithoutWhitespaceAndComments(it) }?.elementType === COMMA -> true
@@ -54,6 +54,10 @@ private class TrailingCommaVisitor(val settings: CodeStyleSettings) : KtTreeVisi
super.visitTypeArgumentList(typeArgumentList)
}
override fun visitCollectionLiteralExpression(expression: KtCollectionLiteralExpression) = processCommaOwnerIfInRange(expression) {
super.visitCollectionLiteralExpression(expression)
}
private fun processCommaOwnerIfInRange(element: KtElement, preHook: () -> Unit = {}) {
if (myPostProcessor.isElementPartlyInRange(element)) {
preHook()
+1 -1
View File
@@ -2,6 +2,6 @@
modules = [
AppModule::class,
DataModule::class,
DomainModule::class
DomainModule::class,
],
)
@@ -0,0 +1,131 @@
// SET_TRUE: ALLOW_TRAILING_COMMA
@Anno([1])
fun a() = Unit
@Anno([
1,
])
fun a() = Unit
@Anno([1
])
fun a() = Unit
@Anno([
1,
])
fun a() = Unit
@Anno([
1])
fun a() = Unit
@Anno([1, 2])
fun a() = Unit
@Anno([
1, 2,
])
fun a() = Unit
@Anno([1, 2
])
fun a() = Unit
@Anno([
1, 2,
])
fun a() = Unit
@Anno([
1, 2])
fun a() = Unit
@Anno([1, 2, 2])
fun a() = Unit
@Anno([
1, 2, 2,
])
fun a() = Unit
@Anno(["1"
])
fun a() = Unit
@Anno([
1,
])
fun a() = Unit
@Anno([
1, 2, 2])
fun a() = Unit
@Anno([1/*
*/])
fun a() = Unit
@Anno([
1, //dw
])
fun a() = Unit
@Anno([1 // ds
])
fun a() = Unit
@Anno([
/*
*/
// d
1,/*
*//*
*/
])
fun a() = Unit
@Anno([
/*
*/ 1])
fun a() = Unit
@Anno([1/*
*/, 2])
fun a() = Unit
@Anno([
1,
2/*
*/,/*
*/
])
fun a() = Unit
@Anno([/*
*/1, 2
])
fun a() = Unit
@Anno(["1"
])
fun a() = Unit
@Anno([
1,
])
fun a() = Unit
@Anno([
1, 2 /*
*/])
fun a() = Unit
@Component(
modules = [
AppModule::class, DataModule::class,
DomainModule::class
],
)
fun b() = Unit
@@ -0,0 +1,196 @@
// SET_TRUE: ALLOW_TRAILING_COMMA
@Anno([1])
fun a() = Unit
@Anno(
[
1,
],
)
fun a() = Unit
@Anno(
[
1,
],
)
fun a() = Unit
@Anno(
[
1,
],
)
fun a() = Unit
@Anno(
[
1,
],
)
fun a() = Unit
@Anno([1, 2])
fun a() = Unit
@Anno(
[
1, 2,
],
)
fun a() = Unit
@Anno(
[
1, 2,
],
)
fun a() = Unit
@Anno(
[
1, 2,
],
)
fun a() = Unit
@Anno(
[
1, 2,
],
)
fun a() = Unit
@Anno([1, 2, 2])
fun a() = Unit
@Anno(
[
1, 2, 2,
],
)
fun a() = Unit
@Anno(
[
"1",
],
)
fun a() = Unit
@Anno(
[
1,
],
)
fun a() = Unit
@Anno(
[
1, 2, 2,
],
)
fun a() = Unit
@Anno(
[
1,/*
*/
],
)
fun a() = Unit
@Anno(
[
1, //dw
],
)
fun a() = Unit
@Anno(
[
1, // ds
],
)
fun a() = Unit
@Anno(
[
/*
*/
// d
1,/*
*//*
*/
],
)
fun a() = Unit
@Anno(
[
/*
*/
1,
],
)
fun a() = Unit
@Anno(
[
1,/*
*/
2,
],
)
fun a() = Unit
@Anno(
[
1,
2/*
*/,/*
*/
],
)
fun a() = Unit
@Anno(
[
/*
*/
1, 2,
],
)
fun a() = Unit
@Anno(
[
"1",
],
)
fun a() = Unit
@Anno(
[
1,
],
)
fun a() = Unit
@Anno(
[
1,
2, /*
*/
],
)
fun a() = Unit
@Component(
modules = [
AppModule::class, DataModule::class,
DomainModule::class,
],
)
fun b() = Unit
@@ -0,0 +1,114 @@
// SET_TRUE: ALLOW_TRAILING_COMMA
@Anno([1])
fun a() = Unit
@Anno([1,])
fun a() = Unit
@Anno([1
])
fun a() = Unit
@Anno([
1, ])
fun a() = Unit
@Anno([
1 ])
fun a() = Unit
@Anno([1,2])
fun a() = Unit
@Anno([1, 2,])
fun a() = Unit
@Anno([1, 2
])
fun a() = Unit
@Anno([
1, 2, ])
fun a() = Unit
@Anno([
1, 2 ])
fun a() = Unit
@Anno([1, 2, 2])
fun a() = Unit
@Anno([1, 2, 2,])
fun a() = Unit
@Anno(["1"
])
fun a() = Unit
@Anno([
1, ])
fun a() = Unit
@Anno([
1 , 2 , 2 ])
fun a() = Unit
@Anno([1/*
*/])
fun a() = Unit
@Anno([1, //dw
])
fun a() = Unit
@Anno([1 // ds
])
fun a() = Unit
@Anno([/*
*/ // d
1/*
*/,/*
*/ ])
fun a() = Unit
@Anno([
/*
*/ 1 ])
fun a() = Unit
@Anno([1/*
*/,2])
fun a() = Unit
@Anno([1, 2/*
*/,/*
*/])
fun a() = Unit
@Anno([/*
*/1, 2
])
fun a() = Unit
@Anno(["1"
])
fun a() = Unit
@Anno([
1, ])
fun a() = Unit
@Anno([
1 , 2 /*
*/ ])
fun a() = Unit
@Component(
modules = [
AppModule::class, DataModule::class,
DomainModule::class
],
)
fun b() = Unit
@@ -1164,6 +1164,24 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/formatter/trailingComma"), Pattern.compile("^([^\\.]+)\\.after\\.kt.*$"), null, true);
}
@TestMetadata("idea/testData/formatter/trailingComma/collectionLiteralExpression")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class CollectionLiteralExpression extends AbstractFormatterTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInCollectionLiteralExpression() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/formatter/trailingComma/collectionLiteralExpression"), Pattern.compile("^([^\\.]+)\\.after\\.kt.*$"), null, true);
}
@TestMetadata("CollectionLiteralInAnnotation.after.kt")
public void testCollectionLiteralInAnnotation() throws Exception {
runTest("idea/testData/formatter/trailingComma/collectionLiteralExpression/CollectionLiteralInAnnotation.after.kt");
}
}
@TestMetadata("idea/testData/formatter/trailingComma/indices")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
@@ -1662,6 +1680,24 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/formatter/trailingComma"), Pattern.compile("^([^\\.]+)\\.after\\.inv\\.kt.*$"), null, true);
}
@TestMetadata("idea/testData/formatter/trailingComma/collectionLiteralExpression")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class CollectionLiteralExpression extends AbstractFormatterTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTestInverted, this, testDataFilePath);
}
public void testAllFilesPresentInCollectionLiteralExpression() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/formatter/trailingComma/collectionLiteralExpression"), Pattern.compile("^([^\\.]+)\\.after\\.inv\\.kt.*$"), null, true);
}
@TestMetadata("CollectionLiteralInAnnotation.after.inv.kt")
public void testCollectionLiteralInAnnotation() throws Exception {
runTest("idea/testData/formatter/trailingComma/collectionLiteralExpression/CollectionLiteralInAnnotation.after.inv.kt");
}
}
@TestMetadata("idea/testData/formatter/trailingComma/indices")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)