aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-term/src/config.rs')
-rw-r--r--helix-term/src/config.rs71
1 files changed, 64 insertions, 7 deletions
diff --git a/helix-term/src/config.rs b/helix-term/src/config.rs
index 1c6289ec..06e44ad9 100644
--- a/helix-term/src/config.rs
+++ b/helix-term/src/config.rs
@@ -1,25 +1,71 @@
+use crate::keymap::{default::default, merge_keys, Keymap};
+use helix_view::document::Mode;
use serde::Deserialize;
+use std::collections::HashMap;
+use std::fmt::Display;
+use std::io::Error as IOError;
+use std::path::PathBuf;
+use toml::de::Error as TomlError;
-use crate::keymap::Keymaps;
-
-#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
+#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
pub theme: Option<String>,
#[serde(default)]
pub lsp: LspConfig,
- #[serde(default)]
- pub keys: Keymaps,
+ #[serde(default = "default")]
+ pub keys: HashMap<Mode, Keymap>,
#[serde(default)]
pub editor: helix_view::editor::Config,
}
+impl Default for Config {
+ fn default() -> Config {
+ Config {
+ theme: None,
+ lsp: LspConfig::default(),
+ keys: default(),
+ editor: helix_view::editor::Config::default(),
+ }
+ }
+}
+
+#[derive(Debug)]
+pub enum ConfigLoadError {
+ BadConfig(TomlError),
+ Error(IOError),
+}
+
+impl Display for ConfigLoadError {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ ConfigLoadError::BadConfig(err) => err.fmt(f),
+ ConfigLoadError::Error(err) => err.fmt(f),
+ }
+ }
+}
+
#[derive(Debug, Default, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LspConfig {
pub display_messages: bool,
}
+impl Config {
+ pub fn load(config_path: PathBuf) -> Result<Config, ConfigLoadError> {
+ match std::fs::read_to_string(config_path) {
+ Ok(config) => toml::from_str(&config)
+ .map(merge_keys)
+ .map_err(ConfigLoadError::BadConfig),
+ Err(err) => Err(ConfigLoadError::Error(err)),
+ }
+ }
+
+ pub fn load_default() -> Result<Config, ConfigLoadError> {
+ Config::load(helix_loader::config_file())
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -43,7 +89,7 @@ mod tests {
assert_eq!(
toml::from_str::<Config>(sample_keymaps).unwrap(),
Config {
- keys: Keymaps::new(hashmap! {
+ keys: hashmap! {
Mode::Insert => Keymap::new(keymap!({ "Insert mode"
"y" => move_line_down,
"S-C-a" => delete_selection,
@@ -51,9 +97,20 @@ mod tests {
Mode::Normal => Keymap::new(keymap!({ "Normal mode"
"A-F12" => move_next_word_end,
})),
- }),
+ },
..Default::default()
}
);
}
+
+ #[test]
+ fn keys_resolve_to_correct_defaults() {
+ // From serde default
+ let default_keys = toml::from_str::<Config>("").unwrap().keys;
+ assert_eq!(default_keys, default());
+
+ // From the Default trait
+ let default_keys = Config::default().keys;
+ assert_eq!(default_keys, default());
+ }
}