diff --git a/formatter/src/main/kotlin/org/kotlin/formatter/scanning/KDocScanner.kt b/formatter/src/main/kotlin/org/kotlin/formatter/scanning/KDocScanner.kt index b2841ec..2c048e9 100644 --- a/formatter/src/main/kotlin/org/kotlin/formatter/scanning/KDocScanner.kt +++ b/formatter/src/main/kotlin/org/kotlin/formatter/scanning/KDocScanner.kt @@ -46,7 +46,7 @@ internal class KDocScanner(private val kotlinScanner: KotlinScanner) : NodeScann private fun String.trimOneWhitespace(): String = if (startsWith(' ') && endsWith(' ')) { - substring(1, length - 1) + substring(1, Integer.max(length - 1, 1)) } else { this } diff --git a/formatter/src/test/kotlin/org/kotlin/formatter/KotlinFormatterTest.kt b/formatter/src/test/kotlin/org/kotlin/formatter/KotlinFormatterTest.kt index df338a3..07a3807 100644 --- a/formatter/src/test/kotlin/org/kotlin/formatter/KotlinFormatterTest.kt +++ b/formatter/src/test/kotlin/org/kotlin/formatter/KotlinFormatterTest.kt @@ -3839,6 +3839,25 @@ class KotlinFormatterTest { ) } + @Test + fun `accepts empty KDoc`() { + val subject = KotlinFormatter(maxLineLength = 40) + + val result = subject.format( + """ + /** */ + class MyClass + """.trimIndent() + ) + + assertThat(result).isEqualTo( + """ + /** */ + class MyClass + """.trimIndent() + ) + } + @Test fun `maintains a line break between KDoc and object declaration`() { val subject = KotlinFormatter(maxLineLength = 40)