1212import fr .adrienbrault .idea .symfony2plugin .util .MethodMatcher ;
1313import fr .adrienbrault .idea .symfony2plugin .util .PhpElementsUtil ;
1414import org .jetbrains .annotations .NotNull ;
15- import org .mozilla .javascript .ast .VariableDeclaration ;
1615
1716import java .util .Collections ;
1817import java .util .List ;
@@ -40,14 +39,20 @@ public class ParameterLanguageInjector implements MultiHostInjector {
4039 new MethodMatcher .CallToSignature ("\\ Doctrine\\ ORM\\ EntityManager" , "createQuery" ),
4140 new MethodMatcher .CallToSignature ("\\ Doctrine\\ ORM\\ Query" , "setDQL" ),
4241 };
42+ public static final String DQL_VARIABLE_NAME = "dql" ;
4343
4444 private final MethodLanguageInjection [] LANGUAGE_INJECTIONS = {
45- new MethodLanguageInjection ("CSS" , "@media all { " , " }" , CSS_SELECTOR_SIGNATURES ),
46- new MethodLanguageInjection ("XPath" , null , null , XPATH_SIGNATURES ),
47- new MethodLanguageInjection ("JSON" , null , null , JSON_SIGNATURES ),
48- new MethodLanguageInjection ("DQL" , null , null , DQL_SIGNATURES ),
45+ new MethodLanguageInjection (LANGUAGE_ID_CSS , "@media all { " , " }" , CSS_SELECTOR_SIGNATURES ),
46+ new MethodLanguageInjection (LANGUAGE_ID_XPATH , null , null , XPATH_SIGNATURES ),
47+ new MethodLanguageInjection (LANGUAGE_ID_JSON , null , null , JSON_SIGNATURES ),
48+ new MethodLanguageInjection (LANGUAGE_ID_DQL , null , null , DQL_SIGNATURES ),
4949 };
5050
51+ private static final String LANGUAGE_ID_JSON = "JSON" ;
52+ private static final String LANGUAGE_ID_XPATH = "XPath" ;
53+ private static final String LANGUAGE_ID_CSS = "CSS" ;
54+ private static final String LANGUAGE_ID_DQL = "DQL" ;
55+
5156 public ParameterLanguageInjector () {
5257 }
5358
@@ -70,11 +75,16 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
7075
7176 PsiElement parent = expr .getParent ();
7277
73- if (!(parent instanceof ParameterList ) || expr .getPrevPsiSibling () != null ) {
78+ final boolean isParameter = parent instanceof ParameterList && expr .getPrevPsiSibling () == null ; // 1st parameter
79+ final boolean isAssignment = parent instanceof AssignmentExpression ;
80+
81+ if (!isParameter && !isAssignment ) {
7482 return ;
7583 }
7684
77- parent = parent .getParent ();
85+ if (isParameter ) {
86+ parent = parent .getParent ();
87+ }
7888
7989 for (MethodLanguageInjection languageInjection : LANGUAGE_INJECTIONS ) {
8090 Language language = languageInjection .getLanguage ();
@@ -91,19 +101,19 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
91101 }
92102 }
93103 // $dql = "...";
94- // else if (parent instanceof AssignmentExpression) {
95- // if ("DQL".equals(language.getID())) {
96- // PhpPsiElement variable = ((AssignmentExpression) parent).getVariable();
97- //
98- // if (variable instanceof VariableDeclaration) {
99- // if ("dql".equals(variable.getName())) {
100- // injectLanguage(registrar, expr, language, languageInjection);
101- // return;
102- // }
103- // }
104- // }
105- // }
104+ else if (parent instanceof AssignmentExpression ) {
105+ if (LANGUAGE_ID_DQL .equals (language .getID ())) {
106+ PhpPsiElement variable = ((AssignmentExpression ) parent ).getVariable ();
107+ if (variable instanceof Variable ) {
108+ if (DQL_VARIABLE_NAME .equals (variable .getName ())) {
109+ injectLanguage (registrar , expr , language , languageInjection );
110+ return ;
111+ }
112+ }
113+ }
114+ }
106115 }
116+
107117 }
108118
109119 private void injectLanguage (@ NotNull MultiHostRegistrar registrar , @ NotNull StringLiteralExpressionImpl element , Language language , MethodLanguageInjection languageInjection ) {
0 commit comments