as a workaround for KT-4502, wrap KDoc rendering functions in an object

This commit is contained in:
Dmitry Jemerov
2015-02-05 12:44:26 +01:00
parent 6eead1b1a1
commit 2bdb222516
2 changed files with 38 additions and 35 deletions
@@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.asJava.KotlinLightMethod; import org.jetbrains.kotlin.asJava.KotlinLightMethod;
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage; import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage;
import org.jetbrains.kotlin.idea.kdoc.KDocRenderer;
import org.jetbrains.kotlin.kdoc.KdocPackage; import org.jetbrains.kotlin.kdoc.KdocPackage;
import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag; import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag;
import org.jetbrains.kotlin.psi.JetDeclaration; import org.jetbrains.kotlin.psi.JetDeclaration;
@@ -92,7 +93,7 @@ public class JetQuickDocumentationProvider extends AbstractDocumentationProvider
KDocTag comment = KdocPackage.findKDoc(declarationDescriptor); KDocTag comment = KdocPackage.findKDoc(declarationDescriptor);
if (comment != null) { if (comment != null) {
renderedDecl = renderedDecl + "<br/>" + org.jetbrains.kotlin.idea.kdoc.KdocPackage.renderKDoc(comment); renderedDecl = renderedDecl + "<br/>" + KDocRenderer.INSTANCE$.renderKDoc(comment);
} }
return renderedDecl; return renderedDecl;
@@ -20,47 +20,49 @@ import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag
import com.intellij.openapi.util.text.StringUtil import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.kdoc.psi.impl.KDocSection import org.jetbrains.kotlin.kdoc.psi.impl.KDocSection
fun renderKDoc(docComment: KDocTag): String { object KDocRenderer {
val content = docComment.getContent() fun renderKDoc(docComment: KDocTag): String {
val result = StringBuilder("<p>") val content = docComment.getContent()
result.append(markdownToHtml(content)) val result = StringBuilder("<p>")
if (docComment is KDocSection) { result.append(markdownToHtml(content))
val paramTags = docComment.findTagsByName("param").filter { it.getSubjectName() != null } if (docComment is KDocSection) {
renderTagList(paramTags, "Parameters", result) val paramTags = docComment.findTagsByName("param").filter { it.getSubjectName() != null }
renderTagList(paramTags, "Parameters", result)
renderTag(docComment.findTagByName("return"), "Returns", result) renderTag(docComment.findTagByName("return"), "Returns", result)
val throwsTags = (docComment.findTagsByName("exception").union(docComment.findTagsByName("throws"))) val throwsTags = (docComment.findTagsByName("exception").union(docComment.findTagsByName("throws")))
.filter { it.getSubjectName() != null } .filter { it.getSubjectName() != null }
renderTagList(throwsTags, "Throws", result) renderTagList(throwsTags, "Throws", result)
renderTag(docComment.findTagByName("author"), "Author", result) renderTag(docComment.findTagByName("author"), "Author", result)
renderTag(docComment.findTagByName("since"), "Since", result) renderTag(docComment.findTagByName("since"), "Since", result)
}
result.append("</p>")
return result.toString()
} }
result.append("</p>")
return result.toString()
}
private fun renderTagList(tags: List<KDocTag>, title: String, to: StringBuilder) { private fun renderTagList(tags: List<KDocTag>, title: String, to: StringBuilder) {
if (tags.isEmpty()) { if (tags.isEmpty()) {
return return
} }
to.append("<dl><dt><b>${title}:</b></dt>")
tags.forEach {
to.append("<dd><code>${it.getSubjectName()}</code> - ${markdownToHtml(it.getContent().trimLeading())}</dd>")
}
to.append("</dl>")
}
private fun renderTag(tag: KDocTag?, title: String, to: StringBuilder) {
if (tag != null) {
to.append("<dl><dt><b>${title}:</b></dt>") to.append("<dl><dt><b>${title}:</b></dt>")
to.append("<dd>${markdownToHtml(tag.getContent())}</dd>") tags.forEach {
to.append("<dd><code>${it.getSubjectName()}</code> - ${markdownToHtml(it.getContent().trimLeading())}</dd>")
}
to.append("</dl>") to.append("</dl>")
} }
}
fun markdownToHtml(markdown: String): String { private fun renderTag(tag: KDocTag?, title: String, to: StringBuilder) {
// TODO Integrate a real Markdown parser if (tag != null) {
return StringUtil.replace(markdown, "\n", "<br/>"); to.append("<dl><dt><b>${title}:</b></dt>")
to.append("<dd>${markdownToHtml(tag.getContent())}</dd>")
to.append("</dl>")
}
}
fun markdownToHtml(markdown: String): String {
// TODO Integrate a real Markdown parser
return StringUtil.replace(markdown, "\n", "<br/>");
}
} }