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

new rule tool: helper #242

Merged
merged 1 commit into from
Feb 25, 2020
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
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.2.7-SNAPSHOT') {
compile('com.itangcent:intellij-idea:0.2.9-SNAPSHOT') {
exclude group: 'com.google.inject'
exclude group: 'com.google.code.gson'
}

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

compile('com.itangcent:intellij-scala-support:0.2.7-SNAPSHOT') {
compile('com.itangcent:intellij-scala-support:0.2.9-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 @@ -270,7 +270,7 @@ abstract class AbstractRequestClassExporter : ClassExporter, Worker {
var returnType = method.returnType
val returnTypeByRule = ruleComputer!!.computer(ClassExportRuleKeys.METHOD_RETURN, method)
if (!returnTypeByRule.isNullOrBlank()) {
val resolvedReturnType = duckTypeHelper!!.buildPsiType(returnTypeByRule!!.trim(), method)
val resolvedReturnType = duckTypeHelper!!.findType(returnTypeByRule!!.trim(), method)
if (resolvedReturnType != null) {
returnType = resolvedReturnType
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ class DebugDialog : JDialog() {
}

override fun demoCode(): String {
return "var separator = tool.repeat(\"-\", 35) + \"\\n\"\nvar sb = \"\"\nsb += \"debug `tool`:\\n\"\nsb += tool.debug(tool)\nsb += separator\nsb += \"debug `it`:\\n\"\nsb += tool.debug(it)\nsb += separator\nsb += \"debug `regex`:\\n\"\nsb += tool.debug(regex)\nsb += separator\nsb += \"debug `logger`:\\n\"\nsb += tool.debug(logger)\nsb"
return "var separator = tool.repeat(\"-\", 35) + \"\\n\\n\"\nvar sb = \"\"\nsb += \"debug `tool`:\\n\"\nsb += tool.debug(tool)\nsb += separator\nsb += \"debug `it`:\\n\"\nsb += tool.debug(it)\nsb += separator\nsb += \"debug `regex`:\\n\"\nsb += tool.debug(regex)\nsb += separator\nsb += \"debug `logger`:\\n\"\nsb += tool.debug(logger)\nsb += separator\nsb += \"debug `helper`:\\n\"\nsb += tool.debug(helper)\nsb"
}
}

Expand All @@ -370,7 +370,7 @@ class DebugDialog : JDialog() {
}

override fun demoCode(): String {
return "def separator = tool.repeat(\"-\", 35) + \"\\n\"\ndef sb = \"\"\nsb += \"debug `tool`:\\n\"\nsb += tool.debug(tool)\nsb += separator\nsb += \"debug `it`:\\n\"\nsb += tool.debug(it)\nsb += separator\nsb += \"debug `regex`:\\n\"\nsb += tool.debug(regex)\nsb += separator\nsb += \"debug `logger`:\\n\"\nsb += tool.debug(logger)\nreturn sb"
return "def separator = tool.repeat(\"-\", 35) + \"\\n\\n\"\ndef sb = \"\"\nsb += \"debug `tool`:\\n\"\nsb += tool.debug(tool)\nsb += separator\nsb += \"debug `it`:\\n\"\nsb += tool.debug(it)\nsb += separator\nsb += \"debug `regex`:\\n\"\nsb += tool.debug(regex)\nsb += separator\nsb += \"debug `logger`:\\n\"\nsb += tool.debug(logger)\nsb += separator\nsb += \"debug `helper`:\\n\"\nsb += tool.debug(helper)\nreturn sb"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ abstract class ScriptRuleParser : RuleParser {
val contextForScript: RuleContext? = (context as? BaseScriptRuleContext) ?: contextOf(
context.getCore() ?: context.getResource()!!, context.getResource()!!)
simpleScriptContext.setAttribute("it", contextForScript, ScriptContext.ENGINE_SCOPE)
initScriptContext(simpleScriptContext)
initScriptContext(simpleScriptContext, context)
getScriptEngine().eval(ruleScript, simpleScriptContext)
} catch (e: UnsupportedScriptException) {
logger?.error("unsupported script type:${e.getType()},script:$ruleScript")
Expand All @@ -67,7 +67,7 @@ abstract class ScriptRuleParser : RuleParser {

protected abstract fun getScriptEngine(): ScriptEngine

protected open fun initScriptContext(scriptContext: ScriptContext) {
protected open fun initScriptContext(scriptContext: ScriptContext, context: RuleContext) {

}

Expand Down Expand Up @@ -498,6 +498,10 @@ abstract class ScriptRuleParser : RuleParser {
return "class"
}

override fun getPsiContext(): PsiElement? {
return getResource() ?: jvmClassHelper!!.resolveClassInType(psiType)
}

private var duckType: DuckType? = null

override fun getName(): String? {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.itangcent.idea.plugin.rule

import com.intellij.psi.PsiElement
import com.itangcent.annotation.script.ScriptTypeName
import com.itangcent.idea.plugin.utils.RegexUtils
import com.itangcent.intellij.config.rule.RuleContext
import javax.script.*

abstract class StandardJdkRuleParser : ScriptRuleParser() {
Expand Down Expand Up @@ -35,10 +38,18 @@ abstract class StandardJdkRuleParser : ScriptRuleParser() {
scriptEngine.setBindings(SimpleBindings(toolBindings), ScriptContext.GLOBAL_SCOPE)
}

override fun initScriptContext(scriptContext: ScriptContext) {
override fun initScriptContext(scriptContext: ScriptContext, context: RuleContext) {
val engineBindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE)
engineBindings.putAll(toolBindings)
engineBindings["logger"] = logger
engineBindings["helper"] = Helper(context.getPsiContext())
}

@ScriptTypeName("helper")
inner class Helper(val context: PsiElement?) {
fun findClass(canonicalText: String): ScriptPsiTypeContext? {
return context?.let { duckTypeHelper!!.findType(canonicalText, it)?.let { type -> ScriptPsiTypeContext(type) } }
}
}

companion object {
Expand Down