on master help sign in
add 'use-grammars' to languages.toml
08ee949Michael Davis
book/src/languages.md+10−1
@@ -28,4 +28,13 @@ name = "c"
28source = { path = "/path/to/tree-sitter-c" }
28source = { path = "/path/to/tree-sitter-c" }
29```
29```
30
30
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
445 auto_pairs: None,
445 auto_pairs: None,
446 }],
446 }],
447 grammar: vec![],
447 grammar: vec![],
448+ grammar_selection: None,
448 });
449 });
449
450
450 // set runtime path so we can find the queries
451 // set runtime path so we can find the queries
helix-core/src/syntax.rs+11−1
@@ -81,12 +81,21 @@ where
81}
81}
82
82
83#[derive(Debug, Serialize, Deserialize)]
83#[derive(Debug, Serialize, Deserialize)]
84−#[serde(deny_unknown_fields)]
84+#[serde(rename_all = "kebab-case", deny_unknown_fields)]
85pub struct Configuration {
85pub struct Configuration {
86+ #[serde(rename = "use-grammars")]
87+ pub grammar_selection: Option<GrammarSelection>,
86 pub language: Vec<LanguageConfiguration>,
88 pub language: Vec<LanguageConfiguration>,
87 pub grammar: Vec<GrammarConfiguration>,
89 pub grammar: Vec<GrammarConfiguration>,
88}
90}
89
91
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+
90// largely based on tree-sitter/cli/src/loader.rs
99// largely based on tree-sitter/cli/src/loader.rs
91#[derive(Debug, Serialize, Deserialize)]
100#[derive(Debug, Serialize, Deserialize)]
92#[serde(rename_all = "kebab-case", deny_unknown_fields)]
101#[serde(rename_all = "kebab-case", deny_unknown_fields)]
@@ -2110,6 +2119,7 @@ mod test {
2110 let loader = Loader::new(Configuration {
2119 let loader = Loader::new(Configuration {
2111 language: vec![],
2120 language: vec![],
2112 grammar: vec![],
2121 grammar: vec![],
2122+ grammar_selection: None,
2113 });
2123 });
2114
2124
2115 let language = get_language(&crate::RUNTIME_DIR, "Rust").unwrap();
2125 let language = get_language(&crate::RUNTIME_DIR, "Rust").unwrap();
helix-term/src/grammars.rs+14−2
@@ -7,7 +7,7 @@ use std::{
7 sync::mpsc::channel,
7 sync::mpsc::channel,
8};
8};
9
9
10−use helix_core::syntax::{GrammarConfiguration, GrammarSource, DYLIB_EXTENSION};
10+use helix_core::syntax::{GrammarConfiguration, GrammarSelection, GrammarSource, DYLIB_EXTENSION};
11
11
12const BUILD_TARGET: &str = env!("BUILD_TARGET");
12const BUILD_TARGET: &str = env!("BUILD_TARGET");
13const REMOTE_NAME: &str = "origin";
13const REMOTE_NAME: &str = "origin";
@@ -163,7 +163,19 @@ fn build_grammar(grammar: GrammarConfiguration) -> Result<()> {
163fn get_grammar_configs() -> Vec<GrammarConfiguration> {
163fn get_grammar_configs() -> Vec<GrammarConfiguration> {
164 let config = helix_core::config::user_syntax_loader().expect("Could not parse languages.toml");
164 let config = helix_core::config::user_syntax_loader().expect("Could not parse languages.toml");
165
165
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(),
166− config.grammar
177+ None => config.grammar,
178+ }
167}
179}
168
180
169fn build_tree_sitter_library(src_path: &Path, grammar: GrammarConfiguration) -> Result<()> {
181fn build_tree_sitter_library(src_path: &Path, grammar: GrammarConfiguration) -> Result<()> {