Skip to content

Commit 9b00960

Browse files
committed
simplify config parsing
move config from main() to Application::new(), and use fold() to merge toml values
1 parent ecf7598 commit 9b00960

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

helix-term/src/application.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,50 @@ fn load_lang_config(path: std::path::PathBuf) -> Result<toml::Value, Error> {
6060
}
6161

6262
impl Application {
63-
pub fn new(args: Args, mut config: Config) -> Result<Self, Error> {
63+
pub fn new(args: Args) -> Result<Self, Error> {
6464
use helix_view::editor::Action;
6565

6666
// These configuration directories can contain `config.toml` and `languages.toml`.
6767
// `local_config_dir` is a `.helix` folder within the projec directory.
6868
let config_dir = helix_core::config_dir();
6969
let local_config_dir = helix_core::local_config_dir();
7070
let cwd = std::env::current_dir().expect("unable to determine current directory");
71+
if !config_dir.exists() {
72+
std::fs::create_dir_all(&config_dir).ok();
73+
}
74+
75+
// `config.toml`
76+
let mut config = match std::fs::read_to_string(config_dir.join("config.toml")) {
77+
Ok(config) => toml::from_str(&config)
78+
.map(crate::keymap::merge_keys)
79+
.unwrap_or_else(|err| {
80+
eprintln!("Bad config: {}", err);
81+
eprintln!("Press <ENTER> to continue with default config");
82+
use std::io::Read;
83+
// This waits for an enter press.
84+
let _ = std::io::stdin().read(&mut []);
85+
Config::default()
86+
}),
87+
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(),
88+
Err(err) => return Err(Error::new(err)),
89+
};
7190

7291
// Config override order: local (cwd) -> local (root) -> global -> default.
7392
// Read and parse the `languages.toml` files as TOML objects.
7493
let default_lang_config: toml::Value =
7594
toml::from_slice(include_bytes!("../../languages.toml"))
7695
.expect("failed to read the default `languages.toml`");
77-
let lang_config = {
96+
let lang_config: toml::Value = {
7897
let local_cwd_config = load_lang_config(cwd.join(".helix/languages.toml"))?;
7998
let local_root_config = load_lang_config(local_config_dir.join("languages.toml"))?;
8099
let global_config = load_lang_config(config_dir.join("languages.toml"))?;
81-
82-
merge_toml_values(
100+
[
101+
local_root_config,
102+
global_config,
83103
default_lang_config.clone(),
84-
merge_toml_values(
85-
global_config,
86-
merge_toml_values(local_root_config, local_cwd_config),
87-
),
88-
)
104+
]
105+
.into_iter()
106+
.fold(local_cwd_config, |a, b| merge_toml_values(b, a))
89107
};
90108

91109
// Convert previous `toml::Value`s into the config type.

helix-term/src/main.rs

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use anyhow::{Context, Error, Result};
1+
use anyhow::{Context, Result};
22
use helix_term::application::Application;
33
use helix_term::args::Args;
4-
use helix_term::config::Config;
5-
use helix_term::keymap::merge_keys;
64
use std::path::PathBuf;
75

86
fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> {
@@ -78,40 +76,15 @@ FLAGS:
7876
if args.display_help {
7977
print!("{}", help);
8078
std::process::exit(0);
81-
}
82-
83-
if args.display_version {
79+
} else if args.display_version {
8480
println!("helix {}", env!("VERSION_AND_GIT_HASH"));
8581
std::process::exit(0);
8682
}
8783

88-
let config_dir = helix_core::config_dir();
89-
if !config_dir.exists() {
90-
std::fs::create_dir_all(&config_dir).ok();
91-
}
92-
93-
// `config.toml`
94-
let config = match std::fs::read_to_string(config_dir.join("config.toml")) {
95-
Ok(config) => toml::from_str(&config)
96-
.map(merge_keys)
97-
.unwrap_or_else(|err| {
98-
eprintln!("Bad config: {}", err);
99-
eprintln!("Press <ENTER> to continue with default config");
100-
use std::io::Read;
101-
// This waits for an enter press.
102-
let _ = std::io::stdin().read(&mut []);
103-
Config::default()
104-
}),
105-
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(),
106-
Err(err) => return Err(Error::new(err)),
107-
};
108-
10984
setup_logging(logpath, args.verbosity).context("failed to initialize logging")?;
11085

11186
// TODO: use the thread local executor to spawn the application task separately from the work pool
112-
let mut app = Application::new(args, config).context("unable to create new application")?;
113-
87+
let mut app = Application::new(args).context("unable to create new application")?;
11488
let exit_code = app.run().await?;
115-
11689
Ok(exit_code)
11790
}

0 commit comments

Comments
 (0)