diff --git a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/KDocCommentKeeper.kt b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/KDocCommentKeeper.kt index 5d7869a4739..a937c409577 100644 --- a/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/KDocCommentKeeper.kt +++ b/plugins/kapt3/kapt3-compiler/src/org/jetbrains/kotlin/kapt3/stubs/KDocCommentKeeper.kt @@ -17,7 +17,7 @@ package org.jetbrains.kotlin.kapt3.stubs import com.intellij.psi.PsiElement -import com.intellij.psi.PsiWhiteSpace +import com.intellij.psi.PsiRecursiveElementVisitor import com.intellij.psi.impl.source.tree.LeafPsiElement import com.sun.tools.javac.parser.Tokens import com.sun.tools.javac.tree.DCTree @@ -128,13 +128,29 @@ class KDocCommentKeeper(private val kaptContext: KaptContextForStubGeneration) { } private fun extractCommentText(docComment: KDoc): String { - return docComment.children.dropWhile { it is PsiWhiteSpace || it.isKDocStart() } - .dropLastWhile { it is PsiWhiteSpace || it.isKDocEnd() } - .joinToString("") { it.text } + return buildString { + docComment.accept(object : PsiRecursiveElementVisitor() { + override fun visitElement(element: PsiElement) { + if (element is LeafPsiElement) { + if (element.isKDocLeadingAsterisk()) { + val indent = takeLastWhile { it == ' ' || it == '\t' }.length + if (indent > 0) { + delete(length - indent, length) + } + } else if (!element.isKDocStart() && !element.isKDocEnd()) { + append(element.text) + } + } + + super.visitElement(element) + } + }) + }.trimIndent().trim() } - private fun PsiElement.isKDocStart() = this is LeafPsiElement && elementType == KDocTokens.START - private fun PsiElement.isKDocEnd() = this is LeafPsiElement && elementType == KDocTokens.END + private fun LeafPsiElement.isKDocStart() = elementType == KDocTokens.START + private fun LeafPsiElement.isKDocEnd() = elementType == KDocTokens.END + private fun LeafPsiElement.isKDocLeadingAsterisk() = elementType == KDocTokens.LEADING_ASTERISK } private class KDocComment(val body: String) : Tokens.Comment { diff --git a/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/ClassFileToSourceStubConverterTestGenerated.java b/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/ClassFileToSourceStubConverterTestGenerated.java index ad62204c293..d3398dd3a9c 100644 --- a/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/ClassFileToSourceStubConverterTestGenerated.java +++ b/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/ClassFileToSourceStubConverterTestGenerated.java @@ -194,6 +194,11 @@ public class ClassFileToSourceStubConverterTestGenerated extends AbstractClassFi runTest("plugins/kapt3/kapt3-compiler/testData/converter/javaKeywordsInPackageNames.kt"); } + @TestMetadata("javadoc.kt") + public void testJavadoc() throws Exception { + runTest("plugins/kapt3/kapt3-compiler/testData/converter/javadoc.kt"); + } + @TestMetadata("jvmOverloads.kt") public void testJvmOverloads() throws Exception { runTest("plugins/kapt3/kapt3-compiler/testData/converter/jvmOverloads.kt"); diff --git a/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/KotlinKapt3IntegrationTests.kt b/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/KotlinKapt3IntegrationTests.kt index 15dc8ae56d4..b0fed40a2bf 100644 --- a/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/KotlinKapt3IntegrationTests.kt +++ b/plugins/kapt3/kapt3-compiler/test/org/jetbrains/kotlin/kapt3/test/KotlinKapt3IntegrationTests.kt @@ -56,7 +56,7 @@ class KotlinKapt3IntegrationTests : AbstractKotlinKapt3IntegrationTest(), Java9T fun testComments() = test("Simple", "test.MyAnnotation") { _, _, env -> fun commentOf(className: String) = env.elementUtils.getDocComment(env.elementUtils.getTypeElement(className)) - assert(commentOf("test.Simple") == " * KDoc comment.\n") + assert(commentOf("test.Simple") == " KDoc comment.\n") assert(commentOf("test.EnumClass") == null) // simple comment - not saved assert(commentOf("test.MyAnnotation") == null) // multiline comment - not saved } diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/comments.txt b/plugins/kapt3/kapt3-compiler/testData/converter/comments.txt index 5d135abd301..de40b23ee52 100644 --- a/plugins/kapt3/kapt3-compiler/testData/converter/comments.txt +++ b/plugins/kapt3/kapt3-compiler/testData/converter/comments.txt @@ -118,9 +118,9 @@ public final class Test { import java.lang.System; /** - * * Test2 - * * Multiline - * * documentation. + * Test2 + * Multiline + * documentation. */ @kotlin.Metadata() public final class Test2 { @@ -184,7 +184,7 @@ public final class Test4 { import java.lang.System; /** - * * `/ * Failure * /` + * `/ * Failure * /` */ @kotlin.Metadata() public abstract interface TestComponent { diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/javadoc.kt b/plugins/kapt3/kapt3-compiler/testData/converter/javadoc.kt new file mode 100644 index 00000000000..74adb9117dc --- /dev/null +++ b/plugins/kapt3/kapt3-compiler/testData/converter/javadoc.kt @@ -0,0 +1,35 @@ +package javadoc + +/** Simple */ +class A + +/** + * Multi + * line + * comment. + */ +class B { + /** Nested + * member + * comment. */ + val a = "" + + /** + * Mixed + * tabs/spaces + */ + val b = "" + + /** + * List: + * * first item + * * second item + */ + val c = "" + + /** + Without + stars + */ + val d = "" +} \ No newline at end of file diff --git a/plugins/kapt3/kapt3-compiler/testData/converter/javadoc.txt b/plugins/kapt3/kapt3-compiler/testData/converter/javadoc.txt new file mode 100644 index 00000000000..c8e28d7075d --- /dev/null +++ b/plugins/kapt3/kapt3-compiler/testData/converter/javadoc.txt @@ -0,0 +1,83 @@ +package javadoc; + +import java.lang.System; + +/** + * Simple + */ +@kotlin.Metadata() +public final class A { + + public A() { + super(); + } +} + +//////////////////// + +package javadoc; + +import java.lang.System; + +/** + * Multi + * line + * comment. + */ +@kotlin.Metadata() +public final class B { + + /** + * Nested + * member + * comment. + */ + @org.jetbrains.annotations.NotNull() + private final java.lang.String a = ""; + + /** + * Mixed + * tabs/spaces + */ + @org.jetbrains.annotations.NotNull() + private final java.lang.String b = ""; + + /** + * List: + * * first item + * * second item + */ + @org.jetbrains.annotations.NotNull() + private final java.lang.String c = ""; + + /** + * Without + * stars + */ + @org.jetbrains.annotations.NotNull() + private final java.lang.String d = ""; + + @org.jetbrains.annotations.NotNull() + public final java.lang.String getA() { + return null; + } + + @org.jetbrains.annotations.NotNull() + public final java.lang.String getB() { + return null; + } + + @org.jetbrains.annotations.NotNull() + public final java.lang.String getC() { + return null; + } + + @org.jetbrains.annotations.NotNull() + public final java.lang.String getD() { + return null; + } + + public B() { + super(); + } +} diff --git a/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/Simple.it.txt b/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/Simple.it.txt index a74829d0bbf..ce4fd90be1b 100644 --- a/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/Simple.it.txt +++ b/plugins/kapt3/kapt3-compiler/testData/kotlinRunner/Simple.it.txt @@ -10,7 +10,7 @@ package test; import java.lang.System; /** - * * KDoc comment. + * KDoc comment. */ @kotlin.Suppress(names = {"UNRESOLVED_REFERENCE"}) @kotlin.Metadata()