on master help sign in
add 'use-grammars' to languages.toml

The vision with 'use-grammars' is to allow the long-requested feature of being able to declare your own set of grammars that you would like. A simple schema with only/except grammar names controls the list of grammars that is fetched and built. It does not (yet) control which grammars may be loaded at runtime if they already exist.

commit08ee949dcb904dc27aa41a62ad686c14c0a406bb authorMichael Davis <mcarsondavis@gmail.com> dateMon 14 Feb 2022, 23:23 (5 years ago) parentdb3470d
4 files changed+36−4
book/src/languages.md+10−1 helix-core/src/indent.rs+1 helix-core/src/syntax.rs+11−1 helix-term/src/grammars.rs+14−2
book/src/languages.md+10−1
@@ -28,4 +28,13 @@ name = "c"
2828source = { path = "/path/to/tree-sitter-c" }
2929```
3030
31−If a user has a `languages.toml`, only the grammars in that `languages.toml` are evaluated when running `hx --fetch-grammars` and `hx --build-grammars`. Otherwise the [default `languages.toml`](https://github.com/helix-editor/helix/blob/master/languages.toml) is used.
31+You may use a top-level `use-grammars` key to control which grammars are fetched and built.
32+
33+```toml
34+# Note: this key must come **before** the [[language]] and [[grammar]] sections
35+use-grammars = { only = [ "rust", "c", "cpp" ] }
36+# or
37+use-grammars = { except = [ "yaml", "json" ] }
38+```
39+
40+When omitted, all grammars are fetched and built.
helix-core/src/indent.rs+1
@@ -445,6 +445,7 @@ where
445445 auto_pairs: None,
446446 }],
447447 grammar: vec![],
448+ grammar_selection: None,
448449 });
449450
450451 // set runtime path so we can find the queries
helix-core/src/syntax.rs+11−1
@@ -81,12 +81,21 @@ where
8181}
8282
8383#[derive(Debug, Serialize, Deserialize)]
84−#[serde(deny_unknown_fields)]
84+#[serde(rename_all = "kebab-case", deny_unknown_fields)]
8585pub struct Configuration {
86+ #[serde(rename = "use-grammars")]
87+ pub grammar_selection: Option<GrammarSelection>,
8688 pub language: Vec<LanguageConfiguration>,
8789 pub grammar: Vec<GrammarConfiguration>,
8890}
8991
92+#[derive(Debug, Serialize, Deserialize)]
93+#[serde(rename_all = "lowercase", untagged)]
94+pub enum GrammarSelection {
95+ Only(HashSet<String>),
96+ Except(HashSet<String>),
97+}
98+
9099// largely based on tree-sitter/cli/src/loader.rs
91100#[derive(Debug, Serialize, Deserialize)]
92101#[serde(rename_all = "kebab-case", deny_unknown_fields)]
@@ -2110,6 +2119,7 @@ mod test {
21102119 let loader = Loader::new(Configuration {
21112120 language: vec![],
21122121 grammar: vec![],
2122+ grammar_selection: None,
21132123 });
21142124
21152125 let language = get_language(&crate::RUNTIME_DIR, "Rust").unwrap();
helix-term/src/grammars.rs+14−2
@@ -7,7 +7,7 @@ use std::{
77 sync::mpsc::channel,
88};
99
10−use helix_core::syntax::{GrammarConfiguration, GrammarSource, DYLIB_EXTENSION};
10+use helix_core::syntax::{GrammarConfiguration, GrammarSelection, GrammarSource, DYLIB_EXTENSION};
1111
1212const BUILD_TARGET: &str = env!("BUILD_TARGET");
1313const REMOTE_NAME: &str = "origin";
@@ -163,7 +163,19 @@ fn build_grammar(grammar: GrammarConfiguration) -> Result<()> {
163163fn get_grammar_configs() -> Vec<GrammarConfiguration> {
164164 let config = helix_core::config::user_syntax_loader().expect("Could not parse languages.toml");
165165
166− config.grammar
166+ match config.grammar_selection {
167+ Some(GrammarSelection::Only(selections)) => config
168+ .grammar
169+ .into_iter()
170+ .filter(|grammar| selections.contains(&grammar.grammar_id))
171+ .collect(),
172+ Some(GrammarSelection::Except(rejections)) => config
173+ .grammar
174+ .into_iter()
175+ .filter(|grammar| !rejections.contains(&grammar.grammar_id))
176+ .collect(),
177+ None => config.grammar,
178+ }
167179}
168180
169181fn build_tree_sitter_library(src_path: &Path, grammar: GrammarConfiguration) -> Result<()> {