Skip to content

Commit

Permalink
Expression Language Injection (raw impl.)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwojs committed Jun 20, 2021
1 parent 99b5760 commit 9129feb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package fr.adrienbrault.idea.symfony2plugin.config.xml;

import com.intellij.lang.injection.MultiHostInjector;
import com.intellij.lang.injection.MultiHostRegistrar;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.patterns.XmlElementPattern;
import com.intellij.patterns.XmlPatterns;
import com.intellij.psi.ElementManipulators;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiLanguageInjectionHost;
import com.intellij.psi.xml.XmlText;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.expressionLanguage.ExpressionLanguage;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

public class XmlLanguageInjector implements MultiHostInjector {

@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
if (!Symfony2ProjectComponent.isEnabled(context.getProject())) {
return;
}

if (isExpressionLanguageString(context)) {
registrar
.startInjecting(ExpressionLanguage.INSTANCE)
.addPlace(null, null, (PsiLanguageInjectionHost) context, ElementManipulators.getValueTextRange(context))
.doneInjecting();
}
}

@Override
public @NotNull List<? extends Class<? extends PsiElement>> elementsToInjectIn() {
return Collections.singletonList(XmlText.class);
}

private boolean isExpressionLanguageString(@NotNull PsiElement element) {
return PlatformPatterns.or(
getExpressionArgumentPattern(),
getRouteConditionPattern()
).accepts(element);
}

/**
* <argument type="expression">container.get('service_id')</argument>
*/
private XmlElementPattern.XmlTextPattern getExpressionArgumentPattern() {
return XmlPatterns
.xmlText()
.withParent(XmlPatterns
.xmlTag()
.withName("argument")
.withAttributeValue("type", "expression")
)
.inside(
XmlHelper.getInsideTagPattern("services")
).inFile(XmlHelper.getXmlFilePattern());
}

/**
* <routes ...>
* <route ...>
* <condition>context.getMethod() in ['GET', 'HEAD']</condition>
* </route>
* </routes>
*/
private XmlElementPattern.XmlTextPattern getRouteConditionPattern() {
return XmlPatterns
.xmlText()
.withParent(XmlPatterns
.xmlTag()
.withName("condition")
.withParent(XmlPatterns
.xmlTag()
.withName("route")
.withParent(XmlPatterns
.xmlTag()
.withName("routes")
)
)
)
.inFile(XmlHelper.getXmlFilePattern());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
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;
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@

<multiHostInjector implementation="fr.adrienbrault.idea.symfony2plugin.lang.ParameterLanguageInjector"/>
<multiHostInjector implementation="fr.adrienbrault.idea.symfony2plugin.config.yaml.YamlLanguageInjector" />
<multiHostInjector implementation="fr.adrienbrault.idea.symfony2plugin.config.xml.XmlLanguageInjector" />

<iconProvider implementation="fr.adrienbrault.idea.symfony2plugin.twig.icon.TwigIconProvider"/>

Expand Down

0 comments on commit 9129feb

Please sign in to comment.