From a379a814ba5b3896d49bda489d0ebdb4b1638830 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 22 Jun 2017 19:49:59 +0200 Subject: [PATCH] Implement option for wrapping class annotations --- .../kotlin/idea/formatter/KotlinCommonBlock.kt | 16 ++++++++++------ .../KotlinLanguageCodeStyleSettingsProvider.kt | 3 ++- .../formatter/ClassAnnotationWrapping.after.kt | 4 ++++ .../formatter/ClassAnnotationWrapping.kt | 2 ++ .../classUsages/annotation1.kt.after | 3 ++- .../classUsages/annotation3.kt.after | 3 ++- .../classUsages/annotation4.kt.after | 3 ++- .../classUsages/annotationKeepNamedArgs.kt.after | 3 ++- .../wholeProject/annotation.after.Declaration.kt | 3 ++- .../kotlin/formatter/FormatterTestGenerated.java | 6 ++++++ 10 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 idea/testData/formatter/ClassAnnotationWrapping.after.kt create mode 100644 idea/testData/formatter/ClassAnnotationWrapping.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 c1c57a973ce..81a3684b823 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt @@ -30,10 +30,7 @@ import org.jetbrains.kotlin.kdoc.lexer.KDocTokens import org.jetbrains.kotlin.kdoc.parser.KDocElementTypes import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens.* -import org.jetbrains.kotlin.psi.KtBlockExpression -import org.jetbrains.kotlin.psi.KtDeclaration -import org.jetbrains.kotlin.psi.KtExpression -import org.jetbrains.kotlin.psi.KtSuperTypeListEntry +import org.jetbrains.kotlin.psi.* import java.util.* private val QUALIFIED_OPERATION = TokenSet.create(DOT, SAFE_ACCESS) @@ -353,15 +350,22 @@ abstract class KotlinCommonBlock( } elementType === KtNodeTypes.MODIFIER_LIST -> - when (node.treeParent.elementType) { - KtNodeTypes.VALUE_PARAMETER -> + when (node.treeParent.psi) { + is KtParameter -> return getWrappingStrategyForItemList(commonSettings.PARAMETER_ANNOTATION_WRAP, KtNodeTypes.ANNOTATION_ENTRY, !node.treeParent.isFirstParameter()) + is KtClassOrObject -> + return getWrappingStrategyForItemList(commonSettings.CLASS_ANNOTATION_WRAP, + KtNodeTypes.ANNOTATION_ENTRY) + } elementType === KtNodeTypes.VALUE_PARAMETER -> return wrapAfterAnnotation(commonSettings.PARAMETER_ANNOTATION_WRAP) + + node.psi is KtClassOrObject -> + return wrapAfterAnnotation(commonSettings.CLASS_ANNOTATION_WRAP) } return WrappingStrategy.NoWrapping diff --git a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt index e7d1ac493f1..da7602ab24e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt @@ -30,7 +30,7 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide override fun getCodeSample(settingsType: LanguageCodeStyleSettingsProvider.SettingsType): String = when (settingsType) { LanguageCodeStyleSettingsProvider.SettingsType.WRAPPING_AND_BRACES_SETTINGS -> """ - public class ThisIsASampleClass : Comparable<*>, Appendable { + @Deprecated("Foo") public class ThisIsASampleClass : Comparable<*>, Appendable { val test = 12 @@ -236,6 +236,7 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide "CALL_PARAMETERS_WRAP", "METHOD_PARAMETERS_WRAP", "EXTENDS_LIST_WRAP", + "CLASS_ANNOTATION_WRAP", "PARAMETER_ANNOTATION_WRAP", "METHOD_PARAMETERS_LPAREN_ON_NEXT_LINE", "METHOD_PARAMETERS_RPAREN_ON_NEXT_LINE", diff --git a/idea/testData/formatter/ClassAnnotationWrapping.after.kt b/idea/testData/formatter/ClassAnnotationWrapping.after.kt new file mode 100644 index 00000000000..ba65118a5e9 --- /dev/null +++ b/idea/testData/formatter/ClassAnnotationWrapping.after.kt @@ -0,0 +1,4 @@ +@Deprecated("Foo") +@Deprecated("Bar") +class Foo { +} \ No newline at end of file diff --git a/idea/testData/formatter/ClassAnnotationWrapping.kt b/idea/testData/formatter/ClassAnnotationWrapping.kt new file mode 100644 index 00000000000..8946d6a86aa --- /dev/null +++ b/idea/testData/formatter/ClassAnnotationWrapping.kt @@ -0,0 +1,2 @@ +@Deprecated("Foo") @Deprecated("Bar") class Foo { +} \ No newline at end of file diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotation1.kt.after b/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotation1.kt.after index e9f9e228e25..d45e2745146 100644 --- a/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotation1.kt.after +++ b/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotation1.kt.after @@ -7,4 +7,5 @@ annotation class Foo annotation class Bar -@Bar class C {} \ No newline at end of file +@Bar +class C {} \ No newline at end of file diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotation3.kt.after b/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotation3.kt.after index e2db915cda4..2f429a18811 100644 --- a/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotation3.kt.after +++ b/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotation3.kt.after @@ -7,4 +7,5 @@ annotation class Foo annotation class Bar(val p: Int) -@Bar(1) class C {} \ No newline at end of file +@Bar(1) +class C {} \ No newline at end of file diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotation4.kt.after b/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotation4.kt.after index 76f17395310..1b63a125a56 100644 --- a/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotation4.kt.after +++ b/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotation4.kt.after @@ -7,4 +7,5 @@ annotation class Foo(val p: Int) annotation class Bar(val p: Int, val s: String) -@Bar(1, "") class C {} \ No newline at end of file +@Bar(1, "") +class C {} \ No newline at end of file diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotationKeepNamedArgs.kt.after b/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotationKeepNamedArgs.kt.after index baac3fe1e66..84d8ee83be4 100644 --- a/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotationKeepNamedArgs.kt.after +++ b/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/annotationKeepNamedArgs.kt.after @@ -7,4 +7,5 @@ annotation class Foo(val p: Int) annotation class Bar(val p: Int, val s: String) -@Bar(p = 1, s = "") class C \ No newline at end of file +@Bar(p = 1, s = "") +class C \ No newline at end of file diff --git a/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/wholeProject/annotation.after.Declaration.kt b/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/wholeProject/annotation.after.Declaration.kt index 24426db5991..e44bff3b6c8 100644 --- a/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/wholeProject/annotation.after.Declaration.kt +++ b/idea/testData/quickfix/deprecatedSymbolUsage/classUsages/wholeProject/annotation.after.Declaration.kt @@ -5,4 +5,5 @@ annotation class OldAnnotation(val p: Int = 0) annotation class NewAnnotation(val p: Int = 0, val newP: String = "") -@NewAnnotation class C \ No newline at end of file +@NewAnnotation +class C \ 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 4866a291371..777b54d8ee5 100644 --- a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java @@ -152,6 +152,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest { doTest(fileName); } + @TestMetadata("ClassAnnotationWrapping.after.kt") + public void testClassAnnotationWrapping() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/ClassAnnotationWrapping.after.kt"); + doTest(fileName); + } + @TestMetadata("ClassInBody.after.kt") public void testClassInBody() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/ClassInBody.after.kt");