Skip to content

Commit

Permalink
add 'use-grammars' to languages.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
the-mikedavis committed Feb 15, 2022
1 parent fc7f8cb commit 3985b72
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
11 changes: 10 additions & 1 deletion book/src/languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions helix-core/src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ where
debugger: None,
}],
grammar: vec![],
grammar_selection: None,
});

// set runtime path so we can find the queries
Expand Down
12 changes: 11 additions & 1 deletion helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GrammarSelection>,
pub language: Vec<LanguageConfiguration>,
pub grammar: Vec<GrammarConfiguration>,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase", untagged)]
pub enum GrammarSelection {
Only(HashSet<String>),
Except(HashSet<String>),
}

// largely based on tree-sitter/cli/src/loader.rs
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 17 additions & 6 deletions helix-term/src/grammars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<GrammarConfiguration> {
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<()> {
Expand Down

0 comments on commit 3985b72

Please sign in to comment.