-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support configuring doc.source.disable to disable documentation…
… reading
- Loading branch information
Showing
4 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
idea-plugin/src/main/kotlin/com/itangcent/idea/psi/DisableDocSupport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.itangcent.idea.psi | ||
|
||
import com.google.inject.matcher.Matchers | ||
import com.itangcent.common.logger.Log | ||
import com.itangcent.common.utils.toBool | ||
import com.itangcent.intellij.config.ConfigReader | ||
import com.itangcent.intellij.context.ActionContext | ||
import com.itangcent.intellij.jvm.DocHelper | ||
import org.aopalliance.intercept.MethodInterceptor | ||
import org.aopalliance.intercept.MethodInvocation | ||
|
||
/* | ||
* The DisableDocSupport object is responsible for binding the EmptyInterceptor to the ActionContextBuilder. | ||
* It provides a way to disable the plugin from reading documentation. | ||
*/ | ||
object DisableDocSupport { | ||
/* | ||
* Binds the EmptyInterceptor to the ActionContextBuilder, enabling the plugin to intercept method invocations. | ||
* @param builder The ActionContextBuilder to bind the interceptor to. | ||
*/ | ||
fun bind(builder: ActionContext.ActionContextBuilder) { | ||
builder.bindInterceptor( | ||
Matchers.subclassesOf(DocHelper::class.java), | ||
Matchers.any(), | ||
EmptyInterceptor() | ||
) | ||
} | ||
} | ||
|
||
/* | ||
* The EmptyInterceptor class is an interceptor used to disable documentation support. | ||
* Use 'doc.source.disable' configuration property to determine if documentation is enabled or disabled. | ||
*/ | ||
class EmptyInterceptor : MethodInterceptor { | ||
|
||
companion object : Log() | ||
|
||
private val disableDoc by lazy { | ||
val disable = ActionContext.getContext() | ||
?.instance(ConfigReader::class) | ||
?.first("doc.source.disable") | ||
?.toBool(false) ?: false | ||
if (disable) { | ||
LOG.info("disable doc") | ||
} | ||
disable | ||
} | ||
|
||
override fun invoke(invocation: MethodInvocation): Any? { | ||
if (disableDoc) { | ||
val returnType = invocation.method.returnType | ||
return when (returnType) { | ||
Map::class.java -> emptyMap<String, String?>() | ||
List::class.java -> emptyList<String>() | ||
String::class.java -> "" | ||
Boolean::class.java, Boolean::class.javaObjectType -> false | ||
else -> null | ||
} | ||
} | ||
return invocation.proceed() | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
idea-plugin/src/test/kotlin/com/itangcent/idea/psi/DisableDocSupportTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.itangcent.idea.psi | ||
|
||
import com.google.inject.Inject | ||
import com.intellij.psi.PsiClass | ||
import com.itangcent.intellij.context.ActionContext | ||
import com.itangcent.intellij.jvm.DocHelper | ||
import com.itangcent.testFramework.PluginContextLightCodeInsightFixtureTestCase | ||
|
||
/** | ||
* Test case of [com.itangcent.idea.psi.DisableDocSupport] | ||
*/ | ||
abstract class DisableDocSupportTest : PluginContextLightCodeInsightFixtureTestCase() { | ||
|
||
abstract val disableDoc: Boolean | ||
|
||
protected lateinit var userInfoPsiClass: PsiClass | ||
protected lateinit var userCtrlPsiClass: PsiClass | ||
|
||
@Inject | ||
protected lateinit var docHelper: DocHelper | ||
|
||
override fun bind(builder: ActionContext.ActionContextBuilder) { | ||
super.bind(builder) | ||
|
||
DisableDocSupport.bind(builder) | ||
|
||
userInfoPsiClass = loadClass("model/UserInfo.java")!! | ||
userCtrlPsiClass = loadClass("api/UserCtrl.java")!! | ||
} | ||
|
||
override fun customConfig(): String? { | ||
return super.customConfig() + "\n" + | ||
"doc.source.disable=$disableDoc" | ||
} | ||
|
||
class DisableDocSupportWithConfigTrueTest : DisableDocSupportTest() { | ||
override val disableDoc: Boolean = true | ||
|
||
fun testGetDoc() { | ||
assertEmpty(docHelper.getAttrOfDocComment(userInfoPsiClass)) | ||
|
||
val fields = userInfoPsiClass.findFieldByName("name", false) | ||
assertEmpty(docHelper.getAttrOfDocComment(fields!!)) | ||
assertTrue(docHelper.getTagMapOfDocComment(fields).isEmpty()) | ||
assertEmpty(docHelper.findDocByTag(fields, "mock")) | ||
assertFalse(docHelper.hasTag(fields, "mock")) | ||
|
||
val method = userCtrlPsiClass.findMethodsByName("get", false).first() | ||
assertEmpty(docHelper.getAttrOfDocComment(method!!)) | ||
assertTrue(docHelper.getTagMapOfDocComment(method).isEmpty()) | ||
assertTrue(docHelper.getSubTagMapOfDocComment(method, "param").isEmpty()) | ||
assertEmpty(docHelper.findDocByTag(method, "folder")) | ||
assertEmpty(docHelper.findDocsByTagAndName(method, "param", "id")) | ||
} | ||
} | ||
|
||
class DisableDocSupportWithConfigFalseTest : DisableDocSupportTest() { | ||
override val disableDoc: Boolean = false | ||
|
||
fun testGetDoc() { | ||
assertEquals(docHelper.getAttrOfDocComment(userInfoPsiClass), "user info") | ||
|
||
val fields = userInfoPsiClass.findFieldByName("name", false) | ||
assertEquals(docHelper.getAttrOfDocComment(fields!!), "user name") | ||
assertEquals(docHelper.getTagMapOfDocComment(fields), mapOf("default" to "tangcent", "mock" to "tangcent")) | ||
assertEquals(docHelper.findDocByTag(fields, "mock"), "tangcent") | ||
assertTrue(docHelper.hasTag(fields, "mock")) | ||
|
||
val method = userCtrlPsiClass.findMethodsByName("get", false).first() | ||
assertEquals(docHelper.getAttrOfDocComment(method!!), "get user info") | ||
assertEquals( | ||
docHelper.getTagMapOfDocComment(method), | ||
mapOf("folder" to "update-apis", "param" to "id user id", "undone" to "") | ||
) | ||
assertEquals(docHelper.getSubTagMapOfDocComment(method, "param"), mapOf("id" to "user id")) | ||
assertEquals(docHelper.findDocByTag(method, "folder"), "update-apis") | ||
assertEquals(docHelper.findDocsByTagAndName(method, "param", "id"), "user id") | ||
} | ||
} | ||
} |