diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 0f5892e979729..f58dfde388067 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -60,7 +60,7 @@ fn load_lang_config(path: std::path::PathBuf) -> Result { } impl Application { - pub fn new(args: Args, mut config: Config) -> Result { + pub fn new(args: Args) -> Result { use helix_view::editor::Action; // These configuration directories can contain `config.toml` and `languages.toml`. @@ -68,24 +68,42 @@ impl Application { let config_dir = helix_core::config_dir(); let local_config_dir = helix_core::local_config_dir(); let cwd = std::env::current_dir().expect("unable to determine current directory"); + if !config_dir.exists() { + std::fs::create_dir_all(&config_dir).ok(); + } + + // `config.toml` + let mut config = match std::fs::read_to_string(config_dir.join("config.toml")) { + Ok(config) => toml::from_str(&config) + .map(crate::keymap::merge_keys) + .unwrap_or_else(|err| { + eprintln!("Bad config: {}", err); + eprintln!("Press to continue with default config"); + use std::io::Read; + // This waits for an enter press. + let _ = std::io::stdin().read(&mut []); + Config::default() + }), + Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(), + Err(err) => return Err(Error::new(err)), + }; // Config override order: local (cwd) -> local (root) -> global -> default. // Read and parse the `languages.toml` files as TOML objects. let default_lang_config: toml::Value = toml::from_slice(include_bytes!("../../languages.toml")) .expect("failed to read the default `languages.toml`"); - let lang_config = { + let lang_config: toml::Value = { let local_cwd_config = load_lang_config(cwd.join(".helix/languages.toml"))?; let local_root_config = load_lang_config(local_config_dir.join("languages.toml"))?; let global_config = load_lang_config(config_dir.join("languages.toml"))?; - - merge_toml_values( + [ + local_root_config, + global_config, default_lang_config.clone(), - merge_toml_values( - global_config, - merge_toml_values(local_root_config, local_cwd_config), - ), - ) + ] + .into_iter() + .fold(local_cwd_config, |a, b| merge_toml_values(b, a)) }; // Convert previous `toml::Value`s into the config type. diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 685b0d3818a06..6c3b9a4ba7f3e 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -1,8 +1,6 @@ -use anyhow::{Context, Error, Result}; +use anyhow::{Context, Result}; use helix_term::application::Application; use helix_term::args::Args; -use helix_term::config::Config; -use helix_term::keymap::merge_keys; use std::path::PathBuf; fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> { @@ -78,40 +76,15 @@ FLAGS: if args.display_help { print!("{}", help); std::process::exit(0); - } - - if args.display_version { + } else if args.display_version { println!("helix {}", env!("VERSION_AND_GIT_HASH")); std::process::exit(0); } - let config_dir = helix_core::config_dir(); - if !config_dir.exists() { - std::fs::create_dir_all(&config_dir).ok(); - } - - // `config.toml` - let config = match std::fs::read_to_string(config_dir.join("config.toml")) { - Ok(config) => toml::from_str(&config) - .map(merge_keys) - .unwrap_or_else(|err| { - eprintln!("Bad config: {}", err); - eprintln!("Press to continue with default config"); - use std::io::Read; - // This waits for an enter press. - let _ = std::io::stdin().read(&mut []); - Config::default() - }), - Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(), - Err(err) => return Err(Error::new(err)), - }; - setup_logging(logpath, args.verbosity).context("failed to initialize logging")?; // TODO: use the thread local executor to spawn the application task separately from the work pool - let mut app = Application::new(args, config).context("unable to create new application")?; - + let mut app = Application::new(args).context("unable to create new application")?; let exit_code = app.run().await?; - Ok(exit_code) }