-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
298 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
plugins/base/src/main/kotlin/transformers/documentables/utils/ClassGraphBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.jetbrains.dokka.base.transformers.documentables.utils | ||
|
||
import kotlinx.coroutines.* | ||
import org.jetbrains.dokka.DokkaConfiguration | ||
import org.jetbrains.dokka.analysis.DescriptorDocumentableSource | ||
import org.jetbrains.dokka.analysis.from | ||
import org.jetbrains.dokka.links.DRI | ||
import org.jetbrains.dokka.model.* | ||
import org.jetbrains.dokka.utilities.parallelForEach | ||
import org.jetbrains.kotlin.descriptors.ClassDescriptor | ||
import org.jetbrains.kotlin.types.KotlinType | ||
import org.jetbrains.kotlin.types.typeUtil.immediateSupertypes | ||
import org.jetbrains.kotlin.types.typeUtil.isAnyOrNullableAny | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
|
||
/** | ||
* The class allows lo build a full class hierarchy via descriptors | ||
*/ | ||
class ClassGraphBuilder { | ||
suspend operator fun invoke(original: DModule,): SourceSetDependent<Map<DRI, List<DRI>>> = coroutineScope{ | ||
val map = original.sourceSets.associateWith { ConcurrentHashMap<DRI, List<DRI>>() } | ||
original.packages.parallelForEach{ visitDocumentable(it, map) } | ||
map | ||
} | ||
|
||
private suspend fun collectSupertypesFromKotlinType( | ||
DRIWithKType: Pair<DRI, KotlinType>, | ||
sourceSet: DokkaConfiguration.DokkaSourceSet, | ||
supersMap: SourceSetDependent<MutableMap<DRI, List<DRI>>> | ||
): Unit = coroutineScope { | ||
val supertypes = DRIWithKType.second.immediateSupertypes().filterNot { it.isAnyOrNullableAny() } | ||
val supertypesDRIWithKType = supertypes.mapNotNull { supertype -> | ||
supertype.constructor.declarationDescriptor?.let { | ||
DRI.from(it) to supertype | ||
} | ||
} | ||
|
||
supersMap[sourceSet]?.let { | ||
if (it[DRIWithKType.first] == null) { | ||
// another thread can rewrite the same value, but it isn't a problem | ||
it[DRIWithKType.first] = supertypesDRIWithKType.map { it.first } | ||
supertypesDRIWithKType.parallelForEach { collectSupertypesFromKotlinType(it, sourceSet, supersMap) } | ||
} | ||
} | ||
} | ||
|
||
private suspend fun visitDocumentable( | ||
documentable: Documentable, | ||
supersMap: SourceSetDependent<MutableMap<DRI, List<DRI>>> | ||
): Unit = coroutineScope { | ||
if (documentable is WithScope) { | ||
documentable.classlikes. | ||
parallelForEach{ visitDocumentable(it, supersMap) } | ||
} | ||
if(documentable is DClasslike) { | ||
documentable.sources.forEach { (sourceSet, source) -> | ||
if (source is DescriptorDocumentableSource) { | ||
val descriptor = source.descriptor as ClassDescriptor | ||
val type = descriptor.defaultType | ||
collectSupertypesFromKotlinType( documentable.dri to type, sourceSet, supersMap) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.