Skip to content

Commit d314843

Browse files
committed
Trigger GraphQL language injection for builder APIs ending with supported tag (#112)
1 parent f8a2190 commit d314843

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/main/com/intellij/lang/jsgraphql/ide/editor/JSGraphQLLineMarkerProvider.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.Collection;
2525
import java.util.List;
2626

27+
import static com.intellij.lang.jsgraphql.ide.injection.JSGraphQLLanguageInjectionUtil.*;
28+
2729
public class JSGraphQLLineMarkerProvider implements LineMarkerProvider {
2830

2931
@Nullable
@@ -46,20 +48,21 @@ private LineMarkerInfo createLineMarkerInfo(PsiElement element, Ref<String> envR
4648
final String tooltip;
4749
final String url;
4850
boolean configureGQL = false;
49-
if(JSGraphQLLanguageInjectionUtil.RELAY_ENVIRONMENT.equals(envRef.get())) {
51+
final String environment = envRef.get();
52+
if(RELAY_ENVIRONMENT.equals(environment)) {
5053
icon = JSGraphQLIcons.Logos.Relay;
5154
tooltip = "Relay GraphQL Template";
5255
url = "https://facebook.github.io/relay/docs/api-reference-relay-ql.html";
53-
} else if(JSGraphQLLanguageInjectionUtil.GRAPHQL_ENVIRONMENT.equals(envRef.get())) {
56+
} else if(GRAPHQL_ENVIRONMENT.equals(environment) || GRAPHQL_TEMPLATE_ENVIRONMENT.equals(environment)) {
5457
icon = JSGraphQLIcons.Logos.GraphQL;
5558
tooltip = "GraphQL";
5659
url = "http://graphql.org/";
57-
} else if(JSGraphQLLanguageInjectionUtil.APOLLO_ENVIRONMENT.equals(envRef.get())) {
60+
} else if(APOLLO_ENVIRONMENT.equals(environment)) {
5861
icon = JSGraphQLIcons.Logos.Apollo;
5962
tooltip = "Apollo Client GraphQL Template";
6063
url = "http://docs.apollostack.com/apollo-client/core.html";
6164
configureGQL = true;
62-
} else if(JSGraphQLLanguageInjectionUtil.LOKKA_ENVIRONMENT.equals(envRef.get())) {
65+
} else if(LOKKA_ENVIRONMENT.equals(environment)) {
6366
icon = JSGraphQLIcons.Logos.Lokka;
6467
tooltip = "Lokka GraphQL Template";
6568
url = "https://github.com/kadirahq/lokka";

src/main/com/intellij/lang/jsgraphql/ide/injection/JSGraphQLLanguageInjectionUtil.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import com.google.common.collect.Sets;
1111
import com.intellij.ide.util.PropertiesComponent;
12+
import com.intellij.lang.javascript.psi.JSExpression;
1213
import com.intellij.lang.javascript.psi.JSFile;
1314
import com.intellij.lang.javascript.psi.JSReferenceExpression;
1415
import com.intellij.lang.javascript.psi.ecma6.JSStringTemplateExpression;
@@ -42,6 +43,7 @@ public class JSGraphQLLanguageInjectionUtil {
4243

4344

4445
public static final String GRAPHQL_ENVIRONMENT = "graphql";
46+
public static final String GRAPHQL_TEMPLATE_ENVIRONMENT = "graphql-template";
4547
public static final String RELAY_ENVIRONMENT = "relay";
4648
public static final String APOLLO_ENVIRONMENT = "apollo";
4749
public static final String LOKKA_ENVIRONMENT = "lokka";
@@ -93,8 +95,14 @@ public static boolean isJSGraphQLLanguageInjectionTarget(PsiElement host, @Nulla
9395
tagExpression = (JSReferenceExpression) template.getPrevSibling();
9496
}
9597
if (tagExpression != null) {
96-
final String tagText = tagExpression.getText();
97-
if (SUPPORTED_TAG_NAMES.contains(tagText)) {
98+
String tagText = tagExpression.getText();
99+
boolean isSupportedTag = SUPPORTED_TAG_NAMES.contains(tagText);
100+
if(!isSupportedTag && tagExpression.getQualifier() != null && tagExpression.getReferenceNameElement() != null) {
101+
// also allow builder patterns to trigger injection, e.g. 'builder.foo.bar.graphql``'
102+
tagText = tagExpression.getReferenceNameElement().getText();
103+
isSupportedTag = SUPPORTED_TAG_NAMES.contains(tagText);
104+
}
105+
if (isSupportedTag) {
98106
if (envRef != null) {
99107
envRef.set(getEnvironmentFromTemplateTag(tagText, host));
100108
}
@@ -110,6 +118,13 @@ public static String getEnvironmentFromTemplateTag(String tagText, PsiElement ho
110118
return RELAY_ENVIRONMENT;
111119
}
112120
if (GRAPHQL_TEMPLATE_TAG.equals(tagText) || GRAPHQL_EXPERIMENTAL_TEMPLATE_TAG.equals(tagText)) {
121+
if(host instanceof JSStringTemplateExpression) {
122+
final JSExpression[] arguments = ((JSStringTemplateExpression) host).getArguments();
123+
if(arguments.length > 0) {
124+
// one or more placeholders inside the tagged template text, so consider it templated graphql
125+
return GRAPHQL_TEMPLATE_ENVIRONMENT;
126+
}
127+
}
113128
return GRAPHQL_ENVIRONMENT;
114129
}
115130
if (GQL_TEMPLATE_TAG.equals(tagText)) {

0 commit comments

Comments
 (0)