Skip to content

Commit c5b83a5

Browse files
eyupcanakmanEgorand
authored andcommitted
Keep // prefix on wrapped file comment lines
Fixes #1922.
1 parent 246419e commit c5b83a5

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

docs/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Change Log
33

44
## Unreleased
55

6+
* Fix: Keep the `//` prefix on wrapped file comment lines. (#1922)
7+
68
## Version 2.3.0
79

810
Thanks to [@haruue][haruue], [@hfhbd][hfhbd], [@yz4230][yz4230], [@mina-jaff][mina-jaff], [@BoD][BoD],

kotlinpoet/src/jvmMain/kotlin/com/squareup/kotlinpoet/CodeWriter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,8 @@ internal class CodeWriter(
644644
} else {
645645
out.append(
646646
line,
647-
indentLevel = if (kdoc) indentLevel else indentLevel + 2,
648-
linePrefix = if (kdoc) " * " else "",
647+
indentLevel = if (kdoc || comment) indentLevel else indentLevel + 2,
648+
linePrefix = if (kdoc) " * " else if (comment) "// " else "",
649649
)
650650
}
651651
trailingNewline = false

kotlinpoet/src/jvmTest/kotlin/com/squareup/kotlinpoet/LineWrappingTest.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,26 @@ class LineWrappingTest {
261261
)
262262
}
263263

264+
@Test
265+
fun fileCommentKeepsPrefixOnWrappedLines() {
266+
val fileSpec =
267+
FileSpec.builder("com.squareup.tacos", "Taco.kt")
268+
.addFileComment(
269+
"This is a really long line of text that will♢have to be wrapped when it is♢used in a comment because it is so long"
270+
)
271+
.build()
272+
assertThat(fileSpec.toString())
273+
.isEqualTo(
274+
"""
275+
|// This is a really long line of text that will have to be wrapped when it is
276+
|// used in a comment because it is so long
277+
|package com.squareup.tacos
278+
|
279+
|"""
280+
.trimMargin()
281+
)
282+
}
283+
264284
private fun toString(typeSpec: TypeSpec): String {
265285
return FileSpec.get("com.squareup.tacos", typeSpec).toString()
266286
}

0 commit comments

Comments
 (0)