From 3458c5dfcbfab8fcca39edbda9e8c4c754657d30 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Sat, 18 Nov 2023 22:37:53 -0500 Subject: [PATCH] Remove superfluous newline (#1727) --- .../com/squareup/kotlinpoet/TypeSpec.kt | 33 +++++++++---------- .../com/squareup/kotlinpoet/TypeSpecTest.kt | 21 ++++++++++++ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeSpec.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeSpec.kt index 42549f272e..43ae82a2fc 100644 --- a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeSpec.kt +++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/TypeSpec.kt @@ -380,25 +380,24 @@ public class TypeSpec private constructor( * property/parameter, and the parameter's KDoc will be printed in the type header. */ private fun kdocWithConstructorDocs(): CodeBlock { - if (primaryConstructor == null) { - return kdoc.ensureEndsWithNewLine() - } - val constructorProperties = constructorProperties() - val parametersWithKdoc = primaryConstructor.parameters.filter { parameter -> - val propertyKdoc = constructorProperties[parameter.name]?.kdoc ?: CodeBlock.EMPTY - return@filter parameter.kdoc.isNotEmpty() && propertyKdoc.isNotEmpty() && - parameter.kdoc != propertyKdoc - } - return with(kdoc.ensureEndsWithNewLine().toBuilder()) { - if (kdoc.isNotEmpty()) add("\n") - if (primaryConstructor.kdoc.isNotEmpty()) { - add("@constructor %L", primaryConstructor.kdoc.ensureEndsWithNewLine()) - } - parametersWithKdoc.forEach { parameter -> - add("@param %L %L", parameter.name, parameter.kdoc.ensureEndsWithNewLine()) + val classKdoc = kdoc.ensureEndsWithNewLine() + val constructorKdoc = buildCodeBlock { + if (primaryConstructor != null) { + if (primaryConstructor.kdoc.isNotEmpty()) { + add("@constructor %L", primaryConstructor.kdoc.ensureEndsWithNewLine()) + } + val constructorProperties = constructorProperties() + primaryConstructor.parameters.forEach { parameter -> + val propertyKdoc = constructorProperties[parameter.name]?.kdoc ?: CodeBlock.EMPTY + if (parameter.kdoc.isNotEmpty() && propertyKdoc.isNotEmpty() && parameter.kdoc != propertyKdoc) { + add("@param %L %L", parameter.name, parameter.kdoc.ensureEndsWithNewLine()) + } + } } - build() } + return listOf(classKdoc, constructorKdoc) + .filter(CodeBlock::isNotEmpty) + .joinToCode(separator = "\n") } private val hasInitializer: Boolean get() = initializerIndex != -1 && initializerBlock.isNotEmpty() diff --git a/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeSpecTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeSpecTest.kt index ebac6356ff..4d8a836939 100644 --- a/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeSpecTest.kt +++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/TypeSpecTest.kt @@ -5615,6 +5615,27 @@ class TypeSpecTest { ) } + @Test fun classKdoc() { + val type = TypeSpec.classBuilder("MyClass") + .addKdoc("This is my class") + .primaryConstructor( + FunSpec.constructorBuilder() + .build(), + ) + .build() + + //language=kotlin + assertThat(type.toString()).isEqualTo( + """ + /** + * This is my class + */ + public class MyClass() + + """.trimIndent(), + ) + } + // https://github.com/square/kotlinpoet/issues/1630 @Test fun primaryConstructorKDoc() { val type = TypeSpec.classBuilder("MyClass")