Skip to content

Commit

Permalink
optimized action interrupt
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent committed Jun 17, 2019
1 parent 4d5323d commit ece4048
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 69 deletions.
Binary file modified idea-plugin/libs/intellij-kotlin.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import com.itangcent.idea.plugin.api.cache.ProjectCacheRepository
import com.itangcent.idea.plugin.api.export.CommonRules
import com.itangcent.idea.plugin.api.export.SpringClassExporter
import com.itangcent.idea.utils.ModuleHelper
import com.itangcent.intellij.config.rule.RuleParser
import com.itangcent.intellij.config.rule.SimpleRuleParser
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.guice.singleton
import com.itangcent.intellij.extend.guice.with
Expand All @@ -21,6 +23,7 @@ abstract class ApiExportAction(text: String) : BasicAnAction(text) {
override fun onBuildActionContext(builder: ActionContext.ActionContextBuilder) {
super.onBuildActionContext(builder)

builder.bind(RuleParser::class) { it.with(SimpleRuleParser::class) }
builder.bind(CommonRules::class) { it.singleton() }
builder.bind(PsiClassHelper::class) { it.with(DefaultPsiClassHelper::class).singleton() }
builder.bind(DuckTypeHelper::class) { it.singleton() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import com.intellij.openapi.components.ServiceManager
import com.itangcent.idea.plugin.settings.SettingBinder
import com.itangcent.idea.utils.ConfigurableLogger
import com.itangcent.intellij.actions.KotlinAnAction
import com.itangcent.intellij.config.rule.RuleParser
import com.itangcent.intellij.config.rule.SimpleRuleParser
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.guice.singleton
import com.itangcent.intellij.extend.guice.with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.PlatformDataKeys
import com.intellij.openapi.project.Project
import com.itangcent.idea.plugin.fields.FieldJsonGenerator
import com.itangcent.idea.utils.traceError
import com.itangcent.intellij.config.rule.RuleParser
import com.itangcent.intellij.config.rule.SimpleRuleParser
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.guice.singleton
import com.itangcent.intellij.extend.guice.with
Expand All @@ -26,6 +29,7 @@ class FieldsToJsonAction : BasicAnAction("To Json") {
override fun onBuildActionContext(builder: ActionContext.ActionContextBuilder) {
super.onBuildActionContext(builder)

builder.bind(RuleParser::class) { it.with(SimpleRuleParser::class) }
builder.bind(PsiClassHelper::class) { it.with(DefaultPsiClassHelper::class).singleton() }
builder.bind(DuckTypeHelper::class) { it.singleton() }
}
Expand All @@ -44,7 +48,8 @@ class FieldsToJsonAction : BasicAnAction("To Json") {
}
}
} catch (e: Exception) {
logger!!.error("To json failed:" + ExceptionUtils.getStackTrace(e))
logger!!.error("To json failed")
logger.traceError(e)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import com.intellij.psi.*
import com.intellij.psi.util.*
import com.itangcent.common.utils.GsonUtils
import com.itangcent.common.utils.Visional
import com.itangcent.idea.utils.traceError
import com.itangcent.intellij.context.ActionContext
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.DuckTypeHelper
import com.itangcent.intellij.spring.SpringClassName
import com.itangcent.intellij.util.KV
import com.siyeh.ig.psiutils.ClassUtils
import org.apache.commons.lang3.exception.ExceptionUtils
import java.lang.reflect.Method
import java.lang.reflect.Modifier
import java.util.*
Expand All @@ -32,6 +32,9 @@ class MethodReturnInferHelper {
@Inject
val duckTypeHelper: DuckTypeHelper? = null

@Inject
val actionContext: ActionContext? = null

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

private val methodStack: Stack<Infer> = Stack()
Expand All @@ -57,7 +60,7 @@ class MethodReturnInferHelper {
}

fun inferReturn(context: PsiElement?, psiMethod: PsiMethod, caller: Any? = null, args: Array<Any?>?, option: Int = DEFAULT_OPTION): Any? {

actionContext!!.checkStatus()
if (methodStack.size < maxDeep) {
try {
var inferRet: Any?
Expand All @@ -80,7 +83,8 @@ class MethodReturnInferHelper {
return findComplexResult(GsonUtils.resolveCycle(inferRet), byType)
}
} catch (e: Exception) {
logger!!.error("infer error:" + ExceptionUtils.getStackTrace(e))
logger!!.error("infer error")
logger!!.traceError(e)
//infer failed
}
}
Expand Down Expand Up @@ -160,6 +164,7 @@ class MethodReturnInferHelper {
* method of collection(Set/List/Map...)
*/
private fun callSimpleMethod(context: PsiElement?, psiMethod: PsiMethod, caller: Any? = null, args: Array<Any?>?): Any? {
actionContext!!.checkStatus()
try {
if (psiMethod.hasModifier(JvmModifier.STATIC)) {
val unboxedArgs = unboxArgs(args)
Expand All @@ -171,7 +176,7 @@ class MethodReturnInferHelper {
if (tryCallRet != CALL_FAILED) {
return tryCallRet
}
val inferRet = tryInfe(MethodReturnInfer(psiMethod, caller, unboxedArgs, this))
val inferRet = tryInfer(MethodReturnInfer(psiMethod, caller, unboxedArgs, this))
staticMethodCache[key] = inferRet
return inferRet
}
Expand Down Expand Up @@ -245,17 +250,18 @@ class MethodReturnInferHelper {
}

private fun inferReturnUnsafely(psiMethod: PsiMethod, caller: Any? = null, args: Array<Any?>?, option: Int): Any? {
actionContext!!.checkStatus()
val realCaller = valueOf(caller)

//try quickly infer
if (allowQuickCall(option)) {
try {
return tryInfe(QuicklyMethodReturnInfer(psiMethod, this))
return tryInfer(QuicklyMethodReturnInfer(psiMethod, this))
} catch (e: Exception) {
}
}

return tryInfe(MethodReturnInfer(psiMethod, realCaller, args, this))
return tryInfer(MethodReturnInfer(psiMethod, realCaller, args, this))
}

@Suppress("UNCHECKED_CAST")
Expand All @@ -268,7 +274,8 @@ class MethodReturnInferHelper {
(comment as HashMap<Any?, Any?>)[field] = attr
}

fun tryInfe(infer: Infer): Any? {
fun tryInfer(infer: Infer): Any? {
actionContext!!.checkStatus()
//find recursive call
methodStack.filter { it.callMethod() == infer.callMethod() }
.forEach { return it.possible() }
Expand All @@ -281,6 +288,7 @@ class MethodReturnInferHelper {
}

fun tayCallStaticMethod(psiMethod: PsiMethod, args: Array<Any?>?): Any? {
actionContext!!.checkStatus()
try {
val psiCls = psiMethod.containingClass ?: return null
val cls = Class.forName(psiCls.qualifiedName)
Expand Down Expand Up @@ -337,6 +345,7 @@ class MethodReturnInferHelper {

@Suppress("UNCHECKED_CAST")
private fun callMethod(caller: Any?, method: Method, args: Array<Any?>?): Any? {
actionContext!!.checkStatus()

val argCount = args?.size ?: 0
when {
Expand Down Expand Up @@ -431,8 +440,6 @@ class MethodReturnInferHelper {
return (option and ALLOW_QUICK_CALL) != 0
}

private val loggerUnmatched: Boolean = false

val CALL_FAILED = Object()
val collection_methods = HashSet(Arrays.asList("put", "set", "add", "addAll", "putAll"))
fun init(content: PsiElement) {
Expand Down Expand Up @@ -589,6 +596,7 @@ class MethodReturnInferHelper {
if (deep >= maxObjectDeep) {
return null
}
actionContext!!.checkStatus()
val kv = KV.create<String, Any?>()
for (field in psiClass.allFields) {
val type = field.type
Expand All @@ -610,6 +618,7 @@ class MethodReturnInferHelper {
}

private fun getSimpleFields(psiType: PsiType?, context: PsiElement): Any? {
actionContext!!.checkStatus()
when {
psiType == null || psiType == PsiType.NULL -> return null
psiType is PsiPrimitiveType -> return PsiTypesUtil.getDefaultValue(psiType)
Expand Down Expand Up @@ -642,6 +651,7 @@ class MethodReturnInferHelper {
if (deep >= maxObjectDeep) {
return null
}
actionContext!!.checkStatus()
when {
psiType == null || psiType == PsiType.NULL -> return null
psiType is PsiPrimitiveType -> return PsiTypesUtil.getDefaultValue(psiType)
Expand Down Expand Up @@ -744,7 +754,9 @@ class MethodReturnInferHelper {
}

override fun setValue(value: Any?) {
this.holderValue = value
if (value != this) {
this.holderValue = value
}
}

override fun getComputedValue(): Any? {
Expand Down Expand Up @@ -823,8 +835,8 @@ class MethodReturnInferHelper {
returnVal = findComplexResult(returnVal, processExpression(returnValue))
}
}
else -> if (loggerUnmatched) {
methodReturnInferHelper.logger!!.info("no matched statement:${statement::class} - ${statement.text}")
else -> {
methodReturnInferHelper.logger!!.debug("no matched statement:${statement::class} - ${statement.text}")
}
}

Expand All @@ -849,11 +861,10 @@ class MethodReturnInferHelper {
}
} else {
variable.addLazyAction {
var processValue: Any?
if (psiElement.initializer != null) {
processValue = processExpression(psiElement.initializer!!)
val processValue: Any? = if (psiElement.initializer != null) {
processExpression(psiElement.initializer!!)
} else {
processValue = methodReturnInferHelper.getSimpleFields(psiElement.type,
methodReturnInferHelper.getSimpleFields(psiElement.type,
psiElement)
}
variable.setValue(processValue)
Expand Down Expand Up @@ -883,8 +894,46 @@ class MethodReturnInferHelper {
is PsiClassInitializer -> {
processBlock(psiElement.body)
}
else -> if (loggerUnmatched) {
methodReturnInferHelper.logger!!.info("no matched ele ${psiElement::class.qualifiedName}:${psiElement.text}")
is PsiKeyword -> {
//todo:any keyword return null??
return null
}
is PsiReferenceParameterList -> {
//todo:what does PsiReferenceParameterList mean
return null
}
is PsiWhiteSpace -> {
//ignore white space
return null
}
is PsiJavaCodeReferenceElement -> {
//todo:what does PsiJavaCodeReferenceElement mean
return null
}
is PsiExpressionList -> {
val list = ArrayList<Any?>()
for (expression in psiElement.expressions) {
list.add(processExpression(expression))
}
return list
}
is PsiArrayAccessExpression -> {
val array = processExpression(psiElement.arrayExpression) ?: return null
if (array is Array<*> && array.size > 0) {
var index = psiElement.indexExpression?.let { processExpression(it) } ?: 0
if (index !is Int) {
index = 0
}
return array[index]
}

}
is PsiLambdaExpression -> {
return psiElement.body?.let { processElement(it) }
}
else -> {
//ignore
// methodReturnInferHelper.logger!!.debug("no matched ele ${psiElement::class.qualifiedName}:${psiElement.text}")
}
}
return null
Expand Down Expand Up @@ -942,10 +991,10 @@ class MethodReturnInferHelper {
is PsiLiteralExpression -> return psiExpression.value
is PsiBinaryExpression -> return processBinaryExpression(psiExpression)
is PsiUnaryExpression -> return processUnaryExpression(psiExpression)
is PsiLambdaExpression -> return psiExpression.body?.let { processElement(it) }
else -> {
if (loggerUnmatched) {
methodReturnInferHelper.logger!!.info("no matched exp ${psiExpression::class.qualifiedName}:${psiExpression.text}")
}
//ignore
// methodReturnInferHelper.logger!!.debug("no matched exp ${psiExpression::class.qualifiedName}:${psiExpression.text}")
return null
}
}
Expand Down Expand Up @@ -1181,8 +1230,8 @@ class MethodReturnInferHelper {
return MappedVariable(targetMap, finalName)
}

fun processBlock(psicodeBlock: PsiCodeBlock) {
psicodeBlock.statements.forEach {
fun processBlock(psiCodeBlock: PsiCodeBlock) {
psiCodeBlock.statements.forEach {
try {
processStatement(it)
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import com.google.inject.name.Named
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiMethod
import com.itangcent.common.exception.ProcessCanceledException
import com.itangcent.common.exporter.ClassExporter
import com.itangcent.common.exporter.ParseHandle
import com.itangcent.common.model.Request
import com.itangcent.common.model.RequestHandle
import com.itangcent.idea.plugin.StatusRecorder
import com.itangcent.idea.plugin.Worker
import com.itangcent.idea.plugin.WorkerStatus
import com.itangcent.idea.utils.traceError
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.logger.Logger
import com.itangcent.intellij.psi.PsiClassUtils
import com.itangcent.intellij.util.ActionUtils
import com.itangcent.intellij.util.FileUtils
import org.apache.commons.lang3.exception.ExceptionUtils

class CachedClassExporter : ClassExporter, Worker {

Expand Down Expand Up @@ -131,8 +132,11 @@ class CachedClassExporter : ClassExporter, Worker {
statusRecorder.endWork()
}
}
} catch (e: ProcessCanceledException) {
//ignore cancel
} catch (e: Exception) {
logger!!.error("error to cache api info," + ExceptionUtils.getStackTrace(e))
logger!!.error("error to cache api info")
logger.traceError(e)
disabled = true

statusRecorder.newWork()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ApiCaller {

SelectedHelper.Builder()
.classHandle {
actionContext!!.checkStatus()
classExporter!!.export(it, parseHandle!!) { request -> requests.add(request) }
}
.onCompleted {
Expand Down
Loading

0 comments on commit ece4048

Please sign in to comment.