Fix KT-13021, KT-13020, Test for already fixed KT-6941
Fixes on javadoc to kdoc comments convert
This commit is contained in:
@@ -73,7 +73,7 @@ object IdeaDocCommentConverter : DocCommentConverter {
|
||||
return this
|
||||
}
|
||||
|
||||
private fun convertInlineDocTag(tag: PsiInlineDocTag) = when(tag.name) {
|
||||
private fun convertInlineDocTag(tag: PsiInlineDocTag) = when (tag.name) {
|
||||
"code", "literal" -> {
|
||||
val text = tag.dataElements.joinToString("") { it.text }
|
||||
val escaped = StringUtil.escapeXml(text.trimStart())
|
||||
@@ -92,11 +92,17 @@ object IdeaDocCommentConverter : DocCommentConverter {
|
||||
}
|
||||
|
||||
private fun convertJavadocLink(link: String?): String =
|
||||
if (link != null) link.substringBefore('(').replace('#', '.') else ""
|
||||
if (link != null) link.substringBefore('(').replace('#', '.') else ""
|
||||
|
||||
private fun PsiDocTag.linkElement(): PsiElement? =
|
||||
valueElement ?: dataElements.firstOrNull { it !is PsiWhiteSpace }
|
||||
|
||||
private fun XmlTag.attributesAsString() =
|
||||
if (attributes.isNotEmpty())
|
||||
attributes.joinToString(separator = " ", prefix = " ") { it.text }
|
||||
else
|
||||
""
|
||||
|
||||
private class HtmlToMarkdownConverter() : XmlRecursiveElementVisitor() {
|
||||
private enum class ListType { Ordered, Unordered; }
|
||||
data class MarkdownSpan(val prefix: String, val suffix: String) {
|
||||
@@ -105,6 +111,9 @@ object IdeaDocCommentConverter : DocCommentConverter {
|
||||
|
||||
fun wrap(text: String) = MarkdownSpan(text, text)
|
||||
fun prefix(text: String) = MarkdownSpan(text, "")
|
||||
|
||||
fun preserveTag(tag: XmlTag) =
|
||||
MarkdownSpan("<${tag.name}${tag.attributesAsString()}>", "</${tag.name}>")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,7 +151,7 @@ object IdeaDocCommentConverter : DocCommentConverter {
|
||||
XmlTokenType.XML_CHAR_ENTITY_REF -> {
|
||||
appendPendingText()
|
||||
val grandParent = element.parent.parent
|
||||
if(grandParent is HtmlTag && (grandParent.name == "code" || grandParent.name == "literal"))
|
||||
if (grandParent is HtmlTag && (grandParent.name == "code" || grandParent.name == "literal"))
|
||||
markdownBuilder.append(StringUtil.unescapeXml(element.text))
|
||||
else
|
||||
markdownBuilder.append(element.text)
|
||||
@@ -183,7 +192,7 @@ object IdeaDocCommentConverter : DocCommentConverter {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getMarkdownForTag(tag: XmlTag, atLineStart: Boolean): MarkdownSpan = when(tag.name) {
|
||||
private fun getMarkdownForTag(tag: XmlTag, atLineStart: Boolean): MarkdownSpan = when (tag.name) {
|
||||
"b", "strong" -> MarkdownSpan.wrap("**")
|
||||
|
||||
"p" -> if (atLineStart) MarkdownSpan.prefix("\n * ") else MarkdownSpan.prefix("\n *\n *")
|
||||
@@ -200,22 +209,29 @@ object IdeaDocCommentConverter : DocCommentConverter {
|
||||
val innerText = tag.value.text
|
||||
if (docRef == innerText) MarkdownSpan("[", "]") else MarkdownSpan("[", "][$docRef]")
|
||||
}
|
||||
else if (tag.getAttributeValue("href") != null) {
|
||||
MarkdownSpan("[", "](${tag.getAttributeValue("href") ?: ""})")
|
||||
}
|
||||
else {
|
||||
MarkdownSpan("[", "](${tag.getAttributeValue("href")})")
|
||||
MarkdownSpan.preserveTag(tag)
|
||||
}
|
||||
}
|
||||
|
||||
"ul" -> { currentListType = ListType.Unordered; MarkdownSpan.Empty }
|
||||
"ul" -> {
|
||||
currentListType = ListType.Unordered; MarkdownSpan.Empty
|
||||
}
|
||||
|
||||
"ol" -> { currentListType = ListType.Ordered; MarkdownSpan.Empty }
|
||||
"ol" -> {
|
||||
currentListType = ListType.Ordered; MarkdownSpan.Empty
|
||||
}
|
||||
|
||||
"li" -> if (currentListType == ListType.Unordered) MarkdownSpan.prefix(" * ") else MarkdownSpan.prefix(" 1. ")
|
||||
|
||||
else -> MarkdownSpan.Empty
|
||||
else -> MarkdownSpan.preserveTag(tag)
|
||||
}
|
||||
|
||||
private fun appendPendingText() {
|
||||
if (afterLineBreak ) {
|
||||
if (afterLineBreak) {
|
||||
markdownBuilder.append(" * ")
|
||||
afterLineBreak = false
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* <a name="some_important_name">Important</a>
|
||||
* <a>Just an link without href</a>
|
||||
*/
|
||||
public class A {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* <a name="some_important_name">Important</a>
|
||||
* <a>Just an link without href</a>
|
||||
*/
|
||||
class A
|
||||
@@ -0,0 +1,5 @@
|
||||
/**
|
||||
* {@code A<B}
|
||||
*/
|
||||
public class C {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* `A<B`
|
||||
*/
|
||||
class C
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Doc comment.<p>
|
||||
* <cite>cited</cite> <p>
|
||||
* <span class="important_class">spanned</span> <p>
|
||||
* <someSpecialTag>!!!</someSpecialTag>
|
||||
*/
|
||||
public class A {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Doc comment.
|
||||
*
|
||||
*
|
||||
* <cite>cited</cite>
|
||||
*
|
||||
*
|
||||
* <span class="important_class">spanned</span>
|
||||
*
|
||||
*
|
||||
* <someSpecialTag>!!!</someSpecialTag>
|
||||
*/
|
||||
class A
|
||||
@@ -1719,6 +1719,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class DocComments extends AbstractJavaToKotlinConverterForWebDemoTest {
|
||||
@TestMetadata("aWithoutHref.java")
|
||||
public void testAWithoutHref() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/aWithoutHref.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInDocComments() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("j2k/testData/fileOrElement/docComments"), Pattern.compile("^(.+)\\.java$"), true);
|
||||
}
|
||||
@@ -1735,6 +1741,13 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("escapedCharactersInCodeQuote.java")
|
||||
public void testEscapedCharactersInCodeQuote() throws Exception {
|
||||
String fileName =
|
||||
KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/escapedCharactersInCodeQuote.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("htmlInDocComment.java")
|
||||
public void testHtmlInDocComment() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/htmlInDocComment.java");
|
||||
@@ -1765,6 +1778,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("preserveUnknownTags.java")
|
||||
public void testPreserveUnknownTags() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/preserveUnknownTags.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("quoted.java")
|
||||
public void testQuoted() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/quoted.java");
|
||||
|
||||
@@ -1719,6 +1719,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class DocComments extends AbstractJavaToKotlinConverterSingleFileTest {
|
||||
@TestMetadata("aWithoutHref.java")
|
||||
public void testAWithoutHref() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/aWithoutHref.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInDocComments() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("j2k/testData/fileOrElement/docComments"), Pattern.compile("^(.+)\\.java$"), true);
|
||||
}
|
||||
@@ -1735,6 +1741,13 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("escapedCharactersInCodeQuote.java")
|
||||
public void testEscapedCharactersInCodeQuote() throws Exception {
|
||||
String fileName =
|
||||
KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/escapedCharactersInCodeQuote.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("htmlInDocComment.java")
|
||||
public void testHtmlInDocComment() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/htmlInDocComment.java");
|
||||
@@ -1765,6 +1778,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("preserveUnknownTags.java")
|
||||
public void testPreserveUnknownTags() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/preserveUnknownTags.java");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("quoted.java")
|
||||
public void testQuoted() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/quoted.java");
|
||||
|
||||
Reference in New Issue
Block a user