| 1 | ## Adding new languages to Helix |
| 2 | |
| 3 | In order to add a new language to Helix, you will need to follow the steps |
| 4 | below. |
| 5 | |
| 6 | ## Language configuration |
| 7 | |
| 8 | 1. Add a new `[[language]]` entry in the `languages.toml` file and provide the |
| 9 | necessary configuration for the new language. For more information on |
| 10 | language configuration, refer to the |
| 11 | [language configuration section](../languages.md) of the documentation. |
| 12 | A new language server can be added by extending the `[language-server]` table in the same file. |
| 13 | 2. If you are adding a new language or updating an existing language server |
| 14 | configuration, run the command `cargo xtask docgen` to update the |
| 15 | [Language Support](../lang-support.md) documentation. |
| 16 | |
| 17 | > 💡 If you are adding a new Language Server configuration, make sure to update |
| 18 | > the |
| 19 | > [Language Server Wiki](https://github.com/helix-editor/helix/wiki/Language-Server-Configurations) |
| 20 | > with the installation instructions. |
| 21 | |
| 22 | ## Grammar configuration |
| 23 | |
| 24 | 1. If a tree-sitter grammar is available for the new language, add a new |
| 25 | `[[grammar]]` entry to the `languages.toml` file. |
| 26 | 2. If you are testing the grammar locally, you can use the `source.path` key |
| 27 | with an absolute path to the grammar. However, before submitting a pull |
| 28 | request, make sure to switch to using `source.git`. |
| 29 | |
| 30 | ## Queries |
| 31 | |
| 32 | 1. In order to provide syntax highlighting and indentation for the new language, |
| 33 | you will need to add queries. |
| 34 | 2. Create a new directory for the language with the path |
| 35 | `runtime/queries/<name>/`. |
| 36 | 3. Refer to the |
| 37 | [tree-sitter website](https://tree-sitter.github.io/tree-sitter/3-syntax-highlighting.html#highlights) |
| 38 | for more information on writing queries. |
| 39 | 4. The highlight captures (`@function`, `@type`, ...) and how they resolve are |
| 40 | documented [on the themes page](./themes.md): |
| 41 | match the most specific scope that fits, capture the leaf node you mean, and |
| 42 | remember that the last matching pattern (and the innermost node) wins. |
| 43 | 5. Helix loads several query files from that directory; only `highlights.scm` is |
| 44 | required: |
| 45 | |
| 46 | | File | Purpose | Guide | |
| 47 | |---|---|---| |
| 48 | | `highlights.scm` | syntax highlighting | [highlights.md](./highlights.md) | |
| 49 | | `injections.scm` | embed other languages in regions (strings, code fences) | [injection.md](./injection.md) | |
| 50 | | `indents.scm` | indentation | [indent.md](./indent.md) | |
| 51 | | `textobjects.scm` | textobjects and navigation (`mif`, `]f`, …) | [textobject.md](./textobject.md) | |
| 52 | | `locals.scm` | scope tracking so locals highlight distinctly | [locals.md](./locals.md) | |
| 53 | | `tags.scm` | document/workspace symbol pickers | [tags.md](./tags.md) | |
| 54 | | `rainbows.scm` | rainbow brackets | [rainbow_bracket_queries.md](./rainbow_bracket_queries.md) | |
| 55 | |
| 56 | A query file may reuse another language's with `; inherits: <lang>` on the |
| 57 | first line. Run `cargo xtask query-check [language]` to check that the queries |
| 58 | are valid against the grammar. |
| 59 | |
| 60 | ## Common issues |
| 61 | |
| 62 | - If you encounter errors when running Helix after switching branches, you may |
| 63 | need to update the tree-sitter grammars. Run the command `hx --grammar fetch` |
| 64 | to fetch the grammars and `hx --grammar build` to build any out-of-date |
| 65 | grammars. |
| 66 | - If a parser is causing a segfault, or you want to remove it, make sure to |
| 67 | remove the compiled parser located at `runtime/grammars/<name>.so`. |
| 68 | - If you are attempting to add queries and Helix is unable to locate them, ensure that the environment variable `HELIX_RUNTIME` is set to the location of the `runtime` folder you're developing in. |
| 69 | - Validate queries with `cargo xtask query-check [language]` (every query file |
| 70 | must compile against the grammar). `highlight-check` and `indent-check` |
| 71 | additionally run the real highlighter and indenter over the test fixtures catch mistakes. |