Skip to content

Commit

Permalink
Refactor highlight scope fallback for markdown popup
Browse files Browse the repository at this point in the history
  • Loading branch information
sudormrfbin committed Feb 27, 2022
1 parent 6a6a9ab commit 8169887
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
28 changes: 11 additions & 17 deletions helix-term/src/ui/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -59,13 +58,8 @@ 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 get_theme =
|key: &str| -> Style { theme.map(|t| t.get_with_fallback(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
Expand Down
10 changes: 10 additions & 0 deletions helix-view/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ impl Theme {
self.styles.get(scope).copied()
}

/// 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. If none of the fallbacks
/// are found, the default style is returned.
pub fn get_with_fallback<'s>(&self, scope: &'s str) -> Style {
std::iter::successors(Some(scope), |s| Some(s.rsplit_once('.')?.0))
.find_map(|s| self.try_get(s))
.unwrap_or_default()
}

#[inline]
pub fn scopes(&self) -> &[String] {
&self.scopes
Expand Down

0 comments on commit 8169887

Please sign in to comment.