diff options
Diffstat (limited to 'core')
4 files changed, 57 insertions, 3 deletions
diff --git a/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt b/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt index 648a6b67..baf44904 100644 --- a/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt +++ b/core/src/main/kotlin/Samples/KotlinWebsiteSampleProcessingService.kt @@ -2,10 +2,9 @@ package org.jetbrains.dokka.Samples import com.google.inject.Inject import com.intellij.psi.PsiElement -import org.jetbrains.dokka.DocumentationOptions -import org.jetbrains.dokka.DokkaLogger -import org.jetbrains.dokka.DokkaResolutionFacade +import org.jetbrains.dokka.* import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.resolve.ImportPath open class KotlinWebsiteSampleProcessingService @Inject constructor(options: DocumentationOptions, @@ -65,6 +64,22 @@ open class KotlinWebsiteSampleProcessingService return sampleBuilder.text } + val importsToIgnore = arrayOf("samples.*").map(::ImportPath) + + override fun processImports(psiElement: PsiElement): ContentBlockCode { + val psiFile = psiElement.containingFile + if (psiFile is KtFile) { + return ContentBlockCode("kotlin").apply { + psiFile.importList?.let { + it.children.filter { + it !is KtImportDirective || it.importPath !in importsToIgnore + }.forEach { append(ContentText(it.text)) } + } + } + } + return super.processImports(psiElement) + } + override fun processSampleBody(psiElement: PsiElement) = when (psiElement) { is KtDeclarationWithBody -> { val bodyExpression = psiElement.bodyExpression diff --git a/core/src/test/kotlin/format/KotlinWebSiteRunnableSamplesFormatTest.kt b/core/src/test/kotlin/format/KotlinWebSiteRunnableSamplesFormatTest.kt index 1f514932..742d2908 100644 --- a/core/src/test/kotlin/format/KotlinWebSiteRunnableSamplesFormatTest.kt +++ b/core/src/test/kotlin/format/KotlinWebSiteRunnableSamplesFormatTest.kt @@ -7,6 +7,11 @@ import org.junit.Test class KotlinWebSiteRunnableSamplesFormatTest { private val kwsService = KotlinWebsiteRunnableSamplesFormatService(InMemoryLocationService, KotlinLanguageService()) + + @Test fun dropImport() { + verifyKWSNodeByName("dropImport", "foo") + } + @Test fun sample() { verifyKWSNodeByName("sample", "foo") } diff --git a/core/testdata/format/website-samples/dropImport.kt b/core/testdata/format/website-samples/dropImport.kt new file mode 100644 index 00000000..7b8d9f4e --- /dev/null +++ b/core/testdata/format/website-samples/dropImport.kt @@ -0,0 +1,12 @@ +import samples.* +import some.* + +/** + * @sample example1 + */ +fun foo() { +} + +fun example1() { + +}
\ No newline at end of file diff --git a/core/testdata/format/website-samples/dropImport.md b/core/testdata/format/website-samples/dropImport.md new file mode 100644 index 00000000..13c8fb79 --- /dev/null +++ b/core/testdata/format/website-samples/dropImport.md @@ -0,0 +1,22 @@ +--- +title: foo - test +layout: api +--- + +<div class='api-docs-breadcrumbs'><a href="test/index">test</a> / <a href="test/foo">foo</a></div> + +# foo + +<div class="signature"><code><span class="keyword">fun </span><span class="identifier">foo</span><span class="symbol">(</span><span class="symbol">)</span><span class="symbol">: </span><span class="identifier">Unit</span></code></div> +<div class="sample" markdown="1"> + +``` kotlin +import some.* +fun main(args: Array<String>) { +//sampleStart + +//sampleEnd +} +``` + +</div> |