|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.config.xml; |
| 2 | + |
| 3 | +import com.intellij.lang.injection.MultiHostInjector; |
| 4 | +import com.intellij.lang.injection.MultiHostRegistrar; |
| 5 | +import com.intellij.patterns.PlatformPatterns; |
| 6 | +import com.intellij.patterns.XmlElementPattern; |
| 7 | +import com.intellij.patterns.XmlPatterns; |
| 8 | +import com.intellij.psi.ElementManipulators; |
| 9 | +import com.intellij.psi.PsiElement; |
| 10 | +import com.intellij.psi.PsiLanguageInjectionHost; |
| 11 | +import com.intellij.psi.xml.XmlText; |
| 12 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; |
| 13 | +import fr.adrienbrault.idea.symfony2plugin.expressionLanguage.ExpressionLanguage; |
| 14 | +import org.jetbrains.annotations.NotNull; |
| 15 | + |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class XmlLanguageInjector implements MultiHostInjector { |
| 20 | + |
| 21 | + @Override |
| 22 | + public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) { |
| 23 | + if (!Symfony2ProjectComponent.isEnabled(context.getProject())) { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + if (isExpressionLanguageString(context)) { |
| 28 | + registrar |
| 29 | + .startInjecting(ExpressionLanguage.INSTANCE) |
| 30 | + .addPlace(null, null, (PsiLanguageInjectionHost) context, ElementManipulators.getValueTextRange(context)) |
| 31 | + .doneInjecting(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public @NotNull List<? extends Class<? extends PsiElement>> elementsToInjectIn() { |
| 37 | + return Collections.singletonList(XmlText.class); |
| 38 | + } |
| 39 | + |
| 40 | + private boolean isExpressionLanguageString(@NotNull PsiElement element) { |
| 41 | + return PlatformPatterns.or( |
| 42 | + getExpressionArgumentPattern(), |
| 43 | + getRouteConditionPattern() |
| 44 | + ).accepts(element); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * <argument type="expression">container.get('service_id')</argument> |
| 49 | + */ |
| 50 | + private XmlElementPattern.XmlTextPattern getExpressionArgumentPattern() { |
| 51 | + return XmlPatterns |
| 52 | + .xmlText() |
| 53 | + .withParent(XmlPatterns |
| 54 | + .xmlTag() |
| 55 | + .withName("argument") |
| 56 | + .withAttributeValue("type", "expression") |
| 57 | + ) |
| 58 | + .inside( |
| 59 | + XmlHelper.getInsideTagPattern("services") |
| 60 | + ).inFile(XmlHelper.getXmlFilePattern()); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * <routes ...> |
| 65 | + * <route ...> |
| 66 | + * <condition>context.getMethod() in ['GET', 'HEAD']</condition> |
| 67 | + * </route> |
| 68 | + * </routes> |
| 69 | + */ |
| 70 | + private XmlElementPattern.XmlTextPattern getRouteConditionPattern() { |
| 71 | + return XmlPatterns |
| 72 | + .xmlText() |
| 73 | + .withParent(XmlPatterns |
| 74 | + .xmlTag() |
| 75 | + .withName("condition") |
| 76 | + .withParent(XmlPatterns |
| 77 | + .xmlTag() |
| 78 | + .withName("route") |
| 79 | + .withParent(XmlPatterns |
| 80 | + .xmlTag() |
| 81 | + .withName("routes") |
| 82 | + ) |
| 83 | + ) |
| 84 | + ) |
| 85 | + .inFile(XmlHelper.getXmlFilePattern()); |
| 86 | + } |
| 87 | +} |
0 commit comments