Skip to content

Commit

Permalink
Use analysisContext.modulesWithFiles in symbols to find if file is …
Browse files Browse the repository at this point in the history
…from currently analyzed module
  • Loading branch information
whyoleg committed Dec 4, 2023
1 parent 4814627 commit 4553f98
Showing 1 changed file with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.hasActualModifier
import org.jetbrains.kotlin.psi.psiUtil.hasExpectModifier
import java.nio.file.Paths

internal class DefaultSymbolToDocumentableTranslator(context: DokkaContext) : AsyncSourceToDocumentableTranslator {
private val kotlinAnalysis = context.plugin<SymbolsAnalysisPlugin>().querySingle { kotlinAnalysis }
Expand Down Expand Up @@ -111,51 +110,46 @@ internal class DokkaSymbolVisitor(
private val KtDeclarationSymbol.isExpect
get() = (psi as? KtModifierListOwner)?.hasExpectModifier() == true

private fun <T : KtSymbol> Collection<T>.filterSymbolsInSourceSet() = filter {
val pathString = it.psi?.containingFile?.virtualFile?.path
when {
pathString.isNullOrBlank() -> false
else -> {
val absolutePath = Paths.get(pathString).toRealPath()
sourceSet.sourceRoots.any { root ->
absolutePath.startsWith(root.toPath().toRealPath())
}
}
private fun <T : KtSymbol> List<T>.filterSymbolsInSourceSet(moduleFiles: Set<KtFile>): List<T> = filter {
when (val file = it.psi?.containingFile) {
is KtFile -> moduleFiles.contains(file)
else -> false
}
}

fun visitModule(): DModule {
val sourceModule = analysisContext.getModule(sourceSet)
val ktFiles = analysisContext.modulesWithFiles[sourceModule]?.filterIsInstance<KtFile>() ?: throw IllegalStateException("No source files for a source module ${sourceModule.moduleName} of source set ${sourceSet.sourceSetID}")
val ktFiles = analysisContext.modulesWithFiles[sourceModule]?.filterIsInstance<KtFile>()?.toSet()
?: throw IllegalStateException("No source files for a source module ${sourceModule.moduleName} of source set ${sourceSet.sourceSetID}")
val processedPackages: MutableSet<FqName> = mutableSetOf()
return analyze(sourceModule) {
val packageSymbols: List<DPackage> = ktFiles
.mapNotNull {
if (processedPackages.contains(it.packageFqName))
return@mapNotNull null
processedPackages.add(it.packageFqName)
getPackageSymbolIfPackageExists(it.packageFqName)?.let { it1 ->
visitPackageSymbol(
it1
)
}
val packages = ktFiles.mapNotNull { file ->
if (processedPackages.contains(file.packageFqName))
return@mapNotNull null
processedPackages.add(file.packageFqName)
getPackageSymbolIfPackageExists(file.packageFqName)?.let { packageSymbol ->
visitPackageSymbol(packageSymbol, ktFiles)
}
}

DModule(
name = moduleName,
packages = packageSymbols,
packages = packages,
documentation = emptyMap(),
expectPresentInSet = null,
sourceSets = setOf(sourceSet)
)
}
}

private fun KtAnalysisSession.visitPackageSymbol(packageSymbol: KtPackageSymbol): DPackage {
private fun KtAnalysisSession.visitPackageSymbol(
packageSymbol: KtPackageSymbol,
moduleFiles: Set<KtFile>
): DPackage {
val dri = getDRIFromPackage(packageSymbol)
val scope = packageSymbol.getPackageScope()
val callables = scope.getCallableSymbols().toList().filterSymbolsInSourceSet()
val classifiers = scope.getClassifierSymbols().toList().filterSymbolsInSourceSet()
val callables = scope.getCallableSymbols().toList().filterSymbolsInSourceSet(moduleFiles)
val classifiers = scope.getClassifierSymbols().toList().filterSymbolsInSourceSet(moduleFiles)

val functions = callables.filterIsInstance<KtFunctionSymbol>().map { visitFunctionSymbol(it, dri) }
val properties = callables.filterIsInstance<KtPropertySymbol>().map { visitPropertySymbol(it, dri) }
Expand Down

0 comments on commit 4553f98

Please sign in to comment.