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 --- .../Samples/DefaultSampleProcessingService.kt | 4 +-- .../KotlinWebsiteSampleProcessingService.kt | 14 +++++----- .../KotlinWebSiteRunnableSamplesFormatTest.kt | 4 +++ core/src/test/kotlin/model/CommentTest.kt | 6 ++++- .../format/website-samples/newLinesInSamples.kt | 19 +++++++++++++ .../format/website-samples/newLinesInSamples.md | 31 ++++++++++++++++++++++ .../format/website-samples/sampleWithAsserts.md | 1 + 7 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 core/testdata/format/website-samples/newLinesInSamples.kt create mode 100644 core/testdata/format/website-samples/newLinesInSamples.md (limited to 'core') 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) |} diff --git a/core/testdata/format/website-samples/newLinesInSamples.kt b/core/testdata/format/website-samples/newLinesInSamples.kt new file mode 100644 index 00000000..ee49aefc --- /dev/null +++ b/core/testdata/format/website-samples/newLinesInSamples.kt @@ -0,0 +1,19 @@ +fun groupBySample() { + val words = listOf("a", "abc", "ab", "def", "abcd") + val byLength = words.groupBy { it.length } + + assertPrints(byLength.keys, "[1, 3, 2, 4]") + assertPrints(byLength.values, "[[a], [abc, def], [ab], [abcd]]") + + val mutableByLength: MutableMap> = words.groupByTo(mutableMapOf()) { it.length } + // same content as in byLength map, but the map is mutable + assertTrue(mutableByLength == byLength) +} + + +/** + * @sample groupBySample + */ +fun foo() { + +} \ No newline at end of file diff --git a/core/testdata/format/website-samples/newLinesInSamples.md b/core/testdata/format/website-samples/newLinesInSamples.md new file mode 100644 index 00000000..9d3af798 --- /dev/null +++ b/core/testdata/format/website-samples/newLinesInSamples.md @@ -0,0 +1,31 @@ +--- +title: foo - test +layout: api +--- + + + +# foo + +
fun foo(): Unit
+
+ +``` kotlin + +fun main(args: Array) { +//sampleStart + +val words = listOf("a", "abc", "ab", "def", "abcd") +val byLength = words.groupBy { it.length } + +println(byLength.keys) // [1, 3, 2, 4] +println(byLength.values) // [[a], [abc, def], [ab], [abcd]] + +val mutableByLength: MutableMap> = words.groupByTo(mutableMapOf()) { it.length } +// same content as in byLength map, but the map is mutable +println("mutableByLength == byLength is ${mutableByLength == byLength}") // true +//sampleEnd +} +``` + +
diff --git a/core/testdata/format/website-samples/sampleWithAsserts.md b/core/testdata/format/website-samples/sampleWithAsserts.md index 98d7df33..e5284258 100644 --- a/core/testdata/format/website-samples/sampleWithAsserts.md +++ b/core/testdata/format/website-samples/sampleWithAsserts.md @@ -14,6 +14,7 @@ layout: api fun main(args: Array) { //sampleStart + println(a()) // Hello, Work println("a() == b() is ${a() == b()}") // true //sampleEnd -- cgit From 4517ce588431fc6080bad24d947c53c8d82fcaa0 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Thu, 9 Feb 2017 17:14:34 +0300 Subject: Removed leading newline --- core/src/main/kotlin/Samples/DefaultSampleProcessingService.kt | 5 ++--- core/src/test/kotlin/model/CommentTest.kt | 3 --- core/testdata/format/website-samples/newLinesInSamples.md | 1 - core/testdata/format/website-samples/sampleWithAsserts.md | 1 - 4 files changed, 2 insertions(+), 8 deletions(-) (limited to 'core') diff --git a/core/src/main/kotlin/Samples/DefaultSampleProcessingService.kt b/core/src/main/kotlin/Samples/DefaultSampleProcessingService.kt index e9cf5e18..1eb0c114 100644 --- a/core/src/main/kotlin/Samples/DefaultSampleProcessingService.kt +++ b/core/src/main/kotlin/Samples/DefaultSampleProcessingService.kt @@ -42,9 +42,8 @@ open class DefaultSampleProcessingService return ContentBlockSampleCode().apply { append(ContentText("//Source not found: $functionName")) } } - val text = processSampleBody(psiElement) - - val lines = text.trimEnd().split("\n") + val text = processSampleBody(psiElement).trim { it == '\n' || it == '\r' }.trimEnd() + val lines = text.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") diff --git a/core/src/test/kotlin/model/CommentTest.kt b/core/src/test/kotlin/model/CommentTest.kt index 0d6a3374..0d919128 100644 --- a/core/src/test/kotlin/model/CommentTest.kt +++ b/core/src/test/kotlin/model/CommentTest.kt @@ -160,19 +160,16 @@ line two""", toTestString()) |} |[/code] |[code lang=kotlin] - | |if (true) { | println(property) |} |[/code] |[code lang=kotlin] - | |if (true) { | println(property) |} |[/code] |[code lang=kotlin] - | |if (true) { | println(property) |} diff --git a/core/testdata/format/website-samples/newLinesInSamples.md b/core/testdata/format/website-samples/newLinesInSamples.md index 9d3af798..448725c3 100644 --- a/core/testdata/format/website-samples/newLinesInSamples.md +++ b/core/testdata/format/website-samples/newLinesInSamples.md @@ -14,7 +14,6 @@ layout: api fun main(args: Array) { //sampleStart - val words = listOf("a", "abc", "ab", "def", "abcd") val byLength = words.groupBy { it.length } diff --git a/core/testdata/format/website-samples/sampleWithAsserts.md b/core/testdata/format/website-samples/sampleWithAsserts.md index e5284258..98d7df33 100644 --- a/core/testdata/format/website-samples/sampleWithAsserts.md +++ b/core/testdata/format/website-samples/sampleWithAsserts.md @@ -14,7 +14,6 @@ layout: api fun main(args: Array) { //sampleStart - println(a()) // Hello, Work println("a() == b() is ${a() == b()}") // true //sampleEnd -- cgit From 64c8f35c00fcf483da76451b276e750533fbe9a4 Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Fri, 10 Feb 2017 16:34:26 +0300 Subject: Tweaking of sample formatting --- core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt | 2 +- core/testdata/format/website-samples/dropImport.md | 1 + core/testdata/format/website-samples/newLinesInSamples.md | 1 + core/testdata/format/website-samples/sample.md | 1 + core/testdata/format/website-samples/sampleWithAsserts.md | 1 + 5 files changed, 5 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt b/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt index 007e9353..9a43f9c4 100644 --- a/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt +++ b/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt @@ -164,7 +164,7 @@ class KotlinWebsiteRunnableSamplesOutputBuilder(to: StringBuilder, div(to, "sample", true) { appendBlockCode(language) { imports() - wrap("\nfun main(args: Array) {", "}") { + wrap("\n\nfun main(args: Array) {", "}") { wrap("\n//sampleStart\n", "\n//sampleEnd\n", body) } } diff --git a/core/testdata/format/website-samples/dropImport.md b/core/testdata/format/website-samples/dropImport.md index 13c8fb79..1e05678b 100644 --- a/core/testdata/format/website-samples/dropImport.md +++ b/core/testdata/format/website-samples/dropImport.md @@ -12,6 +12,7 @@ layout: api ``` kotlin import some.* + fun main(args: Array) { //sampleStart diff --git a/core/testdata/format/website-samples/newLinesInSamples.md b/core/testdata/format/website-samples/newLinesInSamples.md index 448725c3..5344b983 100644 --- a/core/testdata/format/website-samples/newLinesInSamples.md +++ b/core/testdata/format/website-samples/newLinesInSamples.md @@ -12,6 +12,7 @@ layout: api ``` kotlin + fun main(args: Array) { //sampleStart val words = listOf("a", "abc", "ab", "def", "abcd") diff --git a/core/testdata/format/website-samples/sample.md b/core/testdata/format/website-samples/sample.md index 203f1b02..b29075a7 100644 --- a/core/testdata/format/website-samples/sample.md +++ b/core/testdata/format/website-samples/sample.md @@ -18,6 +18,7 @@ applied to each element and returns a map where each group key is associated wit ``` kotlin + fun main(args: Array) { //sampleStart if (true) { diff --git a/core/testdata/format/website-samples/sampleWithAsserts.md b/core/testdata/format/website-samples/sampleWithAsserts.md index 98d7df33..c114468a 100644 --- a/core/testdata/format/website-samples/sampleWithAsserts.md +++ b/core/testdata/format/website-samples/sampleWithAsserts.md @@ -12,6 +12,7 @@ layout: api ``` kotlin + fun main(args: Array) { //sampleStart println(a()) // Hello, Work -- cgit