|
| 1 | +package com.axellience.vuegwtplugin; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.jetbrains.annotations.NotNull; |
| 7 | + |
| 8 | +import com.intellij.lang.injection.MultiHostInjector; |
| 9 | +import com.intellij.lang.injection.MultiHostRegistrar; |
| 10 | +import com.intellij.lang.java.JavaLanguage; |
| 11 | +import com.intellij.openapi.editor.impl.TextRangeInterval; |
| 12 | +import com.intellij.psi.PsiElement; |
| 13 | +import com.intellij.psi.PsiFile; |
| 14 | +import com.intellij.psi.PsiLanguageInjectionHost; |
| 15 | +import com.intellij.psi.impl.source.PsiJavaFileImpl; |
| 16 | +import com.intellij.psi.xml.XmlAttribute; |
| 17 | +import com.intellij.psi.xml.XmlAttributeValue; |
| 18 | + |
| 19 | +public class VueCustomInjector implements MultiHostInjector |
| 20 | +{ |
| 21 | + @Override |
| 22 | + public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, |
| 23 | + @NotNull PsiElement host) |
| 24 | + { |
| 25 | + if (!(host.getParent() instanceof XmlAttribute)) |
| 26 | + return; |
| 27 | + |
| 28 | + XmlAttribute attribute = (XmlAttribute) host.getParent(); |
| 29 | + String attr = attribute.getName(); |
| 30 | + if (attr.startsWith(":") || attr.startsWith("@") || attr.startsWith("v-")) |
| 31 | + { |
| 32 | + registrar.startInjecting(JavaLanguage.INSTANCE); |
| 33 | + |
| 34 | + PsiFile templateFile = host.getContainingFile(); |
| 35 | + String componentName = templateFile |
| 36 | + .getName() |
| 37 | + .substring(0, templateFile.getName().length() - ".html".length()); |
| 38 | + |
| 39 | + String javaClassFileName = componentName + ".java"; |
| 40 | + |
| 41 | + if (templateFile.getContainingDirectory() == null) |
| 42 | + return; |
| 43 | + |
| 44 | + PsiJavaFileImpl componentFile = null; |
| 45 | + for (PsiFile psiFile : templateFile.getContainingDirectory().getFiles()) |
| 46 | + { |
| 47 | + if (psiFile.getName().equals(javaClassFileName)) |
| 48 | + { |
| 49 | + componentFile = (PsiJavaFileImpl) psiFile; |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (componentFile == null) |
| 55 | + return; |
| 56 | + |
| 57 | + String templateExpressionMethod; |
| 58 | + if (attr.startsWith("@")) |
| 59 | + templateExpressionMethod = "public void templateExpression() {\n\t\t"; |
| 60 | + else |
| 61 | + templateExpressionMethod = "public Object templateExpression() {\n\t\treturn "; |
| 62 | + |
| 63 | + String expressionStart = "package " |
| 64 | + + componentFile.getPackageName() |
| 65 | + + ";\n" |
| 66 | + + "class " |
| 67 | + + componentName |
| 68 | + + "TemplateImpl extends " |
| 69 | + + componentName |
| 70 | + + "JsType {\n" |
| 71 | + + templateExpressionMethod; |
| 72 | + |
| 73 | + String expressionEnd = ";\n\t}\n}"; |
| 74 | + |
| 75 | + registrar.addPlace(expressionStart, |
| 76 | + expressionEnd, |
| 77 | + (PsiLanguageInjectionHost) host, |
| 78 | + new TextRangeInterval(1, host.getTextLength() - 1)); |
| 79 | + registrar.doneInjecting(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @NotNull |
| 84 | + @Override |
| 85 | + public List<? extends Class<? extends PsiElement>> elementsToInjectIn() |
| 86 | + { |
| 87 | + return Collections.singletonList(XmlAttributeValue.class); |
| 88 | + } |
| 89 | +} |
0 commit comments