@@ -2,11 +2,28 @@ pub mod config;
22pub mod grammar;
33
44use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
5+use std::path::PathBuf;
56
6−pub static RUNTIME_DIR: once_cell::sync::Lazy<std::path::PathBuf> =
7− once_cell::sync::Lazy::new(runtime_dir);
7+pub static RUNTIME_DIR: once_cell::sync::Lazy<PathBuf> = once_cell::sync::Lazy::new(runtime_dir);
88
9−pub fn runtime_dir() -> std::path::PathBuf {
9+static CONFIG_FILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
10+
11+pub fn initialize_config_file(specified_file: Option<PathBuf>) {
12+ let config_file = specified_file.unwrap_or_else(|| {
13+ let config_dir = config_dir();
14+
15+ if !config_dir.exists() {
16+ std::fs::create_dir_all(&config_dir).ok();
17+ }
18+
19+ config_dir.join("config.toml")
20+ });
21+
22+ // We should only initialize this value once.
23+ CONFIG_FILE.set(config_file).ok();
24+}
25+
26+pub fn runtime_dir() -> PathBuf {
1027 if let Ok(dir) = std::env::var("HELIX_RUNTIME") {
1128 return dir.into();
1229 }
@@ -31,7 +48,7 @@ pub fn runtime_dir() -> std::path::PathBuf {
3148 .unwrap()
3249}
3350
34−pub fn config_dir() -> std::path::PathBuf {
51+pub fn config_dir() -> PathBuf {
3552 // TODO: allow env var override
3653 let strategy = choose_base_strategy().expect("Unable to find the config directory!");
3754 let mut path = strategy.config_dir();
@@ -39,7 +56,7 @@ pub fn config_dir() -> std::path::PathBuf {
3956 path
4057}
4158
42−pub fn local_config_dirs() -> Vec<std::path::PathBuf> {
59+pub fn local_config_dirs() -> Vec<PathBuf> {
4360 let directories = find_root_impl(None, &[".helix".to_string()])
4461 .into_iter()
4562 .map(|path| path.join(".helix"))
@@ -48,7 +65,7 @@ pub fn local_config_dirs() -> Vec<std::path::PathBuf> {
4865 directories
4966}
5067
51−pub fn cache_dir() -> std::path::PathBuf {
68+pub fn cache_dir() -> PathBuf {
5269 // TODO: allow env var override
5370 let strategy = choose_base_strategy().expect("Unable to find the config directory!");
5471 let mut path = strategy.cache_dir();
@@ -56,19 +73,22 @@ pub fn cache_dir() -> std::path::PathBuf {
5673 path
5774}
5875
59−pub fn config_file() -> std::path::PathBuf {
60− config_dir().join("config.toml")
76+pub fn config_file() -> PathBuf {
77+ CONFIG_FILE
78+ .get()
79+ .map(|path| path.to_path_buf())
80+ .unwrap_or_else(|| config_dir().join("config.toml"))
6181}
6282
63−pub fn lang_config_file() -> std::path::PathBuf {
83+pub fn lang_config_file() -> PathBuf {
6484 config_dir().join("languages.toml")
6585}
6686
67−pub fn log_file() -> std::path::PathBuf {
87+pub fn log_file() -> PathBuf {
6888 cache_dir().join("helix.log")
6989}
7090
71−pub fn find_root_impl(root: Option<&str>, root_markers: &[String]) -> Vec<std::path::PathBuf> {
91+pub fn find_root_impl(root: Option<&str>, root_markers: &[String]) -> Vec<PathBuf> {
7292 let current_dir = std::env::current_dir().expect("unable to determine current directory");
7393 let mut directories = Vec::new();
7494