From 94a4edd5ccf43fbdb6ed833761afe659b82d4bf9 Mon Sep 17 00:00:00 2001 From: Andrey Tyrin Date: Tue, 14 Nov 2023 16:35:45 +0100 Subject: Remove empty spans rendering for missed modifiers (#3343) * Remove property modifiers if there are none * Remove variance modifier rendering if it is absent --- .../base/signatures/KotlinSignatureProvider.kt | 6 +- .../src/test/kotlin/signatures/SignatureTest.kt | 66 +++++++++++++++++++++- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/dokka-subprojects/plugin-base/src/main/kotlin/org/jetbrains/dokka/base/signatures/KotlinSignatureProvider.kt b/dokka-subprojects/plugin-base/src/main/kotlin/org/jetbrains/dokka/base/signatures/KotlinSignatureProvider.kt index 2180e776..0fc7ca72 100644 --- a/dokka-subprojects/plugin-base/src/main/kotlin/org/jetbrains/dokka/base/signatures/KotlinSignatureProvider.kt +++ b/dokka-subprojects/plugin-base/src/main/kotlin/org/jetbrains/dokka/base/signatures/KotlinSignatureProvider.kt @@ -248,7 +248,7 @@ public class KotlinSignatureProvider( p.modifier[sourceSet].takeIf { it !in ignoredModifiers }?.let { if (it is JavaModifier.Empty) KotlinModifier.Open else it }?.name?.let { keyword("$it ") } - p.modifiers()[sourceSet]?.toSignatureString()?.let { keyword(it) } + p.modifiers()[sourceSet]?.toSignatureString()?.takeIf { it.isNotEmpty() }?.let { keyword(it) } if (p.isMutable()) keyword("var ") else keyword("val ") list(p.generics, prefix = "<", suffix = "> ", separatorStyles = mainStyles + TokenStyle.Punctuation, @@ -303,7 +303,7 @@ public class KotlinSignatureProvider( f.modifier[sourceSet]?.takeIf { it !in ignoredModifiers }?.let { if (it is JavaModifier.Empty) KotlinModifier.Open else it }?.name?.let { keyword("$it ") } - f.modifiers()[sourceSet]?.toSignatureString()?.let { keyword(it) } + f.modifiers()[sourceSet]?.toSignatureString()?.takeIf { it.isNotEmpty() }?.let { keyword(it) } keyword("fun ") list( f.generics, prefix = "<", suffix = "> ", @@ -434,7 +434,7 @@ public class KotlinSignatureProvider( } is Variance<*> -> group(styles = emptySet()) { - keyword("$p ".takeIf { it.isNotBlank() } ?: "") + p.takeIf { it.toString().isNotEmpty() }?.let { keyword("$it ") } signatureForProjection(p.inner, showFullyQualifiedName) } diff --git a/dokka-subprojects/plugin-base/src/test/kotlin/signatures/SignatureTest.kt b/dokka-subprojects/plugin-base/src/test/kotlin/signatures/SignatureTest.kt index 80a043fe..3099400e 100644 --- a/dokka-subprojects/plugin-base/src/test/kotlin/signatures/SignatureTest.kt +++ b/dokka-subprojects/plugin-base/src/test/kotlin/signatures/SignatureTest.kt @@ -197,6 +197,27 @@ class SignatureTest : BaseAbstractTest() { } } + @Test + fun `fun with use site variance modifier in`() { + val source = source("fun simpleFun(params: Array): Unit") + val writerPlugin = TestOutputWriterPlugin() + + testInline( + source, + configuration, + pluginOverrides = listOf(writerPlugin) + ) { + renderingStage = { _, _ -> + writerPlugin.writer.renderedContent("root/example/simple-fun.html").firstSignature().match( + "fun ", A("simpleFun"), "(", Parameters( + Parameter("params: ", A("Array"), ""), + ), ")", + ignoreSpanWithTokenStyle = true + ) + } + } + } + @Test fun `fun with definitely non-nullable types`() { val source = source("fun elvisLike(x: T, y: T & Any): T & Any = x ?: y") @@ -312,6 +333,48 @@ class SignatureTest : BaseAbstractTest() { } } + @Test + fun `class with declaration site variance modifier`() { + val writerPlugin = TestOutputWriterPlugin() + + testInline( + """ + |/src/main/kotlin/common/Test.kt + |package example + | + |class PrimaryConstructorClass { } + """.trimMargin(), + configuration, + pluginOverrides = listOf(writerPlugin) + ) { + renderingStage = { _, _ -> + writerPlugin.writer.renderedContent("root/example/-primary-constructor-class/index.html").firstSignature().match( + Span("class "), A("PrimaryConstructorClass"), Span("<"), Span("out "), A("T"), Span(">"), + ) + } + } + } + + @Test + fun `constructor property on class page`() { + val source = source("data class DataClass(val arg: String)") + val writerPlugin = TestOutputWriterPlugin() + + testInline( + source, + configuration, + pluginOverrides = listOf(writerPlugin) + ) { + renderingStage = { _, _ -> + assertEquals( + writerPlugin.writer.renderedContent("root/example/-data-class/index.html").lastSignature().html(), + "val arg: String" + + ) + } + } + } + @Test fun `functional interface`() { val source = source("fun interface KRunnable") @@ -896,8 +959,7 @@ class SignatureTest : BaseAbstractTest() { ) { renderingStage = { _, _ -> writerPlugin.writer.renderedContent("root/example/-primary-constructor-class/index.html").firstSignature().match( - // In `` expression, an empty `` is present for some reason - Span("class "), A("PrimaryConstructorClass"), Span("<"), Span(), A("T"), Span(">"), Span("("), Parameters( + Span("class "), A("PrimaryConstructorClass"), Span("<"), A("T"), Span(">"), Span("("), Parameters( Parameter(Span("val "), "x", Span(": "), A("Int"), Span(",")), Parameter(Span("var "), "s", Span(": "), A("String")) ), Span(")"), -- cgit