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,38 +397,49 @@ impl Application {
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
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 {
anyhow::bail!("theme requires truecolor support, which is not available")
}
}
Ok(())
}
fn refresh_config(&mut self) {
match Config::load_default() {
Ok(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());
}
}

Loading…
Cancel
Save