From ada91f40f716b20a69d8372dc5c6e250312543f7 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 5 Jan 2016 18:01:13 +0100 Subject: render multiple type parameter constraints according to current Kotlin syntax --- .../main/kotlin/Kotlin/KotlinLanguageService.kt | 25 +++++++++++++++++++--- core/src/test/kotlin/format/MarkdownFormatTest.kt | 4 ++++ 2 files changed, 26 insertions(+), 3 deletions(-) (limited to 'core/src') diff --git a/core/src/main/kotlin/Kotlin/KotlinLanguageService.kt b/core/src/main/kotlin/Kotlin/KotlinLanguageService.kt index 9c9c9fc7..a5bb5ee7 100644 --- a/core/src/main/kotlin/Kotlin/KotlinLanguageService.kt +++ b/core/src/main/kotlin/Kotlin/KotlinLanguageService.kt @@ -115,8 +115,8 @@ class KotlinLanguageService : LanguageService { identifier(node.name) } - private fun ContentBlock.renderList(nodes: List, separator: String = ", ", - noWrap: Boolean = false, renderItem: (DocumentationNode) -> Unit) { + private fun ContentBlock.renderList(nodes: List, separator: String = ", ", + noWrap: Boolean = false, renderItem: (T) -> Unit) { if (nodes.none()) return renderItem(nodes.first()) @@ -201,7 +201,7 @@ class KotlinLanguageService : LanguageService { identifier(node.name) val constraints = node.details(NodeKind.UpperBound) - if (constraints.any()) { + if (constraints.size == 1) { nbsp() symbol(":") nbsp() @@ -240,6 +240,22 @@ class KotlinLanguageService : LanguageService { } } + private fun ContentBlock.renderExtraTypeParameterConstraints(node: DocumentationNode, renderMode: RenderMode) { + val parametersWithMultipleConstraints = node.details(NodeKind.TypeParameter).filter { it.details(NodeKind.UpperBound).size > 1 } + val parametersWithConstraints = parametersWithMultipleConstraints + .flatMap { parameter -> parameter.details(NodeKind.UpperBound).map { constraint -> parameter to constraint } } + if (parametersWithMultipleConstraints.isNotEmpty()) { + keyword(" where ") + renderList(parametersWithConstraints) { + identifier(it.first.name) + nbsp() + symbol(":") + nbsp() + renderType(it.second, renderMode) + } + } + } + private fun ContentBlock.renderSupertypesForNode(node: DocumentationNode, renderMode: RenderMode) { val supertypes = node.details(NodeKind.Supertype) if (supertypes.any()) { @@ -305,6 +321,7 @@ class KotlinLanguageService : LanguageService { identifierOrDeprecated(node) renderTypeParametersForNode(node, renderMode) renderSupertypesForNode(node, renderMode) + renderExtraTypeParameterConstraints(node, renderMode) } private fun ContentBlock.renderFunction(node: DocumentationNode, @@ -347,6 +364,7 @@ class KotlinLanguageService : LanguageService { else { symbol(")") } + renderExtraTypeParameterConstraints(node, renderMode) } private fun ContentBlock.renderReceiver(node: DocumentationNode, renderMode: RenderMode, signatureMapper: SignatureMapper?) { @@ -391,6 +409,7 @@ class KotlinLanguageService : LanguageService { identifierOrDeprecated(node) symbol(": ") renderType(node.detail(NodeKind.Type), renderMode) + renderExtraTypeParameterConstraints(node, renderMode) } fun DocumentationNode.getPropertyKeyword() = diff --git a/core/src/test/kotlin/format/MarkdownFormatTest.kt b/core/src/test/kotlin/format/MarkdownFormatTest.kt index 81e0cb6b..a723cb2a 100644 --- a/core/src/test/kotlin/format/MarkdownFormatTest.kt +++ b/core/src/test/kotlin/format/MarkdownFormatTest.kt @@ -222,6 +222,10 @@ public class MarkdownFormatTest { verifyMarkdownNodeByName("arrayAverage", "XArray") } + @Test fun multipleTypeParameterConstraints() { + verifyMarkdownNode("multipleTypeParameterConstraints", withKotlinRuntime = true) + } + private fun verifyMarkdownPackage(fileName: String, withKotlinRuntime: Boolean = false) { verifyOutput("testdata/format/$fileName.kt", ".package.md", withKotlinRuntime = withKotlinRuntime) { model, output -> markdownService.appendNodes(tempLocation, output, model.members) -- cgit