Skip to content

Commit

Permalink
Merge pull request #154 from tangcent/merge-easy-api
Browse files Browse the repository at this point in the history
resolve generic info in super class
  • Loading branch information
tangcent authored Mar 9, 2020
2 parents d86b330 + ea561d5 commit caada4d
Show file tree
Hide file tree
Showing 19 changed files with 781 additions and 322 deletions.
15 changes: 13 additions & 2 deletions common-api/src/main/kotlin/com/itangcent/common/model/Request.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.itangcent.common.model

import com.itangcent.common.constant.HttpMethod

open class Request : Doc() {

var path: String? = null
Expand All @@ -25,14 +27,23 @@ open class Request : Doc() {
}

fun Request.getContentType(): String? {
return this.header("content-type")
}

fun Request.header(name: String): String? {
if (this.headers.isNullOrEmpty()) {
return null
}
return this.headers!!.filter { it.name?.toLowerCase() == "content-type" }
val lowerName = name.toLowerCase()
return this.headers!!.filter { it.name?.toLowerCase() == lowerName }
.map { it.value }
.firstOrNull()
}

fun Request.hasBody(): Boolean {
return this.method != null && this.method != "GET"
return this.method != null && this.method != HttpMethod.GET
}

fun Request.hasMethod(): Boolean {
return this.method != null && this.method != HttpMethod.NO_METHOD
}
6 changes: 3 additions & 3 deletions idea-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ dependencies {
// compile fileTree(dir: 'libs', include: ['*.jar'])


compile('com.itangcent:intellij-idea:0.3.1-SNAPSHOT') {
compile('com.itangcent:intellij-idea:0.4.0-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}

compile('com.itangcent:intellij-kotlin-support:0.3.1-SNAPSHOT') {
compile('com.itangcent:intellij-kotlin-support:0.4.0-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}

compile('com.itangcent:intellij-scala-support:0.3.1-SNAPSHOT') {
compile('com.itangcent:intellij-scala-support:0.4.0-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import com.intellij.lang.jvm.JvmModifier
import com.intellij.psi.*
import com.intellij.psi.util.*
import com.itangcent.common.logger.traceError
import com.itangcent.common.utils.GsonUtils
import com.itangcent.common.utils.KV
import com.itangcent.common.utils.KVUtils
import com.itangcent.common.utils.Visional
import com.itangcent.common.utils.*
import com.itangcent.idea.plugin.settings.SettingBinder
import com.itangcent.intellij.config.rule.RuleComputer
import com.itangcent.intellij.context.ActionContext
Expand Down Expand Up @@ -87,8 +84,14 @@ class DefaultMethodInferHelper : MethodInferHelper {
return maxDeep!!
}

private val emptyCallMethodCache: HashMap<PsiMethod, Any?> = HashMap()

override fun inferReturn(psiMethod: PsiMethod, option: Int): Any? {
return cleanInvalidKeys(inferReturn(psiMethod, null, null, option))
return cleanInvalidKeys(
emptyCallMethodCache.safeComputeIfAbsent(psiMethod) {
return@safeComputeIfAbsent inferReturn(psiMethod, null, null, option)
}
)
}

override fun inferReturn(psiMethod: PsiMethod, caller: Any?, args: Array<Any?>?, option: Int): Any? {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import com.intellij.psi.PsiMethod
import com.itangcent.common.utils.concat
import com.itangcent.common.utils.headLine
import com.itangcent.common.utils.notEmpty
import com.itangcent.idea.plugin.rule.computer
import com.itangcent.intellij.config.rule.RuleComputer
import com.itangcent.intellij.jvm.DocHelper
import com.itangcent.intellij.jvm.element.ExplicitMethod

@Singleton
open class ApiHelper {
Expand Down Expand Up @@ -39,6 +41,14 @@ open class ApiHelper {
return psiMethod.name
}

protected open fun findAttrOfMethod(method: ExplicitMethod): String? {
val attrOfDocComment = docHelper!!.getAttrOfDocComment(method.psi())

val docByRule = ruleComputer!!.computer(ClassExportRuleKeys.METHOD_DOC, method)

return attrOfDocComment.concat(docByRule)
}

protected open fun findAttrOfMethod(method: PsiMethod): String? {
val attrOfDocComment = docHelper!!.getAttrOfDocComment(method)

Expand All @@ -47,43 +57,72 @@ open class ApiHelper {
return attrOfDocComment.concat(docByRule)
}

fun nameAndAttrOfApi(psiMethod: PsiMethod): Pair<String?, String?> {
fun nameAndAttrOfApi(explicitMethod: ExplicitMethod): Pair<String?, String?> {
var name: String? = null
var attr: String? = null
nameAndAttrOfApi(psiMethod, {
nameAndAttrOfApi(explicitMethod, {
name = it
}, {
attr = attr.concat(it)
})
return name to attr
}

fun nameAndAttrOfApi(psiMethod: PsiMethod, nameHandle: (String) -> Unit,
fun nameAndAttrOfApi(explicitMethod: ExplicitMethod, nameHandle: (String) -> Unit,
attrHandle: (String) -> Unit) {


var named = false
val nameByRule = ruleComputer!!.computer(ClassExportRuleKeys.API_NAME, psiMethod)
val nameByRule = ruleComputer!!.computer(ClassExportRuleKeys.API_NAME, explicitMethod)
if (nameByRule.notEmpty()) {
nameHandle(nameByRule!!)
named = true
}

var attrOfMethod = findAttrOfMethod(explicitMethod)

attrOfMethod = docParseHelper!!.resolveLinkInAttr(attrOfMethod, explicitMethod.psi())

if (attrOfMethod.notEmpty()) {
attrHandle(attrOfMethod!!)

if (!named) {
nameHandle(attrOfMethod.headLine() ?: explicitMethod.name())
named = true
}
}

if (!named) {
nameHandle(explicitMethod.name())
}
}

fun nameAndAttrOfApi(explicitMethod: PsiMethod, nameHandle: (String) -> Unit,
attrHandle: (String) -> Unit) {


var named = false
val nameByRule = ruleComputer!!.computer(ClassExportRuleKeys.API_NAME, explicitMethod)
if (nameByRule.notEmpty()) {
nameHandle(nameByRule!!)
named = true
}

var attrOfMethod = findAttrOfMethod(psiMethod)
var attrOfMethod = findAttrOfMethod(explicitMethod)

attrOfMethod = docParseHelper!!.resolveLinkInAttr(attrOfMethod, psiMethod)
attrOfMethod = docParseHelper!!.resolveLinkInAttr(attrOfMethod, explicitMethod)

if (attrOfMethod.notEmpty()) {
attrHandle(attrOfMethod!!)

if (!named) {
nameHandle(attrOfMethod.headLine() ?: psiMethod.name)
nameHandle(attrOfMethod.headLine() ?: explicitMethod.name)
named = true
}
}

if (!named) {
nameHandle(psiMethod.name)
nameHandle(explicitMethod.name)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiType
import com.itangcent.common.utils.KVUtils
import com.itangcent.intellij.config.rule.RuleComputer
import com.itangcent.intellij.jvm.DuckTypeHelper
import com.itangcent.intellij.jvm.JvmClassHelper
import com.itangcent.intellij.jvm.PsiClassHelper
import com.itangcent.intellij.jvm.PsiResolver
import com.itangcent.intellij.jvm.duck.DuckType
import com.itangcent.intellij.jvm.duck.SingleDuckType
import com.itangcent.intellij.jvm.duck.SingleUnresolvedDuckType
import com.itangcent.intellij.logger.Logger
import com.itangcent.intellij.psi.ClassRuleKeys

Expand All @@ -30,6 +34,62 @@ class CommentResolver {
@Inject
protected val psiClassHelper: PsiClassHelper? = null

@Inject
protected val duckTypeHelper: DuckTypeHelper? = null


fun resolveCommentForType(duckType: DuckType, context: PsiElement): String? {

if (!duckType.isSingle()) {
return null

}

if (jvmClassHelper!!.isEnum(duckType)) {

if (duckType is SingleUnresolvedDuckType) {
return resolveCommentForType(duckType.psiType(), context)
}

val convertTo = ruleComputer!!.computer(ClassRuleKeys.ENUM_CONVERT, duckType, context)

if (!convertTo.isNullOrBlank()) {
if (convertTo.contains("#")) {
val options = psiClassHelper!!.resolveEnumOrStatic(convertTo, context, "")
if (!options.isNullOrEmpty()) {
return KVUtils.getOptionDesc(options)
}
} else {
val resolveClass = duckTypeHelper!!.resolveClass(convertTo, context)
if (resolveClass == null) {
logger!!.error("failed to resolve class:$convertTo")
return null
}
val constants = psiClassHelper!!.parseEnumConstant(resolveClass)
if (constants.isEmpty()) {
logger!!.error("nothing be found at:$convertTo")
return null
}

return KVUtils.getConstantDesc(constants)
}
}

if (duckType is SingleDuckType) {
val enumClass = duckType.psiClass()

val constants = psiClassHelper!!.parseEnumConstant(enumClass)
if (constants.isEmpty()) {
logger!!.error("nothing be found at:$convertTo")
return null
}

return KVUtils.getConstantDesc(constants)
}
}

return null
}

fun resolveCommentForType(psiType: PsiType, context: PsiElement): String? {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ fun RequestHelper.addHeader(request: Request, name: String, value: String) {
addHeader(request, header)
}


fun RequestHelper.addHeaderIfMissed(request: Request, name: String, value: String): Boolean {
if (request.header(name) != null) {
return false
}
addHeader(request, name, value)
return true
}

fun RequestHelper.addPathParam(request: Request, name: String, desc: String) {
val pathParam = PathParam()
pathParam.name = name
Expand All @@ -103,4 +112,11 @@ fun RequestHelper.addResponseHeader(response: Response, name: String, value: Str
header.value = value
addResponseHeader(response, header)
}

fun RequestHelper.setMethodIfMissed(request: Request, method: String) {
if (request.hasMethod()) {
return
}
this.setMethod(request, method)
}
//endregion utils------------------------------------------------------------------
Loading

0 comments on commit caada4d

Please sign in to comment.