Skip to content

Commit

Permalink
Remove superfluous newline (#1727)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Nov 19, 2023
1 parent 6e302a9 commit 3458c5d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 3458c5d

Please sign in to comment.