Skip to content

Commit e0234ff

Browse files
committed
v1.1.0, SpEL Template Support
1 parent 1c6372c commit e0234ff

10 files changed

+112
-5
lines changed

README.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,35 @@
2020

2121
### 配置详解
2222

23-
![demo](./doc/img/json.png)
23+
```json5
24+
{
25+
// 注解声明, 格式为`注解类@字段`
26+
"kim.nzxy.demo.DemoAop@value": {
27+
// 模板前缀, 为null和空字符串表示非模板, 默认为空
28+
"prefix": "#{",
29+
// 模板后缀, 为null和空字符串表示非模板, 默认为空
30+
"suffix": "}",
31+
// 对方法的扩展, 默认值如示例
32+
"method": {
33+
// 作用于方法上时, 支持方法返回值提示
34+
"result": false,
35+
// 方法返回值的SpEL变量名
36+
"resultName": "result",
37+
// 作用于方法上时, 支持方法参数提示
38+
"parameters": false,
39+
// 方法参数别名配置, 支持多个别名前缀, 如 [p0, a1, p2]分表表示第一个, 第二个, 第三个参数, 可空
40+
"parametersPrefix": [
41+
"p",
42+
"a"
43+
]
44+
},
45+
// 自定义字段, 默认为空
46+
"fields": {
47+
// 自定义变量以及类型, 支持非限定类名, 如String, Integer等, 否则需提供全限定类名
48+
"demo": "java.util.Map<String, String>",
49+
// 如果变量名为root, 则表示rootObject(可以直接)
50+
"root": "kim.nzxy.demo.DemoRoot"
51+
}
52+
}
53+
}
54+
```

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group = "kim.nzxy"
8-
version = "1.0.1"
8+
version = "1.1.0"
99

1010
tasks {
1111
buildSearchableOptions{

doc/img/json.png

-107 KB
Binary file not shown.

src/main/kotlin/kim/nzxy/spel/LySpELExtensionLanguageInject.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class LySpELExtensionLanguageInject : LanguageInjectionContributor {
1919

2020
val service = SpELConfigService.getInstance()
2121
val path = service.getFieldPath(context) ?: return null
22-
if (service.hasSpELInfo(context.project, "${path.first}@${path.second}")) {
22+
val spELInfo = service.getSpELInfo(context.project, "${path.first}@${path.second}") ?: return null
23+
if (LyUtil.isEmpty(spELInfo.prefix) && LyUtil.isEmpty(spELInfo.suffix)) {
2324
return injection
2425
}
2526
return null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package kim.nzxy.spel
2+
3+
import com.intellij.lang.injection.MultiHostInjector
4+
import com.intellij.lang.injection.MultiHostRegistrar
5+
import com.intellij.openapi.util.TextRange
6+
import com.intellij.psi.PsiElement
7+
import com.intellij.psi.PsiLanguageInjectionHost
8+
import com.intellij.psi.PsiLiteralExpression
9+
import com.intellij.spring.el.SpringELLanguage
10+
import org.jetbrains.kotlin.psi.KtStringTemplateExpression
11+
12+
13+
class LySpELLanguageMultiHostInjector : MultiHostInjector {
14+
override fun getLanguagesToInject(registrar: MultiHostRegistrar, context: PsiElement) {
15+
val service = SpELConfigService.getInstance()
16+
val path = service.getFieldPath(context) ?: return
17+
val spELInfo = service.getSpELInfo(context.project, "${path.first}@${path.second}") ?: return
18+
if (LyUtil.isEmpty(spELInfo.prefix) || LyUtil.isEmpty(spELInfo.suffix)) {
19+
return
20+
}
21+
registrar.startInjecting(SpringELLanguage.INSTANCE)
22+
val text = context.text
23+
val prefix = spELInfo.prefix!!
24+
val suffix = spELInfo.suffix!!
25+
var index = 0
26+
while (true) {
27+
val start = text.indexOf(prefix, index)
28+
if (start == -1) {
29+
break
30+
}
31+
val end = text.indexOf(suffix, start + prefix.length)
32+
if (end == -1) {
33+
break
34+
}
35+
registrar.addPlace(null, null, context as PsiLanguageInjectionHost, TextRange(start + prefix.length, end))
36+
println(context.text)
37+
println(context.textRange)
38+
index = end + suffix.length
39+
}
40+
registrar.doneInjecting()
41+
}
42+
43+
override fun elementsToInjectIn(): MutableList<out Class<out PsiElement>> {
44+
val result: MutableList<Class<out PsiElement>> = mutableListOf(PsiLiteralExpression::class.java)
45+
if (PluginChecker.getInstance().kotlin()) {
46+
result.add(KtStringTemplateExpression::class.java)
47+
}
48+
return result
49+
}
50+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package kim.nzxy.spel
2+
3+
object LyUtil {
4+
/**
5+
* 判空
6+
*/
7+
fun isEmpty(str: String?): Boolean{
8+
str ?: return true
9+
return str.isEmpty()
10+
}
11+
}

src/main/kotlin/kim/nzxy/spel/json/SpELInfo.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import com.intellij.psi.PsiFile
99
data class SpELInfo(
1010
var method: SpELInfoMethod? = SpELInfoMethod(),
1111
var fields: HashMap<String, String>? = hashMapOf(),
12-
var sourceFile: PsiFile?
12+
var sourceFile: PsiFile?,
13+
var prefix: String?,
14+
var suffix: String?
1315
)
1416

1517
data class SpELInfoMethod(

src/main/kotlin/kim/nzxy/spel/json/SpELJsonCompletionContributor.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class SpELJsonCompletionContributor : CompletionContributor() {
7575
val quoted = if (hasBody) {
7676
"\"${item.lookupString}\""
7777
} else {
78-
""""${item.lookupString}": {"method": {"result": false,"resultName": "result","parameters": false,
78+
""""${item.lookupString}": {"prefix": "", "suffix": "", "method": {"result": false,"resultName": "result","parameters": false,
7979
"parametersPrefix": ["p", "a"],},"fields": {"demo": "java.util.Map<String, String>"}},"""
8080
}
8181
val endOffSet = if (hasBody || hasQuote) range.endOffset else range.startOffset + item.lookupString.length

src/main/resources/META-INF/plugin.xml

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
<spring.el.contexts implementation="kim.nzxy.spel.LySpringElContextsExtension" order="first" id="SpEL-ly-chn"/>
2828
<languageInjectionContributor implementationClass="kim.nzxy.spel.LySpELExtensionLanguageInject"
2929
order="first" language="UAST"/>
30+
<multiHostInjector implementationClass="kim.nzxy.spel.LySpELLanguageMultiHostInjector"
31+
order="first" language="UAST"/>
3032
<localInspection language="JSON" shortName="SpELJsonKey"
3133
displayName="SpEL JSON Inspection"
3234
groupName="SpEL assistant"

src/main/resources/schemas/spel.schema.json

+10
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,15 @@
7171
}
7272
}
7373
},
74+
"properties": {
75+
"prefix": {
76+
"type": "string",
77+
"description": "Defines SpEL template prefix"
78+
},
79+
"suffix": {
80+
"type": "string",
81+
"description": "Defines SpEL template suffix"
82+
}
83+
},
7484
"additionalProperties": false
7585
}

0 commit comments

Comments
 (0)