Skip to content

Commit

Permalink
feat: support config ignore_irregular_api_method (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent authored Mar 27, 2023
1 parent eb8fefa commit f86c3bb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ package com.itangcent.idea.plugin.api

import com.google.inject.Inject
import com.google.inject.Singleton
import com.intellij.psi.*
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiField
import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiType
import com.itangcent.common.logger.Log
import com.itangcent.common.model.Doc
import com.itangcent.common.utils.KV
import com.itangcent.common.utils.notNullOrBlank
import com.itangcent.common.utils.notNullOrEmpty
import com.itangcent.common.utils.stream
import com.itangcent.common.utils.*
import com.itangcent.idea.plugin.api.export.core.ClassExportRuleKeys
import com.itangcent.idea.plugin.api.export.core.ClassExporter
import com.itangcent.idea.plugin.api.export.core.LinkResolver
import com.itangcent.idea.swing.MessagesHelper
import com.itangcent.intellij.config.ConfigReader
import com.itangcent.intellij.config.rule.RuleComputer
import com.itangcent.intellij.config.rule.computer
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.withBoundary
import com.itangcent.intellij.jvm.*
import com.itangcent.intellij.jvm.element.ExplicitElement
import com.itangcent.intellij.jvm.element.ExplicitMethod
import com.itangcent.intellij.logger.Logger
import com.itangcent.intellij.psi.SelectedHelper
Expand Down Expand Up @@ -64,6 +64,9 @@ open class ClassApiExporterHelper {
@Inject
protected lateinit var messagesHelper: MessagesHelper

@Inject
private lateinit var configReader: ConfigReader

companion object : Log()

fun extractParamComment(psiMethod: PsiMethod): KV<String, Any>? {
Expand Down Expand Up @@ -138,9 +141,6 @@ open class ClassApiExporterHelper {
val methods = duckTypeHelper!!.explicit(cls)
.methods()
.stream()
.filter { !jvmClassHelper!!.isBasicMethod(it.psi().name) }
.filter { !it.psi().hasModifierProperty("static") }
.filter { !it.psi().isConstructor }
.filter { !shouldIgnore(it) }
.toList()
actionContext.runAsync {
Expand All @@ -160,21 +160,30 @@ open class ClassApiExporterHelper {
}
}

protected open fun shouldIgnore(explicitElement: ExplicitElement<*>): Boolean {
protected open fun shouldIgnore(explicitElement: ExplicitMethod): Boolean {
if (ignoreIrregularApiMethod() && (jvmClassHelper!!.isBasicMethod(explicitElement.psi().name)
|| explicitElement.psi().hasModifierProperty("static")
|| explicitElement.psi().isConstructor)
) {
return true
}
return ruleComputer!!.computer(ClassExportRuleKeys.IGNORE, explicitElement) ?: false
}

protected open fun shouldIgnore(psiElement: PsiElement): Boolean {
return ruleComputer!!.computer(ClassExportRuleKeys.IGNORE, psiElement) ?: false
protected open fun shouldIgnore(psiMethod: PsiMethod): Boolean {
if (ignoreIrregularApiMethod() && (jvmClassHelper!!.isBasicMethod(psiMethod.name)
|| psiMethod.hasModifierProperty("static")
|| psiMethod.isConstructor)
) {
return true
}
return ruleComputer!!.computer(ClassExportRuleKeys.IGNORE, psiMethod) ?: false
}

fun foreachPsiMethod(cls: PsiClass, handle: (PsiMethod) -> Unit) {
actionContext.runInReadUI {
jvmClassHelper!!.getAllMethods(cls)
.stream()
.filter { !jvmClassHelper.isBasicMethod(it.name) }
.filter { !it.hasModifierProperty("static") }
.filter { !it.isConstructor }
.filter { !shouldIgnore(it) }
.forEach(handle)
}
Expand Down Expand Up @@ -240,4 +249,8 @@ open class ClassApiExporterHelper {
}
}
}

private fun ignoreIrregularApiMethod(): Boolean {
return (configReader.first("ignore_irregular_api_method")?.toBool() != false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.intellij.psi.PsiMethod
import com.itangcent.common.logger.traceError
import com.itangcent.common.model.MethodDoc
import com.itangcent.common.utils.KV
import com.itangcent.idea.plugin.api.ClassApiExporterHelper
import com.itangcent.idea.plugin.api.export.Orders
import com.itangcent.idea.plugin.api.export.condition.ConditionOnDoc
import com.itangcent.idea.plugin.api.export.condition.ConditionOnSimple
Expand All @@ -33,6 +34,9 @@ open class SimpleGenericMethodDocClassExporter : ClassExporter {
@Inject
protected val jvmClassHelper: JvmClassHelper? = null

@Inject
protected lateinit var classApiExporterHelper: ClassApiExporterHelper

override fun support(docType: KClass<*>): Boolean {
return docType == MethodDoc::class
}
Expand Down Expand Up @@ -67,18 +71,20 @@ open class SimpleGenericMethodDocClassExporter : ClassExporter {
!hasApi(cls) -> {
return false
}

shouldIgnore(cls) -> {
logger!!.info("ignore class: $clsQualifiedName")
return true
}

else -> {
logger!!.info("search api from: $clsQualifiedName")

val kv = KV.create<String, Any?>()

processClass(cls, kv)

foreachMethod(cls) { method ->
classApiExporterHelper.foreachPsiMethod(cls) { method ->
if (isApi(method) && methodFilter?.checkMethod(method) != false) {
exportMethodApi(cls, method, kv, docHandle)
}
Expand Down Expand Up @@ -144,13 +150,4 @@ open class SimpleGenericMethodDocClassExporter : ClassExporter {
protected open fun processMethod(method: PsiMethod, kv: KV<String, Any?>, methodDoc: MethodDoc) {
methodDoc.name = apiHelper!!.nameOfApi(method)
}

private fun foreachMethod(cls: PsiClass, handle: (PsiMethod) -> Unit) {
jvmClassHelper!!.getAllMethods(cls)
.filter { !jvmClassHelper.isBasicMethod(it.name) }
.filter { !it.hasModifierProperty("static") }
.filter { !it.isConstructor }
.filter { !shouldIgnore(it) }
.forEach(handle)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ open class SimpleGenericRequestClassExporter : ClassExporter {

private fun exportMethodApi(psiClass: PsiClass, method: PsiMethod, docHandle: DocHandle) {

actionContext!!.checkStatus()
actionContext.checkStatus()
if (!isApi(method)) {
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ open class CustomizedPsiClassHelper : ContextualPsiClassHelper() {
}

override fun ignoreField(psiField: PsiField): Boolean {
if (configReader.first("ignore_static_and_final")?.asBool() == false) {
if (configReader.first("ignore_static_and_final_field")?.asBool() == false) {
return false
}
return super.ignoreField(psiField)
Expand Down
6 changes: 5 additions & 1 deletion idea-plugin/src/main/resources/.recommend.easy.api.config
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ method.doc[@kotlin.Deprecated]=groovy:"\n「deprecated」" + it.ann("kotlin.Depr
method.doc[groovy:it.containingClass().hasAnn("kotlin.Deprecated")]=groovy:"\n「deprecated」 " + it.containingClass().ann("kotlin.Deprecated","message")
field.doc[@kotlin.Deprecated]=groovy:"\n「deprecated」" + it.ann("kotlin.Deprecated","message")

#[not_ignore_irregular_api_method]
# not ignore irregular api method
ignore_irregular_api_method=false

#[Jackson]*
#Support for Jackson annotations
[email protected]#value
Expand Down Expand Up @@ -272,7 +276,7 @@ field.ignore=groovy:!(it.hasModifier("private")||it.hasModifier("protected"))

#[not_ignore_static_final_field]
# not ignore `static final` field
ignore_static_and_final=false
ignore_static_and_final_field=false

#[Jackson_JsonNaming]
# use Jackson JsonNaming
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal class RecommendConfigLoaderTest {
@Test
fun testCodes() {
assertEquals(
"[module, ignore, deprecated_java, deprecated_kotlin, Jackson, Jackson_JsonIgnoreProperties, Jackson_JsonUnwrapped, Gson, ignore_transient_field, converts, spring_Entity, spring_webflux, spring.validations, spring.ui, jakarta.validation, jakarta.validation(grouped), javax.validation, javax.validation(grouped), is_file, import_spring_properties, resolve_spring_properties, ignore_serialVersionUID, private_protected_field_only, not_ignore_static_final_field, Jackson_JsonNaming, Jackson_UpperCamelCaseStrategy, Jackson_SnakeCaseStrategy, Jackson_LowerCaseStrategy, Jackson_KebabCaseStrategy, Jackson_LowerDotCaseStrategy, properties, Fastjson, enum_auto_select_field_by_type, enum_use_name, enum_use_ordinal]",
"[module, ignore, deprecated_java, deprecated_kotlin, not_ignore_irregular_api_method, Jackson, Jackson_JsonIgnoreProperties, Jackson_JsonUnwrapped, Gson, ignore_transient_field, converts, spring_Entity, spring_webflux, spring.validations, spring.ui, jakarta.validation, jakarta.validation(grouped), javax.validation, javax.validation(grouped), is_file, import_spring_properties, resolve_spring_properties, ignore_serialVersionUID, private_protected_field_only, not_ignore_static_final_field, Jackson_JsonNaming, Jackson_UpperCamelCaseStrategy, Jackson_SnakeCaseStrategy, Jackson_LowerCaseStrategy, Jackson_KebabCaseStrategy, Jackson_LowerDotCaseStrategy, properties, Fastjson, enum_auto_select_field_by_type, enum_use_name, enum_use_ordinal]",
RecommendConfigLoader.codes().contentToString()
)

Expand Down

0 comments on commit f86c3bb

Please sign in to comment.