Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support kotlin project #125

Merged
merged 2 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion common-api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.41'
ext.kotlin_version = '1.3.50'

repositories {
mavenCentral()
Expand All @@ -21,6 +21,8 @@ repositories {
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.9'

Expand Down
21 changes: 21 additions & 0 deletions common-api/src/main/kotlin/com/itangcent/common/utils/KitUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,24 @@ fun StringBuilder.appendlnIfNotEmpty(): StringBuilder {
}
return this
}

fun Any?.tinyString(): String? {
return when {
this == null -> null
this is String -> this
this is Array<*> -> this.first().tinyString()
this is Collection<*> -> this.first().tinyString()
else -> this.toString()
}
}

fun <K, V> Map<K, V>?.any(vararg ks: K): V? {
if (this == null) return null
for (k in ks) {
val v = this[k]
if (v != null) {
return v
}
}
return null
}
14 changes: 10 additions & 4 deletions idea-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.41'
ext.kotlin_version = '1.3.50'

repositories {
mavenCentral()
Expand Down Expand Up @@ -32,6 +32,9 @@ intellij {
instrumentCode = true
updateSinceUntilBuild false
sameSinceUntilBuild false

plugins 'org.jetbrains.kotlin:1.3.11-release-IJ2017.3-1'
// plugins 'org.jetbrains.kotlin:1.3.21-release-IJ2018.2-1' //dependency kotlin
}

task javadocJar(type: Jar, dependsOn: javadoc) {
Expand All @@ -51,7 +54,7 @@ patchPluginXml {

jar {
excludes = ["com/google/common/**"
, "kotlin/**"
// , "kotlin/**"
]
// excludes = ["com/google/common/**"]
// excludes = ["com/google/common/**", "org/apache/http/util/**"]
Expand Down Expand Up @@ -83,8 +86,11 @@ dependencies {

// compile fileTree(dir: 'libs', include: ['*.jar'])

compile name: 'intellij-kotlin'

compile name: 'commons'
compile name: 'intellij-idea'
compile name: 'intellij-jvm'
compile name: 'intellij-kotlin-support'
compile name: 'guice-action'

compile("com.google.inject:guice:4.2.2") {
exclude group: 'org.checkerframework', module: 'checker-compat-qual'
Expand Down
Binary file added idea-plugin/libs/commons.jar
Binary file not shown.
Binary file added idea-plugin/libs/guice-action.jar
Binary file not shown.
Binary file added idea-plugin/libs/intellij-idea.jar
Binary file not shown.
Binary file added idea-plugin/libs/intellij-jvm.jar
Binary file not shown.
Binary file added idea-plugin/libs/intellij-kotlin-support.jar
Binary file not shown.
Binary file removed idea-plugin/libs/intellij-kotlin.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.Project
import com.itangcent.common.Setup
import com.itangcent.idea.plugin.script.GroovyActionExtLoader
import com.itangcent.idea.plugin.script.LoggerBuffer
import com.itangcent.idea.plugin.settings.SettingBinder
Expand All @@ -14,6 +15,7 @@ import com.itangcent.intellij.constant.EventKey
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.guice.singleton
import com.itangcent.intellij.extend.guice.with
import com.itangcent.intellij.jvm.kotlin.KotlinAutoInject
import com.itangcent.intellij.logger.ConsoleRunnerLogger
import com.itangcent.intellij.logger.Logger
import javax.swing.Icon
Expand All @@ -29,6 +31,7 @@ abstract class BasicAnAction : KotlinAnAction {
}

override fun onBuildActionContext(event: AnActionEvent, builder: ActionContext.ActionContextBuilder) {

super.onBuildActionContext(event, builder)
builder.bindInstance("plugin.name", "easy_api")

Expand Down Expand Up @@ -66,4 +69,10 @@ abstract class BasicAnAction : KotlinAnAction {
?: return
loadActionExt.init(builder)
}

companion object {
init {
Setup.setup(KotlinAutoInject::class)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.itangcent.common.utils.GsonUtils
import com.itangcent.common.utils.Visional
import com.itangcent.intellij.config.rule.RuleComputer
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.jvm.JvmClassHelper
import com.itangcent.intellij.logger.Logger
import com.itangcent.intellij.psi.*
import com.itangcent.intellij.util.KV
Expand Down Expand Up @@ -42,6 +43,9 @@ class MethodReturnInferHelper {
@Inject
private val actionContext: ActionContext? = null

@Inject
private val jvmClassHelper: JvmClassHelper? = null

private val staticMethodCache: HashMap<Pair<PsiMethod, Array<Any?>?>, Any?> = HashMap()

private val methodStack: Stack<Infer> = Stack()
Expand Down Expand Up @@ -710,10 +714,10 @@ class MethodReturnInferHelper {
psiType is PsiArrayType -> {
return psiClassHelper.getTypeObject(psiType, context, simpleJsonOption)
}
PsiClassHelper.isCollection(psiType) -> { //list type
jvmClassHelper!!.isCollection(psiType) -> { //list type
return psiClassHelper.getTypeObject(psiType, context, jsonOption)
}
PsiClassHelper.isMap(psiType) -> { //map type
jvmClassHelper.isMap(psiType) -> { //map type
return psiClassHelper.getTypeObject(psiType, context, simpleJsonOption)
}
else -> {
Expand Down Expand Up @@ -743,10 +747,10 @@ class MethodReturnInferHelper {
psiType is PsiArrayType -> {
return psiClassHelper.getTypeObject(psiType, context, simpleJsonOption)
}
PsiClassHelper.isCollection(psiType) -> { //list type
jvmClassHelper!!.isCollection(psiType) -> { //list type
return psiClassHelper.getTypeObject(psiType, context, simpleJsonOption)
}
PsiClassHelper.isMap(psiType) -> { //map type
jvmClassHelper.isMap(psiType) -> { //map type
return psiClassHelper.getTypeObject(psiType, context, simpleJsonOption)
}
else -> {
Expand Down Expand Up @@ -958,7 +962,7 @@ class MethodReturnInferHelper {
return variable
}
is PsiField -> {
if (PsiClassHelper.hasAnyModify(psiElement, PsiClassHelper.staticFinalFieldModifiers)) {
if (methodReturnInferHelper.jvmClassHelper!!.isStaticFinal(psiElement)) {
return processStaticField(psiElement)
}
val fieldName = methodReturnInferHelper.psiClassHelper!!.getJsonFieldName(psiElement)
Expand Down Expand Up @@ -1460,7 +1464,7 @@ class MethodReturnInferHelper {
is PsiExpression -> return processExpression(psiElement)
is PsiStatement -> return processStatement(psiElement)
is PsiField -> {
if (PsiClassHelper.hasAnyModify(psiElement, PsiClassHelper.staticFinalFieldModifiers)) {
if (methodReturnInferHelper.jvmClassHelper!!.isStaticFinal(psiElement)) {
return processStaticField(psiElement)
}
throw IllegalArgumentException("Quickly Infer Failed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import com.google.inject.Singleton
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiMethod
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.util.DocCommentUtils
import com.itangcent.intellij.jvm.DocHelper

@Singleton
class ResourceHelper {

@Inject
private val actionContext: ActionContext? = null

@Inject
private val docHelper: DocHelper? = null

fun findResourceClass(resource: Any): PsiClass? {
return when (resource) {
is PsiMethod -> actionContext!!.callInReadUI { resource.containingClass }
Expand All @@ -23,8 +26,7 @@ class ResourceHelper {


fun findAttrOfClass(cls: PsiClass): String? {
val docComment = actionContext!!.callInReadUI { cls.docComment }
val docText = DocCommentUtils.getAttrOfDocComment(docComment)
val docText = docHelper!!.getAttrOfDocComment(cls)
return when {
docText.isNullOrBlank() -> cls.name
else -> docText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ import com.itangcent.idea.plugin.api.MethodReturnInferHelper
import com.itangcent.idea.plugin.settings.SettingBinder
import com.itangcent.intellij.config.rule.RuleComputer
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.jvm.DocHelper
import com.itangcent.intellij.jvm.JvmClassHelper
import com.itangcent.intellij.logger.Logger
import com.itangcent.intellij.psi.DuckTypeHelper
import com.itangcent.intellij.psi.JsonOption
import com.itangcent.intellij.psi.PsiClassHelper
import com.itangcent.intellij.psi.PsiClassUtils
import com.itangcent.intellij.util.*
import org.apache.commons.lang3.StringUtils
import com.itangcent.intellij.util.KV
import com.itangcent.intellij.util.Magics
import com.itangcent.intellij.util.forEachValid
import com.itangcent.intellij.util.traceError
import java.util.*
import java.util.regex.Pattern
import kotlin.reflect.KClass
Expand Down Expand Up @@ -51,6 +55,9 @@ abstract class AbstractRequestClassExporter : ClassExporter, Worker {
@Inject
protected val logger: Logger? = null

@Inject
private val docHelper: DocHelper? = null

@Inject
protected val psiClassHelper: PsiClassHelper? = null

Expand All @@ -72,6 +79,9 @@ abstract class AbstractRequestClassExporter : ClassExporter, Worker {
@Inject
protected val ruleComputer: RuleComputer? = null

@Inject
protected val jvmClassHelper: JvmClassHelper? = null

@Inject(optional = true)
protected val methodFilter: MethodFilter? = null

Expand Down Expand Up @@ -224,60 +234,48 @@ abstract class AbstractRequestClassExporter : ClassExporter, Worker {
}

open protected fun findAttrOfMethod(method: PsiMethod): String? {
return DocCommentUtils.getAttrOfDocComment(method.docComment)
return docHelper!!.getAttrOfDocComment(method)
}

private fun extractParamComment(psiMethod: PsiMethod): KV<String, Any>? {
val docComment = psiMethod.docComment
var methodParamComment: KV<String, Any>? = null
if (docComment != null) {
for (paramDocTag in docComment.findTagsByName("param")) {
var name: String? = null
var value: String? = null
paramDocTag.dataElements
.asSequence()
.map { it?.text }
.filterNot { StringUtils.isBlank(it) }
.forEach {
when {
name == null -> name = it
value == null -> value = it
else -> value += it
}
}
if (StringUtils.isNoneBlank(name, value)) {
if (methodParamComment == null) methodParamComment = KV.create()

if (value!!.contains("@link")) {
val pattern = Pattern.compile("\\{@link (.*?)\\}")
val matcher = pattern.matcher(value)
val subTagMap = docHelper!!.getSubTagMapOfDocComment(psiMethod, "param")

val options: ArrayList<HashMap<String, Any?>> = ArrayList()

val sb = StringBuffer()
while (matcher.find()) {
matcher.appendReplacement(sb, "")
val linkClassOrProperty = matcher.group(1)
psiClassHelper!!.resolveEnumOrStatic(linkClassOrProperty, psiMethod, name!!)
?.let { options.addAll(it) }
}
matcher.appendTail(sb)
methodParamComment[name!!] = sb.toString()
if (!options.isNullOrEmpty()) {
methodParamComment["$name@options"] = options
}
continue
var methodParamComment: KV<String, Any>? = null
subTagMap.entries.forEach { entry ->
val name: String = entry.key
val value: String? = entry.value
if (methodParamComment == null) methodParamComment = KV.create()
if (!value.isNullOrBlank()) {
if (value.contains("@link")) {
val pattern = Pattern.compile("\\{@link (.*?)\\}")
val matcher = pattern.matcher(value)

val options: ArrayList<HashMap<String, Any?>> = ArrayList()

val sb = StringBuffer()
while (matcher.find()) {
matcher.appendReplacement(sb, "")
val linkClassOrProperty = matcher.group(1)
psiClassHelper!!.resolveEnumOrStatic(linkClassOrProperty, psiMethod, name!!)
?.let { options.addAll(it) }
}
matcher.appendTail(sb)
methodParamComment!![name] = sb.toString()
if (!options.isNullOrEmpty()) {
methodParamComment!!["$name@options"] = options
}
methodParamComment[name!!] = value!!
} else {
methodParamComment!![name] = value
}
}

}
return methodParamComment
}

private fun foreachMethod(cls: PsiClass, handle: (PsiMethod) -> Unit) {
cls.allMethods
.filter { !PsiClassHelper.JAVA_OBJECT_METHODS.contains(it.name) }
.filter { !jvmClassHelper!!.isBasicMethod(it.name) }
.filter { !it.hasModifier(JvmModifier.STATIC) }
.filter { !it.isConstructor }
.filter { !shouldIgnore(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.itangcent.common.model.MethodDoc
import com.itangcent.common.model.Request
import kotlin.reflect.KClass

import org.jetbrains.kotlin.psi.KtFile

interface ClassExporter {
fun export(cls: Any, docHandle: DocHandle): Boolean

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.itangcent.idea.plugin.api.export

import com.google.inject.Inject
import com.google.inject.Singleton
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiField
import com.intellij.psi.PsiMethod
import com.itangcent.intellij.jvm.DocHelper
import com.itangcent.intellij.psi.PsiClassUtils
import com.itangcent.intellij.util.DocCommentUtils

@Singleton
open class DefaultLinkResolver : LinkResolver {

@Inject
private val docHelper: DocHelper? = null

override fun linkToClass(linkClass: Any): String? {
if (linkClass !is PsiClass) {
return "[$linkClass]"
}
val attrOfClass = DocCommentUtils.getAttrOfDocComment(linkClass.docComment)
val attrOfClass = docHelper!!.getAttrOfDocComment(linkClass)
return when {
attrOfClass.isNullOrBlank() -> "[${linkClass.name}]"
else -> "[$attrOfClass]"
Expand All @@ -24,7 +29,7 @@ open class DefaultLinkResolver : LinkResolver {
if (linkMethod !is PsiMethod) {
return "[$linkMethod]"
}
val attrOfMethod = DocCommentUtils.getAttrOfDocComment(linkMethod.docComment)
val attrOfMethod = docHelper!!.getAttrOfDocComment(linkMethod)
return when {
attrOfMethod.isNullOrBlank() -> "[${PsiClassUtils.fullNameOfMethod(linkMethod)}]"
else -> "[$attrOfMethod]"
Expand All @@ -35,7 +40,7 @@ open class DefaultLinkResolver : LinkResolver {
if (linkField !is PsiField) {
return "[$linkField]"
}
val attrOfProperty = DocCommentUtils.getAttrOfDocComment(linkField.docComment)
val attrOfProperty = docHelper!!.getAttrOfDocComment(linkField)
return when {
attrOfProperty.isNullOrBlank() -> "[${PsiClassUtils.fullNameOfField(linkField)}]"
else -> "[$attrOfProperty]"
Expand Down
Loading