2023-01-24 20:07:01 +04:00
|
|
|
use std::str::from_utf8;
|
|
|
|
|
2022-04-18 08:12:47 +04:00
|
|
|
/// Default built-in languages.toml.
|
2022-04-18 07:10:51 +04:00
|
|
|
pub fn default_lang_config() -> toml::Value {
|
2023-01-24 20:07:01 +04:00
|
|
|
let default_config = include_bytes!("../../languages.toml");
|
|
|
|
toml::from_str(from_utf8(default_config).unwrap())
|
2022-04-18 08:12:47 +04:00
|
|
|
.expect("Could not parse built-in languages.toml to valid toml")
|
2022-04-18 07:10:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// User configured languages.toml file, merged with the default config.
|
|
|
|
pub fn user_lang_config() -> Result<toml::Value, toml::de::Error> {
|
2023-01-31 03:31:21 +04:00
|
|
|
let config = [crate::config_dir(), crate::find_workspace().join(".helix")]
|
2022-04-18 07:10:51 +04:00
|
|
|
.into_iter()
|
|
|
|
.map(|path| path.join("languages.toml"))
|
|
|
|
.filter_map(|file| {
|
2023-01-27 19:43:46 +04:00
|
|
|
std::fs::read_to_string(file)
|
2023-01-24 20:07:01 +04:00
|
|
|
.map(|config| toml::from_str(&config))
|
2022-04-18 07:10:51 +04:00
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?
|
|
|
|
.into_iter()
|
2023-01-31 03:31:21 +04:00
|
|
|
.fold(default_lang_config(), |a, b| {
|
2022-07-26 04:40:38 +04:00
|
|
|
// combines for example
|
|
|
|
// b:
|
|
|
|
// [[language]]
|
|
|
|
// name = "toml"
|
|
|
|
// language-server = { command = "taplo", args = ["lsp", "stdio"] }
|
|
|
|
//
|
|
|
|
// a:
|
|
|
|
// [[language]]
|
|
|
|
// language-server = { command = "/usr/bin/taplo" }
|
|
|
|
//
|
|
|
|
// into:
|
|
|
|
// [[language]]
|
|
|
|
// name = "toml"
|
|
|
|
// language-server = { command = "/usr/bin/taplo" }
|
|
|
|
//
|
|
|
|
// thus it overrides the third depth-level of b with values of a if they exist, but otherwise merges their values
|
2023-01-31 03:31:21 +04:00
|
|
|
crate::merge_toml_values(a, b, 3)
|
2022-04-18 07:10:51 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(config)
|
|
|
|
}
|