From cf4bb4673730cc0787784380fc986efec8f8e9fb Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Tue, 7 Feb 2017 15:48:30 +0300 Subject: KT-16197 WebDemo: No empty strings #KT-16197 fixed --- .../main/kotlin/Samples/DefaultSampleProcessingService.kt | 4 ++-- .../kotlin/Samples/KotlinWebsiteSampleProcessingService.kt | 14 ++++++-------- .../format/KotlinWebSiteRunnableSamplesFormatTest.kt | 4 ++++ core/src/test/kotlin/model/CommentTest.kt | 6 +++++- 4 files changed, 17 insertions(+), 11 deletions(-) (limited to 'core/src') diff --git a/core/src/main/kotlin/Samples/DefaultSampleProcessingService.kt b/core/src/main/kotlin/Samples/DefaultSampleProcessingService.kt index e6539135..e9cf5e18 100644 --- a/core/src/main/kotlin/Samples/DefaultSampleProcessingService.kt +++ b/core/src/main/kotlin/Samples/DefaultSampleProcessingService.kt @@ -44,8 +44,8 @@ open class DefaultSampleProcessingService val text = processSampleBody(psiElement) - val lines = text.trimEnd().split("\n".toRegex()).toTypedArray().filterNot(String::isEmpty) - val indent = lines.map { it.takeWhile(Char::isWhitespace).count() }.min() ?: 0 + val lines = text.trimEnd().split("\n") + val indent = lines.filter(String::isNotBlank).map { it.takeWhile(Char::isWhitespace).count() }.min() ?: 0 val finalText = lines.map { it.drop(indent) }.joinToString("\n") return ContentBlockSampleCode(importsBlock = processImports(psiElement)).apply { append(ContentText(finalText)) } diff --git a/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt b/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt index baf44904..864033f0 100644 --- a/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt +++ b/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt @@ -2,6 +2,7 @@ package org.jetbrains.dokka.Samples import com.google.inject.Inject import com.intellij.psi.PsiElement +import com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.dokka.* import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.resolve.ImportPath @@ -12,7 +13,7 @@ open class KotlinWebsiteSampleProcessingService resolutionFacade: DokkaResolutionFacade) : DefaultSampleProcessingService(options, logger, resolutionFacade) { - private class SampleBuilder() : KtVisitorVoid() { + private class SampleBuilder : KtTreeVisitorVoid() { val builder = StringBuilder() val text: String get() = builder.toString() @@ -48,13 +49,10 @@ open class KotlinWebsiteSampleProcessingService } } - override fun visitElement(element: PsiElement?) { - if (element != null) { - if (element.children.isEmpty()) - builder.append(element.text) - else - element.acceptChildren(this) - } + override fun visitElement(element: PsiElement) { + if (element is LeafPsiElement) + builder.append(element.text) + super.visitElement(element) } } diff --git a/core/src/test/kotlin/format/KotlinWebSiteRunnableSamplesFormatTest.kt b/core/src/test/kotlin/format/KotlinWebSiteRunnableSamplesFormatTest.kt index 742d2908..2a9900af 100644 --- a/core/src/test/kotlin/format/KotlinWebSiteRunnableSamplesFormatTest.kt +++ b/core/src/test/kotlin/format/KotlinWebSiteRunnableSamplesFormatTest.kt @@ -20,6 +20,10 @@ class KotlinWebSiteRunnableSamplesFormatTest { verifyKWSNodeByName("sampleWithAsserts", "a") } + @Test fun newLinesInSamples() { + verifyKWSNodeByName("newLinesInSamples", "foo") + } + private fun verifyKWSNodeByName(fileName: String, name: String) { verifyOutput("testdata/format/website-samples/$fileName.kt", ".md", format = "kotlin-website-samples") { model, output -> kwsService.createOutputBuilder(output, tempLocation).appendNodes(model.members.single().members.filter { it.name == name }) diff --git a/core/src/test/kotlin/model/CommentTest.kt b/core/src/test/kotlin/model/CommentTest.kt index 25fea48d..0d6a3374 100644 --- a/core/src/test/kotlin/model/CommentTest.kt +++ b/core/src/test/kotlin/model/CommentTest.kt @@ -153,22 +153,26 @@ line two""", toTestString()) with(model.members.single().members.first()) { assertEquals("Summary", content.summary.toTestString()) with (content.description) { - assertEqualsIgnoringSeparators("""[code lang=kotlin] + assertEqualsIgnoringSeparators(""" + |[code lang=kotlin] |if (true) { | println(property) |} |[/code] |[code lang=kotlin] + | |if (true) { | println(property) |} |[/code] |[code lang=kotlin] + | |if (true) { | println(property) |} |[/code] |[code lang=kotlin] + | |if (true) { | println(property) |} -- cgit