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 26, 2021
1 parent 9129feb commit 78b7cd8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
}

if (!(context instanceof YAMLQuotedText)) {
return ;
return;
}

var file = context.getContainingFile();
Expand Down Expand Up @@ -70,6 +70,6 @@ private TextRange getExpressionLanguageTextRange(@NotNull String str) {
}

private boolean isInsideRouteConditionKey(@NotNull YAMLPsiElement element) {
return YamlElementPatternHelper.getInsideKeyValue("condition").accepts(element);
return YamlElementPatternHelper.getInsideKeyValue("condition").accepts(element);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.intellij.lang.injection.MultiHostRegistrar;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiLanguageInjectionHost;
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
import com.jetbrains.php.lang.psi.elements.*;
import com.jetbrains.php.lang.psi.elements.impl.ParameterListImpl;
import com.jetbrains.php.lang.psi.elements.impl.StringLiteralExpressionImpl;
import de.espend.idea.php.annotation.util.AnnotationUtil;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -61,7 +63,13 @@ public class ParameterLanguageInjector implements MultiHostInjector {
new AttributeLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Entity", "expr", 1),
new AttributeLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\ParamConverter", "expr", 1),
new AttributeLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Route", "condition", 9),
new FunctionCallArgumentLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\expr", "expression", 0)
new FunctionCallArgumentLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\expr", "expression", 0),
new AnnotationLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Symfony\\Component\\Routing\\Annotation\\Route", "condition"),
new AnnotationLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Security", "expression"),
new AnnotationLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Cache", "lastModified"),
new AnnotationLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Cache", "Etag"),
new AnnotationLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Entity", "expr"),
new AnnotationLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\ParamConverter", "expr"),
};

public static final String LANGUAGE_ID_CSS = "CSS";
Expand All @@ -86,35 +94,24 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
if (!(element instanceof StringLiteralExpression) || !((PsiLanguageInjectionHost) element).isValidHost()) {
return;
}

if (!Symfony2ProjectComponent.isEnabled(element.getProject())) {
return;
}

final StringLiteralExpressionImpl expr = (StringLiteralExpressionImpl) element;

PsiElement parent = expr.getParent();

final boolean isParameter = parent instanceof ParameterList /* && expr.getPrevPsiSibling() == null */; // 1st parameter
final boolean isAssignment = parent instanceof AssignmentExpression;

if (!isParameter && !isAssignment) {
return;
}

if (isParameter) {
parent = parent.getParent();
}

for (LanguageInjection languageInjection : LANGUAGE_INJECTIONS) {
if (languageInjection.accepts(expr)) {
injectLanguage(registrar, expr, languageInjection);
return;
}

if (parent instanceof AssignmentExpression) {
Language language = languageInjection.getLanguage();
if (expr.getParent() instanceof AssignmentExpression) {
var parent = expr.getParent();
var language = languageInjection.getLanguage();
if (language != null && LANGUAGE_ID_DQL.equals(language.getID())) {
PhpPsiElement variable = ((AssignmentExpression) parent).getVariable();
var variable = ((AssignmentExpression) parent).getVariable();
if (variable instanceof Variable) {
if (DQL_VARIABLE_NAME.equals(variable.getName())) {
injectLanguage(registrar, expr, languageInjection);
Expand Down Expand Up @@ -404,6 +401,39 @@ public boolean accepts(@NotNull StringLiteralExpression element) {
}
}

public static class AnnotationLanguageInjection extends LanguageInjection {
@NotNull
private final String classFQN;
@NotNull
private final String propertyName;

public AnnotationLanguageInjection(@NotNull String languageId, @NotNull String classFQN, @NotNull String propertyName) {
this(languageId, null, null, classFQN, propertyName);
}

public AnnotationLanguageInjection(@NotNull String languageId, @Nullable String prefix, @Nullable String suffix, @NotNull String classFQN, @NotNull String propertyName) {
super(languageId, prefix, suffix);
this.classFQN = classFQN;
this.propertyName = propertyName;
}

@Override
public boolean accepts(@NotNull StringLiteralExpression element) {
if (element.getParent() == null || !(element.getParent().getParent() instanceof PhpDocTag)) {
return false;
}

var phpDocTag = (PhpDocTag) element.getParent().getParent();

var annotationClass = AnnotationUtil.getAnnotationReference(phpDocTag);
if (annotationClass != null && annotationClass.getFQN().equals(classFQN)) {
return element.equals(AnnotationUtil.getPropertyValueAsPsiElement(phpDocTag, propertyName));
}

return false;
}
}

private static class NewExpressionLanguageInjection extends LanguageInjection {
@NotNull
private final String classFQN;
Expand Down

0 comments on commit 78b7cd8

Please sign in to comment.