-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expression Language Injection (raw impl.)
- Loading branch information
Showing
4 changed files
with
547 additions
and
60 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/main/java/fr/adrienbrault/idea/symfony2plugin/config/xml/XmlLanguageInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlLanguageInjector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
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 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<? extends Class<? extends PsiElement>> 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); | ||
} | ||
} |
Oops, something went wrong.