on master help sign in
Fallback to broader scope if theme scope not found (#1714)
commit74a9dd51ffb7cd3c14d4c7b5502e4febad24caa0 authorGokul Soumya <gokulps15@gmail.com> dateFri 4 Mar 2022, 06:05 (5 years ago) parentc484b08
2 files changed+17−20
helix-term/src/ui/markdown.rs+12−19 helix-view/src/theme.rs+5−1
helix-term/src/ui/markdown.rs+12−19
@@ -27,16 +27,15 @@ pub struct Markdown {
2727// better yet, just use Tendril + subtendril for references
2828
2929impl Markdown {
30− // theme keys, including fallbacks
31− const TEXT_STYLE: [&'static str; 2] = ["ui.text", "ui"];
32− const BLOCK_STYLE: [&'static str; 3] = ["markup.raw.inline", "markup.raw", "markup"];
33− const HEADING_STYLES: [[&'static str; 3]; 6] = [
34− ["markup.heading.1", "markup.heading", "markup"],
35− ["markup.heading.2", "markup.heading", "markup"],
36− ["markup.heading.3", "markup.heading", "markup"],
37− ["markup.heading.4", "markup.heading", "markup"],
38− ["markup.heading.5", "markup.heading", "markup"],
39− ["markup.heading.6", "markup.heading", "markup"],
30+ const TEXT_STYLE: &'static str = "ui.text";
31+ const BLOCK_STYLE: &'static str = "markup.raw.inline";
32+ const HEADING_STYLES: [&'static str; 6] = [
33+ "markup.heading.1",
34+ "markup.heading.2",
35+ "markup.heading.3",
36+ "markup.heading.4",
37+ "markup.heading.5",
38+ "markup.heading.6",
4039 ];
4140
4241 pub fn new(contents: String, config_loader: Arc<syntax::Loader>) -> Self {
@@ -59,15 +58,9 @@ impl Markdown {
5958 let mut spans = Vec::new();
6059 let mut lines = Vec::new();
6160
62− let get_theme = |keys: &[&str]| match theme {
63− Some(theme) => keys
64− .iter()
65− .find_map(|key| theme.try_get(key))
66− .unwrap_or_default(),
67− None => Default::default(),
68− };
69− let text_style = get_theme(&Self::TEXT_STYLE);
70− let code_style = get_theme(&Self::BLOCK_STYLE);
61+ let get_theme = |key: &str| -> Style { theme.map(|t| t.get(key)).unwrap_or_default() };
62+ let text_style = get_theme(Self::TEXT_STYLE);
63+ let code_style = get_theme(Self::BLOCK_STYLE);
7164 let heading_styles: Vec<Style> = Self::HEADING_STYLES
7265 .iter()
7366 .map(|key| get_theme(key))
helix-view/src/theme.rs+5−1
@@ -153,8 +153,12 @@ impl Theme {
153153 self.try_get(scope).unwrap_or_default()
154154 }
155155
156+ /// Get the style of a scope, falling back to dot separated broader
157+ /// scopes. For example if `ui.text.focus` is not defined in the theme,
158+ /// `ui.text` is tried and then `ui` is tried.
156159 pub fn try_get(&self, scope: &str) -> Option<Style> {
157− self.styles.get(scope).copied()
160+ std::iter::successors(Some(scope), |s| Some(s.rsplit_once('.')?.0))
161+ .find_map(|s| self.styles.get(s).copied())
158162 }
159163
160164 #[inline]