Skip to content

Commit

Permalink
simplify config parsing
Browse files Browse the repository at this point in the history
move config from main() to Application::new(), and use fold() to merge toml values
  • Loading branch information
kirawi committed Dec 18, 2021
1 parent ecf7598 commit 9b00960
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 39 deletions.
36 changes: 27 additions & 9 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,50 @@ fn load_lang_config(path: std::path::PathBuf) -> Result<toml::Value, Error> {
}

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

// These configuration directories can contain `config.toml` and `languages.toml`.
// `local_config_dir` is a `.helix` folder within the projec directory.
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 <ENTER> 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.
Expand Down
33 changes: 3 additions & 30 deletions helix-term/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<()> {
Expand Down Expand Up @@ -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 <ENTER> 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)
}

0 comments on commit 9b00960

Please sign in to comment.