Skip to content

Commit

Permalink
wip: diagnostic icons now have the theme's default colors
Browse files Browse the repository at this point in the history
  • Loading branch information
lazytanuki committed Nov 16, 2022
1 parent 89a346c commit 470d4ff
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
4 changes: 2 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
6 changes: 4 additions & 2 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand Down
37 changes: 30 additions & 7 deletions helix-view/src/icons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Icons, anyhow::Error> {
/// 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<Icons, anyhow::Error> {
if name == "default" {
return Ok(self.default());
return Ok(self.default(theme));
}
let filename = format!("{}.toml", name);

Expand All @@ -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<String> {
Expand All @@ -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)
}
}

0 comments on commit 470d4ff

Please sign in to comment.