diff options
author | Blaž Hrastnik | 2022-02-21 07:32:51 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2022-02-21 07:47:14 +0000 |
commit | 1ca6ba03cae040111154c96189c4aacf3126bfc8 (patch) | |
tree | 0e5a4411f43d70495e0d6fb868c5e9dbf911a010 | |
parent | d5ba0b516263f08116c43eb0796b1027b094dabd (diff) |
Simplify some code
-rw-r--r-- | helix-term/src/commands.rs | 2 | ||||
-rw-r--r-- | helix-term/src/ui/markdown.rs | 65 |
2 files changed, 32 insertions, 35 deletions
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 982ae013..4cded7ef 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4790,7 +4790,7 @@ fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) { let reg = cx.register.unwrap_or('/'); let prompt = ui::regex_prompt( cx, - if !remove { "keep:" } else { "remove:" }.into(), + if remove { "remove:" } else { "keep:" }.into(), Some(reg), ui::completers::none, move |view, doc, regex, event| { diff --git a/helix-term/src/ui/markdown.rs b/helix-term/src/ui/markdown.rs index bcb63c29..d1835be6 100644 --- a/helix-term/src/ui/markdown.rs +++ b/helix-term/src/ui/markdown.rs @@ -6,7 +6,7 @@ use tui::{ use std::sync::Arc; -use pulldown_cmark::{CodeBlockKind, CowStr, Event, HeadingLevel, Options, Parser, Tag}; +use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag}; use helix_core::{ syntax::{self, HighlightEvent, Syntax}, @@ -59,15 +59,6 @@ impl Markdown { let mut spans = Vec::new(); let mut lines = Vec::new(); - fn to_span(text: pulldown_cmark::CowStr) -> Span { - use std::ops::Deref; - Span::raw::<std::borrow::Cow<_>>(match text { - CowStr::Borrowed(s) => s.into(), - CowStr::Boxed(s) => s.to_string().into(), - CowStr::Inlined(s) => s.deref().to_owned().into(), - }) - } - let get_theme = |keys: &[&str]| match theme { Some(theme) => keys .iter() @@ -82,15 +73,28 @@ impl Markdown { .map(|key| get_theme(key)) .collect(); + let mut list_stack = Vec::new(); + for event in parser { match event { - Event::Start(tag) => tags.push(tag), + Event::Start(Tag::List(list)) => list_stack.push(list), + Event::End(Tag::List(_)) => { + list_stack.pop(); + } + Event::Start(Tag::Item) => { + tags.push(Tag::Item); + spans.push(Span::from("- ")); + } + Event::Start(tag) => { + tags.push(tag); + } Event::End(tag) => { tags.pop(); match tag { Tag::Heading(_, _, _) | Tag::Paragraph - | Tag::CodeBlock(CodeBlockKind::Fenced(_)) => { + | Tag::CodeBlock(CodeBlockKind::Fenced(_)) + | Tag::Item => { // whenever code block or paragraph closes, new line let spans = std::mem::take(&mut spans); if !spans.is_empty() { @@ -132,8 +136,6 @@ impl Markdown { None => text_style, }; - // TODO: replace tabs with indentation - let mut slice = &text[start..end]; // TODO: do we need to handle all unicode line endings // here, or is just '\n' okay? @@ -175,27 +177,24 @@ impl Markdown { lines.push(Spans::from(span)); } } - } else if let Some(Tag::Heading(level, _, _)) = tags.last() { - let mut span = to_span(text); - 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); } else { - let mut span = to_span(text); - span.style = text_style; - spans.push(span); + let style = if let Some(Tag::Heading(level, ..)) = tags.last() { + 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], + } + } else { + text_style + }; + spans.push(Span::styled(text, style)); } } Event::Code(text) | Event::Html(text) => { - let mut span = to_span(text); - span.style = code_style; - spans.push(span); + spans.push(Span::styled(text, code_style)); } Event::SoftBreak | Event::HardBreak => { // let spans = std::mem::replace(&mut spans, Vec::new()); @@ -203,9 +202,7 @@ impl Markdown { spans.push(Span::raw(" ")); } Event::Rule => { - let mut span = Span::raw("---"); - span.style = code_style; - lines.push(Spans::from(span)); + lines.push(Spans::from(Span::styled("---", code_style))); lines.push(Spans::default()); } // TaskListMarker(bool) true if checked |