From ba65720f71b871f9df5d4ced5b0485a2cac45cbd Mon Sep 17 00:00:00 2001 From: A-Walrus Date: Sat, 3 Sep 2022 16:26:09 +0300 Subject: [PATCH 1/3] Don't change config to default when refreshing invalid config --- helix-term/src/application.rs | 42 ++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 9e79e7c96037..d370ce981526 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -367,28 +367,30 @@ impl Application { } fn refresh_config(&mut self) { - let config = Config::load_default().unwrap_or_else(|err| { - self.editor.set_error(err.to_string()); - Config::default() - }); + match Config::load_default() { + Ok(config) => { + // Refresh theme + if let Some(theme) = config.theme.clone() { + let true_color = self.true_color(); + self.editor.set_theme( + self.theme_loader + .load(&theme) + .map_err(|e| { + log::warn!("failed to load theme `{}` - {}", theme, e); + e + }) + .ok() + .filter(|theme| (true_color || theme.is_16_color())) + .unwrap_or_else(|| self.theme_loader.default_theme(true_color)), + ); + } - // Refresh theme - if let Some(theme) = config.theme.clone() { - let true_color = self.true_color(); - self.editor.set_theme( - self.theme_loader - .load(&theme) - .map_err(|e| { - log::warn!("failed to load theme `{}` - {}", theme, e); - e - }) - .ok() - .filter(|theme| (true_color || theme.is_16_color())) - .unwrap_or_else(|| self.theme_loader.default_theme(true_color)), - ); + self.config.store(Arc::new(config)); + } + Err(err) => { + self.editor.set_error(err.to_string()); + } } - - self.config.store(Arc::new(config)); } fn true_color(&self) -> bool { From 301f96947cdd6b012f838fca2ef6b891155268fb Mon Sep 17 00:00:00 2001 From: A-Walrus Date: Sat, 3 Sep 2022 16:36:16 +0300 Subject: [PATCH 2/3] Propely handle theme errors with config-reload --- helix-term/src/application.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index d370ce981526..94b8691cc01f 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -372,19 +372,24 @@ impl Application { // Refresh theme if let Some(theme) = config.theme.clone() { let true_color = self.true_color(); - self.editor.set_theme( - self.theme_loader - .load(&theme) - .map_err(|e| { - log::warn!("failed to load theme `{}` - {}", theme, e); - e - }) - .ok() - .filter(|theme| (true_color || theme.is_16_color())) - .unwrap_or_else(|| self.theme_loader.default_theme(true_color)), - ); + match self.theme_loader.load(&theme) { + Ok(theme) => { + if true_color || theme.is_16_color() { + self.editor.set_theme(theme); + } else { + self.editor.set_error( + "theme requires truecolor support, which is not available", + ); + } + } + Err(err) => { + let err_string = format!("failed to load theme `{}` - {}", theme, err); + self.editor.set_error(err_string); + } + } } + // Refresh config self.config.store(Arc::new(config)); } Err(err) => { From 2f93202806c2b922bc9211b7d4372cf318b8f938 Mon Sep 17 00:00:00 2001 From: A-Walrus Date: Tue, 6 Sep 2022 16:24:26 +0300 Subject: [PATCH 3/3] Extract refresh theme into seperate function --- helix-term/src/application.rs | 43 +++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 94b8691cc01f..15fb8156d3ec 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -366,30 +366,33 @@ impl Application { self.editor.refresh_config(); } + /// Refresh theme after config change + fn refresh_theme(&mut self, config: &Config) { + if let Some(theme) = config.theme.clone() { + let true_color = self.true_color(); + match self.theme_loader.load(&theme) { + Ok(theme) => { + if true_color || theme.is_16_color() { + self.editor.set_theme(theme); + } else { + self.editor + .set_error("theme requires truecolor support, which is not available"); + } + } + Err(err) => { + let err_string = format!("failed to load theme `{}` - {}", theme, err); + self.editor.set_error(err_string); + } + } + } + } + fn refresh_config(&mut self) { match Config::load_default() { Ok(config) => { - // Refresh theme - if let Some(theme) = config.theme.clone() { - let true_color = self.true_color(); - match self.theme_loader.load(&theme) { - Ok(theme) => { - if true_color || theme.is_16_color() { - self.editor.set_theme(theme); - } else { - self.editor.set_error( - "theme requires truecolor support, which is not available", - ); - } - } - Err(err) => { - let err_string = format!("failed to load theme `{}` - {}", theme, err); - self.editor.set_error(err_string); - } - } - } + self.refresh_theme(&config); - // Refresh config + // Store new config self.config.store(Arc::new(config)); } Err(err) => {