diff --git a/book/src/languages.md b/book/src/languages.md index c5a7708faef5e..3372a12020181 100644 --- a/book/src/languages.md +++ b/book/src/languages.md @@ -28,4 +28,13 @@ name = "c" source = { path = "/path/to/tree-sitter-c" } ``` -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. +You may use a top-level `use-grammars` key to control which grammars are fetched and built. + +```toml +# Note: this key must come **before** the [[language]] and [[grammar]] sections +use-grammars = { only = [ "rust", "c", "cpp" ] } +# or +use-grammars = { except = [ "yaml", "json" ] } +``` + +When omitted, all grammars are fetched and built. diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index c4f4e28b88e95..0ba08d1f75618 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -444,6 +444,7 @@ where debugger: None, }], grammar: vec![], + grammar_selection: None, }); // set runtime path so we can find the queries diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 1e6d8f3750d48..38567ad08d265 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -72,12 +72,21 @@ where } #[derive(Debug, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] pub struct Configuration { + #[serde(rename = "use-grammars")] + pub grammar_selection: Option, pub language: Vec, pub grammar: Vec, } +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "lowercase", untagged)] +pub enum GrammarSelection { + Only(HashSet), + Except(HashSet), +} + // largely based on tree-sitter/cli/src/loader.rs #[derive(Debug, Serialize, Deserialize)] #[serde(rename_all = "kebab-case", deny_unknown_fields)] @@ -1930,6 +1939,7 @@ mod test { let loader = Loader::new(Configuration { language: vec![], grammar: vec![], + grammar_selection: None, }); let language = get_language(&crate::RUNTIME_DIR, "Rust").unwrap(); diff --git a/helix-term/src/grammars.rs b/helix-term/src/grammars.rs index ec36dc63f713f..d89a5336bdf26 100644 --- a/helix-term/src/grammars.rs +++ b/helix-term/src/grammars.rs @@ -7,7 +7,7 @@ use std::{ sync::mpsc::channel, }; -use helix_core::syntax::{GrammarConfiguration, GrammarSource, DYLIB_EXTENSION}; +use helix_core::syntax::{GrammarConfiguration, GrammarSelection, GrammarSource, DYLIB_EXTENSION}; const BUILD_TARGET: &str = env!("BUILD_TARGET"); const REMOTE_NAME: &str = "helix-origin"; @@ -162,11 +162,22 @@ fn build_grammar(grammar: GrammarConfiguration) { // Returns the user-defined set of grammars if the user has a languages.toml, // defaulting to the built-in languages.toml entries. fn get_grammar_configs() -> Vec { - let config = helix_core::config::user_syntax_loader() - .map(|config| config.expect("Could not parse user-defined languages.toml")) - .unwrap_or_else(helix_core::config::default_syntax_loader); - - config.grammar + let config = + helix_core::config::merged_syntax_loader().expect("Could not parse languages.toml"); + + match config.grammar_selection { + Some(GrammarSelection::Only(selections)) => config + .grammar + .into_iter() + .filter(|grammar| selections.contains(&grammar.grammar_id)) + .collect(), + Some(GrammarSelection::Except(rejections)) => config + .grammar + .into_iter() + .filter(|grammar| !rejections.contains(&grammar.grammar_id)) + .collect(), + None => config.grammar, + } } fn build_library(src_path: &Path, grammar: GrammarConfiguration) -> Result<()> {