Skip to content

Commit

Permalink
Add manual creation of SampleAnalysisEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnatBeresnev authored and whyoleg committed Apr 26, 2024
1 parent 17ea994 commit 40f3c9c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
Expand Up @@ -5,6 +5,7 @@
package org.jetbrains.dokka.analysis.kotlin.sample

import org.jetbrains.dokka.DokkaConfiguration
import java.io.Closeable

/**
* Fully-configured and ready-to-use sample analysis environment.
Expand All @@ -17,7 +18,7 @@ import org.jetbrains.dokka.DokkaConfiguration
* in one iteration and at the same time, so that the environment is created once and lives for
* as little is possible, as opposed to creating it again and again for every individual sample.
*/
public interface SampleAnalysisEnvironment {
public interface SampleAnalysisEnvironment : Closeable {

/**
* Resolves a Kotlin sample function by its fully qualified name, and returns its import statements and body.
Expand Down
Expand Up @@ -35,4 +35,6 @@ public interface SampleAnalysisEnvironmentCreator {
* ```
*/
public fun <T> use(block: SampleAnalysisEnvironment.() -> T): T

public fun create(): SampleAnalysisEnvironment
}
Expand Up @@ -14,6 +14,64 @@ import kotlin.test.*

class SampleAnalysisTest {

@Test
fun `should resolve a valid sample if the environment is created manually`() {
val testProject = kotlinJvmTestProject {
dokkaConfiguration {
kotlinSourceSet {
samples = setOf("/samples/collections.kt")
}
}
sampleFile("/samples/collections.kt", fqPackageName = "org.jetbrains.dokka.sample.collections") {
+"""
import org.jetbrains.dokka.DokkaConfiguration
import org.jetbrains.dokka.DokkaGenerator
import org.jetbrains.dokka.utilities.DokkaLogger
fun specificPositionOperations() {
val numbers = mutableListOf(1, 2, 3, 4)
numbers.add(5)
numbers.removeAt(1)
numbers[0] = 0
numbers.shuffle()
if (numbers.size > 0) {
println(numbers)
}
}
"""
}
}

testProject.useServices { context ->
val sampleAnalysisEnvironment = sampleAnalysisEnvironmentCreator.create()
val sample = sampleAnalysisEnvironment.resolveSample(
sourceSet = context.singleSourceSet(),
fullyQualifiedLink = "org.jetbrains.dokka.sample.collections.specificPositionOperations"
)
assertNotNull(sample)

val expectedImports = listOf(
"org.jetbrains.dokka.DokkaConfiguration",
"org.jetbrains.dokka.DokkaGenerator",
"org.jetbrains.dokka.utilities.DokkaLogger"
)

val expectedBody = """
val numbers = mutableListOf(1, 2, 3, 4)
numbers.add(5)
numbers.removeAt(1)
numbers[0] = 0
numbers.shuffle()
if (numbers.size > 0) {
println(numbers)
}
""".trimIndent()

assertEquals(expectedImports, sample.imports)
assertEquals(expectedBody, sample.body)
}
}

@Test
fun `should resolve a valid sample if set via the samples option`() {
val testProject = kotlinJvmTestProject {
Expand Down
Expand Up @@ -32,6 +32,7 @@ import org.jetbrains.kotlin.resolve.lazy.ResolveSession
import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyPackageDescriptor
import org.jetbrains.kotlin.resolve.source.KotlinSourceElement
import org.jetbrains.kotlin.utils.addToStdlib.applyIf
import java.io.Closeable

internal class DescriptorSampleAnalysisEnvironmentCreator(
private val context: DokkaContext,
Expand Down Expand Up @@ -65,14 +66,28 @@ internal class DescriptorSampleAnalysisEnvironmentCreator(
}
}
}

override fun create(): SampleAnalysisEnvironment {
@OptIn(DokkaPluginApiPreview::class)
return DescriptorSampleAnalysisEnvironment(
kdocFinder = descriptorAnalysisPlugin.querySingle { kdocFinder },
kotlinAnalysis = SamplesKotlinAnalysis(
sourceSets = context.configuration.sourceSets,
context = context,
projectKotlinAnalysis = descriptorAnalysisPlugin.querySingle { kotlinAnalysis }
),
sampleRewriter = sampleRewriter,
dokkaLogger = context.logger
)
}
}

internal class DescriptorSampleAnalysisEnvironment(
private val kdocFinder: KDocFinder,
private val kotlinAnalysis: KotlinAnalysis,
private val sampleRewriter: SampleRewriter?,
private val dokkaLogger: DokkaLogger,
) : SampleAnalysisEnvironment {
) : SampleAnalysisEnvironment, Closeable {

override fun resolveSample(
sourceSet: DokkaConfiguration.DokkaSourceSet,
Expand Down Expand Up @@ -221,6 +236,10 @@ internal class DescriptorSampleAnalysisEnvironment(
}
return textBuilder.toString()
}

override fun close() {
kotlinAnalysis.close()
}
}

private class SampleBuilder(
Expand Down
Expand Up @@ -61,6 +61,18 @@ internal class SymbolSampleAnalysisEnvironmentCreator(
}
}
}

override fun create(): SampleAnalysisEnvironment {
return SymbolSampleAnalysisEnvironment(
samplesKotlinAnalysis = SamplesKotlinAnalysis(
sourceSets = context.configuration.sourceSets,
context = context
),
projectKotlinAnalysis = projectKotlinAnalysis,
sampleRewriter = sampleRewriter,
dokkaLogger = context.logger
)
}
}

private class SymbolSampleAnalysisEnvironment(
Expand Down Expand Up @@ -174,6 +186,11 @@ private class SymbolSampleAnalysisEnvironment(
}
return textBuilder.toString()
}

override fun close() {
samplesKotlinAnalysis.close()
projectKotlinAnalysis.close()
}
}

private class SampleBuilder(
Expand Down

0 comments on commit 40f3c9c

Please sign in to comment.