From ca163ad00358ee163162fe3c1e9057770bbe8aaf Mon Sep 17 00:00:00 2001 From: CptPotato <3957610+CptPotato@users.noreply.github.com> Date: Sun, 13 Feb 2022 16:35:32 +0100 Subject: [PATCH] remove markdown theme scopes in ui --- helix-term/src/commands.rs | 3 +- helix-term/src/ui/completion.rs | 8 ++--- helix-term/src/ui/markdown.rs | 52 +++++++++++++-------------------- 3 files changed, 24 insertions(+), 39 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 6bc2b9b6c29b0..60709d0563a8a 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -5459,8 +5459,7 @@ fn hover(cx: &mut Context) { // skip if contents empty - let contents = - ui::Markdown::new(contents, editor.syn_loader.clone()).style_group("hover"); + let contents = ui::Markdown::new(contents, editor.syn_loader.clone()); let popup = Popup::new("hover", contents); if let Some(doc_popup) = compositor.find_id("hover") { *doc_popup = popup; diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index 35afe81e9ca47..d3b618a915aba 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -305,8 +305,6 @@ impl Component for Completion { let coords = helix_core::visual_coords_at_pos(text, cursor_pos, doc.tab_width()); let cursor_pos = (coords.row - view.offset.row) as u16; - let markdown_ui = - |content, syn_loader| Markdown::new(content, syn_loader).style_group("completion"); let mut markdown_doc = match &option.documentation { Some(lsp::Documentation::String(contents)) | Some(lsp::Documentation::MarkupContent(lsp::MarkupContent { @@ -314,7 +312,7 @@ impl Component for Completion { value: contents, })) => { // TODO: convert to wrapped text - markdown_ui( + Markdown::new( format!( "```{}\n{}\n```\n{}", language, @@ -329,7 +327,7 @@ impl Component for Completion { value: contents, })) => { // TODO: set language based on doc scope - markdown_ui( + Markdown::new( format!( "```{}\n{}\n```\n{}", language, @@ -343,7 +341,7 @@ impl Component for Completion { // TODO: copied from above // TODO: set language based on doc scope - markdown_ui( + Markdown::new( format!( "```{}\n{}\n```", language, diff --git a/helix-term/src/ui/markdown.rs b/helix-term/src/ui/markdown.rs index 68fc3555e7d05..7eaaf6e0b531d 100644 --- a/helix-term/src/ui/markdown.rs +++ b/helix-term/src/ui/markdown.rs @@ -21,40 +21,28 @@ pub struct Markdown { contents: String, config_loader: Arc, - - text_style: String, - block_style: String, - heading_styles: [String; 6], } // TODO: pre-render and self reference via Pin // better yet, just use Tendril + subtendril for references impl Markdown { + const TEXT_STYLE: [&'static str; 2] = ["markup.normal", "markup"]; + 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"], + ]; + pub fn new(contents: String, config_loader: Arc) -> Self { Self { contents, config_loader, - text_style: "markup.normal".into(), - block_style: "markup.raw.inline".into(), - heading_styles: [ - "markup.heading.1".into(), - "markup.heading.2".into(), - "markup.heading.3".into(), - "markup.heading.4".into(), - "markup.heading.5".into(), - "markup.heading.6".into(), - ], - } - } - - pub fn style_group(mut self, suffix: &str) -> Self { - self.text_style = format!("markup.normal.{}", suffix); - self.block_style = format!("markup.raw.inline.{}", suffix); - for i in 0..self.heading_styles.len() { - self.heading_styles[i] = format!("markup.heading.{}.{}", i + 1, suffix); } - self } fn parse(&self, theme: Option<&Theme>) -> tui::text::Text<'_> { @@ -79,16 +67,16 @@ impl Markdown { }) } - let get_theme = |key: &str| { - theme - .map(|theme| theme.try_get(key)) - .flatten() - .unwrap_or_default() + 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 heading_styles: Vec