Reload language config with `:config-reload` (#5239)

pull/5341/head
willful759 2 years ago committed by GitHub
parent 3fe3f2c4ee
commit 9d15b85209
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -397,40 +397,51 @@ impl Application {
self.editor.refresh_config(); self.editor.refresh_config();
} }
/// refresh language config after config change
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 /// 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() { if let Some(theme) = config.theme.clone() {
let true_color = self.true_color(); let true_color = self.true_color();
match self.theme_loader.load(&theme) { let theme = self
Ok(theme) => { .theme_loader
.load(&theme)
.map_err(|err| anyhow::anyhow!("Failed to load theme `{}`: {}", theme, err))?;
if true_color || theme.is_16_color() { if true_color || theme.is_16_color() {
self.editor.set_theme(theme); self.editor.set_theme(theme);
} else { } else {
self.editor anyhow::bail!("theme requires truecolor support, which is not available")
.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);
}
} }
} }
Ok(())
} }
fn refresh_config(&mut self) { fn refresh_config(&mut self) {
match Config::load_default() { let mut refresh_config = || -> Result<(), Error> {
Ok(config) => { let default_config = Config::load_default()
self.refresh_theme(&config); .map_err(|err| anyhow::anyhow!("Failed to load config: {}", err))?;
self.refresh_language_config()?;
self.refresh_theme(&default_config)?;
Ok(())
};
// Store new config if let Err(err) = refresh_config() {
self.config.store(Arc::new(config));
}
Err(err) => {
self.editor.set_error(err.to_string()); self.editor.set_error(err.to_string());
} }
} }
}
fn true_color(&self) -> bool { fn true_color(&self) -> bool {
self.config.load().editor.true_color || crate::true_color() self.config.load().editor.true_color || crate::true_color()

Loading…
Cancel
Save