| 1 | ## Adding Injection Queries |
| 2 | |
| 3 | Writing language injection queries allows one to highlight a specific node as a different language. |
| 4 | In addition to the [standard][upstream-docs] language injection options used by tree-sitter, there |
| 5 | are a few Helix specific extensions that allow for more control. |
| 6 | |
| 7 | Injection drives more than highlighting: within an injected region Helix also |
| 8 | uses the injected language's own indentation, textobjects, and comment tokens — |
| 9 | so, for example, editing JavaScript inside an HTML `<script>` indents and |
| 10 | comments as JavaScript. |
| 11 | |
| 12 | An example of a simple query that would highlight all strings as bash in Nix: |
| 13 | ```scm |
| 14 | ((string_expression (string_fragment) @injection.content) |
| 15 | (#set! injection.language "bash")) |
| 16 | ``` |
| 17 | Another example is this query, which highlights links in comments and keywords like "TODO", by reusing the dedicated "comment" language: |
| 18 | ```scm |
| 19 | ((comment) @injection.content |
| 20 | (#set! injection.language "comment")) |
| 21 | ``` |
| 22 | |
| 23 | ## Capture Types |
| 24 | |
| 25 | - `@injection.language` (standard): |
| 26 | The captured node may contain the language name used to highlight the node captured by |
| 27 | `@injection.content`. |
| 28 | |
| 29 | - `@injection.content` (standard): |
| 30 | Marks the content to be highlighted as the language captured with `@injection.language` _et al_. |
| 31 | |
| 32 | - `@injection.filename` (extension): |
| 33 | The captured node may contain a filename with a file-extension known to Helix, |
| 34 | highlighting `@injection.content` as that language. This uses the language extensions defined in |
| 35 | both the default languages.toml distributed with Helix, as well as user defined languages. |
| 36 | |
| 37 | - `@injection.shebang` (extension): |
| 38 | The captured node may contain a shebang used to choose a language to highlight as. This also uses |
| 39 | the shebangs defined in the default and user `languages.toml`. |
| 40 | |
| 41 | ## Settings |
| 42 | |
| 43 | - `injection.combined` (standard): |
| 44 | Indicates that all the matching nodes in the tree should have their content parsed as one |
| 45 | nested document. |
| 46 | |
| 47 | - `injection.language` (standard): |
| 48 | Forces the captured content to be highlighted as the given language |
| 49 | |
| 50 | - `injection.include-children` (standard): |
| 51 | Indicates that the content node’s entire text should be re-parsed, including the text of its child |
| 52 | nodes. By default, child nodes’ text will be excluded from the injected document. |
| 53 | |
| 54 | - `injection.include-unnamed-children` (extension): |
| 55 | Same as `injection.include-children` but only for unnamed child nodes. |
| 56 | |
| 57 | ## Predicates |
| 58 | |
| 59 | - `#eq?` (standard): |
| 60 | The first argument (a capture) must be equal to the second argument |
| 61 | (a capture or a string). |
| 62 | |
| 63 | - `#match?` (standard): |
| 64 | The first argument (a capture) must match the regex given in the |
| 65 | second argument (a string). |
| 66 | |
| 67 | - `#any-of?` (standard): |
| 68 | The first argument (a capture) must be one of the other arguments (strings). |
| 69 | |
| 70 | [upstream-docs]: https://tree-sitter.github.io/tree-sitter/3-syntax-highlighting.html#language-injection |