3
3
import com .intellij .lang .Language ;
4
4
import com .intellij .lang .injection .MultiHostInjector ;
5
5
import com .intellij .lang .injection .MultiHostRegistrar ;
6
- import com .intellij .openapi .util .TextRange ;
7
6
import com .intellij .psi .PsiElement ;
8
7
import com .intellij .psi .PsiLanguageInjectionHost ;
9
8
import com .jetbrains .php .lang .psi .elements .*;
12
11
import fr .adrienbrault .idea .symfony2plugin .util .MethodMatcher ;
13
12
import fr .adrienbrault .idea .symfony2plugin .util .PhpElementsUtil ;
14
13
import org .jetbrains .annotations .NotNull ;
14
+ import org .jetbrains .annotations .Nullable ;
15
15
16
16
import java .util .Collections ;
17
17
import java .util .List ;
@@ -41,10 +41,10 @@ public class ParameterLanguageInjector implements MultiHostInjector {
41
41
};
42
42
43
43
private final MethodLanguageInjection [] LANGUAGE_INJECTIONS = {
44
- new MethodLanguageInjection (LANGUAGE_ID_CSS , "@media all { " , " }" , CSS_SELECTOR_SIGNATURES ),
45
- new MethodLanguageInjection (LANGUAGE_ID_XPATH , null , null , XPATH_SIGNATURES ),
46
- new MethodLanguageInjection (LANGUAGE_ID_JSON , null , null , JSON_SIGNATURES ),
47
- new MethodLanguageInjection (LANGUAGE_ID_DQL , null , null , DQL_SIGNATURES ),
44
+ new MethodLanguageInjection (LANGUAGE_ID_CSS , "@media all { " , " }" , CSS_SELECTOR_SIGNATURES ),
45
+ new MethodLanguageInjection (LANGUAGE_ID_XPATH , null , null , XPATH_SIGNATURES ),
46
+ new MethodLanguageInjection (LANGUAGE_ID_JSON , null , null , JSON_SIGNATURES ),
47
+ new MethodLanguageInjection (LANGUAGE_ID_DQL , null , null , DQL_SIGNATURES ),
48
48
};
49
49
50
50
public static final String LANGUAGE_ID_CSS = "CSS" ;
@@ -88,26 +88,23 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
88
88
}
89
89
90
90
for (MethodLanguageInjection languageInjection : LANGUAGE_INJECTIONS ) {
91
- Language language = languageInjection .getLanguage ();
92
- if (language == null ) {
93
- continue ;
94
- }
95
91
// $crawler->filter('...')
96
92
// $em->createQuery('...')
97
93
// JsonResponse::fromJsonString('...')
98
94
if (parent instanceof MethodReference ) {
99
95
if (PhpElementsUtil .isMethodReferenceInstanceOf ((MethodReference ) parent , languageInjection .getSignatures ())) {
100
- injectLanguage (registrar , expr , language , languageInjection );
96
+ injectLanguage (registrar , expr , languageInjection );
101
97
return ;
102
98
}
103
99
}
104
100
// $dql = "...";
105
101
else if (parent instanceof AssignmentExpression ) {
106
- if (LANGUAGE_ID_DQL .equals (language .getID ())) {
102
+ Language language = languageInjection .getLanguage ();
103
+ if (language != null && LANGUAGE_ID_DQL .equals (language .getID ())) {
107
104
PhpPsiElement variable = ((AssignmentExpression ) parent ).getVariable ();
108
105
if (variable instanceof Variable ) {
109
106
if (DQL_VARIABLE_NAME .equals (variable .getName ())) {
110
- injectLanguage (registrar , expr , language , languageInjection );
107
+ injectLanguage (registrar , expr , languageInjection );
111
108
return ;
112
109
}
113
110
}
@@ -117,28 +114,33 @@ else if (parent instanceof AssignmentExpression) {
117
114
118
115
}
119
116
120
- private void injectLanguage (@ NotNull MultiHostRegistrar registrar , @ NotNull StringLiteralExpressionImpl element , Language language , MethodLanguageInjection languageInjection ) {
117
+ private void injectLanguage (@ NotNull MultiHostRegistrar registrar , @ NotNull StringLiteralExpressionImpl element , MethodLanguageInjection languageInjection ) {
118
+ Language language = languageInjection .getLanguage ();
119
+ if (language == null ) {
120
+ return ;
121
+ }
122
+
121
123
registrar .startInjecting (language )
122
- .addPlace (languageInjection .getPrefix (), languageInjection .getSuffix (), element , element .getValueRange ())
123
- .doneInjecting ();
124
+ .addPlace (languageInjection .getPrefix (), languageInjection .getSuffix (), element , element .getValueRange ())
125
+ .doneInjecting ();
124
126
}
125
127
126
- private class MethodLanguageInjection {
127
- private final Language language ;
128
+ private static class MethodLanguageInjection {
129
+ private final String language ;
128
130
private final String prefix ;
129
131
private final String suffix ;
130
132
private final MethodMatcher .CallToSignature [] signatures ;
131
133
132
- MethodLanguageInjection (@ NotNull String languageId , String prefix , String suffix , MethodMatcher .CallToSignature [] signatures ) {
133
-
134
- this .language = Language .findLanguageByID (languageId );
134
+ MethodLanguageInjection (String languageId , String prefix , String suffix , MethodMatcher .CallToSignature [] signatures ) {
135
+ this .language = languageId ;
135
136
this .prefix = prefix ;
136
137
this .suffix = suffix ;
137
138
this .signatures = signatures ;
138
139
}
139
140
141
+ @ Nullable
140
142
public Language getLanguage () {
141
- return language ;
143
+ return Language . findLanguageByID ( this . language ) ;
142
144
}
143
145
144
146
public String getPrefix () {
0 commit comments