From 44514354d801664409c8ae053123730682ce0f30 Mon Sep 17 00:00:00 2001 From: willful759 Date: Tue, 20 Dec 2022 23:17:14 -0600 Subject: [PATCH 1/5] language config reloading added --- helix-term/src/application.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 7a50e007b9b9..d870fc71e9af 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -393,12 +393,29 @@ impl Application { self.editor.refresh_config(); } + /// refresh language config after config change + fn refresh_language(&mut self) { + match helix_core::config::user_syntax_loader() { + Ok(syntax_config) => { + self.syn_loader = std::sync::Arc::new(syntax::Loader::new(syntax_config)); + for document in self.editor.documents.values_mut() { + document.detect_language(self.syn_loader.clone()); + } + } + Err(err) => { + let err_string = format!("failed to reload language config: {}", err); + self.editor.set_error(err_string); + } + } + } + /// 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) => { + self.refresh_language(); if true_color || theme.is_16_color() { self.editor.set_theme(theme); } else { From b323f5a820981ec8cab2057e1b9796fbf6f2ef6e Mon Sep 17 00:00:00 2001 From: willful759 Date: Wed, 21 Dec 2022 19:29:47 -0600 Subject: [PATCH 2/5] function `refresh_language` renamed to `refresh_language_config` --- helix-term/src/application.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index d870fc71e9af..f106718da818 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -394,7 +394,7 @@ impl Application { } /// refresh language config after config change - fn refresh_language(&mut self) { + fn refresh_language_config(&mut self) { match helix_core::config::user_syntax_loader() { Ok(syntax_config) => { self.syn_loader = std::sync::Arc::new(syntax::Loader::new(syntax_config)); @@ -415,7 +415,7 @@ impl Application { let true_color = self.true_color(); match self.theme_loader.load(&theme) { Ok(theme) => { - self.refresh_language(); + self.refresh_language_config(); if true_color || theme.is_16_color() { self.editor.set_theme(theme); } else { From b0254d2808905d2f9f1833534839f42fcaa95f8b Mon Sep 17 00:00:00 2001 From: willful759 Date: Wed, 21 Dec 2022 19:36:55 -0600 Subject: [PATCH 3/5] `refresh_language_config` factored out of `refresh_theme` --- helix-term/src/application.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index f106718da818..2bca0c60f71f 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -415,7 +415,6 @@ impl Application { let true_color = self.true_color(); match self.theme_loader.load(&theme) { Ok(theme) => { - self.refresh_language_config(); if true_color || theme.is_16_color() { self.editor.set_theme(theme); } else { @@ -434,6 +433,7 @@ impl Application { fn refresh_config(&mut self) { match Config::load_default() { Ok(config) => { + self.refresh_language_config(); self.refresh_theme(&config); // Store new config From 4b16a7b535ed21b74a33291aea7c88d799b1d1ba Mon Sep 17 00:00:00 2001 From: willful759 Date: Tue, 27 Dec 2022 10:54:15 -0600 Subject: [PATCH 4/5] theme and language config reloading logic updated to use `Result` --- helix-term/src/application.rs | 68 +++++++++++++++++------------------ 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 2bca0c60f71f..09b07d2e41fd 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -394,54 +394,50 @@ impl Application { } /// refresh language config after config change - fn refresh_language_config(&mut self) { - match helix_core::config::user_syntax_loader() { - Ok(syntax_config) => { - self.syn_loader = std::sync::Arc::new(syntax::Loader::new(syntax_config)); - for document in self.editor.documents.values_mut() { - document.detect_language(self.syn_loader.clone()); - } - } - Err(err) => { - let err_string = format!("failed to reload language config: {}", err); - self.editor.set_error(err_string); - } + fn refresh_language_config(&mut self) -> Result<(), Error> { + let syntax_config = helix_core::config::user_syntax_loader() + .map_err(|err| anyhow::anyhow!("Failed to load language config: {}", err))?; + + self.syn_loader = std::sync::Arc::new(syntax::Loader::new(syntax_config)); + for document in self.editor.documents.values_mut() { + document.detect_language(self.syn_loader.clone()); } + + Ok(()) } /// Refresh theme after config change - fn refresh_theme(&mut self, config: &Config) { + fn refresh_theme(&mut self, config: &Config) -> Result<(), Error> { 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); - } + let theme = self + .theme_loader + .load(&theme) + .map_err(|err| anyhow::anyhow!("Failed to load theme `{}`: {}", theme, err))?; + + if true_color || theme.is_16_color() { + self.editor.set_theme(theme); + } else { + return Err(anyhow::anyhow!( + "theme requires truecolor support, which is not available" + )); } } + + Ok(()) } fn refresh_config(&mut self) { - match Config::load_default() { - Ok(config) => { - self.refresh_language_config(); - self.refresh_theme(&config); + let mut refresh_config = || -> Result<(), Error> { + let default_config = Config::load_default() + .map_err(|err| anyhow::anyhow!("Failed to load config: {}", err))?; + self.refresh_language_config()?; + self.refresh_theme(&default_config)?; + Ok(()) + }; - // Store new config - self.config.store(Arc::new(config)); - } - Err(err) => { - self.editor.set_error(err.to_string()); - } + if let Err(err) = refresh_config() { + self.editor.set_error(err.to_string()); } } From 523b83052a58643f79a7238afdcfe00a61dfd809 Mon Sep 17 00:00:00 2001 From: willful759 Date: Wed, 28 Dec 2022 10:17:37 -0600 Subject: [PATCH 5/5] truecolor support error message refactored to use `anyhow::bail` --- helix-term/src/application.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 09b07d2e41fd..2786dc658376 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -418,9 +418,7 @@ impl Application { if true_color || theme.is_16_color() { self.editor.set_theme(theme); } else { - return Err(anyhow::anyhow!( - "theme requires truecolor support, which is not available" - )); + anyhow::bail!("theme requires truecolor support, which is not available") } }