Skip to content

Commit ae61981

Browse files
committed
ecma/injections: support more graphql injection modes
GraphQL injections previously only operated on a 'gql' tagged template literal, but a few more options are supported by the official VSCode 'GraphQL: Syntax Highlighting' extension: // Already supported gql`query { foo }`; // New gql('query { foo }'); gql(`query { foo }`); graphql`query { foo }`; graphql('query { foo }'); graphql(`query { foo }`); '#graphql query { foo }'; `#graphql query { foo }`; /* GraphQL */ 'query { foo }'; /* GraphQL */ `query { foo }`; Note that until a PR in tree-sitter-typescript[^1] is merged upstream and updated here, tagged template literals with type arguments (e.g. 'gql<SomeType>`query { foo }`;') won't have a working injection. supports helix-editor#14413 [^1]: tree-sitter/tree-sitter-typescript#350
1 parent 088eb17 commit ae61981

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

runtime/queries/ecma/injections.scm

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,43 @@
2222
(#eq? @_template_function_name "$")
2323
(#set! injection.language "bash"))
2424

25-
; Parse the contents of gql template literals
25+
; GraphQL detection generally matches the rules provided by the 'GraphQL: Syntax Highlighting'
26+
; VSCode extension: https://github.com/graphql/graphiql/blob/8f25b38f4ab14dc99c046109f255fb283bccde52/packages/vscode-graphql-syntax/grammars/graphql.js.json
2627

27-
((call_expression
28-
function: (identifier) @_template_function_name
29-
arguments: (template_string (string_fragment) @injection.content))
30-
(#eq? @_template_function_name "gql")
31-
(#set! injection.language "graphql"))
28+
; Parse the contents of 'gql' and 'graphql' template literals and function calls
29+
(
30+
(call_expression
31+
function: (identifier) @_template_function_name
32+
arguments: [
33+
; Tagged template literal: NAME``
34+
(template_string (string_fragment) @injection.content)
35+
(
36+
arguments . [
37+
; Function call containing a string literal: NAME('')
38+
(string (string_fragment) @injection.content)
39+
; Function call containing a template literal: NAME(``)
40+
(template_string (string_fragment) @injection.content)
41+
]
42+
)
43+
]
44+
)
45+
(#any-of? @_template_function_name "gql" "graphql")
46+
(#set! injection.language "graphql")
47+
)
48+
49+
; Parse the contents of tagged template literals that begin with a GraphQL comment '#graphql'
50+
(
51+
(template_string (string_fragment) @injection.content)
52+
(#match? @injection.content "^\\s*#graphql")
53+
(#set! injection.language "graphql")
54+
)
55+
56+
; Parse the contents of tagged template literals with leading ECMAScript comments '/* GraphQL */'
57+
(
58+
((comment) @_ecma_comment (template_string (string_fragment) @injection.content))
59+
(#eq? @_ecma_comment "/* GraphQL */")
60+
(#set! injection.language "graphql")
61+
)
3262

3363
; Parse regex syntax within regex literals
3464

0 commit comments

Comments
 (0)