<commit_before>/**
 *  Copyright (c) 2015-present, <NAME>
 *  All rights reserved.
 *
 *  This source code is licensed under the MIT license found in the
 *  LICENSE file in the root directory of this source tree.
 */
package com.intellij.lang.jsgraphql.ide.injection;

import com.intellij.lang.javascript.psi.JSFile;
import com.intellij.lang.javascript.psi.JSReferenceExpression;
import com.intellij.lang.javascript.psi.ecma6.JSStringTemplateExpression;
import com.intellij.lang.jsgraphql.psi.JSGraphQLFile;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiLanguageInjectionHost;
import org.jetbrains.annotations.Nullable;

public class JSGraphQLLanguageInjectionUtil {

    public static final String RELAY_QL_TEMPLATE_TAG = "Relay.QL";
    public static final String GRAPHQL_TEMPLATE_TAG = "graphql";

    public enum JSGraphQLInjectionTag {
        RelayQL,
        GraphQL
    }

    public static boolean isJSGraphQLLanguageInjectionTarget(PsiElement host) {
        return isJSGraphQLLanguageInjectionTarget(host, null);
    }

    public static boolean isRelayInjection(PsiFile file) {
        if(file instanceof JSFile) {
            return true;
        } else if (file instanceof JSGraphQLFile) {
            final Ref<JSGraphQLInjectionTag> tag = new Ref<>();
            return file.getContext() != null && isJSGraphQLLanguageInjectionTarget(file.getContext(), tag) && tag.get() == JSGraphQLInjectionTag.RelayQL;
        }
        return false;
    }

    public static boolean isJSGraphQLLanguageInjectionTarget(PsiElement host, @Nullable Ref<JSGraphQLInjectionTag> tagRef) {
        if(host instanceof JSStringTemplateExpression && host instanceof PsiLanguageInjectionHost) {
            JSStringTemplateExpression template = (JSStringTemplateExpression) host;
            // check if we're a Relay.QL or graphql tagged template
            final PsiElement firstChild = template.getFirstChild();
            if (firstChild instanceof JSReferenceExpression) {
                final String tagText = firstChild.getText();
                if (RELAY_QL_TEMPLATE_TAG.equals(tagText) || GRAPHQL_TEMPLATE_TAG.equals(tagText)) {
                    if(tagRef != null) {
                        tagRef.set(RELAY_QL_TEMPLATE_TAG.equals(tagText) ? JSGraphQLInjectionTag.RelayQL : JSGraphQLInjectionTag.GraphQL);<commit_msg>Add support for Apollo's gql template literal<commit_after>/**
 *  Copyright (c) 2015-present, <NAME>
 *  All rights reserved.
 *
 *  This source code is licensed under the MIT license found in the
 *  LICENSE file in the root directory of this source tree.
 */
package com.intellij.lang.jsgraphql.ide.injection;

import com.intellij.lang.javascript.psi.JSFile;
import com.intellij.lang.javascript.psi.JSReferenceExpression;
import com.intellij.lang.javascript.psi.ecma6.JSStringTemplateExpression;
import com.intellij.lang.jsgraphql.psi.JSGraphQLFile;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiLanguageInjectionHost;
import org.jetbrains.annotations.Nullable;

public class JSGraphQLLanguageInjectionUtil {

    public static final String RELAY_QL_TEMPLATE_TAG = "Relay.QL";
    public static final String GRAPHQL_TEMPLATE_TAG = "graphql";
    public static final String APOLLO_GQL_TEMPLATE_TAG = "gql";

    public enum JSGraphQLInjectionTag {
        RelayQL,
        GraphQL
    }

    public static boolean isJSGraphQLLanguageInjectionTarget(PsiElement host) {
        return isJSGraphQLLanguageInjectionTarget(host, null);
    }

    public static boolean isRelayInjection(PsiFile file) {
        if(file instanceof JSFile) {
            return true;
        } else if (file instanceof JSGraphQLFile) {
            final Ref<JSGraphQLInjectionTag> tag = new Ref<>();
            return file.getContext() != null && isJSGraphQLLanguageInjectionTarget(file.getContext(), tag) && tag.get() == JSGraphQLInjectionTag.RelayQL;
        }
        return false;
    }

    public static boolean isJSGraphQLLanguageInjectionTarget(PsiElement host, @Nullable Ref<JSGraphQLInjectionTag> tagRef) {
        if(host instanceof JSStringTemplateExpression && host instanceof PsiLanguageInjectionHost) {
            JSStringTemplateExpression template = (JSStringTemplateExpression) host;
            // check if we're a Relay.QL or graphql tagged template
            final PsiElement firstChild = template.getFirstChild();
            if (firstChild instanceof JSReferenceExpression) {
                final String tagText = firstChild.getText();
                if (RELAY_QL_TEMPLATE_TAG.equals(tagText) || GRAPHQL_TEMPLATE_TAG.equals(tagText) || APOLLO_GQL_TEMPLATE_TAG.equals(tagText)) {
                    if(tagRef != null) {
                        tagRef.set(RELAY_QL_TEMPLATE_TAG.equals(tagText) ? JSGraphQLInjectionTag.RelayQL : JSGraphQLInjectionTag.GraphQL);
{'fraction_non_alphanumeric': 0.08232733029883238, 'fraction_numerical': 0.0019790223629527013, 'mean_word_length': 3.6155251141552514, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1223830', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1456, 'n_words': 324}