Skip to content

Commit

Permalink
Reorganize project model for MPP
Browse files Browse the repository at this point in the history
  • Loading branch information
vmishenev committed Oct 18, 2023
1 parent f225faf commit e71be59
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 175 deletions.
11 changes: 9 additions & 2 deletions plugins/base/src/test/kotlin/model/InheritorsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,22 @@ class InheritorsTest : AbstractModelTest("/src/main/kotlin/inheritors/Test.kt",
fun multiplatform() {
val configuration = dokkaConfiguration {
sourceSets {
val commonSourceSet = sourceSet {
name = "common"
sourceRoots = listOf("common/src/")
analysisPlatform = "common"
}
sourceSet {
name = "jvm"
sourceRoots = listOf("common/src/", "jvm/src/")
sourceRoots = listOf("jvm/src/")
analysisPlatform = "jvm"
dependentSourceSets = setOf(commonSourceSet.value.sourceSetID)
}
sourceSet {
name = "js"
sourceRoots = listOf("common/src/", "js/src/")
sourceRoots = listOf("js/src/")
analysisPlatform = "js"
dependentSourceSets = setOf(commonSourceSet.value.sourceSetID)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion plugins/base/src/test/kotlin/signatures/SignatureTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ class SignatureTest : BaseAbstractTest() {
}
}
}
@OnlyDescriptorsMPP
@Test
fun `actual typealias should have generic parameters and fully qualified name of the expansion type`() {
val writerPlugin = TestOutputWriterPlugin()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal class KotlinDocCommentParser(
}
val kotlinAnalysis = context.plugin<SymbolsAnalysisPlugin>().querySingle { kotlinAnalysis }
val elementName = element.resolveDocContext.ktElement.name
return analyze(kotlinAnalysis[sourceSet].mainModule) {
return analyze(kotlinAnalysis.getModule(sourceSet)) {
parseFromKDocTag(
kDocTag = element.comment,
externalDri = { link -> resolveKDocLink(link).ifUnresolved { context.logger.logUnresolvedLink(link.getLinkText(), elementName) } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ internal fun ModuleAndPackageDocumentationParsingContext(
if (kotlinAnalysis == null || sourceSet == null) {
MarkdownParser(externalDri = { null }, sourceLocation)
} else {
val analysisContext = kotlinAnalysis[sourceSet]
val contextPsi = analyze(analysisContext.mainModule) {
val sourceModule = kotlinAnalysis.getModule(sourceSet)
val contextPsi = analyze(sourceModule) {
val contextSymbol = when (fragment.classifier) {
Module -> ROOT_PACKAGE_SYMBOL
Package -> getPackageSymbolIfPackageExists(FqName(fragment.name))
Expand All @@ -46,7 +46,7 @@ internal fun ModuleAndPackageDocumentationParsingContext(
}
MarkdownParser(
externalDri = { link ->
analyze(analysisContext.mainModule) {
analyze(sourceModule) {
resolveKDocTextLink(
link,
contextPsi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private class ContextModuleAndPackageDocumentationReader(
): SourceSetDependent<DocumentationNode> {
return sourceSets.associateWithNotNull { sourceSet ->
val fragments = documentationFragments[sourceSet].orEmpty().filter(predicate)
kotlinAnalysis[sourceSet] // test: to throw exception for unknown sourceSet
kotlinAnalysis.getModule(sourceSet)// test: to throw exception for unknown sourceSet
val documentations = fragments.map { fragment ->
parseModuleAndPackageDocumentation(
context = ModuleAndPackageDocumentationParsingContext(context.logger, kotlinAnalysis, sourceSet),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,140 +5,46 @@
package org.jetbrains.dokka.analysis.kotlin.symbols.plugin

import com.intellij.openapi.Disposable
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import org.jetbrains.dokka.DokkaConfiguration
import org.jetbrains.dokka.DokkaSourceSetID
import org.jetbrains.dokka.model.SourceSetDependent
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.kotlin.analysis.api.standalone.StandaloneAnalysisAPISession
import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule
import java.io.Closeable
import java.io.File

@Suppress("FunctionName", "UNUSED_PARAMETER")
internal fun SamplesKotlinAnalysis(
sourceSets: List<DokkaConfiguration.DokkaSourceSet>,
context: DokkaContext,
projectKotlinAnalysis: KotlinAnalysis
): KotlinAnalysis {
val environments = sourceSets
.filter { it.samples.isNotEmpty() }
.associateWith { sourceSet ->
createAnalysisContext(
classpath = sourceSet.classpath,
sourceRoots = sourceSet.samples,
sourceSet = sourceSet
)
}

return EnvironmentKotlinAnalysis(environments, projectKotlinAnalysis)
}
): KotlinAnalysis = createAnalysisSession(
sourceSets = sourceSets,
logger = context.logger,
isSampleProject = true
)

internal fun ProjectKotlinAnalysis(
sourceSets: List<DokkaConfiguration.DokkaSourceSet>,
context: DokkaContext,
): KotlinAnalysis {
val environments = sourceSets.associateWith { sourceSet ->
createAnalysisContext(
context = context,
sourceSets = sourceSets,
sourceSet = sourceSet
)
}
return EnvironmentKotlinAnalysis(environments)
}


@Suppress("UNUSED_PARAMETER")
internal fun createAnalysisContext(
context: DokkaContext,
sourceSets: List<DokkaConfiguration.DokkaSourceSet>,
sourceSet: DokkaConfiguration.DokkaSourceSet
): AnalysisContext {
val parentSourceSets = sourceSets.filter { it.sourceSetID in sourceSet.dependentSourceSets }
val classpath = sourceSet.classpath + parentSourceSets.flatMap { it.classpath }
val sources = sourceSet.sourceRoots + parentSourceSets.flatMap { it.sourceRoots }

return createAnalysisContext(classpath, sources, sourceSet)
}

internal fun createAnalysisContext(
classpath: List<File>,
sourceRoots: Set<File>,
sourceSet: DokkaConfiguration.DokkaSourceSet
): AnalysisContext {
val applicationDisposable: Disposable = Disposer.newDisposable("StandaloneAnalysisAPISession.application")
val projectDisposable: Disposable = Disposer.newDisposable("StandaloneAnalysisAPISession.project")

val analysis= createAnalysisSession(
classpath = classpath,
sourceRoots = sourceRoots,
analysisPlatform = sourceSet.analysisPlatform,
languageVersion = sourceSet.languageVersion,
apiVersion = sourceSet.apiVersion,
applicationDisposable = applicationDisposable,
projectDisposable = projectDisposable
)
return AnalysisContextImpl(
mainModule = analysis.second,
analysisSession = analysis.first,
applicationDisposable = applicationDisposable,
projectDisposable = projectDisposable
)
}


/**
* First child delegation. It does not close [parent].
*/
internal abstract class KotlinAnalysis(
private val parent: KotlinAnalysis? = null
): KotlinAnalysis = createAnalysisSession(
sourceSets = sourceSets,
logger = context.logger
)

internal class KotlinAnalysis(
private val sourceModules: SourceSetDependent<KtSourceModule>,
private val analysisSession: StandaloneAnalysisAPISession,
private val applicationDisposable: Disposable,
private val projectDisposable: Disposable
) : Closeable {

operator fun get(key: DokkaConfiguration.DokkaSourceSet): AnalysisContext {
return get(key.sourceSetID)
}

internal operator fun get(key: DokkaSourceSetID): AnalysisContext {
return find(key)
?: parent?.get(key)
?: throw IllegalStateException("Missing EnvironmentAndFacade for sourceSet $key")
}

internal abstract fun find(sourceSetID: DokkaSourceSetID): AnalysisContext?
}

internal open class EnvironmentKotlinAnalysis(
private val environments: SourceSetDependent<AnalysisContext>,
parent: KotlinAnalysis? = null,
) : KotlinAnalysis(parent = parent) {

override fun find(sourceSetID: DokkaSourceSetID): AnalysisContext? =
environments.entries.firstOrNull { (sourceSet, _) -> sourceSet.sourceSetID == sourceSetID }?.value
fun getModule(sourceSet: DokkaConfiguration.DokkaSourceSet) =
sourceModules[sourceSet] ?: error("Missing a source module for sourceSet ${sourceSet.displayName} with id ${sourceSet.sourceSetID}")

override fun close() {
environments.values.forEach(AnalysisContext::close)
}
}

internal interface AnalysisContext: Closeable {
val project: Project
val mainModule: KtSourceModule
val analysisSession: StandaloneAnalysisAPISession
}

private class AnalysisContextImpl(
override val mainModule: KtSourceModule,
override val analysisSession: StandaloneAnalysisAPISession,
private val applicationDisposable: Disposable,
private val projectDisposable: Disposable
) : AnalysisContext {
override val project: Project
get() = analysisSession.project
val modulesWithFiles
get() = analysisSession.modulesWithFiles

override fun close() {
Disposer.dispose(applicationDisposable)
Disposer.dispose(projectDisposable)
}
}
}
Loading

0 comments on commit e71be59

Please sign in to comment.