From 470d4ff3138b108f3dd64af81f439b931abbf2e1 Mon Sep 17 00:00:00 2001 From: LazyTanuki <43273245+lazytanuki@users.noreply.github.com> Date: Wed, 16 Nov 2022 23:49:13 +0100 Subject: [PATCH] wip: diagnostic icons now have the theme's default colors --- helix-term/src/application.rs | 4 ++-- helix-view/src/editor.rs | 6 ++++-- helix-view/src/icons.rs | 37 ++++++++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index d32637e4152a3..e2d393e8bc831 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -165,14 +165,14 @@ impl Application { .as_ref() .and_then(|icons| { icons_loader - .load(icons) + .load(icons, &theme) .map_err(|e| { log::warn!("failed to load icons `{}` - {}", icons, e); e }) .ok() }) - .unwrap_or_else(|| icons_loader.default()); + .unwrap_or_else(|| icons_loader.default(&theme)); let syn_loader = std::sync::Arc::new(syntax::Loader::new(syn_loader_conf)); diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index d10fed07f3ca5..efbb05ed3851d 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -774,6 +774,8 @@ impl Editor { ) -> Self { let conf = config.load(); let auto_pairs = (&conf.auto_pairs).into(); + let theme = theme_loader.default(); + let icons = icons_loader.default(&theme); // HAXX: offset the render area height by 1 to account for prompt/commandline area.height -= 1; @@ -790,10 +792,10 @@ impl Editor { selected_register: None, macro_recording: None, macro_replaying: Vec::new(), - theme: theme_loader.default(), + theme, language_servers: helix_lsp::Registry::new(), diagnostics: BTreeMap::new(), - icons: icons_loader.default(), + icons, debugger: None, debugger_events: SelectAll::new(), breakpoints: HashMap::new(), diff --git a/helix-view/src/icons.rs b/helix-view/src/icons.rs index 890585fa12e00..55c6ba960af0b 100644 --- a/helix-view/src/icons.rs +++ b/helix-view/src/icons.rs @@ -5,6 +5,7 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; use crate::graphics::{Color, Style}; +use crate::Theme; #[derive(Debug, Clone, PartialEq, Deserialize)] #[serde(rename_all = "kebab-case", deny_unknown_fields)] @@ -23,6 +24,12 @@ impl Icon { style: None, } } + + pub fn with_base_style(&mut self, style: Style) { + if self.style.is_none() { + self.style = Some(style); + } + } } #[derive(Debug, Clone, PartialEq, Deserialize)] @@ -33,6 +40,18 @@ pub struct Icons { pub symbol_kind: SymbolKind, } +impl Icons { + pub fn set_diagnostic_icons_base_style(mut self, theme: &Theme) -> Self { + self.diagnostic.error.with_base_style(theme.get("error")); + self.diagnostic.info.with_base_style(theme.get("info")); + self.diagnostic.hint.with_base_style(theme.get("hint")); + self.diagnostic + .warning + .with_base_style(theme.get("warning")); + self + } +} + #[derive(Debug, Clone, PartialEq, Deserialize)] #[serde(rename_all = "kebab-case", deny_unknown_fields)] pub struct Diagnostic { @@ -125,10 +144,11 @@ impl Loader { } } - /// Loads icons flavors first looking in the `user_dir` then in `default_dir` - pub fn load(&self, name: &str) -> Result { + /// Loads icons flavors first looking in the `user_dir` then in `default_dir`. + /// The `theme` is needed in order to load default styles for diagnostic icons. + pub fn load(&self, name: &str, theme: &Theme) -> Result { if name == "default" { - return Ok(self.default()); + return Ok(self.default(theme)); } let filename = format!("{}.toml", name); @@ -140,7 +160,9 @@ impl Loader { }; let data = std::fs::read(&path)?; - toml::from_slice(data.as_slice()).context("Failed to deserialize icon") + toml::from_slice(data.as_slice()) + .and_then(|icons: Icons| Ok(icons.set_diagnostic_icons_base_style(theme))) + .context("Failed to deserialize icon") } pub fn read_names(path: &Path) -> Vec { @@ -165,8 +187,9 @@ impl Loader { names } - /// Returns the default icon flavor - pub fn default(&self) -> Icons { - DEFAULT_ICONS.clone() + /// Returns the default icon flavor. + /// The `theme` is needed in order to load default styles for diagnostic icons. + pub fn default(&self, theme: &Theme) -> Icons { + DEFAULT_ICONS.clone().set_diagnostic_icons_base_style(theme) } }