on master help sign in
Fallback to broader scope if theme scope not found (#1714)
74a9dd5Gokul Soumya
helix-term/src/ui/markdown.rs+12−19
@@ -27,16 +27,15 @@ pub struct Markdown {
27// better yet, just use Tendril + subtendril for references
27// better yet, just use Tendril + subtendril for references
28
28
29impl Markdown {
29impl Markdown {
30− // theme keys, including fallbacks
31− const TEXT_STYLE: [&'static str; 2] = ["ui.text", "ui"];
30+ const TEXT_STYLE: &'static str = "ui.text";
32− const BLOCK_STYLE: [&'static str; 3] = ["markup.raw.inline", "markup.raw", "markup"];
31+ const BLOCK_STYLE: &'static str = "markup.raw.inline";
33− const HEADING_STYLES: [[&'static str; 3]; 6] = [
32+ const HEADING_STYLES: [&'static str; 6] = [
34− ["markup.heading.1", "markup.heading", "markup"],
33+ "markup.heading.1",
35− ["markup.heading.2", "markup.heading", "markup"],
34+ "markup.heading.2",
36− ["markup.heading.3", "markup.heading", "markup"],
35+ "markup.heading.3",
37− ["markup.heading.4", "markup.heading", "markup"],
36+ "markup.heading.4",
38− ["markup.heading.5", "markup.heading", "markup"],
37+ "markup.heading.5",
39− ["markup.heading.6", "markup.heading", "markup"],
38+ "markup.heading.6",
40 ];
39 ];
41
40
42 pub fn new(contents: String, config_loader: Arc<syntax::Loader>) -> Self {
41 pub fn new(contents: String, config_loader: Arc<syntax::Loader>) -> Self {
@@ -59,15 +58,9 @@ impl Markdown {
59 let mut spans = Vec::new();
58 let mut spans = Vec::new();
60 let mut lines = Vec::new();
59 let mut lines = Vec::new();
61
60
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(),
61+ let get_theme = |key: &str| -> Style { theme.map(|t| t.get(key)).unwrap_or_default() };
67− None => Default::default(),
68− };
69− let text_style = get_theme(&Self::TEXT_STYLE);
62+ let text_style = get_theme(Self::TEXT_STYLE);
70− let code_style = get_theme(&Self::BLOCK_STYLE);
63+ let code_style = get_theme(Self::BLOCK_STYLE);
71 let heading_styles: Vec<Style> = Self::HEADING_STYLES
64 let heading_styles: Vec<Style> = Self::HEADING_STYLES
72 .iter()
65 .iter()
73 .map(|key| get_theme(key))
66 .map(|key| get_theme(key))
helix-view/src/theme.rs+5−1
@@ -153,8 +153,12 @@ impl Theme {
153 self.try_get(scope).unwrap_or_default()
153 self.try_get(scope).unwrap_or_default()
154 }
154 }
155
155
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.
156 pub fn try_get(&self, scope: &str) -> Option<Style> {
159 pub fn try_get(&self, scope: &str) -> Option<Style> {
160+ std::iter::successors(Some(scope), |s| Some(s.rsplit_once('.')?.0))
157− self.styles.get(scope).copied()
161+ .find_map(|s| self.styles.get(s).copied())
158 }
162 }
159
163
160 #[inline]
164 #[inline]