Skip to content

Commit

Permalink
add documentation for language injection
Browse files Browse the repository at this point in the history
  • Loading branch information
nrdxp committed Sep 28, 2022
1 parent bdf119d commit 5229fa8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
- [Adding Languages](./guides/adding_languages.md)
- [Adding Textobject Queries](./guides/textobject.md)
- [Adding Indent Queries](./guides/indent.md)
- [Adding Injection Queries](./guides/injection.md)
56 changes: 56 additions & 0 deletions book/src/guides/injection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Adding Injection Queries

Writing language injection queries allows one to highlight a specific node as a different language.
In addition to the [standard](upstream-docs) language injection options used by tree-sitter, there
are a few Helix specific extensions that allow for more control.

And example of a simple query that would highligh all strings as bash:
```scm
((string_expression (string_fragment) @injection.content)
(#set! injection.language "bash"))
```
## Capture Types

- `@injection.language` (standard):
The captured node may contain the language name used to highlight the node captured by
`@injection.content`.

- `@injection.content` (standard):
Marks the content to be highlighted as the language captured with `@injection.language` _et al_.

- `@injection.filename` (extension):
The captured node may contain a filename with a file-extension known to Helix,
highlighting `@injection.content` as that language. This uses the language extensions defined in
both the default languages.toml destributed with Helix, as well as user defined languages.

- `@injection.shebang` (extension):
The captured node may contain a shebang known to Helix, this capture also implies
`@injection.content`. This also uses the shebangs defined in the default and user languages.toml.

## Settings

- `injection.combined` (standard):
Indicates that all of the matching nodes in the tree should have their content parsed as one
nested document.

- `injection.language` (standard):
Forces the captured content to be highlighted as the given language

- `injection.include-children` (standard):
Indicates that the content node’s entire text should be re-parsed, including the text of its child
nodes. By default, child nodes’ text will be excluded from the injected document.

- `injection.include-unamed-children` (extension):
Same as `injection.include-children` but only for unamed child nodes.

## Predicates

- `#eq?`:
The first argument (a capture) must be equal to the second argument
(a capture or a string).

- `#match?`:
The first argument (a capture) must match the regex given in the
second argument (a string).

[upstream-docs]: http://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection

0 comments on commit 5229fa8

Please sign in to comment.