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
+ }
0 commit comments