Skip to content

Commit ea88a0a

Browse files
committed
Fix sample body import separator
1 parent 15921f5 commit ea88a0a

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

dokka-subprojects/plugin-base/src/main/kotlin/org/jetbrains/dokka/base/transformers/pages/DefaultSamplesTransformer.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,38 @@ internal class DefaultSamplesTransformer(val context: DokkaContext) : PageTransf
5858
return dfs(fqLink, node)
5959
}
6060

61+
/**
62+
* If both [imports] and [body] are present, it should return
63+
*
64+
* ```kotlin
65+
* import com.example.One
66+
* import com.example.Two
67+
*
68+
* fun main() {
69+
* //sampleStart
70+
* println("Sample function body")
71+
* println("Another line")
72+
* //sampleEnd
73+
* }
74+
* ```
75+
*
76+
* If [imports] are empty, it should return:
77+
*
78+
* ```kotlin
79+
* fun main() {
80+
* //sampleStart
81+
* println("Sample function body")
82+
* println("Another line")
83+
* //sampleEnd
84+
* }
85+
* ```
86+
*
87+
* Notice the presence/absence of the new line before the body.
88+
*/
6189
private fun createSampleBody(imports: List<String>, body: String) =
62-
""" |${imports.takeIf { it.isNotEmpty() }?.joinToString { "import $it\n" } ?: ""}
90+
// takeIf {} is needed so that joinToString's postfix is not added for empty lists,
91+
// and trimMargin() then removes the first empty line
92+
""" |${imports.takeIf { it.isNotEmpty() }?.joinToString(separator = "\n", postfix = "\n") { "import $it" } ?: "" }
6393
|fun main() {
6494
| //sampleStart
6595
| $body

dokka-subprojects/plugin-base/src/test/kotlin/linkableContent/LinkableContentTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ class LinkableContentTest : BaseAbstractTest() {
213213
assertEquals(
214214
"""
215215
|import p2.${name}Class
216+
|import kotlin.collections.List
217+
|import kotlin.collections.Map
216218
|
217219
|fun main() {
218220
| //sampleStart

dokka-subprojects/plugin-base/src/test/resources/linkable/samples/jsMain/resources/Samples.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package samples
66

77
import p2.JsClass
8+
import kotlin.collections.List
9+
import kotlin.collections.Map
810

911
class SamplesJs {
1012

dokka-subprojects/plugin-base/src/test/resources/linkable/samples/jvmMain/resources/Samples.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
package samples
66

77
import p2.JvmClass
8+
import kotlin.collections.List
9+
import kotlin.collections.Map
810

911
class SamplesJvm {
1012

0 commit comments

Comments
 (0)