| 1 | ## Adding textobject queries |
| 2 | |
| 3 | Helix supports textobjects that are language specific, such as functions, classes, etc. |
| 4 | These textobjects require an accompanying tree-sitter grammar and a `textobjects.scm` query file |
| 5 | to work properly. Tree-sitter allows us to query the source code syntax tree |
| 6 | and capture specific parts of it. The queries are written in a lisp dialect. |
| 7 | More information on how to write queries can be found in the [official tree-sitter |
| 8 | documentation][tree-sitter-queries]. |
| 9 | |
| 10 | Query files should be placed in `runtime/queries/{language}/textobjects.scm` |
| 11 | when contributing to Helix. Note that to test the query files locally you should put |
| 12 | them under your local runtime directory (`~/.config/helix/runtime` on Linux |
| 13 | for example). |
| 14 | |
| 15 | The following [captures][tree-sitter-captures] are recognized: |
| 16 | |
| 17 | | Capture Name | |
| 18 | | --- | |
| 19 | | `function.inside` | |
| 20 | | `function.around` | |
| 21 | | `class.inside` | |
| 22 | | `class.around` | |
| 23 | | `test.inside` | |
| 24 | | `test.around` | |
| 25 | | `parameter.inside` | |
| 26 | | `parameter.around` | |
| 27 | | `comment.inside` | |
| 28 | | `comment.around` | |
| 29 | | `entry.inside` | |
| 30 | | `entry.around` | |
| 31 | | `xml-element.inside` | |
| 32 | | `xml-element.around` | |
| 33 | |
| 34 | [Example query files][textobject-examples] can be found in the helix GitHub repository. |
| 35 | |
| 36 | ## Queries for textobject based navigation |
| 37 | |
| 38 | Tree-sitter based navigation in Helix is done using captures in the |
| 39 | following order: |
| 40 | |
| 41 | - `object.movement` |
| 42 | - `object.around` |
| 43 | - `object.inside` |
| 44 | |
| 45 | For example if a `function.around` capture has been already defined for a language |
| 46 | in its `textobjects.scm` file, function navigation should also work automatically. |
| 47 | `function.movement` should be defined only if the node captured by `function.around` |
| 48 | doesn't make sense in a navigation context. |
| 49 | |
| 50 | [tree-sitter-queries]: https://tree-sitter.github.io/tree-sitter/using-parsers/queries/1-syntax.html |
| 51 | [tree-sitter-captures]: https://tree-sitter.github.io/tree-sitter/using-parsers/queries/2-operators.html#capturing-nodes |
| 52 | [textobject-examples]: https://github.com/search?q=repo%3Ahelix-editor%2Fhelix+path%3A%2A%2A/textobjects.scm&type=Code&ref=advsearch&l=&l= |