on 6c11708 help sign in
helix/book/src/guides/adding_languages.md
71 lines 4.1 kB history raw
1# Adding languages
3## Submodules
5To add a new language, you should first add a tree-sitter submodule. To do this,
6you can run the command
7```sh
8git submodule add -f <repository> helix-syntax/languages/tree-sitter-<name>
9```
10For example, to add tree-sitter-ocaml you would run
11```sh
12git submodule add -f https://github.com/tree-sitter/tree-sitter-ocaml helix-syntax/languages/tree-sitter-ocaml
13```
14Make sure the submodule is shallow by doing
15```sh
16git config -f .gitmodules submodule.helix-syntax/languages/tree-sitter-<name>.shallow true
17```
19or you can manually add `shallow = true` to `.gitmodules`.
21## languages.toml
23Next, you need to add the language to the [`languages.toml`][languages.toml] found in the root of
24the repository; this `languages.toml` file is included at compilation time, and
25is distinct from the `language.toml` file in the user's [configuration
26directory](../configuration.md).
28These are the available keys and descriptions for the file.
30| Key | Description |
31| ---- | ----------- |
32| name | The name of the language |
33| scope | A string like `source.js` that identifies the language. Currently, we strive to match the scope names used by popular TextMate grammars and by the Linguist library. Usually `source.<name>` or `text.<name>` in case of markup languages |
34| injection-regex | regex pattern that will be tested against a language name in order to determine whether this language should be used for a potential [language injection][treesitter-language-injection] site. |
35| file-types | The filetypes of the language, for example `["yml", "yaml"]` |
36| shebangs | The interpreters from the shebang line, for example `["sh", "bash"]` |
37| roots | A set of marker files to look for when trying to find the workspace root. For example `Cargo.lock`, `yarn.lock` |
38| auto-format | Whether to autoformat this language when saving |
39| diagnostic-severity | Minimal severity of diagnostic for it to be displayed. (Allowed values: `Error`, `Warning`, `Info`, `Hint`) |
40| comment-token | The token to use as a comment-token |
41| indent | The indent to use. Has sub keys `tab-width` and `unit` |
42| config | Language server configuration |
44## Queries
46For a language to have syntax-highlighting and indentation among
47other things, you have to add queries. Add a directory for your
48language with the path `runtime/queries/<name>/`. The tree-sitter
49[website](https://tree-sitter.github.io/tree-sitter/syntax-highlighting#queries)
50gives more info on how to write queries.
52> NOTE: When evaluating queries, the first matching query takes
53precedence, which is different from other editors like neovim where
54the last matching query supercedes the ones before it. See
55[this issue][neovim-query-precedence] for an example.
57## Common Issues
59- If you get errors when building after switching branches, you may have to remove or update tree-sitter submodules. You can update submodules by running
60 ```sh
61 git submodule sync; git submodule update --init
62 ```
63- Make sure to not use the `--remote` flag. To remove submodules look inside the `.gitmodules` and remove directories that are not present inside of it.
65- If a parser is segfaulting or you want to remove the parser, make sure to remove the submodule *and* the compiled parser in `runtime/grammar/<name>.so`
67- The indents query is `indents.toml`, *not* `indents.scm`. See [this](https://github.com/helix-editor/helix/issues/114) issue for more information.
69[treesitter-language-injection]: https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection
70[languages.toml]: https://github.com/helix-editor/helix/blob/master/languages.toml
71[neovim-query-precedence]: https://github.com/helix-editor/helix/pull/1170#issuecomment-997294090