diff options
Diffstat (limited to 'plugins')
3 files changed, 32 insertions, 17 deletions
diff --git a/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt b/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt index 53ab6bde..b9ccd02b 100644 --- a/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt +++ b/plugins/base/src/main/kotlin/parsers/MarkdownParser.kt @@ -248,7 +248,12 @@ class MarkdownParser( ) private fun codeBlocksHandler(node: ASTNode): DocTag = - DocTagsFromIElementFactory.getInstance(node.type, children = node.children.evaluateChildren()) + DocTagsFromIElementFactory.getInstance(node.type, children = node.children.mergeLeafASTNodes().map { + DocTagsFromIElementFactory.getInstance( + MarkdownTokenTypes.TEXT, + body = text.substring(it.startOffset, it.endOffset) + ) + }) private fun defaultHandler(node: ASTNode): DocTag = DocTagsFromIElementFactory.getInstance( @@ -281,7 +286,7 @@ class MarkdownParser( MarkdownTokenTypes.HARD_LINE_BREAK -> DocTagsFromIElementFactory.getInstance(node.type) MarkdownTokenTypes.CODE_FENCE_CONTENT, MarkdownTokenTypes.CODE_LINE -> DocTagsFromIElementFactory.getInstance( - MarkdownTokenTypes.TEXT, + MarkdownElementTypes.CODE_BLOCK, body = text.substring(node.startOffset, node.endOffset) ) MarkdownTokenTypes.TEXT -> DocTagsFromIElementFactory.getInstance( @@ -341,20 +346,20 @@ class MarkdownParser( children += this[index] } else { val startOffset = this[index].startOffset - val type = this[index].type + val sIndex = index 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(type, startOffset, endOffset) + mergedLeafNode(this, index, startOffset, sIndex)?.run { + children += this + } break } index++ } if (index == this.lastIndex) { - val endOffset = this[index].endOffset - if (text.substring(startOffset, endOffset).transform().trim().isNotEmpty()) - children += LeafASTNode(type, startOffset, endOffset) + mergedLeafNode(this, index, startOffset, sIndex)?.run { + children += this + } } } index++ @@ -362,6 +367,15 @@ class MarkdownParser( return children } + private fun mergedLeafNode(nodes: List<ASTNode>, index: Int, startOffset: Int, sIndex: Int): LeafASTNode? { + val endOffset = nodes[index].endOffset + if (text.substring(startOffset, endOffset).transform().trim().isNotEmpty()) { + val type = if (nodes.subList(sIndex, index).any { it.type == MarkdownTokenTypes.CODE_LINE }) MarkdownTokenTypes.CODE_LINE else MarkdownTokenTypes.TEXT + return LeafASTNode(type, startOffset, endOffset) + } + return null + } + private fun String.transform() = this .replace(Regex("\n\n+"), "") // Squashing new lines between paragraphs .replace(Regex("\n"), " ") diff --git a/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt b/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt index b9c328dc..5baaf35a 100644 --- a/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt +++ b/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt @@ -151,7 +151,8 @@ class JavadocParser( is PsiInlineDocTag -> convertInlineDocTag(this) is PsiDocParamRef -> toDocumentationLinkString() is PsiDocTagValue, - is LeafPsiElement -> text + is LeafPsiElement -> (if ((prevSibling as? PsiDocToken)?.isLeadingAsterisk() == true) text?.drop(1) else text) + ?.takeUnless { it.isBlank() } else -> null } @@ -214,7 +215,7 @@ class JavadocParser( } private fun convertHtmlNode(node: Node, insidePre: Boolean = false): DocTag? = when (node) { - is TextNode -> Text(body = if (insidePre) node.wholeText else node.text()) + is TextNode -> (if (insidePre) node.wholeText else node.text().takeIf { it.isNotBlank() })?.let { Text(body = it) } is Element -> createBlock(node) else -> null } diff --git a/plugins/base/src/test/kotlin/translators/JavadocParserTest.kt b/plugins/base/src/test/kotlin/translators/JavadocParserTest.kt index 8ffcfb3c..0cd0a151 100644 --- a/plugins/base/src/test/kotlin/translators/JavadocParserTest.kt +++ b/plugins/base/src/test/kotlin/translators/JavadocParserTest.kt @@ -168,12 +168,12 @@ class JavadocParserTest : AbstractCoreTest() { val dateDescription = module.descriptionOf("Date2")!! val preTagContent = dateDescription.firstChildOfType<Pre>().firstChildOfType<Text>() val expectedText = """<androidx.fragment.app.FragmentContainerView - xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:app="http://schemas.android.com/apk/res-auto" - android:id="@+id/fragment_container_view" - android:layout_width="match_parent" - android:layout_height="match_parent"> - </androidx.fragment.app.FragmentContainerView>""".trimIndent() + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:id="@+id/fragment_container_view" + android:layout_width="match_parent" + android:layout_height="match_parent"> +</androidx.fragment.app.FragmentContainerView>""".trimIndent() assertEquals(expectedText.trim(), preTagContent.body.trim()) } } |