Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 78b7cd8

Browse files
committed
Expression Language Injection (raw impl.)
1 parent 9129feb commit 78b7cd8

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

‎src/main/java/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlLanguageInjector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
2727
}
2828

2929
if (!(context instanceof YAMLQuotedText)) {
30-
return;
30+
return;
3131
}
3232

3333
var file = context.getContainingFile();
@@ -70,6 +70,6 @@ private TextRange getExpressionLanguageTextRange(@NotNull String str) {
7070
}
7171

7272
private boolean isInsideRouteConditionKey(@NotNull YAMLPsiElement element) {
73-
return YamlElementPatternHelper.getInsideKeyValue("condition").accepts(element);
73+
return YamlElementPatternHelper.getInsideKeyValue("condition").accepts(element);
7474
}
7575
}

‎src/main/java/fr/adrienbrault/idea/symfony2plugin/lang/ParameterLanguageInjector.java

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import com.intellij.lang.injection.MultiHostRegistrar;
66
import com.intellij.psi.PsiElement;
77
import com.intellij.psi.PsiLanguageInjectionHost;
8+
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
89
import com.jetbrains.php.lang.psi.elements.*;
910
import com.jetbrains.php.lang.psi.elements.impl.ParameterListImpl;
1011
import com.jetbrains.php.lang.psi.elements.impl.StringLiteralExpressionImpl;
12+
import de.espend.idea.php.annotation.util.AnnotationUtil;
1113
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
1214
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
1315
import org.jetbrains.annotations.NotNull;
@@ -61,7 +63,13 @@ public class ParameterLanguageInjector implements MultiHostInjector {
6163
new AttributeLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Entity", "expr", 1),
6264
new AttributeLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\ParamConverter", "expr", 1),
6365
new AttributeLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Route", "condition", 9),
64-
new FunctionCallArgumentLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\expr", "expression", 0)
66+
new FunctionCallArgumentLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\expr", "expression", 0),
67+
new AnnotationLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Symfony\\Component\\Routing\\Annotation\\Route", "condition"),
68+
new AnnotationLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Security", "expression"),
69+
new AnnotationLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Cache", "lastModified"),
70+
new AnnotationLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Cache", "Etag"),
71+
new AnnotationLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Entity", "expr"),
72+
new AnnotationLanguageInjection(LANGUAGE_ID_EXPRESSION_LANGUAGE, "\\Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\ParamConverter", "expr"),
6573
};
6674

6775
public static final String LANGUAGE_ID_CSS = "CSS";
@@ -86,35 +94,24 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
8694
if (!(element instanceof StringLiteralExpression) || !((PsiLanguageInjectionHost) element).isValidHost()) {
8795
return;
8896
}
97+
8998
if (!Symfony2ProjectComponent.isEnabled(element.getProject())) {
9099
return;
91100
}
92101

93102
final StringLiteralExpressionImpl expr = (StringLiteralExpressionImpl) element;
94103

95-
PsiElement parent = expr.getParent();
96-
97-
final boolean isParameter = parent instanceof ParameterList /* && expr.getPrevPsiSibling() == null */; // 1st parameter
98-
final boolean isAssignment = parent instanceof AssignmentExpression;
99-
100-
if (!isParameter && !isAssignment) {
101-
return;
102-
}
103-
104-
if (isParameter) {
105-
parent = parent.getParent();
106-
}
107-
108104
for (LanguageInjection languageInjection : LANGUAGE_INJECTIONS) {
109105
if (languageInjection.accepts(expr)) {
110106
injectLanguage(registrar, expr, languageInjection);
111107
return;
112108
}
113109

114-
if (parent instanceof AssignmentExpression) {
115-
Language language = languageInjection.getLanguage();
110+
if (expr.getParent() instanceof AssignmentExpression) {
111+
var parent = expr.getParent();
112+
var language = languageInjection.getLanguage();
116113
if (language != null && LANGUAGE_ID_DQL.equals(language.getID())) {
117-
PhpPsiElement variable = ((AssignmentExpression) parent).getVariable();
114+
var variable = ((AssignmentExpression) parent).getVariable();
118115
if (variable instanceof Variable) {
119116
if (DQL_VARIABLE_NAME.equals(variable.getName())) {
120117
injectLanguage(registrar, expr, languageInjection);
@@ -404,6 +401,39 @@ public boolean accepts(@NotNull StringLiteralExpression element) {
404401
}
405402
}
406403

404+
public static class AnnotationLanguageInjection extends LanguageInjection {
405+
@NotNull
406+
private final String classFQN;
407+
@NotNull
408+
private final String propertyName;
409+
410+
public AnnotationLanguageInjection(@NotNull String languageId, @NotNull String classFQN, @NotNull String propertyName) {
411+
this(languageId, null, null, classFQN, propertyName);
412+
}
413+
414+
public AnnotationLanguageInjection(@NotNull String languageId, @Nullable String prefix, @Nullable String suffix, @NotNull String classFQN, @NotNull String propertyName) {
415+
super(languageId, prefix, suffix);
416+
this.classFQN = classFQN;
417+
this.propertyName = propertyName;
418+
}
419+
420+
@Override
421+
public boolean accepts(@NotNull StringLiteralExpression element) {
422+
if (element.getParent() == null || !(element.getParent().getParent() instanceof PhpDocTag)) {
423+
return false;
424+
}
425+
426+
var phpDocTag = (PhpDocTag) element.getParent().getParent();
427+
428+
var annotationClass = AnnotationUtil.getAnnotationReference(phpDocTag);
429+
if (annotationClass != null && annotationClass.getFQN().equals(classFQN)) {
430+
return element.equals(AnnotationUtil.getPropertyValueAsPsiElement(phpDocTag, propertyName));
431+
}
432+
433+
return false;
434+
}
435+
}
436+
407437
private static class NewExpressionLanguageInjection extends LanguageInjection {
408438
@NotNull
409439
private final String classFQN;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /