From e99be615ce7c2c2b5c3ee5e3f8941c41c1e7a944 Mon Sep 17 00:00:00 2001 From: Kamil Doległo Date: Fri, 31 Jan 2020 00:37:29 +0100 Subject: Bump Gradle version, migrate to Kotlin DSL, refactor publishing --- testApi/build.gradle.kts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 testApi/build.gradle.kts (limited to 'testApi/build.gradle.kts') diff --git a/testApi/build.gradle.kts b/testApi/build.gradle.kts new file mode 100644 index 00000000..db861b90 --- /dev/null +++ b/testApi/build.gradle.kts @@ -0,0 +1,29 @@ +import org.jetbrains.configureBintrayPublication + +plugins { + `maven-publish` + id("com.jfrog.bintray") +} + +dependencies { + implementation(project(":core")) + implementation("junit:junit:4.13") // TODO: remove dependency to junit + implementation(kotlin("stdlib")) +} + +val sourceJar by tasks.registering(Jar::class) { + archiveClassifier.set("sources") + from(sourceSets["main"].allSource) +} + +publishing { + publications { + register("dokkaTestAPI") { + artifactId = "dokka-test-api" + components["java"] + artifact(sourceJar.get()) + } + } +} + +configureBintrayPublication("dokkaTestAPI") \ No newline at end of file -- cgit From e730b82c4db6306a5b9e552d8a4c6578b0c1e01a Mon Sep 17 00:00:00 2001 From: Kamil Doległo Date: Thu, 13 Feb 2020 12:52:39 +0100 Subject: Update packaging structure, fix testAPI publication, simplify dependencies handling in plugins Fixes #627 This commit removes repackaging `dokka-core` with all its dependencies by creating a publication for those dependencies. Moreover it moves `kotlinx-markdown` dependency to `coreDependencies` as this library is only present in Kotlin Bintray repository. TestAPI now publishes correctly --- core/build.gradle.kts | 18 ++++-------------- coreDependencies/build.gradle.kts | 17 +++++++++++++++++ plugins/build.gradle.kts | 4 ++++ plugins/kotlin-as-java/build.gradle.kts | 9 --------- testApi/build.gradle.kts | 4 ++-- 5 files changed, 27 insertions(+), 25 deletions(-) (limited to 'testApi/build.gradle.kts') diff --git a/core/build.gradle.kts b/core/build.gradle.kts index e70791f3..c8cae2d6 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -7,30 +7,20 @@ plugins { } dependencies { - implementation(project(":coreDependencies", configuration = "shadow")) + api(project(":coreDependencies", configuration = "shadow")) val kotlin_version: String by project - implementation("org.jetbrains.kotlin:kotlin-compiler:$kotlin_version") - implementation("org.jetbrains.kotlin:kotlin-script-runtime:$kotlin_version") + api("org.jetbrains.kotlin:kotlin-compiler:$kotlin_version") + implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.6.10") implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version") implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlin_version") implementation("org.jsoup:jsoup:1.12.1") implementation("com.google.code.gson:gson:2.8.5") - implementation("org.jetbrains:markdown:0.1.41") - implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.6.10") testImplementation(project(":testApi")) testImplementation("junit:junit:4.13") } -tasks { - shadowJar { - val dokka_version: String by project - archiveFileName.set("dokka-core-$dokka_version.jar") - archiveClassifier.set("") - } -} - val sourceJar by tasks.registering(Jar::class) { archiveClassifier.set("sources") from(sourceSets["main"].allSource) @@ -40,7 +30,7 @@ publishing { publications { register("dokkaCore") { artifactId = "dokka-core" - project.shadow.component(this) + from(components["java"]) artifact(sourceJar.get()) } } diff --git a/coreDependencies/build.gradle.kts b/coreDependencies/build.gradle.kts index 514c7ae1..73b04554 100644 --- a/coreDependencies/build.gradle.kts +++ b/coreDependencies/build.gradle.kts @@ -1,6 +1,9 @@ +import org.jetbrains.configureBintrayPublication + plugins { id("com.github.johnrengelman.shadow") `maven-publish` + id("com.jfrog.bintray") } val intellijCore: Configuration by configurations.creating @@ -24,6 +27,9 @@ dependencies { //TODO: parametrize ij version after 1.3.70 isTransitive = false } + implementation("org.jetbrains:markdown:0.1.41") { + because("it's published only on bintray") + } } tasks { @@ -41,3 +47,14 @@ tasks { exclude("src/**") } } + +publishing { + publications { + register("dokkaCoreDependencies") { + artifactId = "dokka-core-dependencies" + project.shadow.component(this) + } + } +} + +configureBintrayPublication("dokkaCoreDependencies") \ No newline at end of file diff --git a/plugins/build.gradle.kts b/plugins/build.gradle.kts index 5f2f6dcb..e29f1355 100644 --- a/plugins/build.gradle.kts +++ b/plugins/build.gradle.kts @@ -6,6 +6,10 @@ subprojects { dependencies { compileOnly(project(":core")) compileOnly(kotlin("stdlib-jdk8")) +// compileOnly(project(":coreDependencies", configuration = "shadow")) // uncomment if IntelliJ does not recognize pacakges from IntelliJ + testImplementation(project(":testApi")) + testImplementation(kotlin("stdlib-jdk8")) + testImplementation("junit:junit:4.13") } } \ No newline at end of file diff --git a/plugins/kotlin-as-java/build.gradle.kts b/plugins/kotlin-as-java/build.gradle.kts index 5d04060f..3b281d53 100644 --- a/plugins/kotlin-as-java/build.gradle.kts +++ b/plugins/kotlin-as-java/build.gradle.kts @@ -5,13 +5,4 @@ publishing { from(components["java"]) } } -} - -dependencies { - implementation(kotlin("stdlib-jdk8")) - compileOnly(project(":coreDependencies", configuration = "shadow")) - testImplementation(project(":core")) - testImplementation(project(":coreDependencies", configuration = "shadow")) - testImplementation(project(":testApi")) - testImplementation("junit:junit:4.13") } \ No newline at end of file diff --git a/testApi/build.gradle.kts b/testApi/build.gradle.kts index db861b90..43c984b5 100644 --- a/testApi/build.gradle.kts +++ b/testApi/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } dependencies { - implementation(project(":core")) + api(project(":core")) implementation("junit:junit:4.13") // TODO: remove dependency to junit implementation(kotlin("stdlib")) } @@ -20,7 +20,7 @@ publishing { publications { register("dokkaTestAPI") { artifactId = "dokka-test-api" - components["java"] + from(components["java"]) artifact(sourceJar.get()) } } -- cgit From 08f40e2a13006882e8f8425f111b8527e7bbcb0f Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Wed, 24 Jun 2020 09:14:37 +0200 Subject: Remove kotlin source analysis from :core to :kotlin-analysis (thanks to Afzal Najam) --- core/build.gradle.kts | 7 +- core/dependencies/build.gradle.kts | 40 ++ core/src/main/kotlin/DokkaGenerator.kt | 83 +-- .../main/kotlin/analysis/AnalysisEnvironment.kt | 625 -------------------- .../main/kotlin/analysis/CoreKotlinCacheService.kt | 42 -- .../main/kotlin/analysis/CoreProjectFileIndex.kt | 569 ------------------ .../main/kotlin/analysis/JavaResolveExtension.kt | 128 ----- core/src/main/kotlin/links/DRI.kt | 132 +---- core/src/main/kotlin/model/Documentable.kt | 12 +- .../kotlin/model/properties/PropertyContainer.kt | 4 +- core/src/main/kotlin/parsers/HtmlParser.kt | 89 --- core/src/main/kotlin/parsers/MarkdownParser.kt | 418 -------------- core/src/main/kotlin/parsers/Parser.kt | 42 -- .../factories/DocTagsFromIElementFactory.kt | 43 -- .../parsers/factories/DocTagsFromStringFactory.kt | 77 --- core/src/main/kotlin/plugability/DokkaContext.kt | 7 +- core/src/main/kotlin/plugability/DokkaPlugin.kt | 6 +- core/src/main/kotlin/utilities/cast.kt | 5 + coreDependencies/build.gradle.kts | 65 --- .../gradle-integration-tests/build.gradle.kts | 2 +- kotlin-analysis/build.gradle.kts | 27 + kotlin-analysis/dependencies/build.gradle.kts | 67 +++ .../dokka/analysis/AnalysisEnvironment.kt | 636 +++++++++++++++++++++ .../jetbrains/dokka/analysis/CallableFactory.kt | 31 + .../dokka/analysis/CoreKotlinCacheService.kt | 42 ++ .../dokka/analysis/CoreProjectFileIndex.kt | 515 +++++++++++++++++ .../org/jetbrains/dokka/analysis/DRIFactory.kt | 38 ++ .../jetbrains/dokka/analysis/DRITargetFactory.kt | 44 ++ .../org/jetbrains/dokka/analysis/Documentable.kt | 14 + .../dokka/analysis/EnvironmentAndFacade.kt | 59 ++ .../dokka/analysis/JavaResolveExtension.kt | 128 +++++ .../org/jetbrains/dokka/analysis/KotlinAnalysis.kt | 46 ++ .../dokka/analysis/TypeReferenceFactory.kt | 51 ++ plugins/base/build.gradle.kts | 1 + plugins/base/src/main/kotlin/DokkaBase.kt | 21 +- .../kotlin/allModulePage/MultimodulePageCreator.kt | 2 +- plugins/base/src/main/kotlin/parsers/HtmlParser.kt | 89 +++ .../base/src/main/kotlin/parsers/MarkdownParser.kt | 419 ++++++++++++++ plugins/base/src/main/kotlin/parsers/Parser.kt | 42 ++ .../factories/DocTagsFromIElementFactory.kt | 43 ++ .../parsers/factories/DocTagsFromStringFactory.kt | 77 +++ .../ModuleAndPackageDocumentationTransformer.kt | 20 +- .../documentables/ReportUndocumentedTransformer.kt | 1 + .../pages/samples/SamplesTransformer.kt | 41 +- .../pages/sourcelinks/SourceLinksTransformer.kt | 4 +- .../DefaultDescriptorToDocumentableTranslator.kt | 13 +- .../psi/DefaultPsiToDocumentableTranslator.kt | 23 +- .../main/kotlin/translators/psi/JavadocParser.kt | 1 + .../src/test/kotlin/basic/FailOnWarningTest.kt | 6 +- plugins/build.gradle.kts | 3 +- .../main/kotlin/javadoc/pages/JavadocPageNodes.kt | 3 + .../javadoc/src/test/kotlin/javadoc/JavadocTest.kt | 2 +- runners/cli/build.gradle.kts | 1 + runners/cli/src/main/kotlin/cli/main.kt | 2 +- .../main/kotlin/org/jetbrains/dokka/gradle/main.kt | 6 +- settings.gradle.kts | 6 +- test-tools/build.gradle.kts | 1 + testApi/build.gradle.kts | 2 + .../src/main/kotlin/testApi/context/MockContext.kt | 4 - .../testApi/testRunner/DokkaTestGenerator.kt | 11 +- .../main/kotlin/testApi/testRunner/TestRunner.kt | 3 - 61 files changed, 2545 insertions(+), 2396 deletions(-) create mode 100644 core/dependencies/build.gradle.kts delete mode 100644 core/src/main/kotlin/analysis/AnalysisEnvironment.kt delete mode 100644 core/src/main/kotlin/analysis/CoreKotlinCacheService.kt delete mode 100644 core/src/main/kotlin/analysis/CoreProjectFileIndex.kt delete mode 100644 core/src/main/kotlin/analysis/JavaResolveExtension.kt delete mode 100644 core/src/main/kotlin/parsers/HtmlParser.kt delete mode 100644 core/src/main/kotlin/parsers/MarkdownParser.kt delete mode 100644 core/src/main/kotlin/parsers/Parser.kt delete mode 100644 core/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt delete mode 100644 core/src/main/kotlin/parsers/factories/DocTagsFromStringFactory.kt create mode 100644 core/src/main/kotlin/utilities/cast.kt delete mode 100644 coreDependencies/build.gradle.kts create mode 100644 kotlin-analysis/build.gradle.kts create mode 100644 kotlin-analysis/dependencies/build.gradle.kts create mode 100644 kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/AnalysisEnvironment.kt create mode 100644 kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/CallableFactory.kt create mode 100644 kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/CoreKotlinCacheService.kt create mode 100644 kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/CoreProjectFileIndex.kt create mode 100644 kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRIFactory.kt create mode 100644 kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRITargetFactory.kt create mode 100644 kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/Documentable.kt create mode 100644 kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/EnvironmentAndFacade.kt create mode 100644 kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/JavaResolveExtension.kt create mode 100644 kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/KotlinAnalysis.kt create mode 100644 kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/TypeReferenceFactory.kt create mode 100644 plugins/base/src/main/kotlin/parsers/HtmlParser.kt create mode 100644 plugins/base/src/main/kotlin/parsers/MarkdownParser.kt create mode 100644 plugins/base/src/main/kotlin/parsers/Parser.kt create mode 100644 plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt create mode 100644 plugins/base/src/main/kotlin/parsers/factories/DocTagsFromStringFactory.kt (limited to 'testApi/build.gradle.kts') diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 1d61e410..fc62b73d 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -6,12 +6,9 @@ plugins { } dependencies { - api(project(":coreDependencies", configuration = "shadow")) + api(project("dependencies", configuration = "shadow")) - val kotlin_version: String by project - api("org.jetbrains.kotlin:kotlin-compiler:$kotlin_version") - implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version") - implementation("org.jetbrains.kotlin:kotlin-reflect:$kotlin_version") + implementation(kotlin("reflect")) implementation("com.google.code.gson:gson:2.8.5") implementation("org.jsoup:jsoup:1.12.1") diff --git a/core/dependencies/build.gradle.kts b/core/dependencies/build.gradle.kts new file mode 100644 index 00000000..a26d109e --- /dev/null +++ b/core/dependencies/build.gradle.kts @@ -0,0 +1,40 @@ +import org.jetbrains.configureBintrayPublication + +plugins { + id("com.github.johnrengelman.shadow") + `maven-publish` + id("com.jfrog.bintray") +} + +repositories { + maven(url = "https://kotlin.bintray.com/kotlin-plugin") +} + +dependencies { + // TODO (see https://github.com/Kotlin/dokka/issues/1009) + implementation(kotlin("stdlib")) + implementation(kotlin("reflect")) + + implementation("org.jetbrains:markdown:0.1.41") { + because("it's published only on bintray") + } +} + +tasks { + shadowJar { + val dokka_version: String by project + archiveFileName.set("dokka-dependencies-$dokka_version.jar") + archiveClassifier.set("") + } +} + +publishing { + publications { + register("dokkaCoreDependencies") { + artifactId = "dokka-core-dependencies" + project.shadow.component(this) + } + } +} + +configureBintrayPublication("dokkaCoreDependencies") diff --git a/core/src/main/kotlin/DokkaGenerator.kt b/core/src/main/kotlin/DokkaGenerator.kt index b2e572d4..b88c6223 100644 --- a/core/src/main/kotlin/DokkaGenerator.kt +++ b/core/src/main/kotlin/DokkaGenerator.kt @@ -1,7 +1,5 @@ package org.jetbrains.dokka -import org.jetbrains.dokka.analysis.AnalysisEnvironment -import org.jetbrains.dokka.analysis.DokkaResolutionFacade import org.jetbrains.dokka.model.DModule import org.jetbrains.dokka.model.SourceSetCache import org.jetbrains.dokka.model.SourceSetData @@ -9,13 +7,7 @@ import org.jetbrains.dokka.pages.RootPageNode import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.plugability.DokkaPlugin import org.jetbrains.dokka.utilities.DokkaLogger -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation -import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity -import org.jetbrains.kotlin.cli.common.messages.MessageCollector -import org.jetbrains.kotlin.cli.common.messages.MessageRenderer -import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.jetbrains.kotlin.utils.PathUtil -import java.io.File + /** * DokkaGenerator is the main entry point for generating documentation @@ -26,15 +18,13 @@ class DokkaGenerator( private val logger: DokkaLogger ) { fun generate() = timed { - report("Setting up analysis environments") val sourceSetsCache = SourceSetCache() - val sourceSets: Map = setUpAnalysis(configuration, sourceSetsCache) report("Initializing plugins") - val context = initializePlugins(configuration, logger, sourceSets, sourceSetsCache) + val context = initializePlugins(configuration, logger, sourceSetsCache) report("Creating documentation models") - val modulesFromPlatforms = createDocumentationModels(sourceSets, context) + val modulesFromPlatforms = createDocumentationModels(context, sourceSetsCache) report("Transforming documentation model before merging") val transformedDocumentationBeforeMerge = transformDocumentationModelBeforeMerge(modulesFromPlatforms, context) @@ -59,9 +49,8 @@ class DokkaGenerator( fun generateAllModulesPage() = timed { val sourceSetsCache = SourceSetCache() - val sourceSets = emptyMap() report("Initializing plugins") - val context = initializePlugins(configuration, logger, sourceSets, sourceSetsCache) + val context = initializePlugins(configuration, logger, sourceSetsCache) report("Creating all modules page") val pages = createAllModulePage(context) @@ -73,26 +62,20 @@ class DokkaGenerator( render(transformedPages, context) }.dump("\n\n === TIME MEASUREMENT ===\n") - fun setUpAnalysis( - configuration: DokkaConfiguration, - sourceSetsCache: SourceSetCache - ): Map = - configuration.passesConfigurations.map { - sourceSetsCache.getSourceSet(it) to createEnvironmentAndFacade(configuration, it) - }.toMap() fun initializePlugins( configuration: DokkaConfiguration, logger: DokkaLogger, - sourceSets: Map, sourceSetsCache: SourceSetCache, pluginOverrides: List = emptyList() - ) = DokkaContext.create(configuration, logger, sourceSets, sourceSetsCache, pluginOverrides) + ) = DokkaContext.create(configuration, logger, sourceSetsCache, pluginOverrides) fun createDocumentationModels( - platforms: Map, - context: DokkaContext - ) = platforms.flatMap { (pdata, _) -> translateSources(pdata, context) } + context: DokkaContext, + sourceSetsCache: SourceSetCache + ) = context.configuration.passesConfigurations + .map { passConfiguration -> sourceSetsCache.getSourceSet(passConfiguration) } + .flatMap { passConfiguration -> translateSources(passConfiguration, context) } fun transformDocumentationModelBeforeMerge( modulesFromPlatforms: List, @@ -150,56 +133,10 @@ class DokkaGenerator( } } - private fun createEnvironmentAndFacade( - configuration: DokkaConfiguration, - pass: DokkaConfiguration.PassConfiguration - ): EnvironmentAndFacade = - AnalysisEnvironment(DokkaMessageCollector(logger), pass.analysisPlatform).run { - if (analysisPlatform == Platform.jvm) { - addClasspath(PathUtil.getJdkClassesRootsFromCurrentJre()) - } - pass.classpath.forEach { addClasspath(File(it)) } - - addSources( - (pass.sourceRoots + configuration.passesConfigurations.filter { it.sourceSetID in pass.dependentSourceSets } - .flatMap { it.sourceRoots }) - .map { it.path } - ) - - loadLanguageVersionSettings(pass.languageVersion, pass.apiVersion) - - val environment = createCoreEnvironment() - val (facade, _) = createResolutionFacade(environment) - EnvironmentAndFacade(environment, facade) - } - private fun translateSources(platformData: SourceSetData, context: DokkaContext) = context[CoreExtensions.sourceToDocumentableTranslator].map { it.invoke(platformData, context) } - - class DokkaMessageCollector(private val logger: DokkaLogger) : MessageCollector { - override fun clear() { - seenErrors = false - } - - private var seenErrors = false - - override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation?) { - if (severity == CompilerMessageSeverity.ERROR) { - seenErrors = true - } - logger.info(MessageRenderer.PLAIN_FULL_PATHS.render(severity, message, location)) - } - - override fun hasErrors() = seenErrors - } -} - -// It is not data class due to ill-defined equals -class EnvironmentAndFacade(val environment: KotlinCoreEnvironment, val facade: DokkaResolutionFacade) { - operator fun component1() = environment - operator fun component2() = facade } private class Timer(startTime: Long, private val logger: DokkaLogger?) { diff --git a/core/src/main/kotlin/analysis/AnalysisEnvironment.kt b/core/src/main/kotlin/analysis/AnalysisEnvironment.kt deleted file mode 100644 index 33451dd6..00000000 --- a/core/src/main/kotlin/analysis/AnalysisEnvironment.kt +++ /dev/null @@ -1,625 +0,0 @@ -package org.jetbrains.dokka.analysis - -import com.google.common.collect.ImmutableMap -import com.intellij.core.CoreApplicationEnvironment -import com.intellij.core.CoreModuleManager -import com.intellij.mock.MockComponentManager -import com.intellij.openapi.Disposable -import com.intellij.openapi.extensions.Extensions -import com.intellij.openapi.module.Module -import com.intellij.openapi.module.ModuleManager -import com.intellij.openapi.project.Project -import com.intellij.openapi.roots.OrderEnumerationHandler -import com.intellij.openapi.roots.ProjectFileIndex -import com.intellij.openapi.roots.ProjectRootManager -import com.intellij.openapi.util.Disposer -import com.intellij.openapi.vfs.StandardFileSystems -import com.intellij.psi.PsiElement -import com.intellij.psi.search.GlobalSearchScope -import org.jetbrains.dokka.Platform -import org.jetbrains.kotlin.analyzer.* -import org.jetbrains.kotlin.analyzer.common.CommonAnalysisParameters -import org.jetbrains.kotlin.analyzer.common.CommonPlatformAnalyzerServices -import org.jetbrains.kotlin.analyzer.common.CommonResolverForModuleFactory -import org.jetbrains.kotlin.builtins.DefaultBuiltIns -import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.builtins.jvm.JvmBuiltIns -import org.jetbrains.kotlin.caches.resolve.* -import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys -import org.jetbrains.kotlin.cli.common.config.ContentRoot -import org.jetbrains.kotlin.cli.common.config.KotlinSourceRoot -import org.jetbrains.kotlin.cli.common.config.addKotlinSourceRoot -import org.jetbrains.kotlin.cli.common.messages.MessageCollector -import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles -import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider -import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment -import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM -import org.jetbrains.kotlin.cli.jvm.config.* -import org.jetbrains.kotlin.cli.jvm.index.JavaRoot -import org.jetbrains.kotlin.config.* -import org.jetbrains.kotlin.container.getService -import org.jetbrains.kotlin.container.tryGetService -import org.jetbrains.kotlin.context.ProjectContext -import org.jetbrains.kotlin.context.withModule -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor -import org.jetbrains.kotlin.descriptors.ModuleDescriptor -import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl -import org.jetbrains.kotlin.ide.konan.analyzer.NativeResolverForModuleFactory -import org.jetbrains.kotlin.js.config.JSConfigurationKeys -import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices -import org.jetbrains.kotlin.library.impl.createKotlinLibrary -import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl -import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.platform.CommonPlatforms -import org.jetbrains.kotlin.platform.TargetPlatform -import org.jetbrains.kotlin.platform.js.JsPlatforms -import org.jetbrains.kotlin.platform.jvm.JvmPlatforms -import org.jetbrains.kotlin.platform.jvm.JvmPlatforms.unspecifiedJvmPlatform -import org.jetbrains.kotlin.psi.* -import org.jetbrains.kotlin.resolve.BindingContext -import org.jetbrains.kotlin.resolve.BindingTrace -import org.jetbrains.kotlin.resolve.CompilerEnvironment -import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices -import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics -import org.jetbrains.kotlin.resolve.jvm.JvmPlatformParameters -import org.jetbrains.kotlin.resolve.jvm.JvmResolverForModuleFactory -import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformAnalyzerServices -import org.jetbrains.kotlin.resolve.konan.platform.NativePlatformAnalyzerServices -import org.jetbrains.kotlin.resolve.lazy.ResolveSession -import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.util.slicedMap.ReadOnlySlice -import org.jetbrains.kotlin.util.slicedMap.WritableSlice -import org.jetbrains.kotlin.idea.resolve.ResolutionFacade -import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode -import org.jetbrains.kotlin.descriptors.konan.KlibModuleOrigin -import org.jetbrains.kotlin.extensions.ApplicationExtensionDescriptor -import org.jetbrains.kotlin.ide.konan.NativePlatformKindResolution -import org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaClass -import org.jetbrains.kotlin.platform.IdePlatformKind -import org.jetbrains.kotlin.platform.impl.CommonIdePlatformKind -import org.jetbrains.kotlin.platform.impl.JsIdePlatformKind -import org.jetbrains.kotlin.platform.impl.JvmIdePlatformKind -import org.jetbrains.kotlin.platform.impl.NativeIdePlatformKind -import org.jetbrains.kotlin.platform.konan.NativePlatforms -import java.io.File - -const val JAR_SEPARATOR = "!/" -const val KLIB_EXTENSION = "klib" - -/** - * Kotlin as a service entry point - * - * Configures environment, analyses files and provides facilities to perform code processing without emitting bytecode - * - * $messageCollector: required by compiler infrastructure and will receive all compiler messages - * $body: optional and can be used to configure environment without creating local variable - */ -class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPlatform: Platform) : Disposable { - val configuration = CompilerConfiguration() - - init { - configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector) - } - - fun createCoreEnvironment(): KotlinCoreEnvironment { - System.setProperty("idea.io.use.nio2", "true") - - val configFiles = when (analysisPlatform) { - Platform.jvm, Platform.common -> EnvironmentConfigFiles.JVM_CONFIG_FILES - Platform.native -> EnvironmentConfigFiles.NATIVE_CONFIG_FILES - Platform.js -> EnvironmentConfigFiles.JS_CONFIG_FILES - } - - val environment = KotlinCoreEnvironment.createForProduction(this, configuration, configFiles) - val projectComponentManager = environment.project as MockComponentManager - - val projectFileIndex = CoreProjectFileIndex( - environment.project, - environment.configuration.getList(CLIConfigurationKeys.CONTENT_ROOTS) - ) - - val moduleManager = object : CoreModuleManager(environment.project, this) { - override fun getModules(): Array = arrayOf(projectFileIndex.module) - } - - CoreApplicationEnvironment.registerComponentInstance( - projectComponentManager.picoContainer, - ModuleManager::class.java, moduleManager - ) - - CoreApplicationEnvironment.registerExtensionPoint( - Extensions.getRootArea(), - OrderEnumerationHandler.EP_NAME, OrderEnumerationHandler.Factory::class.java - ) - - projectComponentManager.registerService( - ProjectFileIndex::class.java, - projectFileIndex - ) - - projectComponentManager.registerService( - ProjectRootManager::class.java, - CoreProjectRootManager(projectFileIndex) - ) - - registerExtensionPoint( - ApplicationExtensionDescriptor("org.jetbrains.kotlin.idePlatformKind", IdePlatformKind::class.java), - listOf( - CommonIdePlatformKind, - JvmIdePlatformKind, - JsIdePlatformKind, - NativeIdePlatformKind - ) - ) - - registerExtensionPoint( - IdePlatformKindResolution.Companion, - listOf( - CommonPlatformKindResolution(), - JvmPlatformKindResolution(), - JsPlatformKindResolution(), - NativePlatformKindResolution() - ) - ) - - return environment - } - - private fun createSourceModuleSearchScope(project: Project, sourceFiles: List): GlobalSearchScope = - when (analysisPlatform) { - Platform.jvm -> TopDownAnalyzerFacadeForJVM.newModuleSearchScope(project, sourceFiles) - Platform.js, Platform.common, Platform.native -> GlobalSearchScope.filesScope( - project, - sourceFiles.map { it.virtualFile }.toSet() - ) - } - - fun createResolutionFacade(environment: KotlinCoreEnvironment): Pair { - val projectContext = ProjectContext(environment.project, "Dokka") - val sourceFiles = environment.getSourceFiles() - - - val targetPlatform = when (analysisPlatform) { - Platform.js -> JsPlatforms.defaultJsPlatform - Platform.common -> CommonPlatforms.defaultCommonPlatform - Platform.native -> NativePlatforms.unspecifiedNativePlatform - Platform.jvm -> JvmPlatforms.defaultJvmPlatform - } - - val nativeLibraries = classpath.filter { it.extension == KLIB_EXTENSION } - .map { createNativeLibraryModuleInfo(it) } - - val library = object : LibraryModuleInfo { - override val analyzerServices: PlatformDependentAnalyzerServices = - analysisPlatform.analyzerServices() - override val name: Name = Name.special("") - override val platform: TargetPlatform = targetPlatform - override fun dependencies(): List = listOf(this) - override fun getLibraryRoots(): Collection = - classpath.filterNot { it.extension == KLIB_EXTENSION }.map { it.absolutePath } - } - - val module = object : ModuleInfo { - override val analyzerServices: PlatformDependentAnalyzerServices = - analysisPlatform.analyzerServices() - override val name: Name = Name.special("") - override val platform: TargetPlatform = targetPlatform - override fun dependencies(): List = listOf(this, library) + nativeLibraries - } - - val sourcesScope = createSourceModuleSearchScope(environment.project, sourceFiles) - val modulesContent: (ModuleInfo) -> ModuleContent = { - when (it) { - library -> ModuleContent(it, emptyList(), GlobalSearchScope.notScope(sourcesScope)) - module -> ModuleContent(it, emptyList(), GlobalSearchScope.allScope(environment.project)) - in nativeLibraries -> ModuleContent(it, emptyList(), GlobalSearchScope.notScope(sourcesScope)) - else -> throw IllegalArgumentException("Unexpected module info") - } - } - - var builtIns: JvmBuiltIns? = null - - val resolverForProject = when (analysisPlatform) { - Platform.jvm -> { - builtIns = JvmBuiltIns( - projectContext.storageManager, - JvmBuiltIns.Kind.FROM_CLASS_LOADER - ) // TODO we should use FROM_DEPENDENCIES - createJvmResolverForProject( - projectContext, - module, - library, - modulesContent, - sourcesScope, - builtIns - ) - } - Platform.common -> createCommonResolverForProject( - projectContext, - module, - library, - modulesContent, - environment - ) - Platform.js -> createJsResolverForProject(projectContext, module, library, modulesContent) - Platform.native -> createNativeResolverForProject(projectContext, module, library, modulesContent) - - } - val libraryModuleDescriptor = resolverForProject.descriptorForModule(library) - val moduleDescriptor = resolverForProject.descriptorForModule(module) - builtIns?.initialize(moduleDescriptor, true) - - val resolverForLibrary = - resolverForProject.resolverForModule(library) // Required before module to initialize library properly - val resolverForModule = resolverForProject.resolverForModule(module) - val libraryResolutionFacade = - DokkaResolutionFacade(environment.project, libraryModuleDescriptor, resolverForLibrary) - val created = DokkaResolutionFacade(environment.project, moduleDescriptor, resolverForModule) - val projectComponentManager = environment.project as MockComponentManager - projectComponentManager.registerService(KotlinCacheService::class.java, CoreKotlinCacheService(created)) - - return created to libraryResolutionFacade - } - - private fun Platform.analyzerServices() = when (this) { - Platform.js -> JsPlatformAnalyzerServices - Platform.common -> CommonPlatformAnalyzerServices - Platform.native -> NativePlatformAnalyzerServices - Platform.jvm -> JvmPlatformAnalyzerServices - } - - private fun createNativeLibraryModuleInfo(libraryFile: File): LibraryModuleInfo { - val kotlinLibrary = createKotlinLibrary(org.jetbrains.kotlin.konan.file.File(libraryFile.absolutePath), "",false) - return object : LibraryModuleInfo { - override val analyzerServices: PlatformDependentAnalyzerServices = - analysisPlatform.analyzerServices() - override val name: Name = Name.special("") - override val platform: TargetPlatform = NativePlatforms.unspecifiedNativePlatform - override fun dependencies(): List = listOf(this) - override fun getLibraryRoots(): Collection = listOf(libraryFile.absolutePath) - override val capabilities: Map, Any?> - get() = super.capabilities + (KlibModuleOrigin.CAPABILITY to kotlinLibrary) - } - } - - private fun createCommonResolverForProject( - projectContext: ProjectContext, - module: ModuleInfo, - library: LibraryModuleInfo, - modulesContent: (ModuleInfo) -> ModuleContent, - environment: KotlinCoreEnvironment - ): ResolverForProject { - return object : AbstractResolverForProject( - "Dokka", - projectContext, - modules = listOf(module, library) - ) { - override fun sdkDependency(module: ModuleInfo): ModuleInfo? = null - - override fun modulesContent(module: ModuleInfo): ModuleContent = modulesContent(module) - - override fun builtInsForModule(module: ModuleInfo): KotlinBuiltIns = DefaultBuiltIns.Instance - - override fun createResolverForModule( - descriptor: ModuleDescriptor, - moduleInfo: ModuleInfo - ): ResolverForModule = - CommonResolverForModuleFactory( - CommonAnalysisParameters { content -> - environment.createPackagePartProvider(content.moduleContentScope) - }, - CompilerEnvironment, - unspecifiedJvmPlatform, - true - ).createResolverForModule( - descriptor as ModuleDescriptorImpl, - projectContext.withModule(descriptor), - modulesContent(moduleInfo), - this, - LanguageVersionSettingsImpl.DEFAULT - ) - } - } - - private fun createJsResolverForProject( - projectContext: ProjectContext, - module: ModuleInfo, - library: LibraryModuleInfo, - modulesContent: (ModuleInfo) -> ModuleContent - ): ResolverForProject { - return object : AbstractResolverForProject( - "Dokka", - projectContext, - modules = listOf(module, library) - ) { - override fun modulesContent(module: ModuleInfo): ModuleContent = modulesContent(module) - override fun createResolverForModule( - descriptor: ModuleDescriptor, - moduleInfo: ModuleInfo - ): ResolverForModule = JsResolverForModuleFactory( - CompilerEnvironment - ).createResolverForModule( - descriptor as ModuleDescriptorImpl, - projectContext.withModule(descriptor), - modulesContent(moduleInfo), - this, - LanguageVersionSettingsImpl.DEFAULT - ) - - override fun builtInsForModule(module: ModuleInfo): KotlinBuiltIns = DefaultBuiltIns.Instance - - override fun sdkDependency(module: ModuleInfo): ModuleInfo? = null - } - } - - private fun createNativeResolverForProject( - projectContext: ProjectContext, - module: ModuleInfo, - library: LibraryModuleInfo, - modulesContent: (ModuleInfo) -> ModuleContent - ): ResolverForProject { - return object : AbstractResolverForProject( - "Dokka", - projectContext, - modules = module.dependencies() - ) { - override fun modulesContent(module: ModuleInfo): ModuleContent = modulesContent(module) - override fun createResolverForModule( - descriptor: ModuleDescriptor, - moduleInfo: ModuleInfo - ): ResolverForModule { - - return NativeResolverForModuleFactory( - PlatformAnalysisParameters.Empty, - CompilerEnvironment, - NativePlatforms.unspecifiedNativePlatform - ).createResolverForModule( - descriptor as ModuleDescriptorImpl, - projectContext.withModule(descriptor), - modulesContent(moduleInfo), - this, - LanguageVersionSettingsImpl.DEFAULT - ) - } - - override fun builtInsForModule(module: ModuleInfo): KotlinBuiltIns = DefaultBuiltIns.Instance - - override fun sdkDependency(module: ModuleInfo): ModuleInfo? = null - } - } - - private fun createJvmResolverForProject( - projectContext: ProjectContext, - module: ModuleInfo, - library: LibraryModuleInfo, - modulesContent: (ModuleInfo) -> ModuleContent, - sourcesScope: GlobalSearchScope, - builtIns: KotlinBuiltIns - ): ResolverForProject { - val javaRoots = classpath - .mapNotNull { - val rootFile = when (it.extension) { - "jar" -> StandardFileSystems.jar().findFileByPath("${it.absolutePath}${JAR_SEPARATOR}") - else -> StandardFileSystems.local().findFileByPath(it.absolutePath) - } - rootFile?.let { JavaRoot(it, JavaRoot.RootType.BINARY) } - } - - return object : AbstractResolverForProject( - "Dokka", - projectContext, - modules = listOf(module, library) - ) { - override fun modulesContent(module: ModuleInfo): ModuleContent = - when (module) { - library -> ModuleContent(module, emptyList(), GlobalSearchScope.notScope(sourcesScope)) - module -> ModuleContent(module, emptyList(), sourcesScope) - else -> throw IllegalArgumentException("Unexpected module info") - } - - override fun builtInsForModule(module: ModuleInfo): KotlinBuiltIns = builtIns - - override fun createResolverForModule( - descriptor: ModuleDescriptor, - moduleInfo: ModuleInfo - ): ResolverForModule = JvmResolverForModuleFactory( - JvmPlatformParameters({ content -> - JvmPackagePartProvider( - configuration.languageVersionSettings, - content.moduleContentScope - ) - .apply { - addRoots(javaRoots, messageCollector) - } - }, { - val file = (it as? BinaryJavaClass)?.virtualFile ?: (it as JavaClassImpl).psi.containingFile.virtualFile - if (file in sourcesScope) - module - else - library - }), - CompilerEnvironment, - unspecifiedJvmPlatform - ).createResolverForModule( - descriptor as ModuleDescriptorImpl, - projectContext.withModule(descriptor), - modulesContent(moduleInfo), - this, - LanguageVersionSettingsImpl.DEFAULT - ) - - override fun sdkDependency(module: ModuleInfo): ModuleInfo? = null - } - } - - fun loadLanguageVersionSettings(languageVersionString: String?, apiVersionString: String?) { - val languageVersion = LanguageVersion.fromVersionString(languageVersionString) ?: LanguageVersion.LATEST_STABLE - val apiVersion = - apiVersionString?.let { ApiVersion.parse(it) } ?: ApiVersion.createByLanguageVersion(languageVersion) - configuration.languageVersionSettings = LanguageVersionSettingsImpl(languageVersion, apiVersion) - } - - /** - * Classpath for this environment. - */ - val classpath: List - get() = configuration.jvmClasspathRoots - - /** - * Adds list of paths to classpath. - * $paths: collection of files to add - */ - fun addClasspath(paths: List) { - if (analysisPlatform == Platform.js) { - configuration.addAll(JSConfigurationKeys.LIBRARIES, paths.map { it.absolutePath }) - } - configuration.addJvmClasspathRoots(paths) - } - - /** - * Adds path to classpath. - * $path: path to add - */ - fun addClasspath(path: File) { - if (analysisPlatform == Platform.js) { - configuration.add(JSConfigurationKeys.LIBRARIES, path.absolutePath) - } - configuration.addJvmClasspathRoot(path) - } - - /** - * List of source roots for this environment. - */ - val sources: List - get() = configuration.get(CLIConfigurationKeys.CONTENT_ROOTS) - ?.filterIsInstance() - ?.map { it.path } ?: emptyList() - - /** - * Adds list of paths to source roots. - * $list: collection of files to add - */ - fun addSources(list: List) { - list.forEach { - configuration.addKotlinSourceRoot(it) - val file = File(it) - if (file.isDirectory || file.extension == ".java") { - configuration.addJavaSourceRoot(file) - } - } - } - - fun addRoots(list: List) { - configuration.addAll(CLIConfigurationKeys.CONTENT_ROOTS, list) - } - - /** - * Disposes the environment and frees all associated resources. - */ - override fun dispose() { - Disposer.dispose(this) - } - - companion object { - private fun registerExtensionPoint( - appExtension: ApplicationExtensionDescriptor, - instances: List - ) { - if (Extensions.getRootArea().hasExtensionPoint(appExtension.extensionPointName)) - return - - appExtension.registerExtensionPoint() - instances.forEach(appExtension::registerExtension) - } - } -} - -fun contentRootFromPath(path: String): ContentRoot { - val file = File(path) - return if (file.extension == "java") JavaSourceRoot(file, null) else KotlinSourceRoot(path, false) -} - - -class DokkaResolutionFacade( - override val project: Project, - override val moduleDescriptor: ModuleDescriptor, - val resolverForModule: ResolverForModule -) : ResolutionFacade { - override fun analyzeWithAllCompilerChecks(elements: Collection): AnalysisResult { - throw UnsupportedOperationException() - } - - override fun tryGetFrontendService(element: PsiElement, serviceClass: Class): T? { - return resolverForModule.componentProvider.tryGetService(serviceClass) - } - - override fun resolveToDescriptor( - declaration: KtDeclaration, - bodyResolveMode: BodyResolveMode - ): DeclarationDescriptor { - return resolveSession.resolveToDescriptor(declaration) - } - - override fun analyze(elements: Collection, bodyResolveMode: BodyResolveMode): BindingContext { - throw UnsupportedOperationException() - } - - val resolveSession: ResolveSession get() = getFrontendService(ResolveSession::class.java) - - override fun analyze(element: KtElement, bodyResolveMode: BodyResolveMode): BindingContext { - if (element is KtDeclaration) { - val descriptor = resolveToDescriptor(element) - return object : BindingContext { - override fun getKeys(p0: WritableSlice?): Collection { - throw UnsupportedOperationException() - } - - override fun getType(p0: KtExpression): KotlinType? { - throw UnsupportedOperationException() - } - - override fun get(slice: ReadOnlySlice?, key: K): V? { - if (key != element) { - throw UnsupportedOperationException() - } - return when { - slice == BindingContext.DECLARATION_TO_DESCRIPTOR -> descriptor as V - slice == BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER && (element as KtParameter).hasValOrVar() -> descriptor as V - else -> null - } - } - - override fun getDiagnostics(): Diagnostics { - throw UnsupportedOperationException() - } - - override fun addOwnDataTo(p0: BindingTrace, p1: Boolean) { - throw UnsupportedOperationException() - } - - override fun getSliceContents(p0: ReadOnlySlice): ImmutableMap { - throw UnsupportedOperationException() - } - - } - } - throw UnsupportedOperationException() - } - - override fun getFrontendService(element: PsiElement, serviceClass: Class): T { - throw UnsupportedOperationException() - } - - override fun getFrontendService(serviceClass: Class): T { - return resolverForModule.componentProvider.getService(serviceClass) - } - - override fun getFrontendService(moduleDescriptor: ModuleDescriptor, serviceClass: Class): T { - return resolverForModule.componentProvider.getService(serviceClass) - } - - override fun getIdeService(serviceClass: Class): T { - throw UnsupportedOperationException() - } - -} diff --git a/core/src/main/kotlin/analysis/CoreKotlinCacheService.kt b/core/src/main/kotlin/analysis/CoreKotlinCacheService.kt deleted file mode 100644 index bcf91225..00000000 --- a/core/src/main/kotlin/analysis/CoreKotlinCacheService.kt +++ /dev/null @@ -1,42 +0,0 @@ -package org.jetbrains.dokka.analysis - -import com.intellij.psi.PsiFile -import org.jetbrains.kotlin.analyzer.ModuleInfo -import org.jetbrains.kotlin.caches.resolve.KotlinCacheService -import org.jetbrains.kotlin.idea.resolve.ResolutionFacade -import org.jetbrains.kotlin.psi.KtElement -import org.jetbrains.kotlin.resolve.diagnostics.KotlinSuppressCache - - -class CoreKotlinCacheService(private val resolutionFacade: DokkaResolutionFacade) : KotlinCacheService { - override fun getResolutionFacade(elements: List): ResolutionFacade { - return resolutionFacade - } - - override fun getResolutionFacade( - elements: List, - platform: org.jetbrains.kotlin.platform.TargetPlatform - ): ResolutionFacade { - return resolutionFacade - } - - override fun getResolutionFacadeByFile( - file: PsiFile, - platform: org.jetbrains.kotlin.platform.TargetPlatform - ): ResolutionFacade? { - return resolutionFacade - } - - override fun getResolutionFacadeByModuleInfo( - moduleInfo: ModuleInfo, - platform: org.jetbrains.kotlin.platform.TargetPlatform - ): ResolutionFacade? { - return resolutionFacade - } - - override fun getSuppressionCache(): KotlinSuppressCache { - throw UnsupportedOperationException() - } - -} - diff --git a/core/src/main/kotlin/analysis/CoreProjectFileIndex.kt b/core/src/main/kotlin/analysis/CoreProjectFileIndex.kt deleted file mode 100644 index 45138e39..00000000 --- a/core/src/main/kotlin/analysis/CoreProjectFileIndex.kt +++ /dev/null @@ -1,569 +0,0 @@ -package org.jetbrains.dokka.analysis - -import com.intellij.openapi.Disposable -import com.intellij.openapi.components.BaseComponent -import com.intellij.openapi.extensions.ExtensionPointName -import com.intellij.openapi.module.Module -import com.intellij.openapi.project.Project -import com.intellij.openapi.projectRoots.Sdk -import com.intellij.openapi.projectRoots.SdkAdditionalData -import com.intellij.openapi.projectRoots.SdkModificator -import com.intellij.openapi.projectRoots.SdkTypeId -import com.intellij.openapi.roots.* -import com.intellij.openapi.roots.impl.ProjectOrderEnumerator -import com.intellij.openapi.util.Condition -import com.intellij.openapi.util.Key -import com.intellij.openapi.util.UserDataHolderBase -import com.intellij.openapi.vfs.StandardFileSystems -import com.intellij.openapi.vfs.VfsUtilCore.getVirtualFileForJar -import com.intellij.openapi.vfs.VirtualFile -import com.intellij.openapi.vfs.VirtualFileFilter -import com.intellij.psi.search.GlobalSearchScope -import com.intellij.util.messages.MessageBus -import org.jetbrains.jps.model.module.JpsModuleSourceRootType -import org.jetbrains.kotlin.cli.common.config.ContentRoot -import org.jetbrains.kotlin.cli.common.config.KotlinSourceRoot -import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot -import org.jetbrains.kotlin.cli.jvm.config.JvmContentRoot -import org.picocontainer.PicoContainer -import java.io.File - -/** - * Workaround for the lack of ability to create a ProjectFileIndex implementation using only - * classes from projectModel-{api,impl}. - */ -class CoreProjectFileIndex(private val project: Project, contentRoots: List) : ProjectFileIndex, ModuleFileIndex { - override fun iterateContent(p0: ContentIterator, p1: VirtualFileFilter?): Boolean { - throw UnsupportedOperationException() - } - - override fun iterateContentUnderDirectory(p0: VirtualFile, p1: ContentIterator, p2: VirtualFileFilter?): Boolean { - throw UnsupportedOperationException() - } - - override fun isInLibrary(p0: VirtualFile): Boolean { - throw UnsupportedOperationException() - } - - val sourceRoots = contentRoots.filter { it !is JvmClasspathRoot } - val classpathRoots = contentRoots.filterIsInstance() - - val module: Module = object : UserDataHolderBase(), Module { - override fun isDisposed(): Boolean { - throw UnsupportedOperationException() - } - - override fun getOptionValue(p0: String): String? { - throw UnsupportedOperationException() - } - - override fun clearOption(p0: String) { - throw UnsupportedOperationException() - } - - override fun getName(): String = "" - - override fun getModuleWithLibrariesScope(): GlobalSearchScope { - throw UnsupportedOperationException() - } - - override fun getModuleWithDependentsScope(): GlobalSearchScope { - throw UnsupportedOperationException() - } - - override fun getModuleContentScope(): GlobalSearchScope { - throw UnsupportedOperationException() - } - - override fun isLoaded(): Boolean { - throw UnsupportedOperationException() - } - - override fun setOption(p0: String, p1: String?) { - throw UnsupportedOperationException() - } - - override fun getModuleWithDependenciesScope(): GlobalSearchScope { - throw UnsupportedOperationException() - } - - override fun getModuleWithDependenciesAndLibrariesScope(p0: Boolean): GlobalSearchScope { - throw UnsupportedOperationException() - } - - override fun getProject(): Project = this@CoreProjectFileIndex.project - - override fun getModuleContentWithDependenciesScope(): GlobalSearchScope { - throw UnsupportedOperationException() - } - - override fun getModuleFilePath(): String { - throw UnsupportedOperationException() - } - - override fun getModuleTestsWithDependentsScope(): GlobalSearchScope { - throw UnsupportedOperationException() - } - - override fun getModuleScope(): GlobalSearchScope { - throw UnsupportedOperationException() - } - - override fun getModuleScope(p0: Boolean): GlobalSearchScope { - throw UnsupportedOperationException() - } - - override fun getModuleRuntimeScope(p0: Boolean): GlobalSearchScope { - throw UnsupportedOperationException() - } - - override fun getModuleFile(): VirtualFile? { - throw UnsupportedOperationException() - } - - override fun getExtensions(p0: ExtensionPointName): Array { - throw UnsupportedOperationException() - } - - override fun getComponent(p0: String): BaseComponent? { - throw UnsupportedOperationException() - } - - override fun getComponent(p0: Class, p1: T): T { - throw UnsupportedOperationException() - } - - override fun getComponent(interfaceClass: Class): T? { - if (interfaceClass == ModuleRootManager::class.java) { - return moduleRootManager as T - } - throw UnsupportedOperationException() - } - - override fun getDisposed(): Condition<*> { - throw UnsupportedOperationException() - } - - override fun getComponents(p0: Class): Array { - throw UnsupportedOperationException() - } - - override fun getPicoContainer(): PicoContainer { - throw UnsupportedOperationException() - } - - override fun hasComponent(p0: Class<*>): Boolean { - throw UnsupportedOperationException() - } - - override fun getMessageBus(): MessageBus { - throw UnsupportedOperationException() - } - - override fun dispose() { - throw UnsupportedOperationException() - } - } - - private val sdk: Sdk = object : Sdk, RootProvider { - override fun getFiles(rootType: OrderRootType): Array = classpathRoots - .mapNotNull { StandardFileSystems.local().findFileByPath(it.file.path) } - .toTypedArray() - - override fun addRootSetChangedListener(p0: RootProvider.RootSetChangedListener) { - throw UnsupportedOperationException() - } - - override fun addRootSetChangedListener(p0: RootProvider.RootSetChangedListener, p1: Disposable) { - throw UnsupportedOperationException() - } - - override fun getUrls(p0: OrderRootType): Array { - throw UnsupportedOperationException() - } - - override fun removeRootSetChangedListener(p0: RootProvider.RootSetChangedListener) { - throw UnsupportedOperationException() - } - - override fun getSdkModificator(): SdkModificator { - throw UnsupportedOperationException() - } - - override fun getName(): String = "" - - override fun getRootProvider(): RootProvider = this - - override fun getHomePath(): String? { - throw UnsupportedOperationException() - } - - override fun getVersionString(): String? { - throw UnsupportedOperationException() - } - - override fun getSdkAdditionalData(): SdkAdditionalData? { - throw UnsupportedOperationException() - } - - override fun clone(): Any { - throw UnsupportedOperationException() - } - - override fun getSdkType(): SdkTypeId { - throw UnsupportedOperationException() - } - - override fun getHomeDirectory(): VirtualFile? { - throw UnsupportedOperationException() - } - - override fun getUserData(p0: Key): T? { - throw UnsupportedOperationException() - } - - override fun putUserData(p0: Key, p1: T?) { - throw UnsupportedOperationException() - } - } - - private val moduleSourceOrderEntry = object : ModuleSourceOrderEntry { - override fun getFiles(p0: OrderRootType): Array { - throw UnsupportedOperationException() - } - - override fun getUrls(p0: OrderRootType): Array { - throw UnsupportedOperationException() - } - - override fun accept(p0: RootPolicy, p1: R?): R { - throw UnsupportedOperationException() - } - - - override fun getPresentableName(): String { - throw UnsupportedOperationException() - } - - override fun getOwnerModule(): Module = module - - - override fun isValid(): Boolean { - throw UnsupportedOperationException() - } - - override fun compareTo(other: OrderEntry?): Int { - throw UnsupportedOperationException() - } - - override fun getRootModel(): ModuleRootModel = moduleRootManager - - override fun isSynthetic(): Boolean { - throw UnsupportedOperationException() - } - } - - private val sdkOrderEntry = object : JdkOrderEntry { - override fun getFiles(p0: OrderRootType): Array { - throw UnsupportedOperationException() - } - - override fun getUrls(p0: OrderRootType): Array { - throw UnsupportedOperationException() - } - - override fun accept(p0: RootPolicy, p1: R?): R { - throw UnsupportedOperationException() - } - - override fun getJdkName(): String? { - throw UnsupportedOperationException() - } - - override fun getJdk(): Sdk = sdk - - override fun getPresentableName(): String { - throw UnsupportedOperationException() - } - - override fun getOwnerModule(): Module { - throw UnsupportedOperationException() - } - - override fun isValid(): Boolean { - throw UnsupportedOperationException() - } - - override fun getRootFiles(p0: OrderRootType): Array { - throw UnsupportedOperationException() - } - - override fun getRootUrls(p0: OrderRootType): Array { - throw UnsupportedOperationException() - } - - override fun compareTo(other: OrderEntry?): Int { - throw UnsupportedOperationException() - } - - override fun isSynthetic(): Boolean { - throw UnsupportedOperationException() - } - - } - - inner class MyModuleRootManager : ModuleRootManager() { - override fun getExternalSource(): ProjectModelExternalSource? { - throw UnsupportedOperationException() - } - - override fun getExcludeRoots(): Array { - throw UnsupportedOperationException() - } - - override fun getContentEntries(): Array { - throw UnsupportedOperationException() - } - - override fun getExcludeRootUrls(): Array { - throw UnsupportedOperationException() - } - - override fun processOrder(p0: RootPolicy, p1: R): R { - throw UnsupportedOperationException() - } - - override fun getSourceRoots(p0: Boolean): Array { - throw UnsupportedOperationException() - } - - override fun getSourceRoots(): Array { - throw UnsupportedOperationException() - } - - override fun getSourceRoots(p0: JpsModuleSourceRootType<*>): MutableList { - throw UnsupportedOperationException() - } - - override fun getSourceRoots(p0: MutableSet>): MutableList { - throw UnsupportedOperationException() - } - - override fun getContentRoots(): Array { - throw UnsupportedOperationException() - } - - override fun orderEntries(): OrderEnumerator = - ProjectOrderEnumerator(project, null).using(object : RootModelProvider { - override fun getModules(): Array = arrayOf(module) - - override fun getRootModel(p0: Module): ModuleRootModel = this@MyModuleRootManager - }) - - override fun getModuleExtension(p0: Class): T { - throw UnsupportedOperationException() - } - - override fun getDependencyModuleNames(): Array { - throw UnsupportedOperationException() - } - - override fun getModule(): Module = this@CoreProjectFileIndex.module - - override fun isSdkInherited(): Boolean { - throw UnsupportedOperationException() - } - - override fun getOrderEntries(): Array = arrayOf(moduleSourceOrderEntry, sdkOrderEntry) - - override fun getSourceRootUrls(): Array { - throw UnsupportedOperationException() - } - - override fun getSourceRootUrls(p0: Boolean): Array { - throw UnsupportedOperationException() - } - - override fun getSdk(): Sdk? { - throw UnsupportedOperationException() - } - - override fun getContentRootUrls(): Array { - throw UnsupportedOperationException() - } - - override fun getModuleDependencies(): Array { - throw UnsupportedOperationException() - } - - override fun getModuleDependencies(p0: Boolean): Array { - throw UnsupportedOperationException() - } - - override fun getModifiableModel(): ModifiableRootModel { - throw UnsupportedOperationException() - } - - override fun isDependsOn(p0: Module): Boolean { - throw UnsupportedOperationException() - } - - override fun getFileIndex(): ModuleFileIndex { - return this@CoreProjectFileIndex - } - - override fun getDependencies(): Array { - throw UnsupportedOperationException() - } - - override fun getDependencies(p0: Boolean): Array { - throw UnsupportedOperationException() - } - } - - val moduleRootManager = MyModuleRootManager() - - override fun getContentRootForFile(p0: VirtualFile): VirtualFile? { - throw UnsupportedOperationException() - } - - override fun getContentRootForFile(p0: VirtualFile, p1: Boolean): VirtualFile? { - throw UnsupportedOperationException() - } - - override fun getPackageNameByDirectory(p0: VirtualFile): String? { - throw UnsupportedOperationException() - } - - override fun isInLibrarySource(file: VirtualFile): Boolean = false - - override fun getClassRootForFile(file: VirtualFile): VirtualFile? = - classpathRoots.firstOrNull { it.contains(file) }?.let { StandardFileSystems.local().findFileByPath(it.file.path) } - - override fun getOrderEntriesForFile(file: VirtualFile): List = - if (classpathRoots.contains(file)) listOf(sdkOrderEntry) else emptyList() - - override fun isInLibraryClasses(file: VirtualFile): Boolean = classpathRoots.contains(file) - - override fun isExcluded(p0: VirtualFile): Boolean { - throw UnsupportedOperationException() - } - - override fun getSourceRootForFile(p0: VirtualFile): VirtualFile? { - throw UnsupportedOperationException() - } - - override fun isUnderIgnored(p0: VirtualFile): Boolean { - throw UnsupportedOperationException() - } - - override fun isLibraryClassFile(p0: VirtualFile): Boolean { - throw UnsupportedOperationException() - } - - override fun getModuleForFile(file: VirtualFile): Module? = - if (sourceRoots.contains(file)) module else null - - private fun List.contains(file: VirtualFile): Boolean = any { it.contains(file) } - - override fun getModuleForFile(p0: VirtualFile, p1: Boolean): Module? { - throw UnsupportedOperationException() - } - - override fun isInSource(p0: VirtualFile): Boolean { - throw UnsupportedOperationException() - } - - override fun isIgnored(p0: VirtualFile): Boolean { - throw UnsupportedOperationException() - } - - override fun isContentSourceFile(p0: VirtualFile): Boolean { - throw UnsupportedOperationException() - } - - override fun isInSourceContent(file: VirtualFile): Boolean = sourceRoots.contains(file) - - override fun iterateContent(p0: ContentIterator): Boolean { - throw UnsupportedOperationException() - } - - override fun isInContent(p0: VirtualFile): Boolean { - throw UnsupportedOperationException() - } - - override fun iterateContentUnderDirectory(p0: VirtualFile, p1: ContentIterator): Boolean { - throw UnsupportedOperationException() - } - - override fun isInTestSourceContent(file: VirtualFile): Boolean = false - - override fun isUnderSourceRootOfType(p0: VirtualFile, p1: MutableSet>): Boolean { - throw UnsupportedOperationException() - } - - override fun getOrderEntryForFile(p0: VirtualFile): OrderEntry? { - throw UnsupportedOperationException() - } -} - -class CoreProjectRootManager(val projectFileIndex: CoreProjectFileIndex) : ProjectRootManager() { - override fun orderEntries(): OrderEnumerator { - throw UnsupportedOperationException() - } - - override fun orderEntries(p0: MutableCollection): OrderEnumerator { - throw UnsupportedOperationException() - } - - override fun getContentRootsFromAllModules(): Array { - throw UnsupportedOperationException() - } - - override fun setProjectSdk(p0: Sdk?) { - throw UnsupportedOperationException() - } - - override fun setProjectSdkName(p0: String) { - throw UnsupportedOperationException() - } - - override fun getModuleSourceRoots(p0: MutableSet>): MutableList { - throw UnsupportedOperationException() - } - - override fun getContentSourceRoots(): Array { - throw UnsupportedOperationException() - } - - override fun getFileIndex(): ProjectFileIndex = projectFileIndex - - override fun getProjectSdkName(): String? { - throw UnsupportedOperationException() - } - - override fun getProjectSdk(): Sdk? { - throw UnsupportedOperationException() - } - - override fun getContentRoots(): Array { - throw UnsupportedOperationException() - } - - override fun getContentRootUrls(): MutableList { - throw UnsupportedOperationException() - } - -} - -fun ContentRoot.contains(file: VirtualFile) = when (this) { - is JvmContentRoot -> { - val path = if (file.fileSystem.protocol == StandardFileSystems.JAR_PROTOCOL) - getVirtualFileForJar(file)?.path ?: file.path - else - file.path - File(path).startsWith(this.file.absoluteFile) - } - is KotlinSourceRoot -> File(file.path).startsWith(File(this.path).absoluteFile) - else -> false -} diff --git a/core/src/main/kotlin/analysis/JavaResolveExtension.kt b/core/src/main/kotlin/analysis/JavaResolveExtension.kt deleted file mode 100644 index ab6bec9c..00000000 --- a/core/src/main/kotlin/analysis/JavaResolveExtension.kt +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -@file:JvmName("JavaResolutionUtils") - -package org.jetbrains.dokka.analysis - -import com.intellij.psi.* -import org.jetbrains.kotlin.asJava.unwrapped -import org.jetbrains.kotlin.caches.resolve.KotlinCacheService -import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.idea.resolve.ResolutionFacade -import org.jetbrains.kotlin.incremental.components.NoLookupLocation -import org.jetbrains.kotlin.load.java.sources.JavaSourceElement -import org.jetbrains.kotlin.load.java.structure.* -import org.jetbrains.kotlin.load.java.structure.impl.* -import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.platform.jvm.JvmPlatforms -import org.jetbrains.kotlin.psi.KtDeclaration -import org.jetbrains.kotlin.resolve.jvm.JavaDescriptorResolver -import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter -import org.jetbrains.kotlin.resolve.scopes.MemberScope - -// TODO: Remove that file - -@JvmOverloads -fun PsiMethod.getJavaMethodDescriptor(resolutionFacade: ResolutionFacade = javaResolutionFacade()): DeclarationDescriptor? { - val method = originalElement as? PsiMethod ?: return null - if (method.containingClass == null || !Name.isValidIdentifier(method.name)) return null - val resolver = method.getJavaDescriptorResolver(resolutionFacade) - return when { - method.isConstructor -> resolver?.resolveConstructor(JavaConstructorImpl(method)) - else -> resolver?.resolveMethod(JavaMethodImpl(method)) - } -} - -@JvmOverloads -fun PsiClass.getJavaClassDescriptor(resolutionFacade: ResolutionFacade = javaResolutionFacade()): ClassDescriptor? { - val psiClass = originalElement as? PsiClass ?: return null - return psiClass.getJavaDescriptorResolver(resolutionFacade)?.resolveClass(JavaClassImpl(psiClass)) -} - -@JvmOverloads -fun PsiField.getJavaFieldDescriptor(resolutionFacade: ResolutionFacade = javaResolutionFacade()): PropertyDescriptor? { - val field = originalElement as? PsiField ?: return null - return field.getJavaDescriptorResolver(resolutionFacade)?.resolveField(JavaFieldImpl(field)) -} - -@JvmOverloads -fun PsiMember.getJavaMemberDescriptor(resolutionFacade: ResolutionFacade = javaResolutionFacade()): DeclarationDescriptor? { - return when (this) { - is PsiEnumConstant -> containingClass?.getJavaClassDescriptor(resolutionFacade) - is PsiClass -> getJavaClassDescriptor(resolutionFacade) - is PsiMethod -> getJavaMethodDescriptor(resolutionFacade) - is PsiField -> getJavaFieldDescriptor(resolutionFacade) - else -> null - } -} - -@JvmOverloads -fun PsiMember.getJavaOrKotlinMemberDescriptor(resolutionFacade: ResolutionFacade = javaResolutionFacade()): DeclarationDescriptor? { - val callable = unwrapped - return when (callable) { - is PsiMember -> getJavaMemberDescriptor(resolutionFacade) - is KtDeclaration -> { - val descriptor = resolutionFacade.resolveToDescriptor(callable) - if (descriptor is ClassDescriptor && this is PsiMethod) descriptor.unsubstitutedPrimaryConstructor else descriptor - } - else -> null - } -} - -private fun PsiElement.getJavaDescriptorResolver(resolutionFacade: ResolutionFacade): JavaDescriptorResolver? { - return resolutionFacade.tryGetFrontendService(this, JavaDescriptorResolver::class.java) -} - -private fun JavaDescriptorResolver.resolveMethod(method: JavaMethod): DeclarationDescriptor? { - return getContainingScope(method) - ?.getContributedDescriptors(nameFilter = { true }, kindFilter = DescriptorKindFilter.CALLABLES) - ?.filterIsInstance() - ?.findByJavaElement(method) -} - -private fun JavaDescriptorResolver.resolveConstructor(constructor: JavaConstructor): ConstructorDescriptor? { - return resolveClass(constructor.containingClass)?.constructors?.findByJavaElement(constructor) -} - -private fun JavaDescriptorResolver.resolveField(field: JavaField): PropertyDescriptor? { - return getContainingScope(field)?.getContributedVariables(field.name, NoLookupLocation.FROM_IDE)?.findByJavaElement(field) -} - -private fun JavaDescriptorResolver.getContainingScope(member: JavaMember): MemberScope? { - val containingClass = resolveClass(member.containingClass) - return if (member.isStatic) - containingClass?.staticScope - else - containingClass?.defaultType?.memberScope -} - -private fun Collection.findByJavaElement(javaElement: JavaElement): T? { - return firstOrNull { member -> - val memberJavaElement = (member.original.source as? JavaSourceElement)?.javaElement - when { - memberJavaElement == javaElement -> - true - memberJavaElement is JavaElementImpl<*> && javaElement is JavaElementImpl<*> -> - memberJavaElement.psi.isEquivalentTo(javaElement.psi) - else -> - false - } - } -} - -fun PsiElement.javaResolutionFacade() = - KotlinCacheService.getInstance(project).getResolutionFacadeByFile(this.originalElement.containingFile, JvmPlatforms.defaultJvmPlatform)!! diff --git a/core/src/main/kotlin/links/DRI.kt b/core/src/main/kotlin/links/DRI.kt index 9cc51fb7..3f6d886f 100644 --- a/core/src/main/kotlin/links/DRI.kt +++ b/core/src/main/kotlin/links/DRI.kt @@ -1,15 +1,5 @@ package org.jetbrains.dokka.links -import com.intellij.psi.* -import org.jetbrains.kotlin.descriptors.* -import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf -import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe -import org.jetbrains.kotlin.resolve.descriptorUtil.parentsWithSelf -import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver -import org.jetbrains.kotlin.types.KotlinType -import org.jetbrains.kotlin.types.TypeProjection -import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull - /** * [DRI] stands for DokkaResourceIdentifier */ @@ -21,33 +11,10 @@ data class DRI( val extra: String? = null ) { override fun toString(): String = - "${packageName.orEmpty()}/${classNames.orEmpty()}/${callable?.name.orEmpty()}/${callable?.signature().orEmpty()}/$target/${extra.orEmpty()}" + "${packageName.orEmpty()}/${classNames.orEmpty()}/${callable?.name.orEmpty()}/${callable?.signature() + .orEmpty()}/$target/${extra.orEmpty()}" companion object { - fun from(descriptor: DeclarationDescriptor) = descriptor.parentsWithSelf.run { - val callable = firstIsInstanceOrNull() - DRI( - firstIsInstanceOrNull()?.fqName?.asString(), - (filterIsInstance() + filterIsInstance()).toList() - .takeIf { it.isNotEmpty() } - ?.asReversed() - ?.joinToString(separator = ".") { it.name.asString() }, - callable?.let { Callable.from(it) }, - DriTarget.from(descriptor) - ) - } - - fun from(psi: PsiElement) = psi.parentsWithSelf.run { - val psiMethod = firstIsInstanceOrNull() - val psiField = firstIsInstanceOrNull() - val classes = filterIsInstance().toList() - DRI( - classes.lastOrNull()?.qualifiedName?.substringBeforeLast('.', ""), - classes.toList().takeIf { it.isNotEmpty() }?.asReversed()?.mapNotNull { it.name }?.joinToString("."), - psiMethod?.let { Callable.from(it) } ?: psiField?.let { Callable.from(it) } , - DriTarget.from(psi) - ) - } val topLevel = DRI() } } @@ -78,71 +45,11 @@ data class Callable( ) { fun signature() = "${receiver?.toString().orEmpty()}#${params.joinToString("#")}" - companion object { - fun from(descriptor: CallableDescriptor) = with(descriptor) { - Callable( - name.asString(), - extensionReceiverParameter?.let { TypeReference.from(it) }, - valueParameters.mapNotNull { TypeReference.from(it) } - ) - } - - fun from(psi: PsiMethod) = with(psi) { - Callable( - name, - null, - parameterList.parameters.map { param -> JavaClassReference(param.type.canonicalText) }) - } - - fun from(psi: PsiField): Callable { - return Callable( - name = psi.name, - receiver = null, - params = emptyList() - ) - } - } + companion object } sealed class TypeReference { - companion object { - fun from(d: ReceiverParameterDescriptor): TypeReference? = - when (d.value) { - is ExtensionReceiver -> fromPossiblyNullable(d.type) - else -> run { - println("Unknown value type for $d") - null - } - } - - fun from(d: ValueParameterDescriptor): TypeReference? = - fromPossiblyNullable(d.type) - - fun from(p: PsiClass) = TypeReference - - private fun fromPossiblyNullable(t: KotlinType, self: KotlinType? = null): TypeReference = - from(t, self).let { if (t.isMarkedNullable) Nullable(it) else it } - - private fun from(t: KotlinType, self: KotlinType? = null): TypeReference = - if (self is KotlinType && self.constructor == t.constructor && self.arguments == t.arguments) - SelfType - else when (val d = t.constructor.declarationDescriptor) { - is TypeParameterDescriptor -> TypeParam( - d.upperBounds.map { fromPossiblyNullable(it, self ?: t) } - ) - else -> TypeConstructor( - t.constructorName.orEmpty(), - t.arguments.map { fromProjection(it, self) } - ) - } - - private fun fromProjection(t: TypeProjection, r: KotlinType? = null): TypeReference = - if (t.isStarProjection) { - StarProjection - } else { - fromPossiblyNullable(t.type, r) - } - } + companion object } data class JavaClassReference(val name: String) : TypeReference() { @@ -171,39 +78,10 @@ object StarProjection : TypeReference() { override fun toString() = "*" } -private val KotlinType.constructorName - get() = constructor.declarationDescriptor?.fqNameSafe?.asString() - sealed class DriTarget { override fun toString(): String = this.javaClass.simpleName - companion object { - fun from(descriptor: DeclarationDescriptor): DriTarget = descriptor.parentsWithSelf.run { - return when (descriptor) { - is TypeParameterDescriptor -> PointingToGenericParameters(descriptor.index) - else -> { - val callable = firstIsInstanceOrNull() - val params = - callable?.let { listOfNotNull(it.extensionReceiverParameter) + it.valueParameters }.orEmpty() - val parameterDescriptor = firstIsInstanceOrNull() - - parameterDescriptor?.let { PointingToCallableParameters(params.indexOf(it)) } - ?: PointingToDeclaration - } - } - } - - fun from(psi: PsiElement): DriTarget = psi.parentsWithSelf.run { - return when (psi) { - is PsiTypeParameter -> PointingToGenericParameters(psi.index) - else -> firstIsInstanceOrNull()?.let { - val callable = firstIsInstanceOrNull() - val params = (callable?.parameterList?.parameters).orEmpty() - PointingToCallableParameters(params.indexOf(it)) - } ?: PointingToDeclaration - } - } - } + companion object } data class PointingToGenericParameters(val parameterIndex: Int) : DriTarget() { diff --git a/core/src/main/kotlin/model/Documentable.kt b/core/src/main/kotlin/model/Documentable.kt index 90958210..768bddc5 100644 --- a/core/src/main/kotlin/model/Documentable.kt +++ b/core/src/main/kotlin/model/Documentable.kt @@ -1,12 +1,10 @@ package org.jetbrains.dokka.model -import com.intellij.psi.PsiNamedElement import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.doc.DocumentationNode import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.model.properties.WithExtraProperties -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor -import org.jetbrains.kotlin.load.kotlin.toSourceElement + abstract class Documentable { abstract val name: String? @@ -406,11 +404,3 @@ fun SourceSetDependent?.orEmpty(): SourceSetDependent = this ?: emptyM interface DocumentableSource { val path: String } - -class DescriptorDocumentableSource(val descriptor: DeclarationDescriptor) : DocumentableSource { - override val path = descriptor.toSourceElement.containingFile.toString() -} - -class PsiDocumentableSource(val psi: PsiNamedElement) : DocumentableSource { - override val path = psi.containingFile.virtualFile.path -} \ No newline at end of file diff --git a/core/src/main/kotlin/model/properties/PropertyContainer.kt b/core/src/main/kotlin/model/properties/PropertyContainer.kt index 107bede5..e1e0250e 100644 --- a/core/src/main/kotlin/model/properties/PropertyContainer.kt +++ b/core/src/main/kotlin/model/properties/PropertyContainer.kt @@ -1,7 +1,5 @@ package org.jetbrains.dokka.model.properties -import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull - class PropertyContainer internal constructor( @PublishedApi internal val map: Map, ExtraProperty> ) { @@ -45,7 +43,7 @@ fun C.mergeExtras(left: C, right: C): C where C : Any, C : WithExtraProperti (l.key as ExtraProperty.Key>).mergeStrategyFor(l, r) } - strategies.firstIsInstanceOrNull()?.error?.invoke() + strategies.filterIsInstance().firstOrNull()?.error?.invoke() val replaces: List> = strategies.filterIsInstance>().map { it.newProperty } diff --git a/core/src/main/kotlin/parsers/HtmlParser.kt b/core/src/main/kotlin/parsers/HtmlParser.kt deleted file mode 100644 index a0652b95..00000000 --- a/core/src/main/kotlin/parsers/HtmlParser.kt +++ /dev/null @@ -1,89 +0,0 @@ -package org.jetbrains.dokka.parsers - -import org.jetbrains.dokka.model.doc.* -import org.jetbrains.dokka.parsers.factories.DocTagsFromStringFactory -import org.jsoup.Jsoup -import org.jsoup.nodes.Node -import org.jsoup.select.NodeFilter -import org.jsoup.select.NodeTraversor - -class HtmlParser : Parser() { - - inner class NodeFilterImpl : NodeFilter { - - private val nodesCache: MutableMap> = mutableMapOf() - private var currentDepth = 0 - - fun collect(): DocTag = nodesCache[currentDepth]!![0] - - override fun tail(node: Node?, depth: Int): NodeFilter.FilterResult { - val nodeName = node!!.nodeName() - val nodeAttributes = node.attributes() - - if(nodeName in listOf("#document", "html", "head")) - return NodeFilter.FilterResult.CONTINUE - - val body: String - val params: Map - - - if(nodeName != "#text") { - body = "" - params = nodeAttributes.map { it.key to it.value }.toMap() - } else { - body = nodeAttributes["#text"] - params = emptyMap() - } - - val docNode = if(depth < currentDepth) { - DocTagsFromStringFactory.getInstance(nodeName, nodesCache.getOrDefault(currentDepth, mutableListOf()).toList(), params, body).also { - nodesCache[currentDepth] = mutableListOf() - currentDepth = depth - } - } else { - DocTagsFromStringFactory.getInstance(nodeName, emptyList(), params, body) - } - - nodesCache.getOrDefault(depth, mutableListOf()) += docNode - return NodeFilter.FilterResult.CONTINUE - } - - override fun head(node: Node?, depth: Int): NodeFilter.FilterResult { - - val nodeName = node!!.nodeName() - - if(currentDepth < depth) { - currentDepth = depth - nodesCache[currentDepth] = mutableListOf() - } - - if(nodeName in listOf("#document", "html", "head")) - return NodeFilter.FilterResult.CONTINUE - - return NodeFilter.FilterResult.CONTINUE - } - } - - - private fun htmlToDocNode(string: String): DocTag { - val document = Jsoup.parse(string) - val nodeFilterImpl = NodeFilterImpl() - NodeTraversor.filter(nodeFilterImpl, document.root()) - return nodeFilterImpl.collect() - } - - private fun replaceLinksWithHrefs(javadoc: String): String = Regex("\\{@link .*?}").replace(javadoc) { - val split = it.value.dropLast(1).split(" ") - if(split.size !in listOf(2, 3)) - return@replace it.value - if(split.size == 3) - return@replace "${split[2]}" - else - return@replace "${split[1]}" - } - - override fun parseStringToDocNode(extractedString: String) = htmlToDocNode(extractedString) - override fun preparse(text: String) = replaceLinksWithHrefs(text) -} - - diff --git a/core/src/main/kotlin/parsers/MarkdownParser.kt b/core/src/main/kotlin/parsers/MarkdownParser.kt deleted file mode 100644 index 07e1fcc3..00000000 --- a/core/src/main/kotlin/parsers/MarkdownParser.kt +++ /dev/null @@ -1,418 +0,0 @@ -package org.jetbrains.dokka.parsers - -import com.intellij.psi.PsiElement -import org.jetbrains.dokka.model.doc.* -import org.intellij.markdown.MarkdownElementTypes -import org.intellij.markdown.MarkdownTokenTypes -import org.intellij.markdown.ast.ASTNode -import org.intellij.markdown.ast.CompositeASTNode -import org.intellij.markdown.ast.LeafASTNode -import org.intellij.markdown.ast.impl.ListItemCompositeNode -import org.intellij.markdown.flavours.gfm.GFMElementTypes -import org.intellij.markdown.flavours.gfm.GFMFlavourDescriptor -import org.intellij.markdown.flavours.gfm.GFMTokenTypes -import org.jetbrains.dokka.analysis.DokkaResolutionFacade -import org.jetbrains.dokka.links.DRI -import org.jetbrains.dokka.parsers.factories.DocTagsFromIElementFactory -import org.jetbrains.dokka.utilities.DokkaLogger -import org.jetbrains.kotlin.descriptors.ClassDescriptor -import org.jetbrains.kotlin.descriptors.DeclarationDescriptor -import org.jetbrains.kotlin.idea.kdoc.resolveKDocLink -import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag -import org.jetbrains.kotlin.kdoc.psi.impl.KDocSection -import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag -import java.net.MalformedURLException -import org.intellij.markdown.parser.MarkdownParser as IntellijMarkdownParser - -class MarkdownParser( - private val resolutionFacade: DokkaResolutionFacade? = null, - private val declarationDescriptor: DeclarationDescriptor? = null, - private val logger: DokkaLogger -) : Parser() { - - inner class MarkdownVisitor(val text: String, val destinationLinksMap: Map) { - - private fun headersHandler(node: ASTNode): DocTag = - DocTagsFromIElementFactory.getInstance( - node.type, - visitNode(node.children.find { it.type == MarkdownTokenTypes.ATX_CONTENT } - ?: throw IllegalStateException("Wrong AST Tree. ATX Header does not contain expected content")).children - ) - - private fun horizontalRulesHandler(node: ASTNode): DocTag = - DocTagsFromIElementFactory.getInstance(MarkdownTokenTypes.HORIZONTAL_RULE) - - private fun emphasisHandler(node: ASTNode): DocTag = - DocTagsFromIElementFactory.getInstance( - node.type, - children = listOf(visitNode(node.children[node.children.size / 2])) - ) - - private fun blockquotesHandler(node: ASTNode): DocTag = - DocTagsFromIElementFactory.getInstance( - node.type, children = node.children - .filterIsInstance() - .evaluateChildren() - ) - - private fun listsHandler(node: ASTNode): DocTag { - - val children = node.children.filterIsInstance().flatMap { - if (it.children.last().type in listOf( - MarkdownElementTypes.ORDERED_LIST, - MarkdownElementTypes.UNORDERED_LIST - ) - ) { - val nestedList = it.children.last() - (it.children as MutableList).removeAt(it.children.lastIndex) - listOf(it, nestedList) - } else - listOf(it) - } - - return DocTagsFromIElementFactory.getInstance( - node.type, - children = - children - .map { - if (it.type == MarkdownElementTypes.LIST_ITEM) - DocTagsFromIElementFactory.getInstance( - it.type, - children = it - .children - .filterIsInstance() - .evaluateChildren() - ) - else - visitNode(it) - }, - params = - if (node.type == MarkdownElementTypes.ORDERED_LIST) { - val listNumberNode = node.children.first().children.first() - mapOf( - "start" to text.substring( - listNumberNode.startOffset, - listNumberNode.endOffset - ).trim().dropLast(1) - ) - } else - emptyMap() - ) - } - - private fun resolveDRI(mdLink: String): DRI? = - mdLink - .removePrefix("[") - .removeSuffix("]") - .let { link -> - try { - java.net.URL(link) - null - } catch (e: MalformedURLException) { - try { - if (resolutionFacade != null && declarationDescriptor != null) { - resolveKDocLink( - resolutionFacade.resolveSession.bindingContext, - resolutionFacade, - declarationDescriptor, - null, - link.split('.') - ).minBy { it is ClassDescriptor }?.let { DRI.from(it) } - } else null - } catch (e1: IllegalArgumentException) { - null - } - } - } - - private fun referenceLinksHandler(node: ASTNode): DocTag { - val linkLabel = node.children.find { it.type == MarkdownElementTypes.LINK_LABEL } - ?: throw IllegalStateException("Wrong AST Tree. Reference link does not contain expected content") - val linkText = node.children.findLast { it.type == MarkdownElementTypes.LINK_TEXT } ?: linkLabel - - val linkKey = text.substring(linkLabel.startOffset, linkLabel.endOffset) - - val link = destinationLinksMap[linkKey.toLowerCase()] ?: linkKey - - return linksHandler(linkText, link) - } - - private fun inlineLinksHandler(node: ASTNode): DocTag { - val linkText = node.children.find { it.type == MarkdownElementTypes.LINK_TEXT } - ?: throw IllegalStateException("Wrong AST Tree. Inline link does not contain expected content") - val linkDestination = node.children.find { it.type == MarkdownElementTypes.LINK_DESTINATION } - ?: throw IllegalStateException("Wrong AST Tree. Inline link does not contain expected content") - val linkTitle = node.children.find { it.type == MarkdownElementTypes.LINK_TITLE } - - val link = text.substring(linkDestination.startOffset, linkDestination.endOffset) - - return linksHandler(linkText, link, linkTitle) - } - - private fun autoLinksHandler(node: ASTNode): DocTag { - val link = text.substring(node.startOffset + 1, node.endOffset - 1) - - return linksHandler(node, link) - } - - private fun linksHandler(linkText: ASTNode, link: String, linkTitle: ASTNode? = null): DocTag { - val dri: DRI? = resolveDRI(link) - val params = if (linkTitle == null) - mapOf("href" to link) - else - mapOf("href" to link, "title" to text.substring(linkTitle.startOffset + 1, linkTitle.endOffset - 1)) - - return DocTagsFromIElementFactory.getInstance( - MarkdownElementTypes.INLINE_LINK, - params = params, - children = linkText.children.drop(1).dropLast(1).evaluateChildren(), - dri = dri - ) - } - - private fun imagesHandler(node: ASTNode): DocTag { - val linkNode = - node.children.last().children.find { it.type == MarkdownElementTypes.LINK_LABEL }!!.children[1] - val link = text.substring(linkNode.startOffset, linkNode.endOffset) - val src = mapOf("src" to link) - return DocTagsFromIElementFactory.getInstance( - node.type, - params = src, - children = listOf(visitNode(node.children.last().children.find { it.type == MarkdownElementTypes.LINK_TEXT }!!)) - ) - } - - private fun codeSpansHandler(node: ASTNode): DocTag = - DocTagsFromIElementFactory.getInstance( - node.type, - children = listOf( - DocTagsFromIElementFactory.getInstance( - MarkdownTokenTypes.TEXT, - body = text.substring(node.startOffset + 1, node.endOffset - 1).replace('\n', ' ').trimIndent() - ) - - ) - ) - - private fun codeFencesHandler(node: ASTNode): DocTag = - DocTagsFromIElementFactory.getInstance( - node.type, - children = node - .children - .dropWhile { it.type != MarkdownTokenTypes.CODE_FENCE_CONTENT } - .dropLastWhile { it.type != MarkdownTokenTypes.CODE_FENCE_CONTENT } - .map { - if (it.type == MarkdownTokenTypes.EOL) - LeafASTNode(MarkdownTokenTypes.HARD_LINE_BREAK, 0, 0) - else - it - }.evaluateChildren(), - params = node - .children - .find { it.type == MarkdownTokenTypes.FENCE_LANG } - ?.let { mapOf("lang" to text.substring(it.startOffset, it.endOffset)) } - ?: emptyMap() - ) - - private fun codeBlocksHandler(node: ASTNode): DocTag = - DocTagsFromIElementFactory.getInstance(node.type, children = node.children.evaluateChildren()) - - private fun defaultHandler(node: ASTNode): DocTag = - DocTagsFromIElementFactory.getInstance( - MarkdownElementTypes.PARAGRAPH, - children = node.children.evaluateChildren() - ) - - fun visitNode(node: ASTNode): DocTag = - when (node.type) { - MarkdownElementTypes.ATX_1, - MarkdownElementTypes.ATX_2, - MarkdownElementTypes.ATX_3, - MarkdownElementTypes.ATX_4, - MarkdownElementTypes.ATX_5, - MarkdownElementTypes.ATX_6 -> headersHandler(node) - MarkdownTokenTypes.HORIZONTAL_RULE -> horizontalRulesHandler(node) - MarkdownElementTypes.STRONG, - MarkdownElementTypes.EMPH -> emphasisHandler(node) - MarkdownElementTypes.FULL_REFERENCE_LINK, - MarkdownElementTypes.SHORT_REFERENCE_LINK -> referenceLinksHandler(node) - MarkdownElementTypes.INLINE_LINK -> inlineLinksHandler(node) - MarkdownElementTypes.AUTOLINK -> autoLinksHandler(node) - MarkdownElementTypes.BLOCK_QUOTE -> blockquotesHandler(node) - MarkdownElementTypes.UNORDERED_LIST, - MarkdownElementTypes.ORDERED_LIST -> listsHandler(node) - MarkdownElementTypes.CODE_BLOCK -> codeBlocksHandler(node) - MarkdownElementTypes.CODE_FENCE -> codeFencesHandler(node) - MarkdownElementTypes.CODE_SPAN -> codeSpansHandler(node) - MarkdownElementTypes.IMAGE -> imagesHandler(node) - MarkdownTokenTypes.HARD_LINE_BREAK -> DocTagsFromIElementFactory.getInstance(node.type) - MarkdownTokenTypes.CODE_FENCE_CONTENT, - MarkdownTokenTypes.CODE_LINE, - MarkdownTokenTypes.TEXT -> DocTagsFromIElementFactory.getInstance( - MarkdownTokenTypes.TEXT, - body = text - .substring(node.startOffset, node.endOffset).transform() - ) - MarkdownElementTypes.MARKDOWN_FILE -> if (node.children.size == 1) visitNode(node.children.first()) else defaultHandler( - node - ) - GFMElementTypes.STRIKETHROUGH -> DocTagsFromIElementFactory.getInstance( - GFMElementTypes.STRIKETHROUGH, - body = text - .substring(node.startOffset, node.endOffset).transform() - ) - GFMElementTypes.TABLE -> DocTagsFromIElementFactory.getInstance( - GFMElementTypes.TABLE, - children = node.children.filterTabSeparators().evaluateChildren() - ) - GFMElementTypes.HEADER -> DocTagsFromIElementFactory.getInstance( - GFMElementTypes.HEADER, - children = node.children.filterTabSeparators().evaluateChildren() - ) - GFMElementTypes.ROW -> DocTagsFromIElementFactory.getInstance( - GFMElementTypes.ROW, - children = node.children.filterTabSeparators().evaluateChildren() - ) - else -> defaultHandler(node) - } - - private fun List.filterTabSeparators() = - this.filterNot { it.type == GFMTokenTypes.TABLE_SEPARATOR } - - private fun List.evaluateChildren(): List = - this.removeUselessTokens().mergeLeafASTNodes().map { visitNode(it) } - - private fun List.removeUselessTokens(): List = - this.filterIndexed { index, node -> - !(node.type == MarkdownElementTypes.LINK_DEFINITION || ( - node.type == MarkdownTokenTypes.EOL && - this.getOrNull(index - 1)?.type == MarkdownTokenTypes.HARD_LINE_BREAK - )) - } - - private val notLeafNodes = listOf(MarkdownTokenTypes.HORIZONTAL_RULE, MarkdownTokenTypes.HARD_LINE_BREAK) - - private fun List.isNotLeaf(index: Int): Boolean = - if (index in 0..this.lastIndex) - (this[index] is CompositeASTNode) || this[index].type in notLeafNodes - else - false - - private fun List.mergeLeafASTNodes(): List { - val children: MutableList = mutableListOf() - var index = 0 - while (index <= this.lastIndex) { - if (this.isNotLeaf(index)) { - children += this[index] - } else { - val startOffset = this[index].startOffset - while (index < this.lastIndex) { - if (this.isNotLeaf(index + 1) || this[index + 1].startOffset != this[index].endOffset) { - val endOffset = this[index].endOffset - if (text.substring(startOffset, endOffset).transform().trim().isNotEmpty()) - children += LeafASTNode(MarkdownTokenTypes.TEXT, startOffset, endOffset) - break - } - index++ - } - if (index == this.lastIndex) { - val endOffset = this[index].endOffset - if (text.substring(startOffset, endOffset).transform().trim().isNotEmpty()) - children += LeafASTNode(MarkdownTokenTypes.TEXT, startOffset, endOffset) - } - } - index++ - } - return children - } - - private fun String.transform() = this - .replace(Regex("\n\n+"), "") // Squashing new lines between paragraphs - .replace(Regex("\n"), " ") - .replace(Regex(" >+ +"), " ") // Replacement used in blockquotes, get rid of garbage - } - - - private fun getAllDestinationLinks(text: String, node: ASTNode): List> = - node.children - .filter { it.type == MarkdownElementTypes.LINK_DEFINITION } - .map { - text.substring(it.children[0].startOffset, it.children[0].endOffset).toLowerCase() to - text.substring(it.children[2].startOffset, it.children[2].endOffset) - } + - node.children.filterIsInstance().flatMap { getAllDestinationLinks(text, it) } - - - private fun markdownToDocNode(text: String): DocTag { - - val flavourDescriptor = GFMFlavourDescriptor() - val markdownAstRoot: ASTNode = IntellijMarkdownParser(flavourDescriptor).buildMarkdownTreeFromString(text) - - return MarkdownVisitor(text, getAllDestinationLinks(text, markdownAstRoot).toMap()).visitNode(markdownAstRoot) - } - - override fun parseStringToDocNode(extractedString: String) = markdownToDocNode(extractedString) - override fun preparse(text: String) = text - - private fun findParent(kDoc: PsiElement): PsiElement = - if (kDoc is KDocSection) findParent(kDoc.parent) else kDoc - - private fun getAllKDocTags(kDocImpl: PsiElement): List = - kDocImpl.children.filterIsInstance().filterNot { it is KDocSection } + kDocImpl.children.flatMap { - getAllKDocTags( - it - ) - } - - fun parseFromKDocTag(kDocTag: KDocTag?): DocumentationNode { - return if (kDocTag == null) - DocumentationNode(emptyList()) - else - DocumentationNode( - (listOf(kDocTag) + getAllKDocTags(findParent(kDocTag))).map { - when (it.knownTag) { - null -> if (it.name == null) Description(parseStringToDocNode(it.getContent())) else CustomTagWrapper( - parseStringToDocNode(it.getContent()), - it.name!! - ) - KDocKnownTag.AUTHOR -> Author(parseStringToDocNode(it.getContent())) - KDocKnownTag.THROWS -> Throws( - parseStringToDocNode(it.getContent()), - it.getSubjectName().orEmpty() - ) - KDocKnownTag.EXCEPTION -> Throws( - parseStringToDocNode(it.getContent()), - it.getSubjectName().orEmpty() - ) - KDocKnownTag.PARAM -> Param( - parseStringToDocNode(it.getContent()), - it.getSubjectName().orEmpty() - ) - KDocKnownTag.RECEIVER -> Receiver(parseStringToDocNode(it.getContent())) - KDocKnownTag.RETURN -> Return(parseStringToDocNode(it.getContent())) - KDocKnownTag.SEE -> See( - parseStringToDocNode(it.getContent()), - it.getSubjectName().orEmpty(), - parseStringToDocNode("[${it.getSubjectName()}]") - .let { - val link = it.children[0] - if (link is DocumentationLink) link.dri - else null - } - ) - KDocKnownTag.SINCE -> Since(parseStringToDocNode(it.getContent())) - KDocKnownTag.CONSTRUCTOR -> Constructor(parseStringToDocNode(it.getContent())) - KDocKnownTag.PROPERTY -> Property( - parseStringToDocNode(it.getContent()), - it.getSubjectName().orEmpty() - ) - KDocKnownTag.SAMPLE -> Sample( - parseStringToDocNode(it.getContent()), - it.getSubjectName().orEmpty() - ) - KDocKnownTag.SUPPRESS -> Suppress(parseStringToDocNode(it.getContent())) - } - } - ) - } -} \ No newline at end of file diff --git a/core/src/main/kotlin/parsers/Parser.kt b/core/src/main/kotlin/parsers/Parser.kt deleted file mode 100644 index 05adc4d5..00000000 --- a/core/src/main/kotlin/parsers/Parser.kt +++ /dev/null @@ -1,42 +0,0 @@ -package org.jetbrains.dokka.parsers - -import org.jetbrains.dokka.model.doc.* - -abstract class Parser { - - abstract fun parseStringToDocNode(extractedString: String): DocTag - abstract fun preparse(text: String): String - - fun parse(text: String): DocumentationNode { - - val list = jkdocToListOfPairs(preparse(text)) - - val mappedList: List = list.map { - when(it.first) { - "description" -> Description(parseStringToDocNode(it.second)) - "author" -> Author(parseStringToDocNode(it.second)) - "version" -> Version(parseStringToDocNode(it.second)) - "since" -> Since(parseStringToDocNode(it.second)) - "see" -> See(parseStringToDocNode(it.second.substringAfter(' ')), it.second.substringBefore(' '), null) - "param" -> Param(parseStringToDocNode(it.second.substringAfter(' ')), it.second.substringBefore(' ')) - "property" -> Property(parseStringToDocNode(it.second.substringAfter(' ')), it.second.substringBefore(' ')) - "return" -> Return(parseStringToDocNode(it.second)) - "constructor" -> Constructor(parseStringToDocNode(it.second)) - "receiver" -> Receiver(parseStringToDocNode(it.second)) - "throws", "exception" -> Throws(parseStringToDocNode(it.second.substringAfter(' ')), it.second.substringBefore(' ')) - "deprecated" -> Deprecated(parseStringToDocNode(it.second)) - "sample" -> Sample(parseStringToDocNode(it.second.substringAfter(' ')), it.second.substringBefore(' ')) - "suppress" -> Suppress(parseStringToDocNode(it.second)) - else -> CustomTagWrapper(parseStringToDocNode(it.second), it.first) - } - } - return DocumentationNode(mappedList) - } - - private fun jkdocToListOfPairs(javadoc: String): List> = - "description $javadoc" - .split("\n@") - .map { - it.substringBefore(' ') to it.substringAfter(' ') - } -} \ No newline at end of file diff --git a/core/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt b/core/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt deleted file mode 100644 index dda6c627..00000000 --- a/core/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt +++ /dev/null @@ -1,43 +0,0 @@ -package org.jetbrains.dokka.parsers.factories - -import org.jetbrains.dokka.model.doc.* -import org.intellij.markdown.IElementType -import org.intellij.markdown.MarkdownElementTypes -import org.intellij.markdown.MarkdownTokenTypes -import org.intellij.markdown.flavours.gfm.GFMElementTypes -import org.jetbrains.dokka.links.DRI -import java.lang.NullPointerException - -object DocTagsFromIElementFactory { - fun getInstance(type: IElementType, children: List = emptyList(), params: Map = emptyMap(), body: String? = null, dri: DRI? = null) = - when(type) { - MarkdownElementTypes.SHORT_REFERENCE_LINK, - MarkdownElementTypes.FULL_REFERENCE_LINK, - MarkdownElementTypes.INLINE_LINK -> if(dri == null) A(children, params) else DocumentationLink(dri, children, params) - MarkdownElementTypes.STRONG -> B(children, params) - MarkdownElementTypes.BLOCK_QUOTE -> BlockQuote(children, params) - MarkdownElementTypes.CODE_SPAN, - MarkdownElementTypes.CODE_BLOCK, - MarkdownElementTypes.CODE_FENCE -> Code(children, params) - MarkdownElementTypes.ATX_1 -> H1(children, params) - MarkdownElementTypes.ATX_2 -> H2(children, params) - MarkdownElementTypes.ATX_3 -> H3(children, params) - MarkdownElementTypes.ATX_4 -> H4(children, params) - MarkdownElementTypes.ATX_5 -> H5(children, params) - MarkdownElementTypes.ATX_6 -> H6(children, params) - MarkdownElementTypes.EMPH -> I(children, params) - MarkdownElementTypes.IMAGE -> Img(children, params) - MarkdownElementTypes.LIST_ITEM -> Li(children, params) - MarkdownElementTypes.ORDERED_LIST -> Ol(children, params) - MarkdownElementTypes.UNORDERED_LIST -> Ul(children, params) - MarkdownElementTypes.PARAGRAPH -> P(children, params) - MarkdownTokenTypes.TEXT -> Text(body ?: throw NullPointerException("Text body should be at least empty string passed to DocNodes factory!"), children, params ) - MarkdownTokenTypes.HORIZONTAL_RULE -> HorizontalRule - MarkdownTokenTypes.HARD_LINE_BREAK -> Br - GFMElementTypes.STRIKETHROUGH -> Strikethrough(children, params) - GFMElementTypes.TABLE -> Table(children, params) - GFMElementTypes.HEADER -> Th(children, params) - GFMElementTypes.ROW -> Tr(children, params) - else -> CustomDocTag(children, params) - } -} \ No newline at end of file diff --git a/core/src/main/kotlin/parsers/factories/DocTagsFromStringFactory.kt b/core/src/main/kotlin/parsers/factories/DocTagsFromStringFactory.kt deleted file mode 100644 index 3425f52e..00000000 --- a/core/src/main/kotlin/parsers/factories/DocTagsFromStringFactory.kt +++ /dev/null @@ -1,77 +0,0 @@ -package org.jetbrains.dokka.parsers.factories - -import org.jetbrains.dokka.model.doc.* -import org.jetbrains.dokka.links.DRI -import java.lang.NullPointerException - -object DocTagsFromStringFactory { - fun getInstance(name: String, children: List = emptyList(), params: Map = emptyMap(), body: String? = null, dri: DRI? = null) = - when(name) { - "a" -> A(children, params) - "big" -> Big(children, params) - "b" -> B(children, params) - "blockquote" -> BlockQuote(children, params) - "br" -> Br - "cite" -> Cite(children, params) - "code" -> Code(children, params) - "dd" -> Dd(children, params) - "dfn" -> Dfn(children, params) - "dir" -> Dir(children, params) - "div" -> Div(children, params) - "dl" -> Dl(children, params) - "dt" -> Dt(children, params) - "Em" -> Em(children, params) - "font" -> Font(children, params) - "footer" -> Footer(children, params) - "frame" -> Frame(children, params) - "frameset" -> FrameSet(children, params) - "h1" -> H1(children, params) - "h2" -> H2(children, params) - "h3" -> H3(children, params) - "h4" -> H4(children, params) - "h5" -> H5(children, params) - "h6" -> H6(children, params) - "head" -> Head(children, params) - "header" -> Header(children, params) - "html" -> Html(children, params) - "i" -> I(children, params) - "iframe" -> IFrame(children, params) - "img" -> Img(children, params) - "input" -> Input(children, params) - "li" -> Li(children, params) - "link" -> Link(children, params) - "listing" -> Listing(children, params) - "main" -> Main(children, params) - "menu" -> Menu(children, params) - "meta" -> Meta(children, params) - "nav" -> Nav(children, params) - "noframes" -> NoFrames(children, params) - "noscript" -> NoScript(children, params) - "ol" -> Ol(children, params) - "p" -> P(children, params) - "pre" -> Pre(children, params) - "script" -> Script(children, params) - "section" -> Section(children, params) - "small" -> Small(children, params) - "span" -> Span(children, params) - "strong" -> Strong(children, params) - "sub" -> Sub(children, params) - "sup" -> Sup(children, params) - "table" -> Table(children, params) - "#text" -> Text(body ?: throw NullPointerException("Text body should be at least empty string passed to DocNodes factory!"), children, params) - "tBody" -> TBody(children, params) - "td" -> Td(children, params) - "tFoot" -> TFoot(children, params) - "th" -> Th(children, params) - "tHead" -> THead(children, params) - "title" -> Title(children, params) - "tr" -> Tr(children, params) - "tt" -> Tt(children, params) - "u" -> U(children, params) - "ul" -> Ul(children, params) - "var" -> Var(children, params) - "documentationlink" -> DocumentationLink(dri ?: throw NullPointerException("DRI cannot be passed null while constructing documentation link!"), children, params) - "hr" -> HorizontalRule - else -> CustomDocTag(children, params) - } -} \ No newline at end of file diff --git a/core/src/main/kotlin/plugability/DokkaContext.kt b/core/src/main/kotlin/plugability/DokkaContext.kt index 692803dd..a2ff26c7 100644 --- a/core/src/main/kotlin/plugability/DokkaContext.kt +++ b/core/src/main/kotlin/plugability/DokkaContext.kt @@ -1,9 +1,7 @@ package org.jetbrains.dokka.plugability import org.jetbrains.dokka.DokkaConfiguration -import org.jetbrains.dokka.EnvironmentAndFacade import org.jetbrains.dokka.model.SourceSetCache -import org.jetbrains.dokka.model.SourceSetData import org.jetbrains.dokka.utilities.DokkaLogger import java.io.File import java.net.URLClassLoader @@ -23,7 +21,6 @@ interface DokkaContext { val sourceSetCache: SourceSetCache val logger: DokkaLogger val configuration: DokkaConfiguration - val platforms: Map val unusedPoints: Collection> @@ -31,11 +28,10 @@ interface DokkaContext { fun create( configuration: DokkaConfiguration, logger: DokkaLogger, - sourceSets: Map, sourceSetsCache: SourceSetCache, pluginOverrides: List ): DokkaContext = - DokkaContextConfigurationImpl(logger, configuration, sourceSets, sourceSetsCache).apply { + DokkaContextConfigurationImpl(logger, configuration, sourceSetsCache).apply { // File(it.path) is a workaround for an incorrect filesystem in a File instance returned by Gradle. configuration.pluginsClasspath.map { File(it.path).toURI().toURL() } .toTypedArray() @@ -59,7 +55,6 @@ interface DokkaContextConfiguration { private class DokkaContextConfigurationImpl( override val logger: DokkaLogger, override val configuration: DokkaConfiguration, - override val platforms: Map, override val sourceSetCache: SourceSetCache ) : DokkaContext, DokkaContextConfiguration { private val plugins = mutableMapOf, DokkaPlugin>() diff --git a/core/src/main/kotlin/plugability/DokkaPlugin.kt b/core/src/main/kotlin/plugability/DokkaPlugin.kt index 24f417d8..fe472b12 100644 --- a/core/src/main/kotlin/plugability/DokkaPlugin.kt +++ b/core/src/main/kotlin/plugability/DokkaPlugin.kt @@ -2,7 +2,6 @@ package org.jetbrains.dokka.plugability import com.google.gson.Gson import org.jetbrains.dokka.DokkaConfiguration -import org.jetbrains.kotlin.utils.addToStdlib.cast import kotlin.properties.ReadOnlyProperty import kotlin.reflect.KProperty import kotlin.reflect.KProperty1 @@ -60,8 +59,9 @@ interface ConfigurableBlock inline fun Configurable.pluginConfiguration(block: T.() -> Unit) { val instance = T::class.createInstance().apply(block) - pluginsConfiguration.cast>()[P::class.qualifiedName!!] = - Gson().toJson(instance, T::class.java) + + val mutablePluginsConfiguration = pluginsConfiguration as MutableMap + mutablePluginsConfiguration[P::class.qualifiedName!!] = Gson().toJson(instance, T::class.java) } inline fun P.query(extension: P.() -> ExtensionPoint): List = diff --git a/core/src/main/kotlin/utilities/cast.kt b/core/src/main/kotlin/utilities/cast.kt new file mode 100644 index 00000000..d4a8d73d --- /dev/null +++ b/core/src/main/kotlin/utilities/cast.kt @@ -0,0 +1,5 @@ +package org.jetbrains.dokka.utilities + +inline fun Any.cast(): T { + return this as T +} diff --git a/coreDependencies/build.gradle.kts b/coreDependencies/build.gradle.kts deleted file mode 100644 index 9af82d9f..00000000 --- a/coreDependencies/build.gradle.kts +++ /dev/null @@ -1,65 +0,0 @@ -import org.jetbrains.configureBintrayPublication - -plugins { - id("com.github.johnrengelman.shadow") - `maven-publish` - id("com.jfrog.bintray") -} - -val intellijCore: Configuration by configurations.creating - -repositories { - maven(url = "https://www.jetbrains.com/intellij-repository/snapshots") - maven(url = "https://www.jetbrains.com/intellij-repository/releases") - maven(url = "https://kotlin.bintray.com/kotlin-plugin") -} - -fun intellijCoreAnalysis() = zipTree(intellijCore.singleFile).matching { - include("intellij-core-analysis.jar") -} - -dependencies { - val idea_version: String by project - intellijCore("com.jetbrains.intellij.idea:intellij-core:$idea_version") - val kotlin_plugin_version: String by project - implementation(intellijCoreAnalysis()) - implementation("org.jetbrains.kotlin:kotlin-plugin-ij193:$kotlin_plugin_version") { - //TODO: parametrize ij version after 1.3.70 - isTransitive = false - } - implementation("org.jetbrains:markdown:0.1.41") { - because("it's published only on bintray") - } - implementation("org.jetbrains.kotlin:ide-common-ij193:$kotlin_plugin_version") - implementation(kotlin("stdlib-jdk8")) - implementation(kotlin("reflect")) -} - -tasks { - shadowJar { - val dokka_version: String by project - archiveFileName.set("dokka-dependencies-$dokka_version.jar") - archiveClassifier.set("") - - exclude("colorScheme/**") - exclude("fileTemplates/**") - exclude("inspectionDescriptions/**") - exclude("intentionDescriptions/**") - exclude("tips/**") - exclude("messages/**") - exclude("src/**") - exclude("**/*.kotlin_metadata") - exclude("**/*.kotlin_builtins") - } -} - -publishing { - publications { - register("dokkaCoreDependencies") { - artifactId = "dokka-core-dependencies" - project.shadow.component(this) - } - } -} - -configureBintrayPublication("dokkaCoreDependencies") diff --git a/integration-tests/gradle-integration-tests/build.gradle.kts b/integration-tests/gradle-integration-tests/build.gradle.kts index 7fc945b5..7cb96738 100644 --- a/integration-tests/gradle-integration-tests/build.gradle.kts +++ b/integration-tests/gradle-integration-tests/build.gradle.kts @@ -14,7 +14,7 @@ dependencies { testImplementation(gradleTestKit()) dokkaPlugin(project(path = ":runners:gradle-plugin")) - dokkaCore(project(path = ":coreDependencies", configuration = "shadow")) + dokkaCore(project(path = ":core:dependencies", configuration = "shadow")) dokkaCore(project(path = ":core")) kotlinGradle("org.jetbrains.kotlin:kotlin-gradle-plugin") } diff --git a/kotlin-analysis/build.gradle.kts b/kotlin-analysis/build.gradle.kts new file mode 100644 index 00000000..67480f82 --- /dev/null +++ b/kotlin-analysis/build.gradle.kts @@ -0,0 +1,27 @@ +import org.jetbrains.configureBintrayPublication + +plugins { + id("com.github.johnrengelman.shadow") + `maven-publish` + id("com.jfrog.bintray") +} + +dependencies { + compileOnly(project(":core")) + + val kotlin_version: String by project + api("org.jetbrains.kotlin:kotlin-compiler:$kotlin_version") + + api(project(":kotlin-analysis:dependencies", configuration = "shadow")) +} + +publishing { + publications { + register("analysis") { + artifactId = "dokka-analysis" + from(components["java"]) + } + } +} + +configureBintrayPublication("analysis") diff --git a/kotlin-analysis/dependencies/build.gradle.kts b/kotlin-analysis/dependencies/build.gradle.kts new file mode 100644 index 00000000..5bba6422 --- /dev/null +++ b/kotlin-analysis/dependencies/build.gradle.kts @@ -0,0 +1,67 @@ +import org.jetbrains.configureBintrayPublication + + +plugins { + id("com.github.johnrengelman.shadow") + `maven-publish` + id("com.jfrog.bintray") +} + +repositories { + maven(url = "https://www.jetbrains.com/intellij-repository/snapshots") + maven(url = "https://www.jetbrains.com/intellij-repository/releases") + maven(url = "https://kotlin.bintray.com/kotlin-plugin") +} + +val intellijCore: Configuration by configurations.creating + +fun intellijCoreAnalysis() = zipTree(intellijCore.singleFile).matching { + include("intellij-core-analysis.jar") +} + +dependencies { + val kotlin_plugin_version: String by project + api("org.jetbrains.kotlin:ide-common-ij193:$kotlin_plugin_version") + api("org.jetbrains.kotlin:kotlin-plugin-ij193:$kotlin_plugin_version") { + //TODO: parametrize ij version after 1.3.70 + isTransitive = false + } + + val idea_version: String by project + intellijCore("com.jetbrains.intellij.idea:intellij-core:$idea_version") + implementation(intellijCoreAnalysis()) + + implementation("org.jetbrains:markdown:0.1.41") { + because("it's published only on bintray") + } +} + +tasks { + shadowJar { + val dokka_version: String by project + archiveFileName.set("dokka-kotlin-analysis-dependencies-$dokka_version.jar") + archiveClassifier.set("") + + exclude("colorScheme/**") + exclude("fileTemplates/**") + exclude("inspectionDescriptions/**") + exclude("intentionDescriptions/**") + exclude("tips/**") + exclude("messages/**") + exclude("src/**") + exclude("**/*.kotlin_metadata") + exclude("**/*.kotlin_builtins") + } +} + + +publishing { + publications { + register("kotlin-analysis-dependencies") { + artifactId = "kotlin-analysis-dependencies" + project.shadow.component(this) + } + } +} + +configureBintrayPublication("kotlin-analysis-dependencies") diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/AnalysisEnvironment.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/AnalysisEnvironment.kt new file mode 100644 index 00000000..7836bde9 --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/AnalysisEnvironment.kt @@ -0,0 +1,636 @@ +package org.jetbrains.dokka.analysis + +import com.google.common.collect.ImmutableMap +import com.intellij.core.CoreApplicationEnvironment +import com.intellij.core.CoreModuleManager +import com.intellij.mock.MockComponentManager +import com.intellij.openapi.Disposable +import com.intellij.openapi.extensions.Extensions +import com.intellij.openapi.module.Module +import com.intellij.openapi.module.ModuleManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.roots.OrderEnumerationHandler +import com.intellij.openapi.roots.ProjectFileIndex +import com.intellij.openapi.roots.ProjectRootManager +import com.intellij.openapi.util.Disposer +import com.intellij.openapi.vfs.StandardFileSystems +import com.intellij.psi.PsiElement +import com.intellij.psi.search.GlobalSearchScope +import org.jetbrains.dokka.Platform +import org.jetbrains.kotlin.analyzer.* +import org.jetbrains.kotlin.analyzer.common.CommonAnalysisParameters +import org.jetbrains.kotlin.analyzer.common.CommonPlatformAnalyzerServices +import org.jetbrains.kotlin.analyzer.common.CommonResolverForModuleFactory +import org.jetbrains.kotlin.builtins.DefaultBuiltIns +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.builtins.jvm.JvmBuiltIns +import org.jetbrains.kotlin.caches.resolve.* +import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys +import org.jetbrains.kotlin.cli.common.config.ContentRoot +import org.jetbrains.kotlin.cli.common.config.KotlinSourceRoot +import org.jetbrains.kotlin.cli.common.config.addKotlinSourceRoot +import org.jetbrains.kotlin.cli.common.messages.MessageCollector +import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles +import org.jetbrains.kotlin.cli.jvm.compiler.JvmPackagePartProvider +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM +import org.jetbrains.kotlin.cli.jvm.config.* +import org.jetbrains.kotlin.cli.jvm.index.JavaRoot +import org.jetbrains.kotlin.config.* +import org.jetbrains.kotlin.container.getService +import org.jetbrains.kotlin.container.tryGetService +import org.jetbrains.kotlin.context.ProjectContext +import org.jetbrains.kotlin.context.withModule +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.ModuleDescriptor +import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl +import org.jetbrains.kotlin.ide.konan.analyzer.NativeResolverForModuleFactory +import org.jetbrains.kotlin.js.config.JSConfigurationKeys +import org.jetbrains.kotlin.js.resolve.JsPlatformAnalyzerServices +import org.jetbrains.kotlin.library.impl.createKotlinLibrary +import org.jetbrains.kotlin.load.java.structure.impl.JavaClassImpl +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.platform.CommonPlatforms +import org.jetbrains.kotlin.platform.TargetPlatform +import org.jetbrains.kotlin.platform.js.JsPlatforms +import org.jetbrains.kotlin.platform.jvm.JvmPlatforms +import org.jetbrains.kotlin.platform.jvm.JvmPlatforms.unspecifiedJvmPlatform +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.BindingTrace +import org.jetbrains.kotlin.resolve.CompilerEnvironment +import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices +import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics +import org.jetbrains.kotlin.resolve.jvm.JvmPlatformParameters +import org.jetbrains.kotlin.resolve.jvm.JvmResolverForModuleFactory +import org.jetbrains.kotlin.resolve.jvm.platform.JvmPlatformAnalyzerServices +import org.jetbrains.kotlin.resolve.konan.platform.NativePlatformAnalyzerServices +import org.jetbrains.kotlin.resolve.lazy.ResolveSession +import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.util.slicedMap.ReadOnlySlice +import org.jetbrains.kotlin.util.slicedMap.WritableSlice +import org.jetbrains.kotlin.idea.resolve.ResolutionFacade +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode +import org.jetbrains.kotlin.descriptors.konan.KlibModuleOrigin +import org.jetbrains.kotlin.extensions.ApplicationExtensionDescriptor +import org.jetbrains.kotlin.ide.konan.NativePlatformKindResolution +import org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaClass +import org.jetbrains.kotlin.platform.IdePlatformKind +import org.jetbrains.kotlin.platform.impl.CommonIdePlatformKind +import org.jetbrains.kotlin.platform.impl.JsIdePlatformKind +import org.jetbrains.kotlin.platform.impl.JvmIdePlatformKind +import org.jetbrains.kotlin.platform.impl.NativeIdePlatformKind +import org.jetbrains.kotlin.platform.konan.NativePlatforms +import java.io.File + +const val JAR_SEPARATOR = "!/" +const val KLIB_EXTENSION = "klib" + +/** + * Kotlin as a service entry point + * + * Configures environment, analyses files and provides facilities to perform code processing without emitting bytecode + * + * $messageCollector: required by compiler infrastructure and will receive all compiler messages + * $body: optional and can be used to configure environment without creating local variable + */ +class AnalysisEnvironment(val messageCollector: MessageCollector, val analysisPlatform: Platform) : Disposable { + val configuration = CompilerConfiguration() + + init { + configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector) + } + + fun createCoreEnvironment(): KotlinCoreEnvironment { + System.setProperty("idea.io.use.nio2", "true") + + val configFiles = when (analysisPlatform) { + Platform.jvm, Platform.common -> EnvironmentConfigFiles.JVM_CONFIG_FILES + Platform.native -> EnvironmentConfigFiles.NATIVE_CONFIG_FILES + Platform.js -> EnvironmentConfigFiles.JS_CONFIG_FILES + } + + val environment = KotlinCoreEnvironment.createForProduction(this, configuration, configFiles) + val projectComponentManager = environment.project as MockComponentManager + + val projectFileIndex = CoreProjectFileIndex( + environment.project, + environment.configuration.getList(CLIConfigurationKeys.CONTENT_ROOTS) + ) + + val moduleManager = object : CoreModuleManager(environment.project, this) { + override fun getModules(): Array = arrayOf(projectFileIndex.module) + } + + CoreApplicationEnvironment.registerComponentInstance( + projectComponentManager.picoContainer, + ModuleManager::class.java, moduleManager + ) + + CoreApplicationEnvironment.registerExtensionPoint( + Extensions.getRootArea(), + OrderEnumerationHandler.EP_NAME, OrderEnumerationHandler.Factory::class.java + ) + + projectComponentManager.registerService( + ProjectFileIndex::class.java, + projectFileIndex + ) + + projectComponentManager.registerService( + ProjectRootManager::class.java, + CoreProjectRootManager(projectFileIndex) + ) + + registerExtensionPoint( + ApplicationExtensionDescriptor("org.jetbrains.kotlin.idePlatformKind", IdePlatformKind::class.java), + listOf( + CommonIdePlatformKind, + JvmIdePlatformKind, + JsIdePlatformKind, + NativeIdePlatformKind + ) + ) + + registerExtensionPoint( + IdePlatformKindResolution, + listOf( + CommonPlatformKindResolution(), + JvmPlatformKindResolution(), + JsPlatformKindResolution(), + NativePlatformKindResolution() + ) + ) + + return environment + } + + private fun createSourceModuleSearchScope(project: Project, sourceFiles: List): GlobalSearchScope = + when (analysisPlatform) { + Platform.jvm -> TopDownAnalyzerFacadeForJVM.newModuleSearchScope(project, sourceFiles) + Platform.js, Platform.common, Platform.native -> GlobalSearchScope.filesScope( + project, + sourceFiles.map { it.virtualFile }.toSet() + ) + } + + fun createResolutionFacade(environment: KotlinCoreEnvironment): Pair { + val projectContext = ProjectContext(environment.project, "Dokka") + val sourceFiles = environment.getSourceFiles() + + + val targetPlatform = when (analysisPlatform) { + Platform.js -> JsPlatforms.defaultJsPlatform + Platform.common -> CommonPlatforms.defaultCommonPlatform + Platform.native -> NativePlatforms.unspecifiedNativePlatform + Platform.jvm -> JvmPlatforms.defaultJvmPlatform + } + + val nativeLibraries = classpath.filter { it.extension == KLIB_EXTENSION } + .map { createNativeLibraryModuleInfo(it) } + + val library = object : LibraryModuleInfo { + override val analyzerServices: PlatformDependentAnalyzerServices = + analysisPlatform.analyzerServices() + override val name: Name = Name.special("") + override val platform: TargetPlatform = targetPlatform + override fun dependencies(): List = listOf(this) + override fun getLibraryRoots(): Collection = + classpath.filterNot { it.extension == KLIB_EXTENSION }.map { it.absolutePath } + } + + val module = object : ModuleInfo { + override val analyzerServices: PlatformDependentAnalyzerServices = + analysisPlatform.analyzerServices() + override val name: Name = Name.special("") + override val platform: TargetPlatform = targetPlatform + override fun dependencies(): List = listOf(this, library) + nativeLibraries + } + + val sourcesScope = createSourceModuleSearchScope(environment.project, sourceFiles) + val modulesContent: (ModuleInfo) -> ModuleContent = { + when (it) { + library -> ModuleContent(it, emptyList(), GlobalSearchScope.notScope(sourcesScope)) + module -> ModuleContent(it, emptyList(), GlobalSearchScope.allScope(environment.project)) + in nativeLibraries -> ModuleContent(it, emptyList(), GlobalSearchScope.notScope(sourcesScope)) + else -> throw IllegalArgumentException("Unexpected module info") + } + } + + var builtIns: JvmBuiltIns? = null + + val resolverForProject = when (analysisPlatform) { + Platform.jvm -> { + builtIns = JvmBuiltIns( + projectContext.storageManager, + JvmBuiltIns.Kind.FROM_CLASS_LOADER + ) // TODO we should use FROM_DEPENDENCIES + createJvmResolverForProject( + projectContext, + module, + library, + modulesContent, + sourcesScope, + builtIns + ) + } + Platform.common -> createCommonResolverForProject( + projectContext, + module, + library, + modulesContent, + environment + ) + Platform.js -> createJsResolverForProject(projectContext, module, library, modulesContent) + Platform.native -> createNativeResolverForProject(projectContext, module, library, modulesContent) + + } + val libraryModuleDescriptor = resolverForProject.descriptorForModule(library) + val moduleDescriptor = resolverForProject.descriptorForModule(module) + builtIns?.initialize(moduleDescriptor, true) + + val resolverForLibrary = + resolverForProject.resolverForModule(library) // Required before module to initialize library properly + val resolverForModule = resolverForProject.resolverForModule(module) + val libraryResolutionFacade = + DokkaResolutionFacade( + environment.project, + libraryModuleDescriptor, + resolverForLibrary + ) + val created = + DokkaResolutionFacade( + environment.project, + moduleDescriptor, + resolverForModule + ) + val projectComponentManager = environment.project as MockComponentManager + projectComponentManager.registerService(KotlinCacheService::class.java, + CoreKotlinCacheService(created) + ) + + return created to libraryResolutionFacade + } + + private fun Platform.analyzerServices() = when (this) { + Platform.js -> JsPlatformAnalyzerServices + Platform.common -> CommonPlatformAnalyzerServices + Platform.native -> NativePlatformAnalyzerServices + Platform.jvm -> JvmPlatformAnalyzerServices + } + + private fun createNativeLibraryModuleInfo(libraryFile: File): LibraryModuleInfo { + val kotlinLibrary = createKotlinLibrary(org.jetbrains.kotlin.konan.file.File(libraryFile.absolutePath), "",false) + return object : LibraryModuleInfo { + override val analyzerServices: PlatformDependentAnalyzerServices = + analysisPlatform.analyzerServices() + override val name: Name = Name.special("") + override val platform: TargetPlatform = NativePlatforms.unspecifiedNativePlatform + override fun dependencies(): List = listOf(this) + override fun getLibraryRoots(): Collection = listOf(libraryFile.absolutePath) + override val capabilities: Map, Any?> + get() = super.capabilities + (KlibModuleOrigin.CAPABILITY to kotlinLibrary) + } + } + + private fun createCommonResolverForProject( + projectContext: ProjectContext, + module: ModuleInfo, + library: LibraryModuleInfo, + modulesContent: (ModuleInfo) -> ModuleContent, + environment: KotlinCoreEnvironment + ): ResolverForProject { + return object : AbstractResolverForProject( + "Dokka", + projectContext, + modules = listOf(module, library) + ) { + override fun sdkDependency(module: ModuleInfo): ModuleInfo? = null + + override fun modulesContent(module: ModuleInfo): ModuleContent = modulesContent(module) + + override fun builtInsForModule(module: ModuleInfo): KotlinBuiltIns = DefaultBuiltIns.Instance + + override fun createResolverForModule( + descriptor: ModuleDescriptor, + moduleInfo: ModuleInfo + ): ResolverForModule = + CommonResolverForModuleFactory( + CommonAnalysisParameters { content -> + environment.createPackagePartProvider(content.moduleContentScope) + }, + CompilerEnvironment, + unspecifiedJvmPlatform, + true + ).createResolverForModule( + descriptor as ModuleDescriptorImpl, + projectContext.withModule(descriptor), + modulesContent(moduleInfo), + this, + LanguageVersionSettingsImpl.DEFAULT + ) + } + } + + private fun createJsResolverForProject( + projectContext: ProjectContext, + module: ModuleInfo, + library: LibraryModuleInfo, + modulesContent: (ModuleInfo) -> ModuleContent + ): ResolverForProject { + return object : AbstractResolverForProject( + "Dokka", + projectContext, + modules = listOf(module, library) + ) { + override fun modulesContent(module: ModuleInfo): ModuleContent = modulesContent(module) + override fun createResolverForModule( + descriptor: ModuleDescriptor, + moduleInfo: ModuleInfo + ): ResolverForModule = JsResolverForModuleFactory( + CompilerEnvironment + ).createResolverForModule( + descriptor as ModuleDescriptorImpl, + projectContext.withModule(descriptor), + modulesContent(moduleInfo), + this, + LanguageVersionSettingsImpl.DEFAULT + ) + + override fun builtInsForModule(module: ModuleInfo): KotlinBuiltIns = DefaultBuiltIns.Instance + + override fun sdkDependency(module: ModuleInfo): ModuleInfo? = null + } + } + + private fun createNativeResolverForProject( + projectContext: ProjectContext, + module: ModuleInfo, + library: LibraryModuleInfo, + modulesContent: (ModuleInfo) -> ModuleContent + ): ResolverForProject { + return object : AbstractResolverForProject( + "Dokka", + projectContext, + modules = module.dependencies() + ) { + override fun modulesContent(module: ModuleInfo): ModuleContent = modulesContent(module) + override fun createResolverForModule( + descriptor: ModuleDescriptor, + moduleInfo: ModuleInfo + ): ResolverForModule { + + return NativeResolverForModuleFactory( + PlatformAnalysisParameters.Empty, + CompilerEnvironment, + NativePlatforms.unspecifiedNativePlatform + ).createResolverForModule( + descriptor as ModuleDescriptorImpl, + projectContext.withModule(descriptor), + modulesContent(moduleInfo), + this, + LanguageVersionSettingsImpl.DEFAULT + ) + } + + override fun builtInsForModule(module: ModuleInfo): KotlinBuiltIns = DefaultBuiltIns.Instance + + override fun sdkDependency(module: ModuleInfo): ModuleInfo? = null + } + } + + private fun createJvmResolverForProject( + projectContext: ProjectContext, + module: ModuleInfo, + library: LibraryModuleInfo, + modulesContent: (ModuleInfo) -> ModuleContent, + sourcesScope: GlobalSearchScope, + builtIns: KotlinBuiltIns + ): ResolverForProject { + val javaRoots = classpath + .mapNotNull { + val rootFile = when (it.extension) { + "jar" -> StandardFileSystems.jar().findFileByPath("${it.absolutePath}$JAR_SEPARATOR") + else -> StandardFileSystems.local().findFileByPath(it.absolutePath) + } + rootFile?.let { JavaRoot(it, JavaRoot.RootType.BINARY) } + } + + return object : AbstractResolverForProject( + "Dokka", + projectContext, + modules = listOf(module, library) + ) { + override fun modulesContent(module: ModuleInfo): ModuleContent = + when (module) { + library -> ModuleContent(module, emptyList(), GlobalSearchScope.notScope(sourcesScope)) + module -> ModuleContent(module, emptyList(), sourcesScope) + else -> throw IllegalArgumentException("Unexpected module info") + } + + override fun builtInsForModule(module: ModuleInfo): KotlinBuiltIns = builtIns + + override fun createResolverForModule( + descriptor: ModuleDescriptor, + moduleInfo: ModuleInfo + ): ResolverForModule = JvmResolverForModuleFactory( + JvmPlatformParameters({ content -> + JvmPackagePartProvider( + configuration.languageVersionSettings, + content.moduleContentScope + ) + .apply { + addRoots(javaRoots, messageCollector) + } + }, { + val file = (it as? BinaryJavaClass)?.virtualFile ?: (it as JavaClassImpl).psi.containingFile.virtualFile + if (file in sourcesScope) + module + else + library + }), + CompilerEnvironment, + unspecifiedJvmPlatform + ).createResolverForModule( + descriptor as ModuleDescriptorImpl, + projectContext.withModule(descriptor), + modulesContent(moduleInfo), + this, + LanguageVersionSettingsImpl.DEFAULT + ) + + override fun sdkDependency(module: ModuleInfo): ModuleInfo? = null + } + } + + fun loadLanguageVersionSettings(languageVersionString: String?, apiVersionString: String?) { + val languageVersion = LanguageVersion.fromVersionString(languageVersionString) ?: LanguageVersion.LATEST_STABLE + val apiVersion = + apiVersionString?.let { ApiVersion.parse(it) } ?: ApiVersion.createByLanguageVersion(languageVersion) + configuration.languageVersionSettings = LanguageVersionSettingsImpl(languageVersion, apiVersion) + } + + /** + * Classpath for this environment. + */ + val classpath: List + get() = configuration.jvmClasspathRoots + + /** + * Adds list of paths to classpath. + * $paths: collection of files to add + */ + fun addClasspath(paths: List) { + if (analysisPlatform == Platform.js) { + configuration.addAll(JSConfigurationKeys.LIBRARIES, paths.map { it.absolutePath }) + } + configuration.addJvmClasspathRoots(paths) + } + + /** + * Adds path to classpath. + * $path: path to add + */ + fun addClasspath(path: File) { + if (analysisPlatform == Platform.js) { + configuration.add(JSConfigurationKeys.LIBRARIES, path.absolutePath) + } + configuration.addJvmClasspathRoot(path) + } + + /** + * List of source roots for this environment. + */ + val sources: List + get() = configuration.get(CLIConfigurationKeys.CONTENT_ROOTS) + ?.filterIsInstance() + ?.map { it.path } ?: emptyList() + + /** + * Adds list of paths to source roots. + * $list: collection of files to add + */ + fun addSources(list: List) { + list.forEach { + configuration.addKotlinSourceRoot(it) + val file = File(it) + if (file.isDirectory || file.extension == ".java") { + configuration.addJavaSourceRoot(file) + } + } + } + + fun addRoots(list: List) { + configuration.addAll(CLIConfigurationKeys.CONTENT_ROOTS, list) + } + + /** + * Disposes the environment and frees all associated resources. + */ + override fun dispose() { + Disposer.dispose(this) + } + + companion object { + private fun registerExtensionPoint( + appExtension: ApplicationExtensionDescriptor, + instances: List + ) { + if (Extensions.getRootArea().hasExtensionPoint(appExtension.extensionPointName)) + return + + appExtension.registerExtensionPoint() + instances.forEach(appExtension::registerExtension) + } + } +} + +fun contentRootFromPath(path: String): ContentRoot { + val file = File(path) + return if (file.extension == "java") JavaSourceRoot(file, null) else KotlinSourceRoot(path, false) +} + + +class DokkaResolutionFacade( + override val project: Project, + override val moduleDescriptor: ModuleDescriptor, + val resolverForModule: ResolverForModule +) : ResolutionFacade { + override fun analyzeWithAllCompilerChecks(elements: Collection): AnalysisResult { + throw UnsupportedOperationException() + } + + override fun tryGetFrontendService(element: PsiElement, serviceClass: Class): T? { + return resolverForModule.componentProvider.tryGetService(serviceClass) + } + + override fun resolveToDescriptor( + declaration: KtDeclaration, + bodyResolveMode: BodyResolveMode + ): DeclarationDescriptor { + return resolveSession.resolveToDescriptor(declaration) + } + + override fun analyze(elements: Collection, bodyResolveMode: BodyResolveMode): BindingContext { + throw UnsupportedOperationException() + } + + val resolveSession: ResolveSession get() = getFrontendService(ResolveSession::class.java) + + override fun analyze(element: KtElement, bodyResolveMode: BodyResolveMode): BindingContext { + if (element is KtDeclaration) { + val descriptor = resolveToDescriptor(element) + return object : BindingContext { + override fun getKeys(p0: WritableSlice?): Collection { + throw UnsupportedOperationException() + } + + override fun getType(p0: KtExpression): KotlinType? { + throw UnsupportedOperationException() + } + + override fun get(slice: ReadOnlySlice?, key: K): V? { + if (key != element) { + throw UnsupportedOperationException() + } + return when { + slice == BindingContext.DECLARATION_TO_DESCRIPTOR -> descriptor as V + slice == BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER && (element as KtParameter).hasValOrVar() -> descriptor as V + else -> null + } + } + + override fun getDiagnostics(): Diagnostics { + throw UnsupportedOperationException() + } + + override fun addOwnDataTo(p0: BindingTrace, p1: Boolean) { + throw UnsupportedOperationException() + } + + override fun getSliceContents(p0: ReadOnlySlice): ImmutableMap { + throw UnsupportedOperationException() + } + + } + } + throw UnsupportedOperationException() + } + + override fun getFrontendService(element: PsiElement, serviceClass: Class): T { + throw UnsupportedOperationException() + } + + override fun getFrontendService(serviceClass: Class): T { + return resolverForModule.componentProvider.getService(serviceClass) + } + + override fun getFrontendService(moduleDescriptor: ModuleDescriptor, serviceClass: Class): T { + return resolverForModule.componentProvider.getService(serviceClass) + } + + override fun getIdeService(serviceClass: Class): T { + throw UnsupportedOperationException() + } + +} diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/CallableFactory.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/CallableFactory.kt new file mode 100644 index 00000000..ebfe20a5 --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/CallableFactory.kt @@ -0,0 +1,31 @@ +package org.jetbrains.dokka.analysis + +import com.intellij.psi.PsiField +import com.intellij.psi.PsiMethod +import org.jetbrains.dokka.links.Callable +import org.jetbrains.dokka.links.JavaClassReference +import org.jetbrains.dokka.links.TypeReference +import org.jetbrains.kotlin.descriptors.CallableDescriptor + +fun Callable.Companion.from(descriptor: CallableDescriptor) = with(descriptor) { + Callable( + name.asString(), + extensionReceiverParameter?.let { TypeReference.from(it) }, + valueParameters.mapNotNull { TypeReference.from(it) } + ) +} + +fun Callable.Companion.from(psi: PsiMethod) = with(psi) { + Callable( + name, + null, + parameterList.parameters.map { param -> JavaClassReference(param.type.canonicalText) }) +} + +fun Callable.Companion.from(psi: PsiField): Callable { + return Callable( + name = psi.name, + receiver = null, + params = emptyList() + ) +} diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/CoreKotlinCacheService.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/CoreKotlinCacheService.kt new file mode 100644 index 00000000..68415875 --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/CoreKotlinCacheService.kt @@ -0,0 +1,42 @@ +package org.jetbrains.dokka.analysis + +import com.intellij.psi.PsiFile +import org.jetbrains.dokka.analysis.DokkaResolutionFacade +import org.jetbrains.kotlin.analyzer.ModuleInfo +import org.jetbrains.kotlin.caches.resolve.KotlinCacheService +import org.jetbrains.kotlin.idea.resolve.ResolutionFacade +import org.jetbrains.kotlin.psi.KtElement +import org.jetbrains.kotlin.resolve.diagnostics.KotlinSuppressCache + + +class CoreKotlinCacheService(private val resolutionFacade: DokkaResolutionFacade) : KotlinCacheService { + override fun getResolutionFacade(elements: List): ResolutionFacade { + return resolutionFacade + } + + override fun getResolutionFacade( + elements: List, + platform: org.jetbrains.kotlin.platform.TargetPlatform + ): ResolutionFacade { + return resolutionFacade + } + + override fun getResolutionFacadeByFile( + file: PsiFile, + platform: org.jetbrains.kotlin.platform.TargetPlatform + ): ResolutionFacade? { + return resolutionFacade + } + + override fun getResolutionFacadeByModuleInfo( + moduleInfo: ModuleInfo, + platform: org.jetbrains.kotlin.platform.TargetPlatform + ): ResolutionFacade? { + return resolutionFacade + } + + override fun getSuppressionCache(): KotlinSuppressCache { + throw UnsupportedOperationException() + } + +} diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/CoreProjectFileIndex.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/CoreProjectFileIndex.kt new file mode 100644 index 00000000..d0e0bb4f --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/CoreProjectFileIndex.kt @@ -0,0 +1,515 @@ +package org.jetbrains.dokka.analysis + +import com.intellij.openapi.Disposable +import com.intellij.openapi.components.BaseComponent +import com.intellij.openapi.extensions.ExtensionPointName +import com.intellij.openapi.module.Module +import com.intellij.openapi.project.Project +import com.intellij.openapi.projectRoots.Sdk +import com.intellij.openapi.projectRoots.SdkAdditionalData +import com.intellij.openapi.projectRoots.SdkModificator +import com.intellij.openapi.projectRoots.SdkTypeId +import com.intellij.openapi.roots.* +import com.intellij.openapi.roots.impl.ProjectOrderEnumerator +import com.intellij.openapi.util.Condition +import com.intellij.openapi.util.Key +import com.intellij.openapi.util.UserDataHolderBase +import com.intellij.openapi.vfs.StandardFileSystems +import com.intellij.openapi.vfs.VfsUtilCore.getVirtualFileForJar +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.openapi.vfs.VirtualFileFilter +import com.intellij.psi.search.GlobalSearchScope +import com.intellij.util.messages.MessageBus +import org.jetbrains.jps.model.module.JpsModuleSourceRootType +import org.jetbrains.kotlin.cli.common.config.ContentRoot +import org.jetbrains.kotlin.cli.common.config.KotlinSourceRoot +import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot +import org.jetbrains.kotlin.cli.jvm.config.JvmContentRoot +import org.picocontainer.PicoContainer +import java.io.File + +/** + * Workaround for the lack of ability to create a ProjectFileIndex implementation using only + * classes from projectModel-{api,impl}. + */ +class CoreProjectFileIndex(private val project: Project, contentRoots: List) : ProjectFileIndex, ModuleFileIndex { + override fun iterateContent(p0: ContentIterator, p1: VirtualFileFilter?): Boolean { + throw UnsupportedOperationException() + } + + override fun iterateContentUnderDirectory(p0: VirtualFile, p1: ContentIterator, p2: VirtualFileFilter?): Boolean { + throw UnsupportedOperationException() + } + + override fun isInLibrary(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + val sourceRoots = contentRoots.filter { it !is JvmClasspathRoot } + val classpathRoots = contentRoots.filterIsInstance() + + val module: Module = object : UserDataHolderBase(), Module { + override fun isDisposed(): Boolean { + throw UnsupportedOperationException() + } + + override fun getName(): String = "" + + override fun getModuleWithLibrariesScope(): GlobalSearchScope { + throw UnsupportedOperationException() + } + + override fun getModuleWithDependentsScope(): GlobalSearchScope { + throw UnsupportedOperationException() + } + + override fun getModuleWithDependenciesScope(): GlobalSearchScope { + throw UnsupportedOperationException() + } + + override fun getModuleWithDependenciesAndLibrariesScope(p0: Boolean): GlobalSearchScope { + throw UnsupportedOperationException() + } + + override fun getModuleTestsWithDependentsScope(): GlobalSearchScope { + throw UnsupportedOperationException() + } + + override fun getModuleScope(): GlobalSearchScope { + throw UnsupportedOperationException() + } + + override fun getModuleRuntimeScope(p0: Boolean): GlobalSearchScope { + throw UnsupportedOperationException() + } + + override fun getExtensions(p0: ExtensionPointName): Array { + throw UnsupportedOperationException() + } + + override fun getComponent(interfaceClass: Class): T? { + if (interfaceClass == ModuleRootManager::class.java) { + return moduleRootManager as T + } + throw UnsupportedOperationException() + } + + override fun getDisposed(): Condition<*> { + throw UnsupportedOperationException() + } + + override fun getPicoContainer(): PicoContainer { + throw UnsupportedOperationException() + } + + override fun getMessageBus(): MessageBus { + throw UnsupportedOperationException() + } + + override fun dispose() { + throw UnsupportedOperationException() + } + } + + private val sdk: Sdk = object : Sdk, RootProvider { + override fun getFiles(rootType: OrderRootType): Array = classpathRoots + .mapNotNull { StandardFileSystems.local().findFileByPath(it.file.path) } + .toTypedArray() + + override fun addRootSetChangedListener(p0: RootProvider.RootSetChangedListener) { + throw UnsupportedOperationException() + } + + override fun addRootSetChangedListener(p0: RootProvider.RootSetChangedListener, p1: Disposable) { + throw UnsupportedOperationException() + } + + override fun getUrls(p0: OrderRootType): Array { + throw UnsupportedOperationException() + } + + override fun removeRootSetChangedListener(p0: RootProvider.RootSetChangedListener) { + throw UnsupportedOperationException() + } + + override fun getSdkModificator(): SdkModificator { + throw UnsupportedOperationException() + } + + override fun getName(): String = "" + + override fun getRootProvider(): RootProvider = this + + override fun getHomePath(): String? { + throw UnsupportedOperationException() + } + + override fun getVersionString(): String? { + throw UnsupportedOperationException() + } + + override fun getSdkAdditionalData(): SdkAdditionalData? { + throw UnsupportedOperationException() + } + + override fun clone(): Any { + throw UnsupportedOperationException() + } + + override fun getSdkType(): SdkTypeId { + throw UnsupportedOperationException() + } + + override fun getHomeDirectory(): VirtualFile? { + throw UnsupportedOperationException() + } + + override fun getUserData(p0: Key): T? { + throw UnsupportedOperationException() + } + + override fun putUserData(p0: Key, p1: T?) { + throw UnsupportedOperationException() + } + } + + private val moduleSourceOrderEntry = object : ModuleSourceOrderEntry { + override fun getFiles(p0: OrderRootType): Array { + throw UnsupportedOperationException() + } + + override fun getUrls(p0: OrderRootType): Array { + throw UnsupportedOperationException() + } + + override fun accept(p0: RootPolicy, p1: R?): R { + throw UnsupportedOperationException() + } + + + override fun getPresentableName(): String { + throw UnsupportedOperationException() + } + + override fun getOwnerModule(): Module = module + + + override fun isValid(): Boolean { + throw UnsupportedOperationException() + } + + override fun compareTo(other: OrderEntry?): Int { + throw UnsupportedOperationException() + } + + override fun getRootModel(): ModuleRootModel = moduleRootManager + + override fun isSynthetic(): Boolean { + throw UnsupportedOperationException() + } + } + + private val sdkOrderEntry = object : JdkOrderEntry { + override fun getFiles(p0: OrderRootType): Array { + throw UnsupportedOperationException() + } + + override fun getUrls(p0: OrderRootType): Array { + throw UnsupportedOperationException() + } + + override fun accept(p0: RootPolicy, p1: R?): R { + throw UnsupportedOperationException() + } + + override fun getJdkName(): String? { + throw UnsupportedOperationException() + } + + override fun getJdk(): Sdk = sdk + + override fun getPresentableName(): String { + throw UnsupportedOperationException() + } + + override fun getOwnerModule(): Module { + throw UnsupportedOperationException() + } + + override fun isValid(): Boolean { + throw UnsupportedOperationException() + } + + override fun getRootFiles(p0: OrderRootType): Array { + throw UnsupportedOperationException() + } + + override fun getRootUrls(p0: OrderRootType): Array { + throw UnsupportedOperationException() + } + + override fun compareTo(other: OrderEntry?): Int { + throw UnsupportedOperationException() + } + + override fun isSynthetic(): Boolean { + throw UnsupportedOperationException() + } + + } + + inner class MyModuleRootManager : ModuleRootManager() { + override fun getExternalSource(): ProjectModelExternalSource? { + throw UnsupportedOperationException() + } + + override fun getExcludeRoots(): Array { + throw UnsupportedOperationException() + } + + override fun getContentEntries(): Array { + throw UnsupportedOperationException() + } + + override fun getExcludeRootUrls(): Array { + throw UnsupportedOperationException() + } + + override fun processOrder(p0: RootPolicy, p1: R): R { + throw UnsupportedOperationException() + } + + override fun getSourceRoots(p0: Boolean): Array { + throw UnsupportedOperationException() + } + + override fun getSourceRoots(): Array { + throw UnsupportedOperationException() + } + + override fun getSourceRoots(p0: JpsModuleSourceRootType<*>): MutableList { + throw UnsupportedOperationException() + } + + override fun getSourceRoots(p0: MutableSet>): MutableList { + throw UnsupportedOperationException() + } + + override fun getContentRoots(): Array { + throw UnsupportedOperationException() + } + + override fun orderEntries(): OrderEnumerator = + ProjectOrderEnumerator(project, null).using(object : RootModelProvider { + override fun getModules(): Array = arrayOf(module) + + override fun getRootModel(p0: Module): ModuleRootModel = this@MyModuleRootManager + }) + + override fun getModuleExtension(p0: Class): T { + throw UnsupportedOperationException() + } + + override fun getDependencyModuleNames(): Array { + throw UnsupportedOperationException() + } + + override fun getModule(): Module = this@CoreProjectFileIndex.module + + override fun isSdkInherited(): Boolean { + throw UnsupportedOperationException() + } + + override fun getOrderEntries(): Array = arrayOf(moduleSourceOrderEntry, sdkOrderEntry) + + override fun getSourceRootUrls(): Array { + throw UnsupportedOperationException() + } + + override fun getSourceRootUrls(p0: Boolean): Array { + throw UnsupportedOperationException() + } + + override fun getSdk(): Sdk? { + throw UnsupportedOperationException() + } + + override fun getContentRootUrls(): Array { + throw UnsupportedOperationException() + } + + override fun getModuleDependencies(): Array { + throw UnsupportedOperationException() + } + + override fun getModuleDependencies(p0: Boolean): Array { + throw UnsupportedOperationException() + } + + override fun getModifiableModel(): ModifiableRootModel { + throw UnsupportedOperationException() + } + + override fun isDependsOn(p0: Module): Boolean { + throw UnsupportedOperationException() + } + + override fun getFileIndex(): ModuleFileIndex { + return this@CoreProjectFileIndex + } + + override fun getDependencies(): Array { + throw UnsupportedOperationException() + } + + override fun getDependencies(p0: Boolean): Array { + throw UnsupportedOperationException() + } + } + + val moduleRootManager = MyModuleRootManager() + + override fun getContentRootForFile(p0: VirtualFile): VirtualFile? { + throw UnsupportedOperationException() + } + + override fun getContentRootForFile(p0: VirtualFile, p1: Boolean): VirtualFile? { + throw UnsupportedOperationException() + } + + override fun getPackageNameByDirectory(p0: VirtualFile): String? { + throw UnsupportedOperationException() + } + + override fun isInLibrarySource(file: VirtualFile): Boolean = false + + override fun getClassRootForFile(file: VirtualFile): VirtualFile? = + classpathRoots.firstOrNull { it.contains(file) }?.let { StandardFileSystems.local().findFileByPath(it.file.path) } + + override fun getOrderEntriesForFile(file: VirtualFile): List = + if (classpathRoots.contains(file)) listOf(sdkOrderEntry) else emptyList() + + override fun isInLibraryClasses(file: VirtualFile): Boolean = classpathRoots.contains(file) + + override fun isExcluded(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun getSourceRootForFile(p0: VirtualFile): VirtualFile? { + throw UnsupportedOperationException() + } + + override fun isUnderIgnored(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun isLibraryClassFile(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun getModuleForFile(file: VirtualFile): Module? = + if (sourceRoots.contains(file)) module else null + + private fun List.contains(file: VirtualFile): Boolean = any { it.contains(file) } + + override fun getModuleForFile(p0: VirtualFile, p1: Boolean): Module? { + throw UnsupportedOperationException() + } + + override fun isInSource(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun isIgnored(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun isContentSourceFile(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun isInSourceContent(file: VirtualFile): Boolean = sourceRoots.contains(file) + + override fun iterateContent(p0: ContentIterator): Boolean { + throw UnsupportedOperationException() + } + + override fun isInContent(p0: VirtualFile): Boolean { + throw UnsupportedOperationException() + } + + override fun iterateContentUnderDirectory(p0: VirtualFile, p1: ContentIterator): Boolean { + throw UnsupportedOperationException() + } + + override fun isInTestSourceContent(file: VirtualFile): Boolean = false + + override fun isUnderSourceRootOfType(p0: VirtualFile, p1: MutableSet>): Boolean { + throw UnsupportedOperationException() + } + + override fun getOrderEntryForFile(p0: VirtualFile): OrderEntry? { + throw UnsupportedOperationException() + } +} + +class CoreProjectRootManager(val projectFileIndex: CoreProjectFileIndex) : ProjectRootManager() { + override fun orderEntries(): OrderEnumerator { + throw UnsupportedOperationException() + } + + override fun orderEntries(p0: MutableCollection): OrderEnumerator { + throw UnsupportedOperationException() + } + + override fun getContentRootsFromAllModules(): Array { + throw UnsupportedOperationException() + } + + override fun setProjectSdk(p0: Sdk?) { + throw UnsupportedOperationException() + } + + override fun setProjectSdkName(p0: String) { + throw UnsupportedOperationException() + } + + override fun getModuleSourceRoots(p0: MutableSet>): MutableList { + throw UnsupportedOperationException() + } + + override fun getContentSourceRoots(): Array { + throw UnsupportedOperationException() + } + + override fun getFileIndex(): ProjectFileIndex = projectFileIndex + + override fun getProjectSdkName(): String? { + throw UnsupportedOperationException() + } + + override fun getProjectSdk(): Sdk? { + throw UnsupportedOperationException() + } + + override fun getContentRoots(): Array { + throw UnsupportedOperationException() + } + + override fun getContentRootUrls(): MutableList { + throw UnsupportedOperationException() + } + +} + +fun ContentRoot.contains(file: VirtualFile) = when (this) { + is JvmContentRoot -> { + val path = if (file.fileSystem.protocol == StandardFileSystems.JAR_PROTOCOL) + getVirtualFileForJar(file)?.path ?: file.path + else + file.path + File(path).startsWith(this.file.absoluteFile) + } + is KotlinSourceRoot -> File(file.path).startsWith(File(this.path).absoluteFile) + else -> false +} diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRIFactory.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRIFactory.kt new file mode 100644 index 00000000..513817f3 --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRIFactory.kt @@ -0,0 +1,38 @@ +package org.jetbrains.dokka.analysis + +import com.intellij.psi.PsiClass +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiField +import com.intellij.psi.PsiMethod +import org.jetbrains.dokka.links.Callable +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.links.DriTarget +import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf +import org.jetbrains.kotlin.resolve.descriptorUtil.parentsWithSelf +import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull + +fun DRI.Companion.from(descriptor: DeclarationDescriptor) = descriptor.parentsWithSelf.run { + val callable = firstIsInstanceOrNull() + DRI( + firstIsInstanceOrNull()?.fqName?.asString(), + (filterIsInstance() + filterIsInstance()).toList() + .takeIf { it.isNotEmpty() } + ?.asReversed() + ?.joinToString(separator = ".") { it.name.asString() }, + callable?.let { Callable.from(it) }, + DriTarget.from(descriptor) + ) +} + +fun DRI.Companion.from(psi: PsiElement) = psi.parentsWithSelf.run { + val psiMethod = firstIsInstanceOrNull() + val psiField = firstIsInstanceOrNull() + val classes = filterIsInstance().toList() + DRI( + classes.lastOrNull()?.qualifiedName?.substringBeforeLast('.', ""), + classes.toList().takeIf { it.isNotEmpty() }?.asReversed()?.mapNotNull { it.name }?.joinToString("."), + psiMethod?.let { Callable.from(it) } ?: psiField?.let { Callable.from(it) }, + DriTarget.from(psi) + ) +} diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRITargetFactory.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRITargetFactory.kt new file mode 100644 index 00000000..031a5a32 --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/DRITargetFactory.kt @@ -0,0 +1,44 @@ +package org.jetbrains.dokka.analysis + +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiMethod +import com.intellij.psi.PsiParameter +import com.intellij.psi.PsiTypeParameter +import org.jetbrains.dokka.links.DriTarget +import org.jetbrains.dokka.links.PointingToCallableParameters +import org.jetbrains.dokka.links.PointingToDeclaration +import org.jetbrains.dokka.links.PointingToGenericParameters +import org.jetbrains.kotlin.descriptors.CallableDescriptor +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.descriptors.ParameterDescriptor +import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor +import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf +import org.jetbrains.kotlin.resolve.descriptorUtil.parentsWithSelf +import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull + +fun DriTarget.Companion.from(descriptor: DeclarationDescriptor): DriTarget = descriptor.parentsWithSelf.run { + return when (descriptor) { + is TypeParameterDescriptor -> PointingToGenericParameters(descriptor.index) + else -> { + val callable = firstIsInstanceOrNull() + val params = + callable?.let { listOfNotNull(it.extensionReceiverParameter) + it.valueParameters }.orEmpty() + val parameterDescriptor = firstIsInstanceOrNull() + + parameterDescriptor?.let { PointingToCallableParameters(params.indexOf(it)) } + ?: PointingToDeclaration + } + } +} + + +fun DriTarget.Companion.from(psi: PsiElement): DriTarget = psi.parentsWithSelf.run { + return when (psi) { + is PsiTypeParameter -> PointingToGenericParameters(psi.index) + else -> firstIsInstanceOrNull()?.let { + val callable = firstIsInstanceOrNull() + val params = (callable?.parameterList?.parameters).orEmpty() + PointingToCallableParameters(params.indexOf(it)) + } ?: PointingToDeclaration + } +} diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/Documentable.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/Documentable.kt new file mode 100644 index 00000000..0c55fed4 --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/Documentable.kt @@ -0,0 +1,14 @@ +package org.jetbrains.dokka.analysis + +import com.intellij.psi.PsiNamedElement +import org.jetbrains.dokka.model.DocumentableSource +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.load.kotlin.toSourceElement + +class DescriptorDocumentableSource(val descriptor: DeclarationDescriptor) : DocumentableSource { + override val path = descriptor.toSourceElement.containingFile.toString() +} + +class PsiDocumentableSource(val psi: PsiNamedElement) : DocumentableSource { + override val path = psi.containingFile.virtualFile.path +} diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/EnvironmentAndFacade.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/EnvironmentAndFacade.kt new file mode 100644 index 00000000..9d15123a --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/EnvironmentAndFacade.kt @@ -0,0 +1,59 @@ +package org.jetbrains.dokka.analysis + +import org.jetbrains.dokka.DokkaConfiguration +import org.jetbrains.dokka.Platform +import org.jetbrains.dokka.utilities.DokkaLogger +import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation +import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity +import org.jetbrains.kotlin.cli.common.messages.MessageCollector +import org.jetbrains.kotlin.cli.common.messages.MessageRenderer +import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment +import org.jetbrains.kotlin.utils.PathUtil +import java.io.File + +internal fun createEnvironmentAndFacade( + logger: DokkaLogger, + configuration: DokkaConfiguration, + pass: DokkaConfiguration.PassConfiguration +): EnvironmentAndFacade = + AnalysisEnvironment(DokkaMessageCollector(logger), pass.analysisPlatform).run { + if (analysisPlatform == Platform.jvm) { + addClasspath(PathUtil.getJdkClassesRootsFromCurrentJre()) + } + pass.classpath.forEach { addClasspath(File(it)) } + + addSources( + (pass.sourceRoots + configuration.passesConfigurations.filter { it.sourceSetID in pass.dependentSourceSets } + .flatMap { it.sourceRoots }) + .map { it.path } + ) + + loadLanguageVersionSettings(pass.languageVersion, pass.apiVersion) + + val environment = createCoreEnvironment() + val (facade, _) = createResolutionFacade(environment) + EnvironmentAndFacade(environment, facade) + } + +class DokkaMessageCollector(private val logger: DokkaLogger) : MessageCollector { + override fun clear() { + seenErrors = false + } + + private var seenErrors = false + + override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageLocation?) { + if (severity == CompilerMessageSeverity.ERROR) { + seenErrors = true + } + logger.info(MessageRenderer.PLAIN_FULL_PATHS.render(severity, message, location)) + } + + override fun hasErrors() = seenErrors +} + +// It is not data class due to ill-defined equals +class EnvironmentAndFacade(val environment: KotlinCoreEnvironment, val facade: DokkaResolutionFacade) { + operator fun component1() = environment + operator fun component2() = facade +} diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/JavaResolveExtension.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/JavaResolveExtension.kt new file mode 100644 index 00000000..ab6bec9c --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/JavaResolveExtension.kt @@ -0,0 +1,128 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +@file:JvmName("JavaResolutionUtils") + +package org.jetbrains.dokka.analysis + +import com.intellij.psi.* +import org.jetbrains.kotlin.asJava.unwrapped +import org.jetbrains.kotlin.caches.resolve.KotlinCacheService +import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.idea.resolve.ResolutionFacade +import org.jetbrains.kotlin.incremental.components.NoLookupLocation +import org.jetbrains.kotlin.load.java.sources.JavaSourceElement +import org.jetbrains.kotlin.load.java.structure.* +import org.jetbrains.kotlin.load.java.structure.impl.* +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.platform.jvm.JvmPlatforms +import org.jetbrains.kotlin.psi.KtDeclaration +import org.jetbrains.kotlin.resolve.jvm.JavaDescriptorResolver +import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter +import org.jetbrains.kotlin.resolve.scopes.MemberScope + +// TODO: Remove that file + +@JvmOverloads +fun PsiMethod.getJavaMethodDescriptor(resolutionFacade: ResolutionFacade = javaResolutionFacade()): DeclarationDescriptor? { + val method = originalElement as? PsiMethod ?: return null + if (method.containingClass == null || !Name.isValidIdentifier(method.name)) return null + val resolver = method.getJavaDescriptorResolver(resolutionFacade) + return when { + method.isConstructor -> resolver?.resolveConstructor(JavaConstructorImpl(method)) + else -> resolver?.resolveMethod(JavaMethodImpl(method)) + } +} + +@JvmOverloads +fun PsiClass.getJavaClassDescriptor(resolutionFacade: ResolutionFacade = javaResolutionFacade()): ClassDescriptor? { + val psiClass = originalElement as? PsiClass ?: return null + return psiClass.getJavaDescriptorResolver(resolutionFacade)?.resolveClass(JavaClassImpl(psiClass)) +} + +@JvmOverloads +fun PsiField.getJavaFieldDescriptor(resolutionFacade: ResolutionFacade = javaResolutionFacade()): PropertyDescriptor? { + val field = originalElement as? PsiField ?: return null + return field.getJavaDescriptorResolver(resolutionFacade)?.resolveField(JavaFieldImpl(field)) +} + +@JvmOverloads +fun PsiMember.getJavaMemberDescriptor(resolutionFacade: ResolutionFacade = javaResolutionFacade()): DeclarationDescriptor? { + return when (this) { + is PsiEnumConstant -> containingClass?.getJavaClassDescriptor(resolutionFacade) + is PsiClass -> getJavaClassDescriptor(resolutionFacade) + is PsiMethod -> getJavaMethodDescriptor(resolutionFacade) + is PsiField -> getJavaFieldDescriptor(resolutionFacade) + else -> null + } +} + +@JvmOverloads +fun PsiMember.getJavaOrKotlinMemberDescriptor(resolutionFacade: ResolutionFacade = javaResolutionFacade()): DeclarationDescriptor? { + val callable = unwrapped + return when (callable) { + is PsiMember -> getJavaMemberDescriptor(resolutionFacade) + is KtDeclaration -> { + val descriptor = resolutionFacade.resolveToDescriptor(callable) + if (descriptor is ClassDescriptor && this is PsiMethod) descriptor.unsubstitutedPrimaryConstructor else descriptor + } + else -> null + } +} + +private fun PsiElement.getJavaDescriptorResolver(resolutionFacade: ResolutionFacade): JavaDescriptorResolver? { + return resolutionFacade.tryGetFrontendService(this, JavaDescriptorResolver::class.java) +} + +private fun JavaDescriptorResolver.resolveMethod(method: JavaMethod): DeclarationDescriptor? { + return getContainingScope(method) + ?.getContributedDescriptors(nameFilter = { true }, kindFilter = DescriptorKindFilter.CALLABLES) + ?.filterIsInstance() + ?.findByJavaElement(method) +} + +private fun JavaDescriptorResolver.resolveConstructor(constructor: JavaConstructor): ConstructorDescriptor? { + return resolveClass(constructor.containingClass)?.constructors?.findByJavaElement(constructor) +} + +private fun JavaDescriptorResolver.resolveField(field: JavaField): PropertyDescriptor? { + return getContainingScope(field)?.getContributedVariables(field.name, NoLookupLocation.FROM_IDE)?.findByJavaElement(field) +} + +private fun JavaDescriptorResolver.getContainingScope(member: JavaMember): MemberScope? { + val containingClass = resolveClass(member.containingClass) + return if (member.isStatic) + containingClass?.staticScope + else + containingClass?.defaultType?.memberScope +} + +private fun Collection.findByJavaElement(javaElement: JavaElement): T? { + return firstOrNull { member -> + val memberJavaElement = (member.original.source as? JavaSourceElement)?.javaElement + when { + memberJavaElement == javaElement -> + true + memberJavaElement is JavaElementImpl<*> && javaElement is JavaElementImpl<*> -> + memberJavaElement.psi.isEquivalentTo(javaElement.psi) + else -> + false + } + } +} + +fun PsiElement.javaResolutionFacade() = + KotlinCacheService.getInstance(project).getResolutionFacadeByFile(this.originalElement.containingFile, JvmPlatforms.defaultJvmPlatform)!! diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/KotlinAnalysis.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/KotlinAnalysis.kt new file mode 100644 index 00000000..cd07b2a3 --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/KotlinAnalysis.kt @@ -0,0 +1,46 @@ +@file:Suppress("FunctionName") + +package org.jetbrains.dokka.analysis + +import org.jetbrains.dokka.DokkaConfiguration +import org.jetbrains.dokka.model.SourceSetCache +import org.jetbrains.dokka.model.SourceSetData +import org.jetbrains.dokka.model.SourceSetDependent +import org.jetbrains.dokka.plugability.DokkaContext + +fun KotlinAnalysis(context: DokkaContext): KotlinAnalysis { + val environments = context.configuration.passesConfigurations + .associate { passConfiguration -> + context.sourceSetCache.getSourceSet(passConfiguration) to createEnvironmentAndFacade( + logger = context.logger, + configuration = context.configuration, + pass = passConfiguration + ) + } + + return KotlinAnalysisImpl(context.sourceSetCache, environments) +} + +interface KotlinAnalysis : SourceSetDependent { + override fun get(key: SourceSetData): EnvironmentAndFacade + operator fun get(sourceSetID: String): EnvironmentAndFacade + operator fun get(passConfiguration: DokkaConfiguration.PassConfiguration): EnvironmentAndFacade +} + +internal class KotlinAnalysisImpl( + private val sourceSetCache: SourceSetCache, + private val environments: SourceSetDependent +) : KotlinAnalysis, SourceSetDependent by environments { + + override fun get(key: SourceSetData): EnvironmentAndFacade { + return environments[key] ?: throw IllegalStateException("Missing EnvironmentAndFacade for sourceSet $key") + } + + override fun get(sourceSetID: String): EnvironmentAndFacade { + return environments.entries.first { (sourceSet, _) -> sourceSet.sourceSetID == sourceSetID }.value + } + + override fun get(passConfiguration: DokkaConfiguration.PassConfiguration): EnvironmentAndFacade { + return get(sourceSetCache.getSourceSet(passConfiguration)) + } +} diff --git a/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/TypeReferenceFactory.kt b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/TypeReferenceFactory.kt new file mode 100644 index 00000000..e07672d4 --- /dev/null +++ b/kotlin-analysis/src/main/kotlin/org/jetbrains/dokka/analysis/TypeReferenceFactory.kt @@ -0,0 +1,51 @@ +package org.jetbrains.dokka.analysis + +import com.intellij.psi.PsiClass +import org.jetbrains.dokka.links.* +import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor +import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor +import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor +import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe +import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver +import org.jetbrains.kotlin.types.KotlinType +import org.jetbrains.kotlin.types.TypeProjection + +fun TypeReference.Companion.from(d: ReceiverParameterDescriptor): TypeReference? = + when (d.value) { + is ExtensionReceiver -> fromPossiblyNullable(d.type) + else -> run { + println("Unknown value type for $d") + null + } + } + +fun TypeReference.Companion.from(d: ValueParameterDescriptor): TypeReference? = + fromPossiblyNullable(d.type) + +fun TypeReference.Companion.from(p: PsiClass) = TypeReference + +private fun TypeReference.Companion.fromPossiblyNullable(t: KotlinType, self: KotlinType? = null): TypeReference = + from(t, self).let { if (t.isMarkedNullable) Nullable(it) else it } + +private fun TypeReference.Companion.from(t: KotlinType, self: KotlinType? = null): TypeReference = + if (self is KotlinType && self.constructor == t.constructor && self.arguments == t.arguments) + SelfType + else when (val d = t.constructor.declarationDescriptor) { + is TypeParameterDescriptor -> TypeParam( + d.upperBounds.map { fromPossiblyNullable(it, self ?: t) } + ) + else -> TypeConstructor( + t.constructorName.orEmpty(), + t.arguments.map { fromProjection(it, self) } + ) + } + +private fun TypeReference.Companion.fromProjection(t: TypeProjection, r: KotlinType? = null): TypeReference = + if (t.isStarProjection) { + StarProjection + } else { + fromPossiblyNullable(t.type, r) + } + +private val KotlinType.constructorName + get() = constructor.declarationDescriptor?.fqNameSafe?.asString() diff --git a/plugins/base/build.gradle.kts b/plugins/base/build.gradle.kts index 24a7f593..97d6a646 100644 --- a/plugins/base/build.gradle.kts +++ b/plugins/base/build.gradle.kts @@ -5,6 +5,7 @@ plugins { } dependencies { + api(project(":kotlin-analysis")) implementation("org.jsoup:jsoup:1.12.1") implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.6.10") testImplementation(project(":test-tools")) diff --git a/plugins/base/src/main/kotlin/DokkaBase.kt b/plugins/base/src/main/kotlin/DokkaBase.kt index cf4ad88f..f1b0d969 100644 --- a/plugins/base/src/main/kotlin/DokkaBase.kt +++ b/plugins/base/src/main/kotlin/DokkaBase.kt @@ -1,6 +1,9 @@ +@file:Suppress("unused") + package org.jetbrains.dokka.base import org.jetbrains.dokka.CoreExtensions +import org.jetbrains.dokka.analysis.KotlinAnalysis import org.jetbrains.dokka.base.allModulePage.MultimodulePageCreator import org.jetbrains.dokka.base.renderers.* import org.jetbrains.dokka.base.renderers.html.* @@ -37,13 +40,19 @@ class DokkaBase : DokkaPlugin() { val externalLocationProviderFactory by extensionPoint() val outputWriter by extensionPoint() val htmlPreprocessors by extensionPoint() + val kotlinAnalysis by extensionPoint() + val descriptorToDocumentableTranslator by extending { - CoreExtensions.sourceToDocumentableTranslator with DefaultDescriptorToDocumentableTranslator + CoreExtensions.sourceToDocumentableTranslator providing { ctx -> + DefaultDescriptorToDocumentableTranslator(ctx.single(kotlinAnalysis)) + } } val psiToDocumentableTranslator by extending { - CoreExtensions.sourceToDocumentableTranslator with DefaultPsiToDocumentableTranslator + CoreExtensions.sourceToDocumentableTranslator providing { ctx -> + DefaultPsiToDocumentableTranslator(ctx.single(kotlinAnalysis)) + } } val documentableMerger by extending(isFallback = true) { @@ -69,7 +78,9 @@ class DokkaBase : DokkaPlugin() { } val modulesAndPackagesDocumentation by extending(isFallback = true) { - CoreExtensions.preMergeDocumentableTransformer providing ::ModuleAndPackageDocumentationTransformer + CoreExtensions.preMergeDocumentableTransformer providing { ctx -> + ModuleAndPackageDocumentationTransformer(ctx, ctx.single(kotlinAnalysis)) + } } val kotlinSignatureProvider by extending(isFallback = true) { @@ -124,6 +135,10 @@ class DokkaBase : DokkaPlugin() { CoreExtensions.renderer providing ::HtmlRenderer applyIf { format == "html" } } + val defaultKotlinAnalysis by extending(isFallback = true) { + kotlinAnalysis providing { ctx -> KotlinAnalysis(ctx) } + } + val locationProvider by extending(isFallback = true) { locationProviderFactory providing ::DefaultLocationProviderFactory } diff --git a/plugins/base/src/main/kotlin/allModulePage/MultimodulePageCreator.kt b/plugins/base/src/main/kotlin/allModulePage/MultimodulePageCreator.kt index ea1d8510..cdaa0274 100644 --- a/plugins/base/src/main/kotlin/allModulePage/MultimodulePageCreator.kt +++ b/plugins/base/src/main/kotlin/allModulePage/MultimodulePageCreator.kt @@ -9,7 +9,7 @@ import org.jetbrains.dokka.model.SourceSetData import org.jetbrains.dokka.model.doc.DocumentationNode import org.jetbrains.dokka.model.doc.P import org.jetbrains.dokka.pages.* -import org.jetbrains.dokka.parsers.MarkdownParser +import org.jetbrains.dokka.base.parsers.MarkdownParser import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.plugability.querySingle import org.jetbrains.dokka.transformers.pages.PageCreator diff --git a/plugins/base/src/main/kotlin/parsers/HtmlParser.kt b/plugins/base/src/main/kotlin/parsers/HtmlParser.kt new file mode 100644 index 00000000..ece3cf24 --- /dev/null +++ b/plugins/base/src/main/kotlin/parsers/HtmlParser.kt @@ -0,0 +1,89 @@ +package org.jetbrains.dokka.base.parsers + +import org.jetbrains.dokka.model.doc.* +import org.jetbrains.dokka.base.parsers.factories.DocTagsFromStringFactory +import org.jsoup.Jsoup +import org.jsoup.nodes.Node +import org.jsoup.select.NodeFilter +import org.jsoup.select.NodeTraversor + +class HtmlParser : Parser() { + + inner class NodeFilterImpl : NodeFilter { + + private val nodesCache: MutableMap> = mutableMapOf() + private var currentDepth = 0 + + fun collect(): DocTag = nodesCache[currentDepth]!![0] + + override fun tail(node: Node?, depth: Int): NodeFilter.FilterResult { + val nodeName = node!!.nodeName() + val nodeAttributes = node.attributes() + + if(nodeName in listOf("#document", "html", "head")) + return NodeFilter.FilterResult.CONTINUE + + val body: String + val params: Map + + + if(nodeName != "#text") { + body = "" + params = nodeAttributes.map { it.key to it.value }.toMap() + } else { + body = nodeAttributes["#text"] + params = emptyMap() + } + + val docNode = if(depth < currentDepth) { + DocTagsFromStringFactory.getInstance(nodeName, nodesCache.getOrDefault(currentDepth, mutableListOf()).toList(), params, body).also { + nodesCache[currentDepth] = mutableListOf() + currentDepth = depth + } + } else { + DocTagsFromStringFactory.getInstance(nodeName, emptyList(), params, body) + } + + nodesCache.getOrDefault(depth, mutableListOf()) += docNode + return NodeFilter.FilterResult.CONTINUE + } + + override fun head(node: Node?, depth: Int): NodeFilter.FilterResult { + + val nodeName = node!!.nodeName() + + if(currentDepth < depth) { + currentDepth = depth + nodesCache[currentDepth] = mutableListOf() + } + + if(nodeName in listOf("#document", "html", "head")) + return NodeFilter.FilterResult.CONTINUE + + return NodeFilter.FilterResult.CONTINUE + } + } + + + private fun htmlToDocNode(string: String): DocTag { + val document = Jsoup.parse(string) + val nodeFilterImpl = NodeFilterImpl() + NodeTraversor.filter(nodeFilterImpl, document.root()) + return nodeFilterImpl.collect() + } + + private fun replaceLinksWithHrefs(javadoc: String): String = Regex("\\{@link .*?}").replace(javadoc) { + val split = it.value.dropLast(1).split(" ") + if(split.size !in listOf(2, 3)) + return@replace it.value + if(split.size == 3) + return@replace "${split[2]}" + else + return@replace "${split[1]}" + } + + override fun parseStringToDocNode(extractedString: String) = htmlToDocNode(extractedString) + override fun preparse(text: String) = replaceLinksWithHrefs(text) +} + + diff --git a/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt b/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt new file mode 100644 index 00000000..6075ef73 --- /dev/null +++ b/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt @@ -0,0 +1,419 @@ +package org.jetbrains.dokka.base.parsers + +import com.intellij.psi.PsiElement +import org.jetbrains.dokka.model.doc.* +import org.intellij.markdown.MarkdownElementTypes +import org.intellij.markdown.MarkdownTokenTypes +import org.intellij.markdown.ast.ASTNode +import org.intellij.markdown.ast.CompositeASTNode +import org.intellij.markdown.ast.LeafASTNode +import org.intellij.markdown.ast.impl.ListItemCompositeNode +import org.intellij.markdown.flavours.gfm.GFMElementTypes +import org.intellij.markdown.flavours.gfm.GFMFlavourDescriptor +import org.intellij.markdown.flavours.gfm.GFMTokenTypes +import org.jetbrains.dokka.analysis.DokkaResolutionFacade +import org.jetbrains.dokka.analysis.from +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.base.parsers.factories.DocTagsFromIElementFactory +import org.jetbrains.dokka.utilities.DokkaLogger +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.idea.kdoc.resolveKDocLink +import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag +import org.jetbrains.kotlin.kdoc.psi.impl.KDocSection +import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag +import java.net.MalformedURLException +import org.intellij.markdown.parser.MarkdownParser as IntellijMarkdownParser + +class MarkdownParser( + private val resolutionFacade: DokkaResolutionFacade? = null, + private val declarationDescriptor: DeclarationDescriptor? = null, + private val logger: DokkaLogger +) : Parser() { + + inner class MarkdownVisitor(val text: String, val destinationLinksMap: Map) { + + private fun headersHandler(node: ASTNode): DocTag = + DocTagsFromIElementFactory.getInstance( + node.type, + visitNode(node.children.find { it.type == MarkdownTokenTypes.ATX_CONTENT } + ?: throw IllegalStateException("Wrong AST Tree. ATX Header does not contain expected content")).children + ) + + private fun horizontalRulesHandler(node: ASTNode): DocTag = + DocTagsFromIElementFactory.getInstance(MarkdownTokenTypes.HORIZONTAL_RULE) + + private fun emphasisHandler(node: ASTNode): DocTag = + DocTagsFromIElementFactory.getInstance( + node.type, + children = listOf(visitNode(node.children[node.children.size / 2])) + ) + + private fun blockquotesHandler(node: ASTNode): DocTag = + DocTagsFromIElementFactory.getInstance( + node.type, children = node.children + .filterIsInstance() + .evaluateChildren() + ) + + private fun listsHandler(node: ASTNode): DocTag { + + val children = node.children.filterIsInstance().flatMap { + if (it.children.last().type in listOf( + MarkdownElementTypes.ORDERED_LIST, + MarkdownElementTypes.UNORDERED_LIST + ) + ) { + val nestedList = it.children.last() + (it.children as MutableList).removeAt(it.children.lastIndex) + listOf(it, nestedList) + } else + listOf(it) + } + + return DocTagsFromIElementFactory.getInstance( + node.type, + children = + children + .map { + if (it.type == MarkdownElementTypes.LIST_ITEM) + DocTagsFromIElementFactory.getInstance( + it.type, + children = it + .children + .filterIsInstance() + .evaluateChildren() + ) + else + visitNode(it) + }, + params = + if (node.type == MarkdownElementTypes.ORDERED_LIST) { + val listNumberNode = node.children.first().children.first() + mapOf( + "start" to text.substring( + listNumberNode.startOffset, + listNumberNode.endOffset + ).trim().dropLast(1) + ) + } else + emptyMap() + ) + } + + private fun resolveDRI(mdLink: String): DRI? = + mdLink + .removePrefix("[") + .removeSuffix("]") + .let { link -> + try { + java.net.URL(link) + null + } catch (e: MalformedURLException) { + try { + if (resolutionFacade != null && declarationDescriptor != null) { + resolveKDocLink( + resolutionFacade.resolveSession.bindingContext, + resolutionFacade, + declarationDescriptor, + null, + link.split('.') + ).minBy { it is ClassDescriptor }?.let { DRI.from(it) } + } else null + } catch (e1: IllegalArgumentException) { + null + } + } + } + + private fun referenceLinksHandler(node: ASTNode): DocTag { + val linkLabel = node.children.find { it.type == MarkdownElementTypes.LINK_LABEL } + ?: throw IllegalStateException("Wrong AST Tree. Reference link does not contain expected content") + val linkText = node.children.findLast { it.type == MarkdownElementTypes.LINK_TEXT } ?: linkLabel + + val linkKey = text.substring(linkLabel.startOffset, linkLabel.endOffset) + + val link = destinationLinksMap[linkKey.toLowerCase()] ?: linkKey + + return linksHandler(linkText, link) + } + + private fun inlineLinksHandler(node: ASTNode): DocTag { + val linkText = node.children.find { it.type == MarkdownElementTypes.LINK_TEXT } + ?: throw IllegalStateException("Wrong AST Tree. Inline link does not contain expected content") + val linkDestination = node.children.find { it.type == MarkdownElementTypes.LINK_DESTINATION } + ?: throw IllegalStateException("Wrong AST Tree. Inline link does not contain expected content") + val linkTitle = node.children.find { it.type == MarkdownElementTypes.LINK_TITLE } + + val link = text.substring(linkDestination.startOffset, linkDestination.endOffset) + + return linksHandler(linkText, link, linkTitle) + } + + private fun autoLinksHandler(node: ASTNode): DocTag { + val link = text.substring(node.startOffset + 1, node.endOffset - 1) + + return linksHandler(node, link) + } + + private fun linksHandler(linkText: ASTNode, link: String, linkTitle: ASTNode? = null): DocTag { + val dri: DRI? = resolveDRI(link) + val params = if (linkTitle == null) + mapOf("href" to link) + else + mapOf("href" to link, "title" to text.substring(linkTitle.startOffset + 1, linkTitle.endOffset - 1)) + + return DocTagsFromIElementFactory.getInstance( + MarkdownElementTypes.INLINE_LINK, + params = params, + children = linkText.children.drop(1).dropLast(1).evaluateChildren(), + dri = dri + ) + } + + private fun imagesHandler(node: ASTNode): DocTag { + val linkNode = + node.children.last().children.find { it.type == MarkdownElementTypes.LINK_LABEL }!!.children[1] + val link = text.substring(linkNode.startOffset, linkNode.endOffset) + val src = mapOf("src" to link) + return DocTagsFromIElementFactory.getInstance( + node.type, + params = src, + children = listOf(visitNode(node.children.last().children.find { it.type == MarkdownElementTypes.LINK_TEXT }!!)) + ) + } + + private fun codeSpansHandler(node: ASTNode): DocTag = + DocTagsFromIElementFactory.getInstance( + node.type, + children = listOf( + DocTagsFromIElementFactory.getInstance( + MarkdownTokenTypes.TEXT, + body = text.substring(node.startOffset + 1, node.endOffset - 1).replace('\n', ' ').trimIndent() + ) + + ) + ) + + private fun codeFencesHandler(node: ASTNode): DocTag = + DocTagsFromIElementFactory.getInstance( + node.type, + children = node + .children + .dropWhile { it.type != MarkdownTokenTypes.CODE_FENCE_CONTENT } + .dropLastWhile { it.type != MarkdownTokenTypes.CODE_FENCE_CONTENT } + .map { + if (it.type == MarkdownTokenTypes.EOL) + LeafASTNode(MarkdownTokenTypes.HARD_LINE_BREAK, 0, 0) + else + it + }.evaluateChildren(), + params = node + .children + .find { it.type == MarkdownTokenTypes.FENCE_LANG } + ?.let { mapOf("lang" to text.substring(it.startOffset, it.endOffset)) } + ?: emptyMap() + ) + + private fun codeBlocksHandler(node: ASTNode): DocTag = + DocTagsFromIElementFactory.getInstance(node.type, children = node.children.evaluateChildren()) + + private fun defaultHandler(node: ASTNode): DocTag = + DocTagsFromIElementFactory.getInstance( + MarkdownElementTypes.PARAGRAPH, + children = node.children.evaluateChildren() + ) + + fun visitNode(node: ASTNode): DocTag = + when (node.type) { + MarkdownElementTypes.ATX_1, + MarkdownElementTypes.ATX_2, + MarkdownElementTypes.ATX_3, + MarkdownElementTypes.ATX_4, + MarkdownElementTypes.ATX_5, + MarkdownElementTypes.ATX_6 -> headersHandler(node) + MarkdownTokenTypes.HORIZONTAL_RULE -> horizontalRulesHandler(node) + MarkdownElementTypes.STRONG, + MarkdownElementTypes.EMPH -> emphasisHandler(node) + MarkdownElementTypes.FULL_REFERENCE_LINK, + MarkdownElementTypes.SHORT_REFERENCE_LINK -> referenceLinksHandler(node) + MarkdownElementTypes.INLINE_LINK -> inlineLinksHandler(node) + MarkdownElementTypes.AUTOLINK -> autoLinksHandler(node) + MarkdownElementTypes.BLOCK_QUOTE -> blockquotesHandler(node) + MarkdownElementTypes.UNORDERED_LIST, + MarkdownElementTypes.ORDERED_LIST -> listsHandler(node) + MarkdownElementTypes.CODE_BLOCK -> codeBlocksHandler(node) + MarkdownElementTypes.CODE_FENCE -> codeFencesHandler(node) + MarkdownElementTypes.CODE_SPAN -> codeSpansHandler(node) + MarkdownElementTypes.IMAGE -> imagesHandler(node) + MarkdownTokenTypes.HARD_LINE_BREAK -> DocTagsFromIElementFactory.getInstance(node.type) + MarkdownTokenTypes.CODE_FENCE_CONTENT, + MarkdownTokenTypes.CODE_LINE, + MarkdownTokenTypes.TEXT -> DocTagsFromIElementFactory.getInstance( + MarkdownTokenTypes.TEXT, + body = text + .substring(node.startOffset, node.endOffset).transform() + ) + MarkdownElementTypes.MARKDOWN_FILE -> if (node.children.size == 1) visitNode(node.children.first()) else defaultHandler( + node + ) + GFMElementTypes.STRIKETHROUGH -> DocTagsFromIElementFactory.getInstance( + GFMElementTypes.STRIKETHROUGH, + body = text + .substring(node.startOffset, node.endOffset).transform() + ) + GFMElementTypes.TABLE -> DocTagsFromIElementFactory.getInstance( + GFMElementTypes.TABLE, + children = node.children.filterTabSeparators().evaluateChildren() + ) + GFMElementTypes.HEADER -> DocTagsFromIElementFactory.getInstance( + GFMElementTypes.HEADER, + children = node.children.filterTabSeparators().evaluateChildren() + ) + GFMElementTypes.ROW -> DocTagsFromIElementFactory.getInstance( + GFMElementTypes.ROW, + children = node.children.filterTabSeparators().evaluateChildren() + ) + else -> defaultHandler(node) + } + + private fun List.filterTabSeparators() = + this.filterNot { it.type == GFMTokenTypes.TABLE_SEPARATOR } + + private fun List.evaluateChildren(): List = + this.removeUselessTokens().mergeLeafASTNodes().map { visitNode(it) } + + private fun List.removeUselessTokens(): List = + this.filterIndexed { index, node -> + !(node.type == MarkdownElementTypes.LINK_DEFINITION || ( + node.type == MarkdownTokenTypes.EOL && + this.getOrNull(index - 1)?.type == MarkdownTokenTypes.HARD_LINE_BREAK + )) + } + + private val notLeafNodes = listOf(MarkdownTokenTypes.HORIZONTAL_RULE, MarkdownTokenTypes.HARD_LINE_BREAK) + + private fun List.isNotLeaf(index: Int): Boolean = + if (index in 0..this.lastIndex) + (this[index] is CompositeASTNode) || this[index].type in notLeafNodes + else + false + + private fun List.mergeLeafASTNodes(): List { + val children: MutableList = mutableListOf() + var index = 0 + while (index <= this.lastIndex) { + if (this.isNotLeaf(index)) { + children += this[index] + } else { + val startOffset = this[index].startOffset + while (index < this.lastIndex) { + if (this.isNotLeaf(index + 1) || this[index + 1].startOffset != this[index].endOffset) { + val endOffset = this[index].endOffset + if (text.substring(startOffset, endOffset).transform().trim().isNotEmpty()) + children += LeafASTNode(MarkdownTokenTypes.TEXT, startOffset, endOffset) + break + } + index++ + } + if (index == this.lastIndex) { + val endOffset = this[index].endOffset + if (text.substring(startOffset, endOffset).transform().trim().isNotEmpty()) + children += LeafASTNode(MarkdownTokenTypes.TEXT, startOffset, endOffset) + } + } + index++ + } + return children + } + + private fun String.transform() = this + .replace(Regex("\n\n+"), "") // Squashing new lines between paragraphs + .replace(Regex("\n"), " ") + .replace(Regex(" >+ +"), " ") // Replacement used in blockquotes, get rid of garbage + } + + + private fun getAllDestinationLinks(text: String, node: ASTNode): List> = + node.children + .filter { it.type == MarkdownElementTypes.LINK_DEFINITION } + .map { + text.substring(it.children[0].startOffset, it.children[0].endOffset).toLowerCase() to + text.substring(it.children[2].startOffset, it.children[2].endOffset) + } + + node.children.filterIsInstance().flatMap { getAllDestinationLinks(text, it) } + + + private fun markdownToDocNode(text: String): DocTag { + + val flavourDescriptor = GFMFlavourDescriptor() + val markdownAstRoot: ASTNode = IntellijMarkdownParser(flavourDescriptor).buildMarkdownTreeFromString(text) + + return MarkdownVisitor(text, getAllDestinationLinks(text, markdownAstRoot).toMap()).visitNode(markdownAstRoot) + } + + override fun parseStringToDocNode(extractedString: String) = markdownToDocNode(extractedString) + override fun preparse(text: String) = text + + private fun findParent(kDoc: PsiElement): PsiElement = + if (kDoc is KDocSection) findParent(kDoc.parent) else kDoc + + private fun getAllKDocTags(kDocImpl: PsiElement): List = + kDocImpl.children.filterIsInstance().filterNot { it is KDocSection } + kDocImpl.children.flatMap { + getAllKDocTags( + it + ) + } + + fun parseFromKDocTag(kDocTag: KDocTag?): DocumentationNode { + return if (kDocTag == null) + DocumentationNode(emptyList()) + else + DocumentationNode( + (listOf(kDocTag) + getAllKDocTags(findParent(kDocTag))).map { + when (it.knownTag) { + null -> if (it.name == null) Description(parseStringToDocNode(it.getContent())) else CustomTagWrapper( + parseStringToDocNode(it.getContent()), + it.name!! + ) + KDocKnownTag.AUTHOR -> Author(parseStringToDocNode(it.getContent())) + KDocKnownTag.THROWS -> Throws( + parseStringToDocNode(it.getContent()), + it.getSubjectName().orEmpty() + ) + KDocKnownTag.EXCEPTION -> Throws( + parseStringToDocNode(it.getContent()), + it.getSubjectName().orEmpty() + ) + KDocKnownTag.PARAM -> Param( + parseStringToDocNode(it.getContent()), + it.getSubjectName().orEmpty() + ) + KDocKnownTag.RECEIVER -> Receiver(parseStringToDocNode(it.getContent())) + KDocKnownTag.RETURN -> Return(parseStringToDocNode(it.getContent())) + KDocKnownTag.SEE -> See( + parseStringToDocNode(it.getContent()), + it.getSubjectName().orEmpty(), + parseStringToDocNode("[${it.getSubjectName()}]") + .let { + val link = it.children[0] + if (link is DocumentationLink) link.dri + else null + } + ) + KDocKnownTag.SINCE -> Since(parseStringToDocNode(it.getContent())) + KDocKnownTag.CONSTRUCTOR -> Constructor(parseStringToDocNode(it.getContent())) + KDocKnownTag.PROPERTY -> Property( + parseStringToDocNode(it.getContent()), + it.getSubjectName().orEmpty() + ) + KDocKnownTag.SAMPLE -> Sample( + parseStringToDocNode(it.getContent()), + it.getSubjectName().orEmpty() + ) + KDocKnownTag.SUPPRESS -> Suppress(parseStringToDocNode(it.getContent())) + } + } + ) + } +} \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/parsers/Parser.kt b/plugins/base/src/main/kotlin/parsers/Parser.kt new file mode 100644 index 00000000..1dd0c34a --- /dev/null +++ b/plugins/base/src/main/kotlin/parsers/Parser.kt @@ -0,0 +1,42 @@ +package org.jetbrains.dokka.base.parsers + +import org.jetbrains.dokka.model.doc.* + +abstract class Parser { + + abstract fun parseStringToDocNode(extractedString: String): DocTag + abstract fun preparse(text: String): String + + fun parse(text: String): DocumentationNode { + + val list = jkdocToListOfPairs(preparse(text)) + + val mappedList: List = list.map { + when(it.first) { + "description" -> Description(parseStringToDocNode(it.second)) + "author" -> Author(parseStringToDocNode(it.second)) + "version" -> Version(parseStringToDocNode(it.second)) + "since" -> Since(parseStringToDocNode(it.second)) + "see" -> See(parseStringToDocNode(it.second.substringAfter(' ')), it.second.substringBefore(' '), null) + "param" -> Param(parseStringToDocNode(it.second.substringAfter(' ')), it.second.substringBefore(' ')) + "property" -> Property(parseStringToDocNode(it.second.substringAfter(' ')), it.second.substringBefore(' ')) + "return" -> Return(parseStringToDocNode(it.second)) + "constructor" -> Constructor(parseStringToDocNode(it.second)) + "receiver" -> Receiver(parseStringToDocNode(it.second)) + "throws", "exception" -> Throws(parseStringToDocNode(it.second.substringAfter(' ')), it.second.substringBefore(' ')) + "deprecated" -> Deprecated(parseStringToDocNode(it.second)) + "sample" -> Sample(parseStringToDocNode(it.second.substringAfter(' ')), it.second.substringBefore(' ')) + "suppress" -> Suppress(parseStringToDocNode(it.second)) + else -> CustomTagWrapper(parseStringToDocNode(it.second), it.first) + } + } + return DocumentationNode(mappedList) + } + + private fun jkdocToListOfPairs(javadoc: String): List> = + "description $javadoc" + .split("\n@") + .map { + it.substringBefore(' ') to it.substringAfter(' ') + } +} \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt b/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt new file mode 100644 index 00000000..e81eb513 --- /dev/null +++ b/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromIElementFactory.kt @@ -0,0 +1,43 @@ +package org.jetbrains.dokka.base.parsers.factories + +import org.jetbrains.dokka.model.doc.* +import org.intellij.markdown.IElementType +import org.intellij.markdown.MarkdownElementTypes +import org.intellij.markdown.MarkdownTokenTypes +import org.intellij.markdown.flavours.gfm.GFMElementTypes +import org.jetbrains.dokka.links.DRI +import java.lang.NullPointerException + +object DocTagsFromIElementFactory { + fun getInstance(type: IElementType, children: List = emptyList(), params: Map = emptyMap(), body: String? = null, dri: DRI? = null) = + when(type) { + MarkdownElementTypes.SHORT_REFERENCE_LINK, + MarkdownElementTypes.FULL_REFERENCE_LINK, + MarkdownElementTypes.INLINE_LINK -> if(dri == null) A(children, params) else DocumentationLink(dri, children, params) + MarkdownElementTypes.STRONG -> B(children, params) + MarkdownElementTypes.BLOCK_QUOTE -> BlockQuote(children, params) + MarkdownElementTypes.CODE_SPAN, + MarkdownElementTypes.CODE_BLOCK, + MarkdownElementTypes.CODE_FENCE -> Code(children, params) + MarkdownElementTypes.ATX_1 -> H1(children, params) + MarkdownElementTypes.ATX_2 -> H2(children, params) + MarkdownElementTypes.ATX_3 -> H3(children, params) + MarkdownElementTypes.ATX_4 -> H4(children, params) + MarkdownElementTypes.ATX_5 -> H5(children, params) + MarkdownElementTypes.ATX_6 -> H6(children, params) + MarkdownElementTypes.EMPH -> I(children, params) + MarkdownElementTypes.IMAGE -> Img(children, params) + MarkdownElementTypes.LIST_ITEM -> Li(children, params) + MarkdownElementTypes.ORDERED_LIST -> Ol(children, params) + MarkdownElementTypes.UNORDERED_LIST -> Ul(children, params) + MarkdownElementTypes.PARAGRAPH -> P(children, params) + MarkdownTokenTypes.TEXT -> Text(body ?: throw NullPointerException("Text body should be at least empty string passed to DocNodes factory!"), children, params ) + MarkdownTokenTypes.HORIZONTAL_RULE -> HorizontalRule + MarkdownTokenTypes.HARD_LINE_BREAK -> Br + GFMElementTypes.STRIKETHROUGH -> Strikethrough(children, params) + GFMElementTypes.TABLE -> Table(children, params) + GFMElementTypes.HEADER -> Th(children, params) + GFMElementTypes.ROW -> Tr(children, params) + else -> CustomDocTag(children, params) + } +} \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromStringFactory.kt b/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromStringFactory.kt new file mode 100644 index 00000000..de7c3cad --- /dev/null +++ b/plugins/base/src/main/kotlin/parsers/factories/DocTagsFromStringFactory.kt @@ -0,0 +1,77 @@ +package org.jetbrains.dokka.base.parsers.factories + +import org.jetbrains.dokka.model.doc.* +import org.jetbrains.dokka.links.DRI +import java.lang.NullPointerException + +object DocTagsFromStringFactory { + fun getInstance(name: String, children: List = emptyList(), params: Map = emptyMap(), body: String? = null, dri: DRI? = null) = + when(name) { + "a" -> A(children, params) + "big" -> Big(children, params) + "b" -> B(children, params) + "blockquote" -> BlockQuote(children, params) + "br" -> Br + "cite" -> Cite(children, params) + "code" -> Code(children, params) + "dd" -> Dd(children, params) + "dfn" -> Dfn(children, params) + "dir" -> Dir(children, params) + "div" -> Div(children, params) + "dl" -> Dl(children, params) + "dt" -> Dt(children, params) + "Em" -> Em(children, params) + "font" -> Font(children, params) + "footer" -> Footer(children, params) + "frame" -> Frame(children, params) + "frameset" -> FrameSet(children, params) + "h1" -> H1(children, params) + "h2" -> H2(children, params) + "h3" -> H3(children, params) + "h4" -> H4(children, params) + "h5" -> H5(children, params) + "h6" -> H6(children, params) + "head" -> Head(children, params) + "header" -> Header(children, params) + "html" -> Html(children, params) + "i" -> I(children, params) + "iframe" -> IFrame(children, params) + "img" -> Img(children, params) + "input" -> Input(children, params) + "li" -> Li(children, params) + "link" -> Link(children, params) + "listing" -> Listing(children, params) + "main" -> Main(children, params) + "menu" -> Menu(children, params) + "meta" -> Meta(children, params) + "nav" -> Nav(children, params) + "noframes" -> NoFrames(children, params) + "noscript" -> NoScript(children, params) + "ol" -> Ol(children, params) + "p" -> P(children, params) + "pre" -> Pre(children, params) + "script" -> Script(children, params) + "section" -> Section(children, params) + "small" -> Small(children, params) + "span" -> Span(children, params) + "strong" -> Strong(children, params) + "sub" -> Sub(children, params) + "sup" -> Sup(children, params) + "table" -> Table(children, params) + "#text" -> Text(body ?: throw NullPointerException("Text body should be at least empty string passed to DocNodes factory!"), children, params) + "tBody" -> TBody(children, params) + "td" -> Td(children, params) + "tFoot" -> TFoot(children, params) + "th" -> Th(children, params) + "tHead" -> THead(children, params) + "title" -> Title(children, params) + "tr" -> Tr(children, params) + "tt" -> Tt(children, params) + "u" -> U(children, params) + "ul" -> Ul(children, params) + "var" -> Var(children, params) + "documentationlink" -> DocumentationLink(dri ?: throw NullPointerException("DRI cannot be passed null while constructing documentation link!"), children, params) + "hr" -> HorizontalRule + else -> CustomDocTag(children, params) + } +} \ No newline at end of file diff --git a/plugins/base/src/main/kotlin/transformers/documentables/ModuleAndPackageDocumentationTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/ModuleAndPackageDocumentationTransformer.kt index 2aab6018..63232793 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/ModuleAndPackageDocumentationTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/ModuleAndPackageDocumentationTransformer.kt @@ -1,10 +1,13 @@ package org.jetbrains.dokka.base.transformers.documentables +import org.jetbrains.dokka.analysis.EnvironmentAndFacade +import org.jetbrains.dokka.analysis.KotlinAnalysis import org.jetbrains.dokka.model.DModule import org.jetbrains.dokka.model.SourceSetData import org.jetbrains.dokka.model.doc.DocumentationNode import org.jetbrains.dokka.model.sourceSet -import org.jetbrains.dokka.parsers.MarkdownParser +import org.jetbrains.dokka.base.parsers.MarkdownParser +import org.jetbrains.dokka.model.SourceSetDependent import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.transformers.documentation.PreMergeDocumentableTransformer import org.jetbrains.kotlin.name.FqName @@ -12,9 +15,12 @@ import java.nio.file.Files import java.nio.file.Paths -internal class ModuleAndPackageDocumentationTransformer(val context: DokkaContext) : PreMergeDocumentableTransformer { +internal class ModuleAndPackageDocumentationTransformer( + private val context: DokkaContext, + private val kotlinAnalysis: KotlinAnalysis +) : PreMergeDocumentableTransformer { - override fun invoke(original: List): List { + override fun invoke(modules: List): List { val modulesAndPackagesDocumentation = context.configuration.passesConfigurations @@ -47,13 +53,12 @@ internal class ModuleAndPackageDocumentationTransformer(val context: DokkaContex } }.toMap() - return original.map { module -> + return modules.map { module -> val moduleDocumentation = module.sourceSets.mapNotNull { pd -> val doc = modulesAndPackagesDocumentation[Pair(module.name, pd)] - val facade = context.platforms[pd]?.facade - ?: return@mapNotNull null.also { context.logger.warn("Could not find platform data for ${pd.moduleName}/${pd.sourceSetID}") } + val facade = kotlinAnalysis[pd].facade try { doc?.get("Module")?.get(module.name)?.run { pd to MarkdownParser( @@ -71,8 +76,7 @@ internal class ModuleAndPackageDocumentationTransformer(val context: DokkaContex val packagesDocumentation = module.packages.map { it.name to it.sourceSets.mapNotNull { pd -> val doc = modulesAndPackagesDocumentation[Pair(module.name, pd)] - val facade = context.platforms[pd]?.facade - ?: return@mapNotNull null.also { context.logger.warn("Could not find platform data for ${pd.moduleName}/${pd.sourceSetID}") } + val facade = kotlinAnalysis[pd].facade val descriptor = facade.moduleDescriptor.getPackage(FqName(it.name.let { if(it == "[JS root]") "" else it })) doc?.get("Package")?.get(it.name)?.run { pd to MarkdownParser( diff --git a/plugins/base/src/main/kotlin/transformers/documentables/ReportUndocumentedTransformer.kt b/plugins/base/src/main/kotlin/transformers/documentables/ReportUndocumentedTransformer.kt index 7d475571..0b47345d 100644 --- a/plugins/base/src/main/kotlin/transformers/documentables/ReportUndocumentedTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/documentables/ReportUndocumentedTransformer.kt @@ -2,6 +2,7 @@ package org.jetbrains.dokka.base.transformers.documentables import org.jetbrains.dokka.DokkaConfiguration import org.jetbrains.dokka.DokkaConfiguration.PassConfiguration +import org.jetbrains.dokka.analysis.DescriptorDocumentableSource import org.jetbrains.dokka.model.* import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.transformers.documentation.DocumentableTransformer diff --git a/plugins/base/src/main/kotlin/transformers/pages/samples/SamplesTransformer.kt b/plugins/base/src/main/kotlin/transformers/pages/samples/SamplesTransformer.kt index 57655a9d..0d133a65 100644 --- a/plugins/base/src/main/kotlin/transformers/pages/samples/SamplesTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/pages/samples/SamplesTransformer.kt @@ -1,11 +1,11 @@ package org.jetbrains.dokka.base.transformers.pages.samples import com.intellij.psi.PsiElement -import org.jetbrains.dokka.DokkaGenerator -import org.jetbrains.dokka.EnvironmentAndFacade import org.jetbrains.dokka.Platform import org.jetbrains.dokka.analysis.AnalysisEnvironment +import org.jetbrains.dokka.analysis.DokkaMessageCollector import org.jetbrains.dokka.analysis.DokkaResolutionFacade +import org.jetbrains.dokka.analysis.EnvironmentAndFacade import org.jetbrains.dokka.base.renderers.platforms import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.SourceSetData @@ -41,7 +41,7 @@ abstract class SamplesTransformer(val context: DokkaContext) : PageTransformer { } private fun setUpAnalysis(context: DokkaContext) = context.configuration.passesConfigurations.map { - context.sourceSet(it) to AnalysisEnvironment(DokkaGenerator.DokkaMessageCollector(context.logger), it.analysisPlatform).run { + context.sourceSet(it) to AnalysisEnvironment(DokkaMessageCollector(context.logger), it.analysisPlatform).run { if (analysisPlatform == Platform.jvm) { addClasspath(PathUtil.getJdkClassesRootsFromCurrentJre()) } @@ -57,11 +57,16 @@ abstract class SamplesTransformer(val context: DokkaContext) : PageTransformer { } }.toMap() - private fun ContentNode.addSample(contentPage: ContentPage, platform: SourceSetData, fqName: String, analysis: Map): ContentNode { - val facade = analysis[platform]?.facade ?: - return this.also { context.logger.warn("Cannot resolve facade for platform ${platform.moduleName}")} - val psiElement = fqNameToPsiElement(facade, fqName) ?: - return this.also { context.logger.warn("Cannot find PsiElement corresponding to $fqName") } + private fun ContentNode.addSample( + contentPage: ContentPage, + platform: SourceSetData, + fqName: String, + analysis: Map + ): ContentNode { + val facade = analysis[platform]?.facade + ?: return this.also { context.logger.warn("Cannot resolve facade for platform ${platform.moduleName}") } + val psiElement = fqNameToPsiElement(facade, fqName) + ?: return this.also { context.logger.warn("Cannot find PsiElement corresponding to $fqName") } val imports = processImports(psiElement) // TODO: Process somehow imports. Maybe just attach them at the top of each body val body = processBody(psiElement) val node = contentCode(contentPage.platforms(), contentPage.dri, body, "kotlin") @@ -70,10 +75,14 @@ abstract class SamplesTransformer(val context: DokkaContext) : PageTransformer { } private fun ContentNode.dfs(fqName: String, node: ContentCode): ContentNode { - return when(this) { + return when (this) { is ContentHeader -> copy(children.map { it.dfs(fqName, node) }) is ContentDivergentGroup -> copy(children.map { it.dfs(fqName, node) } as List) - is ContentDivergentInstance -> copy(before.let { it?.dfs(fqName, node) }, divergent.let { it.dfs(fqName, node) }, after.let { it?.dfs(fqName, node) }) + is ContentDivergentInstance -> copy( + before.let { it?.dfs(fqName, node) }, + divergent.dfs(fqName, node), + after.let { it?.dfs(fqName, node) } + ) is ContentCode -> copy(children.map { it.dfs(fqName, node) }) is ContentDRILink -> copy(children.map { it.dfs(fqName, node) }) is ContentResolvedLink -> copy(children.map { it.dfs(fqName, node) }) @@ -90,10 +99,14 @@ abstract class SamplesTransformer(val context: DokkaContext) : PageTransformer { private fun fqNameToPsiElement(resolutionFacade: DokkaResolutionFacade, functionName: String): PsiElement? { val packageName = functionName.takeWhile { it != '.' } - val descriptor = resolutionFacade.resolveSession.getPackageFragment(FqName(packageName)) ?: - return null.also { context.logger.warn("Cannot find descriptor for package $packageName") } - val symbol = resolveKDocSampleLink(BindingContext.EMPTY, resolutionFacade, descriptor, functionName.split(".")).firstOrNull() ?: - return null.also { context.logger.warn("Unresolved function $functionName in @sample") } + val descriptor = resolutionFacade.resolveSession.getPackageFragment(FqName(packageName)) + ?: return null.also { context.logger.warn("Cannot find descriptor for package $packageName") } + val symbol = resolveKDocSampleLink( + BindingContext.EMPTY, + resolutionFacade, + descriptor, + functionName.split(".") + ).firstOrNull() ?: return null.also { context.logger.warn("Unresolved function $functionName in @sample") } return DescriptorToSourceUtils.descriptorToDeclaration(symbol) } diff --git a/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt b/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt index 8113d277..7603c80b 100644 --- a/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt +++ b/plugins/base/src/main/kotlin/transformers/pages/sourcelinks/SourceLinksTransformer.kt @@ -3,11 +3,11 @@ package org.jetbrains.dokka.base.transformers.pages.sourcelinks import com.intellij.psi.PsiElement import com.intellij.psi.PsiDocumentManager import org.jetbrains.dokka.DokkaConfiguration +import org.jetbrains.dokka.analysis.DescriptorDocumentableSource +import org.jetbrains.dokka.analysis.PsiDocumentableSource import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder -import org.jetbrains.dokka.model.DescriptorDocumentableSource import org.jetbrains.dokka.model.SourceSetData import org.jetbrains.dokka.model.DocumentableSource -import org.jetbrains.dokka.model.PsiDocumentableSource import org.jetbrains.dokka.model.WithExpectActual import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.plugability.DokkaContext diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index 49d8ae0f..cefea4ec 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -1,6 +1,6 @@ package org.jetbrains.dokka.base.translators.descriptors -import org.jetbrains.dokka.analysis.DokkaResolutionFacade +import org.jetbrains.dokka.analysis.* import org.jetbrains.dokka.links.* import org.jetbrains.dokka.links.Callable import org.jetbrains.dokka.model.* @@ -8,7 +8,7 @@ import org.jetbrains.dokka.model.Nullable import org.jetbrains.dokka.model.TypeConstructor import org.jetbrains.dokka.model.doc.* import org.jetbrains.dokka.model.properties.PropertyContainer -import org.jetbrains.dokka.parsers.MarkdownParser +import org.jetbrains.dokka.base.parsers.MarkdownParser import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.transformers.sources.SourceToDocumentableTranslator import org.jetbrains.dokka.utilities.DokkaLogger @@ -52,18 +52,19 @@ import org.jetbrains.kotlin.resolve.constants.EnumValue as ConstantsEnumValue import org.jetbrains.kotlin.resolve.constants.KClassValue as ConstantsKtClassValue import kotlin.IllegalArgumentException -object DefaultDescriptorToDocumentableTranslator : SourceToDocumentableTranslator { +class DefaultDescriptorToDocumentableTranslator( + private val kotlinAnalysis: KotlinAnalysis +) : SourceToDocumentableTranslator { override fun invoke(sourceSet: SourceSetData, context: DokkaContext): DModule { - - val (environment, facade) = context.platforms.getValue(sourceSet) + val (environment, facade) = kotlinAnalysis[sourceSet] val packageFragments = environment.getSourceFiles().asSequence() .map { it.packageFqName } .distinct() .mapNotNull { facade.resolveSession.getPackageFragment(it) } .toList() - return DokkaDescriptorVisitor(sourceSet, context.platforms.getValue(sourceSet).facade, context.logger).run { + return DokkaDescriptorVisitor(sourceSet, kotlinAnalysis[sourceSet].facade, context.logger).run { packageFragments.mapNotNull { it.safeAs() }.map { visitPackageFragmentDescriptor( it, diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt index a5117b09..e71d936a 100644 --- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt @@ -7,6 +7,10 @@ import com.intellij.openapi.vfs.VirtualFileManager import com.intellij.psi.* import com.intellij.psi.impl.source.PsiClassReferenceType import com.intellij.psi.impl.source.PsiImmediateClassType +import org.jetbrains.dokka.analysis.KotlinAnalysis +import org.jetbrains.dokka.analysis.PsiDocumentableSource +import org.jetbrains.dokka.analysis.from +import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.links.nextTarget import org.jetbrains.dokka.links.withClass @@ -17,6 +21,7 @@ import org.jetbrains.dokka.model.doc.Param import org.jetbrains.dokka.model.doc.Text import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.plugability.DokkaContext +import org.jetbrains.dokka.plugability.DokkaPlugin import org.jetbrains.dokka.transformers.sources.SourceToDocumentableTranslator import org.jetbrains.dokka.utilities.DokkaLogger import org.jetbrains.kotlin.asJava.elements.KtLightAbstractAnnotation @@ -33,15 +38,17 @@ import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull import org.jetbrains.kotlin.utils.addToStdlib.safeAs import java.io.File -object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { +class DefaultPsiToDocumentableTranslator( + private val kotlinAnalysis: KotlinAnalysis +) : SourceToDocumentableTranslator { - override fun invoke(sourceSetData: SourceSetData, context: DokkaContext): DModule { + override fun invoke(sourceSet: SourceSetData, context: DokkaContext): DModule { fun isFileInSourceRoots(file: File): Boolean { - return sourceSetData.sourceRoots.any { root -> file.path.startsWith(File(root.path).absolutePath) } + return sourceSet.sourceRoots.any { root -> file.path.startsWith(File(root.path).absolutePath) } } - val (environment, _) = context.platforms.getValue(sourceSetData) + val (environment, _) = kotlinAnalysis[sourceSet] val sourceRoots = environment.configuration.get(CLIConfigurationKeys.CONTENT_ROOTS) ?.filterIsInstance() @@ -59,11 +66,11 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { val docParser = DokkaPsiParser( - sourceSetData, + sourceSet, context.logger ) return DModule( - sourceSetData.moduleName, + sourceSet.moduleName, psiFiles.mapNotNull { it.safeAs() }.groupBy { it.packageName }.map { (packageName, psiFiles) -> val dri = DRI(packageName = packageName) DPackage( @@ -76,12 +83,12 @@ object DefaultPsiToDocumentableTranslator : SourceToDocumentableTranslator { emptyList(), emptyMap(), null, - setOf(sourceSetData) + setOf(sourceSet) ) }, emptyMap(), null, - setOf(sourceSetData) + setOf(sourceSet) ) } diff --git a/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt b/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt index d8130974..7119ba68 100644 --- a/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt +++ b/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt @@ -6,6 +6,7 @@ import com.intellij.psi.impl.source.tree.JavaDocElementType import com.intellij.psi.impl.source.tree.LeafPsiElement import com.intellij.psi.javadoc.* import com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.dokka.analysis.from import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.doc.* import org.jetbrains.dokka.model.doc.Deprecated diff --git a/plugins/base/src/test/kotlin/basic/FailOnWarningTest.kt b/plugins/base/src/test/kotlin/basic/FailOnWarningTest.kt index 2c805110..d548b75b 100644 --- a/plugins/base/src/test/kotlin/basic/FailOnWarningTest.kt +++ b/plugins/base/src/test/kotlin/basic/FailOnWarningTest.kt @@ -28,7 +28,7 @@ class FailOnWarningTest : AbstractCoreTest() { |package sample """.trimIndent(), configuration ) { - analysisSetupStage = { + pluginsSetupStage = { logger.warn("Warning!") } } @@ -53,7 +53,7 @@ class FailOnWarningTest : AbstractCoreTest() { |package sample """.trimIndent(), configuration ) { - analysisSetupStage = { + pluginsSetupStage = { logger.error("Error!") } } @@ -102,7 +102,7 @@ class FailOnWarningTest : AbstractCoreTest() { |package sample """.trimIndent(), configuration ) { - analysisSetupStage = { + pluginsSetupStage = { logger.warn("Error!") logger.error("Error!") } diff --git a/plugins/build.gradle.kts b/plugins/build.gradle.kts index d681cf2a..bb3b1a3d 100644 --- a/plugins/build.gradle.kts +++ b/plugins/build.gradle.kts @@ -6,8 +6,9 @@ subprojects { dependencies { compileOnly(project(":core")) -// compileOnly(project(":coreDependencies", configuration = "shadow")) // uncomment if IntelliJ does not recognize pacakges from IntelliJ implementation(kotlin("stdlib-jdk8")) + implementation(kotlin("stdlib")) + implementation(kotlin("reflect")) testImplementation(project(":testApi")) testImplementation("org.junit.jupiter:junit-jupiter:5.6.0") diff --git a/plugins/javadoc/src/main/kotlin/javadoc/pages/JavadocPageNodes.kt b/plugins/javadoc/src/main/kotlin/javadoc/pages/JavadocPageNodes.kt index c148a440..becacd26 100644 --- a/plugins/javadoc/src/main/kotlin/javadoc/pages/JavadocPageNodes.kt +++ b/plugins/javadoc/src/main/kotlin/javadoc/pages/JavadocPageNodes.kt @@ -2,6 +2,9 @@ package javadoc.pages import com.intellij.psi.PsiClass import org.jetbrains.dokka.Platform +import org.jetbrains.dokka.analysis.DescriptorDocumentableSource +import org.jetbrains.dokka.analysis.PsiDocumentableSource +import org.jetbrains.dokka.analysis.from import org.jetbrains.dokka.base.renderers.platforms import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.* diff --git a/plugins/javadoc/src/test/kotlin/javadoc/JavadocTest.kt b/plugins/javadoc/src/test/kotlin/javadoc/JavadocTest.kt index 45e97945..905cb61b 100644 --- a/plugins/javadoc/src/test/kotlin/javadoc/JavadocTest.kt +++ b/plugins/javadoc/src/test/kotlin/javadoc/JavadocTest.kt @@ -66,4 +66,4 @@ class JavadocTest : AbstractCoreTest() { // } // } // } -} \ No newline at end of file +} diff --git a/runners/cli/build.gradle.kts b/runners/cli/build.gradle.kts index 6c3a4d7d..8c4955ca 100644 --- a/runners/cli/build.gradle.kts +++ b/runners/cli/build.gradle.kts @@ -12,6 +12,7 @@ dependencies { implementation("org.jetbrains.kotlinx:kotlinx-cli-jvm:0.2.1") implementation("com.google.code.gson:gson:2.8.5") implementation(project(":core")) + implementation(kotlin("stdlib")) } diff --git a/runners/cli/src/main/kotlin/cli/main.kt b/runners/cli/src/main/kotlin/cli/main.kt index f0df08e0..80971ae2 100644 --- a/runners/cli/src/main/kotlin/cli/main.kt +++ b/runners/cli/src/main/kotlin/cli/main.kt @@ -5,7 +5,7 @@ import kotlinx.cli.* import org.jetbrains.dokka.DokkaConfiguration.ExternalDocumentationLink import org.jetbrains.dokka.DokkaConfiguration.PassConfiguration.* import org.jetbrains.dokka.utilities.DokkaConsoleLogger -import org.jetbrains.kotlin.utils.addToStdlib.cast +import org.jetbrains.dokka.utilities.cast import java.io.* import java.net.MalformedURLException import java.net.URL diff --git a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/main.kt b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/main.kt index b7961b6e..41d6e67a 100644 --- a/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/main.kt +++ b/runners/gradle-plugin/src/main/kotlin/org/jetbrains/dokka/gradle/main.kt @@ -3,6 +3,7 @@ package org.jetbrains.dokka.gradle import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.artifacts.Configuration +import org.gradle.api.attributes.Usage import org.gradle.util.GradleVersion import java.io.File import java.io.InputStream @@ -20,9 +21,8 @@ open class DokkaPlugin : Plugin { val pluginsConfiguration = project.configurations.create("dokkaPlugins").apply { dependencies.add(project.dependencies.create("org.jetbrains.dokka:dokka-base:${DokkaVersion.version}")) attributes.attribute( - org.gradle.api.attributes.Usage.USAGE_ATTRIBUTE, - project.objects.named(org.gradle.api.attributes.Usage::class.java, "java-runtime") - ) + Usage.USAGE_ATTRIBUTE, project.objects.named(Usage::class.java, "java-runtime") + ) isCanBeConsumed = false } addDokkaTasks(project, dokkaRuntimeConfiguration, pluginsConfiguration, DokkaTask::class.java) diff --git a/settings.gradle.kts b/settings.gradle.kts index c813518d..bb91edc6 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,13 +1,15 @@ rootProject.name = "dokka" include("core") +include("core:dependencies") include("plugins:base:search-component") -include("coreDependencies") include("testApi") include("test-tools") include("runners:gradle-plugin") include("runners:cli") include("runners:maven-plugin") +include("kotlin-analysis") +include("kotlin-analysis:dependencies") include("plugins:base") include("plugins:base:frontend") include("plugins:mathjax") @@ -27,7 +29,7 @@ pluginManagement { } repositories { - maven(url="https://dl.bintray.com/kotlin/kotlin-dev/") + maven(url = "https://dl.bintray.com/kotlin/kotlin-dev/") mavenLocal() mavenCentral() jcenter() diff --git a/test-tools/build.gradle.kts b/test-tools/build.gradle.kts index 7fd8a879..a61787a8 100644 --- a/test-tools/build.gradle.kts +++ b/test-tools/build.gradle.kts @@ -1,5 +1,6 @@ dependencies { implementation(project(":testApi")) implementation(kotlin("stdlib-jdk8")) + implementation(kotlin("reflect")) implementation("com.willowtreeapps.assertk:assertk-jvm:0.22") } \ No newline at end of file diff --git a/testApi/build.gradle.kts b/testApi/build.gradle.kts index 43c984b5..c8b510a1 100644 --- a/testApi/build.gradle.kts +++ b/testApi/build.gradle.kts @@ -7,8 +7,10 @@ plugins { dependencies { api(project(":core")) + implementation(project(":kotlin-analysis")) implementation("junit:junit:4.13") // TODO: remove dependency to junit implementation(kotlin("stdlib")) + implementation(kotlin("reflect")) } val sourceJar by tasks.registering(Jar::class) { diff --git a/testApi/src/main/kotlin/testApi/context/MockContext.kt b/testApi/src/main/kotlin/testApi/context/MockContext.kt index 11520280..e3da5f68 100644 --- a/testApi/src/main/kotlin/testApi/context/MockContext.kt +++ b/testApi/src/main/kotlin/testApi/context/MockContext.kt @@ -1,7 +1,6 @@ package org.jetbrains.dokka.testApi.context import org.jetbrains.dokka.DokkaConfiguration -import org.jetbrains.dokka.EnvironmentAndFacade import org.jetbrains.dokka.model.SourceSetCache import org.jetbrains.dokka.model.SourceSetData import org.jetbrains.dokka.plugability.DokkaContext @@ -16,7 +15,6 @@ import kotlin.reflect.full.memberProperties class MockContext( vararg extensions: Pair, (DokkaContext) -> Any>, private val testConfiguration: DokkaConfiguration? = null, - private val testSourceSets: Map? = null, private val unusedExtensionPoints: List>? = null, override val sourceSetCache: SourceSetCache ) : DokkaContext { @@ -41,8 +39,6 @@ class MockContext( override val configuration: DokkaConfiguration get() = testConfiguration ?: throw IllegalStateException("This mock context doesn't provide configuration") - override val platforms: Map - get() = testSourceSets ?: throw IllegalStateException("This mock context doesn't provide platforms data") override val unusedPoints: Collection> get() = unusedExtensionPoints ?: throw IllegalStateException("This mock context doesn't provide unused extension points") diff --git a/testApi/src/main/kotlin/testApi/testRunner/DokkaTestGenerator.kt b/testApi/src/main/kotlin/testApi/testRunner/DokkaTestGenerator.kt index 2ca8523b..7248a4cb 100644 --- a/testApi/src/main/kotlin/testApi/testRunner/DokkaTestGenerator.kt +++ b/testApi/src/main/kotlin/testApi/testRunner/DokkaTestGenerator.kt @@ -2,7 +2,6 @@ package org.jetbrains.dokka.testApi.testRunner import org.jetbrains.dokka.DokkaConfiguration import org.jetbrains.dokka.DokkaGenerator -import org.jetbrains.dokka.EnvironmentAndFacade import org.jetbrains.dokka.model.SourceSetCache import org.jetbrains.dokka.model.SourceSetData import org.jetbrains.dokka.plugability.DokkaPlugin @@ -19,17 +18,11 @@ internal class DokkaTestGenerator( val dokkaGenerator = DokkaGenerator(configuration, logger) val sourceSetsCache = SourceSetCache() - val platforms: Map = dokkaGenerator.setUpAnalysis( - configuration, - sourceSetsCache - ) - analysisSetupStage(platforms) - val context = - dokkaGenerator.initializePlugins(configuration, logger, platforms, sourceSetsCache, pluginOverrides) + dokkaGenerator.initializePlugins(configuration, logger, sourceSetsCache, pluginOverrides) pluginsSetupStage(context) - val modulesFromPlatforms = dokkaGenerator.createDocumentationModels(platforms, context) + val modulesFromPlatforms = dokkaGenerator.createDocumentationModels(context, sourceSetsCache) documentablesCreationStage(modulesFromPlatforms) val filteredModules = dokkaGenerator.transformDocumentationModelBeforeMerge(modulesFromPlatforms, context) diff --git a/testApi/src/main/kotlin/testApi/testRunner/TestRunner.kt b/testApi/src/main/kotlin/testApi/testRunner/TestRunner.kt index 9df20138..88595f85 100644 --- a/testApi/src/main/kotlin/testApi/testRunner/TestRunner.kt +++ b/testApi/src/main/kotlin/testApi/testRunner/TestRunner.kt @@ -106,7 +106,6 @@ abstract class AbstractCoreTest { } protected class TestBuilder { - var analysisSetupStage: (Map) -> Unit = {} var pluginsSetupStage: (DokkaContext) -> Unit = {} var documentablesCreationStage: (List) -> Unit = {} var documentablesFirstTransformationStep: (List) -> Unit = {} @@ -118,7 +117,6 @@ abstract class AbstractCoreTest { @PublishedApi internal fun build() = TestMethods( - analysisSetupStage, pluginsSetupStage, documentablesCreationStage, documentablesFirstTransformationStep, @@ -240,7 +238,6 @@ abstract class AbstractCoreTest { } data class TestMethods( - val analysisSetupStage: (Map) -> Unit, val pluginsSetupStage: (DokkaContext) -> Unit, val documentablesCreationStage: (List) -> Unit, val documentablesFirstTransformationStep: (List) -> Unit, -- cgit From 7001425367a4db79c6fcdf6b8d0c53508ba50aa7 Mon Sep 17 00:00:00 2001 From: Aurimas Liutikas Date: Mon, 13 Jul 2020 18:33:28 -0700 Subject: Simplify publishing configuration and enable sourcejars --- .../org/jetbrains/PublicationConfiguration.kt | 34 ++++++++++++++++++++++ core/build.gradle.kts | 19 ++---------- core/dependencies/build.gradle.kts | 13 ++------- kotlin-analysis/build.gradle.kts | 13 ++------- kotlin-analysis/dependencies/build.gradle.kts | 15 ++-------- plugins/base/build.gradle.kts | 13 ++------- plugins/gfm/build.gradle.kts | 15 ++-------- plugins/javadoc/build.gradle.kts | 13 ++------- plugins/jekyll/build.gradle.kts | 15 ++-------- plugins/kotlin-as-java/build.gradle.kts | 13 ++------- plugins/mathjax/build.gradle.kts | 13 ++------- plugins/xml/build.gradle.kts | 11 ++----- runners/cli/build.gradle.kts | 13 ++------- runners/maven-plugin/build.gradle.kts | 15 ++-------- testApi/build.gradle.kts | 19 ++---------- 15 files changed, 65 insertions(+), 169 deletions(-) create mode 100644 buildSrc/src/main/kotlin/org/jetbrains/PublicationConfiguration.kt (limited to 'testApi/build.gradle.kts') diff --git a/buildSrc/src/main/kotlin/org/jetbrains/PublicationConfiguration.kt b/buildSrc/src/main/kotlin/org/jetbrains/PublicationConfiguration.kt new file mode 100644 index 00000000..5dbb8fb9 --- /dev/null +++ b/buildSrc/src/main/kotlin/org/jetbrains/PublicationConfiguration.kt @@ -0,0 +1,34 @@ +package org.jetbrains + +import com.github.jengelman.gradle.plugins.shadow.ShadowExtension +import org.gradle.api.Project +import org.gradle.api.plugins.JavaPlugin +import org.gradle.api.plugins.JavaPluginExtension +import org.gradle.api.publish.PublishingExtension +import org.gradle.api.publish.maven.MavenPublication +import org.gradle.kotlin.dsl.configure +import org.gradle.kotlin.dsl.create +import org.gradle.kotlin.dsl.get + +fun Project.configurePublication(artifactId: String, useShadow: Boolean = false) { + configure { + publications { + create("maven") { + this.artifactId = artifactId + if (useShadow) { + val shadow = extensions.getByType(ShadowExtension::class.java) + shadow.component(this) + } else { + from(components["java"]) + } + } + } + } + configureBintrayPublication("maven") + plugins.all { + if (this is JavaPlugin) { + val extension = extensions.getByType(JavaPluginExtension::class.java) + @Suppress("UnstableApiUsage") extension.withSourcesJar() + } + } +} diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 45ddccf3..030db5c6 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configureBintrayPublication +import org.jetbrains.configurePublication plugins { `maven-publish` @@ -16,11 +16,6 @@ dependencies { testImplementation(kotlin("test-junit")) } -val sourceJar by tasks.registering(Jar::class) { - archiveClassifier.set("sources") - from(sourceSets["main"].allSource) -} - tasks { processResources { val dokka_version: String by project @@ -34,14 +29,4 @@ tasks { } } -publishing { - publications { - register("dokkaCore") { - artifactId = "dokka-core" - from(components["java"]) - artifact(sourceJar.get()) - } - } -} - -configureBintrayPublication("dokkaCore") +configurePublication("dokka-core") diff --git a/core/dependencies/build.gradle.kts b/core/dependencies/build.gradle.kts index a26d109e..082861b6 100644 --- a/core/dependencies/build.gradle.kts +++ b/core/dependencies/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configureBintrayPublication +import org.jetbrains.configurePublication plugins { id("com.github.johnrengelman.shadow") @@ -28,13 +28,4 @@ tasks { } } -publishing { - publications { - register("dokkaCoreDependencies") { - artifactId = "dokka-core-dependencies" - project.shadow.component(this) - } - } -} - -configureBintrayPublication("dokkaCoreDependencies") +configurePublication("dokka-core-dependencies", useShadow = true) diff --git a/kotlin-analysis/build.gradle.kts b/kotlin-analysis/build.gradle.kts index 67480f82..5acc1f06 100644 --- a/kotlin-analysis/build.gradle.kts +++ b/kotlin-analysis/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configureBintrayPublication +import org.jetbrains.configurePublication plugins { id("com.github.johnrengelman.shadow") @@ -15,13 +15,4 @@ dependencies { api(project(":kotlin-analysis:dependencies", configuration = "shadow")) } -publishing { - publications { - register("analysis") { - artifactId = "dokka-analysis" - from(components["java"]) - } - } -} - -configureBintrayPublication("analysis") +configurePublication("dokka-analysis") diff --git a/kotlin-analysis/dependencies/build.gradle.kts b/kotlin-analysis/dependencies/build.gradle.kts index 5bba6422..1fd13630 100644 --- a/kotlin-analysis/dependencies/build.gradle.kts +++ b/kotlin-analysis/dependencies/build.gradle.kts @@ -1,5 +1,4 @@ -import org.jetbrains.configureBintrayPublication - +import org.jetbrains.configurePublication plugins { id("com.github.johnrengelman.shadow") @@ -54,14 +53,4 @@ tasks { } } - -publishing { - publications { - register("kotlin-analysis-dependencies") { - artifactId = "kotlin-analysis-dependencies" - project.shadow.component(this) - } - } -} - -configureBintrayPublication("kotlin-analysis-dependencies") +configurePublication("kotlin-analysis-dependencies", useShadow = true) diff --git a/plugins/base/build.gradle.kts b/plugins/base/build.gradle.kts index 1fe3dbc2..61e2ef01 100644 --- a/plugins/base/build.gradle.kts +++ b/plugins/base/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configureBintrayPublication +import org.jetbrains.configurePublication plugins { id("com.jfrog.bintray") @@ -30,13 +30,4 @@ tasks { } } -publishing { - publications { - register("basePlugin") { - artifactId = "dokka-base" - from(components["java"]) - } - } -} - -configureBintrayPublication("basePlugin") +configurePublication("dokka-base") diff --git a/plugins/gfm/build.gradle.kts b/plugins/gfm/build.gradle.kts index fac2c1c0..73958046 100644 --- a/plugins/gfm/build.gradle.kts +++ b/plugins/gfm/build.gradle.kts @@ -1,18 +1,9 @@ -import org.jetbrains.configureBintrayPublication - -publishing { - publications { - register("gfmPlugin") { - artifactId = "gfm-plugin" - from(components["java"]) - } - } -} - -configureBintrayPublication("gfmPlugin") +import org.jetbrains.configurePublication dependencies { implementation(project(":plugins:base")) testImplementation(project(":plugins:base")) testImplementation(project(":plugins:base:test-utils")) } + +configurePublication("gfm-plugin") diff --git a/plugins/javadoc/build.gradle.kts b/plugins/javadoc/build.gradle.kts index ebf79f8f..5846ee62 100644 --- a/plugins/javadoc/build.gradle.kts +++ b/plugins/javadoc/build.gradle.kts @@ -1,13 +1,4 @@ -import org.jetbrains.configureBintrayPublication - -publishing { - publications { - register("javadocPlugin") { - artifactId = "javadoc-plugin" - from(components["java"]) - } - } -} +import org.jetbrains.configurePublication dependencies { implementation("com.soywiz.korlibs.korte:korte-jvm:1.10.3") @@ -19,4 +10,4 @@ dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version") } -configureBintrayPublication("javadocPlugin") +configurePublication("javadoc-plugin") diff --git a/plugins/jekyll/build.gradle.kts b/plugins/jekyll/build.gradle.kts index 90002466..0d1e38d6 100644 --- a/plugins/jekyll/build.gradle.kts +++ b/plugins/jekyll/build.gradle.kts @@ -1,17 +1,8 @@ -import org.jetbrains.configureBintrayPublication - -publishing { - publications { - register("jekyllPlugin") { - artifactId = "jekyll-plugin" - from(components["java"]) - } - } -} - -configureBintrayPublication("jekyllPlugin") +import org.jetbrains.configurePublication dependencies { implementation(project(":plugins:base")) implementation(project(":plugins:gfm")) } + +configurePublication("jekyll-plugin") diff --git a/plugins/kotlin-as-java/build.gradle.kts b/plugins/kotlin-as-java/build.gradle.kts index bd08ab92..27e90f84 100644 --- a/plugins/kotlin-as-java/build.gradle.kts +++ b/plugins/kotlin-as-java/build.gradle.kts @@ -1,13 +1,4 @@ -import org.jetbrains.configureBintrayPublication - -publishing { - publications { - register("kotlinAsJavaPlugin") { - artifactId = "kotlin-as-java-plugin" - from(components["java"]) - } - } -} +import org.jetbrains.configurePublication dependencies { implementation(project(":plugins:base")) @@ -16,4 +7,4 @@ dependencies { testImplementation(project(":test-tools")) } -configureBintrayPublication("kotlinAsJavaPlugin") \ No newline at end of file +configurePublication("kotlin-as-java-plugin") diff --git a/plugins/mathjax/build.gradle.kts b/plugins/mathjax/build.gradle.kts index 67f4822f..ea4cdff0 100644 --- a/plugins/mathjax/build.gradle.kts +++ b/plugins/mathjax/build.gradle.kts @@ -1,12 +1,3 @@ -import org.jetbrains.configureBintrayPublication +import org.jetbrains.configurePublication -publishing { - publications { - register("mathjaxPlugin") { - artifactId = "mathjax-plugin" - from(components["java"]) - } - } -} - -configureBintrayPublication("mathjaxPlugin") +configurePublication("mathjax-plugin") diff --git a/plugins/xml/build.gradle.kts b/plugins/xml/build.gradle.kts index cd65cfe4..5d3984bc 100644 --- a/plugins/xml/build.gradle.kts +++ b/plugins/xml/build.gradle.kts @@ -1,8 +1,3 @@ -publishing { - publications { - register("xmlPlugin") { - artifactId = "xml-plugin" - from(components["java"]) - } - } -} \ No newline at end of file +import org.jetbrains.configurePublication + +configurePublication("xml-plugin") \ No newline at end of file diff --git a/runners/cli/build.gradle.kts b/runners/cli/build.gradle.kts index 00e459b2..19a49346 100644 --- a/runners/cli/build.gradle.kts +++ b/runners/cli/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configureBintrayPublication +import org.jetbrains.configurePublication plugins { id("com.github.johnrengelman.shadow") @@ -15,7 +15,6 @@ dependencies { implementation(kotlin("stdlib")) } - tasks { shadowJar { val dokka_version: String by project @@ -27,13 +26,5 @@ tasks { } } -publishing { - publications { - register("dokkaCli") { - artifactId = "dokka-cli" - project.shadow.component(this) - } - } -} +configurePublication("dokka-cli", useShadow = true) -configureBintrayPublication("dokkaCli") diff --git a/runners/maven-plugin/build.gradle.kts b/runners/maven-plugin/build.gradle.kts index 6214da6d..83d9f5af 100644 --- a/runners/maven-plugin/build.gradle.kts +++ b/runners/maven-plugin/build.gradle.kts @@ -1,7 +1,6 @@ -import org.jetbrains.configureBintrayPublication import org.jetbrains.CrossPlatformExec import org.jetbrains.SetupMaven - +import org.jetbrains.configurePublication val setupMaven by tasks.register("setupMaven") @@ -91,14 +90,4 @@ tasks.named("jar") { } } -publishing { - publications { - register("dokkaMavenPlugin") { - artifactId = "dokka-maven-plugin" - from(components["java"]) - artifact(sourceJar.get()) - } - } -} - -configureBintrayPublication("dokkaMavenPlugin") +configurePublication("dokka-maven-plugin") diff --git a/testApi/build.gradle.kts b/testApi/build.gradle.kts index c8b510a1..1576dd99 100644 --- a/testApi/build.gradle.kts +++ b/testApi/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configureBintrayPublication +import org.jetbrains.configurePublication plugins { `maven-publish` @@ -13,19 +13,4 @@ dependencies { implementation(kotlin("reflect")) } -val sourceJar by tasks.registering(Jar::class) { - archiveClassifier.set("sources") - from(sourceSets["main"].allSource) -} - -publishing { - publications { - register("dokkaTestAPI") { - artifactId = "dokka-test-api" - from(components["java"]) - artifact(sourceJar.get()) - } - } -} - -configureBintrayPublication("dokkaTestAPI") \ No newline at end of file +configurePublication("dokka-test-api") -- cgit From 7ff409fdf621932590dbf0118fff2c73dc76b951 Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Tue, 14 Jul 2020 10:06:04 +0200 Subject: Adapt changes from "Simplify publishing configuration and enable sourcejars" --- build.gradle.kts | 2 + .../main/kotlin/org/jetbrains/BintrayPublishing.kt | 29 ----------- .../org/jetbrains/PublicationConfiguration.kt | 34 ------------- .../src/main/kotlin/org/jetbrains/publication.kt | 58 ++++++++++++++++++++++ core/build.gradle.kts | 6 ++- core/dependencies/build.gradle.kts | 8 ++- kotlin-analysis/build.gradle.kts | 6 ++- kotlin-analysis/dependencies/build.gradle.kts | 8 ++- plugins/base/build.gradle.kts | 6 ++- plugins/gfm/build.gradle.kts | 6 ++- plugins/javadoc/build.gradle.kts | 6 ++- plugins/jekyll/build.gradle.kts | 6 ++- plugins/kotlin-as-java/build.gradle.kts | 6 ++- plugins/mathjax/build.gradle.kts | 6 ++- runners/cli/build.gradle.kts | 8 ++- runners/gradle-plugin/build.gradle.kts | 4 +- runners/maven-plugin/build.gradle.kts | 11 ++-- testApi/build.gradle.kts | 6 ++- 18 files changed, 123 insertions(+), 93 deletions(-) delete mode 100644 buildSrc/src/main/kotlin/org/jetbrains/BintrayPublishing.kt delete mode 100644 buildSrc/src/main/kotlin/org/jetbrains/PublicationConfiguration.kt create mode 100644 buildSrc/src/main/kotlin/org/jetbrains/publication.kt (limited to 'testApi/build.gradle.kts') diff --git a/build.gradle.kts b/build.gradle.kts index 7cc0394f..c6335a31 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -42,6 +42,8 @@ subprojects { // Gradle metadata java { + @Suppress("UnstableApiUsage") + withSourcesJar() targetCompatibility = JavaVersion.VERSION_1_8 } } diff --git a/buildSrc/src/main/kotlin/org/jetbrains/BintrayPublishing.kt b/buildSrc/src/main/kotlin/org/jetbrains/BintrayPublishing.kt deleted file mode 100644 index 4180c445..00000000 --- a/buildSrc/src/main/kotlin/org/jetbrains/BintrayPublishing.kt +++ /dev/null @@ -1,29 +0,0 @@ -package org.jetbrains - -import com.jfrog.bintray.gradle.BintrayExtension -import org.gradle.api.Project -import org.gradle.kotlin.dsl.provideDelegate - -fun Project.configureBintrayPublication(vararg publications: String) { - val dokka_version: String by this - val dokka_publication_channel: String by this - extensions.configure("bintray") { - user = System.getenv("BINTRAY_USER") - key = System.getenv("BINTRAY_KEY") - dryRun = System.getenv("BINTRAY_DRY_RUN") == "true" || - project.properties["bintray_dry_run"] == "true" - pkg = PackageConfig().apply { - repo = dokka_publication_channel - name = "dokka" - userOrg = "kotlin" - desc = "Dokka, the Kotlin documentation tool" - vcsUrl = "https://github.com/kotlin/dokka.git" - setLicenses("Apache-2.0") - version = VersionConfig().apply { - name = dokka_version - } - } - setPublications(*publications) - } -} - diff --git a/buildSrc/src/main/kotlin/org/jetbrains/PublicationConfiguration.kt b/buildSrc/src/main/kotlin/org/jetbrains/PublicationConfiguration.kt deleted file mode 100644 index 5dbb8fb9..00000000 --- a/buildSrc/src/main/kotlin/org/jetbrains/PublicationConfiguration.kt +++ /dev/null @@ -1,34 +0,0 @@ -package org.jetbrains - -import com.github.jengelman.gradle.plugins.shadow.ShadowExtension -import org.gradle.api.Project -import org.gradle.api.plugins.JavaPlugin -import org.gradle.api.plugins.JavaPluginExtension -import org.gradle.api.publish.PublishingExtension -import org.gradle.api.publish.maven.MavenPublication -import org.gradle.kotlin.dsl.configure -import org.gradle.kotlin.dsl.create -import org.gradle.kotlin.dsl.get - -fun Project.configurePublication(artifactId: String, useShadow: Boolean = false) { - configure { - publications { - create("maven") { - this.artifactId = artifactId - if (useShadow) { - val shadow = extensions.getByType(ShadowExtension::class.java) - shadow.component(this) - } else { - from(components["java"]) - } - } - } - } - configureBintrayPublication("maven") - plugins.all { - if (this is JavaPlugin) { - val extension = extensions.getByType(JavaPluginExtension::class.java) - @Suppress("UnstableApiUsage") extension.withSourcesJar() - } - } -} diff --git a/buildSrc/src/main/kotlin/org/jetbrains/publication.kt b/buildSrc/src/main/kotlin/org/jetbrains/publication.kt new file mode 100644 index 00000000..47fb23f2 --- /dev/null +++ b/buildSrc/src/main/kotlin/org/jetbrains/publication.kt @@ -0,0 +1,58 @@ +package org.jetbrains + +import com.github.jengelman.gradle.plugins.shadow.ShadowExtension +import com.jfrog.bintray.gradle.BintrayExtension +import org.gradle.api.Project +import org.gradle.api.publish.PublishingExtension +import org.gradle.api.publish.maven.MavenPublication +import org.gradle.kotlin.dsl.* + +class DokkaPublicationBuilder { + enum class Component { + Java, Shadow + } + var artifactId: String? = null + var component: Component = Component.Java +} + +fun Project.registerDokkaArtifactPublication(publicationName: String, configure: DokkaPublicationBuilder.() -> Unit) { + configure { + publications { + register(publicationName) { + val builder = DokkaPublicationBuilder().apply(configure) + this.artifactId = builder.artifactId + when (builder.component) { + DokkaPublicationBuilder.Component.Java -> from(components["java"]) + DokkaPublicationBuilder.Component.Shadow -> + extensions.getByType(ShadowExtension::class.java).component(this) + } + } + } + } + + configureBintrayPublication(publicationName) +} + +fun Project.configureBintrayPublication(vararg publications: String) { + val dokka_version: String by this + val dokka_publication_channel: String by this + extensions.configure("bintray") { + user = System.getenv("BINTRAY_USER") + key = System.getenv("BINTRAY_KEY") + dryRun = System.getenv("BINTRAY_DRY_RUN") == "true" || + project.properties["bintray_dry_run"] == "true" + pkg = PackageConfig().apply { + repo = dokka_publication_channel + name = "dokka" + userOrg = "kotlin" + desc = "Dokka, the Kotlin documentation tool" + vcsUrl = "https://github.com/kotlin/dokka.git" + setLicenses("Apache-2.0") + version = VersionConfig().apply { + name = dokka_version + } + } + setPublications(*publications) + } +} + diff --git a/core/build.gradle.kts b/core/build.gradle.kts index 030db5c6..b52d3729 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configurePublication +import org.jetbrains.registerDokkaArtifactPublication plugins { `maven-publish` @@ -29,4 +29,6 @@ tasks { } } -configurePublication("dokka-core") +registerDokkaArtifactPublication("dokkaCore") { + artifactId = "dokka-core" +} diff --git a/core/dependencies/build.gradle.kts b/core/dependencies/build.gradle.kts index 082861b6..a074958d 100644 --- a/core/dependencies/build.gradle.kts +++ b/core/dependencies/build.gradle.kts @@ -1,4 +1,5 @@ -import org.jetbrains.configurePublication +import org.jetbrains.DokkaPublicationBuilder.Component.Shadow +import org.jetbrains.registerDokkaArtifactPublication plugins { id("com.github.johnrengelman.shadow") @@ -28,4 +29,7 @@ tasks { } } -configurePublication("dokka-core-dependencies", useShadow = true) +registerDokkaArtifactPublication("dokkaCoreDependencies") { + artifactId = "dokka-core-dependencies" + component = Shadow +} diff --git a/kotlin-analysis/build.gradle.kts b/kotlin-analysis/build.gradle.kts index 5acc1f06..f87811e1 100644 --- a/kotlin-analysis/build.gradle.kts +++ b/kotlin-analysis/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configurePublication +import org.jetbrains.registerDokkaArtifactPublication plugins { id("com.github.johnrengelman.shadow") @@ -15,4 +15,6 @@ dependencies { api(project(":kotlin-analysis:dependencies", configuration = "shadow")) } -configurePublication("dokka-analysis") +registerDokkaArtifactPublication("dokkaAnalysis") { + artifactId = "dokka-analysis" +} diff --git a/kotlin-analysis/dependencies/build.gradle.kts b/kotlin-analysis/dependencies/build.gradle.kts index 1fd13630..4aba6614 100644 --- a/kotlin-analysis/dependencies/build.gradle.kts +++ b/kotlin-analysis/dependencies/build.gradle.kts @@ -1,4 +1,5 @@ -import org.jetbrains.configurePublication +import org.jetbrains.DokkaPublicationBuilder.Component.Shadow +import org.jetbrains.registerDokkaArtifactPublication plugins { id("com.github.johnrengelman.shadow") @@ -53,4 +54,7 @@ tasks { } } -configurePublication("kotlin-analysis-dependencies", useShadow = true) +registerDokkaArtifactPublication("kotlinAnalysisDependencies"){ + artifactId = "kotlin-analysis-dependencies" + component = Shadow +} diff --git a/plugins/base/build.gradle.kts b/plugins/base/build.gradle.kts index 61e2ef01..0fc7f55c 100644 --- a/plugins/base/build.gradle.kts +++ b/plugins/base/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configurePublication +import org.jetbrains.registerDokkaArtifactPublication plugins { id("com.jfrog.bintray") @@ -30,4 +30,6 @@ tasks { } } -configurePublication("dokka-base") +registerDokkaArtifactPublication("dokkaBase") { + artifactId = "dokka-base" +} diff --git a/plugins/gfm/build.gradle.kts b/plugins/gfm/build.gradle.kts index 73958046..3f69043d 100644 --- a/plugins/gfm/build.gradle.kts +++ b/plugins/gfm/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configurePublication +import org.jetbrains.registerDokkaArtifactPublication dependencies { implementation(project(":plugins:base")) @@ -6,4 +6,6 @@ dependencies { testImplementation(project(":plugins:base:test-utils")) } -configurePublication("gfm-plugin") +registerDokkaArtifactPublication("gfmPlugin") { + artifactId = "gfm-plugin" +} diff --git a/plugins/javadoc/build.gradle.kts b/plugins/javadoc/build.gradle.kts index 5846ee62..5e2e5600 100644 --- a/plugins/javadoc/build.gradle.kts +++ b/plugins/javadoc/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configurePublication +import org.jetbrains.registerDokkaArtifactPublication dependencies { implementation("com.soywiz.korlibs.korte:korte-jvm:1.10.3") @@ -10,4 +10,6 @@ dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version") } -configurePublication("javadoc-plugin") +registerDokkaArtifactPublication("javadocPlugin") { + artifactId = "javadoc-plugin" +} diff --git a/plugins/jekyll/build.gradle.kts b/plugins/jekyll/build.gradle.kts index 0d1e38d6..64cf9800 100644 --- a/plugins/jekyll/build.gradle.kts +++ b/plugins/jekyll/build.gradle.kts @@ -1,8 +1,10 @@ -import org.jetbrains.configurePublication +import org.jetbrains.registerDokkaArtifactPublication dependencies { implementation(project(":plugins:base")) implementation(project(":plugins:gfm")) } -configurePublication("jekyll-plugin") +registerDokkaArtifactPublication("jekyllPlugin") { + artifactId = "jekyll-plugin" +} diff --git a/plugins/kotlin-as-java/build.gradle.kts b/plugins/kotlin-as-java/build.gradle.kts index 27e90f84..eda6114f 100644 --- a/plugins/kotlin-as-java/build.gradle.kts +++ b/plugins/kotlin-as-java/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configurePublication +import org.jetbrains.registerDokkaArtifactPublication dependencies { implementation(project(":plugins:base")) @@ -7,4 +7,6 @@ dependencies { testImplementation(project(":test-tools")) } -configurePublication("kotlin-as-java-plugin") +registerDokkaArtifactPublication("kotlinAsJavaPlugin") { + artifactId = "kotlin-as-java-plugin" +} diff --git a/plugins/mathjax/build.gradle.kts b/plugins/mathjax/build.gradle.kts index ea4cdff0..142e7dc4 100644 --- a/plugins/mathjax/build.gradle.kts +++ b/plugins/mathjax/build.gradle.kts @@ -1,3 +1,5 @@ -import org.jetbrains.configurePublication +import org.jetbrains.registerDokkaArtifactPublication -configurePublication("mathjax-plugin") +registerDokkaArtifactPublication("mathjaxPlugin") { + artifactId = "mathjax-plugin" +} diff --git a/runners/cli/build.gradle.kts b/runners/cli/build.gradle.kts index 19a49346..4ad34192 100644 --- a/runners/cli/build.gradle.kts +++ b/runners/cli/build.gradle.kts @@ -1,4 +1,5 @@ -import org.jetbrains.configurePublication +import org.jetbrains.DokkaPublicationBuilder.Component.Shadow +import org.jetbrains.registerDokkaArtifactPublication plugins { id("com.github.johnrengelman.shadow") @@ -26,5 +27,8 @@ tasks { } } -configurePublication("dokka-cli", useShadow = true) +registerDokkaArtifactPublication("dokkaCli"){ + artifactId = "dokka-cli" + component = Shadow +} diff --git a/runners/gradle-plugin/build.gradle.kts b/runners/gradle-plugin/build.gradle.kts index 00e3602e..0222f5e0 100644 --- a/runners/gradle-plugin/build.gradle.kts +++ b/runners/gradle-plugin/build.gradle.kts @@ -47,15 +47,13 @@ gradlePlugin { publishing { publications { - maybeCreate("pluginMaven").apply { + register("pluginMaven") { artifactId = "dokka-gradle-plugin" - artifact(sourceJar.get()) } register("dokkaGradlePluginForIntegrationTests") { artifactId = "dokka-gradle-plugin" from(components["java"]) - artifact(sourceJar.get()) version = "for-integration-tests-SNAPSHOT" } } diff --git a/runners/maven-plugin/build.gradle.kts b/runners/maven-plugin/build.gradle.kts index 83d9f5af..fbd2b48a 100644 --- a/runners/maven-plugin/build.gradle.kts +++ b/runners/maven-plugin/build.gradle.kts @@ -1,6 +1,6 @@ import org.jetbrains.CrossPlatformExec import org.jetbrains.SetupMaven -import org.jetbrains.configurePublication +import org.jetbrains.registerDokkaArtifactPublication val setupMaven by tasks.register("setupMaven") @@ -45,7 +45,10 @@ val generatePom by tasks.registering(Copy::class) { line.replace("dokka_version", "$dokka_version") } filter { line -> - line.replace("maven-plugin-plugin", "${setupMaven.mavenPluginToolsVersion}") + line.replace( + "maven-plugin-plugin", + "${setupMaven.mavenPluginToolsVersion}" + ) } } } @@ -90,4 +93,6 @@ tasks.named("jar") { } } -configurePublication("dokka-maven-plugin") +registerDokkaArtifactPublication("dokkaMavenPlugin") { + artifactId = "dokka-maven-plugin" +} diff --git a/testApi/build.gradle.kts b/testApi/build.gradle.kts index 1576dd99..fc882f44 100644 --- a/testApi/build.gradle.kts +++ b/testApi/build.gradle.kts @@ -1,4 +1,4 @@ -import org.jetbrains.configurePublication +import org.jetbrains.registerDokkaArtifactPublication plugins { `maven-publish` @@ -13,4 +13,6 @@ dependencies { implementation(kotlin("reflect")) } -configurePublication("dokka-test-api") +registerDokkaArtifactPublication("dokkaTestApi") { + artifactId = "dokka-test-api" +} -- cgit