11package org.utbot.intellij.plugin.javadoc
22
33import com.intellij.codeInsight.javadoc.JavaDocExternalFilter
4- import com.intellij.codeInsight.javadoc.JavaDocInfoGenerator
4+ import com.intellij.codeInsight.javadoc.JavaDocInfoGeneratorFactory
55import com.intellij.lang.java.JavaDocumentationProvider
66import com.intellij.psi.PsiDocCommentBase
77import com.intellij.psi.PsiJavaDocumentedElement
88
99/* *
10- * To render UtBot custom JavaDoc tags correctly, we need to override the way it generates HTML tags for comments.
11- * We get JavaDoc info generated by IJ platform and include sections related to UTBot,
12- * each section relates to the specific JavaDoc tag.
13- * It renders text, code, and links.
10+ * To render UtBot custom JavaDoc tags messages, we need to override basic behaviour of [JavaDocumentationProvider].
11+ * The IJ platform knows only custom tag names, so we need to add their messages in rendered comments to make it look nice.
1412 */
1513class UtDocumentationProvider : JavaDocumentationProvider () {
1614 override fun generateRenderedDoc (comment : PsiDocCommentBase ): String? {
@@ -20,16 +18,29 @@ class UtDocumentationProvider : JavaDocumentationProvider() {
2018 return " "
2119 }
2220
23- val docComment = target.docComment ? : return " "
21+ val baseJavaDocInfoGenerator = JavaDocInfoGeneratorFactory .getBuilder(target.getProject())
22+ .setPsiElement(target)
23+ .setIsGenerationForRenderedDoc(true )
24+ .create()
2425
25- val baseJavaDocInfoGenerator = JavaDocInfoGenerator (target.project, target)
26- // get JavaDoc comment rendered by the platform.
27- val baseJavaDocInfo = baseJavaDocInfoGenerator.generateRenderedDocInfo()
28- val utJavaDocInfoGenerator = UtJavaDocInfoGenerator ()
29- // add UTBot sections with custom tags.
30- val javaDocInfoWithUtSections =
31- utJavaDocInfoGenerator.addUtBotSpecificSectionsToJavaDoc(baseJavaDocInfo, docComment)
26+ val finalDocContent = replaceTagNamesWithMessages(baseJavaDocInfoGenerator.generateRenderedDocInfo())
3227
33- return JavaDocExternalFilter .filterInternalDocInfo(javaDocInfoWithUtSections )
28+ return JavaDocExternalFilter .filterInternalDocInfo(finalDocContent )
3429 }
30+
31+ /* *
32+ * Replaces names of plugin's custom JavaDoc tags with their messages in the comment generated by the IJ platform.
33+ * Example: utbot.MethodUnderTest -> Method under test.
34+ */
35+ private fun replaceTagNamesWithMessages (comment : String? ) =
36+ comment?.let {
37+ val docTagProvider = UtCustomJavaDocTagProvider ()
38+ docTagProvider.supportedTags.fold(it) { result, tag ->
39+ if (result.contains(tag.name)) {
40+ result.replace(tag.name, " ${tag.getMessage()} :" )
41+ } else {
42+ result
43+ }
44+ }
45+ } ? : " "
3546}
0 commit comments