|
22 | 22 | (#eq? @_template_function_name "$") |
23 | 23 | (#set! injection.language "bash")) |
24 | 24 |
|
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 |
26 | 27 |
|
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 | +) |
32 | 62 |
|
33 | 63 | ; Parse regex syntax within regex literals |
34 | 64 |
|
|
0 commit comments