Fix KT-13021, KT-13020, Test for already fixed KT-6941

Fixes on javadoc to kdoc comments convert
This commit is contained in:
Simon Ogorodnik
2016-10-13 12:47:13 +03:00
parent 155cfcb1f8
commit d731f97c7d
9 changed files with 106 additions and 9 deletions
@@ -73,7 +73,7 @@ object IdeaDocCommentConverter : DocCommentConverter {
return this return this
} }
private fun convertInlineDocTag(tag: PsiInlineDocTag) = when(tag.name) { private fun convertInlineDocTag(tag: PsiInlineDocTag) = when (tag.name) {
"code", "literal" -> { "code", "literal" -> {
val text = tag.dataElements.joinToString("") { it.text } val text = tag.dataElements.joinToString("") { it.text }
val escaped = StringUtil.escapeXml(text.trimStart()) val escaped = StringUtil.escapeXml(text.trimStart())
@@ -92,11 +92,17 @@ object IdeaDocCommentConverter : DocCommentConverter {
} }
private fun convertJavadocLink(link: String?): String = 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? = private fun PsiDocTag.linkElement(): PsiElement? =
valueElement ?: dataElements.firstOrNull { it !is PsiWhiteSpace } 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 class HtmlToMarkdownConverter() : XmlRecursiveElementVisitor() {
private enum class ListType { Ordered, Unordered; } private enum class ListType { Ordered, Unordered; }
data class MarkdownSpan(val prefix: String, val suffix: String) { data class MarkdownSpan(val prefix: String, val suffix: String) {
@@ -105,6 +111,9 @@ object IdeaDocCommentConverter : DocCommentConverter {
fun wrap(text: String) = MarkdownSpan(text, text) fun wrap(text: String) = MarkdownSpan(text, text)
fun prefix(text: String) = MarkdownSpan(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 -> { XmlTokenType.XML_CHAR_ENTITY_REF -> {
appendPendingText() appendPendingText()
val grandParent = element.parent.parent 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)) markdownBuilder.append(StringUtil.unescapeXml(element.text))
else else
markdownBuilder.append(element.text) 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("**") "b", "strong" -> MarkdownSpan.wrap("**")
"p" -> if (atLineStart) MarkdownSpan.prefix("\n * ") else MarkdownSpan.prefix("\n *\n *") "p" -> if (atLineStart) MarkdownSpan.prefix("\n * ") else MarkdownSpan.prefix("\n *\n *")
@@ -200,22 +209,29 @@ object IdeaDocCommentConverter : DocCommentConverter {
val innerText = tag.value.text val innerText = tag.value.text
if (docRef == innerText) MarkdownSpan("[", "]") else MarkdownSpan("[", "][$docRef]") if (docRef == innerText) MarkdownSpan("[", "]") else MarkdownSpan("[", "][$docRef]")
} }
else if (tag.getAttributeValue("href") != null) {
MarkdownSpan("[", "](${tag.getAttributeValue("href") ?: ""})")
}
else { 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. ") "li" -> if (currentListType == ListType.Unordered) MarkdownSpan.prefix(" * ") else MarkdownSpan.prefix(" 1. ")
else -> MarkdownSpan.Empty else -> MarkdownSpan.preserveTag(tag)
} }
private fun appendPendingText() { private fun appendPendingText() {
if (afterLineBreak ) { if (afterLineBreak) {
markdownBuilder.append(" * ") markdownBuilder.append(" * ")
afterLineBreak = false 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&lt;B}
*/
public class C {
}
@@ -0,0 +1,4 @@
/**
* `A&lt;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") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @RunWith(JUnit3RunnerWithInners.class)
public static class DocComments extends AbstractJavaToKotlinConverterForWebDemoTest { 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 { public void testAllFilesPresentInDocComments() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("j2k/testData/fileOrElement/docComments"), Pattern.compile("^(.+)\\.java$"), true); 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); 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") @TestMetadata("htmlInDocComment.java")
public void testHtmlInDocComment() throws Exception { public void testHtmlInDocComment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/htmlInDocComment.java"); String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/htmlInDocComment.java");
@@ -1765,6 +1778,12 @@ public class JavaToKotlinConverterForWebDemoTestGenerated extends AbstractJavaTo
doTest(fileName); 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") @TestMetadata("quoted.java")
public void testQuoted() throws Exception { public void testQuoted() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/quoted.java"); String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/quoted.java");
@@ -1719,6 +1719,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
@TestDataPath("$PROJECT_ROOT") @TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class) @RunWith(JUnit3RunnerWithInners.class)
public static class DocComments extends AbstractJavaToKotlinConverterSingleFileTest { 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 { public void testAllFilesPresentInDocComments() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("j2k/testData/fileOrElement/docComments"), Pattern.compile("^(.+)\\.java$"), true); 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); 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") @TestMetadata("htmlInDocComment.java")
public void testHtmlInDocComment() throws Exception { public void testHtmlInDocComment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/htmlInDocComment.java"); String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/htmlInDocComment.java");
@@ -1765,6 +1778,12 @@ public class JavaToKotlinConverterSingleFileTestGenerated extends AbstractJavaTo
doTest(fileName); 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") @TestMetadata("quoted.java")
public void testQuoted() throws Exception { public void testQuoted() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/quoted.java"); String fileName = KotlinTestUtils.navigationMetadata("j2k/testData/fileOrElement/docComments/quoted.java");