From ccd780c00832c39d7c95de955eb5e1dd6af685d5 Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Thu, 26 Dec 2019 20:08:04 +0700 Subject: [PATCH] Formatter: support trailing comma in type arguments #KT-34744 --- .../idea/formatter/KotlinCommonBlock.kt | 4 + .../TrailingCommaPostFormatProcessor.kt | 4 + .../TypeArgumentList.after.inv.kt | 133 ++++++++++++ .../typeArguments/TypeArgumentList.after.kt | 197 ++++++++++++++++++ .../typeArguments/TypeArgumentList.kt | 106 ++++++++++ .../formatter/FormatterTestGenerated.java | 36 ++++ 6 files changed, 480 insertions(+) create mode 100644 idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.after.inv.kt create mode 100644 idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.after.kt create mode 100644 idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.kt diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt index b8c432a726d..727f1f66659 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt @@ -619,6 +619,10 @@ abstract class KotlinCommonBlock( trailingCommaWrappingStrategyWithMultiLineCheck(LT, GT)(childElement) } + elementType === TYPE_ARGUMENT_LIST -> return { childElement -> + trailingCommaWrappingStrategyWithMultiLineCheck(LT, GT)(childElement) + } + elementType === SUPER_TYPE_LIST -> { val wrap = Wrap.createWrap(commonSettings.EXTENDS_LIST_WRAP, false) return { childElement -> if (childElement.psi is KtSuperTypeListEntry) wrap else null } diff --git a/idea/src/org/jetbrains/kotlin/idea/formatter/TrailingCommaPostFormatProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/formatter/TrailingCommaPostFormatProcessor.kt index 4b7dc65fbf8..47f04368e4c 100644 --- a/idea/src/org/jetbrains/kotlin/idea/formatter/TrailingCommaPostFormatProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/formatter/TrailingCommaPostFormatProcessor.kt @@ -50,6 +50,10 @@ private class TrailingCommaVisitor(val settings: CodeStyleSettings) : KtTreeVisi super.visitTypeParameterList(list) } + override fun visitTypeArgumentList(typeArgumentList: KtTypeArgumentList) = processCommaOwnerIfInRange(typeArgumentList) { + super.visitTypeArgumentList(typeArgumentList) + } + private fun processCommaOwnerIfInRange(element: KtElement, preHook: () -> Unit = {}) { if (myPostProcessor.isElementPartlyInRange(element)) { preHook() diff --git a/idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.after.inv.kt b/idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.after.inv.kt new file mode 100644 index 00000000000..89e803e114e --- /dev/null +++ b/idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.after.inv.kt @@ -0,0 +1,133 @@ +// SET_TRUE: ALLOW_TRAILING_COMMA + +fun foo() { + testtest< + foofoo, + testtest< + testtest< + foofoo, + >, + >, + testsa, + >() + + testtest() + + testtest< + foofoo, foofoo, foofoo, foofoo, bar + >() + + testtest() + + testtest() + + testtest() + + testtest< + foofoo>() + + testtest< + foofoo + >() + + testtest< + foofoo, + >() + + testtest>>() + + testtest< + foofoo, fososos, testtest>, + >() + + testtest>, testsa>() + + testtest>, testsa>() + + testtest< + foofoo, foofoo, foofoo, foofoo, + bar /* + */, /* */ foo + >() + + testtest() + + testtest() + + testtest() + + testtest() + + testtest() + + testtest< /**/ + foofoo + >() + + testtest< + foofoo,/**/ + >() + + testtest() + + testtest() + + testtest< /**/ + foofoo + >() + + testtest< + foofoo,/**/ + >() + + testtest< + foofoo, fososos,/* + */ + testtest>, + >() + + testtest>, /**/testsa>() + + testtest>/* */, /**/testsa>() + + testtest>/* + */, testsa>() + + testtest>, /**/testsa>() + + testtest>, /* + */testsa>() +} \ No newline at end of file diff --git a/idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.after.kt b/idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.after.kt new file mode 100644 index 00000000000..2224c4ea0c2 --- /dev/null +++ b/idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.after.kt @@ -0,0 +1,197 @@ +// SET_TRUE: ALLOW_TRAILING_COMMA + +fun foo() { + testtest< + foofoo, + testtest< + testtest< + foofoo, + >, + >, + testsa, + >() + + testtest< + foofoo, foofoo, foofoo, + foofoo, bar, + >() + + testtest< + foofoo, foofoo, foofoo, foofoo, bar, + >() + + testtest< + foofoo, foofoo, foofoo, foofoo, bar, + >() + + testtest< + foofoo, foofoo, foofoo, foofoo, + bar, + >() + + testtest< + foofoo, + >() + + testtest< + foofoo, + >() + + testtest< + foofoo, + >() + + testtest< + foofoo, + >() + + testtest>>() + + testtest< + foofoo, fososos, testtest>, + >() + + testtest< + foofoo, + testtest< + testtest< + foofoo, + >, + >, + testsa, + >() + + testtest< + foofoo, *, + testtest< + testtest< + foofoo, + >, + >, + testsa, + >() + + testtest< + foofoo, foofoo, foofoo, foofoo, + bar, /* + */ /* */ + foo, + >() + + testtest< +/* + */ + foofoo, foofoo, foofoo, /* + + */ + foofoo, bar, + >() + + testtest< + foofoo, foofoo, foofoo, foofoo, + bar,/* + */ + >() + + testtest< + foofoo, foofoo, foofoo, foofoo, bar, // awdawda + >() + + testtest< + foofoo, foofoo, foofoo, foofoo, /* + + */ + bar, + >() + + testtest< + foofoo, // fd + >() + + testtest< + /**/ + foofoo, + >() + + testtest< + foofoo,/**/ + >() + + testtest< + foofoo, foofoo, foofoo, + foofoo,/* + */ /* */ + bar, + >() + + testtest< + foofoo, // fd + >() + + testtest< + /**/ + foofoo, + >() + + testtest< + foofoo,/**/ + >() + + testtest< + foofoo, fososos,/* + */ + testtest>, + >() + + testtest< + foofoo, + testtest< + testtest< + foofoo, + >, + >, /**/ + testsa, + >() + + testtest< + foofoo, + testtest< + testtest< + foofoo, + >, + >,/* */ /**/ + testsa, + >() + + testtest< + foofoo, + testtest< + testtest< + foofoo, + >, + >,/* + */ + testsa, + >() + + testtest< + foofoo, seee, + testtest< + testtest< + foofoo, + >, + >, /**/ + testsa, + >() + + testtest< + foofoo, seee, + testtest< + testtest< + foofoo, + >, + >, /* + */ + testsa, + >() +} \ No newline at end of file diff --git a/idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.kt b/idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.kt new file mode 100644 index 00000000000..d1c32bea453 --- /dev/null +++ b/idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.kt @@ -0,0 +1,106 @@ +// SET_TRUE: ALLOW_TRAILING_COMMA + +fun foo() { + testtest, + >, + testsa, + >() + + testtest() + + testtest< + foofoo, foofoo, foofoo, foofoo, bar + >() + + testtest() + + testtest() + + testtest() + + testtest< + foofoo>() + + testtest< + foofoo + >() + + testtest() + + testtest>>() + + testtest>,>() + + testtest>, testsa>() + + testtest>, testsa>() + + testtest< + foofoo, foofoo, foofoo, foofoo, + bar /* + */, /* */ foo + >() + + testtest() + + testtest() + + testtest() + + testtest() + + testtest() + + testtest< /**/ + foofoo + >() + + testtest() + + testtest() + + testtest() + + testtest< /**/ + foofoo + >() + + testtest() + + testtest>,>() + + testtest>, /**/testsa>() + + testtest>/* */ , /**/testsa>() + + testtest>/* + */ ,testsa>() + + testtest>, /**/testsa>() + + testtest>, /* + */testsa>() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java index 61ccdbcce10..db5bbcf2858 100644 --- a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java @@ -1182,6 +1182,24 @@ public class FormatterTestGenerated extends AbstractFormatterTest { } } + @TestMetadata("idea/testData/formatter/trailingComma/typeArguments") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class TypeArguments extends AbstractFormatterTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, this, testDataFilePath); + } + + public void testAllFilesPresentInTypeArguments() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/formatter/trailingComma/typeArguments"), Pattern.compile("^([^\\.]+)\\.after\\.kt.*$"), null, true); + } + + @TestMetadata("TypeArgumentList.after.kt") + public void testTypeArgumentList() throws Exception { + runTest("idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.after.kt"); + } + } + @TestMetadata("idea/testData/formatter/trailingComma/typeParameters") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) @@ -1662,6 +1680,24 @@ public class FormatterTestGenerated extends AbstractFormatterTest { } } + @TestMetadata("idea/testData/formatter/trailingComma/typeArguments") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class TypeArguments extends AbstractFormatterTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestInverted, this, testDataFilePath); + } + + public void testAllFilesPresentInTypeArguments() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/testData/formatter/trailingComma/typeArguments"), Pattern.compile("^([^\\.]+)\\.after\\.inv\\.kt.*$"), null, true); + } + + @TestMetadata("TypeArgumentList.after.inv.kt") + public void testTypeArgumentList() throws Exception { + runTest("idea/testData/formatter/trailingComma/typeArguments/TypeArgumentList.after.inv.kt"); + } + } + @TestMetadata("idea/testData/formatter/trailingComma/typeParameters") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)