aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGokul Soumya2022-03-04 00:35:21 +0000
committerGitHub2022-03-04 00:35:21 +0000
commit74a9dd51ffb7cd3c14d4c7b5502e4febad24caa0 (patch)
tree57c03851f647a3fe5e1d613fe46bed835c77a7ec
parentc484b089230b541e63edc34a041e2842f2942e23 (diff)
Fallback to broader scope if theme scope not found (#1714)
-rw-r--r--helix-term/src/ui/markdown.rs31
-rw-r--r--helix-view/src/theme.rs6
2 files changed, 17 insertions, 20 deletions
diff --git a/helix-term/src/ui/markdown.rs b/helix-term/src/ui/markdown.rs
index e6f2316e..b01e7935 100644
--- a/helix-term/src/ui/markdown.rs
+++ b/helix-term/src/ui/markdown.rs
@@ -27,16 +27,15 @@ pub struct Markdown {
// better yet, just use Tendril + subtendril for references
impl Markdown {
- // theme keys, including fallbacks
- const TEXT_STYLE: [&'static str; 2] = ["ui.text", "ui"];
- const BLOCK_STYLE: [&'static str; 3] = ["markup.raw.inline", "markup.raw", "markup"];
- const HEADING_STYLES: [[&'static str; 3]; 6] = [
- ["markup.heading.1", "markup.heading", "markup"],
- ["markup.heading.2", "markup.heading", "markup"],
- ["markup.heading.3", "markup.heading", "markup"],
- ["markup.heading.4", "markup.heading", "markup"],
- ["markup.heading.5", "markup.heading", "markup"],
- ["markup.heading.6", "markup.heading", "markup"],
+ const TEXT_STYLE: &'static str = "ui.text";
+ const BLOCK_STYLE: &'static str = "markup.raw.inline";
+ const HEADING_STYLES: [&'static str; 6] = [
+ "markup.heading.1",
+ "markup.heading.2",
+ "markup.heading.3",
+ "markup.heading.4",
+ "markup.heading.5",
+ "markup.heading.6",
];
pub fn new(contents: String, config_loader: Arc<syntax::Loader>) -> Self {
@@ -59,15 +58,9 @@ impl Markdown {
let mut spans = Vec::new();
let mut lines = Vec::new();
- let get_theme = |keys: &[&str]| match theme {
- Some(theme) => keys
- .iter()
- .find_map(|key| theme.try_get(key))
- .unwrap_or_default(),
- None => Default::default(),
- };
- let text_style = get_theme(&Self::TEXT_STYLE);
- let code_style = get_theme(&Self::BLOCK_STYLE);
+ let get_theme = |key: &str| -> Style { theme.map(|t| t.get(key)).unwrap_or_default() };
+ let text_style = get_theme(Self::TEXT_STYLE);
+ let code_style = get_theme(Self::BLOCK_STYLE);
let heading_styles: Vec<Style> = Self::HEADING_STYLES
.iter()
.map(|key| get_theme(key))
diff --git a/helix-view/src/theme.rs b/helix-view/src/theme.rs
index 00c1bbbd..3f45aac6 100644
--- a/helix-view/src/theme.rs
+++ b/helix-view/src/theme.rs
@@ -153,8 +153,12 @@ impl Theme {
self.try_get(scope).unwrap_or_default()
}
+ /// Get the style of a scope, falling back to dot separated broader
+ /// scopes. For example if `ui.text.focus` is not defined in the theme,
+ /// `ui.text` is tried and then `ui` is tried.
pub fn try_get(&self, scope: &str) -> Option<Style> {
- self.styles.get(scope).copied()
+ std::iter::successors(Some(scope), |s| Some(s.rsplit_once('.')?.0))
+ .find_map(|s| self.styles.get(s).copied())
}
#[inline]