pull/13524/merge
KJ 2025-06-15 22:17:06 +02:00 committed by GitHub
commit 7f22dfe245
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 2 deletions

View File

@ -17,7 +17,7 @@ pub fn user_lang_config() -> Result<toml::Value, toml::de::Error> {
.map(|path| path.join("languages.toml")) .map(|path| path.join("languages.toml"))
.filter_map(|file| { .filter_map(|file| {
std::fs::read_to_string(file) std::fs::read_to_string(file)
.map(|config| toml::from_str(&config)) .map(|config| crate::get_env_expanded_toml(&config))
.ok() .ok()
}) })
.collect::<Result<Vec<_>, _>>()? .collect::<Result<Vec<_>, _>>()?

View File

@ -1,7 +1,7 @@
pub mod config; pub mod config;
pub mod grammar; pub mod grammar;
use helix_stdx::{env::current_working_dir, path}; use helix_stdx::{env::current_working_dir, path, env::expand};
use etcetera::base_strategy::{choose_base_strategy, BaseStrategy}; use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
@ -269,6 +269,16 @@ fn ensure_parent_dir(path: &Path) {
} }
} }
pub fn get_env_expanded_toml<T>(toml_str: &str) -> Result<T, toml::de::Error>
where
T: serde::de::DeserializeOwned
{
let expanded = expand(toml_str);
let expanded_str = expanded.to_string_lossy();
toml::from_str(&expanded_str)
}
#[cfg(test)] #[cfg(test)]
mod merge_toml_tests { mod merge_toml_tests {
use std::str; use std::str;
@ -341,3 +351,43 @@ mod merge_toml_tests {
) )
} }
} }
#[cfg(test)]
mod env_expander_tests {
use toml::Value;
use std::env;
use super::get_env_expanded_toml;
#[test]
fn expand_envs_and_parse_toml() {
let toml_text = r#"
title = "My App"
dir = "${HOME}/project/source"
user = "${USER}"
config = "${CONFIG_DIR}"
undefined = "${UNDEFINED}"
"#;
env::set_var("HOME", "/home/example");
env::set_var("USER", "example_user");
env::set_var("CONFIG_DIR", "/etc/myapp");
let parsed: Value = get_env_expanded_toml(toml_text).unwrap();
assert_eq!(
parsed["dir"],
"/home/example/project/source".into()
);
assert_eq!(
parsed["user"],
"example_user".into()
);
assert_eq!(
parsed["config"],
"/etc/myapp".into()
);
assert_eq!(
parsed["undefined"],
"".into()
);
}
}