Skip to content

Commit

Permalink
remove markdown theme scopes in ui
Browse files Browse the repository at this point in the history
  • Loading branch information
CptPotato committed Feb 13, 2022
1 parent a39c452 commit 997a3e7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 39 deletions.
3 changes: 1 addition & 2 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions helix-term/src/ui/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,14 @@ 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 {
kind: lsp::MarkupKind::PlainText,
value: contents,
})) => {
// TODO: convert to wrapped text
markdown_ui(
Markdown::new(
format!(
"```{}\n{}\n```\n{}",
language,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
53 changes: 21 additions & 32 deletions helix-term/src/ui/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,29 @@ pub struct Markdown {
contents: String,

config_loader: Arc<syntax::Loader>,

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 {
// theme keys, including fallbacks
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<syntax::Loader>) -> 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<'_> {
Expand All @@ -79,16 +68,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<Style> = self
.heading_styles
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))
.collect();
Expand Down

0 comments on commit 997a3e7

Please sign in to comment.