Skip to content

Expanding envs in language config #13524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion helix-loader/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn user_lang_config() -> Result<toml::Value, toml::de::Error> {
.map(|path| path.join("languages.toml"))
.filter_map(|file| {
std::fs::read_to_string(file)
.map(|config| toml::from_str(&config))
.map(|config| crate::get_env_expanded_toml(&config))
.ok()
})
.collect::<Result<Vec<_>, _>>()?
Expand Down
52 changes: 51 additions & 1 deletion helix-loader/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod config;
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 std::path::{Path, PathBuf};
Expand Down Expand Up @@ -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)]
mod merge_toml_tests {
use std::str;
Expand Down Expand Up @@ -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()
);
}
}