Allow separate styles for markup headings (#1618)

* update markdown highlighting to use separate heading themes

* remove markdown theme scopes in ui
pull/1687/head
Alex 3 years ago committed by GitHub
parent 700058f433
commit d5ba0b5162
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -166,6 +166,8 @@ We use a similar set of scopes as
- `markup` - `markup`
- `heading` - `heading`
- `marker`
- `1`, `2`, `3`, `4`, `5`, `6` - heading text for h1 through h6
- `list` - `list`
- `unnumbered` - `unnumbered`
- `numbered` - `numbered`

@ -632,8 +632,7 @@ pub fn hover(cx: &mut Context) {
// skip if contents empty // skip if contents empty
let contents = let contents = ui::Markdown::new(contents, editor.syn_loader.clone());
ui::Markdown::new(contents, editor.syn_loader.clone()).style_group("hover");
let popup = Popup::new("hover", contents); let popup = Popup::new("hover", contents);
compositor.replace_or_push("hover", Box::new(popup)); compositor.replace_or_push("hover", Box::new(popup));
} }

@ -305,8 +305,6 @@ impl Component for Completion {
let coords = helix_core::visual_coords_at_pos(text, cursor_pos, doc.tab_width()); 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 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 { let mut markdown_doc = match &option.documentation {
Some(lsp::Documentation::String(contents)) Some(lsp::Documentation::String(contents))
| Some(lsp::Documentation::MarkupContent(lsp::MarkupContent { | Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
@ -314,7 +312,7 @@ impl Component for Completion {
value: contents, value: contents,
})) => { })) => {
// TODO: convert to wrapped text // TODO: convert to wrapped text
markdown_ui( Markdown::new(
format!( format!(
"```{}\n{}\n```\n{}", "```{}\n{}\n```\n{}",
language, language,
@ -329,7 +327,7 @@ impl Component for Completion {
value: contents, value: contents,
})) => { })) => {
// TODO: set language based on doc scope // TODO: set language based on doc scope
markdown_ui( Markdown::new(
format!( format!(
"```{}\n{}\n```\n{}", "```{}\n{}\n```\n{}",
language, language,
@ -343,7 +341,7 @@ impl Component for Completion {
// TODO: copied from above // TODO: copied from above
// TODO: set language based on doc scope // TODO: set language based on doc scope
markdown_ui( Markdown::new(
format!( format!(
"```{}\n{}\n```", "```{}\n{}\n```",
language, language,

@ -6,14 +6,14 @@ use tui::{
use std::sync::Arc; use std::sync::Arc;
use pulldown_cmark::{CodeBlockKind, CowStr, Event, Options, Parser, Tag}; use pulldown_cmark::{CodeBlockKind, CowStr, Event, HeadingLevel, Options, Parser, Tag};
use helix_core::{ use helix_core::{
syntax::{self, HighlightEvent, Syntax}, syntax::{self, HighlightEvent, Syntax},
Rope, Rope,
}; };
use helix_view::{ use helix_view::{
graphics::{Margin, Rect}, graphics::{Margin, Rect, Style},
Theme, Theme,
}; };
@ -21,28 +21,29 @@ pub struct Markdown {
contents: String, contents: String,
config_loader: Arc<syntax::Loader>, config_loader: Arc<syntax::Loader>,
block_style: String,
heading_style: String,
} }
// TODO: pre-render and self reference via Pin // TODO: pre-render and self reference via Pin
// better yet, just use Tendril + subtendril for references // better yet, just use Tendril + subtendril for references
impl Markdown { 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"],
];
pub fn new(contents: String, config_loader: Arc<syntax::Loader>) -> Self { pub fn new(contents: String, config_loader: Arc<syntax::Loader>) -> Self {
Self { Self {
contents, contents,
config_loader, config_loader,
block_style: "markup.raw.inline".into(),
heading_style: "markup.heading".into(),
}
} }
pub fn style_group(mut self, suffix: &str) -> Self {
self.block_style = format!("markup.raw.inline.{}", suffix);
self.heading_style = format!("markup.heading.{}", suffix);
self
} }
fn parse(&self, theme: Option<&Theme>) -> tui::text::Text<'_> { fn parse(&self, theme: Option<&Theme>) -> tui::text::Text<'_> {
@ -67,17 +68,19 @@ impl Markdown {
}) })
} }
macro_rules! get_theme { let get_theme = |keys: &[&str]| match theme {
($s1: expr) => { Some(theme) => keys
theme .iter()
.map(|theme| theme.try_get($s1.as_str())) .find_map(|key| theme.try_get(key))
.flatten() .unwrap_or_default(),
.unwrap_or_default() None => Default::default(),
}; };
} let text_style = get_theme(&Self::TEXT_STYLE);
let text_style = theme.map(|theme| theme.get("ui.text")).unwrap_or_default(); let code_style = get_theme(&Self::BLOCK_STYLE);
let code_style = get_theme!(self.block_style); let heading_styles: Vec<Style> = Self::HEADING_STYLES
let heading_style = get_theme!(self.heading_style); .iter()
.map(|key| get_theme(key))
.collect();
for event in parser { for event in parser {
match event { match event {
@ -172,9 +175,16 @@ impl Markdown {
lines.push(Spans::from(span)); lines.push(Spans::from(span));
} }
} }
} else if let Some(Tag::Heading(_, _, _)) = tags.last() { } else if let Some(Tag::Heading(level, _, _)) = tags.last() {
let mut span = to_span(text); let mut span = to_span(text);
span.style = heading_style; span.style = match level {
HeadingLevel::H1 => heading_styles[0],
HeadingLevel::H2 => heading_styles[1],
HeadingLevel::H3 => heading_styles[2],
HeadingLevel::H4 => heading_styles[3],
HeadingLevel::H5 => heading_styles[4],
HeadingLevel::H6 => heading_styles[5],
};
spans.push(span); spans.push(span);
} else { } else {
let mut span = to_span(text); let mut span = to_span(text);

@ -1,7 +1,12 @@
[ (setext_heading (heading_content) @markup.heading.1 (setext_h1_underline) @markup.heading.marker)
(atx_heading) (setext_heading (heading_content) @markup.heading.2 (setext_h2_underline) @markup.heading.marker)
(setext_heading)
] @markup.heading (atx_heading (atx_h1_marker) @markup.heading.marker (heading_content) @markup.heading.1)
(atx_heading (atx_h2_marker) @markup.heading.marker (heading_content) @markup.heading.2)
(atx_heading (atx_h3_marker) @markup.heading.marker (heading_content) @markup.heading.3)
(atx_heading (atx_h4_marker) @markup.heading.marker (heading_content) @markup.heading.4)
(atx_heading (atx_h5_marker) @markup.heading.marker (heading_content) @markup.heading.5)
(atx_heading (atx_h6_marker) @markup.heading.marker (heading_content) @markup.heading.6)
(code_fence_content) @none (code_fence_content) @none

Loading…
Cancel
Save