on master help sign in
Simplify some code
commit1ca6ba03cae040111154c96189c4aacf3126bfc8 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 21 Feb 2022, 16:32 (5 years ago) parentd5ba0b5
2 files changed+32−35
helix-term/src/commands.rs+1−1 helix-term/src/ui/markdown.rs+31−34
helix-term/src/commands.rs+1−1
@@ -4790,7 +4790,7 @@ fn keep_or_remove_selections_impl(cx: &mut Context, remove: bool) {
47904790 let reg = cx.register.unwrap_or('/');
47914791 let prompt = ui::regex_prompt(
47924792 cx,
4793− if !remove { "keep:" } else { "remove:" }.into(),
4793+ if remove { "remove:" } else { "keep:" }.into(),
47944794 Some(reg),
47954795 ui::completers::none,
47964796 move |view, doc, regex, event| {
helix-term/src/ui/markdown.rs+31−34
@@ -6,7 +6,7 @@ use tui::{
66
77use std::sync::Arc;
88
9−use pulldown_cmark::{CodeBlockKind, CowStr, Event, HeadingLevel, Options, Parser, Tag};
9+use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Options, Parser, Tag};
1010
1111use helix_core::{
1212 syntax::{self, HighlightEvent, Syntax},
@@ -59,15 +59,6 @@ impl Markdown {
5959 let mut spans = Vec::new();
6060 let mut lines = Vec::new();
6161
62− fn to_span(text: pulldown_cmark::CowStr) -> Span {
63− use std::ops::Deref;
64− Span::raw::<std::borrow::Cow<_>>(match text {
65− CowStr::Borrowed(s) => s.into(),
66− CowStr::Boxed(s) => s.to_string().into(),
67− CowStr::Inlined(s) => s.deref().to_owned().into(),
68− })
69− }
70−
7162 let get_theme = |keys: &[&str]| match theme {
7263 Some(theme) => keys
7364 .iter()
@@ -82,15 +73,28 @@ impl Markdown {
8273 .map(|key| get_theme(key))
8374 .collect();
8475
76+ let mut list_stack = Vec::new();
77+
8578 for event in parser {
8679 match event {
87− Event::Start(tag) => tags.push(tag),
80+ Event::Start(Tag::List(list)) => list_stack.push(list),
81+ Event::End(Tag::List(_)) => {
82+ list_stack.pop();
83+ }
84+ Event::Start(Tag::Item) => {
85+ tags.push(Tag::Item);
86+ spans.push(Span::from("- "));
87+ }
88+ Event::Start(tag) => {
89+ tags.push(tag);
90+ }
8891 Event::End(tag) => {
8992 tags.pop();
9093 match tag {
9194 Tag::Heading(_, _, _)
9295 | Tag::Paragraph
93− | Tag::CodeBlock(CodeBlockKind::Fenced(_)) => {
96+ | Tag::CodeBlock(CodeBlockKind::Fenced(_))
97+ | Tag::Item => {
9498 // whenever code block or paragraph closes, new line
9599 let spans = std::mem::take(&mut spans);
96100 if !spans.is_empty() {
@@ -132,8 +136,6 @@ impl Markdown {
132136 None => text_style,
133137 };
134138
135− // TODO: replace tabs with indentation
136−
137139 let mut slice = &text[start..end];
138140 // TODO: do we need to handle all unicode line endings
139141 // here, or is just '\n' okay?
@@ -175,27 +177,24 @@ impl Markdown {
175177 lines.push(Spans::from(span));
176178 }
177179 }
178− } else if let Some(Tag::Heading(level, _, _)) = tags.last() {
179− let mut span = to_span(text);
180− span.style = match level {
181− HeadingLevel::H1 => heading_styles[0],
182− HeadingLevel::H2 => heading_styles[1],
183− HeadingLevel::H3 => heading_styles[2],
184− HeadingLevel::H4 => heading_styles[3],
185− HeadingLevel::H5 => heading_styles[4],
186− HeadingLevel::H6 => heading_styles[5],
187− };
188− spans.push(span);
189180 } else {
190− let mut span = to_span(text);
191− span.style = text_style;
192− spans.push(span);
181+ let style = if let Some(Tag::Heading(level, ..)) = tags.last() {
182+ match level {
183+ HeadingLevel::H1 => heading_styles[0],
184+ HeadingLevel::H2 => heading_styles[1],
185+ HeadingLevel::H3 => heading_styles[2],
186+ HeadingLevel::H4 => heading_styles[3],
187+ HeadingLevel::H5 => heading_styles[4],
188+ HeadingLevel::H6 => heading_styles[5],
189+ }
190+ } else {
191+ text_style
192+ };
193+ spans.push(Span::styled(text, style));
193194 }
194195 }
195196 Event::Code(text) | Event::Html(text) => {
196− let mut span = to_span(text);
197− span.style = code_style;
198− spans.push(span);
197+ spans.push(Span::styled(text, code_style));
199198 }
200199 Event::SoftBreak | Event::HardBreak => {
201200 // let spans = std::mem::replace(&mut spans, Vec::new());
@@ -203,9 +202,7 @@ impl Markdown {
203202 spans.push(Span::raw(" "));
204203 }
205204 Event::Rule => {
206− let mut span = Span::raw("---");
207− span.style = code_style;
208− lines.push(Spans::from(span));
205+ lines.push(Spans::from(Span::styled("---", code_style)));
209206 lines.push(Spans::default());
210207 }
211208 // TaskListMarker(bool) true if checked