Skip to content

Commit

Permalink
undo changes to heilx-core config
Browse files Browse the repository at this point in the history
With the addition of the `use-grammars` config, we no longer need
custom (lack of) merging behavior: we simply request "the config"
(which is merged) and use that.
  • Loading branch information
the-mikedavis committed Feb 15, 2022
1 parent 1750420 commit 9ae22c3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
37 changes: 13 additions & 24 deletions helix-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,28 @@ pub fn default_lang_config() -> toml::Value {
.expect("Could not parse bultin-in languages.toml to valid toml")
}

/// User-only configured languages.toml, or None if the user does not define one.
pub fn user_lang_config() -> Option<Result<toml::Value, toml::de::Error>> {
match std::fs::read(crate::config_dir().join("languages.toml")) {
Ok(raw) => Some(toml::from_slice(&raw)),
Err(_) => None,
}
}

/// User configured languages.toml file, merged with the default config.
pub fn merged_lang_config() -> Result<toml::Value, toml::de::Error> {
pub fn user_lang_config() -> Result<toml::Value, toml::de::Error> {
let def_lang_conf = default_lang_config();
let merged_lang_conf = match user_lang_config() {
Some(toml) => merge_toml_values(def_lang_conf, toml?),
None => def_lang_conf,
let data = std::fs::read(crate::config_dir().join("languages.toml"));
let user_lang_conf = match data {
Ok(raw) => {
let value = toml::from_slice(&raw)?;
merge_toml_values(def_lang_conf, value)
}
Err(_) => def_lang_conf,
};

Ok(merged_lang_conf)
Ok(user_lang_conf)
}

/// Syntax configuration loader based on built-in languages.toml.
pub fn default_syntax_loader() -> crate::syntax::Configuration {
default_lang_config()
.try_into()
.expect("Could not serialize built-in languages.toml")
.expect("Could not serialize built-in language.toml")
}

/// Syntax configuration loader based only on user configured languages.toml.
pub fn user_syntax_loader() -> Option<Result<crate::syntax::Configuration, toml::de::Error>> {
user_lang_config().map(|config| config?.try_into())
}

/// Syntax configuration loader based on user configured languages.toml merged
/// with the default languages.toml
pub fn merged_syntax_loader() -> Result<crate::syntax::Configuration, toml::de::Error> {
merged_lang_config()?.try_into()
/// Syntax configuration loader based on user configured languages.toml.
pub fn user_syntax_loader() -> Result<crate::syntax::Configuration, toml::de::Error> {
user_lang_config()?.try_into()
}
4 changes: 2 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use helix_core::{
config::{default_syntax_loader, merged_syntax_loader},
config::{default_syntax_loader, user_syntax_loader},
pos_at_coords, syntax, Selection,
};
use helix_dap::{self as dap, Payload, Request};
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Application {
}
});

let syn_loader_conf = merged_syntax_loader().unwrap_or_else(|err| {
let syn_loader_conf = user_syntax_loader().unwrap_or_else(|err| {
eprintln!("Bad language config: {}", err);
eprintln!("Press <ENTER> to continue with default language config");
use std::io::Read;
Expand Down
9 changes: 5 additions & 4 deletions helix-term/src/grammars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ fn build_grammar(grammar: GrammarConfiguration) {
build_library(&path, grammar).unwrap();
}

// Returns the user-defined set of grammars if the user has a languages.toml,
// defaulting to the built-in languages.toml entries.
// Returns the set of grammar configurations the user requests.
// Grammars are configured in the default and user `languages.toml` and are
// merged. The `grammar_selection` key of the config is then used to filter
// down all grammars into a subset of the user's choosing.
fn get_grammar_configs() -> Vec<GrammarConfiguration> {
let config =
helix_core::config::merged_syntax_loader().expect("Could not parse languages.toml");
let config = helix_core::config::user_syntax_loader().expect("Could not parse languages.toml");

match config.grammar_selection {
Some(GrammarSelection::Only(selections)) => config
Expand Down

0 comments on commit 9ae22c3

Please sign in to comment.