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

feat: Add new rule field.order.with #1047

Merged
merged 1 commit into from
Oct 22, 2023
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ plugin_version=2.6.3.212.0
kotlin.code.style=official
kotlin_version=1.8.0
junit_version=5.9.2
itangcent_intellij_version=1.5.7
itangcent_intellij_version=1.5.8
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ object ClassExportRuleKeys {
BooleanRuleMode.ANY
)

val FIELD_ORDER_WITH: RuleKey<Int> = SimpleRuleKey(
"field.order.with",
IntRuleMode
)

val CLASS_PREFIX_PATH: RuleKey<String> = SimpleRuleKey(
"class.prefix.path",
StringRuleMode.SINGLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.itangcent.idea.utils.FileSaveHelper
import com.itangcent.idea.utils.FileSelectHelper
import com.itangcent.intellij.config.rule.RuleParser
import com.itangcent.intellij.config.rule.StringRule
import com.itangcent.intellij.config.rule.parseStringRule
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.extend.rx.AutoComputer
import com.itangcent.intellij.extend.rx.mutual
Expand All @@ -40,7 +41,6 @@ import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import java.util.*
import java.util.Timer
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicLong
import javax.script.ScriptEngineManager
import javax.swing.*
Expand Down Expand Up @@ -297,12 +297,15 @@ class ScriptExecutorDialog : ContextDialog() {
is PsiClass -> {
ele
}

is PsiElement -> {
ele.getContainingClass()
}

is ExplicitElement<*> -> {
ele.containClass().psi()
}

else -> {
throw IllegalArgumentException("unknown context:$ele")
}
Expand Down Expand Up @@ -427,7 +430,7 @@ class ScriptExecutorDialog : ContextDialog() {
val ret: String?
try {
val context = scriptInfo.context
ret = parseStringRule.compute(
ret = parseStringRule(
when (context) {
is ExplicitElement<*> -> ruleParser.contextOf(context)
is PsiClass -> ruleParser.contextOf(duckTypeHelper.explicit(context))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class FieldPatternRule(
val pathPredict: (String?) -> Boolean,
val typePredict: (String?) -> Boolean,
) : BooleanRule {
override fun compute(context: RuleContext): Boolean {
override fun invoke(context: RuleContext): Boolean {
val path = context.getExt<ParseScriptContext>("fieldContext")?.path() ?: return false
val name = context.rawType() ?: return false
return pathPredict(path) && typePredict(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import javax.script.ScriptContext
import javax.script.ScriptEngine
import javax.script.SimpleScriptContext

abstract class ScriptRuleParser : RuleParser {
abstract class ScriptRuleParser : AbstractRuleParser() {

@Inject
protected val duckTypeHelper: DuckTypeHelper? = null
Expand Down Expand Up @@ -59,22 +59,28 @@ abstract class ScriptRuleParser : RuleParser {

@Inject
protected val logger: Logger? = null
override fun parseAnyRule(rule: String): AnyRule? {
return { context ->
eval(rule, context)
}
}

override fun parseBooleanRule(rule: String): BooleanRule? {
return BooleanRule.of { context ->
return@of eval(rule, context).asBool()
return { context ->
eval(rule, context).asBool()
}
}

override fun parseStringRule(rule: String): StringRule? {
return StringRule.of { context ->
return@of eval(rule, context)?.toPrettyString()
return { context ->
eval(rule, context)?.toPrettyString()
}
}

override fun parseEventRule(rule: String): EventRule? {
return EventRule.of {
return {
eval(rule, it)
Unit
}
}

Expand All @@ -83,7 +89,15 @@ abstract class ScriptRuleParser : RuleParser {
val simpleScriptContext = SimpleScriptContext()

context.exts()?.forEach {
simpleScriptContext.setAttribute(it.key, it.value, ScriptContext.ENGINE_SCOPE)
simpleScriptContext.setAttribute(
it.key,
wrap(
obj = it.value,
context = context.getPsiContext(),
shouldCopy = false
),
ScriptContext.ENGINE_SCOPE
)
}

val contextForScript: RuleContext? =
Expand Down Expand Up @@ -128,6 +142,54 @@ abstract class ScriptRuleParser : RuleParser {
}
}

private fun wrap(obj: Any?, context: PsiElement?, shouldCopy: Boolean = true): Any? {
if (obj == null) {
return null
}

if (obj is RuleContext) {
return obj
}

val className = obj::class.qualifiedName ?: return obj
if (className.startsWith("com.intellij") || className.startsWith("com.itangcent.intellij.jvm")) {
return try {
contextOf(obj, context)
} catch (e: IllegalArgumentException) {
obj
}
}

if (shouldCopy) {
when (obj) {
is Map<*, *> -> {
val copy = LinkedHashMap<Any?, Any?>()
for ((k, v) in obj.entries) {
copy[k] = wrap(v, context)
}
return copy
}

is Collection<*> -> {
val copy = LinkedList<Any?>()
for (ele in obj) {
copy.add(wrap(ele, context))
}
return copy
}

is Array<*> -> {
val copy = LinkedList<Any?>()
for (ele in obj) {
copy.add(wrap(ele, context))
}
return copy
}
}
}
return obj
}

/**
* support usages:
*
Expand All @@ -149,48 +211,9 @@ abstract class ScriptRuleParser : RuleParser {

@Suppress("UNCHECKED_CAST")
private fun <T> wrapAs(obj: Any?): T? {
return wrap(obj) as T?
return wrap(obj, getPsiContext()) as T?
}

private fun wrap(obj: Any?): Any? {
if (obj == null) {
return null
}

if (obj is RuleContext) {
return obj
}

val className = obj::class.qualifiedName ?: return obj
if (className.startsWith("com.intellij") || className.startsWith("com.itangcent")) {
return contextOf(obj, getPsiContext())
}

if (obj is Map<*, *>) {
val copy = LinkedHashMap<Any?, Any?>()
for ((k, v) in obj.entries) {
copy[k] = wrap(v)
}
return copy
}

if (obj is Collection<*>) {
val copy = LinkedList<Any?>()
for (ele in obj) {
copy.add(wrap(ele))
}
return copy
}

if (obj is Array<*>) {
val copy = LinkedList<Any?>()
for (ele in obj) {
copy.add(wrap(ele))
}
return copy
}
return obj
}

fun name(): String {
return getName() ?: ""
Expand Down Expand Up @@ -568,7 +591,7 @@ abstract class ScriptRuleParser : RuleParser {
/**
* attention:it should not be used in [json.rule.field.name]
*/
fun jsonName(): String? {
fun jsonName(): String {
return psiClassHelper!!.getJsonFieldName(psiField)
}

Expand Down Expand Up @@ -802,7 +825,7 @@ abstract class ScriptRuleParser : RuleParser {
}

@ScriptIgnore
override fun getCore(): Any? {
override fun getCore(): Any {
return explicitMethod
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.itangcent.idea.plugin.rule

import com.google.inject.Inject
import com.itangcent.intellij.config.rule.*
import com.itangcent.intellij.config.rule.Rule
import com.itangcent.intellij.config.rule.RuleContext
import com.itangcent.intellij.config.rule.RuleParser
import com.itangcent.intellij.config.rule.SimpleRuleParser
import com.itangcent.intellij.context.ActionContext
import kotlin.reflect.KClass

Expand All @@ -22,56 +25,28 @@ class SuvRuleParser : RuleParser {
return getRuleParser(SimpleRuleParser::class).contextOf(target, context)
}

override fun parseBooleanRule(rule: String): BooleanRule? {
override fun parseRule(rule: String, targetType: KClass<*>): Rule<Any>? {
return when {
rule.isBlank() -> null
rule.startsWith(JS_PREFIX) -> getRuleParser(JsRuleParser::class).parseBooleanRule(
rule.startsWith(JS_PREFIX) -> getRuleParser(JsRuleParser::class).parseRule(
rule.removePrefix(
JS_PREFIX
)
), targetType
)

rule.startsWith(GROOVY_PREFIX) -> getRuleParser(GroovyRuleParser::class).parseBooleanRule(
rule.startsWith(GROOVY_PREFIX) -> getRuleParser(GroovyRuleParser::class).parseRule(
rule.removePrefix(
GROOVY_PREFIX
)
), targetType
)

rule.startsWith(FIELD_PREFIX) -> getRuleParser(FieldPatternRuleParser::class).parseBooleanRule(
rule.startsWith(FIELD_PREFIX) -> getRuleParser(FieldPatternRuleParser::class).parseRule(
rule.removePrefix(
FIELD_PREFIX
)
), targetType
)

else -> getRuleParser(SimpleRuleParser::class).parseBooleanRule(rule)
}
}

override fun parseStringRule(rule: String): StringRule? {
return when {
rule.isBlank() -> null
rule.startsWith(JS_PREFIX) -> getRuleParser(JsRuleParser::class).parseStringRule(rule.removePrefix(JS_PREFIX))
rule.startsWith(GROOVY_PREFIX) -> getRuleParser(GroovyRuleParser::class).parseStringRule(
rule.removePrefix(
GROOVY_PREFIX
)
)

else -> getRuleParser(SimpleRuleParser::class).parseStringRule(rule)
}
}

override fun parseEventRule(rule: String): EventRule? {
return when {
rule.isBlank() -> null
rule.startsWith(JS_PREFIX) -> getRuleParser(JsRuleParser::class).parseEventRule(rule.removePrefix(JS_PREFIX))
rule.startsWith(GROOVY_PREFIX) -> getRuleParser(GroovyRuleParser::class).parseEventRule(
rule.removePrefix(
GROOVY_PREFIX
)
)

else -> getRuleParser(SimpleRuleParser::class).parseEventRule(rule)
else -> getRuleParser(SimpleRuleParser::class).parseRule(rule, targetType)
}
}

Expand Down
Loading
Loading