|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.config.yaml; |
| 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.patterns.PlatformPatterns; |
| 7 | +import com.intellij.psi.ElementManipulators; |
| 8 | +import com.intellij.psi.PsiElement; |
| 9 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; |
| 10 | +import fr.adrienbrault.idea.symfony2plugin.expressionLanguage.ExpressionLanguage; |
| 11 | +import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | +import org.jetbrains.yaml.psi.YAMLPsiElement; |
| 14 | +import org.jetbrains.yaml.psi.YAMLQuotedText; |
| 15 | + |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +public class YamlLanguageInjector implements MultiHostInjector { |
| 20 | + |
| 21 | + private static final String EXPRESSION_LANGUAGE_PREFIX = "@="; |
| 22 | + |
| 23 | + @Override |
| 24 | + public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) { |
| 25 | + if (!Symfony2ProjectComponent.isEnabled(context.getProject())) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + if (!(context instanceof YAMLQuotedText)) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + var file = context.getContainingFile(); |
| 34 | + var element = (YAMLQuotedText) context; |
| 35 | + var value = element.getTextValue(); |
| 36 | + |
| 37 | + if (YamlHelper.isServicesFile(file) && isExpressionLanguageString(value) && isExpressionLanguageStringAllowed(element)) { |
| 38 | + registrar |
| 39 | + .startInjecting(ExpressionLanguage.INSTANCE) |
| 40 | + .addPlace(null, null, element, getExpressionLanguageTextRange(value)) |
| 41 | + .doneInjecting(); |
| 42 | + } else if (YamlHelper.isRoutingFile(file) && isInsideRouteConditionKey(element)) { |
| 43 | + registrar |
| 44 | + .startInjecting(ExpressionLanguage.INSTANCE) |
| 45 | + .addPlace(null, null, element, ElementManipulators.getValueTextRange(element)) |
| 46 | + .doneInjecting(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @NotNull |
| 51 | + @Override |
| 52 | + public List<? extends Class<? extends PsiElement>> elementsToInjectIn() { |
| 53 | + return Collections.singletonList(YAMLQuotedText.class); |
| 54 | + } |
| 55 | + |
| 56 | + private boolean isExpressionLanguageString(@NotNull String str) { |
| 57 | + return str.startsWith(EXPRESSION_LANGUAGE_PREFIX) && str.length() > EXPRESSION_LANGUAGE_PREFIX.length(); |
| 58 | + } |
| 59 | + |
| 60 | + private boolean isExpressionLanguageStringAllowed(@NotNull YAMLPsiElement element) { |
| 61 | + return PlatformPatterns.and( |
| 62 | + YamlElementPatternHelper.getInsideKeyValue("services"), |
| 63 | + YamlElementPatternHelper.getInsideKeyValue("arguments", "properties", "calls", "configurator") |
| 64 | + ).accepts(element); |
| 65 | + } |
| 66 | + |
| 67 | + @NotNull |
| 68 | + private TextRange getExpressionLanguageTextRange(@NotNull String str) { |
| 69 | + return new TextRange(EXPRESSION_LANGUAGE_PREFIX.length() + 1, str.length() + 1); |
| 70 | + } |
| 71 | + |
| 72 | + private boolean isInsideRouteConditionKey(@NotNull YAMLPsiElement element) { |
| 73 | + return YamlElementPatternHelper.getInsideKeyValue("condition").accepts(element); |
| 74 | + } |
| 75 | +} |
0 commit comments