From c978c5f6833358729b9171d87241e22ca54404b2 Mon Sep 17 00:00:00 2001 From: IgnatBeresnev Date: Tue, 31 Oct 2023 23:31:30 +0100 Subject: [PATCH] Resolve rebase conflicts --- .../test/jvm/java/JavaAnalysisTest.kt | 42 -------- .../test/jvm/kotlin/KotlinJvmAnalysisTest.kt | 32 ------ .../test/jvm/mixed/MixedJvmAnalysisTest.kt | 69 ------------ .../test/sample/SampleAnalysisTest.kt | 54 ++-------- .../test/api/analysis/TestProjectAnalyzer.kt | 4 +- .../symbols/services/KotlinSampleProvider.kt | 100 ------------------ .../SymbolSampleAnalysisEnvironment.kt | 25 +++-- 7 files changed, 23 insertions(+), 303 deletions(-) delete mode 100644 subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/jvm/java/JavaAnalysisTest.kt delete mode 100644 subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/jvm/kotlin/KotlinJvmAnalysisTest.kt delete mode 100644 subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/jvm/mixed/MixedJvmAnalysisTest.kt delete mode 100644 subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/services/KotlinSampleProvider.kt diff --git a/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/jvm/java/JavaAnalysisTest.kt b/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/jvm/java/JavaAnalysisTest.kt deleted file mode 100644 index 0834d5c9c4..0000000000 --- a/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/jvm/java/JavaAnalysisTest.kt +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.analysis.test.jvm.java - -import org.jetbrains.dokka.analysis.test.api.javaTestProject -import org.jetbrains.dokka.analysis.test.api.parse -import kotlin.test.Test -import kotlin.test.assertEquals - -class JavaAnalysisTest { - - @Test - fun javaTestProject() { - val testProject = javaTestProject { - dokkaConfiguration { - moduleName = "java-module-name-for-unit-test" - } - javaFile(pathFromSrc = "org/jetbrains/dokka/test/java/Bar.java") { - +""" - public class Bar { - public static void bar() { - System.out.println("Bar"); - } - } - """ - } - } - - val module = testProject.parse() - assertEquals("java-module-name-for-unit-test", module.name) - assertEquals(1, module.packages.size) - - val pckg = module.packages[0] - assertEquals("org.jetbrains.dokka.test.java", pckg.name) - assertEquals(1, pckg.classlikes.size) - - val fooClass = pckg.classlikes[0] - assertEquals("Bar", fooClass.name) - } -} diff --git a/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/jvm/kotlin/KotlinJvmAnalysisTest.kt b/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/jvm/kotlin/KotlinJvmAnalysisTest.kt deleted file mode 100644 index b3c89d19e8..0000000000 --- a/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/jvm/kotlin/KotlinJvmAnalysisTest.kt +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.analysis.test.jvm.kotlin - -import org.jetbrains.dokka.analysis.test.api.kotlinJvmTestProject -import org.jetbrains.dokka.analysis.test.api.parse -import kotlin.test.Test -import kotlin.test.assertEquals - -class KotlinJvmAnalysisTest { - - @Test - fun kotlinJvmTestProject() { - val testProject = kotlinJvmTestProject { - ktFile(pathFromSrc = "org/jetbrains/dokka/test/kotlin/MyFile.kt") { - +"public class Foo {}" - } - } - - val module = testProject.parse() - assertEquals(1, module.packages.size) - - val pckg = module.packages[0] - assertEquals("org.jetbrains.dokka.test.kotlin", pckg.name) - assertEquals(1, pckg.classlikes.size) - - val fooClass = pckg.classlikes[0] - assertEquals("Foo", fooClass.name) - } -} diff --git a/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/jvm/mixed/MixedJvmAnalysisTest.kt b/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/jvm/mixed/MixedJvmAnalysisTest.kt deleted file mode 100644 index 3b083534b5..0000000000 --- a/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/jvm/mixed/MixedJvmAnalysisTest.kt +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.analysis.test.jvm.mixed - -import org.jetbrains.dokka.analysis.test.api.mixedJvmTestProject -import org.jetbrains.dokka.analysis.test.api.parse -import kotlin.test.Test -import kotlin.test.assertEquals - -class MixedJvmAnalysisTest { - - @Test - fun mixedJvmTestProject() { - val testProject = mixedJvmTestProject { - kotlinSourceSet { - ktFile(pathFromSrc = "test/MyFile.kt") { - +"fun foo(): String = \"Foo\"" - } - javaFile(pathFromSrc = "test/MyJavaFileInKotlin.java") { - +""" - public class MyJavaFileInKotlin { - public static void bar() { - System.out.println("Bar"); - } - } - """ - } - } - - javaSourceSet { - ktFile(pathFromSrc = "test/MyFile.kt") { - +"fun bar(): String = \"Foo\"" - } - javaFile(pathFromSrc = "test/MyJavaFileInJava.java") { - +""" - public class MyJavaFileInJava { - public static void bar() { - System.out.println("Bar"); - } - } - """ - } - } - } - - val module = testProject.parse() - assertEquals(1, module.packages.size) - - val pckg = module.packages[0] - assertEquals("test", pckg.name) - - assertEquals(2, pckg.classlikes.size) - assertEquals(2, pckg.functions.size) - - val firstClasslike = pckg.classlikes[0] - assertEquals("MyJavaFileInKotlin", firstClasslike.name) - - val secondClasslike = pckg.classlikes[1] - assertEquals("MyJavaFileInJava", secondClasslike.name) - - val firstFunction = pckg.functions[0] - assertEquals("foo", firstFunction.name) - - val secondFunction = pckg.functions[1] - assertEquals("bar", secondFunction.name) - } -} diff --git a/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/sample/SampleAnalysisTest.kt b/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/sample/SampleAnalysisTest.kt index ad15171a01..f0aa951df9 100644 --- a/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/sample/SampleAnalysisTest.kt +++ b/subprojects/analysis-kotlin-api/src/test/kotlin/org/jetbrains/dokka/analysis/test/sample/SampleAnalysisTest.kt @@ -285,52 +285,6 @@ class SampleAnalysisTest { } } - @Test - fun `should return null for existing sample when resolving with the wrong source set`() { - val testProject = mixedJvmTestProject { - dokkaConfiguration { - kotlinSourceSet { - // settings samples dir only for the kotlin source set - samples = setOf("/samples/fooSample.kt") - } - } - - javaSourceSet { - // creating a file in java source set to make sure it gets created - javaFile("org/jetbrains/dokka/test/Foo.java") { - +""" - public class Foo {} - """ - } - } - - sampleFile("/samples/fooSample.kt", fqPackageName = "org.jetbrains.dokka.sample") { - +""" - fun foo() { - println("foo") - } - """ - } - } - - testProject.useServices { context -> - sampleAnalysisEnvironmentCreator.use { - val sampleFromJavaSourceSet = resolveSample( - sourceSet = context.defaultJavaSourceSet(), - fullyQualifiedLink = "org.jetbrains.dokka.sample.foo" - ) - assertNull(sampleFromJavaSourceSet) - - val sampleFromKotlinSourceSet = resolveSample( - sourceSet = context.defaultKotlinSourceSet(), - fullyQualifiedLink = "org.jetbrains.dokka.sample.foo" - ) - assertNotNull(sampleFromKotlinSourceSet) - assertEquals("println(\"foo\")", sampleFromKotlinSourceSet.body) - } - } - } - @Test fun `should return null if sample is resolved just by class name`() { val testProject = kotlinJvmTestProject { @@ -372,7 +326,7 @@ class SampleAnalysisTest { @Test fun `should return null if trying to resolve a non-kotlin sample link`() { val testProject = mixedJvmTestProject { - kotlinSourceSet { + kotlinSourceDirectory { javaFile("org/jetbrains/test/sample/JavaClass.java") { +""" public class JavaClass { @@ -393,7 +347,7 @@ class SampleAnalysisTest { val collectingLogger = CollectingDokkaConsoleLogger() testProject.useServices(collectingLogger) { context -> sampleAnalysisEnvironmentCreator.use { - val kotlinSourceSet = context.defaultKotlinSourceSet() + val kotlinSourceSet = context.singleSourceSet() val byClassName = resolveSample(kotlinSourceSet, "org.jetbrains.test.sample.JavaClass") assertNull(byClassName) @@ -551,4 +505,8 @@ class SampleAnalysisTest { """.trimIndent() ) } + + @Test + @Ignore // TODO [beresnev] should be implemented when there's api for KMP projects + fun `should return null for existing sample when resolving with the wrong source set`() {} } diff --git a/subprojects/analysis-kotlin-api/src/testFixtures/kotlin/org/jetbrains/dokka/analysis/test/api/analysis/TestProjectAnalyzer.kt b/subprojects/analysis-kotlin-api/src/testFixtures/kotlin/org/jetbrains/dokka/analysis/test/api/analysis/TestProjectAnalyzer.kt index 17c881c5e3..674c6d4779 100644 --- a/subprojects/analysis-kotlin-api/src/testFixtures/kotlin/org/jetbrains/dokka/analysis/test/api/analysis/TestProjectAnalyzer.kt +++ b/subprojects/analysis-kotlin-api/src/testFixtures/kotlin/org/jetbrains/dokka/analysis/test/api/analysis/TestProjectAnalyzer.kt @@ -228,7 +228,9 @@ internal object TestProjectAnalyzer { val internalAnalysisPlugin = context.plugin() return TestAnalysisServices( sampleAnalysisEnvironmentCreator = publicAnalysisPlugin.querySingle { sampleAnalysisEnvironmentCreator }, - moduleAndPackageDocumentationReader = internalAnalysisPlugin.querySingle { moduleAndPackageDocumentationReader } + moduleAndPackageDocumentationReader = internalAnalysisPlugin.querySingle { + moduleAndPackageDocumentationReader + } ) } } diff --git a/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/services/KotlinSampleProvider.kt b/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/services/KotlinSampleProvider.kt deleted file mode 100644 index e453c72de1..0000000000 --- a/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/services/KotlinSampleProvider.kt +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package org.jetbrains.dokka.analysis.kotlin.symbols.services - -import com.intellij.psi.PsiElement -import org.jetbrains.dokka.DokkaConfiguration -import org.jetbrains.dokka.InternalDokkaApi -import org.jetbrains.dokka.plugability.DokkaContext -import org.jetbrains.dokka.plugability.plugin -import org.jetbrains.dokka.plugability.querySingle -import org.jetbrains.dokka.analysis.kotlin.internal.SampleProvider -import org.jetbrains.dokka.analysis.kotlin.internal.SampleProviderFactory -import org.jetbrains.dokka.analysis.kotlin.symbols.plugin.SamplesKotlinAnalysis -import org.jetbrains.dokka.analysis.kotlin.symbols.plugin.SymbolsAnalysisPlugin -import org.jetbrains.kotlin.analysis.api.analyze -import org.jetbrains.kotlin.analysis.project.structure.KtSourceModule -import org.jetbrains.kotlin.name.FqName -import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.psi.KtBlockExpression -import org.jetbrains.kotlin.psi.KtDeclarationWithBody -import org.jetbrains.kotlin.psi.KtFile - -public class KotlinSampleProviderFactory( - public val context: DokkaContext -): SampleProviderFactory { - override fun build(): SampleProvider { - return KotlinSampleProvider(context) - } - -} -/** - * It's declared as open since StdLib has its own sample transformer - * with [processBody] and [processImports] - */ -@InternalDokkaApi -public open class KotlinSampleProvider( - public val context: DokkaContext -): SampleProvider { - private val kotlinAnalysisOfRegularSources = context.plugin().querySingle { kotlinAnalysis } - - private val kotlinAnalysisOfSamples = SamplesKotlinAnalysis( - sourceSets = context.configuration.sourceSets, context = context - ) - - protected open fun processBody(psiElement: PsiElement): String { - val text = processSampleBody(psiElement).trim { it == '\n' || it == '\r' }.trimEnd() - val lines = text.split("\n") - val indent = lines.filter(String::isNotBlank).minOfOrNull { it.takeWhile(Char::isWhitespace).count() } ?: 0 - return lines.joinToString("\n") { it.drop(indent) } - } - - private fun processSampleBody(psiElement: PsiElement): String = when (psiElement) { - is KtDeclarationWithBody -> { - when (val bodyExpression = psiElement.bodyExpression) { - is KtBlockExpression -> bodyExpression.text.removeSurrounding("{", "}") - else -> bodyExpression!!.text - } - } - else -> psiElement.text - } - - protected open fun processImports(psiElement: PsiElement): String { - val psiFile = psiElement.containingFile - return when(val text = (psiFile as? KtFile)?.importList?.text) { - is String -> text - else -> "" - } - } - - /** - * @return [SampleProvider.SampleSnippet] or null if it has not found by [fqLink] - */ - override fun getSample(sourceSet: DokkaConfiguration.DokkaSourceSet, fqLink: String): SampleProvider.SampleSnippet? { - return kotlinAnalysisOfSamples.getModuleOrNull(sourceSet)?.let { getSampleFromModule(it, fqLink) } - ?: getSampleFromModule( - kotlinAnalysisOfRegularSources.getModule(sourceSet), fqLink - ) - } - private fun getSampleFromModule(module: KtSourceModule, fqLink: String): SampleProvider.SampleSnippet? { - val psiElement = analyze(module) { - val lastDotIndex = fqLink.lastIndexOf('.') - - val functionName = if (lastDotIndex == -1) fqLink else fqLink.substring(lastDotIndex + 1, fqLink.length) - val packageName = if (lastDotIndex == -1) "" else fqLink.substring(0, lastDotIndex) - getTopLevelCallableSymbols(FqName(packageName), Name.identifier(functionName)).firstOrNull()?.psi - } - ?: return null.also { context.logger.warn("Cannot find PsiElement corresponding to $fqLink") } - val imports = - processImports(psiElement) - val body = processBody(psiElement) - - return SampleProvider.SampleSnippet(imports, body) - } - - override fun close() { - kotlinAnalysisOfSamples.close() - } -} diff --git a/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/services/SymbolSampleAnalysisEnvironment.kt b/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/services/SymbolSampleAnalysisEnvironment.kt index 2ca715803b..16a5742130 100644 --- a/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/services/SymbolSampleAnalysisEnvironment.kt +++ b/subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/services/SymbolSampleAnalysisEnvironment.kt @@ -20,6 +20,7 @@ import org.jetbrains.dokka.plugability.plugin import org.jetbrains.dokka.plugability.querySingle import org.jetbrains.dokka.utilities.DokkaLogger import org.jetbrains.kotlin.analysis.api.analyze +import org.jetbrains.kotlin.codegen.optimization.common.analyze import org.jetbrains.kotlin.psi.KtBlockExpression import org.jetbrains.kotlin.psi.KtDeclarationWithBody import org.jetbrains.kotlin.psi.KtFile @@ -29,27 +30,27 @@ internal class SymbolSampleAnalysisEnvironmentCreator( private val context: DokkaContext, ) : SampleAnalysisEnvironmentCreator { - private val symbolAnalysisPlugin = context.plugin() + private val projectKotlinAnalysis = context.plugin().querySingle { kotlinAnalysis } override fun use(block: SampleAnalysisEnvironment.() -> T): T { return runBlocking(Dispatchers.Default) { SamplesKotlinAnalysis( - sourceSets = context.configuration.sourceSets, - context = context, - projectKotlinAnalysis = symbolAnalysisPlugin.querySingle { kotlinAnalysis } - ).use { kotlinAnalysis -> - val sampleAnalysis = SymbolSampleAnalysisEnvironment( - kotlinAnalysis = kotlinAnalysis, + sourceSets = context.configuration.sourceSets, context = context + ).use { samplesKotlinAnalysis -> + val sampleAnalysisEnvironment = SymbolSampleAnalysisEnvironment( + samplesKotlinAnalysis = samplesKotlinAnalysis, + projectKotlinAnalysis = projectKotlinAnalysis, dokkaLogger = context.logger ) - block(sampleAnalysis) + block(sampleAnalysisEnvironment) } } } } private class SymbolSampleAnalysisEnvironment( - private val kotlinAnalysis: KotlinAnalysis, + private val samplesKotlinAnalysis: KotlinAnalysis, + private val projectKotlinAnalysis: KotlinAnalysis, private val dokkaLogger: DokkaLogger, ) : SampleAnalysisEnvironment { @@ -76,8 +77,10 @@ private class SymbolSampleAnalysisEnvironment( } private fun findPsiElement(sourceSet: DokkaSourceSet, fqLink: String): PsiElement? { - val analysisContext = kotlinAnalysis[sourceSet] - return analyze(analysisContext.mainModule) { + val ktSourceModule = samplesKotlinAnalysis.getModuleOrNull(sourceSet) + ?: projectKotlinAnalysis.getModule(sourceSet) + + return analyze(ktSourceModule) { resolveKDocTextLinkSymbol(fqLink)?.psi } }