From 99b576008c15264b695602907577f8ff3276eec1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Sat, 19 Jun 2021 15:43:36 +0200 Subject: [PATCH] Expression Language Injection (raw impl.) --- .../config/yaml/YamlLanguageInjector.java | 76 +++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 2 + 2 files changed, 78 insertions(+) create mode 100644 src/main/java/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlLanguageInjector.java diff --git a/src/main/java/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlLanguageInjector.java b/src/main/java/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlLanguageInjector.java new file mode 100644 index 000000000..a430e8bc9 --- /dev/null +++ b/src/main/java/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlLanguageInjector.java @@ -0,0 +1,76 @@ +package fr.adrienbrault.idea.symfony2plugin.config.yaml; + +import com.intellij.lang.injection.MultiHostInjector; +import com.intellij.lang.injection.MultiHostRegistrar; +import com.intellij.openapi.util.TextRange; +import com.intellij.patterns.PlatformPatterns; +import com.intellij.psi.ElementManipulators; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent; +import fr.adrienbrault.idea.symfony2plugin.expressionLanguage.ExpressionLanguage; +import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.yaml.psi.YAMLPsiElement; +import org.jetbrains.yaml.psi.YAMLQuotedText; + +import java.util.Collections; +import java.util.List; + +public class YamlLanguageInjector implements MultiHostInjector { + + private static final String EXPRESSION_LANGUAGE_PREFIX = "@="; + + @Override + public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) { + if (!Symfony2ProjectComponent.isEnabled(context.getProject())) { + return; + } + + if (!(context instanceof YAMLQuotedText)) { + return ; + } + + var file = context.getContainingFile(); + var element = (YAMLQuotedText) context; + var value = element.getTextValue(); + + if (YamlHelper.isServicesFile(file) && isExpressionLanguageString(value) && isExpressionLanguageStringAllowed(element)) { + registrar + .startInjecting(ExpressionLanguage.INSTANCE) + .addPlace(null, null, element, getExpressionLanguageTextRange(value)) + .doneInjecting(); + } else if (YamlHelper.isRoutingFile(file) && isInsideRouteConditionKey(element)) { + registrar + .startInjecting(ExpressionLanguage.INSTANCE) + .addPlace(null, null, element, ElementManipulators.getValueTextRange(element)) + .doneInjecting(); + } + } + + @NotNull + @Override + public List> elementsToInjectIn() { + return Collections.singletonList(YAMLQuotedText.class); + } + + private boolean isExpressionLanguageString(@NotNull String str) { + return str.startsWith(EXPRESSION_LANGUAGE_PREFIX) && str.length() > EXPRESSION_LANGUAGE_PREFIX.length(); + } + + private boolean isExpressionLanguageStringAllowed(@NotNull YAMLPsiElement element) { + return PlatformPatterns.and( + YamlElementPatternHelper.getInsideKeyValue("services"), + YamlElementPatternHelper.getInsideKeyValue("arguments", "properties", "calls", "configurator") + ).accepts(element); + } + + @NotNull + private TextRange getExpressionLanguageTextRange(@NotNull String str) { + return new TextRange(EXPRESSION_LANGUAGE_PREFIX.length() + 1, str.length() + 1); + } + + private boolean isInsideRouteConditionKey(@NotNull YAMLPsiElement element) { + return YamlElementPatternHelper.getInsideKeyValue("condition").accepts(element); + } +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 2a2fff93a..76609d035 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -252,6 +252,8 @@ + +