on master help sign in
rewrite language configuration docs (#2838)
0ad10ceMichael Davis
book/src/guides/adding_languages.md+7−60
@@ -2,40 +2,8 @@
2
2
3## Language configuration
3## Language configuration
4
4
5−To add a new language, you need to add a `language` entry to the
5+To add a new language, you need to add a `[[language]]` entry to the
6−[`languages.toml`][languages.toml] found in the root of the repository;
6+`languages.toml` (see the [language configuration section]).
7−this `languages.toml` file is included at compilation time, and is
8−distinct from the `languages.toml` file in the user's [configuration
9−directory](../configuration.md).
10−
11−```toml
12−[[language]]
13−name = "mylang"
14−scope = "scope.mylang"
15−injection-regex = "^mylang$"
16−file-types = ["mylang", "myl"]
17−comment-token = "#"
18−indent = { tab-width = 2, unit = " " }
19−language-server = { command = "mylang-lsp", args = ["--stdio"] }
20−```
21−
22−These are the available keys and descriptions for the file.
23−
24−| Key | Description |
25−| ---- | ----------- |
26−| `name` | The name of the language |
27−| `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 |
28−| `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. |
29−| `file-types` | The filetypes of the language, for example `["yml", "yaml"]`. Extensions and full file names are supported. |
30−| `shebangs` | The interpreters from the shebang line, for example `["sh", "bash"]` |
31−| `roots` | A set of marker files to look for when trying to find the workspace root. For example `Cargo.lock`, `yarn.lock` |
32−| `auto-format` | Whether to autoformat this language when saving |
33−| `diagnostic-severity` | Minimal severity of diagnostic for it to be displayed. (Allowed values: `Error`, `Warning`, `Info`, `Hint`) |
34−| `comment-token` | The token to use as a comment-token |
35−| `indent` | The indent to use. Has sub keys `tab-width` and `unit` |
36−| `language-server` | The Language Server to run. Has sub keys `command` and `args` |
37−| `config` | Language Server configuration |
38−| `grammar` | The tree-sitter grammar to use (defaults to the value of `name`) |
39
7
40When adding a new language or Language Server configuration for an existing
8When adding a new language or Language Server configuration for an existing
41language, run `cargo xtask docgen` to add the new configuration to the
9language, run `cargo xtask docgen` to add the new configuration to the
@@ -45,32 +13,12 @@ When adding a Language Server configuration, be sure to update the
45
13
46## Grammar configuration
14## Grammar configuration
47
15
48−If a tree-sitter grammar is available for the language, add a new `grammar`
16+If a tree-sitter grammar is available for the language, add a new `[[grammar]]`
49entry to `languages.toml`.
17entry to `languages.toml`.
50
18
51−```toml
19+You may use the `source.path` key rather than `source.git` with an absolute path
52−[[grammar]]
53−name = "mylang"
54−source = { git = "https://github.com/example/mylang", rev = "a250c4582510ff34767ec3b7dcdd3c24e8c8aa68" }
55−```
56−
57−Grammar configuration takes these keys:
58−
59−| Key | Description |
60−| --- | ----------- |
61−| `name` | The name of the tree-sitter grammar |
62−| `source` | The method of fetching the grammar - a table with a schema defined below |
63−
64−Where `source` is a table with either these keys when using a grammar from a
65−git repository:
66−
67−| Key | Description |
68−| --- | ----------- |
69−| `git` | A git remote URL from which the grammar should be cloned |
70−| `rev` | The revision (commit hash or tag) which should be fetched |
71−| `subpath` | A path within the grammar directory which should be built. Some grammar repositories host multiple grammars (for example `tree-sitter-typescript` and `tree-sitter-ocaml`) in subdirectories. This key is used to point `hx --grammar build` to the correct path for compilation. When omitted, the root of repository is used |
72−
73−Or a `path` key with an absolute path to a locally available grammar directory.
20+to a locally available grammar for testing, but switch to `source.git` before
21+submitting a pull request.
74
22
75## Queries
23## Queries
76
24
@@ -91,8 +39,7 @@ the last matching query supersedes the ones before it. See
91
39
92- If a parser is segfaulting or you want to remove the parser, make sure to remove the compiled parser in `runtime/grammar/<name>.so`
40- If a parser is segfaulting or you want to remove the parser, make sure to remove the compiled parser in `runtime/grammar/<name>.so`
93
41
94−[treesitter-language-injection]: https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection
42+[language configuration section]: ../languages.md
95−[languages.toml]: https://github.com/helix-editor/helix/blob/master/languages.toml
96[neovim-query-precedence]: https://github.com/helix-editor/helix/pull/1170#issuecomment-997294090
43[neovim-query-precedence]: https://github.com/helix-editor/helix/pull/1170#issuecomment-997294090
97[install-lsp-wiki]: https://github.com/helix-editor/helix/wiki/How-to-install-the-default-language-servers
44[install-lsp-wiki]: https://github.com/helix-editor/helix/wiki/How-to-install-the-default-language-servers
98[lang-support]: ../lang-support.md
45[lang-support]: ../lang-support.md
book/src/languages.md+90−16
@@ -1,10 +1,17 @@
1# Languages
1# Languages
2
2
3−Language-specific settings and settings for particular language servers can be configured in a `languages.toml` file placed in your [configuration directory](./configuration.md). Helix actually uses two `languages.toml` files, the [first one](https://github.com/helix-editor/helix/blob/master/languages.toml) is in the main helix repository; it contains the default settings for each language and is included in the helix binary at compile time. Users who want to see the available settings and options can either reference the helix repo's `languages.toml` file, or consult the table in the [adding languages](./guides/adding_languages.md) section.
3+Language-specific settings and settings for language servers are configured
4+in `languages.toml` files.
4
5
5−A local `languages.toml` can be created within a `.helix` directory. Its settings will be merged with both the global and default configs.
6+## `languages.toml` files
6
7
7−Changes made to the `languages.toml` file in a user's [configuration directory](./configuration.md) are merged with helix's defaults on start-up, such that a user's settings will take precedence over defaults in the event of a collision. For example, the default `languages.toml` sets rust's `auto-format` to `true`. If a user wants to disable auto-format, they can change the `languages.toml` in their [configuration directory](./configuration.md) to make the rust entry read like the example below; the new key/value pair `auto-format = false` will override the default when the two sets of settings are merged on start-up:
8+There are three possible `languages.toml` files. The first is compiled into
9+Helix and lives in the [Helix repository](https://github.com/helix-editor/helix/blob/master/languages.toml).
10+This provides the default configurations for languages and language servers.
11+
12+You may define a `languages.toml` in your [configuration directory](./configuration.md)
13+which overrides values from the built-in language configuration. For example
14+to disable auto-LSP-formatting in Rust:
8
15
9```toml
16```toml
10# in <config_dir>/helix/languages.toml
17# in <config_dir>/helix/languages.toml
@@ -14,9 +21,60 @@ name = "rust"
14auto-format = false
21auto-format = false
15```
22```
16
23
17−## LSP formatting options
24+Language configuration may also be overridden local to a project by creating
25+a `languages.toml` file under a `.helix` directory. Its settings will be merged
26+with the language configuration in the configuration directory and the built-in
27+configuration.
28+
29+## Language configuration
18
30
31+Each language is configured by adding a `[[language]]` section to a
32+`languages.toml` file. For example:
33+
34+```toml
35+[[language]]
36+name = "mylang"
37+scope = "source.mylang"
38+injection-regex = "^mylang$"
39+file-types = ["mylang", "myl"]
40+comment-token = "#"
41+indent = { tab-width = 2, unit = " " }
42+language-server = { command = "mylang-lsp", args = ["--stdio"] }
43+```
44+
45+These configuration keys are available:
46+
47+| Key | Description |
48+| ---- | ----------- |
49+| `name` | The name of the language |
50+| `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 |
51+| `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. |
52+| `file-types` | The filetypes of the language, for example `["yml", "yaml"]`. Extensions and full file names are supported. |
53+| `shebangs` | The interpreters from the shebang line, for example `["sh", "bash"]` |
54+| `roots` | A set of marker files to look for when trying to find the workspace root. For example `Cargo.lock`, `yarn.lock` |
55+| `auto-format` | Whether to autoformat this language when saving |
56+| `diagnostic-severity` | Minimal severity of diagnostic for it to be displayed. (Allowed values: `Error`, `Warning`, `Info`, `Hint`) |
57+| `comment-token` | The token to use as a comment-token |
58+| `indent` | The indent to use. Has sub keys `tab-width` and `unit` |
59+| `language-server` | The Language Server to run. See the Language Server configuration section below. |
60+| `config` | Language Server configuration |
61+| `grammar` | The tree-sitter grammar to use (defaults to the value of `name`) |
62+
63+### Language Server configuration
64+
65+The `language-server` field takes the following keys:
66+
67+| Key | Description |
68+| --- | ----------- |
69+| `command` | The name of the language server binary to execute. Binaries must be in `$PATH` |
70+| `args` | A list of arguments to pass to the language server binary |
71+| `timeout` | The maximum time a request to the language server may take, in seconds. Defaults to `20` |
72+| `language-id` | The language name to pass to the language server. Some language servers support multiple languages and use this field to determine which one is being served in a buffer |
73+
74+The top-level `config` field is used to configure the LSP initialization options. A `format`
75+sub-table within `config` can be used to pass extra formatting options to
19−Use `format` field to pass extra formatting options to [Document Formatting Requests](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-16.md#document-formatting-request--leftwards_arrow_with_hook).
76+[Document Formatting Requests](https://github.com/microsoft/language-server-protocol/blob/gh-pages/_specifications/specification-3-16.md#document-formatting-request--leftwards_arrow_with_hook).
77+For example with typescript:
20
78
21```toml
79```toml
22[[language]]
80[[language]]
@@ -26,23 +84,37 @@ auto-format = true
26config = { format = { "semicolons" = "insert", "insertSpaceBeforeFunctionParenthesis" = true } }
84config = { format = { "semicolons" = "insert", "insertSpaceBeforeFunctionParenthesis" = true } }
27```
85```
28
86
29−## Tree-sitter grammars
87+## Tree-sitter grammar configuration
30
88
31−Tree-sitter grammars can also be configured in `languages.toml`:
89+The source for a language's tree-sitter grammar is specified in a `[[grammar]]`
90+section in `languages.toml`. For example:
32
91
33```toml
92```toml
34−# in <config_dir>/helix/languages.toml
35−
36[[grammar]]
93[[grammar]]
37−name = "rust"
94+name = "mylang"
38−source = { git = "https://github.com/tree-sitter/tree-sitter-rust", rev = "a250c4582510ff34767ec3b7dcdd3c24e8c8aa68" }
95+source = { git = "https://github.com/example/mylang", rev = "a250c4582510ff34767ec3b7dcdd3c24e8c8aa68" }
39−
40−[[grammar]]
41−name = "c"
42−source = { path = "/path/to/tree-sitter-c" }
43```
96```
44
97
98+Grammar configuration takes these keys:
99+
100+| Key | Description |
101+| --- | ----------- |
102+| `name` | The name of the tree-sitter grammar |
103+| `source` | The method of fetching the grammar - a table with a schema defined below |
104+
105+Where `source` is a table with either these keys when using a grammar from a
106+git repository:
107+
108+| Key | Description |
109+| --- | ----------- |
110+| `git` | A git remote URL from which the grammar should be cloned |
111+| `rev` | The revision (commit hash or tag) which should be fetched |
112+| `subpath` | A path within the grammar directory which should be built. Some grammar repositories host multiple grammars (for example `tree-sitter-typescript` and `tree-sitter-ocaml`) in subdirectories. This key is used to point `hx --grammar build` to the correct path for compilation. When omitted, the root of repository is used |
113+
114+### Choosing grammars
115+
45−You may use a top-level `use-grammars` key to control which grammars are fetched and built.
116+You may use a top-level `use-grammars` key to control which grammars are
117+fetched and built when using `hx --grammar fetch` and `hx --grammar build`.
46
118
47```toml
119```toml
48# Note: this key must come **before** the [[language]] and [[grammar]] sections
120# Note: this key must come **before** the [[language]] and [[grammar]] sections
@@ -52,3 +124,5 @@ use-grammars = { except = [ "yaml", "json" ] }
52```
124```
53
125
54When omitted, all grammars are fetched and built.
126When omitted, all grammars are fetched and built.
127+
128+[treesitter-language-injection]: https://tree-sitter.github.io/tree-sitter/syntax-highlighting#language-injection