KT-22815: Wrap parameters and try new DocumentationMarkup

This commit is contained in:
Simon Ogorodnik
2018-06-05 21:14:10 +03:00
parent 86e863951e
commit 99f0a0368a
@@ -20,6 +20,7 @@ import com.google.common.html.HtmlEscapers
import com.intellij.codeInsight.documentation.DocumentationManagerUtil import com.intellij.codeInsight.documentation.DocumentationManagerUtil
import com.intellij.codeInsight.javadoc.JavaDocInfoGeneratorFactory import com.intellij.codeInsight.javadoc.JavaDocInfoGeneratorFactory
import com.intellij.lang.documentation.AbstractDocumentationProvider import com.intellij.lang.documentation.AbstractDocumentationProvider
import com.intellij.lang.documentation.DocumentationMarkup
import com.intellij.lang.java.JavaDocumentationProvider import com.intellij.lang.java.JavaDocumentationProvider
import com.intellij.openapi.diagnostic.Logger import com.intellij.openapi.diagnostic.Logger
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
@@ -88,6 +89,42 @@ class HtmlClassifierNamePolicy(val base: ClassifierNamePolicy) : ClassifierNameP
} }
} }
class WrapValueParameterHandler(val base: DescriptorRenderer.ValueParametersHandler) : DescriptorRenderer.ValueParametersHandler {
override fun appendBeforeValueParameters(parameterCount: Int, builder: StringBuilder) {
base.appendBeforeValueParameters(parameterCount, builder)
}
override fun appendBeforeValueParameter(
parameter: ValueParameterDescriptor,
parameterIndex: Int,
parameterCount: Int,
builder: StringBuilder
) {
builder.append("\n ")
base.appendBeforeValueParameter(parameter, parameterIndex, parameterCount, builder)
}
override fun appendAfterValueParameter(
parameter: ValueParameterDescriptor,
parameterIndex: Int,
parameterCount: Int,
builder: StringBuilder
) {
base.appendAfterValueParameter(parameter, parameterIndex, parameterCount, builder)
}
override fun appendAfterValueParameters(parameterCount: Int, builder: StringBuilder) {
if (parameterCount > 0) {
builder.appendln()
}
base.appendAfterValueParameters(parameterCount, builder)
}
}
class KotlinQuickDocumentationProvider : AbstractDocumentationProvider() { class KotlinQuickDocumentationProvider : AbstractDocumentationProvider() {
override fun getQuickNavigateInfo(element: PsiElement?, originalElement: PsiElement?): String? { override fun getQuickNavigateInfo(element: PsiElement?, originalElement: PsiElement?): String? {
@@ -123,6 +160,7 @@ class KotlinQuickDocumentationProvider : AbstractDocumentationProvider() {
private val DESCRIPTOR_RENDERER = DescriptorRenderer.HTML.withOptions { private val DESCRIPTOR_RENDERER = DescriptorRenderer.HTML.withOptions {
classifierNamePolicy = HtmlClassifierNamePolicy(ClassifierNamePolicy.SHORT) classifierNamePolicy = HtmlClassifierNamePolicy(ClassifierNamePolicy.SHORT)
valueParametersHandler = WrapValueParameterHandler(valueParametersHandler)
renderCompanionObjectName = true renderCompanionObjectName = true
} }
@@ -269,27 +307,32 @@ class KotlinQuickDocumentationProvider : AbstractDocumentationProvider() {
} }
} }
var renderedDecl = DESCRIPTOR_RENDERER.withOptions { var declaration = DESCRIPTOR_RENDERER.withOptions {
withDefinedIn = !DescriptorUtils.isLocal(declarationDescriptor) withDefinedIn = !DescriptorUtils.isLocal(declarationDescriptor)
}.render(declarationDescriptor) }.render(declarationDescriptor)
if (!quickNavigation) {
renderedDecl = "<pre>$renderedDecl</pre>" var renderedHtml: String
}
//if (!quickNavigation) {
renderedHtml = "${DocumentationMarkup.DEFINITION_START}$declaration${DocumentationMarkup.DEFINITION_END}"
//}
val deprecationProvider = ktElement.getResolutionFacade().frontendService<DeprecationResolver>() val deprecationProvider = ktElement.getResolutionFacade().frontendService<DeprecationResolver>()
renderedDecl += renderDeprecationInfo(declarationDescriptor, deprecationProvider) renderedHtml += renderDeprecationInfo(declarationDescriptor, deprecationProvider)
if (!quickNavigation) { if (!quickNavigation) {
val comment = declarationDescriptor.findKDoc { DescriptorToSourceUtilsIde.getAnyDeclaration(ktElement.project, it) } val comment = declarationDescriptor.findKDoc { DescriptorToSourceUtilsIde.getAnyDeclaration(ktElement.project, it) }
if (comment != null) { if (comment != null) {
val renderedComment = KDocRenderer.renderKDoc(comment) val renderedComment = KDocRenderer.renderKDoc(comment)
if (renderedComment.startsWith("<p>")) { renderedHtml += "${DocumentationMarkup.CONTENT_START}$renderedComment${DocumentationMarkup.CONTENT_END}"
renderedDecl += renderedComment // if (renderedComment.startsWith("<p>")) {
} // renderedHtml += renderedComment
else { // }
renderedDecl = "$renderedDecl<br/>$renderedComment" // else {
} // renderedHtml = "$renderedHtml<br/>$renderedComment"
// }
} }
else { else {
if (declarationDescriptor is CallableDescriptor) { // If we couldn't find KDoc, try to find javadoc in one of super's if (declarationDescriptor is CallableDescriptor) { // If we couldn't find KDoc, try to find javadoc in one of super's
@@ -299,13 +342,13 @@ class KotlinQuickDocumentationProvider : AbstractDocumentationProvider() {
val javaDocInfoGenerator = JavaDocInfoGeneratorFactory.create(psi.project, lightElement) val javaDocInfoGenerator = JavaDocInfoGeneratorFactory.create(psi.project, lightElement)
val builder = StringBuilder() val builder = StringBuilder()
if (javaDocInfoGenerator.generateDocInfoCore(builder, false)) if (javaDocInfoGenerator.generateDocInfoCore(builder, false))
renderedDecl += builder.toString().substringAfter("</PRE>") // Cut off light method signature renderedHtml += builder.toString().substringAfter("</PRE>") // Cut off light method signature
} }
} }
} }
} }
return renderedDecl return renderedHtml
} }
private fun renderDeprecationInfo( private fun renderDeprecationInfo(