9
9
10
10
import com .google .common .collect .Sets ;
11
11
import com .intellij .lang .javascript .JSTokenTypes ;
12
+ import com .intellij .lang .javascript .patterns .JSPatterns ;
13
+ import com .intellij .lang .javascript .psi .JSExpression ;
12
14
import com .intellij .lang .javascript .psi .JSReferenceExpression ;
13
15
import com .intellij .lang .javascript .psi .ecma6 .ES6TaggedTemplateExpression ;
14
16
import com .intellij .lang .javascript .psi .ecma6 .JSStringTemplateExpression ;
15
17
import com .intellij .lang .jsgraphql .ide .injection .GraphQLCommentBasedInjectionHelper ;
16
18
import com .intellij .openapi .util .TextRange ;
17
19
import com .intellij .openapi .util .text .StringUtil ;
20
+ import com .intellij .patterns .ElementPattern ;
18
21
import com .intellij .psi .PsiElement ;
19
22
import com .intellij .psi .util .PsiTreeUtil ;
20
23
import com .intellij .psi .util .PsiUtilCore ;
@@ -38,6 +41,8 @@ public class GraphQLJavaScriptLanguageInjectionUtil {
38
41
public static final String GQL_TEMPLATE_TAG = "gql" ;
39
42
public static final String APOLLO_GQL_TEMPLATE_TAG = "Apollo.gql" ;
40
43
44
+ private static final ElementPattern <JSExpression > GRAPHQL_CALL_ARG_PATTERN = JSPatterns .jsArgument (GRAPHQL_TEMPLATE_TAG , 0 );
45
+
41
46
public final static Set <String > SUPPORTED_TAG_NAMES = Sets .newHashSet (
42
47
RELAY_QL_TEMPLATE_TAG ,
43
48
GRAPHQL_TEMPLATE_TAG ,
@@ -47,16 +52,19 @@ public class GraphQLJavaScriptLanguageInjectionUtil {
47
52
);
48
53
49
54
public static boolean isGraphQLLanguageInjectionTarget (@ Nullable PsiElement host ) {
50
- if (!(host instanceof JSStringTemplateExpression )) {
55
+ if (!(host instanceof JSStringTemplateExpression template )) {
51
56
return false ;
52
57
}
53
58
54
59
// gql``, Relay.QL``, etc
55
- JSStringTemplateExpression template = (JSStringTemplateExpression ) host ;
56
60
if (isInjectedUsingTemplateTag (template )) {
57
61
return true ;
58
62
}
59
63
64
+ if (isInjectedInCallArgument (template )) {
65
+ return true ;
66
+ }
67
+
60
68
// built-in "language=GraphQL" injection comments
61
69
if (isInjectedUsingBuiltInComments (template )) {
62
70
return true ;
@@ -100,6 +108,12 @@ private static boolean isInjectedUsingTemplateTag(@NotNull JSStringTemplateExpre
100
108
return builderTailName != null && SUPPORTED_TAG_NAMES .contains (builderTailName );
101
109
}
102
110
111
+ private static boolean isInjectedInCallArgument (@ NotNull JSStringTemplateExpression template ) {
112
+ PsiElement parent = template .getParent ();
113
+ PsiElement expr = parent instanceof ES6TaggedTemplateExpression ? parent : template ;
114
+ return GRAPHQL_CALL_ARG_PATTERN .accepts (expr );
115
+ }
116
+
103
117
/**
104
118
* Checks a case when the possible injection contains a EOL comment "# graphql".
105
119
*/
0 commit comments